-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUSBDevice.cpp
More file actions
610 lines (517 loc) · 13.6 KB
/
Copy pathUSBDevice.cpp
File metadata and controls
610 lines (517 loc) · 13.6 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#include "USBDevice.h"
USBDevice::USBDevice(uint16_t _vendor_id, uint16_t _product_id)
{
vendor_id = _vendor_id;
product_id = _product_id;
out = new std::ostringstream();
this->init();
}
USBDevice::USBDevice(uint16_t _vendor_id, uint16_t _product_id, std::ostringstream& iout)
{
vendor_id = _vendor_id; //
product_id = _product_id; //
out = &iout;
this->init();
}
USBDevice::USBDevice(void)
{
vendor_id = DIGISPARK_VENDOR_ID; //
product_id = DIGISPARK_PRODUCT_ID; //
out = new std::ostringstream();
//out = new std::ostream("");
this->init();
}
USBDevice::~USBDevice()
{
if (connected == true) {
libusb_release_interface(handle, 0);
libusb_close(handle);
}
if (bibo_connected == true)
libusb_exit(NULL);
}
void USBDevice::init() {
int ret;
connected = false;
handle = NULL;
ret = libusb_init(NULL);
if (ret < 0)
{
*out << "libusb_init(NULL) fehlgeschlagen" << endl << ends;
bibo_connected = false;
return;
}
bibo_connected = true;
}
void USBDevice::connect_device() {
int ret;
libusb_device_handle* _handle = NULL;
if (connected == false)
{
_handle = libusb_open_device_with_vid_pid(NULL, vendor_id, product_id);
if (_handle == NULL) { // have not connected my DigiSpark
*out << "could not open device via libusb_open_device_with_vid_pid(...." << endl << ends;
return;
}
ret = libusb_claim_interface(_handle, 0);
if (ret < 0) {
*out << "libusb_claim_interface error: " << libusb_error_text(ret) << endl << ends;
libusb_close(_handle);
return;
}
handle = _handle;
connected = true;
}
else
*out << "device is already connected" << endl << ends;
return;
}
/*
void USBDevice::connect_device_with_search1() {
libusb_device** devs;
libusb_device_handle* _handle; // handle DigiSpark
ssize_t cnt;
int i;
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0)
{
*out << libusb_error_text(cnt) << " - call libusb_get_device_list(.." << endl << ends;
return;
}
*out << "Number of existing USB devices: " << cnt << endl;
for (i = 0; devs[i]; i++) {
if (this->claim_device(devs[i], 0, &_handle) == 1) { //found my DigiSpark
handle = _handle;
connected = true;
break;
}
}
libusb_free_device_list(devs, 1);
}
*/
void USBDevice::connect_device_with_search()
{
struct libusb_device** devs;
struct libusb_device* dev;
struct libusb_device_handle* _handle = NULL;
ssize_t cnt;
int ret, i = 0;
unsigned int bus, addr;
if (connected == false)
{
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0)
{
*out << libusb_error_text(cnt) << " - call libusb_get_device_list(.." << endl << ends;
return;
}
while ((dev = devs[i++]) != NULL)
{
struct libusb_device_descriptor desc;
ret = libusb_get_device_descriptor(dev, &desc);
if (ret < 0)
{
*out << "failed to get device descriptor" << endl << ends;
libusb_free_device_list(devs, 1);
return;
}
if (desc.idVendor == vendor_id && desc.idProduct == product_id)
{
ret = libusb_open(dev, &_handle);
if (ret < 0)
{
*out << "libusb_open error: " << libusb_error_text(ret) << endl << ends;
_handle = NULL;
libusb_free_device_list(devs, 1);
return;
}
ret = libusb_claim_interface(_handle, 0);
if (ret < 0)
{
*out << "libusb_claim_interface error: " << libusb_error_text(ret) << endl << ends;
libusb_close(_handle);
libusb_free_device_list(devs, 1);
return;
}
bus = libusb_get_bus_number(dev);
addr = libusb_get_device_address(dev);
*out << "Device founded: bus: " << bus << " device: " << addr << " vendor ID: " << vendor_id << " product ID: " << product_id << "\n" << ends;
handle = _handle;
connected = true;
libusb_free_device_list(devs, 1);
return;
}
}
libusb_free_device_list(devs, 1);
}
else
*out << "\nDevice is already connected\n" << endl << ends;
}
int USBDevice::claim_device(libusb_device* dev, int level, libusb_device_handle** handleusb)
{
int ret;
std::string sout;
if (find_device(dev, level, handleusb, sout) == 1)
{
ret = libusb_claim_interface(*handleusb, 0);
if (ret < 0) {
*out << "libusb_claim_interface error: " << libusb_error_text(ret) << endl << ends;
libusb_close(*handleusb);
*handleusb = NULL;
return -1;
}
else
return 1;
}
return 0;
}
/* find the device and claim the interface */
int USBDevice::find_device(libusb_device* dev, int level, libusb_device_handle** handleusb, std::string& sout)
{
struct libusb_device_descriptor desc;
unsigned char strBuffer[256];
int ret;
unsigned int i, ii;
std::ostringstream m, m1, m2;
std::istringstream in;
long _vendor_id, _product_id;
libusb_device_handle* _handle;
ret = libusb_get_device_descriptor(dev, &desc);
if (ret < 0) {
*out << "failed to get device descriptor" << endl << ends;
sout = "";
return -1;
}
_handle = NULL;
ret = libusb_open(dev, &_handle);
if (ret == LIBUSB_SUCCESS) {
if (desc.iManufacturer) {
ret = libusb_get_string_descriptor_ascii(_handle, desc.iManufacturer, strBuffer, sizeof(strBuffer));
if (ret > 0) {
strBuffer[ret] = 0;
m1 << strBuffer;
}
else
m1 << desc.idVendor;
}
else
m1 << desc.idVendor;
if (desc.iProduct) {
ret = libusb_get_string_descriptor_ascii(_handle, desc.iProduct, strBuffer, sizeof(strBuffer));
if (ret > 0) {
strBuffer[ret] = 0;
m2 << strBuffer;
}
else
m2 << desc.idProduct;
}
else
m2 << desc.idProduct;
}
else {
m1 << desc.idVendor;
m2 << desc.idProduct;
}
m1 << ends;
m2 << ends;
in.str(m1.str());
in >> _vendor_id;
in.str(m2.str());
in >> _product_id;
if ((_vendor_id == vendor_id) && (_product_id == product_id)) {
i = libusb_get_bus_number(dev);
ii = libusb_get_device_address(dev);
m << "Device founded: " << level * 2 << ".*sDev (bus " << i << ", device " << ii << "): " << m1.str() << ":" << m2.str() << "\n" << ends;
// out << m.str();
*handleusb = _handle;
sout = m.str();
return 1;
}
if (_handle)
libusb_close(_handle);
sout = "";
return 0;
}
std::string USBDevice::print_device(libusb_device* dev, int level)
{
struct libusb_device_descriptor desc;
libusb_device_handle* _handle = NULL;
unsigned char strBuffer[256];
int ret;
unsigned int i, ii;
std::ostringstream m1, mout;
std::string ss("");
ret = libusb_get_device_descriptor(dev, &desc);
if (ret < 0) {
*out << "failed to get device descriptor" << endl << ends;
return "";
}
ret = libusb_open(dev, &_handle);
if (ret == LIBUSB_SUCCESS)
{
if (desc.iManufacturer)
{
ret = libusb_get_string_descriptor_ascii(_handle, desc.iManufacturer, strBuffer, sizeof(strBuffer));
if (ret > 0) {
strBuffer[ret] = 0;
m1 << " idVendor: " << strBuffer;
}
else
m1 << " idVendor: " << desc.idVendor;
}
else
m1 << " idVendor: " << desc.idVendor;
if (desc.iProduct) {
ret = libusb_get_string_descriptor_ascii(_handle, desc.iProduct, strBuffer, sizeof(strBuffer));
if (ret > 0) {
strBuffer[ret] = 0;
m1 << " idProduct: " << strBuffer;
}
else
m1 << " idProduct: " << desc.idProduct;
}
else
m1 << " idProduct: " << desc.idProduct;
}
else
{
m1 << "idVendor: " << desc.idVendor << " idProduct: " << desc.idProduct;
}
i = libusb_get_bus_number(dev);
ii = libusb_get_device_address(dev);
m1 << ends;
mout << level * 2 << ".*sDev (bus " << i << ", device " << ii << "): " << m1.str();
if (_handle) {
if (desc.iSerialNumber) {
ret = libusb_get_string_descriptor_ascii(_handle, desc.iSerialNumber, strBuffer, sizeof(strBuffer));
if (ret > 0)
mout << " - Serial Number: " << strBuffer << endl;
}
}
mout << "\n";
/* needs further seaching work to get more details */
/* for (i = 0; i < desc.bNumConfigurations; i++) {
struct libusb_config_descriptor* config;
ret = libusb_get_config_descriptor(dev, i, &config);
if (LIBUSB_SUCCESS != ret) {
out << " Couldn't retrieve descriptors" << endl;
continue;
}
//print_configuration(config);
libusb_free_config_descriptor(config);
}
if (_handle && desc.bcdUSB >= 0x0201) {
//print_bos(_handle);
} */
if (_handle)
libusb_close(_handle);
mout << ends;
return mout.str();
}
int USBDevice::readUSBhid(const unsigned char* data)
{
int status;
if (connected == true) {
//status = libusb_control_transfer(device, (0x01 << 5) | 0x80, 0x01, 0, 0, (unsigned char*) data, 1, 1000);
status = libusb_control_transfer(handle, CTRL_IN, HID_GET_REPORT, (HID_REPORT_TYPE_FEATURE << 8) | 0x00,
0, (unsigned char*)data, PACKET_CTRL_LEN, TIMEOUT);
if (status < 0) {
*out << endl << libusb_error_name(status) << endl << ends;
this->reset_device();
return 0;
}
else
return status;
}
return 0;
}
int USBDevice::readUSBVendor(const unsigned char* data)
{
int status;
if (connected == true) {
//status = libusb_control_transfer(handle, (0x01 << 5) | 0x80, 0x01, 0, 0, (unsigned char*) data, 1, 1000);
status = libusb_control_transfer(handle, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN,
REQ_LOGGING, 0, 0, (unsigned char*) data, 1, TIMEOUT);
if (status < 0) {
*out << endl << libusb_error_name(status) << endl << ends;
this->reset_device();
return 0;
}
else
return status;
}
return 0;
}
int USBDevice::USBDevice::setLED(int val)
{
int status;
unsigned char data[1];
if (connected == true) {
//status = libusb_control_transfer(device, (0x01 << 5) | 0x80, 0x01, 0, 0, (unsigned char*) data, 1, 1000);
status = libusb_control_transfer(handle, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
REQ_ONBOARD_LED_SET, val, 0, data, 1, TIMEOUT);
if (status < 0) {
*out << endl << libusb_error_name(status) << endl << ends;
reset_device();
return status;
}
else
return status;
}
return -1;
}
int USBDevice::setLED_off(void)
{
return setLED(VAL_STATE_OFF);
}
int USBDevice::getLED()
{
int status, ret = 0;
unsigned char data[1];
if (connected == true) {
//status = libusb_control_transfer(device, (0x01 << 5) | 0x80, 0x01, 0, 0, (unsigned char*) data, 1, 1000);
status = libusb_control_transfer(handle, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN,
REQ_ONBOARD_LED_GET, 0, 0, data, 1, TIMEOUT);
if (status < 0) {
*out << endl << libusb_error_name(status) << endl << ends;
this->reset_device();
return -1;
}
else
{
ret = ret + data[0];
return ret;
}
}
return -1;
}
int USBDevice::setLED_on(void)
{
return setLED(VAL_STATE_ON);
}
int USBDevice::writeUSB(const unsigned char* data)
{
int status;
if (connected == true) {
//status = libusb_control_transfer(this->handle, (0x01 << 5), 0x09, 0, 0, (unsigned char*) data, 0, 1000);
status = libusb_control_transfer(handle, CTRL_OUT, HID_SET_REPORT, (HID_REPORT_TYPE_FEATURE << 8) | 0x00,
0, (unsigned char*)data, PACKET_CTRL_LEN, TIMEOUT);
if (status < 0) {
*out << endl << libusb_error_name(status) << endl << ends;
this->reset_device();
return status;
}
}
return 0;
}
const char* USBDevice::libusb_error_text(ssize_t err_number) {
switch (err_number) {
case 0:
return "LIBUSB_SUCCESS";
case -1:
return "LIBUSB_ERROR_IO";
case -2:
return "LIBUSB_ERROR_INVALID_PARAM";
case -3:
return "LIBUSB_ERROR_ACCESS";
case -4:
return "LIBUSB_ERROR_NO_DEVICE";
case -5:
return "LIBUSB_ERROR_NOT_FOUND";
case -6:
return "LIBUSB_ERROR_BUSY";
case -7:
return "LIBUSB_ERROR_TIMEOUT";
case -8:
return "LIBUSB_ERROR_OVERFLOW";
case -9:
return "LIBUSB_ERROR_PIPE";
case -10:
return "LIBUSB_ERROR_INTERRUPTED";
case -11:
return "LIBUSB_ERROR_NO_MEM";
case -12:
return "LIBUSB_ERROR_NOT_SUPPORTED";
case -99:
return "LIBUSB_ERROR_OTHER";
}
return "UNDEFINED ERROR";
}
int USBDevice::reset_device()
{
if (connected == true) {
libusb_release_interface(handle, 0);
libusb_close(handle);
//libusb_exit(NULL);
connected = false;
}
return 1;
}
std::string USBDevice::print_deviceList()
{
libusb_device** devs;
ssize_t cnt;
int i;
std::string ss;
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0) {
*out << libusb_error_text(cnt) << " - call libusb_get_device_list(.." << endl << ends;
return "";
}
else
*out << "Number of existing USB devices: " << cnt << endl;
for (i = 0; devs[i]; ++i)
ss += this->print_device(devs[i], 0);
libusb_free_device_list(devs, 1);
return ss;
}
void USBDevice::writeln(std::string a) {
}
void USBDevice::writeByte(signed char b) {
}
void write(std::string a) {
}
std::string USBDevice::readString() { // blocking reading
unsigned char c;
std::string ss("");
bool loopcnt = true;
if (connected == true)
{
while (loopcnt == true) {
if (this->readUSBVendor(&c))
{
switch (c) {
case 0: loopcnt = false; break;
default: ss += c;
};
}
else
loopcnt = false;
}
}
return ss;
}
std::string USBDevice::getLog() {
std::string ss;
ss = out->str();
out->str("");
out->clear();
return ss;
}
std::string USBDevice::print_connectedDevice() {
libusb_device** devs;
libusb_device_handle* _handle;
ssize_t cnt;
int i;
std::string ss = "";
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0) {
*out << libusb_error_text(cnt) << " - call libusb_get_device_list(.." << endl << ends;
return "";
}
for (i = 0; devs[i]; i++) {
if (find_device(devs[i], 0, &_handle, ss) == 1) break;
}
libusb_free_device_list(devs, 1);
return ss;
}