esp_app_main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2019
  5. * Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #include "platform_esp32.h"
  22. #include "led.h"
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include "freertos/FreeRTOS.h"
  26. #include "driver/gpio.h"
  27. #include "driver/spi_master.h"
  28. #include "freertos/task.h"
  29. #include "esp_system.h"
  30. #include "esp_spi_flash.h"
  31. #include "esp_wifi.h"
  32. #include "esp_system.h"
  33. #include "esp_event_loop.h"
  34. #include "nvs_flash.h"
  35. #include "esp_log.h"
  36. #include "freertos/event_groups.h"
  37. #include "mdns.h"
  38. #include "lwip/api.h"
  39. #include "lwip/err.h"
  40. #include "lwip/netdb.h"
  41. #include "nvs_utilities.h"
  42. #include "http_server.h"
  43. #include "trace.h"
  44. #include "wifi_manager.h"
  45. #include "squeezelite-ota.h"
  46. static EventGroupHandle_t wifi_event_group;
  47. bool enable_bt_sink=false;
  48. bool enable_airplay=false;
  49. bool jack_mutes_amp=false;
  50. const int CONNECTED_BIT = BIT0;
  51. #define JOIN_TIMEOUT_MS (10000)
  52. static const char TAG[] = "esp_app_main";
  53. char * fwurl = NULL;
  54. #ifdef CONFIG_SQUEEZEAMP
  55. #define LED_GREEN_GPIO 12
  56. #define LED_RED_GPIO 13
  57. #else
  58. #define LED_GREEN_GPIO 0
  59. #define LED_RED_GPIO 0
  60. #endif
  61. static bool bWifiConnected=false;
  62. /* brief this is an exemple of a callback that you can setup in your own app to get notified of wifi manager event */
  63. void cb_connection_got_ip(void *pvParameter){
  64. ESP_LOGI(TAG, "I have a connection!");
  65. xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  66. bWifiConnected=true;
  67. led_unpush(LED_GREEN);
  68. }
  69. void cb_connection_sta_disconnected(void *pvParameter){
  70. led_blink_pushed(LED_GREEN, 250, 250);
  71. bWifiConnected=false;
  72. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  73. }
  74. bool wait_for_wifi(){
  75. bool connected=(xEventGroupGetBits(wifi_event_group) & CONNECTED_BIT)!=0;
  76. if(!connected){
  77. ESP_LOGD(TAG,"Waiting for WiFi...");
  78. connected = (xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
  79. pdFALSE, pdTRUE, JOIN_TIMEOUT_MS / portTICK_PERIOD_MS)& CONNECTED_BIT)!=0;
  80. if(!connected){
  81. ESP_LOGW(TAG,"wifi timeout.");
  82. }
  83. else
  84. {
  85. ESP_LOGI(TAG,"WiFi Connected!");
  86. }
  87. }
  88. return connected;
  89. }
  90. static void initialize_nvs() {
  91. esp_err_t err = nvs_flash_init();
  92. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  93. ESP_ERROR_CHECK(nvs_flash_erase());
  94. err = nvs_flash_init();
  95. }
  96. ESP_ERROR_CHECK(err);
  97. err = nvs_flash_init_partition(settings_partition);
  98. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  99. ESP_ERROR_CHECK(nvs_flash_erase_partition(settings_partition));
  100. err = nvs_flash_init_partition(settings_partition);
  101. }
  102. ESP_ERROR_CHECK(err);
  103. }
  104. char * process_ota_url(){
  105. nvs_handle nvs;
  106. ESP_LOGI(TAG,"Checking for update url");
  107. char * fwurl=get_nvs_value_alloc(NVS_TYPE_STR, "fwurl");
  108. if(fwurl!=NULL)
  109. {
  110. ESP_LOGD(TAG,"Deleting nvs entry for Firmware URL %s", fwurl);
  111. esp_err_t err = nvs_open_from_partition(settings_partition, current_namespace, NVS_READWRITE, &nvs);
  112. if (err == ESP_OK) {
  113. err = nvs_erase_key(nvs, "fwurl");
  114. if (err == ESP_OK) {
  115. ESP_LOGD(TAG,"Firmware url erased from nvs.");
  116. err = nvs_commit(nvs);
  117. if (err == ESP_OK) {
  118. ESP_LOGI(TAG, "Value with key '%s' erased", "fwurl");
  119. ESP_LOGD(TAG,"nvs erase committed.");
  120. }
  121. else
  122. {
  123. ESP_LOGE(TAG,"Unable to commit nvs erase operation. Error : %s.",esp_err_to_name(err));
  124. }
  125. }
  126. else
  127. {
  128. ESP_LOGE(TAG,"Error : %s. Unable to delete firmware url key.",esp_err_to_name(err));
  129. }
  130. nvs_close(nvs);
  131. }
  132. else
  133. {
  134. ESP_LOGE(TAG,"Error opening nvs: %s. Unable to delete firmware url key.",esp_err_to_name(err));
  135. }
  136. }
  137. return fwurl;
  138. }
  139. //CONFIG_SDIF_NUM=0
  140. //CONFIG_SPDIF_BCK_IO=26
  141. //CONFIG_SPDIF_WS_IO=25
  142. //CONFIG_SPDIF_DO_IO=15
  143. //CONFIG_A2DP_CONTROL_DELAY_MS=500
  144. //CONFIG_A2DP_CONNECT_TIMEOUT_MS=1000
  145. //CONFIG_WIFI_MANAGER_MAX_RETRY=2
  146. void register_default_nvs(){
  147. nvs_value_set_default(NVS_TYPE_STR, "bt_sink_name", CONFIG_BT_NAME, 0);
  148. nvs_value_set_default(NVS_TYPE_STR, "bt_sink_pin", STR(CONFIG_BT_SINK_PIN), 0);
  149. nvs_value_set_default(NVS_TYPE_STR, "host_name", "squeezelite-esp32", 0);
  150. nvs_value_set_default(NVS_TYPE_STR, "release_url", SQUEEZELITE_ESP32_RELEASE_URL, 0);
  151. nvs_value_set_default(NVS_TYPE_STR, "ap_ip_address",CONFIG_DEFAULT_AP_IP , 0);
  152. nvs_value_set_default(NVS_TYPE_STR, "ap_ip_gateway",CONFIG_DEFAULT_AP_GATEWAY , 0);
  153. nvs_value_set_default(NVS_TYPE_STR, "ap_ip_netmask",CONFIG_DEFAULT_AP_NETMASK , 0);
  154. nvs_value_set_default(NVS_TYPE_STR, "ap_channel",STR(CONFIG_DEFAULT_AP_CHANNEL) , 0);
  155. nvs_value_set_default(NVS_TYPE_STR, "ap_ssid",CONFIG_DEFAULT_AP_SSID , 0);
  156. nvs_value_set_default(NVS_TYPE_STR, "ap_password", CONFIG_DEFAULT_AP_PASSWORD, 0);
  157. nvs_value_set_default(NVS_TYPE_STR, "airplay_name",CONFIG_AIRPLAY_NAME , 0);
  158. nvs_value_set_default(NVS_TYPE_STR, "airplay_port", CONFIG_AIRPLAY_PORT, 0);
  159. nvs_value_set_default(NVS_TYPE_STR, "a2dp_sink_name", CONFIG_A2DP_SINK_NAME, 0);
  160. nvs_value_set_default(NVS_TYPE_STR, "a2dp_dev_name", CONFIG_A2DP_DEV_NAME, 0);
  161. char * flag = get_nvs_value_alloc_default(NVS_TYPE_STR, "enable_bt_sink", STR(CONFIG_BT_SINK), 0);
  162. enable_bt_sink= (strcmp(flag,"1")==0 ||strcasecmp(flag,"y")==0);
  163. free(flag);
  164. flag = get_nvs_value_alloc_default(NVS_TYPE_STR, "enable_airplay", STR(CONFIG_AIRPLAY_SINK), 0);
  165. enable_airplay= (strcmp(flag,"1")==0 ||strcasecmp(flag,"y")==0);
  166. free(flag);
  167. flag = get_nvs_value_alloc_default(NVS_TYPE_STR, "jack_mutes_amp", "n", 0);
  168. jack_mutes_amp= (strcmp(flag,"1")==0 ||strcasecmp(flag,"y")==0);
  169. free(flag);
  170. }
  171. void app_main()
  172. {
  173. char * fwurl = NULL;
  174. initialize_nvs();
  175. register_default_nvs();
  176. led_config(LED_GREEN, LED_GREEN_GPIO, 0);
  177. led_config(LED_RED, LED_RED_GPIO, 0);
  178. wifi_event_group = xEventGroupCreate();
  179. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  180. fwurl = process_ota_url();
  181. /* start the wifi manager */
  182. led_blink(LED_GREEN, 250, 250);
  183. wifi_manager_start();
  184. wifi_manager_set_callback(EVENT_STA_GOT_IP, &cb_connection_got_ip);
  185. wifi_manager_set_callback(WIFI_EVENT_STA_DISCONNECTED, &cb_connection_sta_disconnected);
  186. console_start();
  187. if(fwurl && strlen(fwurl)>0){
  188. while(!bWifiConnected){
  189. wait_for_wifi();
  190. taskYIELD();
  191. }
  192. ESP_LOGI(TAG,"Updating firmware from link: %s",fwurl);
  193. start_ota(fwurl, true);
  194. free(fwurl);
  195. }
  196. }