platform_console.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /* Console example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include "platform_console.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "esp_log.h"
  12. #include "esp_console.h"
  13. #include "esp_vfs_dev.h"
  14. #include "driver/uart.h"
  15. #include "linenoise/linenoise.h"
  16. #include "argtable3/argtable3.h"
  17. #include "nvs.h"
  18. #include "nvs_flash.h"
  19. #include "pthread.h"
  20. #include "platform_esp32.h"
  21. #include "cmd_decl.h"
  22. #include "trace.h"
  23. #include "platform_config.h"
  24. #include "telnet.h"
  25. #include "messaging.h"
  26. #include "config.h"
  27. static pthread_t thread_console;
  28. static void * console_thread();
  29. void console_start();
  30. static const char * TAG = "console";
  31. extern bool bypass_wifi_manager;
  32. extern void register_squeezelite();
  33. static EXT_RAM_ATTR QueueHandle_t uart_queue;
  34. static EXT_RAM_ATTR struct {
  35. uint8_t _buf[128];
  36. StaticRingbuffer_t _ringbuf;
  37. RingbufHandle_t handle;
  38. QueueSetHandle_t queue_set;
  39. } stdin_redir;
  40. /* Prompt to be printed before each line.
  41. * This can be customized, made dynamic, etc.
  42. */
  43. const char* prompt = LOG_COLOR_I "squeezelite-esp32> " LOG_RESET_COLOR;
  44. const char* recovery_prompt = LOG_COLOR_E "recovery-squeezelite-esp32> " LOG_RESET_COLOR;
  45. /* Console command history can be stored to and loaded from a file.
  46. * The easiest way to do this is to use FATFS filesystem on top of
  47. * wear_levelling library.
  48. */
  49. #define MOUNT_PATH "/data"
  50. #define HISTORY_PATH MOUNT_PATH "/history.txt"
  51. static esp_err_t run_command(char * line);
  52. #define ADD_TO_JSON(o,t,n) if (t->n) cJSON_AddStringToObject(o,QUOTE(n),t->n);
  53. #define ADD_PARMS_TO_CMD(o,t,n) { cJSON * parms = ParmsToJSON(&t.n->hdr); if(parms) cJSON_AddItemToObject(o,QUOTE(n),parms); }
  54. cJSON * cmdList;
  55. cJSON * values_fn_list;
  56. cJSON * get_cmd_list(){
  57. cJSON * element;
  58. cJSON * values=cJSON_CreateObject();
  59. cJSON * list = cJSON_CreateObject();
  60. cJSON_AddItemReferenceToObject(list,"commands",cmdList);
  61. cJSON_AddItemToObject(list,"values",values);
  62. cJSON_ArrayForEach(element,cmdList){
  63. cJSON * name = cJSON_GetObjectItem(element,"name");
  64. cJSON * vals_fn = cJSON_GetObjectItem(values_fn_list,cJSON_GetStringValue(name));
  65. if(vals_fn!=NULL ){
  66. parm_values_fn_t *parm_values_fn = (parm_values_fn_t *)strtoul(cJSON_GetStringValue(vals_fn), NULL, 16);;
  67. if(parm_values_fn){
  68. cJSON_AddItemToObject(values,cJSON_GetStringValue(name),parm_values_fn());
  69. }
  70. }
  71. }
  72. return list;
  73. }
  74. struct arg_end *getParmsEnd(struct arg_hdr * * argtable){
  75. if(!argtable) return NULL;
  76. struct arg_hdr * *table = (struct arg_hdr * *)argtable;
  77. int tabindex = 0;
  78. while (!(table[tabindex]->flag & ARG_TERMINATOR))
  79. {
  80. tabindex++;
  81. }
  82. return (struct arg_end *)table[tabindex];
  83. }
  84. cJSON * ParmsToJSON(struct arg_hdr * * argtable){
  85. if(!argtable) return NULL;
  86. cJSON * arg_list = cJSON_CreateArray();
  87. struct arg_hdr * *table = (struct arg_hdr * *)argtable;
  88. int tabindex = 0;
  89. while (!(table[tabindex]->flag & ARG_TERMINATOR))
  90. {
  91. cJSON * entry = cJSON_CreateObject();
  92. ADD_TO_JSON(entry,table[tabindex],datatype);
  93. ADD_TO_JSON(entry,table[tabindex],glossary);
  94. ADD_TO_JSON(entry,table[tabindex],longopts);
  95. ADD_TO_JSON(entry,table[tabindex],shortopts);
  96. cJSON_AddBoolToObject(entry, "checkbox", (table[tabindex]->flag & ARG_HASOPTVALUE)==0 && (table[tabindex]->flag & ARG_HASVALUE)==0 && (table[tabindex]->longopts || table[tabindex]->shortopts) );
  97. cJSON_AddBoolToObject(entry, "remark", (table[tabindex]->flag & ARG_HASOPTVALUE)==0 && (table[tabindex]->flag & ARG_HASVALUE)==0 && (!table[tabindex]->longopts && !table[tabindex]->shortopts));
  98. cJSON_AddBoolToObject(entry, "hasvalue", table[tabindex]->flag & ARG_HASVALUE);
  99. cJSON_AddNumberToObject(entry,"mincount",table[tabindex]->mincount);
  100. cJSON_AddNumberToObject(entry,"maxcount",table[tabindex]->maxcount);
  101. cJSON_AddItemToArray(arg_list, entry);
  102. tabindex++;
  103. }
  104. return arg_list;
  105. }
  106. esp_err_t cmd_to_json(const esp_console_cmd_t *cmd){
  107. return cmd_to_json_with_cb(cmd, NULL);
  108. }
  109. esp_err_t cmd_to_json_with_cb(const esp_console_cmd_t *cmd, parm_values_fn_t parm_values_fn){
  110. if(!cmdList){
  111. cmdList=cJSON_CreateArray();
  112. }
  113. if(!values_fn_list){
  114. values_fn_list=cJSON_CreateObject();
  115. }
  116. if (cmd->command == NULL) {
  117. return ESP_ERR_INVALID_ARG;
  118. }
  119. if (strchr(cmd->command, ' ') != NULL) {
  120. return ESP_ERR_INVALID_ARG;
  121. }
  122. cJSON * jsoncmd = cJSON_CreateObject();
  123. ADD_TO_JSON(jsoncmd,cmd,help);
  124. ADD_TO_JSON(jsoncmd,cmd,hint);
  125. if(parm_values_fn){
  126. char addr[11]={0};
  127. snprintf(addr,sizeof(addr),"%lx",(unsigned long)parm_values_fn);
  128. cJSON_AddStringToObject(values_fn_list,cmd->command,addr);
  129. }
  130. cJSON_AddBoolToObject(jsoncmd,"hascb",parm_values_fn!=NULL);
  131. if(cmd->argtable){
  132. cJSON_AddItemToObject(jsoncmd,"argtable",ParmsToJSON(cmd->argtable));
  133. }
  134. if (cmd->hint) {
  135. cJSON_AddStringToObject(jsoncmd, "hint", cmd->hint);
  136. }
  137. else if (cmd->argtable) {
  138. /* Generate hint based on cmd->argtable */
  139. char *buf = NULL;
  140. size_t buf_size = 0;
  141. FILE *f = open_memstream(&buf, &buf_size);
  142. if (f != NULL) {
  143. arg_print_syntax(f, cmd->argtable, NULL);
  144. fflush(f);
  145. fclose(f);
  146. }
  147. cJSON_AddStringToObject(jsoncmd, "hint", buf);
  148. FREE_AND_NULL(buf);
  149. }
  150. cJSON_AddStringToObject(jsoncmd, "name", cmd->command);
  151. char * b=cJSON_Print(jsoncmd);
  152. if(b){
  153. ESP_LOGD(TAG,"Adding command table %s",b);
  154. free(b);
  155. }
  156. cJSON_AddItemToArray(cmdList, jsoncmd);
  157. return ESP_OK;
  158. }
  159. int arg_parse_msg(int argc, char **argv, struct arg_hdr ** args){
  160. int nerrors = arg_parse(argc, argv, (void **)args);
  161. if (nerrors != 0) {
  162. char *buf = NULL;
  163. size_t buf_size = 0;
  164. FILE *f = open_memstream(&buf, &buf_size);
  165. if (f != NULL) {
  166. arg_print_errors(f, getParmsEnd(args), argv[0]);
  167. fflush (f);
  168. cmd_send_messaging(argv[0],MESSAGING_ERROR,"%s", buf);
  169. }
  170. fclose(f);
  171. FREE_AND_NULL(buf);
  172. }
  173. return nerrors;
  174. }
  175. void process_autoexec(){
  176. int i=1;
  177. char autoexec_name[21]={0};
  178. char * autoexec_value=NULL;
  179. uint8_t autoexec_flag=0;
  180. char * str_flag = config_alloc_get(NVS_TYPE_STR, "autoexec");
  181. if(!bypass_wifi_manager){
  182. ESP_LOGW(TAG, "Processing autoexec commands while wifi_manager active. Wifi related commands will be ignored.");
  183. }
  184. if(is_recovery_running){
  185. ESP_LOGD(TAG, "Processing autoexec commands in recovery mode. Squeezelite commands will be ignored.");
  186. }
  187. if(str_flag !=NULL ){
  188. autoexec_flag=atoi(str_flag);
  189. ESP_LOGI(TAG,"autoexec is set to %s auto-process", autoexec_flag>0?"perform":"skip");
  190. if(autoexec_flag == 1) {
  191. do {
  192. snprintf(autoexec_name,sizeof(autoexec_name)-1,"autoexec%u",i++);
  193. ESP_LOGD(TAG,"Getting command name %s", autoexec_name);
  194. autoexec_value= config_alloc_get(NVS_TYPE_STR, autoexec_name);
  195. if(autoexec_value!=NULL ){
  196. if(!bypass_wifi_manager && strstr(autoexec_value, "join ")!=NULL ){
  197. ESP_LOGW(TAG,"Ignoring wifi join command.");
  198. }
  199. else if(is_recovery_running && !strstr(autoexec_value, "squeezelite " ) ){
  200. ESP_LOGW(TAG,"Ignoring command. ");
  201. }
  202. else {
  203. ESP_LOGI(TAG,"Running command %s = %s", autoexec_name, autoexec_value);
  204. run_command(autoexec_value);
  205. }
  206. ESP_LOGD(TAG,"Freeing memory for command %s name", autoexec_name);
  207. free(autoexec_value);
  208. }
  209. else {
  210. ESP_LOGD(TAG,"No matching command found for name %s", autoexec_name);
  211. break;
  212. }
  213. } while(1);
  214. }
  215. free(str_flag);
  216. }
  217. else
  218. {
  219. ESP_LOGD(TAG,"No matching command found for name autoexec.");
  220. }
  221. }
  222. static ssize_t stdin_read(int fd, void* data, size_t size) {
  223. size_t bytes = -1;
  224. while (1) {
  225. QueueSetMemberHandle_t activated = xQueueSelectFromSet(stdin_redir.queue_set, portMAX_DELAY);
  226. if (activated == uart_queue) {
  227. uart_event_t event;
  228. xQueueReceive(uart_queue, &event, 0);
  229. if (event.type == UART_DATA) {
  230. bytes = uart_read_bytes(CONFIG_ESP_CONSOLE_UART_NUM, data, size < event.size ? size : event.size, 0);
  231. // we have to do our own line ending translation here
  232. for (int i = 0; i < bytes; i++) if (((char*)data)[i] == '\r') ((char*)data)[i] = '\n';
  233. break;
  234. }
  235. } else if (xRingbufferCanRead(stdin_redir.handle, activated)) {
  236. char *p = xRingbufferReceiveUpTo(stdin_redir.handle, &bytes, 0, size);
  237. // we might receive strings, replace null by \n
  238. for (int i = 0; i < bytes; i++) if (p[i] == '\0' || p[i] == '\r') p[i] = '\n';
  239. memcpy(data, p, bytes);
  240. vRingbufferReturnItem(stdin_redir.handle, p);
  241. break;
  242. }
  243. }
  244. return bytes;
  245. }
  246. static int stdin_dummy(const char * path, int flags, int mode) { return 0; }
  247. void initialize_console() {
  248. /* Minicom, screen, idf_monitor send CR when ENTER key is pressed (unused if we redirect stdin) */
  249. esp_vfs_dev_uart_port_set_rx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CR);
  250. /* Move the caret to the beginning of the next line on '\n' */
  251. esp_vfs_dev_uart_port_set_tx_line_endings(CONFIG_ESP_CONSOLE_UART_NUM, ESP_LINE_ENDINGS_CRLF);
  252. /* Configure UART. Note that REF_TICK is used so that the baud rate remains
  253. * correct while APB frequency is changing in light sleep mode.
  254. */
  255. const uart_config_t uart_config = { .baud_rate =
  256. CONFIG_ESP_CONSOLE_UART_BAUDRATE, .data_bits = UART_DATA_8_BITS,
  257. .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1,
  258. .use_ref_tick = true };
  259. ESP_ERROR_CHECK(uart_param_config(CONFIG_ESP_CONSOLE_UART_NUM, &uart_config));
  260. /* Install UART driver for interrupt-driven reads and writes */
  261. ESP_ERROR_CHECK( uart_driver_install(CONFIG_ESP_CONSOLE_UART_NUM, 256, 0, 3, &uart_queue, 0));
  262. /* Tell VFS to use UART driver */
  263. esp_vfs_dev_uart_use_driver(CONFIG_ESP_CONSOLE_UART_NUM);
  264. /* re-direct stdin to our own driver so we can gather data from various sources */
  265. stdin_redir.queue_set = xQueueCreateSet(2);
  266. stdin_redir.handle = xRingbufferCreateStatic(sizeof(stdin_redir._buf), RINGBUF_TYPE_BYTEBUF, stdin_redir._buf, &stdin_redir._ringbuf);
  267. xRingbufferAddToQueueSetRead(stdin_redir.handle, stdin_redir.queue_set);
  268. xQueueAddToSet(uart_queue, stdin_redir.queue_set);
  269. esp_vfs_t vfs = { };
  270. vfs.flags = ESP_VFS_FLAG_DEFAULT;
  271. vfs.open = stdin_dummy;
  272. vfs.read = stdin_read;
  273. ESP_ERROR_CHECK(esp_vfs_register("/dev/console", &vfs, NULL));
  274. freopen("/dev/console", "r", stdin);
  275. /* Disable buffering on stdin */
  276. setvbuf(stdin, NULL, _IONBF, 0);
  277. /* Initialize the console */
  278. esp_console_config_t console_config = { .max_cmdline_args = 28,
  279. .max_cmdline_length = 600,
  280. #if CONFIG_LOG_COLORS
  281. .hint_color = atoi(LOG_COLOR_CYAN)
  282. #endif
  283. };
  284. ESP_ERROR_CHECK(esp_console_init(&console_config));
  285. /* Configure linenoise line completion library */
  286. /* Enable multiline editing. If not set, long commands will scroll within
  287. * single line.
  288. */
  289. linenoiseSetMultiLine(1);
  290. /* Tell linenoise where to get command completions and hints */
  291. linenoiseSetCompletionCallback(&esp_console_get_completion);
  292. linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
  293. /* Set command history size */
  294. linenoiseHistorySetMaxLen(100);
  295. /* Load command history from filesystem */
  296. //linenoiseHistoryLoad(HISTORY_PATH);
  297. }
  298. bool console_push(const char *data, size_t size) {
  299. return xRingbufferSend(stdin_redir.handle, data, size, pdMS_TO_TICKS(100)) == pdPASS;
  300. }
  301. void console_start() {
  302. /* we always run console b/c telnet sends commands to stdin */
  303. initialize_console();
  304. /* Register commands */
  305. esp_console_register_help_command();
  306. register_system();
  307. register_config_cmd();
  308. register_nvs();
  309. register_wifi();
  310. if(!is_recovery_running){
  311. register_squeezelite();
  312. }
  313. else {
  314. register_ota_cmd();
  315. }
  316. register_i2ctools();
  317. printf("\n");
  318. if(is_recovery_running){
  319. printf("****************************************************************\n"
  320. "RECOVERY APPLICATION\n"
  321. "This mode is used to flash Squeezelite into the OTA partition\n"
  322. "****\n\n");
  323. }
  324. printf("Type 'help' to get the list of commands.\n"
  325. "Use UP/DOWN arrows to navigate through command history.\n"
  326. "Press TAB when typing command name to auto-complete.\n"
  327. "\n");
  328. if(!is_recovery_running){
  329. printf("To automatically execute lines at startup:\n"
  330. "\tSet NVS variable autoexec (U8) = 1 to enable, 0 to disable automatic execution.\n"
  331. "\tSet NVS variable autoexec[1~9] (string)to a command that should be executed automatically\n");
  332. }
  333. printf("\n\n");
  334. /* Figure out if the terminal supports escape sequences */
  335. int probe_status = linenoiseProbe();
  336. if (probe_status) { /* zero indicates success */
  337. printf("\n****************************\n"
  338. "Your terminal application does not support escape sequences.\n"
  339. "Line editing and history features are disabled.\n"
  340. "On Windows, try using Putty instead.\n"
  341. "****************************\n");
  342. linenoiseSetDumbMode(1);
  343. #if CONFIG_LOG_COLORS
  344. /* Since the terminal doesn't support escape sequences,
  345. * don't use color codes in the prompt.
  346. */
  347. if(is_recovery_running){
  348. recovery_prompt= "recovery-squeezelite-esp32>";
  349. }
  350. prompt = "squeezelite-esp32> ";
  351. #endif //CONFIG_LOG_COLORS
  352. }
  353. esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
  354. cfg.thread_name= "console";
  355. cfg.inherit_cfg = true;
  356. cfg.stack_size = 4*1024;
  357. if(is_recovery_running){
  358. prompt = recovery_prompt;
  359. }
  360. esp_pthread_set_cfg(&cfg);
  361. pthread_create(&thread_console, NULL, console_thread, NULL);
  362. }
  363. static esp_err_t run_command(char * line){
  364. /* Try to run the command */
  365. int ret;
  366. esp_err_t err = esp_console_run(line, &ret);
  367. if (err == ESP_ERR_NOT_FOUND) {
  368. ESP_LOGE(TAG,"Unrecognized command: %s", line);
  369. } else if (err == ESP_ERR_INVALID_ARG) {
  370. // command was empty
  371. } else if (err != ESP_OK && ret != ESP_OK) {
  372. ESP_LOGW(TAG,"Command returned non-zero error code: 0x%x (%s)", ret,
  373. esp_err_to_name(err));
  374. } else if (err == ESP_OK && ret != ESP_OK) {
  375. ESP_LOGW(TAG,"Command returned in error");
  376. err = ESP_FAIL;
  377. } else if (err != ESP_OK) {
  378. ESP_LOGE(TAG,"Internal error: %s", esp_err_to_name(err));
  379. }
  380. return err;
  381. }
  382. static void * console_thread() {
  383. if(!is_recovery_running){
  384. process_autoexec();
  385. }
  386. /* Main loop */
  387. while (1) {
  388. /* Get a line using linenoise.
  389. * The line is returned when ENTER is pressed.
  390. */
  391. char* line = linenoise(prompt);
  392. if (line == NULL) { /* Ignore empty lines */
  393. continue;
  394. }
  395. /* Add the command to the history */
  396. linenoiseHistoryAdd(line);
  397. /* Save command history to filesystem */
  398. linenoiseHistorySave(HISTORY_PATH);
  399. printf("\n");
  400. run_command(line);
  401. /* linenoise allocates line buffer on the heap, so need to free it */
  402. linenoiseFree(line);
  403. taskYIELD();
  404. }
  405. return NULL;
  406. }