console.c 7.6 KB

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