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.

317 lines
8.2 KiB

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