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.

297 lines
7.6 KiB

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