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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /*
  2. * PD Buddy Sink Firmware - Smart power jack for USB Power Delivery
  3. * Copyright (C) 2017-2018 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 <pdb.h>
  35. #include <pd.h>
  36. #include "usbcfg.h"
  37. #include "config.h"
  38. #include "led.h"
  39. #include "device_policy_manager.h"
  40. #include "stm32f072_bootloader.h"
  41. /* Buffer for unwritten configuration */
  42. static struct pdbs_config tmpcfg = {
  43. .status = PDBS_CONFIG_STATUS_VALID
  44. };
  45. /* Pointer to the PD Buddy firmware library configuration */
  46. static struct pdb_config *pdb_config;
  47. static struct pdbs_dpm_data *pdbs_dpm_data;
  48. /*
  49. * Helper functions for printing PDOs
  50. */
  51. static void print_src_fixed_pdo(BaseSequentialStream *chp, uint32_t pdo)
  52. {
  53. int tmp;
  54. chprintf(chp, "fixed\r\n");
  55. /* Dual-role power */
  56. tmp = (pdo & PD_PDO_SRC_FIXED_DUAL_ROLE_PWR) >> PD_PDO_SRC_FIXED_DUAL_ROLE_PWR_SHIFT;
  57. if (tmp) {
  58. chprintf(chp, "\tdual_role_pwr: %d\r\n", tmp);
  59. }
  60. /* USB Suspend Supported */
  61. tmp = (pdo & PD_PDO_SRC_FIXED_USB_SUSPEND) >> PD_PDO_SRC_FIXED_USB_SUSPEND_SHIFT;
  62. if (tmp) {
  63. chprintf(chp, "\tusb_suspend: %d\r\n", tmp);
  64. }
  65. /* Unconstrained power */
  66. tmp = (pdo & PD_PDO_SRC_FIXED_UNCONSTRAINED) >> PD_PDO_SRC_FIXED_UNCONSTRAINED_SHIFT;
  67. if (tmp) {
  68. chprintf(chp, "\tunconstrained_pwr: %d\r\n", tmp);
  69. }
  70. /* USB communications capable */
  71. tmp = (pdo & PD_PDO_SRC_FIXED_USB_COMMS) >> PD_PDO_SRC_FIXED_USB_COMMS_SHIFT;
  72. if (tmp) {
  73. chprintf(chp, "\tusb_comms: %d\r\n", tmp);
  74. }
  75. /* Dual-role data */
  76. tmp = (pdo & PD_PDO_SRC_FIXED_DUAL_ROLE_DATA) >> PD_PDO_SRC_FIXED_DUAL_ROLE_DATA_SHIFT;
  77. if (tmp) {
  78. chprintf(chp, "\tdual_role_data: %d\r\n", tmp);
  79. }
  80. /* Peak current */
  81. tmp = (pdo & PD_PDO_SRC_FIXED_PEAK_CURRENT) >> PD_PDO_SRC_FIXED_PEAK_CURRENT_SHIFT;
  82. if (tmp) {
  83. chprintf(chp, "\tpeak_i: %d\r\n", tmp);
  84. }
  85. /* Voltage */
  86. tmp = (pdo & PD_PDO_SRC_FIXED_VOLTAGE) >> PD_PDO_SRC_FIXED_VOLTAGE_SHIFT;
  87. chprintf(chp, "\tv: %d.%02d V\r\n", PD_PDV_V(tmp), PD_PDV_CV(tmp));
  88. /* Maximum current */
  89. tmp = (pdo & PD_PDO_SRC_FIXED_CURRENT) >> PD_PDO_SRC_FIXED_CURRENT_SHIFT;
  90. chprintf(chp, "\ti: %d.%02d A\r\n", PD_PDI_A(tmp), PD_PDI_CA(tmp));
  91. }
  92. static void print_src_pps_apdo(BaseSequentialStream *chp, uint32_t pdo)
  93. {
  94. int tmp;
  95. chprintf(chp, "pps\r\n");
  96. /* Minimum voltage */
  97. tmp = (pdo & PD_APDO_PPS_MIN_VOLTAGE) >> PD_APDO_PPS_MIN_VOLTAGE_SHIFT;
  98. chprintf(chp, "\tvmin: %d.%02d V\r\n", PD_PAV_V(tmp), PD_PAV_CV(tmp));
  99. /* Maximum voltage */
  100. tmp = (pdo & PD_APDO_PPS_MAX_VOLTAGE) >> PD_APDO_PPS_MAX_VOLTAGE_SHIFT;
  101. chprintf(chp, "\tvmax: %d.%02d V\r\n", PD_PAV_V(tmp), PD_PAV_CV(tmp));
  102. /* Maximum current */
  103. tmp = (pdo & PD_APDO_PPS_CURRENT) >> PD_APDO_PPS_CURRENT_SHIFT;
  104. chprintf(chp, "\ti: %d.%02d A\r\n", PD_PAI_A(tmp), PD_PAI_CA(tmp));
  105. }
  106. static void print_src_pdo(BaseSequentialStream *chp, uint32_t pdo, uint8_t index)
  107. {
  108. /* If we have a positive index, print a label for the PDO */
  109. if (index) {
  110. chprintf(chp, "PDO %d: ", index);
  111. }
  112. /* Select the appropriate method for printing the PDO itself */
  113. if ((pdo & PD_PDO_TYPE) == PD_PDO_TYPE_FIXED) {
  114. print_src_fixed_pdo(chp, pdo);
  115. } else if ((pdo & PD_PDO_TYPE) == PD_PDO_TYPE_AUGMENTED
  116. && (pdo & PD_APDO_TYPE) == PD_APDO_TYPE_PPS) {
  117. print_src_pps_apdo(chp, pdo);
  118. } else {
  119. /* Unknown PDO, just print it as hex */
  120. chprintf(chp, "%08X\r\n", pdo);
  121. }
  122. }
  123. /*
  124. * Command functions
  125. */
  126. static void cmd_license(BaseSequentialStream *chp, int argc, char *argv[])
  127. {
  128. (void) argv;
  129. if (argc > 0) {
  130. chprintf(chp, "Usage: license\r\n");
  131. return;
  132. }
  133. chprintf(chp,
  134. "PD Buddy Sink Firmware - Smart power jack for USB Power Delivery\r\n"
  135. "Copyright (C) 2017-2018 Clayton G. Hobbs <clay@lakeserv.net>\r\n"
  136. "\r\n"
  137. "This program is free software: you can redistribute it and/or modify\r\n"
  138. "it under the terms of the GNU General Public License as published by\r\n"
  139. "the Free Software Foundation, either version 3 of the License, or\r\n"
  140. "(at your option) any later version.\r\n"
  141. "\r\n"
  142. "This program is distributed in the hope that it will be useful,\r\n"
  143. "but WITHOUT ANY WARRANTY; without even the implied warranty of\r\n"
  144. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r\n"
  145. "GNU General Public License for more details.\r\n"
  146. "\r\n"
  147. "You should have received a copy of the GNU General Public License\r\n"
  148. "along with this program. If not, see <http://www.gnu.org/licenses/>.\r\n"
  149. );
  150. }
  151. static void cmd_identify(BaseSequentialStream *chp, int argc, char *argv[])
  152. {
  153. (void) chp;
  154. (void) argv;
  155. if (argc > 0) {
  156. chprintf(chp, "Usage: identify\r\n");
  157. return;
  158. }
  159. chEvtSignal(pdbs_led_thread, PDBS_EVT_LED_IDENTIFY);
  160. }
  161. static void cmd_boot(BaseSequentialStream *chp, int argc, char *argv[])
  162. {
  163. (void) argv;
  164. if (argc > 0) {
  165. chprintf(chp, "Usage: boot\r\n");
  166. return;
  167. }
  168. sduStop(&SDU1);
  169. usbDisconnectBus(serusbcfg.usbp);
  170. dfu_run_bootloader();
  171. }
  172. static void cmd_get_cfg(BaseSequentialStream *chp, int argc, char *argv[])
  173. {
  174. struct pdbs_config *cfg = NULL;
  175. if (argc > 1) {
  176. chprintf(chp, "Usage: get_cfg [index]\r\n");
  177. return;
  178. }
  179. /* With no arguments, find the current configuration */
  180. if (argc == 0) {
  181. cfg = pdbs_config_flash_read();
  182. if (cfg == NULL) {
  183. chprintf(chp, "No configuration\r\n");
  184. return;
  185. }
  186. /* With an argument, get a particular configuration array index */
  187. } else if (argc == 1) {
  188. char *endptr;
  189. long i = strtol(argv[0], &endptr, 0);
  190. if (i >= 0 && i < PDBS_CONFIG_ARRAY_LEN && endptr > argv[0]) {
  191. cfg = &pdbs_config_array[i];
  192. } else {
  193. chprintf(chp, "Invalid index\r\n");
  194. return;
  195. }
  196. }
  197. /* Print the configuration */
  198. pdbs_config_print(chp, cfg);
  199. }
  200. static void cmd_load(BaseSequentialStream *chp, int argc, char *argv[])
  201. {
  202. (void) argv;
  203. if (argc > 0) {
  204. chprintf(chp, "Usage: load\r\n");
  205. return;
  206. }
  207. /* Get the current configuration */
  208. struct pdbs_config *cfg = pdbs_config_flash_read();
  209. if (cfg == NULL) {
  210. chprintf(chp, "No configuration\r\n");
  211. return;
  212. }
  213. /* Load the current configuration into tmpcfg */
  214. tmpcfg.status = cfg->status;
  215. tmpcfg.flags = cfg->flags;
  216. tmpcfg.v = cfg->v;
  217. tmpcfg.i = cfg->i;
  218. tmpcfg.vmin = cfg->vmin;
  219. tmpcfg.vmax = cfg->vmax;
  220. }
  221. static void cmd_write(BaseSequentialStream *chp, int argc, char *argv[])
  222. {
  223. (void) argv;
  224. if (argc > 0) {
  225. chprintf(chp, "Usage: write\r\n");
  226. return;
  227. }
  228. pdbs_config_flash_update(&tmpcfg);
  229. chEvtSignal(pdb_config->pe.thread, PDB_EVT_PE_NEW_POWER);
  230. }
  231. static void cmd_erase(BaseSequentialStream *chp, int argc, char *argv[])
  232. {
  233. (void) argv;
  234. if (argc > 0) {
  235. chprintf(chp, "Usage: erase\r\n");
  236. return;
  237. }
  238. pdbs_config_flash_erase();
  239. }
  240. static void cmd_get_tmpcfg(BaseSequentialStream *chp, int argc, char *argv[])
  241. {
  242. (void) argv;
  243. if (argc > 0) {
  244. chprintf(chp, "Usage: get_tmpcfg\r\n");
  245. return;
  246. }
  247. pdbs_config_print(chp, &tmpcfg);
  248. }
  249. static void cmd_clear_flags(BaseSequentialStream *chp, int argc, char *argv[])
  250. {
  251. (void) argv;
  252. if (argc > 0) {
  253. chprintf(chp, "Usage: clear_flags\r\n");
  254. return;
  255. }
  256. /* Clear all flags that can be toggled with toggle_* commands */
  257. tmpcfg.flags &= ~(PDBS_CONFIG_FLAGS_GIVEBACK
  258. | PDBS_CONFIG_FLAGS_VAR_BAT
  259. | PDBS_CONFIG_FLAGS_HV_PREFERRED);
  260. }
  261. static void cmd_toggle_giveback(BaseSequentialStream *chp, int argc, char *argv[])
  262. {
  263. (void) argv;
  264. if (argc > 0) {
  265. chprintf(chp, "Usage: toggle_giveback\r\n");
  266. return;
  267. }
  268. /* Toggle the GiveBack flag */
  269. tmpcfg.flags ^= PDBS_CONFIG_FLAGS_GIVEBACK;
  270. }
  271. static void cmd_toggle_hv_preferred(BaseSequentialStream *chp, int argc, char *argv[])
  272. {
  273. (void) argv;
  274. if (argc > 0) {
  275. chprintf(chp, "Usage: toggle_hv_preferred\r\n");
  276. return;
  277. }
  278. /* Toggle the HV_Preferred flag */
  279. tmpcfg.flags ^= PDBS_CONFIG_FLAGS_HV_PREFERRED;
  280. }
  281. static void cmd_set_v(BaseSequentialStream *chp, int argc, char *argv[])
  282. {
  283. if (argc != 1) {
  284. chprintf(chp, "Usage: set_v voltage_in_mV\r\n");
  285. return;
  286. }
  287. char *endptr;
  288. long i = strtol(argv[0], &endptr, 0);
  289. if (i >= PD_MV_MIN && i <= PD_MV_MAX && endptr > argv[0]) {
  290. tmpcfg.v = i;
  291. } else {
  292. chprintf(chp, "Invalid voltage\r\n");
  293. return;
  294. }
  295. }
  296. static void cmd_set_vrange(BaseSequentialStream *chp, int argc, char *argv[])
  297. {
  298. if (argc != 2) {
  299. chprintf(chp, "Usage: set_vrange min_voltage_in_mV max_voltage_in_mV\r\n");
  300. return;
  301. }
  302. char *endptr;
  303. long min = strtol(argv[0], &endptr, 0);
  304. char *endptr2;
  305. long max = strtol(argv[1], &endptr2, 0);
  306. if (min < PD_MV_MIN || min > PD_MV_MAX || endptr <= argv[0]
  307. || max < PD_MV_MIN || max > PD_MV_MAX || endptr2 <= argv[1]) {
  308. chprintf(chp, "Invalid voltage\r\n");
  309. return;
  310. }
  311. if (min > max) {
  312. chprintf(chp, "Invalid range\r\n");
  313. return;
  314. }
  315. tmpcfg.vmin = min;
  316. tmpcfg.vmax = max;
  317. }
  318. static void cmd_set_i(BaseSequentialStream *chp, int argc, char *argv[])
  319. {
  320. if (argc != 1) {
  321. chprintf(chp, "Usage: set_i current_in_mA\r\n");
  322. return;
  323. }
  324. char *endptr;
  325. long i = strtol(argv[0], &endptr, 0);
  326. if (i >= PD_MA_MIN && i <= PD_MA_MAX && endptr > argv[0]) {
  327. /* Convert mA to the unit used by USB PD */
  328. tmpcfg.i = PD_MA2PDI(i);
  329. /* Set the flags to say we're storing a current */
  330. tmpcfg.flags &= ~PDBS_CONFIG_FLAGS_CURRENT_DEFN;
  331. tmpcfg.flags |= PDBS_CONFIG_FLAGS_CURRENT_DEFN_I;
  332. } else {
  333. chprintf(chp, "Invalid current\r\n");
  334. return;
  335. }
  336. }
  337. static void cmd_set_p(BaseSequentialStream *chp, int argc, char *argv[])
  338. {
  339. if (argc != 1) {
  340. chprintf(chp, "Usage: set_p power_in_mW\r\n");
  341. return;
  342. }
  343. char *endptr;
  344. long i = strtol(argv[0], &endptr, 0);
  345. if (i >= PD_MW_MIN && i <= PD_MW_MAX && endptr > argv[0]) {
  346. /* Convert mW to the unit used in the configuration object */
  347. tmpcfg.p = PD_MW2CW(i);
  348. /* Set the flags to say we're storing a power */
  349. tmpcfg.flags &= ~PDBS_CONFIG_FLAGS_CURRENT_DEFN;
  350. tmpcfg.flags |= PDBS_CONFIG_FLAGS_CURRENT_DEFN_P;
  351. } else {
  352. chprintf(chp, "Invalid power\r\n");
  353. return;
  354. }
  355. }
  356. static void cmd_set_r(BaseSequentialStream *chp, int argc, char *argv[])
  357. {
  358. if (argc != 1) {
  359. chprintf(chp, "Usage: set_r power_in_m\316\251\r\n");
  360. return;
  361. }
  362. char *endptr;
  363. long i = strtol(argv[0], &endptr, 0);
  364. if (i >= PD_MO_MIN && i <= PD_MO_MAX && endptr > argv[0]) {
  365. /* Convert mohms to the unit used in the configuration object */
  366. tmpcfg.r = PD_MO2CO(i);
  367. /* Set the flags to say we're storing a resistance */
  368. tmpcfg.flags &= ~PDBS_CONFIG_FLAGS_CURRENT_DEFN;
  369. tmpcfg.flags |= PDBS_CONFIG_FLAGS_CURRENT_DEFN_R;
  370. } else {
  371. chprintf(chp, "Invalid resistance\r\n");
  372. return;
  373. }
  374. }
  375. static void cmd_output(BaseSequentialStream *chp, int argc, char *argv[])
  376. {
  377. if (argc == 0) {
  378. /* With no arguments, print the output status */
  379. if (pdbs_dpm_data->output_enabled) {
  380. chprintf(chp, "enabled\r\n");
  381. } else {
  382. chprintf(chp, "disabled\r\n");
  383. }
  384. } else if (argc == 1) {
  385. /* Set the output status and re-negotiate power */
  386. if (strcmp(argv[0], "enable") == 0) {
  387. pdbs_dpm_data->output_enabled = true;
  388. chEvtSignal(pdb_config->pe.thread, PDB_EVT_PE_NEW_POWER);
  389. } else if (strcmp(argv[0], "disable") == 0) {
  390. pdbs_dpm_data->output_enabled = false;
  391. chEvtSignal(pdb_config->pe.thread, PDB_EVT_PE_NEW_POWER);
  392. } else {
  393. /* Or, if the argument was invalid, print a usage message */
  394. chprintf(chp, "Usage: output [enable|disable]\r\n");
  395. }
  396. } else {
  397. /* If there are too many arguments, print a usage message */
  398. chprintf(chp, "Usage: output [enable|disable]\r\n");
  399. }
  400. }
  401. static void cmd_get_source_cap(BaseSequentialStream *chp, int argc, char *argv[])
  402. {
  403. (void) argv;
  404. if (argc > 0) {
  405. chprintf(chp, "Usage: get_source_cap\r\n");
  406. return;
  407. }
  408. /* If we haven't seen any Source_Capabilities */
  409. if (pdbs_dpm_data->capabilities == NULL) {
  410. /* Have we started reading Type-C Current advertisements? */
  411. if (pdbs_dpm_data->typec_current != fusb_tcc_none) {
  412. /* Type-C Current is available, so report it */
  413. chprintf(chp, "PDO 1: typec_virtual\r\n");
  414. if (pdbs_dpm_data->typec_current == fusb_tcc_default) {
  415. chprintf(chp, "\ti: 0.50 A\r\n");
  416. } else if (pdbs_dpm_data->typec_current == fusb_tcc_1_5) {
  417. chprintf(chp, "\ti: 1.50 A\r\n");
  418. } else if (pdbs_dpm_data->typec_current == fusb_tcc_3_0) {
  419. chprintf(chp, "\ti: 3.00 A\r\n");
  420. }
  421. return;
  422. } else {
  423. /* No Type-C Current, so report no capabilities */
  424. chprintf(chp, "No Source_Capabilities\r\n");
  425. return;
  426. }
  427. }
  428. /* Print all the PDOs */
  429. uint8_t numobj = PD_NUMOBJ_GET(pdbs_dpm_data->capabilities);
  430. for (uint8_t i = 0; i < numobj; i++) {
  431. print_src_pdo(chp, pdbs_dpm_data->capabilities->obj[i], i+1);
  432. }
  433. }
  434. /*
  435. * List of shell commands
  436. */
  437. static const struct pdbs_shell_cmd commands[] = {
  438. {"license", cmd_license, "Show copyright and license information"},
  439. {"identify", cmd_identify, "Blink the LED to identify the device"},
  440. {"boot", cmd_boot, "Run the DfuSe bootloader"},
  441. {"get_cfg", cmd_get_cfg, "Print the stored configuration"},
  442. {"load", cmd_load, "Load the stored configuration into the buffer"},
  443. {"write", cmd_write, "Store the configuration buffer"},
  444. {"erase", cmd_erase, "Erase all stored configuration"},
  445. {"get_tmpcfg", cmd_get_tmpcfg, "Print the configuration buffer"},
  446. {"clear_flags", cmd_clear_flags, "Clear all flags"},
  447. {"toggle_giveback", cmd_toggle_giveback, "Toggle the GiveBack flag"},
  448. {"toggle_hv_preferred", cmd_toggle_hv_preferred, "Toggle the HV_Preferred flag"},
  449. /* TODO {"toggle_var_bat", cmd_toggle_var_bat, "Toggle the Var/Bat flag"},*/
  450. {"set_v", cmd_set_v, "Set the voltage in millivolts"},
  451. {"set_vrange", cmd_set_vrange, "Set the minimum and maximum voltage in millivolts"},
  452. {"set_i", cmd_set_i, "Set the current in milliamps"},
  453. {"set_p", cmd_set_p, "Set the power in milliwatts"},
  454. {"set_r", cmd_set_r, "Set the resistance in milliohms"},
  455. {"output", cmd_output, "Get or set the output status"},
  456. {"get_source_cap", cmd_get_source_cap, "Print the capabilities of the PD source"},
  457. {NULL, NULL, NULL}
  458. };
  459. /*
  460. * The shell's configuration
  461. */
  462. const struct pdbs_shell_cfg shell_cfg = {
  463. (BaseSequentialStream *)&SDU1,
  464. commands
  465. };
  466. /*
  467. * Utility functions for the shell
  468. */
  469. static char *_strtok(char *str, const char *delim, char **saveptr)
  470. {
  471. char *token;
  472. if (str)
  473. *saveptr = str;
  474. token = *saveptr;
  475. if (!token)
  476. return NULL;
  477. token += strspn(token, delim);
  478. *saveptr = strpbrk(token, delim);
  479. if (*saveptr)
  480. *(*saveptr)++ = '\0';
  481. return *token ? token : NULL;
  482. }
  483. static void list_commands(BaseSequentialStream *chp, const struct pdbs_shell_cmd *scp)
  484. {
  485. while (scp->cmd != NULL) {
  486. chprintf(chp, "\t%s: %s\r\n", scp->cmd, scp->desc);
  487. scp++;
  488. }
  489. }
  490. static bool cmdexec(const struct pdbs_shell_cmd *scp, BaseSequentialStream *chp,
  491. char *name, int argc, char *argv[])
  492. {
  493. while (scp->cmd != NULL) {
  494. if (strcmp(scp->cmd, name) == 0) {
  495. scp->func(chp, argc, argv);
  496. return false;
  497. }
  498. scp++;
  499. }
  500. return true;
  501. }
  502. /*
  503. * PD Buddy Sink configuration shell
  504. */
  505. void pdbs_shell(struct pdb_config *cfg)
  506. {
  507. int n;
  508. BaseSequentialStream *chp = shell_cfg.io;
  509. const struct pdbs_shell_cmd *scp = shell_cfg.commands;
  510. char *lp, *cmd, *tokp, line[PDBS_SHELL_MAX_LINE_LENGTH];
  511. char *args[PDBS_SHELL_MAX_ARGUMENTS + 1];
  512. pdb_config = cfg;
  513. pdbs_dpm_data = cfg->dpm_data;
  514. while (true) {
  515. /* Print the prompt */
  516. chprintf(chp, "PDBS) ");
  517. /* Read a line of input */
  518. if (shellGetLine(chp, line, sizeof(line))) {
  519. /* If a command was not entered, prompt again */
  520. chprintf(chp, "\r\n");
  521. continue;
  522. }
  523. /* Tokenize the line */
  524. lp = _strtok(line, " \t", &tokp);
  525. cmd = lp;
  526. n = 0;
  527. while ((lp = _strtok(NULL, " \t", &tokp)) != NULL) {
  528. /* If we have too many tokens, abort */
  529. if (n >= PDBS_SHELL_MAX_ARGUMENTS) {
  530. chprintf(chp, "too many arguments\r\n");
  531. cmd = NULL;
  532. break;
  533. }
  534. args[n++] = lp;
  535. }
  536. args[n] = NULL;
  537. /* If there's a command to run, run it */
  538. if (cmd != NULL) {
  539. /* Handle "help" in a special way */
  540. if (strcmp(cmd, "help") == 0) {
  541. if (n > 0) {
  542. chprintf(chp, "Usage: help\r\n");
  543. continue;
  544. }
  545. chprintf(chp, "PD Buddy Sink configuration shell\r\n");
  546. chprintf(chp, "Commands:\r\n");
  547. chprintf(chp, "\thelp: Print this message\r\n");
  548. if (scp != NULL)
  549. list_commands(chp, scp);
  550. }
  551. /* Run a command, giving a generic error message if there is no
  552. * such command */
  553. else if ((scp == NULL) || cmdexec(scp, chp, cmd, n, args)) {
  554. chprintf(chp, "%s", cmd);
  555. chprintf(chp, " ?\r\n");
  556. }
  557. }
  558. }
  559. }
  560. /**
  561. * @brief Reads a whole line from the input channel.
  562. *
  563. * @param[in] chp pointer to a @p BaseSequentialStream object
  564. * @param[in] line pointer to the line buffer
  565. * @param[in] size buffer maximum length
  566. * @return The operation status.
  567. * @retval true the channel was reset or CTRL-D pressed.
  568. * @retval false operation successful.
  569. *
  570. * @api
  571. */
  572. bool shellGetLine(BaseSequentialStream *chp, char *line, unsigned size)
  573. {
  574. char *p = line;
  575. while (true) {
  576. char c;
  577. /* Read a character */
  578. /* The cast to BaseAsynchronousChannel * is safe because we know that
  579. * chp is always really of that type. */
  580. while (chnReadTimeout((BaseAsynchronousChannel *) chp, (uint8_t *)&c, 1, TIME_IMMEDIATE) == 0)
  581. chThdSleepMilliseconds(2);
  582. /* Abort if ^D is received */
  583. if (c == '\x04') {
  584. chprintf(chp, "^D");
  585. return true;
  586. }
  587. /* Delete a character if ASCII backspace or delete is received */
  588. if ((c == '\b') || (c == '\x7F')) {
  589. if (p != line) {
  590. chSequentialStreamPut(chp, 0x08);
  591. chSequentialStreamPut(chp, 0x20);
  592. chSequentialStreamPut(chp, 0x08);
  593. p--;
  594. }
  595. continue;
  596. }
  597. /* Finish reading input if Enter is pressed */
  598. if (c == '\r') {
  599. chprintf(chp, "\r\n");
  600. *p = 0;
  601. return false;
  602. }
  603. /* Ignore other non-printing characters */
  604. if (c < ' ')
  605. continue;
  606. /* If there's room in the line buffer, append the new character */
  607. if (p < line + size - 1) {
  608. chSequentialStreamPut(chp, c);
  609. *p++ = (char)c;
  610. }
  611. }
  612. }