Skip to content

Commit b0da719

Browse files
committed
Collect and send config data from Insights
1 parent c1ef8a2 commit b0da719

8 files changed

+237
-21
lines changed

components/esp_insights/include/esp_insights.h

+25
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,31 @@ void esp_insights_disable(void);
202202
*/
203203
const char *esp_insights_get_node_id(void);
204204

205+
/**
206+
* @brief Check if insights reporting is enabled
207+
*
208+
* @return true reporting is on
209+
* @return false reporting is off
210+
*/
211+
bool esp_insights_is_reporting_enabled(void);
212+
213+
/**
214+
* @brief Turn on the Insights reporting
215+
*
216+
* @return esp_err_t ESP_OK on success, apt error otherwise
217+
*/
218+
esp_err_t esp_insights_reporting_enable();
219+
220+
/**
221+
* @brief Turn off the Insights repoting
222+
*
223+
* @return esp_err_t ESP_OK on success, apt error otherwise
224+
* @note meta message if changed and the boot message will still be
225+
* sent as this information is critical for Insights working with the
226+
* cloud. You may disable insight completely using esp_insights_disable
227+
*/
228+
esp_err_t esp_insights_reporting_disable();
229+
205230
/**
206231
* @brief Encode and parse the command directly using esp-insight's parser
207232
*

components/esp_insights/src/esp_insights.c

+137-17
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#include <freertos/FreeRTOS.h>
8+
#include <freertos/timers.h>
9+
#include <freertos/semphr.h>
10+
711
#include <string.h>
812
#include <esp_log.h>
913
#include <esp_wifi.h>
1014
#include <esp_core_dump.h>
11-
#include <freertos/FreeRTOS.h>
12-
#include <freertos/timers.h>
15+
1316
#include <nvs.h>
1417
#include <esp_crc.h>
15-
#include <freertos/semphr.h>
1618
#include <esp_diag_data_store.h>
1719
#include <esp_diagnostics.h>
1820
#include <esp_diagnostics_metrics.h>
@@ -29,6 +31,10 @@
2931
#include "esp_insights_encoder.h"
3032
#include "esp_insights_cbor_decoder.h"
3133

34+
#ifdef CONFIG_ESP_INSIGHTS_CMD_RESP_ENABLED
35+
#define INSIGHTS_CMD_RESP 1
36+
#endif
37+
3238
#include <esp_idf_version.h>
3339
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
3440
#include <esp_mac.h>
@@ -65,10 +71,14 @@
6571
#define KEY_LOG_WR_FAIL "log_wr_fail"
6672

6773
#define DIAG_DATA_STORE_CRC_KEY "rtc_buf_sha"
68-
#define INSIGHTS_NVS_NAMESPACE "storage"
74+
#define INSIGHTS_NVS_NAMESPACE "storage"
6975

7076
ESP_EVENT_DEFINE_BASE(INSIGHTS_EVENT);
7177

78+
#ifdef CONFIG_ESP_INSIGHTS_ENABLED
79+
80+
static const char *TAG = "esp_insights"; /* tag for ESP_LOGx */
81+
7282
typedef struct esp_insights_entry {
7383
esp_rmaker_work_fn_t work_fn;
7484
TimerHandle_t timer;
@@ -87,6 +97,10 @@ typedef struct {
8797
char app_sha256[APP_ELF_SHA256_LEN];
8898
bool data_sent;
8999
#if SEND_INSIGHTS_META
100+
#if INSIGHTS_CMD_RESP
101+
bool conf_meta_msg_pending;
102+
uint32_t conf_meta_msg_id;
103+
#endif
90104
bool meta_msg_pending;
91105
uint32_t meta_msg_id;
92106
uint32_t meta_crc;
@@ -96,13 +110,13 @@ typedef struct {
96110
TimerHandle_t data_send_timer; /* timer to reset data_send_inprogress flag on timeout */
97111
char *node_id;
98112
int boot_msg_id; /* To track whether first message is sent or not, -1:failed, 0:success, >0:inprogress */
113+
#if INSIGHTS_CMD_RESP
114+
int conf_msg_id;
115+
#endif
99116
bool init_done; /* insights init done */
100117
bool enabled; /* insights enable is done */
101118
} esp_insights_data_t;
102119

103-
#ifdef CONFIG_ESP_INSIGHTS_ENABLED
104-
105-
static const char *TAG = "esp_insights";
106120
static esp_insights_data_t s_insights_data;
107121
static esp_insights_entry_t *s_periodic_insights_entry;
108122

@@ -128,6 +142,14 @@ static bool is_insights_active(void)
128142
return wifi_connected && s_insights_data.enabled;
129143
}
130144

145+
/* Returns true if wifi is connected, false otherwise */
146+
static bool is_wifi_connected(void)
147+
{
148+
wifi_ap_record_t ap_info;
149+
bool wifi_connected = esp_wifi_sta_get_ap_info(&ap_info) == ESP_OK;
150+
return wifi_connected;
151+
}
152+
131153
/* This executes in the context of timer task.
132154
*
133155
* There is a dynamic logic to decide the next instance when the insights
@@ -174,16 +196,17 @@ static esp_err_t esp_insights_unregister_periodic_handler(void)
174196
{
175197
if (s_periodic_insights_entry) {
176198
if (s_periodic_insights_entry->timer) {
199+
ESP_LOGI(TAG, "Stopping the periodic timer");
200+
if (xTimerIsTimerActive(s_periodic_insights_entry->timer) == pdTRUE) {
201+
xTimerStop(s_periodic_insights_entry->timer, portMAX_DELAY);
202+
}
177203
ESP_LOGI(TAG, "Deleting the periodic timer");
178-
if (xTimerDelete(s_periodic_insights_entry->timer, 10) != pdPASS) {
179-
ESP_LOGE(TAG, "Failed to delete the periodic timer");
180-
}
204+
xTimerDelete(s_periodic_insights_entry->timer, portMAX_DELAY);
205+
s_periodic_insights_entry->timer = NULL;
181206
}
182207

183-
if (s_periodic_insights_entry) {
184-
free(s_periodic_insights_entry);
185-
s_periodic_insights_entry = NULL;
186-
}
208+
free(s_periodic_insights_entry);
209+
s_periodic_insights_entry = NULL;
187210
}
188211

189212
return ESP_OK;
@@ -238,6 +261,11 @@ static void data_send_timeout_cb(TimerHandle_t handle)
238261
if (s_insights_data.boot_msg_id > 0) {
239262
s_insights_data.boot_msg_id = -1;
240263
}
264+
#if INSIGHTS_CMD_RESP
265+
if (s_insights_data.conf_msg_id > 0) {
266+
s_insights_data.conf_msg_id = -1;
267+
}
268+
#endif
241269
xSemaphoreGive(s_insights_data.data_lock);
242270
}
243271

@@ -275,6 +303,11 @@ static void insights_event_handler(void* arg, esp_event_base_t event_base,
275303
#endif // CONFIG_ESP_INSIGHTS_COREDUMP_ENABLE
276304
s_insights_data.boot_msg_id = 0;
277305
}
306+
#if INSIGHTS_CMD_RESP
307+
else if (s_insights_data.conf_msg_id > 0 && s_insights_data.conf_msg_id == data->msg_id) {
308+
s_insights_data.conf_msg_id = 0;
309+
}
310+
#endif
278311
xSemaphoreGive(s_insights_data.data_lock);
279312
}
280313
break;
@@ -287,6 +320,11 @@ static void insights_event_handler(void* arg, esp_event_base_t event_base,
287320
if (s_insights_data.boot_msg_id > 0 && data->msg_id == s_insights_data.boot_msg_id) {
288321
s_insights_data.boot_msg_id = -1;
289322
}
323+
#if INSIGHTS_CMD_RESP
324+
else if (s_insights_data.conf_msg_id > 0 && data->msg_id == s_insights_data.conf_msg_id) {
325+
s_insights_data.conf_msg_id = -1;
326+
}
327+
#endif
290328
xSemaphoreGive(s_insights_data.data_lock);
291329
break;
292330
default:
@@ -348,10 +386,19 @@ static void send_boottime_data(void)
348386
}
349387
}
350388

389+
#if INSIGHTS_CMD_RESP
390+
static void send_insights_conf_meta(void);
391+
static void send_insights_config(void)
392+
{
393+
send_insights_conf_meta();
394+
}
395+
#endif
396+
351397
#if SEND_INSIGHTS_META
352398
/* Returns true if ESP Insights metadata CRC is changed */
353399
static bool insights_meta_changed(void)
354400
{
401+
return true;
355402
uint32_t nvs_crc;
356403
uint32_t meta_crc = esp_diag_meta_crc_get();
357404
esp_err_t err = esp_insights_meta_nvs_crc_get(&nvs_crc);
@@ -396,6 +443,35 @@ static void send_insights_meta(void)
396443
}
397444
#endif /* SEND_INSIGHTS_META */
398445

446+
#if INSIGHTS_CMD_RESP
447+
static void send_insights_conf_meta(void)
448+
{
449+
uint16_t len = 0;
450+
451+
memset(s_insights_data.scratch_buf, 0, INSIGHTS_DATA_MAX_SIZE);
452+
len = esp_insights_encode_conf_meta(s_insights_data.scratch_buf, INSIGHTS_DATA_MAX_SIZE, s_insights_data.app_sha256);
453+
if (len == 0) {
454+
#if INSIGHTS_DEBUG_ENABLED
455+
ESP_LOGI(TAG, "No conf metadata to send");
456+
#endif
457+
return;
458+
}
459+
#if INSIGHTS_DEBUG_ENABLED
460+
ESP_LOGI(TAG, "Insights conf meta data length %d", len);
461+
insights_dbg_dump(s_insights_data.scratch_buf, len);
462+
#endif
463+
int msg_id = esp_insights_transport_data_send(s_insights_data.scratch_buf, len);
464+
if (msg_id > 0) {
465+
xSemaphoreTake(s_insights_data.data_lock, portMAX_DELAY);
466+
s_insights_data.conf_meta_msg_pending = true;
467+
s_insights_data.conf_meta_msg_id = msg_id;
468+
xSemaphoreGive(s_insights_data.data_lock);
469+
} else if (msg_id == 0) { /* sent successfully */
470+
s_insights_data.conf_meta_msg_pending = false;
471+
}
472+
}
473+
#endif
474+
399475
/* Consider 100 bytes are published and received on cloud but RMAKER_MQTT_EVENT_PUBLISHED
400476
* event is not received for 100 bytes. In a mean time 50 bytes are added to the buffer.
401477
* When the next time timer expires then old 100 bytes plus new 50 bytes will be published
@@ -477,6 +553,13 @@ static void send_insights_data(void)
477553
xSemaphoreGive(s_insights_data.data_lock);
478554
}
479555

556+
#if INSIGHTS_CMD_RESP
557+
static void __insights_report_config_update(void *priv_data)
558+
{
559+
send_insights_config();
560+
}
561+
#endif
562+
480563
static void insights_periodic_handler(void *priv_data)
481564
{
482565
xSemaphoreTake(s_insights_data.data_lock, portMAX_DELAY);
@@ -495,24 +578,58 @@ static void insights_periodic_handler(void *priv_data)
495578
#if SEND_INSIGHTS_META
496579
if (insights_meta_changed()) {
497580
send_insights_meta();
581+
#if INSIGHTS_CMD_RESP
582+
send_insights_conf_meta();
583+
#endif
498584
}
499585
#endif /* SEND_INSIGHTS_META */
586+
587+
#if INSIGHTS_CMD_RESP
588+
xSemaphoreTake(s_insights_data.data_lock, portMAX_DELAY);
589+
if (s_insights_data.conf_msg_id == -1) {
590+
xSemaphoreGive(s_insights_data.data_lock);
591+
send_insights_config();
592+
} else {
593+
xSemaphoreGive(s_insights_data.data_lock);
594+
}
595+
#endif
596+
xSemaphoreTake(s_insights_data.data_lock, portMAX_DELAY);
500597
if (s_insights_data.boot_msg_id == -1) {
598+
xSemaphoreGive(s_insights_data.data_lock);
501599
send_boottime_data();
600+
} else {
601+
xSemaphoreGive(s_insights_data.data_lock);
502602
}
503603
send_insights_data();
504604
}
505605

