Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. include <stdio.h>
  2. #include <stdlib.h>
  3. #include <libusb-1.0/libusb.h>
  4. #define IDVENDOR 0x275d;
  5. #define IDPRODUCT 0x0ba6;
  6.  
  7. /*
  8. *
  9. */
  10. int main(int argc, char** argv) {
  11. int product =(int) 0x0ba6;
  12. int vendor = (int) 0x275d;
  13. int numInterface = 0;
  14. int endAdress = (int)0x81;
  15. char* data = malloc(5);
  16. int flagInit = libusb_init(NULL); //инициализация
  17.  
  18. if(flagInit != 0 ){ //?
  19. printf("%d \n",libusb_init);
  20. exit(1);
  21. }
  22. libusb_device ** list; //объяв список устройств
  23. int count = libusb_get_device_list(NULL, &list);// заносим в список устройст и считаем их
  24. printf("%d",count);
  25. struct libusb_device_descriptor deviceDesk;//создаем дискриптор одного устройства
  26. int num = 0;
  27. for(int i= 0; i<count; i++){
  28. int err = libusb_get_device_descriptor(list[i],&deviceDesk);//берется дескриптор из каждого дев
  29. if(err != 0){ printf("Ошибка получения дескриптора устройства\n"); exit(1); }
  30. if(deviceDesk.idProduct==product && deviceDesk.idVendor == vendor){
  31. num = i;// сохраняемэлемент
  32. i=count;
  33. }
  34. // printf(" \n0x%x , 0x%x \n",deviceDesk.idProduct,deviceDesk.idVendor);
  35. }
  36. printf(" \n0x%x , 0x%x \n",deviceDesk.idProduct,deviceDesk.idVendor);
  37.  
  38. libusb_device_handle *dev_handle;//создаем ссылку на структуру доступа
  39. int err = libusb_open(list[num], &dev_handle);//получение структуры доступа
  40. if(err != 0){ printf("Ошибка открытия устройства. Код: %i\n", err); exit(1); }
  41. libusb_free_device_list(list,0);//очищаем список устройств
  42. libusb_device *device = libusb_get_device(dev_handle); //создаем структуру устройства через стр-ру доступа
  43.  
  44. //Конфигурация
  45. struct libusb_config_descriptor *configDsc;
  46. int flagReadConfig = libusb_get_active_config_descriptor(device, &configDsc);// получаем дескриптор конфиг
  47. if(flagReadConfig!=0){
  48. printf("Error:%d \n",flagReadConfig);
  49. exit(1);
  50. }
  51. //printf("VID %x PID %x\n", );
  52. printf("MaxPower: %d mA \n",configDsc->MaxPower*2);
  53.  
  54. err = libusb_get_device_descriptor(dev, &desc);
  55. if (err < 0){
  56. exit(1);
  57. }else{
  58. unsigned char vend[100];
  59. unsigned char product[100];
  60. int vLen = libusb_get_string_descriptor_ascii(dev_handle, desc.iManufacturer,vend,100);
  61. int pLen = libusb_get_string_descriptor_ascii(dev_handle, desc.iProduct,product,100);
  62. hexDump(vend, vLen);
  63. hexDump(product, pLen);
  64.  
  65.  
  66. }
  67.  
  68.  
  69. //ЗАДАНИЕ2
  70.  
  71.  
  72. if(libusb_kernel_driver_active(dev_handle,numInterface) == 1)// проверка
  73. { //отсоед устр-во от ядра по структуре доступа
  74. int err = libusb_detach_kernel_driver(dev_handle,numInterface);//отключаем от ядра
  75. //printf("Err : %d", err);
  76. if(err !=0)
  77. {
  78. printf("Error detach: %d",err);
  79. exit(err);
  80. }
  81. }
  82. if(libusb_claim_interface(dev_handle,numInterface)!= 0){
  83. printf("Устройство не захвачено \n");
  84. exit(1);
  85. }
  86. printf("Устройство захвачено \n");
  87. int absolWheel = 0; //абсолют смещ
  88. do{
  89. int *transferred;//количество перед бат
  90. int dWheel = 0;//относ
  91. err =libusb_interrupt_transfer(dev_handle,endAdress,data,5,&transferred,0);//функция передачи
  92. if(err==0){
  93. dWheel = data[4] & 0xf;
  94. dWheel = dWheel > 7 ? dWheel - 0x10 : dWheel;
  95. }
  96. else{
  97. printf("Erorr: %d \n",err);
  98. }
  99. // printf("\n %d \n", dWheel);
  100. absolWheel+=dWheel;
  101. printf("%d \n",absolWheel);
  102. }
  103. while(data[0]!=0x02);
  104. // printf("%d \n",absolWheel);
  105. if(libusb_release_interface (dev_handle, numInterface)!=0){
  106. printf("Устройство не освобождено \n");
  107. exit(1);
  108. }
  109. printf("Устройство освобождено \n");
  110.  
  111. err = libusb_attach_kernel_driver(dev_handle,numInterface);//подключаем к ядру
  112. if(err !=0)
  113. {
  114. printf("Error attach: %d",err);
  115. exit(err);
  116. }
  117.  
  118. libusb_close (dev_handle);
  119. libusb_exit(NULL);
  120. return (EXIT_SUCCESS);
  121. }
  122.  
  123. void hexDump (void *addr, int len) {
  124. int i;
  125. unsigned char buff[17];
  126. unsigned char *pc = (unsigned char*)addr;
  127. if (len == 0) {
  128. printf(" ZERO LENGTH\n");
  129. return;
  130. }
  131. if (len < 0) {
  132. printf(" NEGATIVE LENGTH: %i\n",len);
  133. return;
  134. }
  135. for (i = 0; i < len; i++) {
  136. if ((i % 16) == 0) {
  137. if (i != 0)
  138. printf (" %s\n", buff);
  139. printf (" %04x ", i);
  140. }
  141. printf (" %02x", pc[i]);
  142. if ((pc[i] < 0x20) || (pc[i] > 0x7e))
  143. buff[i % 16] = '.';
  144. else
  145. buff[i % 16] = pc[i];
  146. buff[(i % 16) + 1] = '\0';
  147. }
  148. while ((i % 16) != 0) {
  149. printf (" ");
  150. i++;
  151. }
  152. printf (" %s\n", buff);
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement