Skip to content
Snippets Groups Projects
Commit 48d39c9c authored by fxk8y's avatar fxk8y :spider:
Browse files

add ota handlerqueue

parent d908902c
No related branches found
No related tags found
1 merge request!1Feature/mqtt ota
...@@ -9,11 +9,14 @@ ...@@ -9,11 +9,14 @@
#define TAG "MQTT_OTA" #define TAG "MQTT_OTA"
void default_ota_event(ota_event_t event);
ota_callback_t ota_handler = default_ota_event;
esp_ota_handle_t ota_handle = 0; esp_ota_handle_t ota_handle = 0;
const esp_partition_t* partition = NULL; const esp_partition_t* partition = NULL;
void handle_ota_message(const multipart_message_t& message) { void handle_ota_message(const multipart_message_t& message) {
ESP_LOGI(TAG, "length=%d offset=%d", message.length, message.offset); ESP_LOGD(TAG, "length=%d offset=%d", message.length, message.offset);
esp_err_t err; esp_err_t err;
...@@ -21,18 +24,68 @@ void handle_ota_message(const multipart_message_t& message) { ...@@ -21,18 +24,68 @@ void handle_ota_message(const multipart_message_t& message) {
partition = esp_ota_get_next_update_partition(NULL); partition = esp_ota_get_next_update_partition(NULL);
err = esp_ota_begin(partition, message.total_length, &ota_handle); err = esp_ota_begin(partition, message.total_length, &ota_handle);
if (err != ESP_OK) ESP_LOGW(TAG, "BEGIN OTA FAILED!"); if (err != ESP_OK) ESP_LOGW(TAG, "BEGIN OTA FAILED!");
ota_event_t event = {
.state = start,
.error = err,
.bytes_written = 0,
.bytes_total = (uint32_t)message.total_length
};
ota_handler(event);
} }
err = esp_ota_write(ota_handle, (const void*) message.payload, message.length); err = esp_ota_write(ota_handle, (const void*) message.payload, message.length);
if (err != ESP_OK) ESP_LOGW(TAG, "WRITE OTA FAILED!"); if (err != ESP_OK) ESP_LOGW(TAG, "WRITE OTA FAILED!");
ota_event_t event = {
.state = progress,
.error = err,
.bytes_written = (uint32_t)(message.length + message.offset),
.bytes_total = (uint32_t)message.total_length
};
ota_handler(event);
if (message.offset + message.length == message.total_length) { // last ota message if (message.offset + message.length == message.total_length) { // last ota message
err = esp_ota_end(ota_handle); err = esp_ota_end(ota_handle);
if (err != ESP_OK) ESP_LOGW(TAG, "END OTA FAILED!"); if (err != ESP_OK) ESP_LOGW(TAG, "END OTA FAILED!");
ota_event_t event = {
.state = success,
.error = err,
.bytes_written = (uint32_t)message.total_length,
.bytes_total = (uint32_t)message.total_length
};
ota_handler(event);
err = esp_ota_set_boot_partition(partition); err = esp_ota_set_boot_partition(partition);
if (err != ESP_OK) ESP_LOGW(TAG, "FINALIZE OTA FAILED!"); if (err != ESP_OK) ESP_LOGW(TAG, "FINALIZE OTA FAILED!");
esp_restart();
} }
} }
void default_ota_event(ota_event_t event) {
switch(event.state) {
case start:
ESP_LOGI(TAG, "OTA Start");
break;
case progress: {
float pct = ((float)event.bytes_written / (float)event.bytes_total) * 100;
ESP_LOGI(TAG, "OTA Progress %d%% (%d/%d KiB)", (int)pct, event.bytes_written / 1024, event.bytes_total / 1024);
break;
}
case success:
ESP_LOGI(TAG, "OTA Successful");
break;
case error:
ESP_LOGW(TAG, "OTA Error 0x%x", event.error);
break;
}
}
void add_ota_callback(ota_callback_t handler) {
ota_callback_t old_ota_handler = ota_handler;
ota_handler = [old_ota_handler, handler](ota_event_t event){
old_ota_handler(event);
handler(event);
};
}
\ No newline at end of file
...@@ -5,5 +5,6 @@ ...@@ -5,5 +5,6 @@
void handle_ota_message(const multipart_message_t& message); void handle_ota_message(const multipart_message_t& message);
void add_ota_callback(ota_callback_t handler);
#endif #endif
...@@ -20,7 +20,17 @@ typedef struct { ...@@ -20,7 +20,17 @@ typedef struct {
const uint32_t total_length; const uint32_t total_length;
} multipart_message_t; } multipart_message_t;
typedef std::function<void(multipart_message_t)> binary_message_callback_t; // content, length, offset typedef std::function<void(multipart_message_t)> binary_message_callback_t;
enum ota_state_t { start, progress, success, error };
typedef struct {
const ota_state_t state;
const esp_err_t error;
const uint32_t bytes_written;
const uint32_t bytes_total;
} ota_event_t;
typedef std::function<void(ota_event_t)> ota_callback_t;
// network // network
void enable_wlan(); void enable_wlan();
...@@ -28,6 +38,8 @@ void enable_wlan(); ...@@ -28,6 +38,8 @@ void enable_wlan();
// mqtt // mqtt
void publish_message(const std::string& topic, const std::string& message); void publish_message(const std::string& topic, const std::string& message);
void add_message_callback(const std::string& topic, message_callback_t callback); void add_message_callback(const std::string& topic, message_callback_t callback);
void add_binary_message_callback(const std::string& topic, binary_message_callback_t callback);
void add_ota_callback(ota_callback_t handler);
// common IO // common IO
void enable_oled(); void enable_oled();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment