yk_usb_device.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*******************************************************************************
  2. Copyright 2019 Yepkit Lda (www.yepkit.com)
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. *******************************************************************************/
  13. #include "yk_usb_device.h"
  14. #ifdef _LIBUSB_
  15. #include <usbhid.h>
  16. #else
  17. #include <hidapi.h>
  18. #include <string.h>
  19. #endif
  20. #include <iostream>
  21. #include <string>
  22. #include <stdlib.h>
  23. #ifdef _LIBUSB_
  24. // Uses libusb directly
  25. int UsbDevice::listConnected()
  26. {
  27. UsbHid *usbhid = new UsbHid();
  28. struct hid_device_info *devs, *cur_dev;
  29. int i = 1;
  30. devs = usbhid->enumerate(vid, pid);
  31. if (devs == NULL)
  32. return 0;
  33. cur_dev = devs;
  34. while (cur_dev) {
  35. std::cout << i << ". Board found with serial number: " << cur_dev->serial_number_ascii << "\n";
  36. cur_dev = cur_dev->next;
  37. i++;
  38. }
  39. usbhid->free_enumeration(devs);
  40. return i;
  41. }
  42. #else
  43. // Uses hidapi
  44. int UsbDevice::listConnected()
  45. {
  46. int i=0;
  47. struct hid_device_info *devs, *cur_dev;
  48. devs = hid_enumerate(vid, pid);
  49. if (devs == NULL)
  50. return 0;
  51. cur_dev = devs;
  52. while (cur_dev) {
  53. i++;
  54. printf("%d. Board found with serial number: %ls\n", i, cur_dev->serial_number);
  55. cur_dev = cur_dev->next;
  56. }
  57. hid_free_enumeration(devs);
  58. return i;
  59. }
  60. #endif
  61. UsbDevice::UsbDevice(unsigned int vendor_id, unsigned int product_id) {
  62. pid = product_id;
  63. vid = vendor_id;
  64. }
  65. /**
  66. * Sends HID report with the data provided in the input buffer "msg".
  67. *
  68. * \param [in] serial Pointer to a char array containing the serial number.
  69. * \param [in] msg Pointer to buffer containing the HID report message to be sent to USB device.
  70. * \param [out] resp_msg Pointer to buffer to which the HID report message replied by the device.
  71. *
  72. * \retval 0 No error.
  73. * \retval -1 Unable to open USB device.
  74. * \retval -2 Unable to write HID report to USB device.
  75. * \retval -3 Unable to read HID report from USB device.
  76. *
  77. *
  78. * In the case of error, a message is printed into the standard
  79. * output.
  80. *
  81. *
  82. * Precedences:
  83. *
  84. * Requires that VENDOR_ID and PRODUCT_ID constants are defined.
  85. *
  86. *
  87. *****************************************************************/
  88. #ifdef _LIBUSB_
  89. int UsbDevice::sendHidReport(char *serial, unsigned char *msg, unsigned char *resp_msg, int report_size)
  90. {
  91. UsbHid *usbhid = new UsbHid();
  92. int res;
  93. res = usbhid->open(vid, pid, serial);
  94. if (res < 0) {
  95. std::cout << "Unable to open device\n";
  96. return -1;
  97. }
  98. res = usbhid->write(msg, report_size);
  99. if (res < 0) {
  100. std::cout << "Unable to write to device\n";
  101. return -2;
  102. }
  103. res = usbhid->read(resp_msg, report_size);
  104. if (res < 0) {
  105. std::cout << "Unable to read from device\n";
  106. return -3;
  107. }
  108. usbhid->close();
  109. return 0;
  110. }
  111. #else
  112. int UsbDevice::sendHidReport(char *serial, unsigned char *msg, unsigned char *resp_msg, int report_size)
  113. {
  114. const size_t newsize = 100;
  115. wchar_t cserial[newsize];
  116. int res, i;
  117. unsigned char out_buf[65];
  118. unsigned char in_buf[65];
  119. if (report_size <= 64) {
  120. out_buf[0] = 0;
  121. for (i = 0; i < report_size; i++) {
  122. out_buf[i+1] = msg[i];
  123. }
  124. } else {
  125. std::cout << "Invalid report size\n";
  126. return -1;
  127. }
  128. if (serial) {
  129. // Convert to a wchar_t*
  130. size_t origsize = strlen(serial) + 1;
  131. size_t convertedChars = 0;
  132. mbstowcs_s(&convertedChars, cserial, origsize, serial, _TRUNCATE);
  133. }
  134. // Open the USB device
  135. handle = hid_open(vid, pid, serial ? cserial : NULL);
  136. if (handle == NULL) {
  137. //printf("\n\nERROR: Unable to open USB device\n");
  138. return -1;
  139. }
  140. // Set the hid_read() function to be blocking (wait for response from the device).
  141. hid_set_nonblocking(handle, USB_CMD_BLOCKING);
  142. //send HID report
  143. res = hid_write(handle, out_buf, report_size++);
  144. if (res < 0) {
  145. std::cout << "Unable to write HID report to USB device\n";
  146. return -1;
  147. }
  148. //read HID report
  149. res = hid_read(handle, in_buf, report_size++);
  150. if (res < 0) {
  151. std::cout << "Unable to read HID report from USB device\n";
  152. return -1;
  153. }
  154. for (i = 0; i < report_size; i++) {
  155. resp_msg[i] = in_buf[i];
  156. }
  157. return 0;
  158. }
  159. #endif