squeezelite-ota.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_http_client.h"
  14. #include "esp_https_ota.h"
  15. #include "string.h"
  16. #include <stdbool.h>
  17. #include "nvs.h"
  18. #include "nvs_flash.h"
  19. #include "cmd_system.h"
  20. #include "esp_err.h"
  21. #include "tcpip_adapter.h"
  22. #include "squeezelite-ota.h"
  23. static const char *TAG = "squeezelite-ota";
  24. extern const uint8_t server_cert_pem_start[] asm("_binary_github_pem_start");
  25. extern const uint8_t server_cert_pem_end[] asm("_binary_github_pem_end");
  26. extern bool wait_for_wifi();
  27. #define OTA_URL_SIZE 256
  28. static char ota_status[31]={0};
  29. static uint8_t ota_pct=0;
  30. const char * ota_get_status(){
  31. return ota_status;
  32. }
  33. uint8_t ota_get_pct_complete(){
  34. return ota_pct;
  35. }
  36. esp_err_t _http_event_handler(esp_http_client_event_t *evt)
  37. {
  38. switch (evt->event_id) {
  39. case HTTP_EVENT_ERROR:
  40. ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
  41. //strncpy(ota_status,"HTTP_EVENT_ERROR",sizeof(ota_status)-1);
  42. break;
  43. case HTTP_EVENT_ON_CONNECTED:
  44. ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
  45. // strncpy(ota_status,"HTTP_EVENT_ON_CONNECTED",sizsffeof(ota_status)-1);
  46. break;
  47. case HTTP_EVENT_HEADER_SENT:
  48. ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
  49. /// strncpy(ota_status,"HTTP_EVENT_HEADER_SENT",sizeof(ota_status)-1);
  50. break;
  51. case HTTP_EVENT_ON_HEADER:
  52. 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);
  53. // check for header Content-Length:2222240
  54. // convert to numeric and store in total bin size
  55. //snprintf(ota_status,sizeof(ota_status)-1,"HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
  56. break;
  57. case HTTP_EVENT_ON_DATA:
  58. ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, status_code=%d, len=%d",esp_http_client_get_status_code(evt->client), evt->data_len);
  59. //increment the total data received, then divide by the bin size and store as ota_pct
  60. //snprintf(ota_status,sizeof(ota_status)-1, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
  61. break;
  62. case HTTP_EVENT_ON_FINISH:
  63. ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
  64. break;
  65. case HTTP_EVENT_DISCONNECTED:
  66. ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
  67. //strncpy(ota_status,"HTTP_EVENT_DISCONNECTED",sizeof(ota_status)-1);
  68. break;
  69. }
  70. return ESP_OK;
  71. }
  72. void ota_task(void *pvParameter)
  73. {
  74. char * bin_url=(char *)pvParameter;
  75. esp_http_client_config_t config = {
  76. .url = bin_url,
  77. .cert_pem = (char *)server_cert_pem_start,
  78. .event_handler = _http_event_handler,
  79. .buffer_size = 1024*50,
  80. };
  81. config.skip_cert_common_name_check = false;
  82. esp_err_t ret = esp_https_ota(&config);
  83. if (ret == ESP_OK) {
  84. esp_restart();
  85. } else {
  86. ESP_LOGE(TAG, "Firmware upgrade failed");
  87. }
  88. free(pvParameter);
  89. return;
  90. }
  91. void start_ota(const char * bin_url)
  92. {
  93. ESP_LOGW(TAG, "Called to update the firmware from url: %s",bin_url);
  94. #if RECOVERY_APPLICATION
  95. // Initialize NVS.
  96. esp_err_t err = nvs_flash_init();
  97. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  98. // todo: If we ever change the size of the nvs partition, we need to figure out a mechanism to enlarge the nvs.
  99. // 1.OTA app partition table has a smaller NVS partition size than the non-OTA
  100. // partition table. This size mismatch may cause NVS initialization to fail.
  101. // 2.NVS partition contains data in new format and cannot be recognized by this version of code.
  102. // If this happens, we erase NVS partition and initialize NVS again.
  103. ESP_ERROR_CHECK(nvs_flash_erase());
  104. err = nvs_flash_init();
  105. }
  106. ESP_ERROR_CHECK(err);
  107. char * urlPtr=malloc((strlen(bin_url)+1)*sizeof(char));
  108. strcpy(urlPtr,bin_url);
  109. ESP_LOGI(TAG, "Starting ota: %s", urlPtr);
  110. xTaskCreate(&ota_task, "ota_task", 8192*3,(void *) urlPtr, 5, NULL);
  111. #else
  112. ESP_LOGW(TAG, "Rebooting to recovery to complete the installation");
  113. guided_factory();
  114. #endif
  115. }