506606
esp_err_t esp_insights_send_data(void)
507607
{
508-
if (is_insights_active() == true) {
608+
if (is_wifi_connected() == true) {
509609
ESP_LOGI(TAG, "Sending data to cloud");
510610
return esp_rmaker_work_queue_add_task(insights_periodic_handler, NULL);
511611
}
512612
ESP_LOGW(TAG, "Wi-Fi not in connected state");
513613
return ESP_FAIL;
514614
}
515615

616+
#if INSIGHTS_CMD_RESP
617+
void esp_insights_report_config_update(void)
618+
{
619+
s_insights_data.conf_msg_id = -1;
620+
if (is_wifi_connected() == true) {
621+
/* if wifi is connected, immediately send the report */
622+
/* if not, this will be reported from periodic handler */
623+
esp_rmaker_work_queue_add_task(__insights_report_config_update, NULL);
624+
}
625+
}
626+
#else
627+
void esp_insights_report_config_update(void)
628+
{
629+
ESP_LOGI(TAG, "Not reporting config when cmd_resp is not enabled");
630+
}
631+
#endif
632+
516633
static void data_store_event_handler(void* arg, esp_event_base_t event_base,
517634
int32_t event_id, void* event_data)
518635
{
@@ -633,7 +750,7 @@ static void variables_init(void)
633750
ESP_LOGW(TAG, "Failed to initialize network variables");
634751
}
635752
#endif /* CONFIG_DIAG_ENABLE_NETWORK_VARIABLES */
636-
esp_diag_variable_register("diag", KEY_LOG_WR_FAIL, "Log write fail count", "Diagnostics.Log", ESP_DIAG_DATA_TYPE_UINT);
753+
esp_diag_variable_register(TAG_DIAG, KEY_LOG_WR_FAIL, "Log write fail count", "Diagnostics.Log", ESP_DIAG_DATA_TYPE_UINT);
637754
return;
638755
}
639756
ESP_LOGE(TAG, "Failed to initialize param-values.");
@@ -855,6 +972,9 @@ esp_err_t esp_insights_enable(esp_insights_config_t *config)
855972
#endif /* CONFIG_DIAG_ENABLE_VARIABLES */
856973

