sample code - “connect”

usage:

  • for SSH connection(Access Authen.Settings=On)

fingerprint: FBfvXPEdXHymp1QmcGTfegHgyeObiQoxPTY+C98PY1k=
id:test
password:xxxxxxxx
Connected to ILX-LR1 (C8:4D:44:10:75:5D)
  • for pairing connection

    1. connect camera to network, [Access Authen.Settings=Off], [PC Remote=On]

    2. (camera) menu - [Network] - [Cnct./PC Remote] - [PC Remote Function] - [Pairing]

    3. (PC) Launch sample app.

    4. (camera) [Allow paring with the device?] - [OK]

This sample code is included in SimpleCli.

  1// connect sample
  2#include <chrono>
  3#include <cstdint>
  4#include <cstdlib>
  5#include <fstream>
  6#include <future>
  7#include <iostream>
  8#include <mutex>
  9#include <stdexcept>
 10#include <string>
 11#include <thread>
 12
 13#if !defined(__APPLE__)
 14  #if defined(USE_EXPERIMENTAL_FS) // for jetson
 15    #include <experimental/filesystem>
 16    namespace fs = std::experimental::filesystem;
 17  #else
 18    #include <filesystem>
 19    namespace fs = std::filesystem;
 20  #endif
 21#endif
 22
 23#if defined(__APPLE__) || defined(__linux__)
 24  #include <unistd.h>
 25#endif
 26
 27// macro for multibyte character
 28#if defined(_WIN32) || defined(_WIN64)
 29  using CrString = std::wstring;
 30  #define CRSTR(s) L ## s
 31  #define CrCout std::wcout
 32#else
 33  using CrString = std::string;
 34  #define CRSTR(s) s
 35  #define CrCout std::cout
 36#endif
 37
 38
 39#include "CrDeviceProperty.h"
 40#include "CameraRemote_SDK.h"
 41#include "IDeviceCallback.h"
 42
 43#define PrintError(msg, err) { fprintf(stderr, "Error in %s(%d):" msg ",0x%x\n", __FUNCTION__, __LINE__, err); }
 44#define GotoError(msg, err) { PrintError(msg, err); goto Error; }
 45
 46bool  m_connected = false;
 47CrString m_modelId;
 48
 49std::mutex m_eventPromiseMutex;
 50std::promise<void>* m_eventPromise = nullptr;
 51void setEventPromise(std::promise<void>* dp)
 52{
 53    std::lock_guard<std::mutex> lock(m_eventPromiseMutex);
 54    m_eventPromise = dp;
 55}
 56
 57class DeviceCallback : public SCRSDK::IDeviceCallback
 58{
 59public:
 60    DeviceCallback() {};
 61    ~DeviceCallback() {};
 62
 63    void OnConnected(SCRSDK::DeviceConnectionVersioin version)
 64    {
 65        CrCout << "Connected to " << m_modelId << "\n";
 66        m_connected = true;
 67        std::lock_guard<std::mutex> lock(m_eventPromiseMutex);
 68        if(m_eventPromise) {
 69            m_eventPromise->set_value();
 70            m_eventPromise = nullptr;
 71        }
 72    }
 73
 74    void OnError(CrInt32u error)
 75    {
 76        printf("Connection error 0x%x\n", error);
 77        std::lock_guard<std::mutex> lock(m_eventPromiseMutex);
 78        if(m_eventPromise) {
 79            m_eventPromise->set_exception(std::make_exception_ptr(std::runtime_error("error")));
 80            m_eventPromise = nullptr;
 81        }
 82    }
 83
 84    void OnDisconnected(CrInt32u error)
 85    {
 86        CrCout << "Disconnected from " << m_modelId << "\n";
 87        m_connected = false;
 88        std::lock_guard<std::mutex> lock(m_eventPromiseMutex);
 89        if(m_eventPromise) {
 90            m_eventPromise->set_value();
 91            m_eventPromise = nullptr;
 92        }
 93    }
 94
 95    void OnCompleteDownload(CrChar* filename, CrInt32u type )
 96    {
 97        CrCout << "OnCompleteDownload:" << filename << "\n";
 98    }
 99
100    void OnNotifyContentsTransfer(CrInt32u notify, SCRSDK::CrContentHandle contentHandle, CrChar* filename)
101    {
102        std::cout << "OnNotifyContentsTransfer.\n";
103    }
104
105    void OnWarning(CrInt32u warning)
106    {
107        if (warning == SCRSDK::CrWarning_Connect_Reconnecting) {
108            CrCout << "Reconnecting to " << m_modelId << "\n";
109            return;
110        }
111    }
112
113    void OnWarningExt(CrInt32u warning, CrInt32 param1, CrInt32 param2, CrInt32 param3) {}
114    void OnLvPropertyChanged() {}
115    void OnLvPropertyChangedCodes(CrInt32u num, CrInt32u* codes) {}
116    void OnPropertyChanged() {}
117    void OnPropertyChangedCodes(CrInt32u num, CrInt32u* codes) {}
118};
119
120CrString _getModelId(const SCRSDK::ICrCameraObjectInfo* objInfo)
121{
122    CrString id;
123    if (CrString(objInfo->GetConnectionTypeName()) == CRSTR("IP")) {
124        id = CrString(objInfo->GetMACAddressChar());
125    } else {
126        id = CrString((CrChar*)objInfo->GetId());
127    }
128    return CrString(objInfo->GetModel()).append(CRSTR(" (")).append(id).append(CRSTR(")"));
129}
130
131SCRSDK::CrError _getIdPassword(SCRSDK::ICrCameraObjectInfo* objInfo, std::string& fingerprint, std::string& userId, std::string& userPassword)
132{
133    char fpBuff[128] = {0};
134    CrInt32u fpLen = 0;
135    SCRSDK::CrError err = SCRSDK::GetFingerprint(objInfo, fpBuff, &fpLen);
136    if(err) GotoError("", err);
137    fingerprint = std::string(fpBuff, fpLen);
138
139    std::cout << "fingerprint: " << fingerprint.c_str() << "\n";
140    std::cout << "id:";       std::getline(std::cin, userId);
141    std::cout << "password:"; std::getline(std::cin, userPassword);
142    return 0;
143Error:
144    return err;
145}
146
147int main(void)
148{
149    int result = -1;
150    SCRSDK::CrError err = SCRSDK::CrError_None;
151    int64_t  m_device_handle = 0;
152    SCRSDK::ICrEnumCameraObjectInfo* enumCameraObjectInfo = nullptr;
153    SCRSDK::ICrCameraObjectInfo* objInfo = nullptr;
154    DeviceCallback deviceCallback;
155
156  #if defined(__APPLE__)
157    #define MAC_MAX_PATH 255
158    char pathBuf[MAC_MAX_PATH] = {0};
159    if(NULL == getcwd(pathBuf, sizeof(pathBuf) - 1)) return 1;
160    CrString path = pathBuf;
161  #else
162    CrString path = fs::current_path().native();
163  #endif
164
165    bool boolRet = SCRSDK::Init();
166    if(!boolRet) GotoError("", 0);
167
168    // enumeration
169    {
170        uint32_t count = 0;
171        uint32_t index = 1;
172
173        err = SCRSDK::EnumCameraObjects(&enumCameraObjectInfo, 3/*timeInSec*/);
174        if(err || !enumCameraObjectInfo) GotoError("no camera", err);
175
176        count = enumCameraObjectInfo->GetCount();
177        if(count >= 2) {
178            for (uint32_t i = 0; i < count; ++i) {
179                auto* info = enumCameraObjectInfo->GetCameraObjectInfo(i);
180                CrCout << '[' << i + 1 << "] " << _getModelId(info) << "\n";
181            }
182
183            std::string inputLine;
184            std::cout << "select camera:"; std::getline(std::cin, inputLine);
185            try { index = stoi(inputLine); } catch(const std::exception&) { GotoError("", 0); }
186            if(index < 1 || index > count) GotoError("", 0);
187        }
188        objInfo = (SCRSDK::ICrCameraObjectInfo*)enumCameraObjectInfo->GetCameraObjectInfo(index - 1);
189        m_modelId = _getModelId(objInfo);
190    }
191
192    // connect
193    {
194        std::string  fingerprint = "";
195        std::string  userId = "";
196        std::string  userPassword = "";
197        std::promise<void> eventPromise;
198        std::future<void> eventFuture = eventPromise.get_future();
199
200        if (objInfo->GetSSHsupport() == SCRSDK::CrSSHsupport_ON) {
201            err = _getIdPassword(objInfo, fingerprint, userId, userPassword); if(err) goto Error;
202        }
203
204        setEventPromise(&eventPromise);
205        err = SCRSDK::Connect(objInfo, &deviceCallback, &m_device_handle,
206            SCRSDK::CrSdkControlMode_Remote,
207            SCRSDK::CrReconnecting_ON,
208            userId.c_str(), userPassword.c_str(), fingerprint.c_str(), (uint32_t)fingerprint.size());
209        if(err) GotoError("", err);
210
211    //  std::future_status status = eventFuture.wait_for(std::chrono::milliseconds(3000));
212    //  if(status != std::future_status::ready) GotoError("timeout",0);
213        try{
214            eventFuture.get();
215        } catch(const std::exception&) GotoError("", 0);
216    }
217
218    // set work directory
219    {
220        CrCout << "path=" << path.data() << "\n";
221        err = SCRSDK::SetSaveInfo(m_device_handle, const_cast<CrChar*>(path.data()), const_cast<CrChar*>(CRSTR("DSC")), -1/*startNo*/);
222        if(err) GotoError("", err);
223    }
224
225    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
226
227    std::cout << "usage:\n";
228    std::cout << "To exit, please enter 'q'.\n";
229
230    while(1) {
231        std::string inputLine;
232        std::getline(std::cin, inputLine);
233        if(inputLine == "q" || inputLine == "Q") {
234            break;
235        }
236        std::cout << "unknown DP nor CMD\n";
237    }
238
239    result = 0;
240Error:
241    if(enumCameraObjectInfo) enumCameraObjectInfo->Release();
242
243    if(m_connected) {
244        std::promise<void> eventPromise;
245        std::future<void> eventFuture = eventPromise.get_future();
246        setEventPromise(&eventPromise);
247        SCRSDK::Disconnect(m_device_handle);
248        eventFuture.wait_for(std::chrono::milliseconds(3000));
249    }
250    if(m_device_handle) SCRSDK::ReleaseDevice(m_device_handle);
251    SCRSDK::Release();
252
253    return result;
254}