esp_app_main.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "wifi_manager.h"
  44. #include "squeezelite-ota.h"
  45. static EventGroupHandle_t wifi_event_group;
  46. extern char current_namespace[];
  47. const int CONNECTED_BIT = BIT0;
  48. #define JOIN_TIMEOUT_MS (10000)
  49. static const char TAG[] = "esp_app_main";
  50. char * fwurl = NULL;
  51. #ifdef CONFIG_SQUEEZEAMP
  52. #define LED_GREEN_GPIO 12
  53. #define LED_RED_GPIO 13
  54. #else
  55. #define LED_GREEN_GPIO 0
  56. #define LED_RED_GPIO 0
  57. #endif
  58. static bool bWifiConnected=false;
  59. /* brief this is an exemple of a callback that you can setup in your own app to get notified of wifi manager event */
  60. void cb_connection_got_ip(void *pvParameter){
  61. ESP_LOGI(TAG, "I have a connection!");
  62. xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
  63. bWifiConnected=true;
  64. led_unpush(LED_GREEN);
  65. }
  66. void cb_connection_sta_disconnected(void *pvParameter){
  67. led_blink_pushed(LED_GREEN, 250, 250);
  68. bWifiConnected=false;
  69. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  70. }
  71. bool wait_for_wifi(){
  72. bool connected=(xEventGroupGetBits(wifi_event_group) & CONNECTED_BIT)!=0;
  73. if(!connected){
  74. ESP_LOGD(TAG,"Waiting for WiFi...");
  75. connected = (xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
  76. pdFALSE, pdTRUE, JOIN_TIMEOUT_MS / portTICK_PERIOD_MS)& CONNECTED_BIT)!=0;
  77. if(!connected){
  78. ESP_LOGW(TAG,"wifi timeout.");
  79. }
  80. else
  81. {
  82. ESP_LOGI(TAG,"WiFi Connected!");
  83. }
  84. }
  85. return connected;
  86. }
  87. static void initialize_nvs() {
  88. esp_err_t err = nvs_flash_init();
  89. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  90. ESP_ERROR_CHECK(nvs_flash_erase());
  91. err = nvs_flash_init();
  92. }
  93. ESP_ERROR_CHECK(err);
  94. }
  95. char * process_ota_url(){
  96. nvs_handle nvs;
  97. ESP_LOGI(TAG,"Checking for update url");
  98. char * fwurl=get_nvs_value_alloc(NVS_TYPE_STR, "fwurl");
  99. if(fwurl!=NULL)
  100. {
  101. ESP_LOGD(TAG,"Deleting nvs entry for Firmware URL %s", fwurl);
  102. esp_err_t err = nvs_open(current_namespace, NVS_READWRITE, &nvs);
  103. if (err == ESP_OK) {
  104. err = nvs_erase_key(nvs, "fwurl");
  105. if (err == ESP_OK) {
  106. ESP_LOGD(TAG,"Firmware url erased from nvs.");
  107. err = nvs_commit(nvs);
  108. if (err == ESP_OK) {
  109. ESP_LOGI(TAG, "Value with key '%s' erased", "fwurl");
  110. ESP_LOGD(TAG,"nvs erase committed.");
  111. }
  112. else
  113. {
  114. ESP_LOGE(TAG,"Unable to commit nvs erase operation. Error : %s.",esp_err_to_name(err));
  115. }
  116. }
  117. else
  118. {
  119. ESP_LOGE(TAG,"Error : %s. Unable to delete firmware url key.",esp_err_to_name(err));
  120. }
  121. nvs_close(nvs);
  122. }
  123. else
  124. {
  125. ESP_LOGE(TAG,"Error opening nvs: %s. Unable to delete firmware url key.",esp_err_to_name(err));
  126. }
  127. }
  128. return fwurl;
  129. }
  130. void app_main()
  131. {
  132. char * fwurl = NULL;
  133. initialize_nvs();
  134. led_config(LED_GREEN, LED_GREEN_GPIO, 0);
  135. led_config(LED_RED, LED_RED_GPIO, 0);
  136. wifi_event_group = xEventGroupCreate();
  137. xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
  138. fwurl = process_ota_url();
  139. /* start the wifi manager */
  140. led_blink(LED_GREEN, 250, 250);
  141. wifi_manager_start();
  142. wifi_manager_set_callback(EVENT_STA_GOT_IP, &cb_connection_got_ip);
  143. wifi_manager_set_callback(WIFI_EVENT_STA_DISCONNECTED, &cb_connection_sta_disconnected);
  144. console_start();
  145. if(fwurl && strlen(fwurl)>0){
  146. while(!bWifiConnected){
  147. wait_for_wifi();
  148. taskYIELD();
  149. }
  150. ESP_LOGI(TAG,"Updating firmware from link: %s",fwurl);
  151. start_ota(fwurl, true);
  152. free(fwurl);
  153. }
  154. }