PD Buddy Sink Firmware
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.

shell.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * PD Buddy - USB Power Delivery for everyone
  3. * Copyright (C) 2017 Clayton G. Hobbs <clay@lakeserv.net>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  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. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio
  20. Licensed under the Apache License, Version 2.0 (the "License");
  21. you may not use this file except in compliance with the License.
  22. You may obtain a copy of the License at
  23. http://www.apache.org/licenses/LICENSE-2.0
  24. Unless required by applicable law or agreed to in writing, software
  25. distributed under the License is distributed on an "AS IS" BASIS,
  26. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27. See the License for the specific language governing permissions and
  28. limitations under the License.
  29. */
  30. #include "shell.h"
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "chprintf.h"
  34. #include "usbcfg.h"
  35. #include "storage.h"
  36. #include "led.h"
  37. #include "pd.h"
  38. /* Buffer for unwritten configuration */
  39. static struct pdb_config tmpcfg = {
  40. .status = PDB_CONFIG_STATUS_VALID
  41. };
  42. static void cmd_license(BaseSequentialStream *chp, int argc, char *argv[])
  43. {
  44. (void) argv;
  45. if (argc > 0) {
  46. chprintf(chp, "Usage: license\r\n");
  47. return;
  48. }
  49. chprintf(chp,
  50. "PD Buddy - USB Power Delivery for everyone\r\n"
  51. "Copyright (C) 2017 Clayton G. Hobbs <clay@lakeserv.net>\r\n"
  52. "\r\n"
  53. "This program is free software: you can redistribute it and/or modify\r\n"
  54. "it under the terms of the GNU General Public License as published by\r\n"
  55. "the Free Software Foundation, either version 3 of the License, or\r\n"
  56. "(at your option) any later version.\r\n"
  57. "\r\n"
  58. "This program is distributed in the hope that it will be useful,\r\n"
  59. "but WITHOUT ANY WARRANTY; without even the implied warranty of\r\n"
  60. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r\n"
  61. "GNU General Public License for more details.\r\n"
  62. "\r\n"
  63. "You should have received a copy of the GNU General Public License\r\n"
  64. "along with this program. If not, see <http://www.gnu.org/licenses/>.\r\n"
  65. );
  66. }
  67. static void cmd_erase(BaseSequentialStream *chp, int argc, char *argv[])
  68. {
  69. (void) argv;
  70. if (argc > 0) {
  71. chprintf(chp, "Usage: erase\r\n");
  72. return;
  73. }
  74. pdb_config_flash_erase();
  75. }
  76. static void cmd_write(BaseSequentialStream *chp, int argc, char *argv[])
  77. {
  78. (void) argv;
  79. if (argc > 0) {
  80. chprintf(chp, "Usage: write\r\n");
  81. return;
  82. }
  83. pdb_config_flash_update(&tmpcfg);
  84. }
  85. static void cmd_load(BaseSequentialStream *chp, int argc, char *argv[])
  86. {
  87. (void) argv;
  88. if (argc > 0) {
  89. chprintf(chp, "Usage: load\r\n");
  90. return;
  91. }
  92. /* Get the current configuration */
  93. struct pdb_config *cfg = pdb_config_flash_read();
  94. if (cfg == NULL) {
  95. chprintf(chp, "No configuration\r\n");
  96. return;
  97. }
  98. /* Load the current configuration into tmpcfg */
  99. tmpcfg.status = cfg->status;
  100. tmpcfg.flags = cfg->flags;
  101. tmpcfg.v = cfg->v;
  102. tmpcfg.i = cfg->i;
  103. tmpcfg.v_min = cfg->v_min;
  104. tmpcfg.v_max = cfg->v_max;
  105. }
  106. static void cmd_get_cfg(BaseSequentialStream *chp, int argc, char *argv[])
  107. {
  108. struct pdb_config *cfg = NULL;
  109. if (argc > 1) {
  110. chprintf(chp, "Usage: get_cfg [index]\r\n");
  111. return;
  112. }
  113. /* With no arguments, find the current configuration */
  114. if (argc == 0) {
  115. cfg = pdb_config_flash_read();
  116. if (cfg == NULL) {
  117. chprintf(chp, "No configuration\r\n");
  118. return;
  119. }
  120. /* With an argument, get a particular configuration array index */
  121. } else if (argc == 1) {
  122. char *endptr;
  123. long i = strtol(argv[0], &endptr, 0);
  124. if (i >= 0 && i < PDB_CONFIG_ARRAY_LEN && endptr > argv[0]) {
  125. cfg = &pdb_config_array[i];
  126. } else {
  127. chprintf(chp, "Invalid index\r\n");
  128. return;
  129. }
  130. }
  131. /* Print the configuration */
  132. pdb_config_print(chp, cfg);
  133. }
  134. static void cmd_get_tmpcfg(BaseSequentialStream *chp, int argc, char *argv[])
  135. {
  136. (void) argv;
  137. if (argc > 0) {
  138. chprintf(chp, "Usage: get_tmpcfg\r\n");
  139. return;
  140. }
  141. pdb_config_print(chp, &tmpcfg);
  142. }
  143. static void cmd_clear_flags(BaseSequentialStream *chp, int argc, char *argv[])
  144. {
  145. (void) argv;
  146. if (argc > 0) {
  147. chprintf(chp, "Usage: clear_flags\r\n");
  148. return;
  149. }
  150. /* Clear all flags */
  151. tmpcfg.flags = 0;
  152. }
  153. static void cmd_toggle_giveback(BaseSequentialStream *chp, int argc, char *argv[])
  154. {
  155. (void) argv;
  156. if (argc > 0) {
  157. chprintf(chp, "Usage: toggle_giveback\r\n");
  158. return;
  159. }
  160. /* Toggle the GiveBack flag */
  161. tmpcfg.flags ^= PDB_CONFIG_FLAGS_GIVEBACK;
  162. }
  163. static void cmd_set_v(BaseSequentialStream *chp, int argc, char *argv[])
  164. {
  165. if (argc != 1) {
  166. chprintf(chp, "Usage: set_v voltage_in_mV\r\n");
  167. return;
  168. }
  169. char *endptr;
  170. long i = strtol(argv[0], &endptr, 0);
  171. if (i >= 0 && i <= UINT16_MAX && endptr > argv[0]) {
  172. /* Convert mV to the unit used by USB PD */
  173. tmpcfg.v = PD_MV2PDV(i);
  174. } else {
  175. chprintf(chp, "Invalid voltage\r\n");
  176. return;
  177. }
  178. }
  179. static void cmd_set_i(BaseSequentialStream *chp, int argc, char *argv[])
  180. {
  181. if (argc != 1) {
  182. chprintf(chp, "Usage: set_i current_in_mA\r\n");
  183. return;
  184. }
  185. char *endptr;
  186. long i = strtol(argv[0], &endptr, 0);
  187. if (i >= 0 && i <= UINT16_MAX && endptr > argv[0]) {
  188. /* Convert mA to the unit used by USB PD */
  189. tmpcfg.i = PD_MA2PDI(i);
  190. } else {
  191. chprintf(chp, "Invalid current\r\n");
  192. return;
  193. }
  194. }
  195. static void cmd_identify(BaseSequentialStream *chp, int argc, char *argv[])
  196. {
  197. (void) chp;
  198. (void) argv;
  199. if (argc > 0) {
  200. chprintf(chp, "Usage: identify\r\n");
  201. return;
  202. }
  203. chEvtSignal(pdb_led_thread, PDB_EVT_LED_IDENTIFY);
  204. }
  205. static const struct pdb_shell_cmd commands[] = {
  206. {"license", cmd_license, "Show copyright and license information"},
  207. {"erase", cmd_erase, "Erase all stored configuration"},
  208. {"write", cmd_write, "Store the configuration buffer"},
  209. {"load", cmd_load, "Load the stored configuration into the buffer"},
  210. {"get_cfg", cmd_get_cfg, "Print the stored configuration"},
  211. {"get_tmpcfg", cmd_get_tmpcfg, "Print the configuration buffer"},
  212. {"clear_flags", cmd_clear_flags, "Clear all flags"},
  213. {"toggle_giveback", cmd_toggle_giveback, "Toggle the GiveBack flag"},
  214. /* TODO {"toggle_var_bat", cmd_toggle_var_bat, "Toggle the Var/Bat flag"},*/
  215. {"set_v", cmd_set_v, "Set the voltage in millivolts"},
  216. {"set_i", cmd_set_i, "Set the current in milliamps"},
  217. /* TODO {"set_v_range", cmd_set_v_range, "Set the minimum and maximum voltage in millivolts"},*/
  218. {"identify", cmd_identify, "Blink the LED to identify the device"},
  219. {NULL, NULL, NULL}
  220. };
  221. const struct pdb_shell_cfg shell_cfg = {
  222. (BaseSequentialStream *)&SDU1,
  223. commands
  224. };
  225. /*
  226. * Utility functions for the shell
  227. */
  228. static char *_strtok(char *str, const char *delim, char **saveptr)
  229. {
  230. char *token;
  231. if (str)
  232. *saveptr = str;
  233. token = *saveptr;
  234. if (!token)
  235. return NULL;
  236. token += strspn(token, delim);
  237. *saveptr = strpbrk(token, delim);
  238. if (*saveptr)
  239. *(*saveptr)++ = '\0';
  240. return *token ? token : NULL;
  241. }
  242. static void list_commands(BaseSequentialStream *chp, const struct pdb_shell_cmd *scp)
  243. {
  244. while (scp->cmd != NULL) {
  245. chprintf(chp, "\t%s: %s\r\n", scp->cmd, scp->desc);
  246. scp++;
  247. }
  248. }
  249. static bool cmdexec(const struct pdb_shell_cmd *scp, BaseSequentialStream *chp,
  250. char *name, int argc, char *argv[])
  251. {
  252. while (scp->cmd != NULL) {
  253. if (strcmp(scp->cmd, name) == 0) {
  254. scp->func(chp, argc, argv);
  255. return false;
  256. }
  257. scp++;
  258. }
  259. return true;
  260. }
  261. /*
  262. * PD Buddy configuration shell
  263. *
  264. * p: The configuration for the shell itself
  265. */
  266. void pdb_shell(void)
  267. {
  268. int n;
  269. BaseSequentialStream *chp = shell_cfg.io;
  270. const struct pdb_shell_cmd *scp = shell_cfg.commands;
  271. char *lp, *cmd, *tokp, line[PDB_SHELL_MAX_LINE_LENGTH];
  272. char *args[PDB_SHELL_MAX_ARGUMENTS + 1];
  273. while (true) {
  274. chprintf(chp, "PDBS) ");
  275. if (shellGetLine(chp, line, sizeof(line))) {
  276. chprintf(chp, "\r\n");
  277. continue;
  278. }
  279. lp = _strtok(line, " \t", &tokp);
  280. cmd = lp;
  281. n = 0;
  282. while ((lp = _strtok(NULL, " \t", &tokp)) != NULL) {
  283. if (n >= PDB_SHELL_MAX_ARGUMENTS) {
  284. chprintf(chp, "too many arguments\r\n");
  285. cmd = NULL;
  286. break;
  287. }
  288. args[n++] = lp;
  289. }
  290. args[n] = NULL;
  291. if (cmd != NULL) {
  292. if (strcmp(cmd, "help") == 0) {
  293. if (n > 0) {
  294. chprintf(chp, "Usage: help\r\n");
  295. continue;
  296. }
  297. chprintf(chp, "PD Buddy Sink configuration shell\r\n");
  298. chprintf(chp, "Commands:\r\n");
  299. chprintf(chp, "\thelp: Print this message\r\n");
  300. if (scp != NULL)
  301. list_commands(chp, scp);
  302. }
  303. else if ((scp == NULL) || cmdexec(scp, chp, cmd, n, args)) {
  304. chprintf(chp, "%s", cmd);
  305. chprintf(chp, " ?\r\n");
  306. }
  307. }
  308. }
  309. }
  310. /**
  311. * @brief Reads a whole line from the input channel.
  312. *
  313. * @param[in] chp pointer to a @p BaseSequentialStream object
  314. * @param[in] line pointer to the line buffer
  315. * @param[in] size buffer maximum length
  316. * @return The operation status.
  317. * @retval true the channel was reset or CTRL-D pressed.
  318. * @retval false operation successful.
  319. *
  320. * @api
  321. */
  322. bool shellGetLine(BaseSequentialStream *chp, char *line, unsigned size)
  323. {
  324. char *p = line;
  325. while (true) {
  326. char c;
  327. if (chSequentialStreamRead(chp, (uint8_t *)&c, 1) == 0)
  328. return true;
  329. if (c == 4) {
  330. chprintf(chp, "^D");
  331. return true;
  332. }
  333. if ((c == 8) || (c == 127)) {
  334. if (p != line) {
  335. chSequentialStreamPut(chp, 0x08);
  336. chSequentialStreamPut(chp, 0x20);
  337. chSequentialStreamPut(chp, 0x08);
  338. p--;
  339. }
  340. continue;
  341. }
  342. if (c == '\r') {
  343. chprintf(chp, "\r\n");
  344. *p = 0;
  345. return false;
  346. }
  347. if (c < 0x20)
  348. continue;
  349. if (p < line + size - 1) {
  350. chSequentialStreamPut(chp, c);
  351. *p++ = (char)c;
  352. }
  353. }
  354. }