cmd_system.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. /* Console example — various system commands
  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 "cmd_system.h"
  8. #include "Config.h"
  9. #include "argtable3/argtable3.h"
  10. #include "bootstate.h"
  11. #include "driver/rtc_io.h"
  12. #include "driver/uart.h"
  13. #include "esp_console.h"
  14. #include "esp_log.h"
  15. #include "esp_ota_ops.h"
  16. #include "esp_partition.h"
  17. #include "esp_rom_uart.h"
  18. #include "esp_sleep.h"
  19. #include "esp_spi_flash.h"
  20. #include "esp_system.h"
  21. #include "freertos/FreeRTOS.h"
  22. #include "freertos/task.h"
  23. #include "messaging.h"
  24. #include "network_manager.h"
  25. #include "platform_console.h"
  26. #include "platform_esp32.h"
  27. #include "sdkconfig.h"
  28. #include "soc/rtc_cntl_reg.h"
  29. #include "tools.h"
  30. #include <ctype.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include "tools_spiffs_utils.h"
  34. #if defined(CONFIG_WITH_METRICS)
  35. #include "Metrics.h"
  36. #endif
  37. #ifdef CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
  38. #pragma message("Runtime stats enabled")
  39. #define WITH_TASKS_INFO 1
  40. #else
  41. #pragma message("Runtime stats disabled")
  42. #endif
  43. EXT_RAM_ATTR static struct {
  44. struct arg_str* device;
  45. // AirPlay device name
  46. struct arg_str* airplay;
  47. // Spotify device name
  48. struct arg_str* spotify;
  49. // Bluetooth player name advertized
  50. struct arg_str* bluetooth;
  51. // Player name reported to the Logitech Media Server
  52. struct arg_str* squeezelite;
  53. // Wifi Access Point name
  54. struct arg_str* wifi_ap_name;
  55. struct arg_lit* all;
  56. struct arg_end* end;
  57. } names_args;
  58. EXT_RAM_ATTR static struct {
  59. struct arg_str* name;
  60. struct arg_lit*reset;
  61. struct arg_end* end;
  62. } target_args;
  63. EXT_RAM_ATTR static struct {
  64. struct arg_str* confirm;
  65. struct arg_end* end;
  66. } reset_config_args;
  67. // Global or static scope
  68. EXT_RAM_ATTR static struct {
  69. struct arg_str* path;
  70. struct arg_end* end;
  71. } ls_args;
  72. // EXT_RAM_ATTR static struct {
  73. // struct arg_str *component;
  74. // struct arg_int *level;
  75. // struct arg_end *end;
  76. // } ls_level;
  77. // Global or static scope
  78. EXT_RAM_ATTR static struct {
  79. struct arg_str* path;
  80. struct arg_end* end;
  81. } erase_args;
  82. static const char* TAG = "cmd_system";
  83. static void register_free();
  84. static void register_setdevicename();
  85. static void register_settarget();
  86. static void register_heap();
  87. static void register_dump_heap();
  88. static void register_version();
  89. static void register_restart();
  90. #if CONFIG_WITH_CONFIG_UI
  91. static void register_deep_sleep();
  92. static void register_light_sleep();
  93. #endif
  94. static void register_factory_boot();
  95. static void register_restart_ota();
  96. // static void register_set_services();
  97. #if WITH_TASKS_INFO
  98. static void register_tasks();
  99. #endif
  100. extern BaseType_t network_manager_task;
  101. FILE* system_open_memstream(const char* cmdname, char** buf, size_t* buf_size) {
  102. FILE* f = open_memstream(buf, buf_size);
  103. if (f == NULL) {
  104. cmd_send_messaging(cmdname, MESSAGING_ERROR, "Unable to open memory stream.");
  105. }
  106. return f;
  107. }
  108. /* 'version' command */
  109. static int get_version(int argc, char** argv) {
  110. esp_chip_info_t info;
  111. esp_chip_info(&info);
  112. cmd_send_messaging(argv[0], MESSAGING_INFO,
  113. "IDF Version:%s\r\n"
  114. "Chip info:\r\n"
  115. "\tmodel:%s\r\n"
  116. "\tcores:%d\r\n"
  117. "\tfeature:%s%s%s%s%d%s\r\n"
  118. "\trevision number:%d\r\n",
  119. esp_get_idf_version(), info.model == CHIP_ESP32 ? "ESP32" : "Unknow", info.cores,
  120. info.features & CHIP_FEATURE_WIFI_BGN ? "/802.11bgn" : "",
  121. info.features & CHIP_FEATURE_BLE ? "/BLE" : "",
  122. info.features & CHIP_FEATURE_BT ? "/BT" : "",
  123. info.features & CHIP_FEATURE_EMB_FLASH ? "/Embedded-Flash:" : "/External-Flash:",
  124. spi_flash_get_chip_size() / (1024 * 1024), " MB", info.revision);
  125. return 0;
  126. }
  127. static void register_version() {
  128. const esp_console_cmd_t cmd = {
  129. .command = "version",
  130. .help = "Get version of chip and SDK",
  131. .hint = NULL,
  132. .func = &get_version,
  133. };
  134. cmd_to_json(&cmd);
  135. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  136. }
  137. static int restart(int argc, char** argv) {
  138. simple_restart();
  139. return 0;
  140. }
  141. static int restart_factory(int argc, char** argv) {
  142. cmd_send_messaging(argv[0], MESSAGING_WARNING, "Booting to Recovery");
  143. guided_boot(ESP_PARTITION_SUBTYPE_APP_FACTORY);
  144. return 0; // return fail. This should never return... we're rebooting!
  145. }
  146. static int restart_ota(int argc, char** argv) {
  147. cmd_send_messaging(argv[0], MESSAGING_WARNING, "Booting to Squeezelite");
  148. guided_boot(ESP_PARTITION_SUBTYPE_APP_OTA_0);
  149. return 0; // return fail. This should never return... we're rebooting!
  150. }
  151. static void register_restart() {
  152. const esp_console_cmd_t cmd = {
  153. .command = "restart",
  154. .help = "Reboot system",
  155. .hint = NULL,
  156. .func = &restart,
  157. };
  158. #if CONFIG_WITH_CONFIG_UI
  159. cmd_to_json(&cmd);
  160. #endif
  161. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  162. }
  163. static void register_restart_ota() {
  164. const esp_console_cmd_t cmd = {
  165. .command = "restart_ota",
  166. .help = "Reboot system to Squeezelite",
  167. .hint = NULL,
  168. .func = &restart_ota,
  169. };
  170. #if CONFIG_WITH_CONFIG_UI
  171. cmd_to_json(&cmd);
  172. #endif
  173. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  174. }
  175. static void register_factory_boot() {
  176. const esp_console_cmd_t cmd = {
  177. .command = "recovery",
  178. .help = "Reboot system to Recovery",
  179. .hint = NULL,
  180. .func = &restart_factory,
  181. };
  182. #if CONFIG_WITH_CONFIG_UI
  183. cmd_to_json(&cmd);
  184. #endif
  185. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  186. }
  187. /** 'free' command prints available heap memory */
  188. static int free_mem(int argc, char** argv) {
  189. cmd_send_messaging(argv[0], MESSAGING_INFO, "%d", esp_get_free_heap_size());
  190. return 0;
  191. }
  192. static void register_free() {
  193. const esp_console_cmd_t cmd = {
  194. .command = "free",
  195. .help = "Get free heap memory",
  196. .hint = NULL,
  197. .func = &free_mem,
  198. };
  199. #if CONFIG_WITH_CONFIG_UI
  200. cmd_to_json(&cmd);
  201. #endif
  202. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  203. }
  204. static int dump_heap(int argc, char** argv) {
  205. ESP_LOGD(TAG, "Dumping heap");
  206. heap_caps_dump_all();
  207. return 0;
  208. }
  209. static int dump_config(int argc, char** argv) {
  210. ESP_LOGD(TAG, "Dumping config object");
  211. config_dump_config();
  212. return 0;
  213. }
  214. /* 'heap' command prints minumum heap size */
  215. static int heap_size(int argc, char** argv) {
  216. size_t total_internal = heap_caps_get_total_size(MALLOC_CAP_INTERNAL);
  217. size_t free_internal = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
  218. size_t allocated_internal = total_internal - free_internal;
  219. size_t total_external = heap_caps_get_total_size(MALLOC_CAP_SPIRAM);
  220. size_t free_external = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
  221. size_t allocated_external = total_external - free_external;
  222. size_t total_dma = heap_caps_get_total_size(MALLOC_CAP_DMA);
  223. size_t free_dma = heap_caps_get_free_size(MALLOC_CAP_DMA);
  224. size_t allocated_dma = total_dma - free_dma;
  225. ESP_LOGI(TAG,
  226. "\nType | TOTAL | ALLOC | MIN | LARGEST | FREE | USED %%| FREE %%\n"
  227. "Internal | %7zu | %7zu | %7zu | %7zu | %7zu | %4.1f%% | %4.1f%%\n"
  228. "External | %7zu | %7zu | %7zu | %7zu | %7zu | %4.1f%% | %4.1f%%\n"
  229. "DMA | %7zu | %7zu | %7zu | %7zu | %7zu | %4.1f%% | %4.1f%%",
  230. total_internal, allocated_internal, heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  231. heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL), free_internal,
  232. (allocated_internal * 100.0) / total_internal, (free_internal * 100.0) / total_internal,
  233. total_external, allocated_external, heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM),
  234. heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM), free_external,
  235. (allocated_external * 100.0) / total_external, (free_external * 100.0) / total_external,
  236. total_dma, allocated_dma, heap_caps_get_minimum_free_size(MALLOC_CAP_DMA),
  237. heap_caps_get_largest_free_block(MALLOC_CAP_DMA), free_dma,
  238. (allocated_dma * 100.0) / total_dma, (free_dma * 100.0) / total_dma);
  239. return 0;
  240. }
  241. cJSON* setdevicename_cb() {
  242. // char * default_host_name = config_alloc_get_str("host_name",NULL,"Squeezelite");
  243. cJSON* values = cJSON_CreateObject();
  244. // cJSON_AddStringToObject(values,"name",default_host_name);
  245. // free(default_host_name);
  246. // TODO: Add support for the commented code")
  247. return values;
  248. }
  249. static int setnamevar(char* nvsname, FILE* f, char* value) {
  250. esp_err_t err = ESP_OK;
  251. // if((err=config_set_value(NVS_TYPE_STR, nvsname, value))!=ESP_OK){
  252. // fprintf(f,"Unable to set %s=%s. %s\n",nvsname,value,esp_err_to_name(err));
  253. // }
  254. // TODO: Add support for the commented code")
  255. return err == ESP_OK ? 0 : 1;
  256. }
  257. typedef enum { SCANNING, PROCESSING_NAME } scanstate_t;
  258. int set_cspot_player_name(FILE* f, const char* name) {
  259. int ret = 0;
  260. // cJSON * cspot_config = config_alloc_get_cjson("cspot_config");
  261. // if(cspot_config==NULL){
  262. // fprintf(f,"Unable to get cspot_config\n");
  263. // return 1;
  264. // }
  265. // cJSON * player_name = cJSON_GetObjectItemCaseSensitive(cspot_config,"deviceName");
  266. // if(player_name==NULL){
  267. // fprintf(f,"Unable to get deviceName\n");
  268. // ret=1;
  269. // }
  270. // if(strcmp(player_name->valuestring,name)==0){
  271. // fprintf(f,"CSpot device name not changed.\n");
  272. // ret=0;
  273. // }
  274. // else{
  275. // cJSON_SetValuestring(player_name,name);
  276. // if(setnamevar("cspot_config",f,cJSON_Print(cspot_config))!=0){
  277. // fprintf(f,"Unable to set cspot_config\n");
  278. // ret=1;
  279. // }
  280. // else{
  281. // fprintf(f,"CSpot device name set to %s\n",name);
  282. // }
  283. // }
  284. // cJSON_Delete(cspot_config);
  285. // TODO: Add support for the commented code")
  286. return ret;
  287. }
  288. int set_squeezelite_player_name(FILE* f, const char* name) {
  289. int nerrors = 0;
  290. // TODO: Add support for the commented code")
  291. // char * nvs_config= config_alloc_get(NVS_TYPE_STR, "autoexec1");
  292. // char **argv = NULL;
  293. // esp_err_t err=ESP_OK;
  294. // bool bFoundParm=false;
  295. // scanstate_t state=SCANNING;
  296. // char * newCommandLine = NULL;
  297. // char * parm = " -n ";
  298. // char * cleaned_name = strdup(name);
  299. // for(char * p=cleaned_name;*p!='\0';p++){
  300. // if(*p == ' '){
  301. // *p='_'; // no spaces allowed
  302. // }
  303. // }
  304. // if(nvs_config && strlen(nvs_config)>0){
  305. // // allocate enough memory to hold the new command line
  306. // size_t cmdLength = strlen(nvs_config) + strlen(cleaned_name) + strlen(parm) +1 ;
  307. // newCommandLine = malloc_init_external(cmdLength);
  308. // ESP_LOGD(TAG,"Parsing command %s",nvs_config);
  309. // argv = (char **) malloc_init_external(22* sizeof(char *));
  310. // if (argv == NULL) {
  311. // FREE_AND_NULL(nvs_config);
  312. // return 1;
  313. // }
  314. // size_t argc = esp_console_split_argv(nvs_config, argv,22);
  315. // for(int i=0;i<argc;i++) {
  316. // if(i>0){
  317. // strcat(newCommandLine," ");
  318. // }
  319. // switch (state)
  320. // {
  321. // case SCANNING:
  322. // strcat(newCommandLine,argv[i]);
  323. // if(strcasecmp(argv[i],"--name")==0 || strcasecmp(argv[i],"-n")==0 ){
  324. // state = PROCESSING_NAME;
  325. // }
  326. // break;
  327. // case PROCESSING_NAME:
  328. // bFoundParm=true;
  329. // strcat(newCommandLine,cleaned_name);
  330. // state = SCANNING;
  331. // break;
  332. // default:
  333. // break;
  334. // }
  335. // }
  336. // if(!bFoundParm){
  337. // strcat(newCommandLine,parm);
  338. // strcat(newCommandLine,name);
  339. // }
  340. // fprintf(f,"Squeezelite player name changed to %s\n",newCommandLine);
  341. // if((err=config_set_value(NVS_TYPE_STR, "autoexec1",newCommandLine))!=ESP_OK){
  342. // nerrors++;
  343. // fprintf(f,"Failed updating squeezelite command. %s", esp_err_to_name(err));
  344. // }
  345. // }
  346. // FREE_AND_NULL(nvs_config);
  347. // FREE_AND_NULL(argv);
  348. // free(cleaned_name);
  349. return nerrors;
  350. }
  351. static int do_cat(int argc, char** argv) {
  352. int nerrors = arg_parse_msg(argc, argv, (struct arg_hdr**)&ls_args);
  353. if (nerrors != 0) {
  354. arg_print_errors(stderr, ls_args.end, argv[0]);
  355. return 1;
  356. }
  357. // List files in the provided path
  358. cat_file(ls_args.path->sval[0]);
  359. return 0;
  360. }
  361. static int do_ls(int argc, char** argv) {
  362. int nerrors = arg_parse_msg(argc, argv, (struct arg_hdr**)&ls_args);
  363. if (nerrors != 0) {
  364. arg_print_errors(stderr, ls_args.end, argv[0]);
  365. return 1;
  366. }
  367. // List files in the provided path
  368. listFiles(ls_args.path->sval[0]);
  369. return 0;
  370. }
  371. // static int do_level(int argc, char **argv)
  372. // {
  373. // int nerrors = arg_parse_msg(argc, argv, (struct arg_hdr **)&ls_level);
  374. // if (nerrors != 0) {
  375. // arg_print_errors(stderr, ls_level.end, argv[0]);
  376. // return 1;
  377. // }
  378. // if(ls_level.component->count >0 && ls_level.level->count == 0){
  379. // // loop on ls_level.component->sval[i], and call
  380. // }
  381. // return 0;
  382. // }
  383. static int do_erase(int argc, char** argv) {
  384. int nerrors = arg_parse_msg(argc, argv, (struct arg_hdr**)&erase_args);
  385. if (nerrors != 0) {
  386. arg_print_errors(stderr, erase_args.end, argv[0]);
  387. return 1;
  388. }
  389. // Erase file at the provided path
  390. const char* path = erase_args.path->sval[0];
  391. if (!erase_path(path, true)) {
  392. // Handle error if erase_path returns false
  393. // For example, log an error message
  394. fprintf(stderr, "Failed to erase file at path: %s\n", path);
  395. return 1;
  396. }
  397. // Optionally, print a success message
  398. printf("File at path '%s' erased successfully.\n", path);
  399. return 0;
  400. }
  401. static int do_set_target(int argc, char** argv) {
  402. int nerrors = arg_parse_msg(argc, argv, (struct arg_hdr**)&target_args);
  403. if (nerrors != 0) {
  404. return 1;
  405. }
  406. if (target_args.name->count <= 0) {
  407. ESP_LOGW(TAG, "Target name must be specified. Current target: %s",
  408. STR_OR_ALT(platform->target, "N/A"));
  409. return 1;
  410. }
  411. network_async_callback((void*)target_args.name->sval[0], (network_manager_cb_t)(target_args.reset->count>0?config_set_target_reset:config_set_target_no_reset));
  412. return nerrors;
  413. }
  414. static int do_reset_config(int argc, char** argv) {
  415. int nerrors = arg_parse_msg(argc, argv, (struct arg_hdr**)&reset_config_args);
  416. if (nerrors != 0) {
  417. return 1;
  418. }
  419. if (reset_config_args.confirm->count <= 0 ||
  420. strcmp(reset_config_args.confirm->sval[0], "YES") != 0) {
  421. ESP_LOGW(TAG, "Confirmation needed. Call reset_config YES to confirm");
  422. return 1;
  423. }
  424. set_mac_string();
  425. // the below call will restart to factory
  426. config_erase_config();
  427. return nerrors;
  428. }
  429. static void register_reset_config() {
  430. reset_config_args.confirm =
  431. arg_str0(NULL, NULL, "YES", "To execute the reset, confirm with YES");
  432. reset_config_args.end = arg_end(1);
  433. const esp_console_cmd_t reset_config = {.command = "reset_config",
  434. .help = "Reset the configuration and reboot to recovery",
  435. .hint = NULL,
  436. .func = &do_reset_config,
  437. .argtable = &reset_config_args};
  438. ESP_ERROR_CHECK(esp_console_cmd_register(&reset_config));
  439. }
  440. static int setdevicename(int argc, char** argv) {
  441. bool changed = false;
  442. int nerrors = arg_parse_msg(argc, argv, (struct arg_hdr**)&names_args);
  443. if (nerrors != 0) {
  444. return 1;
  445. }
  446. if (names_args.device->count > 0) {
  447. changed = changed | system_set_string(&sys_names_config_msg, sys_names_config_device_tag,
  448. &platform->names, names_args.device->sval[0]);
  449. } else {
  450. ESP_LOGE(TAG, "Device name must be specified");
  451. return 1;
  452. }
  453. if (names_args.airplay->count > 0) {
  454. changed = changed | system_set_string(&sys_names_config_msg, sys_names_config_airplay_tag,
  455. &platform->names, names_args.airplay->sval[0]);
  456. }
  457. if (names_args.bluetooth->count > 0) {
  458. changed = changed | system_set_string(&sys_names_config_msg, sys_names_config_bluetooth_tag,
  459. &platform->names, names_args.bluetooth->sval[0]);
  460. }
  461. if (names_args.spotify->count > 0) {
  462. changed = changed | system_set_string(&sys_names_config_msg, sys_names_config_spotify_tag,
  463. &platform->names, names_args.spotify->sval[0]);
  464. }
  465. if (names_args.squeezelite->count > 0) {
  466. changed = changed | system_set_string(&sys_names_config_msg, sys_names_config_squeezelite_tag,
  467. &platform->names, names_args.squeezelite->sval[0]);
  468. }
  469. if (names_args.wifi_ap_name->count > 0) {
  470. changed = changed | system_set_string(&sys_names_config_msg, sys_names_config_wifi_ap_name_tag,
  471. &platform->names, names_args.wifi_ap_name->sval[0]);
  472. }
  473. if (names_args.all->count > 0) {
  474. ESP_LOGI(TAG, "Setting all names to %s", platform->names.device);
  475. changed = changed | system_set_string(&sys_names_config_msg, sys_names_config_airplay_tag,
  476. &platform->names, platform->names.device);
  477. changed = changed | system_set_string(&sys_names_config_msg, sys_names_config_bluetooth_tag,
  478. &platform->names, platform->names.device);
  479. changed = changed | system_set_string(&sys_names_config_msg, sys_names_config_spotify_tag,
  480. &platform->names, platform->names.device);
  481. changed = changed | system_set_string(&sys_names_config_msg, sys_names_config_squeezelite_tag,
  482. &platform->names, platform->names.device);
  483. changed = changed | system_set_string(&sys_names_config_msg, sys_names_config_wifi_ap_name_tag,
  484. &platform->names, platform->names.device);
  485. }
  486. if (changed) {
  487. ESP_LOGI(TAG, "Found change(s). Saving");
  488. config_raise_changed(false);
  489. } else {
  490. ESP_LOGW(TAG, "No change detected.");
  491. }
  492. return nerrors;
  493. }
  494. static void register_heap() {
  495. const esp_console_cmd_t heap_cmd = {
  496. .command = "heap",
  497. .help = "Get minimum size of free heap memory",
  498. .hint = NULL,
  499. .func = &heap_size,
  500. };
  501. #if CONFIG_WITH_CONFIG_UI
  502. cmd_to_json(&heap_cmd);
  503. #endif
  504. ESP_ERROR_CHECK(esp_console_cmd_register(&heap_cmd));
  505. }
  506. static void register_dump_heap() {
  507. const esp_console_cmd_t heap_cmd = {
  508. .command = "dump_heap",
  509. .help = "Dumps the content of the heap to serial output",
  510. .hint = NULL,
  511. .func = &dump_heap,
  512. };
  513. ESP_ERROR_CHECK(esp_console_cmd_register(&heap_cmd));
  514. }
  515. static void register_dump_config() {
  516. const esp_console_cmd_t cmd = {
  517. .command = "dump_config",
  518. .help = "Dumps the content of the configuration object",
  519. .hint = NULL,
  520. .func = &dump_config,
  521. };
  522. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  523. }
  524. static void register_settarget() {
  525. target_args.name = arg_str0(NULL, NULL, STR_OR_BLANK(platform->target), "New Target Name");
  526. target_args.reset = arg_lit0("r","reset","Full reset before setting target (recommended)");
  527. target_args.end = arg_end(1);
  528. const esp_console_cmd_t set_target = {.command = "target",
  529. .help = "Set the device target Name (SqueezeAMP, SqueezeIO, Muse, etc. )",
  530. .hint = NULL,
  531. .func = &do_set_target,
  532. .argtable = &target_args};
  533. ESP_ERROR_CHECK(esp_console_cmd_register(&set_target));
  534. }
  535. // static void register_loglevel()
  536. // {
  537. // ls_level.component = arg_str1(NULL, NULL, "<component>", "Component name for which to set log
  538. // level. * for all components"); ls_level.level = arg_str1(NULL, NULL, "<level: 0..5>",
  539. // "NONE=0, ERROR=1, WARN=2, INFO=3, DEBUG=4, VERBOSE=5"); ls_args.end = arg_end(1); const
  540. // esp_console_cmd_t ls_cmd = {
  541. // .command = "ls",
  542. // .help = "List files in a path",
  543. // .hint = NULL,
  544. // .func = &do_level,
  545. // .argtable = &ls_level
  546. // };
  547. // ESP_ERROR_CHECK(esp_console_cmd_register(&ls_cmd));
  548. // }
  549. static void register_ls() {
  550. ls_args.path = arg_str1(NULL, NULL, "<path>", "Path to list files");
  551. ls_args.end = arg_end(1);
  552. const esp_console_cmd_t ls_cmd = {.command = "ls",
  553. .help = "List files in a path",
  554. .hint = NULL,
  555. .func = &do_ls,
  556. .argtable = &ls_args};
  557. ESP_ERROR_CHECK(esp_console_cmd_register(&ls_cmd));
  558. }
  559. static void register_cat() {
  560. ls_args.path = arg_str1(NULL, NULL, "<path>", "File name to cat");
  561. ls_args.end = arg_end(1);
  562. const esp_console_cmd_t cat_cmd = {.command = "cat",
  563. .help = "Dumps the content of a file to the console",
  564. .hint = NULL,
  565. .func = &do_cat,
  566. .argtable = &ls_args};
  567. ESP_ERROR_CHECK(esp_console_cmd_register(&cat_cmd));
  568. }
  569. static void register_erase() {
  570. erase_args.path = arg_str1(NULL, NULL, "<path>", "Path of the file(s) to erase");
  571. erase_args.end = arg_end(1);
  572. const esp_console_cmd_t erase_cmd = {.command = "erase",
  573. .help = "Erase file(s) at a given path",
  574. .hint = NULL,
  575. .func = &do_erase,
  576. .argtable = &erase_args};
  577. ESP_ERROR_CHECK(esp_console_cmd_register(&erase_cmd));
  578. }
  579. static void register_setdevicename() {
  580. char* default_host_name = platform->names.device;
  581. names_args.device = arg_str0("n", "device", default_host_name, "New Name");
  582. names_args.airplay = arg_str0("a", "airplay", default_host_name, "New Airplay Device Name");
  583. names_args.bluetooth = arg_str0("b", "bt", default_host_name, "New Bluetooth Device Name");
  584. names_args.spotify = arg_str0("s", "spotify", default_host_name, "New Spotify Device Name");
  585. names_args.squeezelite =
  586. arg_str0("l", "squeezelite", default_host_name, "New Squeezelite Player Name");
  587. names_args.wifi_ap_name = arg_str0("w", "wifiap", default_host_name, "New Wifi AP Name");
  588. names_args.all = arg_lit0(NULL, "all", "Set all names to device name");
  589. names_args.end = arg_end(2);
  590. const esp_console_cmd_t set_name = {.command = CFG_TYPE_SYST("name"),
  591. .help = "Device Name",
  592. .hint = NULL,
  593. .func = &setdevicename,
  594. .argtable = &names_args};
  595. ESP_ERROR_CHECK(esp_console_cmd_register(&set_name));
  596. }
  597. /** 'tasks' command prints the list of tasks and related information */
  598. #if WITH_TASKS_INFO
  599. static int tasks_info(int argc, char** argv) {
  600. const size_t bytes_per_task = 40; /* see vTaskList description */
  601. char* task_list_buffer = malloc_init_external(uxTaskGetNumberOfTasks() * bytes_per_task);
  602. if (task_list_buffer == NULL) {
  603. cmd_send_messaging(
  604. argv[0], MESSAGING_ERROR, "failed to allocate buffer for vTaskList output");
  605. return 1;
  606. }
  607. cmd_send_messaging(argv[0], MESSAGING_INFO,
  608. "Task Name\tStatus\tPrio\tHWM\tTask#"
  609. #ifdef CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID
  610. "\tAffinity"
  611. #endif
  612. "\n");
  613. vTaskList(task_list_buffer);
  614. cmd_send_messaging(argv[0], MESSAGING_INFO, "%s", task_list_buffer);
  615. free(task_list_buffer);
  616. return 0;
  617. }
  618. static void register_tasks() {
  619. const esp_console_cmd_t cmd = {
  620. .command = "tasks",
  621. .help = "Get information about running tasks",
  622. .hint = NULL,
  623. .func = &tasks_info,
  624. };
  625. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  626. }
  627. #endif // WITH_TASKS_INFO
  628. /** 'deep_sleep' command puts the chip into deep sleep mode */
  629. #if CONFIG_WITH_CONFIG_UI
  630. static struct {
  631. struct arg_int* wakeup_time;
  632. struct arg_int* wakeup_gpio_num;
  633. struct arg_int* wakeup_gpio_level;
  634. struct arg_end* end;
  635. } deep_sleep_args;
  636. static int deep_sleep(int argc, char** argv) {
  637. int nerrors = arg_parse_msg(argc, argv, (struct arg_hdr**)&deep_sleep_args);
  638. if (nerrors != 0) {
  639. return 1;
  640. }
  641. if (deep_sleep_args.wakeup_time->count) {
  642. uint64_t timeout = 1000ULL * deep_sleep_args.wakeup_time->ival[0];
  643. cmd_send_messaging(
  644. argv[0], MESSAGING_INFO, "Enabling timer wakeup, timeout=%lluus", timeout);
  645. ESP_ERROR_CHECK(esp_sleep_enable_timer_wakeup(timeout));
  646. }
  647. if (deep_sleep_args.wakeup_gpio_num->count) {
  648. int io_num = deep_sleep_args.wakeup_gpio_num->ival[0];
  649. if (!rtc_gpio_is_valid_gpio(io_num)) {
  650. cmd_send_messaging(argv[0], MESSAGING_ERROR, "GPIO %d is not an RTC IO", io_num);
  651. return 1;
  652. }
  653. int level = 0;
  654. if (deep_sleep_args.wakeup_gpio_level->count) {
  655. level = deep_sleep_args.wakeup_gpio_level->ival[0];
  656. if (level != 0 && level != 1) {
  657. cmd_send_messaging(argv[0], MESSAGING_ERROR, "Invalid wakeup level: %d", level);
  658. return 1;
  659. }
  660. }
  661. cmd_send_messaging(argv[0], MESSAGING_INFO, "Enabling wakeup on GPIO%d, wakeup on %s level",
  662. io_num, level ? "HIGH" : "LOW");
  663. ESP_ERROR_CHECK(esp_sleep_enable_ext1_wakeup(1ULL << io_num, level));
  664. }
  665. rtc_gpio_isolate(GPIO_NUM_12);
  666. esp_deep_sleep_start();
  667. return 0; // this code will never run. deep sleep will cause the system to restart
  668. }
  669. static void register_deep_sleep() {
  670. deep_sleep_args.wakeup_time = arg_int0("t", "time", "<t>", "Wake up time, ms");
  671. deep_sleep_args.wakeup_gpio_num =
  672. arg_int0(NULL, "io", "<n>", "If specified, wakeup using GPIO with given number");
  673. deep_sleep_args.wakeup_gpio_level =
  674. arg_int0(NULL, "io_level", "<0|1>", "GPIO level to trigger wakeup");
  675. deep_sleep_args.end = arg_end(3);
  676. const esp_console_cmd_t cmd = {.command = "deep_sleep",
  677. .help = "Enter deep sleep mode. ",
  678. .hint = NULL,
  679. .func = &deep_sleep,
  680. .argtable = &deep_sleep_args};
  681. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  682. }
  683. #endif
  684. static int enable_disable(FILE* f, char* nvs_name, struct arg_lit* arg) {
  685. esp_err_t err = ESP_OK;
  686. // err= config_set_value(NVS_TYPE_STR, nvs_name, arg->count>0?"Y":"N");
  687. // const char * name = arg->hdr.longopts?arg->hdr.longopts:arg->hdr.glossary;
  688. // if(err!=ESP_OK){
  689. // fprintf(f,"Error %s %s. %s\n",arg->count>0?"Enabling":"Disabling", name,
  690. // esp_err_to_name(err));
  691. // }
  692. // else {
  693. // fprintf(f,"%s %s\n",arg->count>0?"Enabled":"Disabled",name);
  694. // }
  695. return err;
  696. }
  697. // static int do_set_services(int argc, char **argv)
  698. // {
  699. // esp_err_t err = ESP_OK;
  700. // int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&set_services_args);
  701. // if (nerrors != 0) {
  702. // return 1;
  703. // }
  704. // char *buf = NULL;
  705. // size_t buf_size = 0;
  706. // FILE *f = system_open_memstream(argv[0],&buf, &buf_size);
  707. // if (f == NULL) {
  708. // return 1;
  709. // }
  710. // nerrors += enable_disable(f,"enable_airplay",set_services_args.airplay);
  711. // nerrors += enable_disable(f,"enable_bt_sink",set_services_args.btspeaker);
  712. // #if CONFIG_CSPOT_SINK
  713. // nerrors += enable_disable(f,"enable_cspot",set_services_args.cspot);
  714. // #endif
  715. // if(set_services_args.telnet->count>0){
  716. // // if(strcasecmp(set_services_args.telnet->sval[0],"Disabled") == 0){
  717. // // err = config_set_value(NVS_TYPE_STR, "telnet_enable", "N");
  718. // // }
  719. // // else if(strcasecmp(set_services_args.telnet->sval[0],"Telnet Only") == 0){
  720. // // err = config_set_value(NVS_TYPE_STR, "telnet_enable", "Y");
  721. // // }
  722. // // else if(strcasecmp(set_services_args.telnet->sval[0],"Telnet and Serial") == 0){
  723. // // err = config_set_value(NVS_TYPE_STR, "telnet_enable", "D");
  724. // // }
  725. // // TODO: Add support for the commented code")
  726. // if(err!=ESP_OK){
  727. // nerrors++;
  728. // fprintf(f,"Error setting telnet to %s. %s\n",set_services_args.telnet->sval[0],
  729. // esp_err_to_name(err));
  730. // }
  731. // else {
  732. // fprintf(f,"Telnet service changed to %s\n",set_services_args.telnet->sval[0]);
  733. // }
  734. // }
  735. // #if WITH_TASKS_INFO
  736. // nerrors += enable_disable(f,"stats",set_services_args.stats);
  737. // #endif
  738. // if(!nerrors ){
  739. // fprintf(f,"Done.\n");
  740. // }
  741. // fflush (f);
  742. // cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  743. // fclose(f);
  744. // FREE_AND_NULL(buf);
  745. // return nerrors;
  746. // }
  747. // cJSON * set_services_cb(){
  748. // cJSON * values = cJSON_CreateObject();
  749. // char * p=NULL;
  750. // console_set_bool_parameter(values,"enable_bt_sink",set_services_args.btspeaker);
  751. // console_set_bool_parameter(values,"enable_airplay",set_services_args.airplay);
  752. // #if CONFIG_CSPOT_SINK
  753. // console_set_bool_parameter(values,"enable_cspot",set_services_args.cspot);
  754. // #endif
  755. // #if WITH_TASKS_INFO
  756. // console_set_bool_parameter(values,"stats",set_services_args.stats);
  757. // #endif
  758. // // if ((p = config_alloc_get(NVS_TYPE_STR, "telnet_enable")) != NULL) {
  759. // // if(strcasestr("YX",p)!=NULL){
  760. // // cJSON_AddStringToObject(values,set_services_args.telnet->hdr.longopts,"Telnet
  761. // Only");
  762. // // }
  763. // // else if(strcasestr("D",p)!=NULL){
  764. // // cJSON_AddStringToObject(values,set_services_args.telnet->hdr.longopts,"Telnet and
  765. // Serial");
  766. // // }
  767. // // else {
  768. // // cJSON_AddStringToObject(values,set_services_args.telnet->hdr.longopts,"Disabled");
  769. // // }
  770. // // #if defined(CONFIG_WITH_METRICS)
  771. // // metrics_add_feature_variant("telnet",p);
  772. // // #endif
  773. // // FREE_AND_NULL(p);
  774. // // }
  775. // // TODO: Add support for the commented code")
  776. // return values;
  777. // }
  778. // static void register_set_services(){
  779. // set_services_args.airplay = arg_lit0(NULL, "AirPlay", "AirPlay");
  780. // #if CONFIG_CSPOT_SINK
  781. // set_services_args.cspot = arg_lit0(NULL, "cspot", "Spotify (cspot)");
  782. // #endif
  783. // set_services_args.btspeaker = arg_lit0(NULL, "BT_Speaker", "Bluetooth Speaker");
  784. // set_services_args.telnet= arg_str0("t", "telnet","Disabled|Telnet Only|Telnet and
  785. // Serial","Telnet server. Use only for troubleshooting"); #if WITH_TASKS_INFO
  786. // set_services_args.stats= arg_lit0(NULL, "stats", "System Statistics. Use only for
  787. // troubleshooting"); #endif
  788. // set_services_args.end=arg_end(2);
  789. // const esp_console_cmd_t cmd = {
  790. // .command = CFG_TYPE_SYST("services"),
  791. // .help = "Services",
  792. // .argtable = &set_services_args,
  793. // .hint = NULL,
  794. // .func = &do_set_services,
  795. // };
  796. // cmd_to_json_with_cb(&cmd,&set_services_cb);
  797. // ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  798. // }
  799. #if CONFIG_WITH_CONFIG_UI
  800. static struct {
  801. struct arg_int* wakeup_time;
  802. struct arg_int* wakeup_gpio_num;
  803. struct arg_int* wakeup_gpio_level;
  804. struct arg_end* end;
  805. } light_sleep_args;
  806. static int light_sleep(int argc, char** argv) {
  807. int nerrors = arg_parse_msg(argc, argv, (struct arg_hdr**)&light_sleep_args);
  808. if (nerrors != 0) {
  809. return 1;
  810. }
  811. esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
  812. if (light_sleep_args.wakeup_time->count) {
  813. uint64_t timeout = 1000ULL * light_sleep_args.wakeup_time->ival[0];
  814. cmd_send_messaging(
  815. argv[0], MESSAGING_INFO, "Enabling timer wakeup, timeout=%lluus", timeout);
  816. ESP_ERROR_CHECK(esp_sleep_enable_timer_wakeup(timeout));
  817. }
  818. int io_count = light_sleep_args.wakeup_gpio_num->count;
  819. if (io_count != light_sleep_args.wakeup_gpio_level->count) {
  820. cmd_send_messaging(
  821. argv[0], MESSAGING_INFO, "Should have same number of 'io' and 'io_level' arguments");
  822. return 1;
  823. }
  824. for (int i = 0; i < io_count; ++i) {
  825. int io_num = light_sleep_args.wakeup_gpio_num->ival[i];
  826. int level = light_sleep_args.wakeup_gpio_level->ival[i];
  827. if (level != 0 && level != 1) {
  828. cmd_send_messaging(argv[0], MESSAGING_ERROR, "Invalid wakeup level: %d", level);
  829. return 1;
  830. }
  831. cmd_send_messaging(argv[0], MESSAGING_INFO, "Enabling wakeup on GPIO%d, wakeup on %s level",
  832. io_num, level ? "HIGH" : "LOW");
  833. ESP_ERROR_CHECK(
  834. gpio_wakeup_enable(io_num, level ? GPIO_INTR_HIGH_LEVEL : GPIO_INTR_LOW_LEVEL));
  835. }
  836. if (io_count > 0) {
  837. ESP_ERROR_CHECK(esp_sleep_enable_gpio_wakeup());
  838. }
  839. if (CONFIG_ESP_CONSOLE_UART_NUM <= UART_NUM_1) {
  840. cmd_send_messaging(
  841. argv[0], MESSAGING_INFO, "Enabling UART wakeup (press ENTER to exit light sleep)");
  842. ESP_ERROR_CHECK(uart_set_wakeup_threshold(CONFIG_ESP_CONSOLE_UART_NUM, 3));
  843. ESP_ERROR_CHECK(esp_sleep_enable_uart_wakeup(CONFIG_ESP_CONSOLE_UART_NUM));
  844. }
  845. fflush(stdout);
  846. esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
  847. esp_light_sleep_start();
  848. esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
  849. const char* cause_str;
  850. switch (cause) {
  851. case ESP_SLEEP_WAKEUP_GPIO:
  852. cause_str = "GPIO";
  853. break;
  854. case ESP_SLEEP_WAKEUP_UART:
  855. cause_str = "UART";
  856. break;
  857. case ESP_SLEEP_WAKEUP_TIMER:
  858. cause_str = "timer";
  859. break;
  860. default:
  861. cause_str = "unknown";
  862. printf("%d\n", cause);
  863. }
  864. cmd_send_messaging(argv[0], MESSAGING_INFO, "Woke up from: %s", cause_str);
  865. return 0;
  866. }
  867. static void register_light_sleep() {
  868. light_sleep_args.wakeup_time = arg_int0("t", "time", "<t>", "Wake up time, ms");
  869. light_sleep_args.wakeup_gpio_num =
  870. arg_intn(NULL, "io", "<n>", 0, 8, "If specified, wakeup using GPIO with given number");
  871. light_sleep_args.wakeup_gpio_level =
  872. arg_intn(NULL, "io_level", "<0|1>", 0, 8, "GPIO level to trigger wakeup");
  873. light_sleep_args.end = arg_end(3);
  874. const esp_console_cmd_t cmd = {.command = "light_sleep",
  875. .help = "Enter light sleep mode. "
  876. "Two wakeup modes are supported: timer and GPIO. "
  877. "Multiple GPIO pins can be specified using pairs of "
  878. "'io' and 'io_level' arguments. "
  879. "Will also wake up on UART input.",
  880. .hint = NULL,
  881. .func = &light_sleep,
  882. .argtable = &light_sleep_args};
  883. ESP_ERROR_CHECK(esp_console_cmd_register(&cmd));
  884. }
  885. #endif
  886. void register_system() {
  887. // register_set_services();
  888. // register_loglevel();
  889. register_reset_config();
  890. register_setdevicename();
  891. register_free();
  892. register_heap();
  893. register_dump_heap();
  894. register_version();
  895. register_restart();
  896. register_factory_boot();
  897. register_restart_ota();
  898. register_ls();
  899. register_cat();
  900. register_settarget();
  901. register_erase();
  902. register_dump_config();
  903. #if WITH_TASKS_INFO
  904. register_tasks();
  905. #endif
  906. #if CONFIG_WITH_CONFIG_UI
  907. register_deep_sleep();
  908. register_light_sleep();
  909. #endif
  910. }