ykush2.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 "stdafx.h"
  14. #include "ykush2.h"
  15. #include <stdio.h>
  16. #include <ykush_help.h>
  17. enum ykush2Action
  18. {
  19. PORT_UP,
  20. PORT_DOWN,
  21. PORT_STATUS,
  22. LIST_BOARDS,
  23. GET_STATUS,
  24. HELP
  25. };
  26. /*********************************************************
  27. * Function: ykushxs_cmd_parser
  28. *
  29. * Description:
  30. *
  31. * Does the parsing of the user command and call the
  32. * action to be undertaken.
  33. *
  34. *
  35. *
  36. *
  37. *
  38. *
  39. *
  40. *********************************************************/
  41. void ykush2_cmd_parser(int argc, char** argv)
  42. {
  43. char bySerialFlag = 0;
  44. enum ykush2Action action = HELP;
  45. Ykush2 *ykush = new Ykush2(0xEFED);
  46. char port;
  47. char status_response = 0;
  48. Help *help = new Help(argv[0]);
  49. if((argv[2][0]=='-') && (argv[2][1]=='s'))
  50. {
  51. if(argc < 6)
  52. {
  53. //ykush_help(argv[0]);
  54. help->print_ykush2();
  55. return;
  56. }
  57. bySerialFlag = 1;
  58. if(argv[4][0]=='-' && argv[4][1]=='u')
  59. {
  60. action = PORT_UP;
  61. port = argv[5][0];
  62. } else if(argv[4][0]=='-' && argv[4][1]=='d')
  63. {
  64. action = PORT_DOWN;
  65. port = argv[5][0];
  66. } else if(argv[4][0]=='-' && argv[4][1]=='l')
  67. {
  68. action = LIST_BOARDS;
  69. } else if(argv[4][0]=='-' && argv[4][1]=='g')
  70. {
  71. action = GET_STATUS;
  72. port = argv[5][0];
  73. } else
  74. {
  75. //ykush_help(argv[0]);
  76. help->print_ykush2();
  77. return;
  78. }
  79. }
  80. else if((argv[2][0]=='-') && (argv[2][1]=='u'))
  81. {
  82. if(argc < 4)
  83. {
  84. help->print_ykush2();
  85. return;
  86. }
  87. action = PORT_UP;
  88. port = argv[3][0];
  89. }
  90. else if((argv[2][0]=='-') && (argv[2][1]=='d'))
  91. {
  92. if(argc < 4)
  93. {
  94. help->print_ykush2();
  95. return;
  96. }
  97. action = PORT_DOWN;
  98. port = argv[3][0];
  99. }
  100. else if((argv[2][0]=='-') && (argv[2][1]=='l'))
  101. {
  102. action = LIST_BOARDS;
  103. }
  104. else if((argv[2][0]=='-') && (argv[2][1]=='g'))
  105. {
  106. if(argc < 4)
  107. {
  108. help->print_ykush2();
  109. return;
  110. }
  111. action = GET_STATUS;
  112. port = argv[3][0];
  113. }
  114. else
  115. {
  116. //ykush_help(argv[0]);
  117. help->print_ykush2();
  118. return;
  119. }
  120. switch (action)
  121. {
  122. case PORT_UP:
  123. if(bySerialFlag)
  124. {
  125. ykush->port_up(argv[3], port);
  126. }
  127. else
  128. {
  129. ykush->port_up(NULL, port);
  130. }
  131. break;
  132. case PORT_DOWN:
  133. if(bySerialFlag)
  134. {
  135. ykush->port_down(argv[3], port);
  136. }
  137. else
  138. {
  139. ykush->port_down(NULL, port);
  140. }
  141. break;
  142. case LIST_BOARDS:
  143. ykush2_list_attached();
  144. break;
  145. case GET_STATUS:
  146. if(bySerialFlag)
  147. {
  148. status_response = ykush->get_port_status(argv[3], port);
  149. if (status_response >> 4)
  150. {
  151. printf("\n\nDownstream port %d is ON\n\n", status_response & 0x0F );
  152. }
  153. else
  154. {
  155. printf("\n\nDownstream port %d is OFF\n\n", status_response & 0x0F);
  156. }
  157. }
  158. else
  159. {
  160. status_response = ykush->get_port_status(NULL, port);
  161. if (status_response >> 4)
  162. {
  163. printf("\n\nDownstream port %d is ON\n\n", status_response & 0x0F);
  164. }
  165. else
  166. {
  167. printf("\n\nDownstream port %d is OFF\n\n", status_response & 0x0F);
  168. }
  169. }
  170. break;
  171. default:
  172. //ykush_help(argv[0]);
  173. help->print_ykush2();
  174. break;
  175. }
  176. }
  177. /********************************************************
  178. ********************************************************
  179. *
  180. * Ykush Class Implementation
  181. *
  182. ********************************************************
  183. ********************************************************/
  184. /*********************************************************
  185. * Method: port_up
  186. *
  187. * Description:
  188. *
  189. * Commands the YKUSH XS board to turn ON the downstream
  190. * port.
  191. *
  192. *
  193. *
  194. *
  195. *********************************************************/
  196. int Ykush2::port_up(char *serial, char port)
  197. {
  198. switch(port)
  199. {
  200. case '1':
  201. hid_report_out[0] = 0x11;
  202. break;
  203. case '2':
  204. hid_report_out[0] = 0x12;
  205. break;
  206. case '3':
  207. hid_report_out[0] = 0x13;
  208. break;
  209. case 'a':
  210. hid_report_out[0] = 0x1a;
  211. break;
  212. default:
  213. return 0;
  214. break;
  215. }
  216. //send HID report to board
  217. if(is_legacy)
  218. {
  219. return sendHidReport(serial, hid_report_out, hid_report_in, 6);
  220. }
  221. else
  222. {
  223. return sendHidReport(serial, hid_report_out, hid_report_in, 65);
  224. }
  225. }
  226. /*********************************************************
  227. * Method: port_down
  228. *
  229. * Description:
  230. *
  231. * Commands the YKUSH XS board to turn OFF the downstream
  232. * port.
  233. *
  234. *
  235. *
  236. *
  237. *********************************************************/
  238. int Ykush2::port_down(char *serial, char port)
  239. {
  240. switch(port)
  241. {
  242. case '1':
  243. hid_report_out[0] = 0x01;
  244. break;
  245. case '2':
  246. hid_report_out[0] = 0x02;
  247. break;
  248. case '3':
  249. hid_report_out[0] = 0x03;
  250. break;
  251. case 'a':
  252. hid_report_out[0] = 0x0a;
  253. break;
  254. default:
  255. return 0;
  256. break;
  257. }
  258. //send HID report to board
  259. if(is_legacy)
  260. {
  261. return sendHidReport(serial, hid_report_out, hid_report_in, 6);
  262. }
  263. else
  264. {
  265. return sendHidReport(serial, hid_report_out, hid_report_in, 64);
  266. }
  267. }
  268. /*********************************************************
  269. * Method: get_port_status
  270. *
  271. * Description:
  272. *
  273. * Commands the YKUSH XS to respond with the port status.
  274. *
  275. * 1 -> Port is ON/UP
  276. * 0 -> PORT is OFF/DOWN
  277. *
  278. *
  279. *
  280. *
  281. *********************************************************/
  282. int Ykush2::get_port_status(char *serial, char port)
  283. {
  284. int status;
  285. switch(port)
  286. {
  287. case '1':
  288. hid_report_out[0] = 0x21;
  289. break;
  290. case '2':
  291. hid_report_out[0] = 0x22;
  292. break;
  293. case '3':
  294. hid_report_out[0] = 0x23;
  295. break;
  296. default:
  297. return 0;
  298. break;
  299. }
  300. //send HID report to board
  301. if(is_legacy)
  302. {
  303. sendHidReport(serial, hid_report_out, hid_report_in, 6);
  304. }
  305. else
  306. {
  307. sendHidReport(serial, hid_report_out, hid_report_in, 64);
  308. }
  309. //handle board response HID report
  310. status = hid_report_in[1];
  311. return status;
  312. }
  313. /****************************************************
  314. *
  315. *
  316. *
  317. *
  318. ****************************************************/
  319. void ykush2_list_attached()
  320. {
  321. Ykush2 *ykush = new Ykush2(0xEFED);
  322. Ykush2 *ykushLegacy = new Ykush2(0x0042);
  323. printf("\n\nAttached YKUSH Boards:\n");
  324. if(ykush->listConnected()==0)
  325. {
  326. //try legacy
  327. if(ykushLegacy->listConnected()==0)
  328. {
  329. printf("\n\nNo YKUSH boards found.");
  330. }
  331. }
  332. else
  333. {
  334. //list legacy boards if attached
  335. ykushLegacy->listConnected();
  336. }
  337. printf("\n\n");
  338. }