console.c 8.2 KB

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