console.c 7.7 KB

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