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 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 "config.h"
  36. #include "led.h"
  37. #include "device_policy_manager.h"
  38. #include "policy_engine.h"
  39. #include "pd.h"
  40. /* Buffer for unwritten configuration */
  41. static struct pdb_config tmpcfg = {
  42. .status = PDB_CONFIG_STATUS_VALID
  43. };
  44. /*
  45. * Helper functions for printing PDOs
  46. */
  47. static void print_src_fixed_pdo(BaseSequentialStream *chp, uint32_t pdo)
  48. {
  49. int tmp;
  50. chprintf(chp, "fixed\r\n");
  51. /* Dual-role power */
  52. tmp = (pdo & PD_PDO_SRC_FIXED_DUAL_ROLE_PWR) >> PD_PDO_SRC_FIXED_DUAL_ROLE_PWR_SHIFT;
  53. if (tmp) {
  54. chprintf(chp, "\tdual_role_pwr: %d\r\n", tmp);
  55. }
  56. /* USB Suspend Supported */
  57. tmp = (pdo & PD_PDO_SRC_FIXED_USB_SUSPEND) >> PD_PDO_SRC_FIXED_USB_SUSPEND_SHIFT;
  58. if (tmp) {
  59. chprintf(chp, "\tusb_suspend: %d\r\n", tmp);
  60. }
  61. /* Unconstrained power */
  62. tmp = (pdo & PD_PDO_SRC_FIXED_UNCONSTRAINED) >> PD_PDO_SRC_FIXED_UNCONSTRAINED_SHIFT;
  63. if (tmp) {
  64. chprintf(chp, "\tunconstrained_pwr: %d\r\n", tmp);
  65. }
  66. /* USB communications capable */
  67. tmp = (pdo & PD_PDO_SRC_FIXED_USB_COMMS) >> PD_PDO_SRC_FIXED_USB_COMMS_SHIFT;
  68. if (tmp) {
  69. chprintf(chp, "\tusb_comms: %d\r\n", tmp);
  70. }
  71. /* Dual-role data */
  72. tmp = (pdo & PD_PDO_SRC_FIXED_DUAL_ROLE_DATA) >> PD_PDO_SRC_FIXED_DUAL_ROLE_DATA_SHIFT;
  73. if (tmp) {
  74. chprintf(chp, "\tdual_role_data: %d\r\n", tmp);
  75. }
  76. /* Peak current */
  77. tmp = (pdo & PD_PDO_SRC_FIXED_PEAK_CURRENT) >> PD_PDO_SRC_FIXED_PEAK_CURRENT_SHIFT;
  78. if (tmp) {
  79. chprintf(chp, "\tpeak_i: %d\r\n", tmp);
  80. }
  81. /* Voltage */
  82. tmp = (pdo & PD_PDO_SRC_FIXED_VOLTAGE) >> PD_PDO_SRC_FIXED_VOLTAGE_SHIFT;
  83. chprintf(chp, "\tv: %d.%02d V\r\n", PD_PDV_V(tmp), PD_PDV_CV(tmp));
  84. /* Maximum current */
  85. tmp = (pdo & PD_PDO_SRC_FIXED_CURRENT) >> PD_PDO_SRC_FIXED_CURRENT_SHIFT;
  86. chprintf(chp, "\ti: %d.%02d A\r\n", PD_PDI_A(tmp), PD_PDI_CA(tmp));
  87. }
  88. static void print_src_pdo(BaseSequentialStream *chp, uint32_t pdo, uint8_t index)
  89. {
  90. /* If we have a positive index, print a label for the PDO */
  91. if (index) {
  92. chprintf(chp, "PDO %d: ", index);
  93. }
  94. /* Select the appropriate method for printing the PDO itself */
  95. if ((pdo & PD_PDO_TYPE) == PD_PDO_TYPE_FIXED) {
  96. print_src_fixed_pdo(chp, pdo);
  97. } else {
  98. /* Unknown PDO, just print it as hex */
  99. chprintf(chp, "%08X\r\n", pdo);
  100. }
  101. }
  102. /*
  103. * Command functions
  104. */
  105. static void cmd_license(BaseSequentialStream *chp, int argc, char *argv[])
  106. {
  107. (void) argv;
  108. if (argc > 0) {
  109. chprintf(chp, "Usage: license\r\n");
  110. return;
  111. }
  112. chprintf(chp,
  113. "PD Buddy - USB Power Delivery for everyone\r\n"
  114. "Copyright (C) 2017 Clayton G. Hobbs <clay@lakeserv.net>\r\n"
  115. "\r\n"
  116. "This program is free software: you can redistribute it and/or modify\r\n"
  117. "it under the terms of the GNU General Public License as published by\r\n"
  118. "the Free Software Foundation, either version 3 of the License, or\r\n"
  119. "(at your option) any later version.\r\n"
  120. "\r\n"
  121. "This program is distributed in the hope that it will be useful,\r\n"
  122. "but WITHOUT ANY WARRANTY; without even the implied warranty of\r\n"
  123. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r\n"
  124. "GNU General Public License for more details.\r\n"
  125. "\r\n"
  126. "You should have received a copy of the GNU General Public License\r\n"
  127. "along with this program. If not, see <http://www.gnu.org/licenses/>.\r\n"
  128. );
  129. }
  130. static void cmd_erase(BaseSequentialStream *chp, int argc, char *argv[])
  131. {
  132. (void) argv;
  133. if (argc > 0) {
  134. chprintf(chp, "Usage: erase\r\n");
  135. return;
  136. }
  137. pdb_config_flash_erase();
  138. }
  139. static void cmd_write(BaseSequentialStream *chp, int argc, char *argv[])
  140. {
  141. (void) argv;
  142. if (argc > 0) {
  143. chprintf(chp, "Usage: write\r\n");
  144. return;
  145. }
  146. pdb_config_flash_update(&tmpcfg);
  147. chEvtSignal(pdb_pe_thread, PDB_EVT_PE_NEW_POWER);
  148. }
  149. static void cmd_load(BaseSequentialStream *chp, int argc, char *argv[])
  150. {
  151. (void) argv;
  152. if (argc > 0) {
  153. chprintf(chp, "Usage: load\r\n");
  154. return;
  155. }
  156. /* Get the current configuration */
  157. struct pdb_config *cfg = pdb_config_flash_read();
  158. if (cfg == NULL) {
  159. chprintf(chp, "No configuration\r\n");
  160. return;
  161. }
  162. /* Load the current configuration into tmpcfg */
  163. tmpcfg.status = cfg->status;
  164. tmpcfg.flags = cfg->flags;
  165. tmpcfg.v = cfg->v;
  166. tmpcfg.i = cfg->i;
  167. tmpcfg.v_min = cfg->v_min;
  168. tmpcfg.v_max = cfg->v_max;
  169. }
  170. static void cmd_get_cfg(BaseSequentialStream *chp, int argc, char *argv[])
  171. {
  172. struct pdb_config *cfg = NULL;
  173. if (argc > 1) {
  174. chprintf(chp, "Usage: get_cfg [index]\r\n");
  175. return;
  176. }
  177. /* With no arguments, find the current configuration */
  178. if (argc == 0) {
  179. cfg = pdb_config_flash_read();
  180. if (cfg == NULL) {
  181. chprintf(chp, "No configuration\r\n");
  182. return;
  183. }
  184. /* With an argument, get a particular configuration array index */
  185. } else if (argc == 1) {
  186. char *endptr;
  187. long i = strtol(argv[0], &endptr, 0);
  188. if (i >= 0 && i < PDB_CONFIG_ARRAY_LEN && endptr > argv[0]) {
  189. cfg = &pdb_config_array[i];
  190. } else {
  191. chprintf(chp, "Invalid index\r\n");
  192. return;
  193. }
  194. }
  195. /* Print the configuration */
  196. pdb_config_print(chp, cfg);
  197. }
  198. static void cmd_get_tmpcfg(BaseSequentialStream *chp, int argc, char *argv[])
  199. {
  200. (void) argv;
  201. if (argc > 0) {
  202. chprintf(chp, "Usage: get_tmpcfg\r\n");
  203. return;
  204. }
  205. pdb_config_print(chp, &tmpcfg);
  206. }
  207. static void cmd_clear_flags(BaseSequentialStream *chp, int argc, char *argv[])
  208. {
  209. (void) argv;
  210. if (argc > 0) {
  211. chprintf(chp, "Usage: clear_flags\r\n");
  212. return;
  213. }
  214. /* Clear all flags */
  215. tmpcfg.flags = 0;
  216. }
  217. static void cmd_toggle_giveback(BaseSequentialStream *chp, int argc, char *argv[])
  218. {
  219. (void) argv;
  220. if (argc > 0) {
  221. chprintf(chp, "Usage: toggle_giveback\r\n");
  222. return;
  223. }
  224. /* Toggle the GiveBack flag */
  225. tmpcfg.flags ^= PDB_CONFIG_FLAGS_GIVEBACK;
  226. }
  227. static void cmd_set_v(BaseSequentialStream *chp, int argc, char *argv[])
  228. {
  229. if (argc != 1) {
  230. chprintf(chp, "Usage: set_v voltage_in_mV\r\n");
  231. return;
  232. }
  233. char *endptr;
  234. long i = strtol(argv[0], &endptr, 0);
  235. if (i >= PD_MV_MIN && i <= PD_MV_MAX && endptr > argv[0]) {
  236. /* Convert mV to the unit used by USB PD */
  237. tmpcfg.v = PD_MV2PDV(i);
  238. } else {
  239. chprintf(chp, "Invalid voltage\r\n");
  240. return;
  241. }
  242. }
  243. static void cmd_set_i(BaseSequentialStream *chp, int argc, char *argv[])
  244. {
  245. if (argc != 1) {
  246. chprintf(chp, "Usage: set_i current_in_mA\r\n");
  247. return;
  248. }
  249. char *endptr;
  250. long i = strtol(argv[0], &endptr, 0);
  251. if (i >= PD_MA_MIN && i <= PD_MA_MAX && endptr > argv[0]) {
  252. /* Convert mA to the unit used by USB PD */
  253. tmpcfg.i = PD_MA2PDI(i);
  254. } else {
  255. chprintf(chp, "Invalid current\r\n");
  256. return;
  257. }
  258. }
  259. static void cmd_identify(BaseSequentialStream *chp, int argc, char *argv[])
  260. {
  261. (void) chp;
  262. (void) argv;
  263. if (argc > 0) {
  264. chprintf(chp, "Usage: identify\r\n");
  265. return;
  266. }
  267. chEvtSignal(pdb_led_thread, PDB_EVT_LED_IDENTIFY);
  268. }
  269. static void cmd_output(BaseSequentialStream *chp, int argc, char *argv[])
  270. {
  271. if (argc == 0) {
  272. /* With no arguments, print the output status */
  273. if (pdb_dpm_output_enabled) {
  274. chprintf(chp, "enabled\r\n");
  275. } else {
  276. chprintf(chp, "disabled\r\n");
  277. }
  278. } else if (argc == 1) {
  279. /* Set the output status and re-negotiate power */
  280. if (strcmp(argv[0], "enable") == 0) {
  281. pdb_dpm_output_enabled = true;
  282. chEvtSignal(pdb_pe_thread, PDB_EVT_PE_NEW_POWER);
  283. } else if (strcmp(argv[0], "disable") == 0) {
  284. pdb_dpm_output_enabled = false;
  285. chEvtSignal(pdb_pe_thread, PDB_EVT_PE_NEW_POWER);
  286. } else {
  287. /* Or, if the argument was invalid, print a usage message */
  288. chprintf(chp, "Usage: output [enable|disable]\r\n");
  289. }
  290. } else {
  291. /* If there are too many arguments, print a usage message */
  292. chprintf(chp, "Usage: output [enable|disable]\r\n");
  293. }
  294. }
  295. static void cmd_get_source_cap(BaseSequentialStream *chp, int argc, char *argv[])
  296. {
  297. (void) argv;
  298. if (argc > 0) {
  299. chprintf(chp, "Usage: get_source_cap\r\n");
  300. return;
  301. }
  302. /* If we haven't seen any Source_Capabilities */
  303. if (pdb_dpm_capabilities == NULL) {
  304. /* Have we started reading Type-C Current advertisements? */
  305. if (pdb_dpm_typec_current != None) {
  306. /* Type-C Current is available, so report it */
  307. chprintf(chp, "PDO 1: typec_virtual\r\n");
  308. if (pdb_dpm_typec_current == Default) {
  309. chprintf(chp, "\ti: 0.50 A\r\n");
  310. } else if (pdb_dpm_typec_current == OnePointFiveAmps) {
  311. chprintf(chp, "\ti: 1.50 A\r\n");
  312. } else if (pdb_dpm_typec_current == ThreePointZeroAmps) {
  313. chprintf(chp, "\ti: 3.00 A\r\n");
  314. }
  315. return;
  316. } else {
  317. /* No Type-C Current, so report no capabilities */
  318. chprintf(chp, "No Source_Capabilities\r\n");
  319. return;
  320. }
  321. }
  322. /* Print all the PDOs */
  323. uint8_t numobj = PD_NUMOBJ_GET(pdb_dpm_capabilities);
  324. for (uint8_t i = 0; i < numobj; i++) {
  325. print_src_pdo(chp, pdb_dpm_capabilities->obj[i], i+1);
  326. }
  327. }
  328. /*
  329. * List of shell commands
  330. */
  331. static const struct pdb_shell_cmd commands[] = {
  332. {"license", cmd_license, "Show copyright and license information"},
  333. {"erase", cmd_erase, "Erase all stored configuration"},
  334. {"write", cmd_write, "Store the configuration buffer"},
  335. {"load", cmd_load, "Load the stored configuration into the buffer"},
  336. {"get_cfg", cmd_get_cfg, "Print the stored configuration"},
  337. {"get_tmpcfg", cmd_get_tmpcfg, "Print the configuration buffer"},
  338. {"clear_flags", cmd_clear_flags, "Clear all flags"},
  339. {"toggle_giveback", cmd_toggle_giveback, "Toggle the GiveBack flag"},
  340. /* TODO {"toggle_var_bat", cmd_toggle_var_bat, "Toggle the Var/Bat flag"},*/
  341. {"set_v", cmd_set_v, "Set the voltage in millivolts"},
  342. {"set_i", cmd_set_i, "Set the current in milliamps"},
  343. /* TODO {"set_v_range", cmd_set_v_range, "Set the minimum and maximum voltage in millivolts"},*/
  344. {"identify", cmd_identify, "Blink the LED to identify the device"},
  345. {"output", cmd_output, "Get or set the output status"},
  346. {"get_source_cap", cmd_get_source_cap, "Print the capabilities of the PD source"},
  347. {NULL, NULL, NULL}
  348. };
  349. /*
  350. * The shell's configuration
  351. */
  352. const struct pdb_shell_cfg shell_cfg = {
  353. (BaseSequentialStream *)&SDU1,
  354. commands
  355. };
  356. /*
  357. * Utility functions for the shell
  358. */
  359. static char *_strtok(char *str, const char *delim, char **saveptr)
  360. {
  361. char *token;
  362. if (str)
  363. *saveptr = str;
  364. token = *saveptr;
  365. if (!token)
  366. return NULL;
  367. token += strspn(token, delim);
  368. *saveptr = strpbrk(token, delim);
  369. if (*saveptr)
  370. *(*saveptr)++ = '\0';
  371. return *token ? token : NULL;
  372. }
  373. static void list_commands(BaseSequentialStream *chp, const struct pdb_shell_cmd *scp)
  374. {
  375. while (scp->cmd != NULL) {
  376. chprintf(chp, "\t%s: %s\r\n", scp->cmd, scp->desc);
  377. scp++;
  378. }
  379. }
  380. static bool cmdexec(const struct pdb_shell_cmd *scp, BaseSequentialStream *chp,
  381. char *name, int argc, char *argv[])
  382. {
  383. while (scp->cmd != NULL) {
  384. if (strcmp(scp->cmd, name) == 0) {
  385. scp->func(chp, argc, argv);
  386. return false;
  387. }
  388. scp++;
  389. }
  390. return true;
  391. }
  392. /*
  393. * PD Buddy Sink configuration shell
  394. */
  395. void pdb_shell(void)
  396. {
  397. int n;
  398. BaseSequentialStream *chp = shell_cfg.io;
  399. const struct pdb_shell_cmd *scp = shell_cfg.commands;
  400. char *lp, *cmd, *tokp, line[PDB_SHELL_MAX_LINE_LENGTH];
  401. char *args[PDB_SHELL_MAX_ARGUMENTS + 1];
  402. while (true) {
  403. /* Print the prompt */
  404. chprintf(chp, "PDBS) ");
  405. /* Read a line of input */
  406. if (shellGetLine(chp, line, sizeof(line))) {
  407. /* If a command was not entered, prompt again */
  408. chprintf(chp, "\r\n");
  409. continue;
  410. }
  411. /* Tokenize the line */
  412. lp = _strtok(line, " \t", &tokp);
  413. cmd = lp;
  414. n = 0;
  415. while ((lp = _strtok(NULL, " \t", &tokp)) != NULL) {
  416. /* If we have too many tokens, abort */
  417. if (n >= PDB_SHELL_MAX_ARGUMENTS) {
  418. chprintf(chp, "too many arguments\r\n");
  419. cmd = NULL;
  420. break;
  421. }
  422. args[n++] = lp;
  423. }
  424. args[n] = NULL;
  425. /* If there's a command to run, run it */
  426. if (cmd != NULL) {
  427. /* Handle "help" in a special way */
  428. if (strcmp(cmd, "help") == 0) {
  429. if (n > 0) {
  430. chprintf(chp, "Usage: help\r\n");
  431. continue;
  432. }
  433. chprintf(chp, "PD Buddy Sink configuration shell\r\n");
  434. chprintf(chp, "Commands:\r\n");
  435. chprintf(chp, "\thelp: Print this message\r\n");
  436. if (scp != NULL)
  437. list_commands(chp, scp);
  438. }
  439. /* Run a command, giving a generic error message if there is no
  440. * such command */
  441. else if ((scp == NULL) || cmdexec(scp, chp, cmd, n, args)) {
  442. chprintf(chp, "%s", cmd);
  443. chprintf(chp, " ?\r\n");
  444. }
  445. }
  446. }
  447. }
  448. /**
  449. * @brief Reads a whole line from the input channel.
  450. *
  451. * @param[in] chp pointer to a @p BaseSequentialStream object
  452. * @param[in] line pointer to the line buffer
  453. * @param[in] size buffer maximum length
  454. * @return The operation status.
  455. * @retval true the channel was reset or CTRL-D pressed.
  456. * @retval false operation successful.
  457. *
  458. * @api
  459. */
  460. bool shellGetLine(BaseSequentialStream *chp, char *line, unsigned size)
  461. {
  462. char *p = line;
  463. while (true) {
  464. char c;
  465. /* Read a character */
  466. /* The cast to BaseAsynchronousChannel * is safe because we know that
  467. * chp is always really of that type. */
  468. while (chnReadTimeout((BaseAsynchronousChannel *) chp, (uint8_t *)&c, 1, TIME_IMMEDIATE) == 0)
  469. chThdSleepMilliseconds(2);
  470. /* Abort if ^D is received */
  471. if (c == '\x04') {
  472. chprintf(chp, "^D");
  473. return true;
  474. }
  475. /* Delete a character if ASCII backspace or delete is received */
  476. if ((c == '\b') || (c == '\x7F')) {
  477. if (p != line) {
  478. chSequentialStreamPut(chp, 0x08);
  479. chSequentialStreamPut(chp, 0x20);
  480. chSequentialStreamPut(chp, 0x08);
  481. p--;
  482. }
  483. continue;
  484. }
  485. /* Finish reading input if Enter is pressed */
  486. if (c == '\r') {
  487. chprintf(chp, "\r\n");
  488. *p = 0;
  489. return false;
  490. }
  491. /* Ignore other non-printing characters */
  492. if (c < ' ')
  493. continue;
  494. /* If there's room in the line buffer, append the new character */
  495. if (p < line + size - 1) {
  496. chSequentialStreamPut(chp, c);
  497. *p++ = (char)c;
  498. }
  499. }
  500. }