-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathlibusb-test.c
801 lines (642 loc) · 22.6 KB
/
libusb-test.c
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
/********************************************************************************
*
* File Name : libusb-test.c
* Author : baoli
* Create Date : 2017.11.21
* Version : 1.0
* Description : libusb test
* compile command: gcc -o libusb libusb-test.c -lusb-1.0 -lpthread -lm
* History : 1. Data:
* Author:
* Modification:
*
********************************************************************************/
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <signal.h>
#include <semaphore.h>
#include <libusb-1.0/libusb.h>
typedef unsigned char uchar;
typedef unsigned int uint;
#define VID 0x0471
#define PID 0x0999
#define BULK_RECV_EP 0x82
#define BULK_SEND_EP 0x02
#define INT_RECV_EP 0x81
#define INT_SEND_EP 0x01
#define SEND_BUFF_LEN 500
#define RECV_BUFF_LEN 500
#define BULK_TEST 1
#define INT_TEST 2
#define SAVE_FILE_PATH "libusb_test_recv_data"
sem_t print_sem; //信号量
FILE *g_file_stream = NULL;
int g_no_device_flag = 0;
int g_file_save_en = 0; //文件保存使能标志
int g_send_flag = 0; //调用数据发送函数标志
uchar rev_buf[SEND_BUFF_LEN]; //usb 接收缓冲区
uchar send_buf[SEND_BUFF_LEN]; //usb发送缓冲区
libusb_device_handle *dev_handle;
char test_bytes[] =
"abcdefghijklmnopqrstuvwxyz0123456789\n"
"abcdefghijklmnopqrstuvwxyz0123456789\n"
"abcdefghijklmnopqrstuvwxyz0123456789\n"
"abcdefghijklmnopqrstuvwxyz0123456789\n"
"abcdefghijklmnopqrstuvwxyz0123456789\n"
"abcdefghijklmnopqrstuvwxyz0123456789\n"
"abcdefghijklmnopqrstuvwxyz0123456789\n"
"abcdefghijklmnopqrstuvwxyz0123456789\n"
"abcdefghijklmnopqrstuvwxyz0123456789\n"
"abcdefghijklmnopqrstuvwxyz0123456789\n"
"abcdefghijklmnopqrstuvwxyz0123456789\n"
"abcdefghijklmnopqrstuvwxyz0123456789\n"
"abcdefghijklmnopqrstuvwxyz0123456789\n"
"abcdefghijklmnopqrstuvwxyz0123456789\n"
"abcdefghijklmnopqrstuvwxyz0123456789\n"
"abcdefghijklmnopqrstuvwxyz0123456789\n"; //37*15=555 bytes
int bulk_send(char *buf, int num);
int interrupt_send(char *buf, int num);
void *bulk_rev_thread(void *arg);
void *interrupt_rev_thread(void *arg);
int LIBUSB_CALL usb_event_callback(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data);
void help(void);
int str2hex(char *hex);
void hex2str(uchar *pbDest, uchar *pbSrc, ushort nLen);
void *usb_monitor_thread(void *arg);
int save_to_file(FILE *file_stream, uchar *data, int length);
void print_devs(libusb_device **devs);
int list_devices(void);
void print_endpoint(const struct libusb_endpoint_descriptor *endpoint);
void print_altsetting(const struct libusb_interface_descriptor *interface);
void print_interface(const struct libusb_interface *interface);
void print_configuration(struct libusb_config_descriptor *config);
void print_device(libusb_device *dev, struct libusb_device_descriptor *desc);
int print_descriptor(libusb_device *dev);
int get_descriptor_with_vid_pid(int vid, int pid);
void sigint_handler(int sig);
//采用bulk端点发送数据
int bulk_send(char *buf, int num)
{
int size;
int rec;
g_send_flag = 1;
rec = libusb_bulk_transfer(dev_handle, BULK_SEND_EP, buf, num, &size, 0);
if(rec == 0) {
printf("bulk send sucess,length: %d bytes\n", size);
sem_post(&print_sem);
}
else{
g_send_flag = 0;
printf("bulk send faild, err: %s\n", libusb_error_name(rec));
}
return 0;
}
//采用interrupt端点发送数据
int interrupt_send(char *buf, int num)
{
int size;
int rec;
g_send_flag = 1;
rec = libusb_interrupt_transfer(dev_handle, INT_SEND_EP, buf, num, &size, 0);
if(rec == 0) {
printf("interrupt send sucess, length: %d bytes\n", size);
sem_post(&print_sem);
}
else{
g_send_flag = 0;
printf("interrupt send faild, err: %s\n", libusb_error_name(rec));
}
return 0;
}
//bulk 端点接收线程
void *bulk_rev_thread(void *arg)
{
int i=0;
int size;
int rec;
int save_bytes;
char hex_buf[RECV_BUFF_LEN*5];
printf("bulk_rev_thread started.\n");
while(1)
{
if(g_no_device_flag){
usleep(50 * 1000);
continue;
}
memset(rev_buf, 0, RECV_BUFF_LEN);
rec = libusb_bulk_transfer(dev_handle, BULK_RECV_EP, rev_buf, RECV_BUFF_LEN, &size, 0);
if(rec == 0)
{
if(g_send_flag == 1) //考虑如果没有调用发送数据函数,接收到数据后调用sem_wait会一直等待直到调用了bulk_send,才会继续往下执行
{
g_send_flag = 0;
sem_wait(&print_sem);
}
printf("\nbulk ep rev sucess, length: %d bytes. \n", size);
if(g_file_save_en){
save_bytes = save_to_file(g_file_stream, rev_buf, size);
printf("save %d bytes to file.\n", save_bytes);
}
hex2str(hex_buf, rev_buf, size);
printf("data is: \n%s\n", rev_buf);
printf("hex is: \n%s\n", hex_buf);
}
else
{
printf("bulk ep rev faild, err: %s\n", libusb_error_name(rec));
if(rec == LIBUSB_ERROR_IO)
g_no_device_flag = 1; //防止一直输出err
}
}
}
//interrupt 端点接收线程
void *interrupt_rev_thread(void *arg)
{
int i=0;
int size;
int rec;
int save_bytes;
char hex_buf[RECV_BUFF_LEN*5];
printf("interrupt_rev_thread started.\n");
while(1)
{
if(g_no_device_flag){
usleep(50 * 1000);
continue;
}
memset(rev_buf, 0, RECV_BUFF_LEN);
rec = libusb_interrupt_transfer(dev_handle, INT_RECV_EP, rev_buf, RECV_BUFF_LEN, &size, 0);
if(rec == 0)
{
if(g_send_flag == 1) //考虑如果没有调用发送数据函数,接收到数据后调用sem_wait会一直等待直到调用了interrupt_send,才会继续往下执行
{
g_send_flag = 0;
sem_wait(&print_sem);
}
printf("\ninterrupt ep rev sucess, length: %d bytes. \n", size);
if(g_file_save_en){
save_bytes = save_to_file(g_file_stream, rev_buf, size);
printf("save %d bytes to file.\n", save_bytes);
}
hex2str(hex_buf, rev_buf, size);
printf("data is: \n%s\n", rev_buf);
printf("hex is: \n%s\n", hex_buf);
}
else
{
printf("interrupt ep rev faild, err: %s\n", libusb_error_name(rec));
if(rec == LIBUSB_ERROR_IO)
g_no_device_flag = 1; //防止一直输出err
}
}
}
//usb hotplugin callback function
int LIBUSB_CALL usb_event_callback(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
{
struct libusb_device_descriptor desc;
int r;
printf("\nusb hotplugin event.\n");
r = libusb_get_device_descriptor(dev, &desc);
if (LIBUSB_SUCCESS != r) {
printf("error getting device descriptor.\n");
}
if (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED == event) {
printf("usb device attached: %04x:%04x\n", desc.idVendor, desc.idProduct);
if (dev_handle) {
libusb_close(dev_handle);
dev_handle = NULL;
}
r = libusb_open(dev, &dev_handle);
if (LIBUSB_SUCCESS != r) {
printf ("error opening device.\n");
}
else {
if(libusb_kernel_driver_active(dev_handle, 0) == 1) { //find out if kernel driver is attached
printf("kernel driver active\n");
if(libusb_detach_kernel_driver(dev_handle, 0) == 0) //detach it
printf("kernel driver detach\n");
}
int r = libusb_claim_interface(dev_handle, 0); //claim interface 0 (the first) of device (mine had jsut 1)
if (r < 0) {
printf("libusb_claim_interface failed\n");
return 0;
}
g_no_device_flag = 0;
printf("usb device open sucess\n\n");
printf("input data to send or command:");
fflush(stdout);
}
}
else if (LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT == event) {
g_no_device_flag = 1;
printf("usb device removed: %04x:%04x\n", desc.idVendor, desc.idProduct);
if (dev_handle) {
libusb_close(dev_handle);
dev_handle = NULL;
}
}
else
;
return 0;
}
//usb hotplugin monitor thread
void * usb_monitor_thread(void *arg)
{
printf("usb monitor thread started.\n");
int r = 0;
while (1) {
r = libusb_handle_events(NULL);
if (r < 0)
printf("libusb_handle_events() failed: %s\n", libusb_error_name(r));
}
}
void help(void)
{
printf("usage: libusb-test [-h] [-b] [-i] [-v vid] [-p pid] [-ffile_path] [-l] [-a] \n");
printf(" -h : display usage\n");
printf(" -b : test bulk transfer\n");
printf(" -i : test interrupt transfer\n");
printf(" -v : usb VID\n");
printf(" -p : usb PID\n");
printf(" -f : file path to save data\n");
printf(" -l : list usb devices\n");
printf(" -a : print descriptor for the usb(VID:PID)\n");
return;
}
//将char类型(0-255)的buf转换成以十六进制显示的字符串。
//使用这个函数可以避免多次循环调用printf("%x", buf[i]); 提高效率,并且能避免线程切换造成输出乱序的问题。
//buf={'1', '2', '3', 'a', 'b', 'c'} -> "0x31 0x32 0x33 0x61 0x62 0x63"
//buf={0,1,2,3} -> "0x00 0x01 0x02 0x03"
void hex2str(uchar *dest, uchar *src, ushort nLen)
{
int i;
for (i=0; i<nLen; i++)
{
sprintf(dest+5*i, "%#04x ", src[i]); //string类函数,不会造成线程切换
}
dest[nLen*5] = '\0';
}
//十六进制数字字符串转数字(十六进制)
// "0x1234" -> 0x1234 or "1234" -> 0x1234
int str2hex(char *hex)
{
int sum = 0;
int tmp = 0;
char hex_str[5];
if(strlen(hex) == 6) //0x1234
memcpy(hex_str, &hex[2], 4);
else
memcpy(hex_str, hex, 4);
for(int i = 0; i < 4; i++)
{
tmp = hex_str[i] - (((hex_str[i] >= '0') && (hex_str[i] <= '9')) ? '0' : \
((hex_str[i] >= 'A') && (hex_str[i] <= 'Z')) ? 'A' - 10 : 'a' - 10);
sum += tmp * pow(16, 3-i);
}
return sum;
}
//将接收到数据保存到文件中
int save_to_file(FILE *file_stream, uchar *data, int length)
{
int write_num;
write_num = fwrite(data, 1, length, file_stream);
fputc('\n', file_stream); //为每段数据换行,方便查看
fflush(file_stream);
return write_num;
}
void print_devs(libusb_device **devs)
{
libusb_device *dev;
int i = 0, j = 0;
uint8_t path[8];
libusb_device_handle *handle = NULL;
char string[256];
while ((dev = devs[i++]) != NULL) {
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) {
printf("failed to get device descriptor\n");
return;
}
printf("bus: %03d device: %03d, VID: %04x PID: %04x",
libusb_get_bus_number(dev), libusb_get_device_address(dev), desc.idVendor, desc.idProduct);
int ret = libusb_open(dev, &handle);
if (LIBUSB_SUCCESS == ret) {
if (desc.iProduct) {
ret = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, sizeof(string)); //显示产品信息
if (ret > 0)
printf(", %s", string);
}
}
printf("\n");
if (handle)
libusb_close(handle);
}
}
//列出系统所有的USB设备.
//包括:bus number. device number, vid, pid, product info
int list_devices(void)
{
libusb_device **devs;
int r;
ssize_t cnt;
r = libusb_init(NULL);
if (r < 0)
return r;
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0)
return (int) cnt;
print_devs(devs);
libusb_free_device_list(devs, 1);
libusb_exit(NULL);
return 0;
}
void print_endpoint(const struct libusb_endpoint_descriptor *endpoint)
{
int i, ret;
printf(" Endpoint descriptor:\n");
printf(" bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);
printf(" bmAttributes: %02xh\n", endpoint->bmAttributes);
printf(" wMaxPacketSize: %d\n", endpoint->wMaxPacketSize);
printf(" bInterval: %d\n", endpoint->bInterval);
printf(" bRefresh: %d\n", endpoint->bRefresh);
printf(" bSynchAddress: %d\n", endpoint->bSynchAddress);
}
void print_altsetting(const struct libusb_interface_descriptor *interface)
{
int i;
printf(" Interface descriptor:\n");
printf(" bInterfaceNumber: %d\n", interface->bInterfaceNumber);
printf(" bAlternateSetting: %d\n", interface->bAlternateSetting);
printf(" bNumEndpoints: %d\n", interface->bNumEndpoints);
printf(" bInterfaceClass: %d\n", interface->bInterfaceClass);
printf(" bInterfaceSubClass: %d\n", interface->bInterfaceSubClass);
printf(" bInterfaceProtocol: %d\n", interface->bInterfaceProtocol);
printf(" iInterface: %d\n", interface->iInterface);
for (i = 0; i < interface->bNumEndpoints; i++)
print_endpoint(&interface->endpoint[i]);
}
void print_interface(const struct libusb_interface *interface)
{
int i;
for (i = 0; i < interface->num_altsetting; i++)
print_altsetting(&interface->altsetting[i]);
}
void print_configuration(struct libusb_config_descriptor *config)
{
int i;
printf(" Configuration descriptor:\n");
printf(" wTotalLength: %d\n", config->wTotalLength);
printf(" bNumInterfaces: %d\n", config->bNumInterfaces);
printf(" bConfigurationValue: %d\n", config->bConfigurationValue);
printf(" iConfiguration: %d\n", config->iConfiguration);
printf(" bmAttributes: %02xh\n", config->bmAttributes);
printf(" MaxPower: %d\n", config->MaxPower);
for (i = 0; i < config->bNumInterfaces; i++)
print_interface(&config->interface[i]);
}
void print_device(libusb_device *dev, struct libusb_device_descriptor *desc)
{
int i;
printf("Device descriptor:\n");
printf(" bDescriptorType: %d\n", desc->bDescriptorType);
printf(" bcdUSB: %#06x\n", desc->bcdUSB);
printf(" bDeviceClass: %d\n", desc->bDeviceClass);
printf(" bDeviceSubClass: %d\n", desc->bDeviceSubClass);
printf(" bDeviceProtocol: %d\n", desc->bDeviceProtocol);
printf(" bMaxPacketSize0: %d\n", desc->bMaxPacketSize0);
printf(" idVendor: %#06x\n", desc->idVendor);
printf(" idProduct: %#06x\n", desc->idProduct);
printf(" bNumConfigurations: %d\n", desc->bNumConfigurations);
for (i = 0; i < desc->bNumConfigurations; i++) {
struct libusb_config_descriptor *config;
int ret = libusb_get_config_descriptor(dev, i, &config);
if (LIBUSB_SUCCESS != ret) {
printf("Couldn't retrieve descriptors\n");
continue;
}
print_configuration(config);
libusb_free_config_descriptor(config);
}
}
//打印USB设备的描述符
int print_descriptor(libusb_device *dev)
{
struct libusb_device_descriptor desc;
int ret, i;
ret = libusb_get_device_descriptor(dev, &desc);
if (ret < 0) {
printf("failed to get device descriptor\n");
return -1;
}
print_device(dev, &desc);
}
//根据VID:PID,打印特定USB设备的描述符
int get_descriptor_with_vid_pid(int vid, int pid)
{
libusb_device **devs;
libusb_device *found = NULL;
libusb_device *dev;
ssize_t cnt;
int r, i;
r = libusb_init(NULL);
if (r < 0)
return -1;
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0)
return -1;
while ((dev = devs[i++]) != NULL) {
struct libusb_device_descriptor desc;
r = libusb_get_device_descriptor(dev, &desc);
if (r < 0)
return -1;
if (desc.idVendor == vid && desc.idProduct == pid) {
found = dev;
break;
}
}
print_descriptor(found);
libusb_free_device_list(devs, 1);
libusb_exit(NULL);
return 0;
}
//ctrl+c 信号处理函数
void sigint_handler(int sig)
{
if(g_file_stream)
fclose(g_file_stream);
printf("\nlibusb test quit.\n");
exit(0);
}
int main(int argc, char **argv)
{
int r;
int opt;
int test_mode;
int vid, pid;
int send_length;
libusb_context *ctx = NULL;
struct sigaction act;
pthread_t bulk_rev_thread_id;
pthread_t int_rev_thread_id;
pthread_t usb_monitor_thread_id;
test_mode = BULK_TEST;
vid = VID;
pid = PID;
g_file_save_en = 0;
g_send_flag = 0;
act.sa_handler = sigint_handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGINT, &act, NULL);
//命令行参数解析
while((opt = getopt(argc, argv, "bif::hv:p:la")) != -1)
{
switch(opt)
{
case 'b' :
test_mode = BULK_TEST;
break;
case 'i' :
test_mode = INT_TEST;
break;
case 'f' :
g_file_stream = fopen(optarg == NULL ? SAVE_FILE_PATH : optarg, "wb"); //写打开,并将文件长度截为0,没有则新建
if(g_file_stream)
g_file_save_en = 1;
else
perror("file open faild: ");
break;
case 'v' :
vid = str2hex(optarg);
break;
case 'p' :
pid = str2hex(optarg);
break;
case 'l' :
list_devices();
exit(0);
break;
case 'a' :
get_descriptor_with_vid_pid(vid, pid);
exit(0);
break;
case 'h' :
help();
return 0;
break;
default :
printf("unkonw option.\n");
help();
return 0;
}
}
printf("\nlibusb test\n");
if(test_mode == BULK_TEST)
printf("test bulk transfer\n");
else if(test_mode == INT_TEST)
printf("test interrupt transfer\n");
else{
printf("unkonw test mode.\n");
return 0;
}
printf("usb device: VID:%#06x PID:%#06x\n\n", vid, pid); //#:输出0x,06:vid或pid第一个数字为0时,输出0x0471而不是0x471
r = sem_init(&print_sem, 0, 0); //初始化信号量,用于解决多线程下printf输出乱序的问题
if(r != 0)
perror("sem_init faild: ");
r = libusb_init(&ctx); //initialize the library for the session we just declared
if(r < 0) {
printf("libusb init faild, err:%s\n", libusb_error_name(r)); //there was an error
return -1;
}
libusb_set_debug(ctx, LIBUSB_LOG_LEVEL_WARNING);
if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
printf("hotplug capabilites are not supported on this platform.\n");
libusb_exit (NULL);
return -1;
}
r = libusb_hotplug_register_callback(ctx, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, LIBUSB_HOTPLUG_NO_FLAGS, vid, pid, LIBUSB_HOTPLUG_MATCH_ANY, usb_event_callback, NULL, NULL);
if (LIBUSB_SUCCESS != r) {
printf("error registering callback: %s\n", libusb_error_name(r));
libusb_exit(ctx);
return -1;
}
dev_handle = libusb_open_device_with_vid_pid(ctx, vid, pid); //open a usb device with VID&PID
if(dev_handle == NULL) {
printf("cannot open device\n");
return -1;
}
else
printf("usb device opened.\n");
r = libusb_kernel_driver_active(dev_handle, 0);
if(r == 0) //ok
;
else if(r == 1) {
printf("Kernel driver is active, now try detached\n");
if(libusb_detach_kernel_driver(dev_handle, 0) == 0) //detach it
printf("Kernel driver is detached!\n");
else{
printf("libusb_detach_kernel_driver, err:%s\n", libusb_error_name(r));
return -1;
}
}
else{
printf("libusb_kernel_driver_active, err:%s\n", libusb_error_name(r));
return -1;
}
r = libusb_claim_interface(dev_handle, 0); //claim interface 0 ,stm32采用接口0
if(r < 0) {
printf("cannot claim interface, err:%s\n", libusb_error_name(r));
return -1;
}
//热插拔监听
r = pthread_create(&usb_monitor_thread_id, 0, usb_monitor_thread, 0);
if(r != 0 )
{
perror("usb_monitor_thread creation faild\n");
}
//new thread,usb rev data
if(test_mode == BULK_TEST){
r = pthread_create(&bulk_rev_thread_id, NULL, bulk_rev_thread, NULL);
if(r != 0 )
{
perror("thread creation faild\n");
}
}
else if(test_mode == INT_TEST){
r = pthread_create(&int_rev_thread_id, NULL, interrupt_rev_thread, NULL);
if(r != 0 )
{
perror("thread creation faild\n");
}
}
while(1)
{
usleep(10 * 1000);
printf("\ninput data to send or command:");
memset(send_buf, 0, SEND_BUFF_LEN);
fgets(send_buf, SEND_BUFF_LEN, stdin);
send_length = strlen(send_buf)-1;
send_buf[send_length] = '\0' ; //将读入的换行符转为\0
//用于快捷发送数据,如输入test64,会自动发送64字节数据,输入test100则会自动发送100字节数据
if(strncmp(send_buf, "test", 4) == 0){
send_length = atoi(&send_buf[4]);
if(send_length > strlen(test_bytes)){
printf("send_length:%d should less than max_length:%ld\n", send_length, strlen(test_bytes));
continue;
}
printf("test sending %d bytes.\n", send_length);
memcpy(send_buf, test_bytes, send_length);
}
if(test_mode == BULK_TEST)
bulk_send(send_buf, send_length);
else if(test_mode == INT_TEST)
interrupt_send(send_buf, send_length);
else
;
}
}