yk_usb_device.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*******************************************************************************
  2. Copyright 2017 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. #ifndef _YK_USB_DEVICE_H_
  14. #define _YK_USB_DEVICE_H_
  15. #ifdef _LIBUSB_
  16. #include <usbhid.h>
  17. #else
  18. #include <hidapi.h>
  19. #endif
  20. #include <stdlib.h>
  21. /**
  22. * \defgroup yepkit_usb_device_handler Yepkit USB device handling
  23. */
  24. #ifndef _LIBUSB_
  25. #define USB_CMD_NON_BLOCKING 1 //1 -> Set the USB device handle to be non-blocking
  26. #define USB_CMD_BLOCKING 0 //0 -> Set the USB device handle to be blocking and wait for response
  27. #endif
  28. /**
  29. * \ingroup yepkit_usb_device_handler
  30. *
  31. * \brief Handles the USB HID for a given PID and VID.
  32. */
  33. class UsbDevice {
  34. public:
  35. UsbDevice(unsigned int vendor_id, unsigned int product_id);
  36. int sendHidReport(char *serial, unsigned char *msg, unsigned char *resp_msg, int report_size);
  37. /**
  38. * Prints to standard output the list of connected devices with PID a VID provided in the class constructor.
  39. *
  40. * \return Number of attached devices.
  41. */
  42. int listConnected(void); //List connected devices
  43. private:
  44. unsigned short vid;
  45. unsigned short pid;
  46. #ifndef _LIBUSB_
  47. hid_device *handle;
  48. #endif
  49. protected:
  50. unsigned char hid_report_out[64];
  51. unsigned char hid_report_in[64];
  52. char *usb_serial = NULL;
  53. };
  54. /*****************************************************************
  55. * Function:
  56. *
  57. * send_usb_msg
  58. *
  59. *
  60. * Description:
  61. *
  62. * Sends HID report with the data provided in the input buffer
  63. * "msg".
  64. *
  65. *
  66. * Inputs:
  67. *
  68. * serial - target usb device serial number string
  69. *
  70. * msg - message (HID report) to be sent
  71. *
  72. * resp_msg - response message (HID report) received
  73. *
  74. *
  75. * Outputs:
  76. *
  77. * The function returns the following value.
  78. * 0 - No error
  79. * -1 - Error
  80. *
  81. * In the case of error, a message is printed into the standard
  82. * output.
  83. *
  84. *
  85. * Precedences:
  86. *
  87. * Requires that VENDOR_ID and PRODUCT_ID constants are defined.
  88. *
  89. *
  90. *****************************************************************/
  91. int send_usb_msg(char *serial, char *msg, char *resp_msg);
  92. char commands(char *cmd, char *resp, int num);
  93. char command(char cmd);
  94. char commandsBySerial(char *iSerial, char *cmd, char *resp, int num);
  95. char commandBySerial(char *iSerial, char cmd);
  96. int listDevices();
  97. #endif // USBCOM_H