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.

319 lines
7.3 KiB

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