Skip to content

Commit 0b3fadf

Browse files
authored
Merge pull request #98 from baba-dev/codex/audit-and-update-settings_controller-actions
Surface live settings action updates
2 parents 6943d33 + 86821a4 commit 0b3fadf

11 files changed

Lines changed: 582 additions & 49 deletions

File tree

components/diag/include/diag/diag.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,27 @@ extern "C"
2121
esp_mqtt_client_handle_t mqtt;
2222
} diag_handles_t;
2323

24-
esp_err_t diag_start(const app_cfg_t* cfg, diag_handles_t* handles);
24+
typedef enum
25+
{
26+
DIAG_EVENT_STARTING = 0,
27+
DIAG_EVENT_HTTP_READY,
28+
DIAG_EVENT_MQTT_STARTED,
29+
DIAG_EVENT_WARNING,
30+
DIAG_EVENT_ERROR,
31+
} diag_event_type_t;
32+
33+
typedef struct
34+
{
35+
diag_event_type_t type;
36+
esp_err_t error;
37+
} diag_event_t;
38+
39+
typedef void (*diag_event_cb_t)(const diag_event_t* event, void* user_data);
40+
41+
esp_err_t diag_start(const app_cfg_t* cfg,
42+
diag_handles_t* handles,
43+
diag_event_cb_t callback,
44+
void* user_data);
2545
void diag_stop(diag_handles_t* handles);
2646

2747
#ifdef __cplusplus

components/diag/src/diag.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,28 @@
77

88
#include <stdio.h>
99

10+
#include "esp_err.h"
1011
#include "esp_log.h"
1112
#include "esp_system.h"
1213
#include "esp_timer.h"
1314
#include "mdns.h"
1415

1516
static const char* TAG = "diag";
1617

18+
static void
19+
emit_diag_event(diag_event_cb_t callback, void* user_data, diag_event_type_t type, esp_err_t error)
20+
{
21+
if (callback == NULL)
22+
{
23+
return;
24+
}
25+
diag_event_t event = {
26+
.type = type,
27+
.error = error,
28+
};
29+
callback(&event, user_data);
30+
}
31+
1732
static esp_err_t diag_register_mdns(const app_cfg_t* cfg)
1833
{
1934
esp_err_t err = mdns_init();
@@ -49,13 +64,16 @@ static esp_err_t health_handler(httpd_req_t* req)
4964
return httpd_resp_send(req, payload, HTTPD_RESP_USE_STRLEN);
5065
}
5166

