cmd_system.c 27 KB

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