cmd_system.c 26 KB

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