52-
esp_err_t diag_start(const app_cfg_t* cfg, diag_handles_t* handles)
67+
esp_err_t
68+
diag_start(const app_cfg_t* cfg, diag_handles_t* handles, diag_event_cb_t callback, void* user_data)
5369
{
5470
if (!cfg || !handles)
5571
{
5672
return ESP_ERR_INVALID_ARG;
5773
}
5874

75+
emit_diag_event(callback, user_data, DIAG_EVENT_STARTING, ESP_OK);
76+
5977
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
6078
config.uri_match_fn = httpd_uri_match_simple;
6179

@@ -64,9 +82,12 @@ esp_err_t diag_start(const app_cfg_t* cfg, diag_handles_t* handles)
6482
{
6583
ESP_LOGE(TAG, "Failed to start diagnostics server: 0x%x", (unsigned int)err);
6684
handles->httpd = NULL;
85+
emit_diag_event(callback, user_data, DIAG_EVENT_ERROR, err);
6786
return err;
6887
}
6988

89+
emit_diag_event(callback, user_data, DIAG_EVENT_HTTP_READY, ESP_OK);
90+
7091
httpd_uri_t health_uri = {
7192
.uri = "/health",
7293
.method = HTTP_GET,
@@ -83,13 +104,25 @@ esp_err_t diag_start(const app_cfg_t* cfg, diag_handles_t* handles)
83104
handles->mqtt = esp_mqtt_client_init(&mqtt_config);
84105
if (handles->mqtt)
85106
{
86-
esp_mqtt_client_start(handles->mqtt);
107+
esp_err_t mqtt_err = esp_mqtt_client_start(handles->mqtt);
108+
if (mqtt_err != ESP_OK)
109+
{
110+
ESP_LOGE(TAG, "MQTT start failed: 0x%x", (unsigned int)mqtt_err);
111+
emit_diag_event(callback, user_data, DIAG_EVENT_ERROR, mqtt_err);
112+
return mqtt_err;
113+
}
114+
emit_diag_event(callback, user_data, DIAG_EVENT_MQTT_STARTED, ESP_OK);
115+
}
116+
else
117+
{
118+
emit_diag_event(callback, user_data, DIAG_EVENT_WARNING, ESP_ERR_NO_MEM);
87119
}
88120

89121
err = diag_register_mdns(cfg);
90122
if (err != ESP_OK)
91123
{
92124
ESP_LOGW(TAG, "mDNS registration failed: 0x%x", (unsigned int)err);
125+
emit_diag_event(callback, user_data, DIAG_EVENT_WARNING, err);
93126
}
94127

95128
ESP_LOGI(TAG, "Diagnostics server ready");

components/ota_update/include/ota_update/ota_update.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#pragma once
77

88
#include <stdbool.h>
9+
#include <stddef.h>
910

1011
#include "esp_err.h"
1112

@@ -14,6 +15,28 @@ extern "C"
1415
{
1516
#endif
1617

18+
typedef enum
19+
{
20+
OTA_UPDATE_EVENT_START = 0,
21+
OTA_UPDATE_EVENT_PROGRESS,
22+
OTA_UPDATE_EVENT_COMPLETED,
23+
OTA_UPDATE_EVENT_ERROR,
24+
} ota_update_event_type_t;
25+
26+
typedef struct
27+
{
28+
ota_update_event_type_t type;
29+
size_t bytes_downloaded;
30+
size_t image_size;
31+
esp_err_t error;
32+
} ota_update_event_t;
33+
34+
typedef void (*ota_update_event_cb_t)(const ota_update_event_t* event, void* user_data);
35+
36+
esp_err_t ota_update_perform_with_callback(const char* url,
37+
bool reboot_on_success,
38+
ota_update_event_cb_t callback,
39+
void* user_data);
1740
esp_err_t ota_update_perform(const char* url, bool reboot_on_success);
1841

1942
#ifdef __cplusplus

components/ota_update/src/ota_update.c

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,39 @@
1515

1616
static const char* TAG = "ota_update";
1717

18-
esp_err_t ota_update_perform(const char* url, bool reboot_on_success)
18+
static void emit_event(ota_update_event_cb_t callback,
19+
void* user_data,
20+
ota_update_event_type_t type,
21+
size_t bytes_downloaded,
22+
size_t image_size,
23+
esp_err_t error)
24+
{
25+
if (callback == NULL)
26+
{
27+
return;
28+
}
29+
ota_update_event_t event = {
30+
.type = type,
31+
.bytes_downloaded = bytes_downloaded,
32+
.image_size = image_size,
33+
.error = error,
34+
};
35+
callback(&event, user_data);
36+
}
37+
38+
esp_err_t ota_update_perform_with_callback(const char* url,
39+
bool reboot_on_success,
40+
ota_update_event_cb_t callback,
41+
void* user_data)
1942
{
2043
if (!url)
2144
{
45+
emit_event(callback, user_data, OTA_UPDATE_EVENT_ERROR, 0U, 0U, ESP_ERR_INVALID_ARG);
2246
return ESP_ERR_INVALID_ARG;
2347
}
2448

49+
emit_event(callback, user_data, OTA_UPDATE_EVENT_START, 0U, 0U, ESP_OK);
50+
2551
esp_http_client_config_t http_config = {
2652
.url = url,
2753
.timeout_ms = 10000,
@@ -37,11 +63,16 @@ esp_err_t ota_update_perform(const char* url, bool reboot_on_success)
3763
if (err != ESP_OK)
3864
{
3965
ESP_LOGE(TAG, "OTA begin failed: 0x%x", (unsigned int)err);
66+
emit_event(callback, user_data, OTA_UPDATE_EVENT_ERROR, 0U, 0U, err);
4067
return err;
4168
}
4269

4370
while ((err = esp_https_ota_perform(ota_handle)) == ESP_ERR_HTTPS_OTA_IN_PROGRESS)
4471
{
72+
size_t image_size = esp_https_ota_get_image_size(ota_handle);
73+
size_t bytes_downloaded = esp_https_ota_get_image_len_read(ota_handle);
74+
emit_event(
75+
callback, user_data, OTA_UPDATE_EVENT_PROGRESS, bytes_downloaded, image_size, ESP_OK);
4576
vTaskDelay(pdMS_TO_TICKS(100));
4677
}
4778

@@ -52,19 +83,52 @@ esp_err_t ota_update_perform(const char* url, bool reboot_on_success)
5283
else
5384
{
5485
ESP_LOGE(TAG, "OTA perform failed: 0x%x", (unsigned int)err);
86+
emit_event(callback,
87+
user_data,
88+
OTA_UPDATE_EVENT_ERROR,
89+
esp_https_ota_get_image_len_read(ota_handle),
90+
esp_https_ota_get_image_size(ota_handle),
91+
err);
5592
}
5693

5794
esp_err_t finish_err = esp_https_ota_finish(ota_handle);
5895
if (finish_err != ESP_OK)
5996
{
6097
ESP_LOGE(TAG, "OTA finish failed: 0x%x", (unsigned int)finish_err);
98+
emit_event(callback,
99+
user_data,
100+
OTA_UPDATE_EVENT_ERROR,
101+
esp_https_ota_get_image_len_read(ota_handle),
102+
esp_https_ota_get_image_size(ota_handle),
103+
finish_err);
61104
return finish_err;
62105
}
63106

64107
if (err == ESP_OK && reboot_on_success)
65108
{
66109
ESP_LOGI(TAG, "Rebooting after OTA update");
110+
emit_event(callback,
111+
user_data,
112+
OTA_UPDATE_EVENT_COMPLETED,
113+
esp_https_ota_get_image_len_read(ota_handle),
114+
esp_https_ota_get_image_size(ota_handle),
115+
ESP_OK);
67116
esp_restart();
68117
}
118+
else if (err == ESP_OK)
119+
{
120+
emit_event(callback,
121+
user_data,
122+
OTA_UPDATE_EVENT_COMPLETED,
123+
esp_https_ota_get_image_len_read(ota_handle),
124+
esp_https_ota_get_image_size(ota_handle),
125+
ESP_OK);
126+
}
127+
69128
return err;
70129
}
130+
131+
esp_err_t ota_update_perform(const char* url, bool reboot_on_success)
132+
{
133+
return ota_update_perform_with_callback(url, reboot_on_success, NULL, NULL);
134+
}

0 commit comments

Comments
 (0)