Skip to content

Commit c1ef8a2

Browse files
committed
Added support for cmd_store
Also, added generic `reboot` command which uses the command store
1 parent 3022a5c commit c1ef8a2

File tree

1 file changed

+107
-1
lines changed

1 file changed

+107
-1
lines changed

components/esp_insights/src/esp_insights_cmd_resp.c

+107-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static const char *TAG = "insights_cmd_resp";
2424

2525
#include "esp_insights_internal.h"
2626
#include "esp_insights_cbor_decoder.h"
27+
#include "esp_insights_cbor_encoder.h"
2728

2829
#define INS_CONF_STR "config"
2930
#define RMAKER_CFG_TOPIC_SUFFIX "config"
@@ -34,17 +35,116 @@ static const char *TAG = "insights_cmd_resp";
3435
*/
3536
#define INSIGHTS_CONF_CMD 0x101
3637

37-
#define SCRATCH_BUF_SIZE (1 * 1024)
38+
/* depth is dictated by cmd_depth */
3839
#define MAX_CMD_DEPTH 10
40+
#define CMD_STORE_SIZE 10
41+
#define SCRATCH_BUF_SIZE (1 * 1024)
42+
43+
typedef esp_err_t (*esp_insights_cmd_cb_t)(const void *data, size_t data_len, const void *priv);
44+
45+
typedef struct {
46+
const char *cmd[MAX_CMD_DEPTH]; /* complete path of the command */
47+
int depth;
48+
esp_insights_cmd_cb_t cb; /* callback function */
49+
void *prv_data; /* pointer to the private data */
50+
} generic_cmd_t;
3951

4052
typedef struct {
53+
generic_cmd_t cmd_store[CMD_STORE_SIZE];
4154
int cmd_cnt; /* total registered commands */
4255
uint8_t *scratch_buf;
4356
bool enabled;
4457
bool init_done;
4558
} insights_cmd_resp_data_t;
4659

4760
static insights_cmd_resp_data_t s_cmd_resp_data;
61+
static bool reboot_report_pending = false;
62+
63+
static esp_err_t reboot_cmd_handler(const void *data, size_t data_len, const void *prv_data)
64+
{
65+
reboot_report_pending = true;
66+
ESP_LOGI(TAG, "rebooting in 5 seconds...");
67+
esp_rmaker_reboot(5);
68+
return ESP_OK;
69+
}
70+
71+
static void __collect_reboot_meta(CborEncoder *map)
72+
{
73+
cbor_encode_text_stringz(map, "reboot"); /* name of the config */
74+
CborEncoder conf_map, conf_data_map;
75+
cbor_encoder_create_map(map, &conf_map, CborIndefiniteLength);
76+
cbor_encode_text_stringz(&conf_map, "c"); /* denotes this to be config */
77+
cbor_encoder_create_map(&conf_map, &conf_data_map, CborIndefiniteLength);
78+
cbor_encode_text_stringz(&conf_data_map, "type");
79+
cbor_encode_uint(&conf_data_map, ESP_DIAG_DATA_TYPE_NULL); /* data_type */
80+
cbor_encoder_close_container(&conf_map, &conf_data_map);
81+
cbor_encoder_close_container(map, &conf_map);
82+
}
83+
84+
static void __collect_reboot_data(CborEncoder *map)
85+
{
86+
CborEncoder conf_map, key_arr;
87+
cbor_encoder_create_map(map, &conf_map, CborIndefiniteLength);
88+
cbor_encode_text_stringz(&conf_map, "n");
89+
cbor_encoder_create_array(&conf_map, &key_arr, CborIndefiniteLength);
90+
cbor_encode_text_stringz(&key_arr, "reboot");
91+
cbor_encoder_close_container(&conf_map, &key_arr);
92+
cbor_encode_text_stringz(&conf_map, "t");
93+
cbor_encode_uint(&conf_map, esp_diag_timestamp_get());
94+
cbor_encoder_close_container(map, &conf_map);
95+
}
96+
97+
static void esp_insights_cbor_reboot_msg_cb(CborEncoder *map, insights_msg_type_t type)
98+
{
99+
if (type == INSIGHTS_MSG_TYPE_META) {
100+
__collect_reboot_meta(map);
101+
} else if (reboot_report_pending) {
102+
__collect_reboot_data(map);
103+
reboot_report_pending = false;
104+
}
105+
}
106+
107+
esp_err_t esp_insights_cmd_resp_register_cmd(esp_insights_cmd_cb_t cb, void *prv_data, int cmd_depth, ...)
108+
{
109+
int idx = s_cmd_resp_data.cmd_cnt;
110+
111+
va_list valist;
112+
va_start(valist, cmd_depth);
113+
for (int i = 0; i < cmd_depth; i++) {
114+
s_cmd_resp_data.cmd_store[idx].cmd[i] = va_arg(valist, char *);
115+
}
116+
/* clean memory reserved for valist */
117+
va_end(valist);
118+
119+
s_cmd_resp_data.cmd_store[idx].depth = cmd_depth;
120+
s_cmd_resp_data.cmd_store[idx].prv_data = prv_data;
121+
s_cmd_resp_data.cmd_store[idx].cb = cb;
122+
s_cmd_resp_data.cmd_cnt += 1;
123+
124+
return ESP_OK;
125+
}
126+
127+
static esp_err_t insights_cmd_resp_search_execute_cmd_store(char **cmd_tree, int cmd_depth)
128+
{
129+
for(int i = 0; i< s_cmd_resp_data.cmd_cnt; i++) {
130+
if (cmd_depth == s_cmd_resp_data.cmd_store[i].depth) {
131+
bool match_found = true;
132+
/* the command depth matches, now go for whole path */
133+
for (int j = 0; j < cmd_depth; j++) {
134+
if (strcmp(cmd_tree[j], s_cmd_resp_data.cmd_store[i].cmd[j]) != 0) {
135+
match_found = false;
136+
break; /* break at first mismatch */
137+
}
138+
}
139+
if (match_found) {
140+
ESP_LOGI(TAG, "match found in cmd_store... Executing the callback");
141+
s_cmd_resp_data.cmd_store[i].cb(NULL, 0, s_cmd_resp_data.cmd_store[i].prv_data);
142+
return ESP_OK;
143+
}
144+
}
145+
}
146+
return ESP_ERR_NOT_FOUND;
147+
}
48148

49149
static void insights_cmd_parser_clear_cmd_tree(char *cmd_tree[])
50150
{
@@ -259,7 +359,10 @@ static esp_err_t esp_insights_cmd_resp_parse_one_entry(cbor_parse_ctx_t *ctx)
259359
break;
260360
}
261361
}
362+
363+
insights_cmd_resp_search_execute_cmd_store(cmd_tree, cmd_depth);
262364
insights_cmd_parser_clear_cmd_tree(cmd_tree);
365+
263366
return ret;
264367
}
265368

@@ -538,6 +641,9 @@ esp_err_t esp_insights_cmd_resp_enable(void)
538641
}
539642

540643
esp_insights_cbor_encoder_register_meta_cb(&esp_insights_cbor_reboot_msg_cb);
644+
/* register `reboot` command to our commands store */
645+
esp_insights_cmd_resp_register_cmd(reboot_cmd_handler, NULL, 1, "reboot");
646+
541647
ESP_LOGI(TAG, "Enabling Command-Response Module.");
542648

543649
/* Register our config parsing command to cmd_resp module */

0 commit comments

Comments
 (0)