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.

154 lines
3.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. usbrelay: 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 "usbrelay.h"
  19. #include "libusbrelay.h"
  20. #include <ctype.h>
  21. #include <hidapi/hidapi.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <wchar.h>
  27. int main(int argc, char * argv[])
  28. {
  29. struct relay * relays = 0;
  30. char arg_t[20] = {'\0'};
  31. int debug = 0;
  32. char * token;
  33. const char delimiters[] = "_=";
  34. int i;
  35. int size = sizeof(struct relay);
  36. int exit_code = 0;
  37. /* allocate the memory for all the relays */
  38. if (argc > 1)
  39. {
  40. relays = malloc(size * (argc + 1)); /* Yeah, I know. Not using the first member */
  41. relays[0].this_serial[0] = '\0';
  42. }
  43. else
  44. {
  45. debug = 1;
  46. }
  47. /* loop through the command line and grab the relay details */
  48. for (i = 1; i < argc; i++)
  49. {
  50. /* copy the arg and bounds check */
  51. strncpy(arg_t, argv[i], 19);
  52. arg_t[19] = '\0';
  53. token = strtok(arg_t, delimiters);
  54. if (token != NULL)
  55. {
  56. strcpy(relays[i].this_serial, token);
  57. }
  58. token = strtok((char *) NULL, delimiters);
  59. if (token != NULL)
  60. {
  61. relays[i].relay_num = atoi(token);
  62. }
  63. token = strtok(NULL, delimiters);
  64. if (relays[i].relay_num == 0)
  65. { /* command to change the serial - remaining token is the new serial */
  66. if (token != NULL)
  67. {
  68. strcpy(relays[i].new_serial, token);
  69. }
  70. }
  71. else
  72. {
  73. if (token != NULL)
  74. {
  75. if (atoi(token))
  76. {
  77. relays[i].state = CMD_ON;
  78. }
  79. else
  80. {
  81. relays[i].state = CMD_OFF;
  82. }
  83. }
  84. }
  85. fprintf(stderr, "Orig: %s, Serial: %s, Relay: %d State: %x\n", arg_t, relays[i].this_serial, relays[i].relay_num, relays[i].state);
  86. relays[i].found = 0;
  87. }
  88. // Locate and identify attached relay boards
  89. enumerate_relay_boards(getenv("USBID"), 1, debug);
  90. /* loop through the supplied command line and try to match the serial */
  91. for (i = 1; i < argc; i++)
  92. {
  93. fprintf(stderr, "Serial: %s, Relay: %d State: %x \n", relays[i].this_serial, relays[i].relay_num, relays[i].state);
  94. relay_board * board = find_board(relays[i].this_serial);
  95. if (board)
  96. {
  97. fprintf(stderr, "%d HID Serial: %s ", i, board->serial);
  98. if (relays[i].relay_num == 0)
  99. {
  100. if (! relays[i].new_serial[0])
  101. {
  102. fprintf(stderr, "\n \n New serial can't be empty!\n");
  103. }
  104. else
  105. {
  106. set_serial(board->serial, relays[i].new_serial);
  107. }
  108. }
  109. else
  110. {
  111. fprintf(stderr, "Serial: %s, Relay: %d State: %x\n", relays[i].this_serial, relays[i].relay_num, relays[i].state);
  112. if (operate_relay(board->serial, relays[i].relay_num, relays[i].state) < 0)
  113. {
  114. exit_code++;
  115. }
  116. relays[i].found = 1;
  117. }
  118. }
  119. fprintf(stderr, "\n");
  120. }
  121. shutdown();
  122. for (i = 1; i < argc; i++)
  123. {
  124. fprintf(stderr, "Serial: %s, Relay: %d State: %x ", relays[i].this_serial, relays[i].relay_num, relays[i].state);
  125. if (relays[i].found)
  126. {
  127. fprintf(stderr, "--- Found\n");
  128. }
  129. else
  130. {
  131. fprintf(stderr, "--- Not Found\n");
  132. exit_code++;
  133. }
  134. }
  135. if (relays)
  136. {
  137. free(relays);
  138. }
  139. exit(exit_code);
  140. }