857974
s_insights_data.boot_msg_id = -1;
975+
#if INSIGHTS_CMD_RESP
976+
s_insights_data.conf_msg_id = -1;
977+
#endif
858978
s_insights_data.data_send_timer = xTimerCreate("data_send_timer", CLOUD_REPORTING_TIMEOUT_TICKS,
859979
pdFALSE, NULL, data_send_timeout_cb);
860980
if (!s_insights_data.data_send_timer) {

components/esp_insights/src/esp_insights_cbor_encoder.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#define METRICS_PATH_VALUE "M"
2727
#define VARIABLES_PATH_VALUE "P"
2828

29-
static CborEncoder s_encoder, s_result_map, s_diag_map, s_diag_data_map;
29+
static CborEncoder s_encoder, s_result_map, s_diag_map, s_diag_data_map, s_diag_conf_map;
3030
static CborEncoder s_meta_encoder, s_meta_result_map, s_diag_meta_map, s_diag_meta_data_map;
3131

3232
#define CBOR_ENC_MAX_CBS 10
@@ -81,11 +81,22 @@ void esp_insights_cbor_encode_diag_data_begin(void)
8181
cbor_encoder_create_map(&s_diag_map, &s_diag_data_map, CborIndefiniteLength);
8282
}
8383

84+
void esp_insights_cbor_encode_diag_conf_data_begin(void)
85+
{
86+
cbor_encode_text_stringz(&s_diag_data_map, "configs");
87+
cbor_encoder_create_array(&s_diag_data_map, &s_diag_conf_map, CborIndefiniteLength);
88+
}
89+
8490
void esp_insights_cbor_encode_diag_data_end(void)
8591
{
8692
cbor_encoder_close_container(&s_diag_map, &s_diag_data_map);
8793
}
8894

