cmd_system.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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 <stdio.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include "esp_log.h"
  11. #include "esp_console.h"
  12. #include "esp_system.h"
  13. #include "esp_spi_flash.h"
  14. #include "driver/rtc_io.h"
  15. #include "driver/uart.h"
  16. #include "argtable3/argtable3.h"
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/task.h"
  19. #include "soc/rtc_cntl_reg.h"
  20. #include "esp_rom_uart.h"
  21. #include "cmd_system.h"
  22. #include "sdkconfig.h"
  23. #include "esp_partition.h"
  24. #include "esp_ota_ops.h"
  25. #include "platform_esp32.h"
  26. #include "platform_config.h"
  27. #include "esp_sleep.h"
  28. #include "messaging.h"
  29. #include "platform_console.h"
  30. #include "tools.h"
  31. #ifdef CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS
  32. #pragma message("Runtime stats enabled")
  33. #define WITH_TASKS_INFO 1
  34. #else
  35. #pragma message("Runtime stats disabled")
  36. #endif
  37. EXT_RAM_ATTR static struct {
  38. struct arg_str *name;
  39. struct arg_end *end;
  40. } name_args;
  41. EXT_RAM_ATTR static struct {
  42. #if CONFIG_CSPOT_SINK
  43. struct arg_lit *cspot;
  44. #endif
  45. struct arg_lit *btspeaker;
  46. struct arg_lit *airplay;
  47. struct arg_str *telnet;
  48. #if WITH_TASKS_INFO
  49. struct arg_lit *stats;
  50. #endif
  51. struct arg_end *end;
  52. } set_services_args;
  53. static const char * TAG = "cmd_system";
  54. //static void register_setbtsource();
  55. static void register_free();
  56. static void register_setdevicename();
  57. static void register_heap();
  58. static void register_dump_heap();
  59. static void register_abort();
  60. static void register_version();
  61. static void register_restart();
  62. #if CONFIG_WITH_CONFIG_UI
  63. static void register_deep_sleep();
  64. static void register_light_sleep();
  65. #endif
  66. static void register_factory_boot();
  67. static void register_restart_ota();
  68. static void register_set_services();
  69. #if WITH_TASKS_INFO
  70. static void register_tasks();
  71. #endif
  72. extern BaseType_t network_manager_task;
  73. FILE * system_open_memstream(const char * cmdname,char **buf,size_t *buf_size){
  74. FILE *f = open_memstream(buf, buf_size);
  75. if (f == NULL) {
  76. cmd_send_messaging(cmdname,MESSAGING_ERROR,"Unable to open memory stream.");
  77. }
  78. return f;
  79. }
  80. void register_system()
  81. {
  82. register_setdevicename();
  83. register_set_services();
  84. register_free();
  85. register_heap();
  86. register_dump_heap();
  87. register_abort();
  88. register_version();
  89. register_restart();
  90. register_factory_boot();
  91. register_restart_ota();
  92. #if WITH_TASKS_INFO
  93. register_tasks();
  94. #endif
  95. #if CONFIG_WITH_CONFIG_UI
  96. register_deep_sleep();
  97. register_light_sleep();
  98. #endif
  99. }
  100. void simple_restart()
  101. {
  102. log_send_messaging(MESSAGING_WARNING,"Rebooting.");
  103. if(!wait_for_commit()){
  104. log_send_messaging(MESSAGING_WARNING,"Unable to commit configuration. ");
  105. }
  106. vTaskDelay(750/ portTICK_PERIOD_MS);
  107. esp_restart();
  108. }
  109. /* 'version' command */
  110. static int get_version(int argc, char **argv)
  111. {
  112. esp_chip_info_t info;
  113. esp_chip_info(&info);
  114. cmd_send_messaging(argv[0],MESSAGING_INFO,
  115. "IDF Version:%s\r\n"
  116. "Chip info:\r\n"
  117. "\tmodel:%s\r\n"
  118. "\tcores:%d\r\n"
  119. "\tfeature:%s%s%s%s%d%s\r\n"
  120. "\trevision number:%d\r\n",
  121. esp_get_idf_version(), info.model == CHIP_ESP32 ? "ESP32" : "Unknow", info.cores,
  122. info.features & CHIP_FEATURE_WIFI_BGN ? "/802.11bgn" : "",
  123. info.features & CHIP_FEATURE_BLE ? "/BLE" : "",
  124. info.features & CHIP_FEATURE_BT ? "/BT" : "",
  125. info.features & CHIP_FEATURE_EMB_FLASH ? "/Embedded-Flash:" : "/External-Flash:",
  126. spi_flash_get_chip_size() / (1024 * 1024), " MB", info.revision);
  127. return 0;
  128. }
  129. static void register_version()
  130. {
  131. const esp_console_cmd_t cmd = {
  132. .command = "version",
  133. .help = "Get version of chip and SDK",
  134. .hint = NULL,
  135. .func = &get_version,
  136. };
  137. cmd_to_json(&cmd);
  138. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  139. }
  140. /* 'abort' command */
  141. static int cmd_abort(int argc, char **argv)
  142. {
  143. cmd_send_messaging(argv[0],MESSAGING_INFO,"ABORT!\r\n");
  144. abort();
  145. return 0;
  146. }
  147. static void register_abort()
  148. {
  149. const esp_console_cmd_t cmd = {
  150. .command = "abort",
  151. .help = "Crash now!",
  152. .hint = NULL,
  153. .func = &cmd_abort,
  154. };
  155. cmd_to_json(&cmd);
  156. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  157. }
  158. esp_err_t guided_boot(esp_partition_subtype_t partition_subtype)
  159. {
  160. if(is_recovery_running){
  161. if(partition_subtype ==ESP_PARTITION_SUBTYPE_APP_FACTORY){
  162. // log_send_messaging(MESSAGING_WARNING,"RECOVERY application is already active");
  163. simple_restart();
  164. }
  165. }
  166. else {
  167. if(partition_subtype !=ESP_PARTITION_SUBTYPE_APP_FACTORY){
  168. // log_send_messaging(MESSAGING_WARNING,"SQUEEZELITE application is already active");
  169. simple_restart();
  170. }
  171. }
  172. esp_err_t err = ESP_OK;
  173. // log_send_messaging(MESSAGING_INFO, "Looking for partition type %u",partition_subtype);
  174. const esp_partition_t *partition;
  175. esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_APP, partition_subtype, NULL);
  176. if(it == NULL){
  177. log_send_messaging(MESSAGING_ERROR,"Reboot failed. Partitions error");
  178. }
  179. else
  180. {
  181. ESP_LOGD(TAG, "Found partition. Getting info.");
  182. partition = (esp_partition_t *) esp_partition_get(it);
  183. ESP_LOGD(TAG, "Releasing partition iterator");
  184. esp_partition_iterator_release(it);
  185. if(partition != NULL){
  186. log_send_messaging(MESSAGING_INFO, "Rebooting to %s", partition->label);
  187. err=esp_ota_set_boot_partition(partition);
  188. if(err!=ESP_OK){
  189. log_send_messaging(MESSAGING_ERROR,"Unable to select partition for reboot: %s",esp_err_to_name(err));
  190. }
  191. }
  192. else
  193. {
  194. log_send_messaging(MESSAGING_ERROR,"partition type %u not found! Unable to reboot to recovery.",partition_subtype);
  195. }
  196. ESP_LOGD(TAG, "Yielding to other processes");
  197. taskYIELD();
  198. simple_restart();
  199. }
  200. return ESP_OK;
  201. }
  202. static int restart(int argc, char **argv)
  203. {
  204. simple_restart();
  205. return 0;
  206. }
  207. esp_err_t guided_restart_ota(){
  208. log_send_messaging(MESSAGING_WARNING,"Booting to Squeezelite");
  209. guided_boot(ESP_PARTITION_SUBTYPE_APP_OTA_0);
  210. return ESP_FAIL; // return fail. This should never return... we're rebooting!
  211. }
  212. esp_err_t guided_factory(){
  213. log_send_messaging(MESSAGING_WARNING,"Booting to recovery");
  214. guided_boot(ESP_PARTITION_SUBTYPE_APP_FACTORY);
  215. return ESP_FAIL; // return fail. This should never return... we're rebooting!
  216. }
  217. static int restart_factory(int argc, char **argv)
  218. {
  219. cmd_send_messaging(argv[0],MESSAGING_WARNING, "Booting to Recovery");
  220. guided_boot(ESP_PARTITION_SUBTYPE_APP_FACTORY);
  221. return 0; // return fail. This should never return... we're rebooting!
  222. }
  223. static int restart_ota(int argc, char **argv)
  224. {
  225. cmd_send_messaging(argv[0],MESSAGING_WARNING, "Booting to Squeezelite");
  226. guided_boot(ESP_PARTITION_SUBTYPE_APP_OTA_0);
  227. return 0; // return fail. This should never return... we're rebooting!
  228. }
  229. static void register_restart()
  230. {
  231. const esp_console_cmd_t cmd = {
  232. .command = "restart",
  233. .help = "Reboot system",
  234. .hint = NULL,
  235. .func = &restart,
  236. };
  237. #if CONFIG_WITH_CONFIG_UI
  238. cmd_to_json(&cmd);
  239. #endif
  240. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  241. }
  242. static void register_restart_ota()
  243. {
  244. const esp_console_cmd_t cmd = {
  245. .command = "restart_ota",
  246. .help = "Reboot system to Squeezelite",
  247. .hint = NULL,
  248. .func = &restart_ota,
  249. };
  250. #if CONFIG_WITH_CONFIG_UI
  251. cmd_to_json(&cmd);
  252. #endif
  253. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  254. }
  255. static void register_factory_boot()
  256. {
  257. const esp_console_cmd_t cmd = {
  258. .command = "recovery",
  259. .help = "Reboot system to Recovery",
  260. .hint = NULL,
  261. .func = &restart_factory,
  262. };
  263. #if CONFIG_WITH_CONFIG_UI
  264. cmd_to_json(&cmd);
  265. #endif
  266. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  267. }
  268. /** 'free' command prints available heap memory */
  269. static int free_mem(int argc, char **argv)
  270. {
  271. cmd_send_messaging(argv[0],MESSAGING_INFO,"%d", esp_get_free_heap_size());
  272. return 0;
  273. }
  274. static void register_free()
  275. {
  276. const esp_console_cmd_t cmd = {
  277. .command = "free",
  278. .help = "Get free heap memory",
  279. .hint = NULL,
  280. .func = &free_mem,
  281. };
  282. #if CONFIG_WITH_CONFIG_UI
  283. cmd_to_json(&cmd);
  284. #endif
  285. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  286. }
  287. static int dump_heap(int argc, char **argv)
  288. {
  289. ESP_LOGD(TAG, "Dumping heap");
  290. heap_caps_dump_all();
  291. return 0;
  292. }
  293. /* 'heap' command prints minumum heap size */
  294. static int heap_size(int argc, char **argv)
  295. {
  296. // ESP_LOGI(TAG,"Heap internal:%zu (min:%zu) (largest block:%zu)\nexternal:%zu (min:%zu) (largest block:%zd)\ndma :%zu (min:%zu) (largest block:%zd)",
  297. // heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  298. // heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  299. // heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL),
  300. // heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  301. // heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM),
  302. // heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM),
  303. // heap_caps_get_free_size(MALLOC_CAP_DMA),
  304. // heap_caps_get_minimum_free_size(MALLOC_CAP_DMA),
  305. // heap_caps_get_largest_free_block(MALLOC_CAP_DMA));
  306. cmd_send_messaging(argv[0],MESSAGING_INFO,"Heap internal:%zu (min:%zu) (largest block:%zu)\nexternal:%zu (min:%zu) (largest block:%zd)\ndma :%zu (min:%zu) (largest block:%zd)",
  307. heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
  308. heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL),
  309. heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL),
  310. heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
  311. heap_caps_get_minimum_free_size(MALLOC_CAP_SPIRAM),
  312. heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM),
  313. heap_caps_get_free_size(MALLOC_CAP_DMA),
  314. heap_caps_get_minimum_free_size(MALLOC_CAP_DMA),
  315. heap_caps_get_largest_free_block(MALLOC_CAP_DMA));
  316. return 0;
  317. }
  318. cJSON * setdevicename_cb(){
  319. char * default_host_name = config_alloc_get_str("host_name",NULL,"Squeezelite");
  320. cJSON * values = cJSON_CreateObject();
  321. cJSON_AddStringToObject(values,"name",default_host_name);
  322. free(default_host_name);
  323. return values;
  324. }
  325. static int setnamevar(char * nvsname, FILE *f, char * value){
  326. esp_err_t err=ESP_OK;
  327. if((err=config_set_value(NVS_TYPE_STR, nvsname, value))!=ESP_OK){
  328. fprintf(f,"Unable to set %s=%s. %s\n",nvsname,value,esp_err_to_name(err));
  329. }
  330. return err==ESP_OK?0:1;
  331. }
  332. typedef enum {
  333. SCANNING,
  334. PROCESSING_NAME
  335. } scanstate_t;
  336. int set_cspot_player_name(FILE * f,const char * name){
  337. int ret=0;
  338. cJSON * cspot_config = config_alloc_get_cjson("cspot_config");
  339. if(cspot_config==NULL){
  340. fprintf(f,"Unable to get cspot_config\n");
  341. return 1;
  342. }
  343. cJSON * player_name = cJSON_GetObjectItemCaseSensitive(cspot_config,"deviceName");
  344. if(player_name==NULL){
  345. fprintf(f,"Unable to get deviceName\n");
  346. ret=1;
  347. }
  348. if(strcmp(player_name->valuestring,name)==0){
  349. fprintf(f,"CSpot device name not changed.\n");
  350. ret=0;
  351. }
  352. else{
  353. cJSON_SetValuestring(player_name,name);
  354. if(setnamevar("cspot_config",f,cJSON_Print(cspot_config))!=0){
  355. fprintf(f,"Unable to set cspot_config\n");
  356. ret=1;
  357. }
  358. else{
  359. fprintf(f,"CSpot device name set to %s\n",name);
  360. }
  361. }
  362. cJSON_Delete(cspot_config);
  363. return ret;
  364. }
  365. int set_squeezelite_player_name(FILE * f,const char * name){
  366. char * nvs_config= config_alloc_get(NVS_TYPE_STR, "autoexec1");
  367. char **argv = NULL;
  368. esp_err_t err=ESP_OK;
  369. int nerrors=0;
  370. bool bFoundParm=false;
  371. scanstate_t state=SCANNING;
  372. char * newCommandLine = NULL;
  373. char * parm = " -n ";
  374. char * cleaned_name = strdup(name);
  375. for(char * p=cleaned_name;*p!='\0';p++){
  376. if(*p == ' '){
  377. *p='_'; // no spaces allowed
  378. }
  379. }
  380. if(nvs_config && strlen(nvs_config)>0){
  381. // allocate enough memory to hold the new command line
  382. size_t cmdLength = strlen(nvs_config) + strlen(cleaned_name) + strlen(parm) +1 ;
  383. newCommandLine = malloc_init_external(cmdLength);
  384. ESP_LOGD(TAG,"Parsing command %s",nvs_config);
  385. argv = (char **) malloc_init_external(22* sizeof(char *));
  386. if (argv == NULL) {
  387. FREE_AND_NULL(nvs_config);
  388. return 1;
  389. }
  390. size_t argc = esp_console_split_argv(nvs_config, argv,22);
  391. for(int i=0;i<argc;i++) {
  392. if(i>0){
  393. strcat(newCommandLine," ");
  394. }
  395. switch (state)
  396. {
  397. case SCANNING:
  398. strcat(newCommandLine,argv[i]);
  399. if(strcasecmp(argv[i],"--name")==0 || strcasecmp(argv[i],"-n")==0 ){
  400. state = PROCESSING_NAME;
  401. }
  402. break;
  403. case PROCESSING_NAME:
  404. bFoundParm=true;
  405. strcat(newCommandLine,cleaned_name);
  406. state = SCANNING;
  407. break;
  408. default:
  409. break;
  410. }
  411. }
  412. if(!bFoundParm){
  413. strcat(newCommandLine,parm);
  414. strcat(newCommandLine,name);
  415. }
  416. fprintf(f,"Squeezelite player name changed to %s\n",newCommandLine);
  417. if((err=config_set_value(NVS_TYPE_STR, "autoexec1",newCommandLine))!=ESP_OK){
  418. nerrors++;
  419. fprintf(f,"Failed updating squeezelite command. %s", esp_err_to_name(err));
  420. }
  421. }
  422. FREE_AND_NULL(nvs_config);
  423. FREE_AND_NULL(argv);
  424. free(cleaned_name);
  425. return nerrors;
  426. }
  427. static int setdevicename(int argc, char **argv)
  428. {
  429. char * name = NULL;
  430. int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&name_args);
  431. if (nerrors != 0) {
  432. return 1;
  433. }
  434. /* Check "--name" option */
  435. if (name_args.name->count) {
  436. name=strdup_psram(name_args.name->sval[0]);
  437. }
  438. else {
  439. cmd_send_messaging(argv[0],MESSAGING_ERROR,"Name must be specified.");
  440. return 1;
  441. }
  442. char *buf = NULL;
  443. size_t buf_size = 0;
  444. FILE *f = system_open_memstream(argv[0],&buf, &buf_size);
  445. if (f == NULL) {
  446. return 1;
  447. }
  448. nerrors+=setnamevar("a2dp_dev_name", f, name);
  449. nerrors+=setnamevar("airplay_name", f, name);
  450. nerrors+=setnamevar("ap_ssid", f, name);
  451. nerrors+=setnamevar("bt_name", f, name);
  452. nerrors+=setnamevar("host_name", f, name);
  453. nerrors+=set_squeezelite_player_name(f, name);
  454. nerrors+=set_cspot_player_name(f, name);
  455. if(nerrors==0){
  456. fprintf(f,"Device name changed to %s\n",name);
  457. }
  458. if(!nerrors ){
  459. fprintf(f,"Done.\n");
  460. }
  461. FREE_AND_NULL(name);
  462. fflush (f);
  463. cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  464. fclose(f);
  465. FREE_AND_NULL(buf);
  466. return nerrors;
  467. }
  468. static void register_heap()
  469. {
  470. const esp_console_cmd_t heap_cmd = {
  471. .command = "heap",
  472. .help = "Get minimum size of free heap memory",
  473. .hint = NULL,
  474. .func = &heap_size,
  475. };
  476. #if CONFIG_WITH_CONFIG_UI
  477. cmd_to_json(&heap_cmd);
  478. #endif
  479. ESP_ERROR_CHECK( esp_console_cmd_register(&heap_cmd) );
  480. }
  481. static void register_dump_heap()
  482. {
  483. const esp_console_cmd_t heap_cmd = {
  484. .command = "dump_heap",
  485. .help = "Dumps the content of the heap to serial output",
  486. .hint = NULL,
  487. .func = &dump_heap,
  488. };
  489. ESP_ERROR_CHECK( esp_console_cmd_register(&heap_cmd) );
  490. }
  491. static void register_setdevicename()
  492. {
  493. char * default_host_name = config_alloc_get_str("host_name",NULL,"Squeezelite");
  494. name_args.name = arg_str0("n", "name", default_host_name, "New Name");
  495. name_args.end = arg_end(8);
  496. const esp_console_cmd_t set_name= {
  497. .command = CFG_TYPE_SYST("name"),
  498. .help="Device Name",
  499. .hint = NULL,
  500. .func = &setdevicename,
  501. .argtable = &name_args
  502. };
  503. cmd_to_json_with_cb(&set_name,&setdevicename_cb);
  504. ESP_ERROR_CHECK(esp_console_cmd_register(&set_name));
  505. }
  506. /** 'tasks' command prints the list of tasks and related information */
  507. #if WITH_TASKS_INFO
  508. static int tasks_info(int argc, char **argv)
  509. {
  510. const size_t bytes_per_task = 40; /* see vTaskList description */
  511. char *task_list_buffer = malloc_init_external(uxTaskGetNumberOfTasks() * bytes_per_task);
  512. if (task_list_buffer == NULL) {
  513. cmd_send_messaging(argv[0],MESSAGING_ERROR, "failed to allocate buffer for vTaskList output");
  514. return 1;
  515. }
  516. cmd_send_messaging(argv[0],MESSAGING_INFO,"Task Name\tStatus\tPrio\tHWM\tTask#"
  517. #ifdef CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID
  518. "\tAffinity"
  519. #endif
  520. "\n");
  521. vTaskList(task_list_buffer);
  522. cmd_send_messaging(argv[0],MESSAGING_INFO,"%s", task_list_buffer);
  523. free(task_list_buffer);
  524. return 0;
  525. }
  526. static void register_tasks()
  527. {
  528. const esp_console_cmd_t cmd = {
  529. .command = "tasks",
  530. .help = "Get information about running tasks",
  531. .hint = NULL,
  532. .func = &tasks_info,
  533. };
  534. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  535. }
  536. #endif // WITH_TASKS_INFO
  537. /** 'deep_sleep' command puts the chip into deep sleep mode */
  538. #if CONFIG_WITH_CONFIG_UI
  539. static struct {
  540. struct arg_int *wakeup_time;
  541. struct arg_int *wakeup_gpio_num;
  542. struct arg_int *wakeup_gpio_level;
  543. struct arg_end *end;
  544. } deep_sleep_args;
  545. static int deep_sleep(int argc, char **argv)
  546. {
  547. int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&deep_sleep_args);
  548. if (nerrors != 0) {
  549. return 1;
  550. }
  551. if (deep_sleep_args.wakeup_time->count) {
  552. uint64_t timeout = 1000ULL * deep_sleep_args.wakeup_time->ival[0];
  553. cmd_send_messaging(argv[0],MESSAGING_INFO, "Enabling timer wakeup, timeout=%lluus", timeout);
  554. ESP_ERROR_CHECK( esp_sleep_enable_timer_wakeup(timeout) );
  555. }
  556. if (deep_sleep_args.wakeup_gpio_num->count) {
  557. int io_num = deep_sleep_args.wakeup_gpio_num->ival[0];
  558. if (!rtc_gpio_is_valid_gpio(io_num)) {
  559. cmd_send_messaging(argv[0],MESSAGING_ERROR, "GPIO %d is not an RTC IO", io_num);
  560. return 1;
  561. }
  562. int level = 0;
  563. if (deep_sleep_args.wakeup_gpio_level->count) {
  564. level = deep_sleep_args.wakeup_gpio_level->ival[0];
  565. if (level != 0 && level != 1) {
  566. cmd_send_messaging(argv[0],MESSAGING_ERROR, "Invalid wakeup level: %d", level);
  567. return 1;
  568. }
  569. }
  570. cmd_send_messaging(argv[0],MESSAGING_INFO, "Enabling wakeup on GPIO%d, wakeup on %s level",
  571. io_num, level ? "HIGH" : "LOW");
  572. ESP_ERROR_CHECK( esp_sleep_enable_ext1_wakeup(1ULL << io_num, level) );
  573. }
  574. rtc_gpio_isolate(GPIO_NUM_12);
  575. esp_deep_sleep_start();
  576. return 0; // this code will never run. deep sleep will cause the system to restart
  577. }
  578. static void register_deep_sleep()
  579. {
  580. deep_sleep_args.wakeup_time =
  581. arg_int0("t", "time", "<t>", "Wake up time, ms");
  582. deep_sleep_args.wakeup_gpio_num =
  583. arg_int0(NULL, "io", "<n>",
  584. "If specified, wakeup using GPIO with given number");
  585. deep_sleep_args.wakeup_gpio_level =
  586. arg_int0(NULL, "io_level", "<0|1>", "GPIO level to trigger wakeup");
  587. deep_sleep_args.end = arg_end(3);
  588. const esp_console_cmd_t cmd = {
  589. .command = "deep_sleep",
  590. .help = "Enter deep sleep mode. ",
  591. .hint = NULL,
  592. .func = &deep_sleep,
  593. .argtable = &deep_sleep_args
  594. };
  595. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  596. }
  597. #endif
  598. static int enable_disable(FILE * f,char * nvs_name, struct arg_lit *arg){
  599. esp_err_t err = config_set_value(NVS_TYPE_STR, nvs_name, arg->count>0?"Y":"N");
  600. const char * name = arg->hdr.longopts?arg->hdr.longopts:arg->hdr.glossary;
  601. if(err!=ESP_OK){
  602. fprintf(f,"Error %s %s. %s\n",arg->count>0?"Enabling":"Disabling", name, esp_err_to_name(err));
  603. }
  604. else {
  605. fprintf(f,"%s %s\n",arg->count>0?"Enabled":"Disabled",name);
  606. }
  607. return err;
  608. }
  609. static int do_set_services(int argc, char **argv)
  610. {
  611. esp_err_t err = ESP_OK;
  612. int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&set_services_args);
  613. if (nerrors != 0) {
  614. return 1;
  615. }
  616. char *buf = NULL;
  617. size_t buf_size = 0;
  618. FILE *f = system_open_memstream(argv[0],&buf, &buf_size);
  619. if (f == NULL) {
  620. return 1;
  621. }
  622. nerrors += enable_disable(f,"enable_airplay",set_services_args.airplay);
  623. nerrors += enable_disable(f,"enable_bt_sink",set_services_args.btspeaker);
  624. #if CONFIG_CSPOT_SINK
  625. nerrors += enable_disable(f,"enable_cspot",set_services_args.cspot);
  626. #endif
  627. if(set_services_args.telnet->count>0){
  628. if(strcasecmp(set_services_args.telnet->sval[0],"Disabled") == 0){
  629. err = config_set_value(NVS_TYPE_STR, "telnet_enable", "N");
  630. }
  631. else if(strcasecmp(set_services_args.telnet->sval[0],"Telnet Only") == 0){
  632. err = config_set_value(NVS_TYPE_STR, "telnet_enable", "Y");
  633. }
  634. else if(strcasecmp(set_services_args.telnet->sval[0],"Telnet and Serial") == 0){
  635. err = config_set_value(NVS_TYPE_STR, "telnet_enable", "D");
  636. }
  637. if(err!=ESP_OK){
  638. nerrors++;
  639. fprintf(f,"Error setting telnet to %s. %s\n",set_services_args.telnet->sval[0], esp_err_to_name(err));
  640. }
  641. else {
  642. fprintf(f,"Telnet service changed to %s\n",set_services_args.telnet->sval[0]);
  643. }
  644. }
  645. #if WITH_TASKS_INFO
  646. nerrors += enable_disable(f,"stats",set_services_args.stats);
  647. #endif
  648. if(!nerrors ){
  649. fprintf(f,"Done.\n");
  650. }
  651. fflush (f);
  652. cmd_send_messaging(argv[0],nerrors>0?MESSAGING_ERROR:MESSAGING_INFO,"%s", buf);
  653. fclose(f);
  654. FREE_AND_NULL(buf);
  655. return nerrors;
  656. }
  657. cJSON * set_services_cb(){
  658. cJSON * values = cJSON_CreateObject();
  659. char * p=NULL;
  660. console_set_bool_parameter(values,"enable_bt_sink",set_services_args.btspeaker);
  661. console_set_bool_parameter(values,"enable_airplay",set_services_args.airplay);
  662. #if CONFIG_CSPOT_SINK
  663. console_set_bool_parameter(values,"enable_cspot",set_services_args.cspot);
  664. #endif
  665. #if WITH_TASKS_INFO
  666. console_set_bool_parameter(values,"stats",set_services_args.stats);
  667. #endif
  668. if ((p = config_alloc_get(NVS_TYPE_STR, "telnet_enable")) != NULL) {
  669. if(strcasestr("YX",p)!=NULL){
  670. cJSON_AddStringToObject(values,set_services_args.telnet->hdr.longopts,"Telnet Only");
  671. }
  672. else if(strcasestr("D",p)!=NULL){
  673. cJSON_AddStringToObject(values,set_services_args.telnet->hdr.longopts,"Telnet and Serial");
  674. }
  675. else {
  676. cJSON_AddStringToObject(values,set_services_args.telnet->hdr.longopts,"Disabled");
  677. }
  678. FREE_AND_NULL(p);
  679. }
  680. return values;
  681. }
  682. static void register_set_services(){
  683. set_services_args.airplay = arg_lit0(NULL, "AirPlay", "AirPlay");
  684. #if CONFIG_CSPOT_SINK
  685. set_services_args.cspot = arg_lit0(NULL, "cspot", "Spotify (cspot)");
  686. #endif
  687. set_services_args.btspeaker = arg_lit0(NULL, "BT_Speaker", "Bluetooth Speaker");
  688. set_services_args.telnet= arg_str0("t", "telnet","Disabled|Telnet Only|Telnet and Serial","Telnet server. Use only for troubleshooting");
  689. #if WITH_TASKS_INFO
  690. set_services_args.stats= arg_lit0(NULL, "stats", "System Statistics. Use only for troubleshooting");
  691. #endif
  692. set_services_args.end=arg_end(2);
  693. const esp_console_cmd_t cmd = {
  694. .command = CFG_TYPE_SYST("services"),
  695. .help = "Services",
  696. .argtable = &set_services_args,
  697. .hint = NULL,
  698. .func = &do_set_services,
  699. };
  700. cmd_to_json_with_cb(&cmd,&set_services_cb);
  701. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  702. }
  703. #if CONFIG_WITH_CONFIG_UI
  704. static struct {
  705. struct arg_int *wakeup_time;
  706. struct arg_int *wakeup_gpio_num;
  707. struct arg_int *wakeup_gpio_level;
  708. struct arg_end *end;
  709. } light_sleep_args;
  710. static int light_sleep(int argc, char **argv)
  711. {
  712. int nerrors = arg_parse_msg(argc, argv,(struct arg_hdr **)&light_sleep_args);
  713. if (nerrors != 0) {
  714. return 1;
  715. }
  716. esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
  717. if (light_sleep_args.wakeup_time->count) {
  718. uint64_t timeout = 1000ULL * light_sleep_args.wakeup_time->ival[0];
  719. cmd_send_messaging(argv[0],MESSAGING_INFO, "Enabling timer wakeup, timeout=%lluus", timeout);
  720. ESP_ERROR_CHECK( esp_sleep_enable_timer_wakeup(timeout) );
  721. }
  722. int io_count = light_sleep_args.wakeup_gpio_num->count;
  723. if (io_count != light_sleep_args.wakeup_gpio_level->count) {
  724. cmd_send_messaging(argv[0],MESSAGING_INFO, "Should have same number of 'io' and 'io_level' arguments");
  725. return 1;
  726. }
  727. for (int i = 0; i < io_count; ++i) {
  728. int io_num = light_sleep_args.wakeup_gpio_num->ival[i];
  729. int level = light_sleep_args.wakeup_gpio_level->ival[i];
  730. if (level != 0 && level != 1) {
  731. cmd_send_messaging(argv[0],MESSAGING_ERROR, "Invalid wakeup level: %d", level);
  732. return 1;
  733. }
  734. cmd_send_messaging(argv[0],MESSAGING_INFO, "Enabling wakeup on GPIO%d, wakeup on %s level",
  735. io_num, level ? "HIGH" : "LOW");
  736. ESP_ERROR_CHECK( gpio_wakeup_enable(io_num, level ? GPIO_INTR_HIGH_LEVEL : GPIO_INTR_LOW_LEVEL) );
  737. }
  738. if (io_count > 0) {
  739. ESP_ERROR_CHECK( esp_sleep_enable_gpio_wakeup() );
  740. }
  741. if (CONFIG_ESP_CONSOLE_UART_NUM <= UART_NUM_1) {
  742. cmd_send_messaging(argv[0],MESSAGING_INFO, "Enabling UART wakeup (press ENTER to exit light sleep)");
  743. ESP_ERROR_CHECK( uart_set_wakeup_threshold(CONFIG_ESP_CONSOLE_UART_NUM, 3) );
  744. ESP_ERROR_CHECK( esp_sleep_enable_uart_wakeup(CONFIG_ESP_CONSOLE_UART_NUM) );
  745. }
  746. fflush(stdout);
  747. esp_rom_uart_tx_wait_idle(CONFIG_ESP_CONSOLE_UART_NUM);
  748. esp_light_sleep_start();
  749. esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
  750. const char *cause_str;
  751. switch (cause) {
  752. case ESP_SLEEP_WAKEUP_GPIO:
  753. cause_str = "GPIO";
  754. break;
  755. case ESP_SLEEP_WAKEUP_UART:
  756. cause_str = "UART";
  757. break;
  758. case ESP_SLEEP_WAKEUP_TIMER:
  759. cause_str = "timer";
  760. break;
  761. default:
  762. cause_str = "unknown";
  763. printf("%d\n", cause);
  764. }
  765. cmd_send_messaging(argv[0],MESSAGING_INFO, "Woke up from: %s", cause_str);
  766. return 0;
  767. }
  768. static void register_light_sleep()
  769. {
  770. light_sleep_args.wakeup_time =
  771. arg_int0("t", "time", "<t>", "Wake up time, ms");
  772. light_sleep_args.wakeup_gpio_num =
  773. arg_intn(NULL, "io", "<n>", 0, 8,
  774. "If specified, wakeup using GPIO with given number");
  775. light_sleep_args.wakeup_gpio_level =
  776. arg_intn(NULL, "io_level", "<0|1>", 0, 8, "GPIO level to trigger wakeup");
  777. light_sleep_args.end = arg_end(3);
  778. const esp_console_cmd_t cmd = {
  779. .command = "light_sleep",
  780. .help = "Enter light sleep mode. "
  781. "Two wakeup modes are supported: timer and GPIO. "
  782. "Multiple GPIO pins can be specified using pairs of "
  783. "'io' and 'io_level' arguments. "
  784. "Will also wake up on UART input.",
  785. .hint = NULL,
  786. .func = &light_sleep,
  787. .argtable = &light_sleep_args
  788. };
  789. ESP_ERROR_CHECK( esp_console_cmd_register(&cmd) );
  790. }
  791. #endif