当前位置:   article > 正文

使用liusb读取鼠标数据(异步传输)_libusb异步传输

libusb异步传输
  1. #include <errno.h>
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <libusb-1.0/libusb.h>
  7. /*抽象出 鼠标结构体*/
  8. struct usb_mouse
  9. {
  10. struct libusb_device_handle *handle;
  11. int interface;
  12. int endpoint;
  13. unsigned char buf[16];
  14. int transferred;
  15. struct libusb_transfer *transfer;
  16. struct usb_mouse *next; /*添加一个链表,为了处理多个usb_mouse*/
  17. };
  18. static struct usb_mouse *usb_mouse_list; /*链表头*/
  19. void free_usb_mouses(struct usb_mouse *usb_mouse_list)
  20. {
  21. struct usb_mouse *pnext;
  22. while (usb_mouse_list) /*释放当前项,然后指向下一项*/
  23. {
  24. pnext = usb_mouse_list->next;
  25. free(usb_mouse_list);
  26. usb_mouse_list = pnext;
  27. }
  28. }
  29. /* */
  30. int get_usb_mouses(libusb_device **devs, int num_devices, struct usb_mouse **usb_mouse_list) /*如果在函数里面需要修改链表的话,需要传入** */
  31. {
  32. int err;
  33. libusb_device *dev;
  34. int endpoint;
  35. int interface_num;
  36. struct libusb_config_descriptor *config_desc;
  37. struct libusb_device_handle *dev_handle = NULL;
  38. struct usb_mouse *pmouse;
  39. struct usb_mouse *list = NULL;
  40. int mouse_cnt = 0;
  41. /* for each device, get config descriptor */
  42. for (int i = 0; i < num_devices; i++)
  43. {
  44. dev = devs[i];
  45. /* parse interface descriptor, find usb mouse */
  46. err = libusb_get_config_descriptor(dev, 0, &config_desc);
  47. if (err)
  48. {
  49. fprintf(stderr, "could not get configuration descriptor\n");
  50. continue;
  51. }
  52. fprintf(stdout, "libusb_get_config_descriptor() ok\n");
  53. for (int interface = 0; interface < config_desc->bNumInterfaces; interface++)
  54. {
  55. const struct libusb_interface_descriptor *intf_desc = &config_desc->interface[interface].altsetting[0];
  56. interface_num = intf_desc->bInterfaceNumber;
  57. if (intf_desc->bInterfaceClass != 3 || intf_desc->bInterfaceProtocol != 2)
  58. continue;
  59. else
  60. {
  61. /* 找到了USB鼠标 */
  62. fprintf(stdout, "find usb mouse ok\n");
  63. for (int ep = 0; ep < intf_desc->bNumEndpoints; ep++)
  64. {
  65. if ((intf_desc->endpoint[ep].bmAttributes & 3) == LIBUSB_TRANSFER_TYPE_INTERRUPT ||
  66. (intf_desc->endpoint[ep].bEndpointAddress & 0x80) == LIBUSB_ENDPOINT_IN)
  67. {
  68. /* 找到了输入的中断端点 */
  69. fprintf(stdout, "find in int endpoint\n");
  70. endpoint = intf_desc->endpoint[ep].bEndpointAddress;
  71. /* libusb_open */
  72. err = libusb_open(dev, &dev_handle);
  73. if (err)
  74. {
  75. fprintf(stderr, "failed to open usb mouse\n");
  76. return -1;
  77. }
  78. fprintf(stdout, "libusb_open ok\n");
  79. /* 记录下来: 放入链表 */
  80. pmouse = malloc(sizeof(struct usb_mouse));
  81. if (!pmouse)
  82. {
  83. fprintf(stderr, "can not malloc\n");
  84. return -1;
  85. }
  86. pmouse->endpoint = endpoint;
  87. pmouse->interface = interface_num;
  88. pmouse->handle = dev_handle;
  89. pmouse->next = NULL;
  90. if (!list)
  91. list = pmouse;
  92. else
  93. {
  94. pmouse->next = list; /*头插法*/
  95. list = pmouse;
  96. }
  97. mouse_cnt++;
  98. break;
  99. }
  100. }
  101. }
  102. }
  103. libusb_free_config_descriptor(config_desc);
  104. }
  105. *usb_mouse_list = list; /*放入全局链表*/
  106. return mouse_cnt;
  107. }
  108. static void mouse_irq(struct libusb_transfer *transfer)
  109. {
  110. static int count = 0;
  111. if (transfer->status == LIBUSB_TRANSFER_COMPLETED)
  112. {
  113. /* parser data */
  114. printf("%04d datas: ", count++);
  115. for (int i = 0; i < transfer->actual_length; i++)
  116. {
  117. printf("%02x ", transfer->buffer[i]);
  118. }
  119. printf("\n");
  120. }
  121. if (libusb_submit_transfer(transfer) < 0)
  122. {
  123. fprintf(stderr, "libusb_submit_transfer err\n");
  124. }
  125. }
  126. int main(int argc, char **argv)
  127. {
  128. int err;
  129. libusb_device **devs;
  130. int num_devices, num_mouse;
  131. struct usb_mouse *pmouse;
  132. /* libusb_init */
  133. err = libusb_init(NULL);
  134. if (err < 0)
  135. {
  136. fprintf(stderr, "failed to initialise libusb %d - %s\n", err, libusb_strerror(err));
  137. exit(1);
  138. }
  139. /* get device list */
  140. if ((num_devices = libusb_get_device_list(NULL, &devs)) < 0)
  141. {
  142. fprintf(stderr, "libusb_get_device_list() failed\n");
  143. libusb_exit(NULL);
  144. exit(1);
  145. }
  146. fprintf(stdout, "libusb_get_device_list() ok\n");
  147. /* get usb mouse */
  148. num_mouse = get_usb_mouses(devs, num_devices, &usb_mouse_list);
  149. if (num_mouse <= 0)
  150. {
  151. /* free device list */
  152. libusb_free_device_list(devs, 1);
  153. libusb_exit(NULL);
  154. exit(1);
  155. }
  156. fprintf(stdout, "get %d mouses\n", num_mouse);
  157. /* free device list */
  158. libusb_free_device_list(devs, 1);
  159. /* claim interface */
  160. pmouse = usb_mouse_list;
  161. while (pmouse)
  162. {
  163. libusb_set_auto_detach_kernel_driver(pmouse->handle, 1);
  164. err = libusb_claim_interface(pmouse->handle, pmouse->interface);
  165. if (err)
  166. {
  167. fprintf(stderr, "failed to libusb_claim_interface\n");
  168. exit(1);
  169. }
  170. pmouse = pmouse->next;
  171. }
  172. fprintf(stdout, "libusb_claim_interface ok\n");
  173. /* for each mouse, alloc transfer, fill transfer, submit transfer */
  174. pmouse = usb_mouse_list;
  175. while (pmouse)
  176. {
  177. /* alloc transfer */
  178. pmouse->transfer = libusb_alloc_transfer(0);
  179. /* fill transfer */
  180. libusb_fill_interrupt_transfer(pmouse->transfer, pmouse->handle, pmouse->endpoint, pmouse->buf,
  181. sizeof(pmouse->buf), mouse_irq, pmouse, 0);
  182. /* submit transfer */
  183. libusb_submit_transfer(pmouse->transfer);
  184. pmouse = pmouse->next;
  185. }
  186. /* handle events */
  187. while (1)
  188. {
  189. struct timeval tv = {5, 0};
  190. int r;
  191. r = libusb_handle_events_timeout(NULL, &tv);
  192. if (r < 0)
  193. {
  194. fprintf(stderr, "libusb_handle_events_timeout err\n");
  195. break;
  196. }
  197. }
  198. /* libusb_close */
  199. pmouse = usb_mouse_list;
  200. while (pmouse)
  201. {
  202. libusb_release_interface(pmouse->handle, pmouse->interface);
  203. libusb_close(pmouse->handle);
  204. pmouse = pmouse->next;
  205. }
  206. free_usb_mouses(usb_mouse_list);
  207. libusb_exit(NULL);
  208. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/711508
推荐阅读
相关标签
  

闽ICP备14008679号