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