console.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "esp_system.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 "esp_pthread.h"
  22. #include "cmd_decl.h"
  23. #include "console.h"
  24. #include "wifi_manager.h"
  25. #include "telnet.h"
  26. #include "gds.h"
  27. #include "gds_default_if.h"
  28. #include "gds_draw.h"
  29. #include "gds_text.h"
  30. #include "gds_font.h"
  31. #include "display.h"
  32. #include "cmd_squeezelite.h"
  33. #include "config.h"
  34. pthread_t thread_console;
  35. static void * console_thread();
  36. void console_start();
  37. static const char * TAG = "console";
  38. extern bool bypass_wifi_manager;
  39. /* Prompt to be printed before each line.
  40. * This can be customized, made dynamic, etc.
  41. */
  42. const char* prompt = LOG_COLOR_I "squeezelite-esp32> " LOG_RESET_COLOR;
  43. /* Console command history can be stored to and loaded from a file.
  44. * The easiest way to do this is to use FATFS filesystem on top of
  45. * wear_levelling library.
  46. */
  47. #define MOUNT_PATH "/data"
  48. #define HISTORY_PATH MOUNT_PATH "/history.txt"
  49. void run_command(char * line);
  50. void process_autoexec(){
  51. int i=1;
  52. char autoexec_name[21]={0};
  53. char * autoexec_value=NULL;
  54. uint8_t autoexec_flag=0;
  55. char * str_flag = config_alloc_get(NVS_TYPE_STR, "autoexec");
  56. if(!bypass_wifi_manager){
  57. ESP_LOGW(TAG, "Processing autoexec commands while wifi_manager active. Wifi related commands will be ignored.");
  58. }
  59. #if RECOVERY_APPLICATION
  60. ESP_LOGD(TAG, "Processing autoexec commands in recovery mode. Squeezelite commands will be ignored.");
  61. #endif
  62. if(str_flag !=NULL ){
  63. autoexec_flag=atoi(str_flag);
  64. ESP_LOGI(TAG,"autoexec is set to %s auto-process", autoexec_flag>0?"perform":"skip");
  65. if(autoexec_flag == 1) {
  66. do {
  67. snprintf(autoexec_name,sizeof(autoexec_name)-1,"autoexec%u",i++);
  68. ESP_LOGD(TAG,"Getting command name %s", autoexec_name);
  69. autoexec_value= config_alloc_get(NVS_TYPE_STR, autoexec_name);
  70. if(autoexec_value!=NULL ){
  71. if(!bypass_wifi_manager && strstr(autoexec_value, "join ")!=NULL ){
  72. ESP_LOGW(TAG,"Ignoring wifi join command.");
  73. }
  74. #if RECOVERY_APPLICATION
  75. else if(!strstr(autoexec_value, "squeezelite " ) ){
  76. ESP_LOGW(TAG,"Ignoring command. ");
  77. }
  78. #endif
  79. else {
  80. ESP_LOGI(TAG,"Running command %s = %s", autoexec_name, autoexec_value);
  81. run_command(autoexec_value);
  82. }
  83. ESP_LOGD(TAG,"Freeing memory for command %s name", autoexec_name);
  84. free(autoexec_value);
  85. }
  86. else {
  87. ESP_LOGD(TAG,"No matching command found for name %s", autoexec_name);
  88. break;
  89. }
  90. } while(1);
  91. }
  92. free(str_flag);
  93. }
  94. else
  95. {
  96. ESP_LOGD(TAG,"No matching command found for name autoexec.");
  97. }
  98. }
  99. void initialize_console() {
  100. /* Disable buffering on stdin */
  101. setvbuf(stdin, NULL, _IONBF, 0);
  102. /* Minicom, screen, idf_monitor send CR when ENTER key is pressed */
  103. esp_vfs_dev_uart_set_rx_line_endings(ESP_LINE_ENDINGS_CR);
  104. /* Move the caret to the beginning of the next line on '\n' */
  105. esp_vfs_dev_uart_set_tx_line_endings(ESP_LINE_ENDINGS_CRLF);
  106. /* Configure UART. Note that REF_TICK is used so that the baud rate remains
  107. * correct while APB frequency is changing in light sleep mode.
  108. */
  109. const uart_config_t uart_config = { .baud_rate =
  110. CONFIG_CONSOLE_UART_BAUDRATE, .data_bits = UART_DATA_8_BITS,
  111. .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1,
  112. .use_ref_tick = true };
  113. ESP_ERROR_CHECK(uart_param_config(CONFIG_CONSOLE_UART_NUM, &uart_config));
  114. /* Install UART driver for interrupt-driven reads and writes */
  115. ESP_ERROR_CHECK(
  116. uart_driver_install(CONFIG_CONSOLE_UART_NUM, 256, 0, 0, NULL, 0));
  117. /* Tell VFS to use UART driver */
  118. esp_vfs_dev_uart_use_driver(CONFIG_CONSOLE_UART_NUM);
  119. /* Initialize the console */
  120. esp_console_config_t console_config = { .max_cmdline_args = 22,
  121. .max_cmdline_length = 600,
  122. #if CONFIG_LOG_COLORS
  123. .hint_color = atoi(LOG_COLOR_CYAN)
  124. #endif
  125. };
  126. ESP_ERROR_CHECK(esp_console_init(&console_config));
  127. /* Configure linenoise line completion library */
  128. /* Enable multiline editing. If not set, long commands will scroll within
  129. * single line.
  130. */
  131. linenoiseSetMultiLine(1);
  132. /* Tell linenoise where to get command completions and hints */
  133. linenoiseSetCompletionCallback(&esp_console_get_completion);
  134. linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);
  135. /* Set command history size */
  136. linenoiseHistorySetMaxLen(100);
  137. /* Load command history from filesystem */
  138. //linenoiseHistoryLoad(HISTORY_PATH);
  139. }
  140. void console_start() {
  141. if(!is_serial_suppressed()){
  142. initialize_console();
  143. }
  144. else {
  145. /* Initialize the console */
  146. esp_console_config_t console_config = { .max_cmdline_args = 22,
  147. .max_cmdline_length = 600,
  148. #if CONFIG_LOG_COLORS
  149. .hint_color = atoi(LOG_COLOR_CYAN)
  150. #endif
  151. };
  152. ESP_ERROR_CHECK(esp_console_init(&console_config));
  153. }
  154. /* Register commands */
  155. esp_console_register_help_command();
  156. register_system();
  157. register_nvs();
  158. register_wifi();
  159. #if RECOVERY_APPLICATION!=1
  160. register_squeezelite();
  161. #elif RECOVERY_APPLICATION==1
  162. register_ota_cmd();
  163. #else
  164. #error "Unknown build configuration"
  165. #endif
  166. register_i2ctools();
  167. if(!is_serial_suppressed()){
  168. printf("\n"
  169. #if RECOVERY_APPLICATION
  170. "****************************************************************\n"
  171. "RECOVERY APPLICATION\n"
  172. "This mode is used to flash Squeezelite into the OTA partition\n"
  173. "****\n\n"
  174. #endif
  175. "Type 'help' to get the list of commands.\n"
  176. "Use UP/DOWN arrows to navigate through command history.\n"
  177. "Press TAB when typing command name to auto-complete.\n"
  178. "\n"
  179. #if !RECOVERY_APPLICATION
  180. "To automatically execute lines at startup:\n"
  181. "\tSet NVS variable autoexec (U8) = 1 to enable, 0 to disable automatic execution.\n"
  182. "\tSet NVS variable autoexec[1~9] (string)to a command that should be executed automatically\n"
  183. #endif
  184. "\n"
  185. "\n");
  186. #if RECOVERY_APPLICATION
  187. GDS_ClearExt(display, true);
  188. GDS_SetFont(display, &Font_droid_sans_fallback_15x17 );
  189. GDS_TextPos(display, GDS_FONT_MEDIUM, GDS_TEXT_CENTERED, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, "RECOVERY");
  190. #endif
  191. /* Figure out if the terminal supports escape sequences */
  192. int probe_status = linenoiseProbe();
  193. if (probe_status) { /* zero indicates success */
  194. printf("\n****************************\n"
  195. "Your terminal application does not support escape sequences.\n"
  196. "Line editing and history features are disabled.\n"
  197. "On Windows, try using Putty instead.\n"
  198. "****************************\n");
  199. linenoiseSetDumbMode(1);
  200. #if CONFIG_LOG_COLORS
  201. /* Since the terminal doesn't support escape sequences,
  202. * don't use color codes in the prompt.
  203. */
  204. prompt = "squeezelite-esp32> ";
  205. #endif //CONFIG_LOG_COLORS
  206. }
  207. esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
  208. cfg.thread_name= "console";
  209. cfg.inherit_cfg = true;
  210. #if RECOVERY_APPLICATION
  211. cfg.stack_size = 4096 ;
  212. #endif
  213. esp_pthread_set_cfg(&cfg);
  214. pthread_attr_t attr;
  215. pthread_attr_init(&attr);
  216. pthread_create(&thread_console, &attr, console_thread, NULL);
  217. pthread_attr_destroy(&attr);
  218. }
  219. else {
  220. #if !RECOVERY_APPLICATION
  221. // process autoexec locally, as we're not going to start the console thread
  222. process_autoexec();
  223. #endif
  224. }
  225. }
  226. void run_command(char * line){
  227. /* Try to run the command */
  228. int ret;
  229. esp_err_t err = esp_console_run(line, &ret);
  230. if (err == ESP_ERR_NOT_FOUND) {
  231. ESP_LOGE(TAG,"Unrecognized command: %s", line);
  232. } else if (err == ESP_ERR_INVALID_ARG) {
  233. // command was empty
  234. } else if (err == ESP_OK && ret != ESP_OK) {
  235. ESP_LOGW(TAG,"Command returned non-zero error code: 0x%x (%s)", ret,
  236. esp_err_to_name(err));
  237. } else if (err != ESP_OK) {
  238. ESP_LOGE(TAG,"Internal error: %s", esp_err_to_name(err));
  239. }
  240. }
  241. static void * console_thread() {
  242. #if !RECOVERY_APPLICATION
  243. process_autoexec();
  244. #endif
  245. /* Main loop */
  246. while (1) {
  247. /* Get a line using linenoise.
  248. * The line is returned when ENTER is pressed.
  249. */
  250. char* line = linenoise(prompt);
  251. if (line == NULL) { /* Ignore empty lines */
  252. continue;
  253. }
  254. /* Add the command to the history */
  255. linenoiseHistoryAdd(line);
  256. /* Save command history to filesystem */
  257. linenoiseHistorySave(HISTORY_PATH);
  258. printf("\n");
  259. run_command(line);
  260. /* linenoise allocates line buffer on the heap, so need to free it */
  261. linenoiseFree(line);
  262. taskYIELD();
  263. }
  264. return NULL;
  265. }