2
0

cmd_system.c 24 KB

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