arduino-esp32/libraries/Update/src/HttpsOTAUpdate.cpp
Me No Dev 5502879a5b
v2.0.0 Add support for ESP32S2 and update ESP-IDF to 4.4 (#4996)
This is very much still work in progress and much more will change before the final 2.0.0

Some APIs have changed. New libraries have been added. LittleFS included.

Co-authored-by: Seon Rozenblum <seonr@3sprockets.com>
Co-authored-by: Me No Dev <me-no-dev@users.noreply.github.com>
Co-authored-by: geeksville <kevinh@geeksville.com>
Co-authored-by: Mike Dunston <m_dunston@comcast.net>
Co-authored-by: Unexpected Maker <seon@unexpectedmaker.com>
Co-authored-by: Seon Rozenblum <seonr@3sprockets.com>
Co-authored-by: microDev <70126934+microDev1@users.noreply.github.com>
Co-authored-by: tobozo <tobozo@users.noreply.github.com>
Co-authored-by: bobobo1618 <bobobo1618@users.noreply.github.com>
Co-authored-by: lorol <lorolouis@gmail.com>
Co-authored-by: geeksville <kevinh@geeksville.com>
Co-authored-by: Limor "Ladyada" Fried <limor@ladyada.net>
Co-authored-by: Sweety <switi.mhaiske@espressif.com>
Co-authored-by: Loick MAHIEUX <loick111@gmail.com>
Co-authored-by: Larry Bernstone <lbernstone@gmail.com>
Co-authored-by: Valerii Koval <valeros@users.noreply.github.com>
Co-authored-by: 快乐的我531 <2302004040@qq.com>
Co-authored-by: chegewara <imperiaonline4@gmail.com>
Co-authored-by: Clemens Kirchgatterer <clemens@1541.org>
Co-authored-by: Aron Rubin <aronrubin@gmail.com>
Co-authored-by: Pete Lewis <601236+lewispg228@users.noreply.github.com>
2021-04-05 14:23:58 +03:00

108 lines
2.9 KiB
C++

/* OTA task
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <string.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/event_groups.h>
#include "esp32-hal-log.h"
#include "esp_http_client.h"
#include "esp_https_ota.h"
#include "HttpsOTAUpdate.h"
#define OTA_TASK_STACK_SIZE 9216
typedef void (*HttpEventCb)(HttpEvent_t*);
static esp_http_client_config_t config;
static HttpEventCb cb;
static EventGroupHandle_t ota_status = NULL;//check for ota status
static EventBits_t set_bit;
const int OTA_IDLE_BIT = BIT0;
const int OTA_UPDATING_BIT = BIT1;
const int OTA_SUCCESS_BIT = BIT2;
const int OTA_FAIL_BIT = BIT3;
esp_err_t http_event_handler(esp_http_client_event_t *event)
{
cb(event);
return ESP_OK;
}
void https_ota_task(void *param)
{
if(ota_status) {
xEventGroupSetBits(ota_status, OTA_UPDATING_BIT);
xEventGroupClearBits(ota_status, OTA_IDLE_BIT);
}
esp_err_t ret = esp_https_ota((const esp_http_client_config_t *)param);
if(ret == ESP_OK) {
if(ota_status) {
xEventGroupClearBits(ota_status, OTA_UPDATING_BIT);
xEventGroupSetBits(ota_status, OTA_SUCCESS_BIT);
}
} else {
if(ota_status) {
xEventGroupClearBits(ota_status, OTA_UPDATING_BIT);
xEventGroupSetBits(ota_status, OTA_FAIL_BIT);
}
}
vTaskDelete(NULL);
}
HttpsOTAStatus_t HttpsOTAUpdateClass::status()
{
if(ota_status) {
set_bit = xEventGroupGetBits(ota_status);
if(set_bit == OTA_IDLE_BIT) {
return HTTPS_OTA_IDLE;
}
if(set_bit == OTA_UPDATING_BIT) {
return HTTPS_OTA_UPDATING;
}
if(set_bit == OTA_SUCCESS_BIT) {
return HTTPS_OTA_SUCCESS;
}
if(set_bit == OTA_FAIL_BIT) {
return HTTPS_OTA_FAIL;
}
}
return HTTPS_OTA_ERR;
}
void HttpsOTAUpdateClass::onHttpEvent(HttpEventCb cbEvent)
{
cb = cbEvent;
}
void HttpsOTAUpdateClass::begin(const char *url, const char *cert_pem, bool skip_cert_common_name_check)
{
config.url = url;
config.cert_pem = cert_pem;
config.skip_cert_common_name_check = skip_cert_common_name_check;
config.event_handler = http_event_handler;
if(!ota_status) {
ota_status = xEventGroupCreate();
if(!ota_status) {
log_e("OTA Event Group Create Failed");
}
xEventGroupSetBits(ota_status, OTA_IDLE_BIT);
}
if (xTaskCreate(&https_ota_task, "https_ota_task", OTA_TASK_STACK_SIZE, &config, 5, NULL) != pdPASS) {
log_e("Couldn't create ota task\n");
}
}
HttpsOTAUpdateClass HttpsOTA;