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.

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