ykush.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*******************************************************************************
  2. Copyright 2017 Yepkit Lda (www.yepkit.com)
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. *******************************************************************************/
  13. #include "ykush.h"
  14. #include <stdio.h>
  15. #include <ykush_help.h>
  16. #include <command_parser.h>
  17. #include <iostream>
  18. int ykush_cmd_parser(int argc, char** argv)
  19. {
  20. char bySerialFlag = 0;
  21. enum ykushAction action = YKUSH_HELP;
  22. Ykush ykush( 0xF2F7 );
  23. Ykush ykushLegacy( 0x0042 );
  24. char port;
  25. char status_response = 0;
  26. if((argv[1][0]=='-') && (argv[1][1]=='s')) {
  27. if(argc < 5) {
  28. ykush.print_help(argv[0]);
  29. return -1;
  30. }
  31. bySerialFlag = 1;
  32. if(argv[3][0]=='-' && argv[3][1]=='u') {
  33. action = YKUSH_PORT_UP;
  34. port = argv[4][0];
  35. } else if(argv[3][0]=='-' && argv[3][1]=='d') {
  36. action = YKUSH_PORT_DOWN;
  37. port = argv[4][0];
  38. } else if(argv[3][0]=='-' && argv[3][1]=='l') {
  39. action = YKUSH_LIST_BOARDS;
  40. } else if(argv[3][0]=='-' && argv[3][1]=='g') {
  41. action = YKUSH_GET_STATUS;
  42. port = argv[4][0];
  43. } else {
  44. ykush.print_help(argv[0]);
  45. return -1;
  46. }
  47. } else if((argv[1][0]=='-') && (argv[1][1]=='u')) {
  48. if(argc < 3) {
  49. ykush.print_help(argv[0]);
  50. return -1;
  51. }
  52. action = YKUSH_PORT_UP;
  53. port = argv[2][0];
  54. } else if((argv[1][0]=='-') && (argv[1][1]=='d')) {
  55. if(argc < 3) {
  56. ykush.print_help(argv[0]);
  57. return -1;
  58. }
  59. action = YKUSH_PORT_DOWN;
  60. port = argv[2][0];
  61. } else if((argv[1][0]=='-') && (argv[1][1]=='l')) {
  62. action = YKUSH_LIST_BOARDS;
  63. } else if((argv[1][0]=='-') && (argv[1][1]=='g')) {
  64. if(argc < 3) {
  65. ykush.print_help(argv[0]);
  66. return -1;
  67. }
  68. action = YKUSH_GET_STATUS;
  69. port = argv[2][0];
  70. } else {
  71. ykush.print_help(argv[0]);
  72. return -1;
  73. }
  74. switch (action) {
  75. case YKUSH_PORT_UP:
  76. if(bySerialFlag) {
  77. if(ykush.port_up(argv[2], port) == -1) {
  78. ykushLegacy.port_up(argv[2], port);
  79. }
  80. } else {
  81. if(ykush.port_up(NULL, port) == -1) {
  82. ykushLegacy.port_up(NULL, port);
  83. }
  84. }
  85. break;
  86. case YKUSH_PORT_DOWN:
  87. if ( bySerialFlag ) {
  88. if ( ykush.port_down(argv[2], port) < 0 ) {
  89. return ykushLegacy.port_down(argv[2], port);
  90. }
  91. } else {
  92. if ( ykush.port_down(NULL, port) < 0 ) {
  93. return ykushLegacy.port_down(NULL, port);
  94. }
  95. }
  96. break;
  97. case YKUSH_LIST_BOARDS:
  98. return ykush_list_attached();
  99. break;
  100. case YKUSH_GET_STATUS:
  101. if ( bySerialFlag ) {
  102. status_response = ykush.get_port_status(argv[2], port);
  103. if ( status_response >> 4 ) {
  104. printf("\n\nDownstream port %d is ON\n\n", status_response & 0x0F );
  105. } else {
  106. printf("\n\nDownstream port %d is OFF\n\n", status_response & 0x0F);
  107. }
  108. } else {
  109. status_response = ykush.get_port_status(NULL, port);
  110. if ( status_response >> 4 ) {
  111. printf("\n\nDownstream port %d is ON\n\n", status_response & 0x0F);
  112. } else {
  113. printf("\n\nDownstream port %d is OFF\n\n", status_response & 0x0F);
  114. }
  115. }
  116. break;
  117. default:
  118. ykush.print_help(argv[0]);
  119. return -1;
  120. break;
  121. }
  122. return 0;
  123. }
  124. int Ykush::port_up(char *serial, char port)
  125. {
  126. switch(port) {
  127. case '1':
  128. hid_report_out[0] = 0x11;
  129. break;
  130. case '2':
  131. hid_report_out[0] = 0x12;
  132. break;
  133. case '3':
  134. hid_report_out[0] = 0x13;
  135. break;
  136. case 'a':
  137. hid_report_out[0] = 0x1a;
  138. break;
  139. default:
  140. return -1;
  141. break;
  142. }
  143. int res;
  144. if(is_legacy)
  145. res = sendHidReport(serial, hid_report_out, hid_report_in, 6);
  146. else
  147. res = sendHidReport(serial, hid_report_out, hid_report_in, 64);
  148. if ( res < 1 )
  149. return res;
  150. if ( hid_report_in[0] == 0x01 )
  151. return 0;
  152. return -1;
  153. }
  154. int Ykush::port_down(char *serial, char port)
  155. {
  156. switch(port){
  157. case '1':
  158. hid_report_out[0] = 0x01;
  159. break;
  160. case '2':
  161. hid_report_out[0] = 0x02;
  162. break;
  163. case '3':
  164. hid_report_out[0] = 0x03;
  165. break;
  166. case 'a':
  167. hid_report_out[0] = 0x0a;
  168. break;
  169. default:
  170. return -1;
  171. break;
  172. }
  173. int res;
  174. if(is_legacy)
  175. res = sendHidReport(serial, hid_report_out, hid_report_in, 6);
  176. else
  177. res = sendHidReport(serial, hid_report_out, hid_report_in, 64);
  178. if ( res < 1 )
  179. return res;
  180. if ( hid_report_in[0] == 0x01 )
  181. return 0;
  182. return -1;
  183. }
  184. int Ykush::get_port_status(char *serial, char port)
  185. {
  186. switch(port) {
  187. case '1':
  188. hid_report_out[0] = 0x21;
  189. break;
  190. case '2':
  191. hid_report_out[0] = 0x22;
  192. break;
  193. case '3':
  194. hid_report_out[0] = 0x23;
  195. break;
  196. default:
  197. return -1;
  198. break;
  199. }
  200. int res;
  201. if(is_legacy)
  202. res = sendHidReport(serial, hid_report_out, hid_report_in, 6);
  203. else
  204. res = sendHidReport(serial, hid_report_out, hid_report_in, 64);
  205. if ( res < 0 )
  206. return res;
  207. if ( hid_report_in[0] == 0x01 )
  208. return hid_report_in[1];
  209. return -1;
  210. }
  211. int ykush_list_attached()
  212. {
  213. Ykush ykush( 0xF2F7 );
  214. Ykush ykushLegacy( 0x0042 );
  215. printf("\nAttached YKUSH Boards:\n");
  216. if( ykush.listConnected() == 0 ) {
  217. if( ykushLegacy.listConnected() == 0 ) {
  218. printf("\nNo YKUSH boards found.");
  219. }
  220. } else {
  221. ykushLegacy.listConnected();
  222. }
  223. printf("\n");
  224. return 0;
  225. }
  226. void Ykush::print_help(char *exec_name)
  227. {
  228. Help help( exec_name );
  229. help.print_version();
  230. help.print_usage();
  231. help.print_ykush();
  232. }