-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
404 lines (343 loc) · 10 KB
/
Copy pathmain.cpp
File metadata and controls
404 lines (343 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* Set camera See3Cam_CU20 to HW trigger.
* https://github.com/econsysqtcam/qtcam/blob/master/src/see3cam_cu20.cpp
* Author: Martin Sakin
* Date: 2022-03-17
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/hidraw.h>
#include <libudev.h>
#include <errno.h>
#include <linux/videodev2.h>
#include <iostream>
#define SUCCESS 0
#define FAILURE -1
#define TRUE 1
#define FALSE 0
#define CAMERA_CONTROL_CU20 0x86
#define GET_CAMERA_MODE_CU20 0x03
#define SET_CAMERA_MODE_CU20 0x04
#define SET_FAIL 0x00
#define SET_SUCCESS 0x01
#define GET_FAIL 0x00
#define GET_SUCCESS 0x01
#define BUFFER_LENGTH 65
#define MAX_NUM_OF_CAMERAS 20
#define MAX_LEN_SERIAL_NUM 16
const char* ECON_VID = "2560";
const char* CAMERA_PID = "c120"; // PID of SEE3CAM_CU20
char *hid_device;
unsigned char g_out_packet_buf[BUFFER_LENGTH];
unsigned char g_in_packet_buf[BUFFER_LENGTH];
unsigned int econdev_count = 0;
unsigned int dev_node[10];
char dev_name[64];
// Config file
int option = 1;
int stream_mode = 0;
int take_frame = 0;
int stillformatId = 1;
int stillresolutionId = 3;
int cameraMode = 0;
// One camera device is shown twice, so by serial number eliminate duplicity
char serial_nums[MAX_NUM_OF_CAMERAS][MAX_LEN_SERIAL_NUM];
using namespace std;
void loadConfig(char * filename) {
char * line = NULL;
size_t len = 0;
ssize_t read;
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
cout << "Error! Could not open file: " << filename << endl;
exit(2);
}
while ((read = getline(&line, &len, fp)) != -1) {
if (line[0] == ';' || read <= 1) { // ignore comment
continue;
}
char *token = strtok(line, "=");
char *value = strtok(NULL, "=");
/*if (strcmp(token, "input") == 0) {
strncpy(input, value, strlen(value)-1);
}*/
if (strcmp(token, "option") == 0) {
option = strtol(value, NULL, 10);
}
else if (strcmp(token, "stream_mode") == 0) {
stream_mode = strtol(value, NULL, 10);
}
else if (strcmp(token, "stillformatId") == 0) {
stillformatId = strtol(value, NULL, 10);
}
else if (strcmp(token, "stillresolutionId") == 0) {
stillresolutionId = strtol(value, NULL, 10);
}
}
fclose(fp);
cout << "Config file loaded: " << filename << endl << flush;
}
void help(){
printf("HELP:\n"
" --help\t\t\t print this help\n"
" --config filename.ini\t load setting from config file\n"
" on\t\t\t turn on wh trigger\n"
" off\t\t\t turn off wh trigger\n"
" get\t\t\t get info about hw trigger\n"
);
exit(0);
}
void parseAgrs(int argc, char *argv[]) {
if (strcmp(argv[1], "--config") == 0 || strcmp(argv[1], "-c") == 0) {
loadConfig(argv[2]);
}
else if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
help();
}
else if (strcmp(argv[1], "on") == 0 || strcmp(argv[1], "ON") == 0) {
cameraMode = 2;
}
else if (strcmp(argv[1], "off") == 0 || strcmp(argv[1], "OFF") == 0) {
cameraMode = 1;
}
else if (strcmp(argv[1], "get") == 0 || strcmp(argv[1], "GET") == 0) {
cameraMode = 0;
}
else {
help();
}
}
//==============================================================================
void close_hid(int hid_fd) {
if(hid_fd) {
close(hid_fd);
}
}
int is_in_list(const char *serial_num) {
for (unsigned i = 0; i < econdev_count; i ++) {
if (strcmp(serial_nums[i], serial_num) == 0) {
return TRUE;
}
}
return FALSE;
}
/* brief description on initExtensionUnit -Enumerating devices and getting hidnode
* param parameter - string parameter which contains hid type
* return success/failure
*/
int initExtensionUnit(char * parameter) {
struct udev *udev;
struct udev_enumerate *enumerate;
struct udev_list_entry *devices, *dev_list_entry;
struct udev_device *dev,*pdev;
int hid_fd;
// Create the udev object
udev = udev_new();
if (!udev) {
return FAILURE;
}
// Create a list of the devices in the 'video4linux/hidraw' subsystem.
enumerate = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(enumerate, parameter);
udev_enumerate_scan_devices(enumerate);
devices = udev_enumerate_get_list_entry(enumerate);
udev_list_entry_foreach(dev_list_entry, devices) {
const char *path;
path = udev_list_entry_get_name(dev_list_entry);
dev = udev_device_new_from_syspath(udev, path);
pdev = udev_device_get_parent_with_subsystem_devtype(
dev,
"usb",
"usb_device");
if (!pdev) {
continue;
}
if (strcmp(udev_device_get_sysattr_value(pdev,"idVendor"), ECON_VID) == 0 &&
strcmp(udev_device_get_sysattr_value(pdev,"idProduct"), CAMERA_PID) == 0)
{
/*printf("hid_device = %s\n", udev_device_get_devnode(dev)); // /dev/hidraw3
printf("productName = %s\n", udev_device_get_sysattr_value(pdev,"product")); // FSCAM_CU135
printf("serialNumber = %s\n", udev_device_get_sysattr_value(pdev,"serial")); // 28280B0E
printf("vidValue = %s\n", udev_device_get_sysattr_value(pdev,"idVendor")); // 2560
printf("pidValue = %s\n", udev_device_get_sysattr_value(pdev,"idProduct")); // c1d4
*/
const char *hid_dev = udev_device_get_devnode(dev);
hid_fd = open(hid_dev, O_RDWR|O_NONBLOCK);
if(hid_fd < 0) {
cout << "Unable to open device, try sudo" << endl;
return FAILURE;
}
const char *serial_num = udev_device_get_sysattr_value(pdev,"serial");
if(!is_in_list(serial_num)) {
dev_node[econdev_count] = hid_fd;
strncpy(serial_nums[econdev_count], serial_num, MAX_LEN_SERIAL_NUM);
econdev_count++;
}
}
udev_device_unref(dev);
}
// Free the enumerator object
udev_enumerate_unref(enumerate);
udev_unref(udev);
return SUCCESS;
}
/**
* brief sendHidCmd - Sending hid command and get reply back
* param outBuf - Buffer that fills to send into camera
* param inBuf - Buffer to get reply back
* param len - Buffer length
* return success/failure
* */
int sendHidCmd(unsigned char *outBuf, unsigned char *inBuf, int len, int hid_fd) {
// Write data into camera
cout << "sendHidCmd() write" << endl;
int ret = write(hid_fd, outBuf, len);
if (ret < 0) {
perror("write");
return FAILURE;
}
cout << "sendHidCmd() FD" << endl;
struct timeval tv;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(hid_fd, &rfds);
cout << "sendHidCmd() tv" << endl;
// Wait up to 5 seconds.
tv.tv_sec = 3;
tv.tv_usec = 0;
cout << "sendHidCmd() select" << endl;
// Monitor read file descriptor for 5 secs
if(0 > select(1, &rfds, NULL, NULL, &tv)){
perror("select");
return FAILURE;
}
cout << "sendHidCmd() read" << endl;
// Read data from camera
int retval = read(hid_fd, inBuf, len);
if (retval < 0) {
cout << "sendHidCmd() read FAILURE" << endl;
perror("read");
return FAILURE;
}
else{
cout << "sendHidCmd() read SUCCESS" << endl;
return SUCCESS;
}
}
/* The same like sendHidCmd() but without check, just write command and leave */
int sendHidCmdFast(unsigned char *outBuf, unsigned char *inBuf, int len, int hid_fd) {
// Write data into camera
int ret = write(hid_fd, outBuf, len);
if (ret < 0) {
perror("write");
return FAILURE;
}
return SUCCESS;
}
/* Initialize input and output buffers */
void initializeBuffers() {
memset(g_out_packet_buf, 0x00, sizeof(g_out_packet_buf));
memset(g_in_packet_buf, 0x00, sizeof(g_in_packet_buf));
}
/**
* @brief See3CAM_CU20::setCameraMode - getting camera mode
* @return true/false
*/
bool getCameraMode(int hid_fd) {
if(hid_fd < 0) {
return false;
}
initializeBuffers();
// fill buffer values
g_out_packet_buf[1] = CAMERA_CONTROL_CU20; /* camera id */
g_out_packet_buf[2] = GET_CAMERA_MODE_CU20; /* get camera command */
// send request and get reply from camera
if(sendHidCmd(g_out_packet_buf, g_in_packet_buf, BUFFER_LENGTH, hid_fd)){
if (g_in_packet_buf[6]==GET_FAIL) {
return false;
} else if(g_in_packet_buf[0] == CAMERA_CONTROL_CU20 &&
g_in_packet_buf[1]==GET_CAMERA_MODE_CU20 &&
g_in_packet_buf[6]==GET_SUCCESS) {\
return true;
}
}
return false;
}
/**
* @brief See3CAM_CU20::setCameraMode - setting camera mode
* @param cameraMode - master/slave
* @return true/false
*/
bool setCameraMode(uint cameraMode, int hid_fd) {
if(hid_fd < 0) {
return false;
}
initializeBuffers();
// fill buffer values
g_out_packet_buf[1] = CAMERA_CONTROL_CU20; /* camera id */
g_out_packet_buf[2] = SET_CAMERA_MODE_CU20; /* set camera mode command */
g_out_packet_buf[3] = cameraMode; /* pass camera mode value */
// send request and get reply from camera
if(sendHidCmd(g_out_packet_buf, g_in_packet_buf, BUFFER_LENGTH, hid_fd)){
if (g_in_packet_buf[6]==SET_FAIL) {
return false;
} else if(g_in_packet_buf[0] == CAMERA_CONTROL_CU20 &&
g_in_packet_buf[1] == SET_CAMERA_MODE_CU20 &&
g_in_packet_buf[6] == SET_SUCCESS) {
return true;
}
}
return false;
}
//==============================================================================
//==============================================================================
//==============================================================================
int main(int argc, char *argv[]) {
int result = 0;
if (argc >= 2) {
parseAgrs(argc, argv);
} else {
cout << "Missing arguments!!" << endl;
help();
exit(1);
}
// Get list of cameras (dev_node[n]) and init them
char unit[] = "hidraw";
if (initExtensionUnit(unit) < 0) {
cout << "Device not found!" << endl;
exit(4);
}
if (econdev_count == 0) {
cout << "No cameras" << endl << flush;
exit(6);
} else {
cout << "Number of cameras: " << econdev_count << endl << flush;
}
// Set all cameras to HW trigger
for (unsigned i = 0; i < econdev_count; i++) {
cout << "=== Processing dev_node " << dev_node[i] << " | /dev/video" << dev_node[i]+1 << endl;
if(cameraMode == 0) {
cout << "getCameraMode()" << endl;
result = getCameraMode(i);
cout << "result = " << result << endl;
} else {
cout << "setCameraMode(" << cameraMode << ")" << endl;
result = setCameraMode(cameraMode, i);
cout << "result = " << result << endl;
}
}
sleep(2);
// Close all cameras
for (unsigned i = 0; i < econdev_count; i++) {
close_hid(dev_node[i]);
}
return 0;
}