esp_app_main.c 22 KB

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