2
0

squeezelite-ota.c 4.0 KB

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