forked from withrobot/oCam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwithrobot_utility.cpp
298 lines (245 loc) · 8.36 KB
/
withrobot_utility.cpp
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
/*******************************************************************************#
# #
# Withrobot Utilities #
# #
# Copyright (C) 2015 Withrobot. Inc. #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/> #
# #
********************************************************************************/
/*
* withrobot_utility.cpp
*
* Created on: Oct 7, 2015
* Author: gnohead
*/
#include "withrobot_utility.hpp"
using namespace Withrobot;
/*
* References: https://www.kernel.org/pub/linux/utils/kernel/hotplug/libudev/ch01.html, http://www.signal11.us/oss/udev/
*/
int Withrobot::get_usb_device_info_list(std::vector<usb_device_info>& info_list)
{
info_list.clear();
struct udev* my_device;
my_device = udev_new();
if (!my_device) {
printf("Can't create udev\n");
return false;
}
struct udev_enumerate *enumerate;
struct udev_list_entry *devices;
struct udev_list_entry *dev_list_entry;
struct usb_device_info dev_info;
int num_dev = 0;
/* Create a list of the devices in the 'v4l2' subsystem. */
enumerate = udev_enumerate_new(my_device);
udev_enumerate_add_match_subsystem(enumerate, "video4linux");
udev_enumerate_scan_devices(enumerate);
devices = udev_enumerate_get_list_entry(enumerate);
/*
* For each item enumerated, print out its information.
* udev_list_entry_foreach is a macro which expands to
* a loop. The loop will be executed for each member in
* devices, setting dev_list_entry to a list entry
* which contains the device's path in /sys.
*/
udev_list_entry_foreach(dev_list_entry, devices)
{
const char *path;
/*
* Get the filename of the /sys entry for the device
* and create a udev_device object (dev) representing it
*/
path = udev_list_entry_get_name(dev_list_entry);
struct udev_device *dev = udev_device_new_from_syspath(my_device, path);
/* usb_device_get_devnode() returns the path to the device node
itself in /dev. */
const char* dev_node = udev_device_get_devnode(dev);
dev_info.dev_node = dev_node;
/* The device pointed to by dev contains information about
the v4l2 device. In order to get information about the
USB device, get the parent device with the
subsystem/devtype pair of "usb"/"usb_device". This will
be several levels up the tree, but the function will find
it.*/
dev = udev_device_get_parent_with_subsystem_devtype(dev, "usb", "usb_device");
if (!dev)
{
fprintf(stderr, "V4L2_CORE: Unable to find parent usb device [ %s ].\n", dev_node);
continue;
}
num_dev++;
/* From here, we can call get_sysattr_value() for each file
in the device's /sys entry. The strings passed into these
functions (idProduct, idVendor, serial, etc.) correspond
directly to the files in the directory which represents
the USB device. Note that USB strings are Unicode, UCS2
encoded, but the strings returned from
udev_device_get_sysattr_value() are UTF-8 encoded. */
const char* id_vendor = udev_device_get_sysattr_value(dev, "idVendor");
if (id_vendor) {
dev_info.id_vendor = id_vendor;
}
const char* id_product = udev_device_get_sysattr_value(dev, "idProduct");
if (id_product) {
dev_info.id_product = id_product;
}
const char* manufacturer = udev_device_get_sysattr_value(dev, "manufacturer");
if (manufacturer) {
dev_info.manufacturer = manufacturer;
}
const char* product = udev_device_get_sysattr_value(dev, "product");
if (product) {
dev_info.product = product;
}
const char* serial = udev_device_get_sysattr_value(dev, "serial");
if (serial) {
dev_info.serial = serial;
}
const char* busnum = udev_device_get_sysattr_value(dev, "busnum");
if (busnum) {
dev_info.busnum = busnum;
}
const char* devnum = udev_device_get_sysattr_value(dev, "devnum");
if (devnum) {
dev_info.devnum = devnum;
}
info_list.push_back(dev_info);
dev_info.clear();
udev_device_unref(dev);
}
/* Free the enumerator object */
udev_enumerate_unref(enumerate);
return num_dev;
}
/*
* pthread wrapper
*/
Thread::Thread() : id(0), thread(0)
{
}
Thread::~Thread()
{
join();
}
bool Thread::start(void*(*thread_proc)(void*), void* arg)
{
id = pthread_create(&thread, NULL, thread_proc, (void*) arg);
if (id != 0) {
thread = 0;
}
return (id == 0);
}
void Thread::join()
{
if (thread != 0) {
pthread_join(thread, NULL);
}
thread = 0;
}
/*
* pthread mutex wrapper
*/
Mutex::Mutex()
{
// mutex = new pthread_mutex_t PTHREAD_MUTEX_INITIALIZER;
char str_res[16];
int res = pthread_mutexattr_init(&attr);
if(res != 0) {
sprintf(str_res, "%d", res);
throw (WithRobotException("pthread_mutexattr_init returns " + std::string(str_res)));
}
res = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
if(res != 0) {
sprintf(str_res, "%d", res);
throw (WithRobotException("pthread_mutexattr_settype returns " + std::string(str_res)));
}
res = pthread_mutex_init (&mutex, &attr);
if(res != 0) {
sprintf(str_res, "%d", res);
throw (WithRobotException("pthread_mutex_init returns " + std::string(str_res)));
}
res = pthread_mutexattr_destroy(&attr);
if(res != 0) {
sprintf(str_res, "%d", res);
throw (WithRobotException("pthread_mutexattr_destroy returns " + std::string(str_res)));
}
}
Mutex::~Mutex()
{
pthread_mutex_destroy(&mutex);
}
/*
* Timer
*/
Timer::Timer(std::string name, unsigned int max_cnt) : name(name), cnt(0), max_cnt(max_cnt), elapsed_sum(0), elapsed_avg(0)
{
if (max_cnt < 1) {
max_cnt = 1;
}
init();
}
Timer::~Timer()
{
stop();
}
void Timer::start()
{
init();
running = true;
gettimeofday(&start_timeval, NULL);
}
void Timer::stop()
{
running = false;
gettimeofday(&end_timeval, NULL);
start_sec = start_timeval.tv_sec + (start_timeval.tv_usec / 1000000.0);
end_sec = end_timeval.tv_sec + (end_timeval.tv_usec / 1000000.0);
elapsed_sum += (end_sec - start_sec);
cnt++;
if (cnt == max_cnt) {
elapsed_avg = elapsed_sum / (double)cnt;
if (!elapsed_avg) {
elapsed_avg = (end_sec - start_sec);
}
cnt = 0;
elapsed_sum = 0;
}
}
double Timer::restart()
{
double res = get();
start();
return res;
}
double Timer::get()
{
if (running) {
stop();
}
return elapsed_avg;
}
void Timer::print()
{
double elps = get();
printf("[ %s ] ElapsedTime: %f sec (%.2f fps)\n", name.c_str(), elps, 1.0/elps); fflush(stdout);
}
void Timer::init()
{
running = false;
start_sec = 0;
end_sec = 0;
memset(&start_timeval, 0, sizeof(start_timeval));
memset(&end_timeval, 0, sizeof(end_timeval));
}