esp_app_main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2019
  5. * Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This software is released under the MIT License.
  8. * https://opensource.org/licenses/MIT
  9. *
  10. */
  11. #include "platform_esp32.h"
  12. #include "led.h"
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "freertos/FreeRTOS.h"
  16. #include "driver/gpio.h"
  17. #include "driver/spi_master.h"
  18. #include "freertos/task.h"
  19. #include "esp_system.h"
  20. #include "esp_spi_flash.h"
  21. #include "esp_wifi.h"
  22. #include <esp_event.h>
  23. #include "nvs_flash.h"
  24. #include "esp_log.h"
  25. #include "freertos/event_groups.h"
  26. #include "mdns.h"
  27. #include "lwip/api.h"
  28. #include "lwip/err.h"
  29. #include "lwip/netdb.h"
  30. #include "nvs_utilities.h"
  31. #include "trace.h"
  32. #include "network_manager.h"
  33. #include "squeezelite-ota.h"
  34. #include <math.h>
  35. #include "audio_controls.h"
  36. #include "platform_config.h"
  37. #include "telnet.h"
  38. #include "messaging.h"
  39. #include "gds.h"
  40. #include "gds_default_if.h"
  41. #include "gds_draw.h"
  42. #include "gds_text.h"
  43. #include "gds_font.h"
  44. #include "display.h"
  45. #include "accessors.h"
  46. #include "cmd_system.h"
  47. #include "tools.h"
  48. static const char certs_namespace[] = "certificates";
  49. static const char certs_key[] = "blob";
  50. static const char certs_version[] = "version";
  51. const char unknown_string_placeholder[] = "unknown";
  52. const char null_string_placeholder[] = "null";
  53. EventGroupHandle_t network_event_group;
  54. bool bypass_network_manager=false;
  55. const int CONNECTED_BIT = BIT0;
  56. #define JOIN_TIMEOUT_MS (10000)
  57. #define LOCAL_MAC_SIZE 20
  58. static const char TAG[] = "esp_app_main";
  59. #define DEFAULT_HOST_NAME "squeezelite"
  60. char * fwurl = NULL;
  61. RTC_NOINIT_ATTR uint32_t RebootCounter ;
  62. RTC_NOINIT_ATTR uint32_t RecoveryRebootCounter ;
  63. RTC_NOINIT_ATTR uint16_t ColdBootIndicatorFlag;
  64. bool cold_boot=true;
  65. static bool bNetworkConnected=false;
  66. extern const uint8_t server_cert_pem_start[] asm("_binary_github_pem_start");
  67. extern const uint8_t server_cert_pem_end[] asm("_binary_github_pem_end");
  68. // as an exception _init function don't need include
  69. extern void services_init(void);
  70. extern void display_init(char *welcome);
  71. extern void target_init(char *target);
  72. const char * str_or_unknown(const char * str) { return (str?str:unknown_string_placeholder); }
  73. const char * str_or_null(const char * str) { return (str?str:null_string_placeholder); }
  74. bool is_recovery_running;
  75. void cb_connection_got_ip(nm_state_t new_state, int sub_state){
  76. const char *hostname;
  77. static ip4_addr_t ip;
  78. tcpip_adapter_ip_info_t ipInfo;
  79. network_get_ip_info(&ipInfo);
  80. if (ip.addr && ipInfo.ip.addr != ip.addr) {
  81. ESP_LOGW(TAG, "IP change, need to reboot");
  82. if(!wait_for_commit()){
  83. ESP_LOGW(TAG,"Unable to commit configuration. ");
  84. }
  85. esp_restart();
  86. }
  87. // initializing mDNS
  88. network_get_hostname(&hostname);
  89. mdns_init();
  90. mdns_hostname_set(hostname);
  91. ESP_LOGI(TAG, "Network connected and mDNS initialized with %s", hostname);
  92. messaging_post_message(MESSAGING_INFO,MESSAGING_CLASS_SYSTEM,"Network connected");
  93. xEventGroupSetBits(network_event_group, CONNECTED_BIT);
  94. bNetworkConnected=true;
  95. led_unpush(LED_GREEN);
  96. if(is_recovery_running){
  97. // when running in recovery, send a LMS discovery message
  98. // to find a running instance. This is to enable using
  99. // the plugin's proxy mode for FW download and avoid
  100. // expired certificate issues.
  101. discover_ota_server(5);
  102. }
  103. }
  104. void cb_connection_sta_disconnected(nm_state_t new_state, int sub_state){
  105. led_blink_pushed(LED_GREEN, 250, 250);
  106. messaging_post_message(MESSAGING_WARNING,MESSAGING_CLASS_SYSTEM,"Wifi disconnected");
  107. bNetworkConnected=false;
  108. xEventGroupClearBits(network_event_group, CONNECTED_BIT);
  109. }
  110. bool wait_for_wifi(){
  111. bool connected=(xEventGroupGetBits(network_event_group) & CONNECTED_BIT)!=0;
  112. if(!connected){
  113. ESP_LOGD(TAG,"Waiting for Network...");
  114. connected = (xEventGroupWaitBits(network_event_group, CONNECTED_BIT,
  115. pdFALSE, pdTRUE, JOIN_TIMEOUT_MS / portTICK_PERIOD_MS)& CONNECTED_BIT)!=0;
  116. if(!connected){
  117. ESP_LOGW(TAG,"Network timeout.");
  118. }
  119. else
  120. {
  121. ESP_LOGI(TAG,"Network Connected!");
  122. }
  123. }
  124. return connected;
  125. }
  126. char * process_ota_url(){
  127. ESP_LOGI(TAG,"Checking for update url");
  128. char * fwurl=config_alloc_get(NVS_TYPE_STR, "fwurl");
  129. if(fwurl!=NULL)
  130. {
  131. ESP_LOGD(TAG,"Deleting nvs entry for Firmware URL %s", fwurl);
  132. config_delete_key("fwurl");
  133. }
  134. return fwurl;
  135. }
  136. esp_log_level_t get_log_level_from_char(char * level){
  137. if(!strcasecmp(level, "NONE" )) { return ESP_LOG_NONE ;}
  138. if(!strcasecmp(level, "ERROR" )) { return ESP_LOG_ERROR ;}
  139. if(!strcasecmp(level, "WARN" )) { return ESP_LOG_WARN ;}
  140. if(!strcasecmp(level, "INFO" )) { return ESP_LOG_INFO ;}
  141. if(!strcasecmp(level, "DEBUG" )) { return ESP_LOG_DEBUG ;}
  142. if(!strcasecmp(level, "VERBOSE" )) { return ESP_LOG_VERBOSE;}
  143. return ESP_LOG_WARN;
  144. }
  145. void set_log_level(char * tag, char * level){
  146. esp_log_level_set(tag, get_log_level_from_char(level));
  147. }
  148. esp_err_t update_certificates(bool force){
  149. nvs_handle handle;
  150. esp_err_t esp_err;
  151. esp_app_desc_t running_app_info;
  152. ESP_LOGI(TAG, "About to check if certificates need to be updated in flash");
  153. esp_err = nvs_open_from_partition(settings_partition, certs_namespace, NVS_READWRITE, &handle);
  154. if (esp_err != ESP_OK) {
  155. LOG_SEND(MESSAGING_INFO,"Unable to update HTTPS certificates. Could not open NVS namespace %s. Error %s", certs_namespace, esp_err_to_name(esp_err));
  156. return esp_err;
  157. }
  158. const esp_partition_t *running = esp_ota_get_running_partition();
  159. ESP_LOGI(TAG, "Running partition [%s] type %d subtype %d (offset 0x%08x)", running->label, running->type, running->subtype, running->address);
  160. if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) {
  161. ESP_LOGI(TAG, "Running version: %s", running_app_info.version);
  162. }
  163. size_t len=0;
  164. char *str=NULL;
  165. bool changed=false;
  166. if ( (esp_err= nvs_get_str(handle, certs_version, NULL, &len)) == ESP_OK) {
  167. str=(char *)malloc_init_external(len+1);
  168. if(str){
  169. if ( (esp_err = nvs_get_str(handle, certs_version, str, &len)) == ESP_OK) {
  170. ESP_LOGI(TAG,"Certificate version: %s", str);
  171. }
  172. }
  173. }
  174. if(str!=NULL && running->subtype !=ESP_PARTITION_SUBTYPE_APP_FACTORY){
  175. // If certificates were found in nvs, only update if we're not
  176. // running recovery. This will prevent rolling back to an older version
  177. if(strcmp((char *)running_app_info.version,(char *)str )){
  178. // Versions are different
  179. ESP_LOGW(TAG,"Found a different software version. Updating certificates");
  180. changed=true;
  181. }
  182. free(str);
  183. }
  184. else if(str==NULL){
  185. ESP_LOGW(TAG,"No certificate found. Adding certificates");
  186. changed=true;
  187. }
  188. if(changed || force){
  189. esp_err = nvs_set_blob(handle, certs_key, server_cert_pem_start, (server_cert_pem_end-server_cert_pem_start));
  190. if(esp_err!=ESP_OK){
  191. log_send_messaging(MESSAGING_ERROR,"Failed to store certificate data: %s", esp_err_to_name(esp_err));
  192. }
  193. else {
  194. esp_err = nvs_set_str(handle, certs_version, running_app_info.version);
  195. if(esp_err!=ESP_OK){
  196. log_send_messaging(MESSAGING_ERROR,"Unable to update HTTPS Certificates version: %s",esp_err_to_name(esp_err));
  197. }
  198. else {
  199. esp_err = nvs_commit(handle);
  200. if(esp_err!=ESP_OK){
  201. log_send_messaging(MESSAGING_ERROR,"Failed to commit certificates changes : %s",esp_err_to_name(esp_err));
  202. }
  203. else {
  204. log_send_messaging(MESSAGING_INFO,"HTTPS Certificates were updated with version: %s",running_app_info.version);
  205. }
  206. }
  207. }
  208. }
  209. nvs_close(handle);
  210. return ESP_OK;
  211. }
  212. const char * get_certificate(){
  213. nvs_handle handle;
  214. esp_err_t esp_err;
  215. char *blob =NULL;
  216. //
  217. ESP_LOGD(TAG, "Fetching certificate.");
  218. esp_err = nvs_open_from_partition(settings_partition, certs_namespace, NVS_READONLY, &handle);
  219. if(esp_err == ESP_OK){
  220. size_t len;
  221. esp_err = nvs_get_blob(handle, certs_key, NULL, &len);
  222. if( esp_err == ESP_OK) {
  223. blob = (char *) malloc_init_external(len+1);
  224. if(!blob){
  225. log_send_messaging(MESSAGING_ERROR,"Unable to retrieve HTTPS certificates. %s","Memory allocation failed");
  226. return "";
  227. }
  228. memset(blob,0x00,len+1);
  229. esp_err = nvs_get_blob(handle, certs_key, blob, &len);
  230. if ( esp_err == ESP_OK) {
  231. ESP_LOGD(TAG,"Certificates content is %d bytes long: ", len);
  232. }
  233. else {
  234. log_send_messaging(MESSAGING_ERROR,"Unable to retrieve HTTPS certificates. Get blob failed: %s", esp_err_to_name(esp_err));
  235. }
  236. }
  237. else{
  238. log_send_messaging(MESSAGING_ERROR,"Unable to retrieve HTTPS certificates. Get blob failed: %s",esp_err_to_name(esp_err));
  239. }
  240. nvs_close(handle);
  241. }
  242. else{
  243. log_send_messaging(MESSAGING_ERROR,"Unable to retrieve HTTPS certificates. NVS name space %s open failed: %s",certs_namespace, esp_err_to_name(esp_err));
  244. }
  245. return blob;
  246. }
  247. #define DEFAULT_NAME_WITH_MAC(var,defval) char var[strlen(defval)+sizeof(macStr)]; strcpy(var,defval); strcat(var,macStr)
  248. void register_default_string_val(const char * key, char * value){
  249. char * existing =(char *)config_alloc_get(NVS_TYPE_STR,key );
  250. ESP_LOGD(TAG,"Register default called with: %s= %s",key,value );
  251. if(!existing) {
  252. ESP_LOGI(TAG,"Registering default value for key %s, value %s",key,value );
  253. config_set_default(NVS_TYPE_STR, key,value, 0);
  254. }
  255. else {
  256. ESP_LOGD(TAG,"Value found for %s: %s",key,existing );
  257. }
  258. FREE_AND_NULL(existing);
  259. }
  260. char * alloc_get_string_with_mac(const char * val) {
  261. uint8_t mac[6];
  262. char macStr[LOCAL_MAC_SIZE + 1];
  263. char* fullvalue = NULL;
  264. esp_read_mac((uint8_t*)&mac, ESP_MAC_WIFI_STA);
  265. snprintf(macStr, LOCAL_MAC_SIZE - 1, "-%x%x%x", mac[3], mac[4], mac[5]);
  266. fullvalue = malloc_init_external(strlen(val)+sizeof(macStr)+1);
  267. if(fullvalue){
  268. strcpy(fullvalue, val);
  269. strcat(fullvalue, macStr);
  270. }
  271. else {
  272. ESP_LOGE(TAG,"Memory allocation failed when getting mac value for %s", val);
  273. }
  274. return fullvalue;
  275. }
  276. void register_default_with_mac(const char* key, char* defval) {
  277. char * fullvalue=alloc_get_string_with_mac(defval);
  278. if(fullvalue){
  279. register_default_string_val(key,fullvalue);
  280. FREE_AND_NULL(fullvalue);
  281. }
  282. else {
  283. ESP_LOGE(TAG,"Memory allocation failed when registering default value for %s", key);
  284. }
  285. }
  286. void register_default_nvs(){
  287. #ifdef CONFIG_CSPOT_SINK
  288. register_default_string_val("enable_cspot", STR(CONFIG_CSPOT_SINK));
  289. cJSON * cspot_config=config_alloc_get_cjson("cspot_config");
  290. if(!cspot_config){
  291. char * name = alloc_get_string_with_mac(DEFAULT_HOST_NAME);
  292. if(name){
  293. cjson_update_string(&cspot_config,"deviceName",name);
  294. cjson_update_number(&cspot_config,"bitrate",160);
  295. // the call below saves the config and frees the json pointer
  296. config_set_cjson_str_and_free("cspot_config",cspot_config);
  297. FREE_AND_NULL(name);
  298. }
  299. else {
  300. register_default_string_val("cspot_config", "");
  301. }
  302. }
  303. #endif
  304. #ifdef CONFIG_AIRPLAY_SINK
  305. register_default_with_mac("airplay_name", CONFIG_AIRPLAY_NAME);
  306. register_default_string_val("airplay_port", CONFIG_AIRPLAY_PORT);
  307. register_default_string_val( "enable_airplay", STR(CONFIG_AIRPLAY_SINK));
  308. #endif
  309. #ifdef CONFIG_BT_SINK
  310. register_default_string_val( "bt_sink_pin", STR(CONFIG_BT_SINK_PIN));
  311. register_default_string_val( "bt_sink_volume", "127");
  312. register_default_with_mac("bt_name", CONFIG_BT_NAME);
  313. register_default_string_val( "enable_bt_sink", STR(CONFIG_BT_SINK));
  314. register_default_string_val("a2dp_dev_name", CONFIG_A2DP_DEV_NAME);
  315. register_default_string_val("a2dp_ctmt", STR(CONFIG_A2DP_CONNECT_TIMEOUT_MS));
  316. register_default_string_val("a2dp_ctrld", STR(CONFIG_A2DP_CONTROL_DELAY_MS));
  317. register_default_string_val("a2dp_sink_name", CONFIG_A2DP_SINK_NAME);
  318. #endif
  319. register_default_with_mac("host_name", DEFAULT_HOST_NAME);
  320. register_default_with_mac("ap_ssid", CONFIG_DEFAULT_AP_SSID);
  321. register_default_string_val("autoexec","1");
  322. register_default_with_mac("autoexec1",CONFIG_DEFAULT_COMMAND_LINE " -n " DEFAULT_HOST_NAME);
  323. register_default_string_val("release_url", CONFIG_SQUEEZELITE_ESP32_RELEASE_URL);
  324. register_default_string_val("ap_ip_address",CONFIG_DEFAULT_AP_IP);
  325. register_default_string_val("ap_ip_gateway",CONFIG_DEFAULT_AP_GATEWAY );
  326. register_default_string_val("ap_ip_netmask",CONFIG_DEFAULT_AP_NETMASK);
  327. register_default_string_val("ap_channel",STR(CONFIG_DEFAULT_AP_CHANNEL));
  328. register_default_string_val("ap_pwd", CONFIG_DEFAULT_AP_PASSWORD);
  329. register_default_string_val("bypass_wm", "0");
  330. register_default_string_val("equalizer", "");
  331. register_default_string_val("actrls_config", "");
  332. register_default_string_val("lms_ctrls_raw", "n");
  333. register_default_string_val("rotary_config", CONFIG_ROTARY_ENCODER);
  334. char number_buffer[101] = {};
  335. snprintf(number_buffer,sizeof(number_buffer)-1,"%u",OTA_FLASH_ERASE_BLOCK);
  336. register_default_string_val( "ota_erase_blk", number_buffer);
  337. snprintf(number_buffer,sizeof(number_buffer)-1,"%u",OTA_STACK_SIZE);
  338. register_default_string_val( "ota_stack", number_buffer);
  339. snprintf(number_buffer,sizeof(number_buffer)-1,"%d",OTA_TASK_PRIOTITY);
  340. register_default_string_val( "ota_prio", number_buffer);
  341. register_default_string_val( "display_config", CONFIG_DISPLAY_CONFIG);
  342. register_default_string_val( "eth_config", CONFIG_ETH_CONFIG);
  343. register_default_string_val( "i2c_config", CONFIG_I2C_CONFIG);
  344. register_default_string_val( "spi_config", CONFIG_SPI_CONFIG);
  345. register_default_string_val( "set_GPIO", CONFIG_SET_GPIO);
  346. register_default_string_val( "led_brightness", "");
  347. register_default_string_val( "spdif_config", "");
  348. register_default_string_val( "dac_config", "");
  349. register_default_string_val( "dac_controlset", "");
  350. register_default_string_val( "jack_mutes_amp", "n");
  351. register_default_string_val("gpio_exp_config", CONFIG_GPIO_EXP_CONFIG);
  352. register_default_string_val( "bat_config", "");
  353. register_default_string_val( "metadata_config", "");
  354. register_default_string_val( "telnet_enable", "");
  355. register_default_string_val( "telnet_buffer", "40000");
  356. register_default_string_val( "telnet_block", "500");
  357. register_default_string_val( "stats", "n");
  358. register_default_string_val( "rel_api", CONFIG_RELEASE_API);
  359. register_default_string_val("pollmx","600");
  360. register_default_string_val("pollmin","15");
  361. register_default_string_val("ethtmout","8");
  362. register_default_string_val("dhcp_tmout","8");
  363. register_default_string_val("target", CONFIG_TARGET);
  364. wait_for_commit();
  365. ESP_LOGD(TAG,"Done setting default values in nvs.");
  366. }
  367. uint32_t halSTORAGE_RebootCounterRead(void) { return RebootCounter ; }
  368. uint32_t halSTORAGE_RebootCounterUpdate(int32_t xValue) {
  369. if(RebootCounter >100) {
  370. RebootCounter = 0;
  371. RecoveryRebootCounter = 0;
  372. }
  373. RebootCounter = (xValue != 0) ? (RebootCounter + xValue) : 0;
  374. RecoveryRebootCounter = (xValue != 0) && is_recovery_running ? (RecoveryRebootCounter + xValue) : 0;
  375. return (RebootCounter) ;
  376. }
  377. void handle_ap_connect(nm_state_t new_state, int sub_state){
  378. start_telnet(NULL);
  379. }
  380. void handle_network_up(nm_state_t new_state, int sub_state){
  381. halSTORAGE_RebootCounterUpdate(0);
  382. }
  383. esp_reset_reason_t xReason=ESP_RST_UNKNOWN;
  384. void app_main()
  385. {
  386. if(ColdBootIndicatorFlag != 0xFACE ){
  387. ESP_LOGI(TAG, "System is booting from power on.");
  388. cold_boot = true;
  389. ColdBootIndicatorFlag = 0xFACE;
  390. }
  391. else {
  392. cold_boot = false;
  393. }
  394. const esp_partition_t *running = esp_ota_get_running_partition();
  395. is_recovery_running = (running->subtype == ESP_PARTITION_SUBTYPE_APP_FACTORY);
  396. xReason = esp_reset_reason();
  397. ESP_LOGI(TAG,"Reset reason is: %u", xReason);
  398. if(!is_recovery_running ) {
  399. /* unscheduled restart (HW, Watchdog or similar) thus increment dynamic
  400. * counter then log current boot statistics as a warning */
  401. uint32_t Counter = halSTORAGE_RebootCounterUpdate(1) ; // increment counter
  402. ESP_LOGI(TAG,"Reboot counter=%u\n", Counter) ;
  403. if (Counter == 5) {
  404. guided_factory();
  405. }
  406. }
  407. else {
  408. uint32_t Counter = halSTORAGE_RebootCounterUpdate(1) ; // increment counter
  409. if(RecoveryRebootCounter==1 && Counter>=5){
  410. // First time we are rebooting in recovery after crashing
  411. messaging_post_message(MESSAGING_ERROR,MESSAGING_CLASS_SYSTEM,"System was forced into recovery mode after crash likely caused by some bad configuration\n");
  412. }
  413. ESP_LOGI(TAG,"Recovery Reboot counter=%u\n", Counter) ;
  414. if (RecoveryRebootCounter == 5) {
  415. ESP_LOGW(TAG,"System rebooted too many times. This could be an indication that configuration is corrupted. Erasing config.");
  416. erase_settings_partition();
  417. // reboot one more time
  418. guided_factory();
  419. }
  420. if (RecoveryRebootCounter >5){
  421. messaging_post_message(MESSAGING_ERROR,MESSAGING_CLASS_SYSTEM,"System was forced into recovery mode after crash likely caused by some bad configuration. Configuration was reset to factory.\n");
  422. }
  423. }
  424. char * fwurl = NULL;
  425. MEMTRACE_PRINT_DELTA();
  426. ESP_LOGI(TAG,"Starting app_main");
  427. initialize_nvs();
  428. MEMTRACE_PRINT_DELTA();
  429. ESP_LOGI(TAG,"Setting up telnet.");
  430. init_telnet(); // align on 32 bits boundaries
  431. MEMTRACE_PRINT_DELTA();
  432. ESP_LOGI(TAG,"Setting up config subsystem.");
  433. config_init();
  434. MEMTRACE_PRINT_DELTA();
  435. ESP_LOGD(TAG,"Creating event group for wifi");
  436. network_event_group = xEventGroupCreate();
  437. ESP_LOGD(TAG,"Clearing CONNECTED_BIT from wifi group");
  438. xEventGroupClearBits(network_event_group, CONNECTED_BIT);
  439. ESP_LOGI(TAG,"Registering default values");
  440. register_default_nvs();
  441. MEMTRACE_PRINT_DELTA();
  442. ESP_LOGI(TAG,"Configuring services");
  443. services_init();
  444. MEMTRACE_PRINT_DELTA();
  445. ESP_LOGI(TAG,"Initializing display");
  446. display_init("SqueezeESP32");
  447. MEMTRACE_PRINT_DELTA();
  448. char *target = config_alloc_get_str("target", CONFIG_TARGET, NULL);
  449. if (target) {
  450. target_init(target);
  451. free(target);
  452. }
  453. if(is_recovery_running && display){
  454. GDS_ClearExt(display, true);
  455. GDS_SetFont(display, &Font_line_2 );
  456. GDS_TextPos(display, GDS_FONT_DEFAULT, GDS_TEXT_CENTERED, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, "RECOVERY");
  457. }
  458. ESP_LOGI(TAG,"Checking if certificates need to be updated");
  459. update_certificates(false);
  460. MEMTRACE_PRINT_DELTA();
  461. ESP_LOGD(TAG,"Getting firmware OTA URL (if any)");
  462. fwurl = process_ota_url();
  463. ESP_LOGD(TAG,"Getting value for WM bypass, nvs 'bypass_wm'");
  464. char * bypass_wm = config_alloc_get_default(NVS_TYPE_STR, "bypass_wm", "0", 0);
  465. if(bypass_wm==NULL)
  466. {
  467. ESP_LOGE(TAG, "Unable to retrieve the Wifi Manager bypass flag");
  468. bypass_network_manager = false;
  469. }
  470. else {
  471. bypass_network_manager=(strcmp(bypass_wm,"1")==0 ||strcasecmp(bypass_wm,"y")==0);
  472. }
  473. if(!is_recovery_running){
  474. ESP_LOGD(TAG,"Getting audio control mapping ");
  475. char *actrls_config = config_alloc_get_default(NVS_TYPE_STR, "actrls_config", "", 0);
  476. if (actrls_init(actrls_config) == ESP_OK) {
  477. ESP_LOGD(TAG,"Initializing audio control buttons type %s", actrls_config);
  478. } else {
  479. ESP_LOGD(TAG,"No audio control buttons");
  480. }
  481. if (actrls_config) free(actrls_config);
  482. }
  483. /* start the wifi manager */
  484. ESP_LOGD(TAG,"Blinking led");
  485. led_blink_pushed(LED_GREEN, 250, 250);
  486. MEMTRACE_PRINT_DELTA();
  487. if(bypass_network_manager){
  488. ESP_LOGW(TAG,"Network manager is disabled. Use command line for wifi control.");
  489. }
  490. else {
  491. ESP_LOGI(TAG,"Starting Network Manager");
  492. network_start();
  493. MEMTRACE_PRINT_DELTA();
  494. network_register_state_callback(NETWORK_WIFI_ACTIVE_STATE,WIFI_CONNECTED_STATE, "cb_connection_got_ip", &cb_connection_got_ip);
  495. network_register_state_callback(NETWORK_ETH_ACTIVE_STATE,ETH_ACTIVE_CONNECTED_STATE, "cb_connection_got_ip",&cb_connection_got_ip);
  496. network_register_state_callback(NETWORK_WIFI_ACTIVE_STATE,WIFI_LOST_CONNECTION_STATE, "cb_connection_sta_disconnected",&cb_connection_sta_disconnected);
  497. /* Start the telnet service after we are certain that the network stack has been properly initialized.
  498. * This can be either after we're started the AP mode, or after we've started the STA mode */
  499. network_register_state_callback(NETWORK_INITIALIZING_STATE,-1, "handle_ap_connect", &handle_ap_connect);
  500. network_register_state_callback(NETWORK_ETH_ACTIVE_STATE,ETH_ACTIVE_LINKDOWN_STATE, "handle_network_up", &handle_network_up);
  501. network_register_state_callback(NETWORK_WIFI_ACTIVE_STATE,WIFI_INITIALIZING_STATE, "handle_network_up", &handle_network_up);
  502. MEMTRACE_PRINT_DELTA();
  503. }
  504. MEMTRACE_PRINT_DELTA_MESSAGE("Starting Console");
  505. console_start();
  506. MEMTRACE_PRINT_DELTA_MESSAGE("Console started");
  507. if(fwurl && strlen(fwurl)>0){
  508. if(is_recovery_running){
  509. while(!bNetworkConnected){
  510. wait_for_wifi();
  511. taskYIELD();
  512. }
  513. ESP_LOGI(TAG,"Updating firmware from link: %s",fwurl);
  514. start_ota(fwurl, NULL, 0);
  515. }
  516. else {
  517. ESP_LOGE(TAG,"Restarted to application partition. We're not going to perform OTA!");
  518. }
  519. free(fwurl);
  520. }
  521. messaging_post_message(MESSAGING_INFO,MESSAGING_CLASS_SYSTEM,"System started");
  522. }