usbcom.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /****************************************************************************
  2. FileName: usbcom.cpp
  3. Dependencies: See INCLUDES section
  4. Compiler: Visual Studio Community 2015
  5. Company: Yepkit, Lda.
  6. Software License Agreement:
  7. Copyright (c) 2015 Yepkit Lda
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. *****************************************************************************
  24. File Description:
  25. Change History:
  26. Rev Date Description
  27. ---- ----------- -----------------------------------------
  28. 1.0 2015-09-11 First Release
  29. ****************************************************************************
  30. * Summary:
  31. *
  32. * Functions that communicate with the YKUSH board and/or provide
  33. * status information.
  34. *
  35. *****************************************************************************/
  36. //-------------------------------------------------------------------------
  37. // INCLUDES
  38. //-------------------------------------------------------------------------
  39. #include <errno.h>
  40. #include <string.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <iostream>
  44. #include "stdafx.h"
  45. #ifndef LINUX
  46. #include "windows\hidapi.h"
  47. #else
  48. #include "linux/hidapi.h"
  49. #endif
  50. #include "usbcom.h"
  51. using namespace std;
  52. //-------------------------------------------------------------------------
  53. // DEFINES
  54. //-------------------------------------------------------------------------
  55. #define VERSION "0.1.0"
  56. #define VENDOR_ID 0x04D8
  57. #define PRODUCT_ID 0xF2F7
  58. #define OLD_PRODUCT_ID 0x0042
  59. /**************************************************************************
  60. * listDevices() - List YKUSH Devices Serial Number
  61. *-------------------------------------------------------------------------
  62. *
  63. * This function prints the iSerial of the
  64. * attached YKUSH devices.
  65. *
  66. **************************************************************************/
  67. int listDevices() {
  68. // Enumerate and print the YKUSH devices on the system
  69. hid_device *handle;
  70. struct hid_device_info *devs, *cur_dev;
  71. devs = hid_enumerate(0x0, 0x0);
  72. if (devs == NULL) {
  73. //APAGAR
  74. // Limited Legaccy firmware support (does not work in all systems)
  75. //handle = hid_open(VENDOR_ID, OLD_PRODUCT_ID, NULL);
  76. //FIM APAGAR
  77. if (handle == NULL){
  78. // No HID devices found
  79. printf("\nNo HID USB devices connected to this Host\n");
  80. }
  81. }
  82. cur_dev = devs;
  83. while (cur_dev) {
  84. if ((cur_dev->vendor_id == VENDOR_ID) && (cur_dev->product_id == PRODUCT_ID)) {
  85. printf("YKUSH device found with Serial Number: %ls", cur_dev->serial_number);
  86. printf("\n");
  87. printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
  88. printf(" Product: %ls\n", cur_dev->product_string);
  89. printf("\n");
  90. } else if ((cur_dev->vendor_id == VENDOR_ID) && (cur_dev->product_id == OLD_PRODUCT_ID)) {
  91. printf("YKUSH device found with Serial Number: %ls", cur_dev->serial_number);
  92. printf("\n");
  93. printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
  94. printf(" Product: %ls\n", cur_dev->product_string);
  95. printf(" NOTE: This is an old version of the firmware. If you have access to a PIC programmer contact Yepkit to get the latest version of the firmware.");
  96. printf("\n");
  97. }
  98. cur_dev = cur_dev->next;
  99. }
  100. hid_free_enumeration(devs);
  101. return 0;
  102. }
  103. /*******************************************************************************
  104. * commandsBySerial(char* iSerial, char *cmd, char* resp, int num) - Sends commands to YKUSH board
  105. * -----------------------------------------------------------------------------
  106. *
  107. * Description:
  108. *
  109. * All commands in the cmd array are send to the YKUSH board and the
  110. * response bytes are stored in the matching resp entry. If more than
  111. * one YKUSH board is connected to the host it will send the command
  112. * just to the first one that appears in the enumerated list.
  113. *
  114. * If there are more than one YKUSH board connected to the same host
  115. * the commandBySerial function should be used instead.
  116. *
  117. * Inputs:
  118. *
  119. * iSerial - Serial number of the YKUSH board to issue the command.
  120. * cmds - Command bytes to be sent to YKUSH.
  121. * resp - Array for storing response bytes
  122. * num - Number of entries in cmds and resp
  123. *
  124. * Outputs:
  125. *
  126. * The function will return the response byte or zero.
  127. *
  128. ********************************************************************************/
  129. char commandsBySerial(char *iSerial, char *cmd, char *resp, int num)
  130. {
  131. int res;
  132. unsigned char buf[65];
  133. unsigned char buf6[6];
  134. bool isLegacy = false;
  135. #define MAX_STR 255
  136. wchar_t wstr[MAX_STR];
  137. hid_device *handle;
  138. int i;
  139. const size_t newsize = 100;
  140. wchar_t serial[newsize];
  141. if (iSerial) {
  142. // Convert to a wchar_t*
  143. size_t origsize = strlen(iSerial) + 1;
  144. size_t convertedChars = 0;
  145. #ifndef LINUX
  146. mbstowcs_s(&convertedChars, serial, origsize, iSerial, _TRUNCATE);
  147. #else
  148. mbstowcs(serial, iSerial, newsize);
  149. #endif
  150. }
  151. // Open the YKUSH device
  152. handle = hid_open(VENDOR_ID, PRODUCT_ID, iSerial ? serial : NULL);
  153. if (handle == NULL) {
  154. // Limited Legaccy firmware support (does not work in all systems)
  155. handle = hid_open(VENDOR_ID, OLD_PRODUCT_ID, NULL);
  156. if (handle == NULL){
  157. return 0;
  158. }
  159. isLegacy = true;
  160. }
  161. // Set the hid_read() function to be blocking (wait for response from YKUSH).
  162. hid_set_nonblocking(handle, 0);
  163. for (i = 0; i < num; i++) {
  164. // Send an Output report with the desired command
  165. if (isLegacy) {
  166. buf6[0] = 0; // First byte is report number
  167. buf6[1] = cmd[i]; // Command byte
  168. buf6[2] = cmd[i]; // Command confirm byte
  169. res = hid_write(handle, buf6, 6);
  170. } else {
  171. buf[0] = 0; // First byte is report number
  172. buf[1] = cmd[i]; // Command byte
  173. buf[2] = cmd[i]; // Command confirm byte
  174. res = hid_write(handle, buf, 65);
  175. }
  176. // Read response
  177. if (isLegacy) {
  178. res = hid_read(handle, buf, 6);
  179. } else {
  180. res = hid_read(handle, buf, 65);
  181. }
  182. if (res < 0) {
  183. printf("Unable to read YKUSH command response\n");
  184. }
  185. if (res >= 1) {
  186. resp[i] = buf[1];
  187. }
  188. }
  189. return 0;
  190. }
  191. /*******************************************************************************
  192. * commands(char *cmd, char* resp, int num) - Sends commands to YKUSH board
  193. * -----------------------------------------------------------------------------
  194. *
  195. * Description:
  196. *
  197. * All commands in the cmd array are send to the YKUSH board and the
  198. * response bytes are stored in the matching resp entry. If more than
  199. * one YKUSH board is connected to the host it will send the command
  200. * just to the first one that appears in the enumerated list.
  201. *
  202. * If there are more than one YKUSH board connected to the same host
  203. * the commandBySerial function should be used instead.
  204. *
  205. * Inputs:
  206. *
  207. * cmds - Command bytes to be sent to YKUSH.
  208. * resp - Array for storing response bytes
  209. * num - Number of entries in cmds and resp
  210. *
  211. * Outputs:
  212. *
  213. * The function will return the response byte or zero.
  214. *
  215. ********************************************************************************/
  216. char commands(char *cmd, char *resp, int num)
  217. {
  218. return commandsBySerial(NULL, cmd, resp, num);
  219. }
  220. /*******************************************************************************
  221. * commandBySerial(char *iSerial, char cmd) - Sends the command to YKUSH board
  222. * -----------------------------------------------------------------------------
  223. *
  224. * Description:
  225. *
  226. * The command in the variable cmd is sent to YKUSH board. If more than
  227. * one YKUSH board is connected to the host it will send the command
  228. * just to the first one that appears in the enumerated list.
  229. *
  230. * If there are more than one YKUSH board connected to the same host
  231. * the commandBySerial function should be used instead.
  232. *
  233. * Inputs:
  234. *
  235. * iSerial - Serial number of the YKUSH board to issue the command.
  236. * cmd - Command byte to be sent to YKUSH.
  237. *
  238. * Outputs:
  239. *
  240. * The function will return the response byte or zero.
  241. *
  242. ********************************************************************************/
  243. char commandBySerial(char *iSerial, char cmd)
  244. {
  245. char resp;
  246. commandsBySerial(iSerial, &cmd, &resp, 1);
  247. return resp;
  248. }
  249. /*******************************************************************************
  250. * command(char cmd) - Sends the command to YKUSH board
  251. * -----------------------------------------------------------------------------
  252. *
  253. * Description:
  254. *
  255. * The command in the variable cmd is sent to YKUSH board. If more than
  256. * one YKUSH board is connected to the host it will send the command
  257. * just to the first one that appears in the enumerated list.
  258. *
  259. * If there are more than one YKUSH board connected to the same host
  260. * the commandBySerial function should be used instead.
  261. *
  262. * Inputs:
  263. *
  264. * cmd - Command byte to be sent to YKUSH.
  265. *
  266. * Outputs:
  267. *
  268. * The function will return the response byte.
  269. *
  270. ********************************************************************************/
  271. char command(char cmd)
  272. {
  273. return commandBySerial(NULL, cmd);
  274. }