95+
void esp_insights_cbor_encode_diag_conf_data_end(void)
96+
{
97+
cbor_encoder_close_container(&s_diag_data_map, &s_diag_conf_map);
98+
}
99+
89100
#if CONFIG_ESP_INSIGHTS_COREDUMP_ENABLE
90101
void esp_insights_cbor_encode_diag_crash(esp_core_dump_summary_t *summary)
91102
{

components/esp_insights/src/esp_insights_cbor_encoder.h

+11
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,14 @@ void esp_insights_cbor_encode_meta_variables(const esp_diag_variable_meta_t *var
7575
#endif /* CONFIG_DIAG_ENABLE_VARIABLES */
7676
void esp_insights_cbor_encode_meta_data_end(void);
7777
size_t esp_insights_cbor_encode_meta_end(void *data);
78+
79+
80+
/* For encoding conf data */
81+
void esp_insights_cbor_encode_conf_meta_begin(void *data, size_t data_size, const char *version, const char *sha256);
82+
void esp_insights_cbor_encode_conf_meta_data_begin(void);
83+
void esp_insights_cbor_encode_conf_meta_data_end(void);
84+
size_t esp_insights_cbor_encode_conf_meta_end(void *data);
85+
86+
void esp_insights_cbor_encode_diag_conf_data_begin(void);
87+
void esp_insights_cbor_encode_diag_conf_data_end(void);
88+
void esp_insights_cbor_encode_diag_conf_data(void);

components/esp_insights/src/esp_insights_cmd_resp.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,7 @@ static esp_err_t esp_insights_cmd_resp_parse_execute(cbor_parse_ctx_t *ctx)
378378
cmd_cnt++;
379379
}
380380
if (cmd_cnt) {
381-
/* TODO: Report modified configs */
382-
// esp_insights_report_config_update();
381+
esp_insights_report_config_update();
383382
}
384383
esp_insights_cbor_decoder_exit_container(ctx);
385384
ESP_LOGI(TAG, "parsed and executed %d commands", cmd_cnt);

0 commit comments

Comments
 (0)