123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "esp_system.h"
- #include "esp_event.h"
- #include "esp_log.h"
- #include "esp_ota_ops.h"
- #include "esp_http_client.h"
- #include "esp_https_ota.h"
- #include "string.h"
- #include <stdbool.h>
- #include "nvs.h"
- #include "nvs_flash.h"
- #include "esp_err.h"
- #include "tcpip_adapter.h"
- static const char *TAG = "squeezelite-ota";
- #define OTA_URL_SIZE 256
- static char ota_status[31]={0};
- static uint8_t ota_pct=0;
- const char * ota_get_status(){
- return ota_status;
- }
- uint8_t ota_get_pct_complete(){
- return ota_pct;
- }
- esp_err_t _http_event_handler(esp_http_client_event_t *evt)
- {
- switch (evt->event_id) {
- case HTTP_EVENT_ERROR:
- ESP_LOGD(TAG, "HTTP_EVENT_ERROR");
- strncpy(ota_status,sizeof(ota_status)-1,"HTTP_EVENT_ERROR");
- break;
- case HTTP_EVENT_ON_CONNECTED:
- ESP_LOGD(TAG, "HTTP_EVENT_ON_CONNECTED");
- strncpy(ota_status,sizeof(ota_status)-1,"HTTP_EVENT_ON_CONNECTED");
- break;
- case HTTP_EVENT_HEADER_SENT:
- ESP_LOGD(TAG, "HTTP_EVENT_HEADER_SENT");
- strncpy(ota_status,sizeof(ota_status)-1,"HTTP_EVENT_HEADER_SENT");
- break;
- case HTTP_EVENT_ON_HEADER:
- ESP_LOGD(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, evt->header_value);
- break;
- case HTTP_EVENT_ON_DATA:
- ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
- break;
- case HTTP_EVENT_ON_FINISH:
- ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
- break;
- case HTTP_EVENT_DISCONNECTED:
- ESP_LOGD(TAG, "HTTP_EVENT_DISCONNECTED");
- strncpy(ota_status,sizeof(ota_status)-1,"HTTP_EVENT_DISCONNECTED");
- break;
- }
- return ESP_OK;
- }
- void ota_task(void *pvParameter, const char * bin_url)
- {
- ESP_LOGI(TAG, "Starting OTA example");
- esp_http_client_config_t config = {
- .url = bin_url,
-
- .event_handler = _http_event_handler,
- };
-
- config.skip_cert_common_name_check = true;
- esp_err_t ret = esp_https_ota(&config);
- if (ret == ESP_OK) {
- esp_restart();
- } else {
- ESP_LOGE(TAG, "Firmware upgrade failed");
- }
- while (1) {
- vTaskDelay(1000 / portTICK_PERIOD_MS);
- }
- }
- void start_ota(const char * bin_url)
- {
-
- esp_err_t err = nvs_flash_init();
- if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
-
-
-
-
-
- ESP_ERROR_CHECK(nvs_flash_erase());
- err = nvs_flash_init();
- }
- ESP_ERROR_CHECK(err);
- xTaskCreate(&ota_task, "ota_task", 8192, NULL, 5, NULL);
- }
|