platform_console.c 8.3 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 "platform_console.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "esp_system.h"
  12. #include "esp_log.h"
  13. #include "esp_console.h"
  14. #include "esp_vfs_dev.h"
  15. #include "driver/uart.h"
  16. #include "linenoise/linenoise.h"
  17. #include "argtable3/argtable3.h"
  18. #include "nvs.h"
  19. #include "nvs_flash.h"
  20. #include "pthread.h"
  21. #include "platform_esp32.h"
  22. #include "esp_pthread.h"
  23. #include "cmd_decl.h"
  24. #include "wifi_manager.h"
  25. #include "platform_config.h"
  26. #include "telnet.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. extern void register_squeezelite();
  34. /* Prompt to be printed before each line.
  35. * This can be customized, made dynamic, etc.
  36. */
  37. const char* prompt = LOG_COLOR_I "squeezelite-esp32> " LOG_RESET_COLOR;
  38. const char* recovery_prompt = LOG_COLOR_E "recovery-squeezelite-esp32> " LOG_RESET_COLOR;
  39. /* Console command history can be stored to and loaded from a file.
  40. * The easiest way to do this is to use FATFS filesystem on top of
  41. * wear_levelling library.
  42. */
  43. #define MOUNT_PATH "/data"
  44. #define HISTORY_PATH MOUNT_PATH "/history.txt"
  45. void run_command(char * line);
  46. void process_autoexec(){
  47. int i=1;
  48. char autoexec_name[21]={0};
  49. char * autoexec_value=NULL;
  50. uint8_t autoexec_flag=0;
  51. char * str_flag = config_alloc_get(NVS_TYPE_STR, "autoexec");
  52. if(!bypass_wifi_manager){
  53. ESP_LOGW(TAG, "Processing autoexec commands while wifi_manager active. Wifi related commands will be ignored.");
  54. }
  55. if(is_recovery_running){
  56. ESP_LOGD(TAG, "Processing autoexec commands in recovery mode. Squeezelite commands will be ignored.");
  57. }
  58. if(str_flag !=NULL ){
  59. autoexec_flag=atoi(str_flag);
  60. ESP_LOGI(TAG,"autoexec is set to %s auto-process", autoexec_flag>0?"perform":"skip");
  61. if(autoexec_flag == 1) {
  62. do {
  63. snprintf(autoexec_name,sizeof(autoexec_name)-1,"autoexec%u",i++);
  64. ESP_LOGD(TAG,"Getting command name %s", autoexec_name);
  65. autoexec_value= config_alloc_get(NVS_TYPE_STR, autoexec_name);
  66. if(autoexec_value!=NULL ){
  67. if(!bypass_wifi_manager && strstr(autoexec_value, "join ")!=NULL ){
  68. ESP_LOGW(TAG,"Ignoring wifi join command.");
  69. }
  70. else if(is_recovery_running && !strstr(autoexec_value, "squeezelite " ) ){
  71. ESP_LOGW(TAG,"Ignoring command. ");
  72. }
  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(!is_recovery_running){
  154. register_squeezelite();
  155. }
  156. else {
  157. register_ota_cmd();
  158. }
  159. register_i2ctools();
  160. if(!is_serial_suppressed()){
  161. printf("\n");
  162. if(is_recovery_running){
  163. printf("****************************************************************\n"
  164. "RECOVERY APPLICATION\n"
  165. "This mode is used to flash Squeezelite into the OTA partition\n"
  166. "****\n\n");
  167. }
  168. printf("Type 'help' to get the list of commands.\n"
  169. "Use UP/DOWN arrows to navigate through command history.\n"
  170. "Press TAB when typing command name to auto-complete.\n"
  171. "\n");
  172. if(!is_recovery_running){
  173. printf("To automatically execute lines at startup:\n"
  174. "\tSet NVS variable autoexec (U8) = 1 to enable, 0 to disable automatic execution.\n"
  175. "\tSet NVS variable autoexec[1~9] (string)to a command that should be executed automatically\n");
  176. }
  177. printf("\n\n");
  178. /* Figure out if the terminal supports escape sequences */
  179. int probe_status = linenoiseProbe();
  180. if (probe_status) { /* zero indicates success */
  181. printf("\n****************************\n"
  182. "Your terminal application does not support escape sequences.\n"
  183. "Line editing and history features are disabled.\n"
  184. "On Windows, try using Putty instead.\n"
  185. "****************************\n");
  186. linenoiseSetDumbMode(1);
  187. #if CONFIG_LOG_COLORS
  188. /* Since the terminal doesn't support escape sequences,
  189. * don't use color codes in the prompt.
  190. */
  191. if(is_recovery_running){
  192. recovery_prompt= "recovery-squeezelite-esp32>";
  193. }
  194. prompt = "squeezelite-esp32> ";
  195. #endif //CONFIG_LOG_COLORS
  196. }
  197. esp_pthread_cfg_t cfg = esp_pthread_get_default_config();
  198. cfg.thread_name= "console";
  199. cfg.inherit_cfg = true;
  200. if(is_recovery_running){
  201. prompt = recovery_prompt;
  202. cfg.stack_size = 4096 ;
  203. }
  204. esp_pthread_set_cfg(&cfg);
  205. pthread_attr_t attr;
  206. pthread_attr_init(&attr);
  207. pthread_create(&thread_console, &attr, console_thread, NULL);
  208. pthread_attr_destroy(&attr);
  209. }
  210. else if(!is_recovery_running){
  211. process_autoexec();
  212. }
  213. }
  214. void run_command(char * line){
  215. /* Try to run the command */
  216. int ret;
  217. esp_err_t err = esp_console_run(line, &ret);
  218. if (err == ESP_ERR_NOT_FOUND) {
  219. ESP_LOGE(TAG,"Unrecognized command: %s", line);
  220. } else if (err == ESP_ERR_INVALID_ARG) {
  221. // command was empty
  222. } else if (err == ESP_OK && ret != ESP_OK) {
  223. ESP_LOGW(TAG,"Command returned non-zero error code: 0x%x (%s)", ret,
  224. esp_err_to_name(err));
  225. } else if (err != ESP_OK) {
  226. ESP_LOGE(TAG,"Internal error: %s", esp_err_to_name(err));
  227. }
  228. }
  229. static void * console_thread() {
  230. if(!is_recovery_running){
  231. process_autoexec();
  232. }
  233. /* Main loop */
  234. while (1) {
  235. /* Get a line using linenoise.
  236. * The line is returned when ENTER is pressed.
  237. */
  238. char* line = linenoise(prompt);
  239. if (line == NULL) { /* Ignore empty lines */
  240. continue;
  241. }
  242. /* Add the command to the history */
  243. linenoiseHistoryAdd(line);
  244. /* Save command history to filesystem */
  245. linenoiseHistorySave(HISTORY_PATH);
  246. printf("\n");
  247. run_command(line);
  248. /* linenoise allocates line buffer on the heap, so need to free it */
  249. linenoiseFree(line);
  250. taskYIELD();
  251. }
  252. return NULL;
  253. }