cmd_system.c 27 KB

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