started working on OTA but downloading files is not functional yet

This commit is contained in:
Matthias Mitscherlich
2024-03-22 17:33:53 +01:00
parent 0fcd60ff57
commit 55c3ebbbe4
10 changed files with 531 additions and 219 deletions
+90
View File
@@ -0,0 +1,90 @@
// --------------------------------------------------------------------------------------------------------------------
/// \file ota.h
/// \brief File description
// --------------------------------------------------------------------------------------------------------------------
//
// vbchaos software design
//
// --------------------------------------------------------------------------------------------------------------------
/// $Revision: $
/// $Author: $
/// $Date: $
// (c) 2023 vbchaos
// --------------------------------------------------------------------------------------------------------------------
#ifndef MAIN_APPLICATION_INC_OTA_H_
#define MAIN_APPLICATION_INC_OTA_H_
/**
* ota implementation
* \defgroup ota
* \brief {group_description}
* \addtogroup {Layer}
*
* Detailed description
* @{
*/
// --------------------------------------------------------------------------------------------------------------------
// Include files
// --------------------------------------------------------------------------------------------------------------------
// CompilerIncludes
// All include files that are provided by the compiler directly
#include <stdint.h>
// ProjectIncludes
// All include files that are provided by the project
#include "esp_event.h"
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Type definitions.
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function declarations
// --------------------------------------------------------------------------------------------------------------------
class ota
{
// -----------------------------------------------------------------------------------------------------------------
// Public Section
// -----------------------------------------------------------------------------------------------------------------
public:
static const uint32_t checkInterval_ms = 10000;
// Class Constructor
ota();
void task(void);
// -----------------------------------------------------------------------------------------------------------------
// Protected Section
// -----------------------------------------------------------------------------------------------------------------
protected:
// -----------------------------------------------------------------------------------------------------------------
// Private Section
// -----------------------------------------------------------------------------------------------------------------
private:
int imageSize;
static void eventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data);
};
/** @} */
#endif /* MAIN_APPLICATION_INC_OTA_H_ */
+183
View File
@@ -0,0 +1,183 @@
// --------------------------------------------------------------------------------------------------------------------
/// \file ota.cpp
/// \brief Description
// --------------------------------------------------------------------------------------------------------------------
//
// vbchaos software design
//
// --------------------------------------------------------------------------------------------------------------------
/// $Revision: $
/// $Author: $
/// $Date: $
// (c) 2023 vbchaos
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Include files
// --------------------------------------------------------------------------------------------------------------------
#include <ota.h>
#include "esp_crt_bundle.h"
#include "esp_https_ota.h"
#include "logger.h"
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Type definitions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// File-scope variables
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function declarations
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
ota::ota()
{
}
void ota::eventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{
if(event_base == ESP_HTTPS_OTA_EVENT)
{
switch(event_id)
{
case ESP_HTTPS_OTA_START:
LOGGER_INFO("OTA started");
break;
case ESP_HTTPS_OTA_CONNECTED:
LOGGER_INFO("Connected to server");
break;
case ESP_HTTPS_OTA_GET_IMG_DESC:
LOGGER_INFO("Reading image description");
break;
case ESP_HTTPS_OTA_VERIFY_CHIP_ID:
LOGGER_INFO("Verifying chip id of new image: %d", *(esp_chip_id_t *)event_data);
break;
case ESP_HTTPS_OTA_DECRYPT_CB:
LOGGER_INFO("Callback to decrypt function");
break;
case ESP_HTTPS_OTA_WRITE_FLASH:
{
int written = *(int *)event_data;
LOGGER_INFO("Writing to flash: %d written", written);
// ledRing->ledOn(((ledRing->nrOfLeds - 1) * written) / imageSize);
break;
}
case ESP_HTTPS_OTA_UPDATE_BOOT_PARTITION:
LOGGER_INFO("Boot partition updated. Next Partition: %d", *(esp_partition_subtype_t*)event_data);
break;
case ESP_HTTPS_OTA_FINISH:
LOGGER_INFO("OTA finished");
break;
case ESP_HTTPS_OTA_ABORT:
LOGGER_INFO("OTA aborted");
break;
}
}
}
void ota::task(void)
{
esp_err_t err = ESP_OK;
esp_https_ota_handle_t https_ota_handle = NULL;
static esp_http_client_config_t config;
esp_app_desc_t app_desc;
config.timeout_ms = 60000;
config.keep_alive_enable = true;
config.crt_bundle_attach = esp_crt_bundle_attach;
esp_https_ota_config_t ota_config =
{
.http_config = &config,
.http_client_init_cb = NULL,
.bulk_flash_erase = true,
.partial_http_download = false,
.max_http_request_size = 4096,
};
LOGGER_INFO("OTA task executing");
// First try to download specific firmware
config.url = "http://10.10.1.32:8989/code.bin";
LOGGER_INFO("Trying URL %s", config.url);
err = esp_https_ota_begin(&ota_config, &https_ota_handle);
if(err != ESP_OK)
{
LOGGER_ERROR("OTA connection failed");
esp_https_ota_abort(https_ota_handle);
// No specific firmware found, try generic firmware
config.url = "http://10.10.1.32:8989/code.bin";
LOGGER_INFO("Trying URL %s", config.url);
err = esp_https_ota_begin(&ota_config, &https_ota_handle);
if(err != ESP_OK)
{
LOGGER_ERROR("OTA connection failed");
esp_https_ota_abort(https_ota_handle);
}
}
if(err == ESP_OK)
{
err = esp_https_ota_get_img_desc(https_ota_handle, &app_desc);
if(err != ESP_OK)
{
LOGGER_ERROR("Failed to read image header");
esp_https_ota_abort(https_ota_handle);
}
}
if(err == ESP_OK)
{
imageSize = esp_https_ota_get_image_size(https_ota_handle);
LOGGER_INFO("Image is %d bytes large", imageSize);
if(imageSize == -1)
{
err = ESP_FAIL;
esp_https_ota_abort(https_ota_handle);
}
}
}