You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
7.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. libusbrelay: Control USB HID connected electrical relay modules
  3. Copyright (C) 2014 Darryl Bond
  4. Library version
  5. Copyright (C) 2019 Sean Mollet
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License along
  15. with this program; if not, write to the Free Software Foundation, Inc.,
  16. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "libusbrelay.h"
  19. #include <ctype.h>
  20. #include <hidapi/hidapi.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <wchar.h>
  26. // Global variables
  27. unsigned short vendor_id = 0x16c0;
  28. unsigned short product_id = 0x05df;
  29. relay_board * relay_boards = 0;
  30. int relay_board_count = 0;
  31. int i, k;
  32. // Private function prototypes
  33. static int get_board_features(relay_board * board, hid_device * handle);
  34. /**
  35. * Enumerate all possible relay devices in the system
  36. */
  37. int enumerate_relay_boards(const char * product, int verbose, int debug)
  38. {
  39. char * vendor;
  40. int result = 0;
  41. struct hid_device_info *devs, *cur_dev;
  42. // If we were given a product code, use it
  43. if (product != NULL)
  44. {
  45. vendor = strsep((char **) &product, ":");
  46. if (vendor && *vendor)
  47. {
  48. vendor_id = strtol(vendor, NULL, 16);
  49. }
  50. if (product && *product)
  51. {
  52. product_id = strtol(product, NULL, 16);
  53. }
  54. }
  55. // Enumerate USB devices with the given IDs
  56. devs = hid_enumerate(vendor_id, product_id);
  57. // Count the number of returned devices
  58. cur_dev = devs;
  59. for (relay_board_count = 0; cur_dev != NULL; relay_board_count++)
  60. {
  61. cur_dev = cur_dev->next;
  62. }
  63. // Allocate a buffer for the relays
  64. if (relay_board_count > 0)
  65. {
  66. relay_boards = malloc((relay_board_count) * sizeof(relay_board));
  67. // Fill the relay structs
  68. cur_dev = devs;
  69. for (i = 0; i < relay_board_count; i++)
  70. {
  71. // Save the path to this device
  72. relay_boards[i].path = malloc(strlen(cur_dev->path) + 1);
  73. memcpy(relay_boards[i].path, cur_dev->path, strlen(cur_dev->path) + 1);
  74. // The product string is USBRelayx where x is number of relays read to the \0 in case there are more than 9
  75. relay_boards[i].relay_count = atoi((const char *) &cur_dev->product_string[8]);
  76. // Open it to get more details
  77. hid_device * handle;
  78. handle = hid_open_path(cur_dev->path);
  79. if (handle)
  80. {
  81. result = get_board_features(&relay_boards[i], handle);
  82. hid_close(handle);
  83. }
  84. else
  85. {
  86. perror("unable to open device\n");
  87. result = -1;
  88. }
  89. // Output the device enumeration details if verbose is on
  90. if (verbose)
  91. {
  92. fprintf(stderr, "Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
  93. fprintf(stderr, "\n");
  94. fprintf(stderr, " Manufacturer: %ls\n", cur_dev->manufacturer_string);
  95. fprintf(stderr, " Product: %ls\n", cur_dev->product_string);
  96. fprintf(stderr, " Release: %hx\n", cur_dev->release_number);
  97. fprintf(stderr, " Interface: %d\n", cur_dev->interface_number);
  98. fprintf(stderr, " Number of Relays = %d\n", relay_boards[i].relay_count);
  99. // If verbose and debug are on, output individual relay details
  100. if (debug)
  101. {
  102. for (k = 0; k < relay_boards[i].relay_count; k++)
  103. {
  104. if (relay_boards[i].state & 1 << k)
  105. {
  106. printf("%s_%d=1\n", relay_boards[i].serial, k + 1);
  107. }
  108. else
  109. {
  110. printf("%s_%d=0\n", relay_boards[i].serial, k + 1);
  111. }
  112. }
  113. }
  114. }
  115. cur_dev = cur_dev->next;
  116. }
  117. }
  118. hid_free_enumeration(devs);
  119. return result;
  120. }
  121. /**
  122. * Command a relay at a particular /dev path to switch to a given state
  123. */
  124. int operate_relay(const char * serial, unsigned char relay, unsigned char target_state)
  125. {
  126. unsigned char buf[9]; // 1 extra byte for the report ID
  127. int res = -1;
  128. hid_device * handle;
  129. relay_board * board = find_board(serial);
  130. if (board != NULL && relay > 0 && relay <= board->relay_count)
  131. {
  132. handle = hid_open_path(board->path);
  133. if (handle)
  134. {
  135. buf[0] = 0x0; // report number
  136. buf[1] = target_state;
  137. buf[2] = relay;
  138. buf[3] = 0x00;
  139. buf[4] = 0x00;
  140. buf[5] = 0x00;
  141. buf[6] = 0x00;
  142. buf[7] = 0x00;
  143. buf[8] = 0x00;
  144. res = hid_write(handle, buf, sizeof(buf));
  145. }
  146. else
  147. {
  148. res = -1;
  149. }
  150. if (res > 0)
  151. {
  152. // Update our relay status
  153. res = get_board_features(board, handle);
  154. }
  155. else
  156. {
  157. fprintf(stderr, "Unable to write()\n");
  158. fprintf(stderr, "Error: %ls\n", hid_error(handle));
  159. }
  160. hid_close(handle);
  161. }
  162. return (res);
  163. }
  164. int set_serial(const char * serial, char * newserial)
  165. {
  166. unsigned char buf[9]; // 1 extra byte for the report ID
  167. int res = -1;
  168. hid_device * handle;
  169. relay_board * board = find_board(serial);
  170. if (board != NULL)
  171. {
  172. handle = hid_open_path(board->path);
  173. if (handle)
  174. {
  175. buf[0] = 0x0; // report number
  176. buf[1] = CMD_SET_SERIAL;
  177. buf[2] = newserial[0];
  178. buf[3] = newserial[1];
  179. buf[4] = newserial[2];
  180. buf[5] = newserial[3];
  181. buf[6] = newserial[4];
  182. buf[7] = 0x00;
  183. buf[8] = 0x00;
  184. res = hid_write(handle, buf, sizeof(buf));
  185. }
  186. else
  187. {
  188. res = -1;
  189. }
  190. if (res > 0)
  191. {
  192. // Update our copy of the serial number
  193. res = get_board_features(board, handle);
  194. }
  195. else
  196. {
  197. fprintf(stderr, "Unable to write()\n");
  198. fprintf(stderr, "Error: %ls\n", hid_error(handle));
  199. }
  200. hid_close(handle);
  201. }
  202. return (res);
  203. }
  204. /**
  205. * Find a board path given a relay board serial
  206. */
  207. relay_board * find_board(const char * serial)
  208. {
  209. for (i = 0; i < relay_board_count; i++)
  210. {
  211. if (strcmp(relay_boards[i].serial, serial) == 0)
  212. {
  213. return &relay_boards[i];
  214. }
  215. }
  216. return NULL;
  217. }
  218. /**
  219. * Return the count of relay boards
  220. */
  221. int get_relay_board_count()
  222. {
  223. return relay_board_count;
  224. }
  225. /**
  226. * Return the actual relay_board structs
  227. */
  228. relay_board * get_relay_boards()
  229. {
  230. return relay_boards;
  231. }
  232. /**
  233. * Return all allocated resources and perform any other cleanup
  234. */
  235. void shutdown()
  236. {
  237. /* Free static HIDAPI objects. */
  238. hid_exit();
  239. for (i = 0; i < relay_board_count; i++)
  240. {
  241. free(relay_boards[i].path);
  242. }
  243. if (relay_board_count > 0)
  244. {
  245. free(relay_boards);
  246. }
  247. }
  248. // Private functions
  249. /**
  250. * Load the board serial and relay status
  251. * This intentionally reuses a handle as it's meant to be called by the
  252. * other functions while they have one already open
  253. */
  254. static int get_board_features(relay_board * board, hid_device * handle)
  255. {
  256. unsigned char buf[9];
  257. // Get the features of the device
  258. buf[0] = 0x01;
  259. int ret = hid_get_feature_report(handle, buf, sizeof(buf));
  260. if (ret == -1)
  261. {
  262. perror("hid_get_feature_report\n");
  263. }
  264. // Set the serial number (0x0 for termination)
  265. memset(board->serial, 0x0, sizeof(board->serial));
  266. memcpy(board->serial, buf, Serial_Length);
  267. // Byte 7 in the response contains the target_state of the relays
  268. board->state = buf[7];
  269. return ret;
  270. }