esp_app_main.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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 "wifi_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. static const char certs_namespace[] = "certificates";
  48. static const char certs_key[] = "blob";
  49. static const char certs_version[] = "version";
  50. const char unknown_string_placeholder[] = "unknown";
  51. const char null_string_placeholder[] = "null";
  52. EventGroupHandle_t wifi_event_group;
  53. bool bypass_wifi_manager=false;
  54. const int CONNECTED_BIT = BIT0;
  55. #define JOIN_TIMEOUT_MS (10000)
  56. #define LOCAL_MAC_SIZE 20
  57. static const char TAG[] = "esp_app_main";
  58. #define DEFAULT_HOST_NAME "squeezelite"
  59. char * fwurl = NULL;
  60. RTC_NOINIT_ATTR uint32_t RebootCounter ;
  61. static bool bWifiConnected=false;
  62. extern const uint8_t server_cert_pem_start[] asm("_binary_github_pem_start");
  63. extern const uint8_t server_cert_pem_end[] asm("_binary_github_pem_end");
  64. // as an exception _init function don't need include
  65. extern void services_init(void);
  66. extern void display_init(char *welcome);
  67. const char * str_or_unknown(const char * str) { return (str?str:unknown_string_placeholder); }
  68. const char * str_or_null(const char * str) { return (str?str:null_string_placeholder); }
  69. bool is_recovery_running;
  70. /* brief this is an exemple of a callback that you can setup in your own app to get notified of wifi manager event */
  71. void cb_connection_got_ip(void *pvParameter){
  72. static ip4_addr_t ip;
  73. tcpip_adapter_ip_info_t ipInfo;
  74. tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
  75. if (ip.addr && ipInfo.ip.addr != ip.addr) {
  76. ESP_LOGW(TAG, "IP change, need to reboot");
  77. if(!wait_for_commit()){
  78. ESP_LOGW(TAG,"Unable to commit configuration. ");
  79. }
  80. esp_restart();
  81. }
  82. ip.addr = ipInfo.ip.addr;
  83. ESP_LOGI(TAG, "Wifi connected!");
  84. messaging_post_message(MESSAGING_INFO,MESSAGING_CLASS_SYSTEM,"Wifi connected");
  85. xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  86. bWifiConnected=true;
  87. led_unpush(LED_GREEN);
  88. if(is_recovery_running){
  89. // when running in recovery, send a LMS discovery message
  90. // to find a running instance. This is to enable using
  91. // the plugin's proxy mode for FW download and avoid
  92. // expired certificate issues.
  93. discover_ota_server(5);
  94. }
  95. }
  96. void cb_connection_sta_disconnected(void *pvParameter){
  97. led_blink_pushed(LED_GREEN, 250, 250);
  98. messaging_post_message(MESSAGING_WARNING,MESSAGING_CLASS_SYSTEM,"Wifi disconnected");
  99. bWifiConnected=false;
  100. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  101. }
  102. bool wait_for_wifi(){
  103. bool connected=(xEventGroupGetBits(wifi_event_group) & CONNECTED_BIT)!=0;
  104. if(!connected){
  105. ESP_LOGD(TAG,"Waiting for WiFi...");
  106. connected = (xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
  107. pdFALSE, pdTRUE, JOIN_TIMEOUT_MS / portTICK_PERIOD_MS)& CONNECTED_BIT)!=0;
  108. if(!connected){
  109. ESP_LOGW(TAG,"wifi timeout.");
  110. }
  111. else
  112. {
  113. ESP_LOGI(TAG,"WiFi Connected!");
  114. }
  115. }
  116. return connected;
  117. }
  118. char * process_ota_url(){
  119. ESP_LOGI(TAG,"Checking for update url");
  120. char * fwurl=config_alloc_get(NVS_TYPE_STR, "fwurl");
  121. if(fwurl!=NULL)
  122. {
  123. ESP_LOGD(TAG,"Deleting nvs entry for Firmware URL %s", fwurl);
  124. config_delete_key("fwurl");
  125. }
  126. return fwurl;
  127. }
  128. esp_log_level_t get_log_level_from_char(char * level){
  129. if(!strcasecmp(level, "NONE" )) { return ESP_LOG_NONE ;}
  130. if(!strcasecmp(level, "ERROR" )) { return ESP_LOG_ERROR ;}
  131. if(!strcasecmp(level, "WARN" )) { return ESP_LOG_WARN ;}
  132. if(!strcasecmp(level, "INFO" )) { return ESP_LOG_INFO ;}
  133. if(!strcasecmp(level, "DEBUG" )) { return ESP_LOG_DEBUG ;}
  134. if(!strcasecmp(level, "VERBOSE" )) { return ESP_LOG_VERBOSE;}
  135. return ESP_LOG_WARN;
  136. }
  137. void set_log_level(char * tag, char * level){
  138. esp_log_level_set(tag, get_log_level_from_char(level));
  139. }
  140. esp_err_t update_certificates(bool force){
  141. nvs_handle handle;
  142. esp_err_t esp_err;
  143. esp_app_desc_t running_app_info;
  144. ESP_LOGI(TAG, "About to check if certificates need to be updated in flash");
  145. esp_err = nvs_open_from_partition(settings_partition, certs_namespace, NVS_READWRITE, &handle);
  146. if (esp_err != ESP_OK) {
  147. 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));
  148. return esp_err;
  149. }
  150. const esp_partition_t *running = esp_ota_get_running_partition();
  151. ESP_LOGI(TAG, "Running partition [%s] type %d subtype %d (offset 0x%08x)", running->label, running->type, running->subtype, running->address);
  152. if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) {
  153. ESP_LOGI(TAG, "Running version: %s", running_app_info.version);
  154. }
  155. size_t len=0;
  156. char *str=NULL;
  157. bool changed=false;
  158. if ( (esp_err= nvs_get_str(handle, certs_version, NULL, &len)) == ESP_OK) {
  159. str=(char *)malloc(len+1);
  160. if(str){
  161. memset(str,0x00,len+1);
  162. if ( (esp_err = nvs_get_str(handle, certs_version, str, &len)) == ESP_OK) {
  163. ESP_LOGI(TAG,"Certificate version: %s", str);
  164. }
  165. }
  166. }
  167. if(str!=NULL && running->subtype !=ESP_PARTITION_SUBTYPE_APP_FACTORY){
  168. // If certificates were found in nvs, only update if we're not
  169. // running recovery. This will prevent rolling back to an older version
  170. if(strcmp((char *)running_app_info.version,(char *)str )){
  171. // Versions are different
  172. ESP_LOGW(TAG,"Found a different software version. Updating certificates");
  173. changed=true;
  174. }
  175. free(str);
  176. }
  177. else if(str==NULL){
  178. ESP_LOGW(TAG,"No certificate found. Adding certificates");
  179. changed=true;
  180. }
  181. if(changed || force){
  182. esp_err = nvs_set_blob(handle, certs_key, server_cert_pem_start, (server_cert_pem_end-server_cert_pem_start));
  183. if(esp_err!=ESP_OK){
  184. log_send_messaging(MESSAGING_ERROR,"Failed to store certificate data: %s", esp_err_to_name(esp_err));
  185. }
  186. else {
  187. esp_err = nvs_set_str(handle, certs_version, running_app_info.version);
  188. if(esp_err!=ESP_OK){
  189. log_send_messaging(MESSAGING_ERROR,"Unable to update HTTPS Certificates version: %s",esp_err_to_name(esp_err));
  190. }
  191. else {
  192. esp_err = nvs_commit(handle);
  193. if(esp_err!=ESP_OK){
  194. log_send_messaging(MESSAGING_ERROR,"Failed to commit certificates changes : %s",esp_err_to_name(esp_err));
  195. }
  196. else {
  197. log_send_messaging(MESSAGING_INFO,"HTTPS Certificates were updated with version: %s",running_app_info.version);
  198. }
  199. }
  200. }
  201. }
  202. nvs_close(handle);
  203. return ESP_OK;
  204. }
  205. const char * get_certificate(){
  206. nvs_handle handle;
  207. esp_err_t esp_err;
  208. char *blob =NULL;
  209. //
  210. ESP_LOGD(TAG, "Fetching certificate.");
  211. esp_err = nvs_open_from_partition(settings_partition, certs_namespace, NVS_READONLY, &handle);
  212. if(esp_err == ESP_OK){
  213. size_t len;
  214. esp_err = nvs_get_blob(handle, certs_key, NULL, &len);
  215. if( esp_err == ESP_OK) {
  216. blob = (char *) heap_caps_malloc(len+1, (MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT));
  217. if(!blob){
  218. log_send_messaging(MESSAGING_ERROR,"Unable to retrieve HTTPS certificates. %s","Memory allocation failed");
  219. return "";
  220. }
  221. memset(blob,0x00,len+1);
  222. esp_err = nvs_get_blob(handle, certs_key, blob, &len);
  223. if ( esp_err == ESP_OK) {
  224. ESP_LOGD(TAG,"Certificates content is %d bytes long: ", len);
  225. }
  226. else {
  227. log_send_messaging(MESSAGING_ERROR,"Unable to retrieve HTTPS certificates. Get blob failed: %s", esp_err_to_name(esp_err));
  228. }
  229. }
  230. else{
  231. log_send_messaging(MESSAGING_ERROR,"Unable to retrieve HTTPS certificates. Get blob failed: %s",esp_err_to_name(esp_err));
  232. }
  233. nvs_close(handle);
  234. }
  235. else{
  236. 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));
  237. }
  238. return blob;
  239. }
  240. #define DEFAULT_NAME_WITH_MAC(var,defval) char var[strlen(defval)+sizeof(macStr)]; strcpy(var,defval); strcat(var,macStr)
  241. void register_default_nvs(){
  242. uint8_t mac[6];
  243. char macStr[LOCAL_MAC_SIZE+1];
  244. char default_command_line[strlen(CONFIG_DEFAULT_COMMAND_LINE)+sizeof(macStr)];
  245. esp_read_mac((uint8_t *)&mac, ESP_MAC_WIFI_STA);
  246. snprintf(macStr, LOCAL_MAC_SIZE-1,"-%x%x%x", mac[3], mac[4], mac[5]);
  247. DEFAULT_NAME_WITH_MAC(default_bt_name,CONFIG_BT_NAME);
  248. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "bt_name", default_bt_name);
  249. config_set_default(NVS_TYPE_STR, "bt_name", default_bt_name, 0);
  250. DEFAULT_NAME_WITH_MAC(default_host_name,DEFAULT_HOST_NAME);
  251. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "host_name", default_host_name);
  252. config_set_default(NVS_TYPE_STR, "host_name", default_host_name, 0);
  253. DEFAULT_NAME_WITH_MAC(default_airplay_name,CONFIG_AIRPLAY_NAME);
  254. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "airplay_name",default_airplay_name);
  255. config_set_default(NVS_TYPE_STR, "airplay_name",default_airplay_name , 0);
  256. DEFAULT_NAME_WITH_MAC(default_ap_name,CONFIG_DEFAULT_AP_SSID);
  257. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "ap_ssid", default_ap_name);
  258. config_set_default(NVS_TYPE_STR, "ap_ssid",default_ap_name , 0);
  259. strncpy(default_command_line, CONFIG_DEFAULT_COMMAND_LINE,sizeof(default_command_line)-1);
  260. strncat(default_command_line, " -n ",sizeof(default_command_line)-1);
  261. strncat(default_command_line, default_host_name,sizeof(default_command_line)-1);
  262. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "autoexec", "1");
  263. config_set_default(NVS_TYPE_STR,"autoexec","1", 0);
  264. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "autoexec1",default_command_line);
  265. config_set_default(NVS_TYPE_STR,"autoexec1",default_command_line,0);
  266. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "a2dp_sink_name", CONFIG_A2DP_SINK_NAME);
  267. config_set_default(NVS_TYPE_STR, "a2dp_sink_name", CONFIG_A2DP_SINK_NAME, 0);
  268. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "a2dp_sink_name", STR(CONFIG_A2DP_SINK_NAME));
  269. config_set_default(NVS_TYPE_STR, "a2dp_ctmt", STR(CONFIG_A2DP_CONNECT_TIMEOUT_MS), 0);
  270. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "a2dp_ctmt", STR(CONFIG_A2DP_CONNECT_TIMEOUT_MS));
  271. config_set_default(NVS_TYPE_STR, "a2dp_ctrld", STR(CONFIG_A2DP_CONTROL_DELAY_MS), 0);
  272. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "release_url", CONFIG_SQUEEZELITE_ESP32_RELEASE_URL);
  273. config_set_default(NVS_TYPE_STR, "release_url", CONFIG_SQUEEZELITE_ESP32_RELEASE_URL, 0);
  274. ESP_LOGD(TAG,"Registering default value for key %s, value %s","ap_ip_address",CONFIG_DEFAULT_AP_IP );
  275. config_set_default(NVS_TYPE_STR, "ap_ip_address",CONFIG_DEFAULT_AP_IP , 0);
  276. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "ap_ip_gateway",CONFIG_DEFAULT_AP_GATEWAY );
  277. config_set_default(NVS_TYPE_STR, "ap_ip_gateway",CONFIG_DEFAULT_AP_GATEWAY , 0);
  278. ESP_LOGD(TAG,"Registering default value for key %s, value %s","ap_ip_netmask",CONFIG_DEFAULT_AP_NETMASK );
  279. config_set_default(NVS_TYPE_STR, "ap_ip_netmask",CONFIG_DEFAULT_AP_NETMASK , 0);
  280. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "ap_channel",STR(CONFIG_DEFAULT_AP_CHANNEL));
  281. config_set_default(NVS_TYPE_STR, "ap_channel",STR(CONFIG_DEFAULT_AP_CHANNEL) , 0);
  282. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "ap_pwd", CONFIG_DEFAULT_AP_PASSWORD);
  283. config_set_default(NVS_TYPE_STR, "ap_pwd", CONFIG_DEFAULT_AP_PASSWORD, 0);
  284. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "airplay_port", CONFIG_AIRPLAY_PORT);
  285. config_set_default(NVS_TYPE_STR, "airplay_port", CONFIG_AIRPLAY_PORT, 0);
  286. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "a2dp_dev_name", CONFIG_A2DP_DEV_NAME);
  287. config_set_default(NVS_TYPE_STR, "a2dp_dev_name", CONFIG_A2DP_DEV_NAME, 0);
  288. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "bypass_wm", "0");
  289. config_set_default(NVS_TYPE_STR, "bypass_wm", "0", 0);
  290. ESP_LOGD(TAG,"Registering default value for equalizer");
  291. config_set_default(NVS_TYPE_STR, "equalizer", "", 0);
  292. ESP_LOGD(TAG,"Registering default Audio control board type %s, value ","actrls_config");
  293. config_set_default(NVS_TYPE_STR, "actrls_config", "", 0);
  294. ESP_LOGD(TAG,"Registering default value for key %s", "lms_ctrls_raw");
  295. config_set_default(NVS_TYPE_STR, "lms_ctrls_raw", "n", 0);
  296. ESP_LOGD(TAG,"Registering default Audio control board type %s, value %s", "rotary_config", CONFIG_ROTARY_ENCODER);
  297. config_set_default(NVS_TYPE_STR, "rotary_config", CONFIG_ROTARY_ENCODER, 0);
  298. char number_buffer[101] = {};
  299. snprintf(number_buffer,sizeof(number_buffer)-1,"%u",OTA_FLASH_ERASE_BLOCK);
  300. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "ota_erase_blk", number_buffer);
  301. config_set_default(NVS_TYPE_STR, "ota_erase_blk", number_buffer, 0);
  302. snprintf(number_buffer,sizeof(number_buffer)-1,"%u",OTA_STACK_SIZE);
  303. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "ota_stack", number_buffer);
  304. config_set_default(NVS_TYPE_STR, "ota_stack", number_buffer, 0);
  305. snprintf(number_buffer,sizeof(number_buffer)-1,"%d",OTA_TASK_PRIOTITY);
  306. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "ota_prio", number_buffer);
  307. config_set_default(NVS_TYPE_STR, "ota_prio", number_buffer, 0);
  308. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "enable_bt_sink", STR(CONFIG_BT_SINK));
  309. config_set_default(NVS_TYPE_STR, "enable_bt_sink", STR(CONFIG_BT_SINK), 0);
  310. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "bt_sink_pin", STR(CONFIG_BT_SINK_PIN));
  311. config_set_default(NVS_TYPE_STR, "bt_sink_pin", STR(CONFIG_BT_SINK_PIN), 0);
  312. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "bt_sink_volume", "127");
  313. config_set_default(NVS_TYPE_STR, "bt_sink_volume", "127", 0);
  314. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "enable_airplay", STR(CONFIG_AIRPLAY_SINK));
  315. config_set_default(NVS_TYPE_STR, "enable_airplay", STR(CONFIG_AIRPLAY_SINK), 0);
  316. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "display_config", CONFIG_DISPLAY_CONFIG);
  317. config_set_default(NVS_TYPE_STR, "display_config", CONFIG_DISPLAY_CONFIG, 0);
  318. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "eth_config", CONFIG_ETH_CONFIG);
  319. config_set_default(NVS_TYPE_STR, "eth_config", CONFIG_ETH_CONFIG, 0);
  320. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "i2c_config", CONFIG_I2C_CONFIG);
  321. config_set_default(NVS_TYPE_STR, "i2c_config", CONFIG_I2C_CONFIG, 0);
  322. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "spi_config", CONFIG_SPI_CONFIG);
  323. config_set_default(NVS_TYPE_STR, "spi_config", CONFIG_SPI_CONFIG, 0);
  324. ESP_LOGD(TAG,"Registering default value for key %s, value %s", "set_GPIO", CONFIG_SET_GPIO);
  325. config_set_default(NVS_TYPE_STR, "set_GPIO", CONFIG_SET_GPIO, 0);
  326. ESP_LOGD(TAG,"Registering default value for key %s", "led_brightness");
  327. config_set_default(NVS_TYPE_STR, "led_brightness", "", 0);
  328. ESP_LOGD(TAG,"Registering default value for key %s", "spdif_config");
  329. config_set_default(NVS_TYPE_STR, "spdif_config", "", 0);
  330. ESP_LOGD(TAG,"Registering default value for key %s", "dac_config");
  331. config_set_default(NVS_TYPE_STR, "dac_config", "", 0);
  332. //todo: add dac_config for known targets
  333. ESP_LOGD(TAG,"Registering default value for key %s", "dac_controlset");
  334. config_set_default(NVS_TYPE_STR, "dac_controlset", "", 0);
  335. ESP_LOGD(TAG,"Registering default value for key %s", "jack_mutes_amp");
  336. config_set_default(NVS_TYPE_STR, "jack_mutes_amp", "n", 0);
  337. ESP_LOGD(TAG,"Registering default value for key %s", "bat_config");
  338. config_set_default(NVS_TYPE_STR, "bat_config", "", 0);
  339. ESP_LOGD(TAG,"Registering default value for key %s", "metadata_config");
  340. config_set_default(NVS_TYPE_STR, "metadata_config", "", 0);
  341. ESP_LOGD(TAG,"Registering default value for key %s", "telnet_enable");
  342. config_set_default(NVS_TYPE_STR, "telnet_enable", "", 0);
  343. ESP_LOGD(TAG,"Registering default value for key %s", "telnet_buffer");
  344. config_set_default(NVS_TYPE_STR, "telnet_buffer", "40000", 0);
  345. ESP_LOGD(TAG,"Registering default value for key %s", "telnet_block");
  346. config_set_default(NVS_TYPE_STR, "telnet_block", "500", 0);
  347. ESP_LOGD(TAG,"Registering default value for key %s", "stats");
  348. config_set_default(NVS_TYPE_STR, "stats", "n", 0);
  349. ESP_LOGD(TAG,"Registering default value for key %s", "rel_api");
  350. config_set_default(NVS_TYPE_STR, "rel_api", CONFIG_RELEASE_API, 0);
  351. wait_for_commit();
  352. ESP_LOGD(TAG,"Done setting default values in nvs.");
  353. }
  354. uint32_t halSTORAGE_RebootCounterRead(void) { return RebootCounter ; }
  355. uint32_t halSTORAGE_RebootCounterUpdate(int32_t xValue) { return (RebootCounter = (xValue != 0) ? (RebootCounter + xValue) : 0) ; }
  356. void handle_ap_connect(){
  357. start_telnet(NULL);
  358. halSTORAGE_RebootCounterUpdate(0);
  359. }
  360. void app_main()
  361. {
  362. const esp_partition_t *running = esp_ota_get_running_partition();
  363. is_recovery_running = (running->subtype == ESP_PARTITION_SUBTYPE_APP_FACTORY);
  364. esp_reset_reason_t xReason = esp_reset_reason();
  365. ESP_LOGI(TAG,"Reset reason is: %u", xReason);
  366. if(!is_recovery_running && xReason != ESP_RST_SW && xReason != ESP_RST_POWERON ) {
  367. /* unscheduled restart (HW, Watchdog or similar) thus increment dynamic
  368. * counter then log current boot statistics as a warning */
  369. uint32_t Counter = halSTORAGE_RebootCounterUpdate(1) ; // increment counter
  370. ESP_LOGI(TAG,"Reboot counter=%u\n", Counter) ;
  371. if (Counter == 5) {
  372. // before we change the partition, update the info for current running partition.
  373. halSTORAGE_RebootCounterUpdate(0);
  374. guided_factory();
  375. }
  376. }
  377. char * fwurl = NULL;
  378. ESP_LOGI(TAG,"Starting app_main");
  379. initialize_nvs();
  380. ESP_LOGI(TAG,"Setting up telnet.");
  381. init_telnet(); // align on 32 bits boundaries
  382. ESP_LOGI(TAG,"Setting up config subsystem.");
  383. config_init();
  384. ESP_LOGD(TAG,"Creating event group for wifi");
  385. wifi_event_group = xEventGroupCreate();
  386. ESP_LOGD(TAG,"Clearing CONNECTED_BIT from wifi group");
  387. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  388. ESP_LOGI(TAG,"Registering default values");
  389. register_default_nvs();
  390. ESP_LOGI(TAG,"Configuring services");
  391. services_init();
  392. ESP_LOGI(TAG,"Initializing display");
  393. display_init("SqueezeESP32");
  394. if(is_recovery_running && display){
  395. GDS_ClearExt(display, true);
  396. GDS_SetFont(display, &Font_droid_sans_fallback_15x17 );
  397. GDS_TextPos(display, GDS_FONT_MEDIUM, GDS_TEXT_CENTERED, GDS_TEXT_CLEAR | GDS_TEXT_UPDATE, "RECOVERY");
  398. }
  399. ESP_LOGI(TAG,"Checking if certificates need to be updated");
  400. update_certificates(false);
  401. ESP_LOGD(TAG,"Getting firmware OTA URL (if any)");
  402. fwurl = process_ota_url();
  403. ESP_LOGD(TAG,"Getting value for WM bypass, nvs 'bypass_wm'");
  404. char * bypass_wm = config_alloc_get_default(NVS_TYPE_STR, "bypass_wm", "0", 0);
  405. if(bypass_wm==NULL)
  406. {
  407. ESP_LOGE(TAG, "Unable to retrieve the Wifi Manager bypass flag");
  408. bypass_wifi_manager = false;
  409. }
  410. else {
  411. bypass_wifi_manager=(strcmp(bypass_wm,"1")==0 ||strcasecmp(bypass_wm,"y")==0);
  412. }
  413. if(!is_recovery_running){
  414. ESP_LOGD(TAG,"Getting audio control mapping ");
  415. char *actrls_config = config_alloc_get_default(NVS_TYPE_STR, "actrls_config", NULL, 0);
  416. if (actrls_init(actrls_config) == ESP_OK) {
  417. ESP_LOGD(TAG,"Initializing audio control buttons type %s", actrls_config);
  418. } else {
  419. ESP_LOGD(TAG,"No audio control buttons");
  420. }
  421. if (actrls_config) free(actrls_config);
  422. }
  423. /* start the wifi manager */
  424. ESP_LOGD(TAG,"Blinking led");
  425. led_blink_pushed(LED_GREEN, 250, 250);
  426. if(bypass_wifi_manager){
  427. ESP_LOGW(TAG,"*******************************************************************************************");
  428. ESP_LOGW(TAG,"* wifi manager is disabled. Please use wifi commands to connect to your wifi access point.");
  429. ESP_LOGW(TAG,"*******************************************************************************************");
  430. }
  431. else {
  432. ESP_LOGI(TAG,"Starting Wifi Manager");
  433. wifi_manager_start();
  434. wifi_manager_set_callback(EVENT_STA_GOT_IP, &cb_connection_got_ip);
  435. wifi_manager_set_callback(EVENT_STA_DISCONNECTED, &cb_connection_sta_disconnected);
  436. /* Start the telnet service after we are certain that the network stack has been properly initialized.
  437. * This can be either after we're started the AP mode, or after we've started the STA mode */
  438. wifi_manager_set_callback(ORDER_START_AP, &handle_ap_connect);
  439. wifi_manager_set_callback(ORDER_CONNECT_STA, &handle_ap_connect);
  440. }
  441. console_start();
  442. if(fwurl && strlen(fwurl)>0){
  443. if(is_recovery_running){
  444. while(!bWifiConnected){
  445. wait_for_wifi();
  446. taskYIELD();
  447. }
  448. ESP_LOGI(TAG,"Updating firmware from link: %s",fwurl);
  449. start_ota(fwurl, NULL, 0);
  450. }
  451. else {
  452. ESP_LOGE(TAG,"Restarted to application partition. We're not going to perform OTA!");
  453. }
  454. free(fwurl);
  455. }
  456. messaging_post_message(MESSAGING_INFO,MESSAGING_CLASS_SYSTEM,"System started");
  457. }