commandParser.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /****************************************************************************
  2. FileName: commandParser.cpp
  3. Dependencies: See INCLUDES section
  4. Compiler: Visual Studio Community 2015
  5. Company: Yepkit, Lda.
  6. Software License Agreement:
  7. Copyright (c) 2015 Yepkit Lda
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. *****************************************************************************
  24. File Description:
  25. Change History:
  26. Rev Date Description
  27. ---- ----------- -----------------------------------------
  28. 1.0 2015-09-11 First Release
  29. ****************************************************************************
  30. * Summary:
  31. * Main program function
  32. *
  33. *
  34. *
  35. *****************************************************************************/
  36. // INCLUDES ---------------------------------------------------------------
  37. #include <cstdlib>
  38. #include <iostream>
  39. #include <stdio.h>
  40. #include "stdafx.h"
  41. #include "commandParser.h"
  42. #include "usbcom.h"
  43. using namespace std;
  44. enum cmdAction {
  45. PORT_UP,
  46. PORT_DOWN,
  47. LIST_DEVICES,
  48. DISPLAY_SERIAL_NUMBER,
  49. GET_PORT_STATUS,
  50. PRINT_HELP,
  51. };
  52. bool bySerial = false;
  53. int commandParser(int argc, char** argv) {
  54. char choice;
  55. char cmd = 0x00;
  56. enum cmdAction action = PRINT_HELP;
  57. char *iSerial=NULL;
  58. char response;
  59. char port = 0;
  60. if ( argc <= 1){
  61. printUsage(argv[0]);
  62. return 0;
  63. }
  64. //Parse input options and define action
  65. switch (argc) {
  66. case 2:
  67. if ((argv[1][0]=='-') && (argv[1][1]=='l')) {
  68. action = LIST_DEVICES;
  69. } else {
  70. action = PRINT_HELP;
  71. }
  72. break;
  73. case 3:
  74. // Single Option
  75. if ((argv[1][0] == '-') && (argv[1][1]=='d')) {
  76. action = PORT_DOWN;
  77. port = argv[2][0];
  78. } else if ((argv[1][0] == '-') && (argv[1][1]=='u')) {
  79. action = PORT_UP;
  80. port = argv[2][0];
  81. } else if ((argv[1][0] == '-') && (argv[1][1]=='g')) {
  82. action = GET_PORT_STATUS;
  83. port = argv[2][0];
  84. } else {
  85. action = PRINT_HELP;
  86. }
  87. break;
  88. case 5:
  89. // Two Options
  90. if ((argv[1][0] == '-') && (argv[1][1]=='s')) {
  91. bySerial = true;
  92. iSerial = argv[2];
  93. }
  94. if ((argv[3][0] == '-') && (argv[3][1]=='d')) {
  95. action = PORT_DOWN;
  96. port = argv[4][0];
  97. } else if ((argv[3][0] == '-') && (argv[3][1]=='u')) {
  98. action = PORT_UP;
  99. port = argv[4][0];
  100. } else if ((argv[3][0] == '-') && (argv[3][1]=='g')) {
  101. action = GET_PORT_STATUS;
  102. port = argv[4][0];
  103. } else {
  104. action = PRINT_HELP;
  105. }
  106. break;
  107. default:
  108. printUsage(argv[0]);
  109. break;
  110. }
  111. //Get options values and execute action
  112. if ( action == PORT_DOWN || action == PORT_UP ) {
  113. switch(port) {
  114. case '1':
  115. // Downstream 1 down
  116. cmd = 0x01;
  117. break;
  118. case '2':
  119. // Downstream 2 down
  120. cmd = 0x02;
  121. break;
  122. case '3':
  123. // Downstream 3 down
  124. cmd = 0x03;
  125. break;
  126. case 'a':
  127. // All downstreams down
  128. cmd = 0x0a;
  129. break;
  130. default:
  131. printUsage(argv[0]);
  132. return -1;
  133. break;
  134. }
  135. // PORT_UP has 0x11 - 0x1a, while PORT_DOWN has 0x01 - 0x0a
  136. if ( action == PORT_UP )
  137. cmd += 0x10;
  138. if (bySerial)
  139. commandBySerial(iSerial, cmd);
  140. else
  141. command(cmd);
  142. }
  143. if (action == GET_PORT_STATUS && port == 'a') {
  144. char cmds[3] = { 0x21, 0x22, 0x23 };
  145. char resps[3];
  146. int i;
  147. if (bySerial)
  148. commandsBySerial(iSerial, cmds, resps, 3);
  149. else
  150. commands(cmds, resps, 3);
  151. for (i = 0; i < 3; i++) {
  152. response = resps[i] + 0x20 - cmds[i];
  153. if (response == 0x10 + cmd) {
  154. printf("Downstream port %d is: UP\n", i+1);
  155. } else if (response == cmd) {
  156. printf("Downstream port %d is: DOWN\n", i+1);
  157. } else {
  158. printf("Downstream port %d is: UNKNOWN\n", i+1);
  159. }
  160. }
  161. } else if (action == GET_PORT_STATUS) {
  162. switch (port) {
  163. case '1':
  164. //downstream 1 status
  165. cmd = 0x1;
  166. break;
  167. case '2':
  168. //downstream 2 status
  169. cmd = 0x2;
  170. break;
  171. case '3':
  172. //downstream 3 status
  173. cmd = 0x3;
  174. break;
  175. default:
  176. printUsage(argv[0]);
  177. return -1;
  178. break;
  179. }
  180. if (bySerial) //target board specified by serial number
  181. response = commandBySerial(iSerial,cmd + 0x20);
  182. else
  183. response = command(cmd + 0x20);
  184. if (response == 0x10 + cmd) {
  185. printf("\nDownstream port %c is: UP\n", port);
  186. } else if (response == cmd) {
  187. printf("\nDownstream port %c is: DOWN\n", port);
  188. } else {
  189. printf("\nUnable to get port status for port %c\n", port);
  190. }
  191. }
  192. if ( action == LIST_DEVICES ) {
  193. printf("\nAttached YKUSH Boards\n");
  194. printf("\n---------------------\n");
  195. listDevices();
  196. }
  197. if ( action == PRINT_HELP ) {
  198. printUsage(argv[0]);
  199. }
  200. return 0;
  201. }
  202. int printUsage(char* execName){
  203. printf("\n-------------------");
  204. printf("\n\tUsage:\n");
  205. printf("-------------------\n");
  206. printf("\n%s -d downstream_number \t\tTurns DOWN the downstream port with the number downstream_number\n", execName);
  207. printf("\n%s -u downstream_number \t\tTurns UP the downstream port with the number downstream_number\n", execName);
  208. printf("\n%s -g downstream_number \t\tObtains the switching status of port with the number downstream_number\n", execName);
  209. printf("\n%s -l \t\t\t\tLists all currently attached YKUSH boards\n", execName);
  210. printf("\n%s -s serial_number -d downstream_number \tTurns DOWN the downstream port with the number downstream_number for the board with the specified serial number\n", execName);
  211. printf("\n%s -s serial_number -u downstream_number \tTurns UP the downstream port with the number downstream_number for the board with the specified serial number\n\n\n", execName);
  212. printf("\n%s -s serial_number -g downstream_number \tObtains the switching status of port with the number downstream_number for the board with the specified serial number\n\n\n", execName);
  213. return 0;
  214. }