@@ -24,6 +24,7 @@ static const char *TAG = "insights_cmd_resp";
24
24
25
25
#include "esp_insights_internal.h"
26
26
#include "esp_insights_cbor_decoder.h"
27
+ #include "esp_insights_cbor_encoder.h"
27
28
28
29
#define INS_CONF_STR "config"
29
30
#define RMAKER_CFG_TOPIC_SUFFIX "config"
@@ -34,17 +35,116 @@ static const char *TAG = "insights_cmd_resp";
34
35
*/
35
36
#define INSIGHTS_CONF_CMD 0x101
36
37
37
- #define SCRATCH_BUF_SIZE (1 * 1024)
38
+ /* depth is dictated by cmd_depth */
38
39
#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 ;
39
51
40
52
typedef struct {
53
+ generic_cmd_t cmd_store [CMD_STORE_SIZE ];
41
54
int cmd_cnt ; /* total registered commands */
42
55
uint8_t * scratch_buf ;
43
56
bool enabled ;
44
57
bool init_done ;
45
58
} insights_cmd_resp_data_t ;
46
59
47
60
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
+ }
48
148
49
149
static void insights_cmd_parser_clear_cmd_tree (char * cmd_tree [])
50
150
{
@@ -259,7 +359,10 @@ static esp_err_t esp_insights_cmd_resp_parse_one_entry(cbor_parse_ctx_t *ctx)
259
359
break ;
260
360
}
261
361
}
362
+
363
+ insights_cmd_resp_search_execute_cmd_store (cmd_tree , cmd_depth );
262
364
insights_cmd_parser_clear_cmd_tree (cmd_tree );
365
+
263
366
return ret ;
264
367
}
265
368
@@ -538,6 +641,9 @@ esp_err_t esp_insights_cmd_resp_enable(void)
538
641
}
539
642
540
643
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
+
541
647
ESP_LOGI (TAG , "Enabling Command-Response Module." );
542
648
543
649
/* Register our config parsing command to cmd_resp module */
0 commit comments