squeezelite-ota.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* OTA example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "esp_system.h"
  10. #include "esp_event.h"
  11. #include "esp_log.h"
  12. #include "esp_ota_ops.h"
  13. #include "esp_https_ota.h"
  14. #include "string.h"
  15. #include <stdbool.h>
  16. #include "nvs.h"
  17. #include "nvs_flash.h"
  18. #include "cmd_system.h"
  19. #include "esp_err.h"
  20. #include "tcpip_adapter.h"
  21. #include "squeezelite-ota.h"
  22. static const char *TAG = "squeezelite-ota";
  23. extern const uint8_t server_cert_pem_start[] asm("_binary_github_pem_start");
  24. extern const uint8_t server_cert_pem_end[] asm("_binary_github_pem_end");
  25. extern bool wait_for_wifi();
  26. extern char current_namespace[];
  27. static struct {
  28. char status_text[31];
  29. uint32_t ota_actual_len;
  30. uint32_t ota_total_len;
  31. char * actual_url;
  32. bool bOTA_started;
  33. } ota_status;
  34. static esp_http_client_config_t config;
  35. static esp_http_client_config_t ota_config;
  36. static esp_http_client_handle_t client;
  37. const char * ota_get_status(){
  38. return ota_status.status_text;
  39. }
  40. uint8_t ota_get_pct_complete(){
  41. return ota_status.ota_total_len==0?0:
  42. (uint8_t)((uint32_t)ota_status.ota_actual_len/(uint32_t)ota_status.ota_total_len*100);
  43. }
  44. static void __attribute__((noreturn)) task_fatal_error(void)
  45. {
  46. ESP_LOGE(TAG, "Exiting task due to fatal error...");
  47. (void)vTaskDelete(NULL);
  48. while (1) {
  49. ;
  50. }
  51. }
  52. esp_err_t _http_event_handler(esp_http_client_event_t *evt)
  53. {
  54. // --------------
  55. // Received parameters
  56. //
  57. // esp_http_client_event_id_tevent_id event_id, to know the cause of the event
  58. // esp_http_client_handle_t client
  59. // esp_http_client_handle_t context
  60. // void *data data of the event
  61. // int data_len - data length of data
  62. // void *user_data -- user_data context, from esp_http_client_config_t user_data
  63. // char *header_key For HTTP_EVENT_ON_HEADER event_id, it’s store current http header key
  64. // char *header_value For HTTP_EVENT_ON_HEADER event_id, it’s store current http header value
  65. // --------------
  66. switch (evt->event_id) {
  67. case HTTP_EVENT_ERROR:
  68. ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
  69. //strncpy(ota_status,"HTTP_EVENT_ERROR",sizeof(ota_status)-1);
  70. break;
  71. case HTTP_EVENT_ON_CONNECTED:
  72. ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
  73. if(!ota_status.bOTA_started){
  74. ESP_LOGW(TAG, "Resetting the OTA stats");
  75. ota_status.ota_total_len=0;
  76. ota_status.ota_actual_len=0;
  77. if(ota_status.actual_url!=NULL){
  78. free(ota_status.actual_url);
  79. ota_status.actual_url=NULL;
  80. }
  81. }
  82. break;
  83. case HTTP_EVENT_HEADER_SENT:
  84. ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
  85. /// strncpy(ota_status,"HTTP_EVENT_HEADER_SENT",sizeof(ota_status)-1);
  86. break;
  87. case HTTP_EVENT_ON_HEADER:
  88. ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, status_code=%d, key=%s, value=%s",esp_http_client_get_status_code(evt->client),evt->header_key, evt->header_value);
  89. if (strcasecmp(evt->header_key, "location") == 0) {
  90. if(ota_status.actual_url!=NULL) {
  91. free(ota_status.actual_url);
  92. ota_status.actual_url = NULL;
  93. }
  94. ota_status.actual_url=strdup(evt->header_value);
  95. ESP_LOGW(TAG,"OTA will redirect to url: %s",ota_status.actual_url);
  96. ota_status.bOTA_started = true;
  97. }
  98. if (strcasecmp(evt->header_key, "content-length") == 0) {
  99. ota_status.ota_total_len = atol(evt->header_value);
  100. ESP_LOGW(TAG, "Content length found: %s, parsed to %d", evt->header_value, ota_status.ota_total_len);
  101. }
  102. break;
  103. case HTTP_EVENT_ON_DATA:
  104. if(!ota_status.bOTA_started) ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, status_code=%d, len=%d",esp_http_client_get_status_code(evt->client), evt->data_len);
  105. if(esp_http_client_get_status_code(evt->client) == 302){
  106. // This is an indication of a redirect. Let's follow it
  107. return ESP_OK;
  108. }
  109. if(esp_http_client_get_status_code(evt->client) == 200 ){
  110. if(!ota_status.bOTA_started) return ESP_OK;
  111. ota_status.ota_actual_len+=evt->data_len;
  112. ESP_LOGD(TAG,"Receiving OTA data chunk len: %d, %d of %d (%d pct)", evt->data_len, ota_status.ota_actual_len, ota_status.ota_total_len, ota_get_pct_complete());
  113. }
  114. break;
  115. case HTTP_EVENT_ON_FINISH:
  116. ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
  117. break;
  118. case HTTP_EVENT_DISCONNECTED:
  119. ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
  120. break;
  121. }
  122. return ESP_OK;
  123. }
  124. static void check_http_redirect(void)
  125. {
  126. esp_err_t err=ESP_OK;
  127. ESP_LOGD(TAG, "Checking for http redirects. initializing http client");
  128. client = esp_http_client_init(&config);
  129. if (client == NULL) {
  130. ESP_LOGE(TAG, "Failed to initialise HTTP connection");
  131. task_fatal_error();
  132. }
  133. ESP_LOGD(TAG, "opening http connection");
  134. err = esp_http_client_open(client, 0);
  135. if (err != ESP_OK) {
  136. ESP_LOGE(TAG, "Failed to open HTTP connection: %s", esp_err_to_name(err));
  137. esp_http_client_cleanup(client);
  138. task_fatal_error();
  139. }
  140. ESP_LOGD(TAG, "fetching headers");
  141. esp_http_client_fetch_headers(client);
  142. if (err == ESP_OK) {
  143. ESP_LOGI(TAG, "redirect check returned success");
  144. } else {
  145. ESP_LOGE(TAG, "redirect check returned %s", esp_err_to_name(err));
  146. }
  147. }
  148. esp_err_t init_config(esp_http_client_config_t * conf, const char * url){
  149. memset(&ota_status, 0x00, sizeof(ota_status));
  150. if(url==NULL || strlen(url)==0){
  151. ESP_LOGE(TAG,"HTTP OTA called without a url");
  152. return ESP_ERR_INVALID_ARG;
  153. }
  154. memset(conf, 0x00, sizeof(esp_http_client_config_t));
  155. ota_status.actual_url= strdup(url);
  156. ota_status.bOTA_started=false;
  157. conf->cert_pem = (char *)server_cert_pem_start;
  158. conf->event_handler = _http_event_handler;
  159. conf->buffer_size = 1024*4;
  160. conf->disable_auto_redirect=true;
  161. conf->skip_cert_common_name_check = false;
  162. conf->url = strdup(ota_status.actual_url);
  163. conf->max_redirection_count = 0;
  164. return ESP_OK;
  165. }
  166. void ota_task(void *pvParameter)
  167. {
  168. ESP_LOGD(TAG, "HTTP ota Thread started");
  169. if(init_config(&config,(char *)pvParameter)!=ESP_OK){
  170. if(pvParameter!=NULL) free(pvParameter);
  171. vTaskDelete(NULL);
  172. return;
  173. }
  174. free(pvParameter);
  175. check_http_redirect();
  176. if(!ota_status.bOTA_started || ota_status.actual_url == NULL){
  177. // OTA Failed miserably. Errors would have been logged somewhere
  178. ESP_LOGE(TAG,"Redirect check failed. Bailing out");
  179. vTaskDelete(NULL);
  180. }
  181. ESP_LOGD(TAG,"Calling esp_https_ota");
  182. if(init_config(&ota_config,ota_status.actual_url)!=ESP_OK){
  183. if(pvParameter!=NULL) free(pvParameter);
  184. vTaskDelete(NULL);
  185. return;
  186. }
  187. free(ota_status.actual_url);
  188. ota_status.actual_url=NULL;
  189. esp_err_t err = esp_https_ota(&config);
  190. if (err == ESP_OK) {
  191. esp_restart();
  192. } else {
  193. ESP_LOGE(TAG, "Firmware upgrade failed with error : %s", esp_err_to_name(err));
  194. }
  195. vTaskDelete(NULL);
  196. }
  197. void start_ota(const char * bin_url)
  198. {
  199. ESP_LOGW(TAG, "Called to update the firmware from url: %s",bin_url);
  200. #if RECOVERY_APPLICATION
  201. // Initialize NVS.
  202. int ret=0;
  203. esp_err_t err = nvs_flash_init();
  204. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  205. // todo: If we ever change the size of the nvs partition, we need to figure out a mechanism to enlarge the nvs.
  206. // 1.OTA app partition table has a smaller NVS partition size than the non-OTA
  207. // partition table. This size mismatch may cause NVS initialization to fail.
  208. // 2.NVS partition contains data in new format and cannot be recognized by this version of code.
  209. // If this happens, we erase NVS partition and initialize NVS again.
  210. ESP_ERROR_CHECK(nvs_flash_erase());
  211. err = nvs_flash_init();
  212. }
  213. ESP_ERROR_CHECK(err);
  214. char * urlPtr=malloc((strlen(bin_url)+1)*sizeof(char));
  215. strcpy(urlPtr,bin_url);
  216. // the first thing we need to do here is to erase the firmware url
  217. // to avoid a boot loop
  218. nvs_handle nvs;
  219. err = nvs_open(current_namespace, NVS_READWRITE, &nvs);
  220. if (err == ESP_OK) {
  221. err = nvs_erase_key(nvs, "fwurl");
  222. if (err == ESP_OK) {
  223. err = nvs_commit(nvs);
  224. if (err == ESP_OK) {
  225. ESP_LOGI(TAG, "Value with key '%s' erased", "fwurl");
  226. }
  227. }
  228. nvs_close(nvs);
  229. }
  230. ESP_LOGI(TAG, "Starting ota: %s", urlPtr);
  231. ret=xTaskCreate(&ota_task, "ota_task", 1024*8,(void *) urlPtr, 3, NULL);
  232. if (ret != pdPASS) {
  233. ESP_LOGI(TAG, "create thread %s failed", "ota_task");
  234. }
  235. #else
  236. ESP_LOGW(TAG, "Rebooting to recovery to complete the installation");
  237. guided_factory();
  238. #endif
  239. }