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

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