3434#define ESP_RMAKER_NVS_PART_NAME "nvs"
3535#define MAX_PUBLISH_TOPIC_LEN 64
3636
37- static char publish_payload [CONFIG_ESP_RMAKER_MAX_PARAM_DATA_SIZE ];
37+ #define MAX_NODE_PARAMS_SIZE CONFIG_ESP_RMAKER_MAX_PARAM_DATA_SIZE
38+ static char publish_payload [MAX_NODE_PARAMS_SIZE ];
3839static char publish_topic [MAX_PUBLISH_TOPIC_LEN ];
3940
4041static const char * TAG = "esp_rmaker_param" ;
@@ -93,10 +94,10 @@ esp_rmaker_param_val_t esp_rmaker_array(const char *val)
9394 return param_val ;
9495}
9596
96- static esp_err_t esp_rmaker_report_params ( uint8_t flags , bool init )
97+ static esp_err_t esp_rmaker_populate_params ( char * buf , size_t buf_len , uint8_t flags , bool reset_flags )
9798{
9899 json_gen_str_t jstr ;
99- json_gen_str_start (& jstr , publish_payload , sizeof ( publish_payload ) , NULL , NULL );
100+ json_gen_str_start (& jstr , buf , buf_len , NULL , NULL );
100101 json_gen_start_object (& jstr );
101102 _esp_rmaker_device_t * device = esp_rmaker_node_get_first_device (esp_rmaker_get_node ());
102103 while (device ) {
@@ -109,7 +110,9 @@ static esp_err_t esp_rmaker_report_params(uint8_t flags, bool init)
109110 device_added = true;
110111 }
111112 esp_rmaker_report_value (& param -> val , param -> name , & jstr );
112- param -> flags &= ~flags ;
113+ if (reset_flags ) {
114+ param -> flags &= ~flags ;
115+ }
113116 }
114117 param = param -> next ;
115118 }
@@ -119,34 +122,63 @@ static esp_err_t esp_rmaker_report_params(uint8_t flags, bool init)
119122 device = device -> next ;
120123 }
121124 if (json_gen_end_object (& jstr ) < 0 ) {
122- ESP_LOGE (TAG , "Buffer size %d not sufficient for Node Params.\n"
123- "Please increase CONFIG_ESP_RMAKER_MAX_PARAM_DATA_SIZE" ,
124- CONFIG_ESP_RMAKER_MAX_PARAM_DATA_SIZE );
125+ return ESP_ERR_NO_MEM ;
125126 }
126127 json_gen_str_end (& jstr );
127- /* Just checking if there is any data to send by comparing with a decent enough
128- * length as even the smallest possible data, Eg. '{"d":{"p":0}}' will be > 10 bytes
129- */
130- if (strlen (publish_payload ) > 10 ) {
131- snprintf (publish_topic , sizeof (publish_topic ), "node/%s/%s" , esp_rmaker_get_node_id (),
132- init ? NODE_PARAMS_LOCAL_INIT_TOPIC_SUFFIX : NODE_PARAMS_LOCAL_TOPIC_SUFFIX );
133- ESP_LOGI (TAG , "Reporting params: %s" , publish_payload );
134- esp_rmaker_mqtt_publish (publish_topic , publish_payload , strlen (publish_payload ));
135- }
136128 return ESP_OK ;
137129}
138130
131+ char * esp_rmaker_get_node_params (void )
132+ {
133+ char * node_params = calloc (1 , MAX_NODE_PARAMS_SIZE );
134+ if (!node_params ) {
135+ ESP_LOGE (TAG , "Failed to allocate %d bytes for Node params." , MAX_NODE_PARAMS_SIZE );
136+ return NULL ;
137+ }
138+ if (esp_rmaker_populate_params (node_params , MAX_NODE_PARAMS_SIZE , 0 , false) == ESP_OK ) {
139+ return node_params ;
140+ }
141+ return NULL ;
142+ }
143+
139144esp_err_t esp_rmaker_report_param_internal (void )
140145{
141- return esp_rmaker_report_params (RMAKER_PARAM_FLAG_VALUE_CHANGE , false);
146+ esp_err_t err = esp_rmaker_populate_params (publish_payload , sizeof (publish_payload ),
147+ RMAKER_PARAM_FLAG_VALUE_CHANGE , true);
148+ if (err == ESP_OK ) {
149+ /* Just checking if there are indeed any params to report by comparing with a decent enough
150+ * length as even the smallest possible data, Eg. '{"d":{"p":0}}' will be > 10 bytes.
151+ */
152+ if (strlen (publish_payload ) > 10 ) {
153+ snprintf (publish_topic , sizeof (publish_topic ), "node/%s/%s" ,
154+ esp_rmaker_get_node_id (), NODE_PARAMS_LOCAL_TOPIC_SUFFIX );
155+ ESP_LOGI (TAG , "Reporting params: %s" , publish_payload );
156+ esp_rmaker_mqtt_publish (publish_topic , publish_payload , strlen (publish_payload ));
157+ }
158+ return ESP_OK ;
159+ }
160+ return err ;
142161}
143162
144163esp_err_t esp_rmaker_report_node_state (void )
145164{
146- return esp_rmaker_report_params (0 , true);
165+ esp_err_t err = esp_rmaker_populate_params (publish_payload , sizeof (publish_payload ), 0 , false);
166+ if (err == ESP_OK ) {
167+ /* Just checking if there are indeed any params to report by comparing with a decent enough
168+ * length as even the smallest possible data, Eg. '{"d":{"p":0}}' will be > 10 bytes.
169+ */
170+ if (strlen (publish_payload ) > 10 ) {
171+ snprintf (publish_topic , sizeof (publish_topic ), "node/%s/%s" ,
172+ esp_rmaker_get_node_id (), NODE_PARAMS_LOCAL_INIT_TOPIC_SUFFIX );
173+ ESP_LOGI (TAG , "Reporting params (init): %s" , publish_payload );
174+ esp_rmaker_mqtt_publish (publish_topic , publish_payload , strlen (publish_payload ));
175+ }
176+ return ESP_OK ;
177+ }
178+ return err ;
147179}
148180
149- static esp_err_t esp_rmaker_handle_get_params (_esp_rmaker_device_t * device , jparse_ctx_t * jptr )
181+ static esp_err_t esp_rmaker_device_set_params (_esp_rmaker_device_t * device , jparse_ctx_t * jptr , esp_rmaker_req_src_t src )
150182{
151183 _esp_rmaker_param_t * param = device -> params ;
152184 while (param ) {
@@ -224,7 +256,7 @@ static esp_err_t esp_rmaker_handle_get_params(_esp_rmaker_device_t *device, jpar
224256 esp_rmaker_param_update_and_report ((esp_rmaker_param_t * )param , new_val );
225257 } else if (device -> write_cb ) {
226258 esp_rmaker_write_ctx_t ctx = {
227- .src = ESP_RMAKER_REQ_SRC_CLOUD ,
259+ .src = src ,
228260 };
229261 if (device -> write_cb ((esp_rmaker_device_t * )device , (esp_rmaker_param_t * )param ,
230262 new_val , device -> priv_data , & ctx ) != ESP_OK ) {
@@ -243,23 +275,28 @@ static esp_err_t esp_rmaker_handle_get_params(_esp_rmaker_device_t *device, jpar
243275 return ESP_OK ;
244276}
245277
246- static void esp_rmaker_set_params_callback ( const char * topic , void * payload , size_t payload_len , void * priv_data )
278+ esp_err_t esp_rmaker_handle_set_params ( char * data , size_t data_len , esp_rmaker_req_src_t src )
247279{
280+ ESP_LOGI (TAG , "Received params: %.*s" , data_len , data );
248281 jparse_ctx_t jctx ;
249- ESP_LOGI (TAG , "Received params: %.*s" , payload_len , (char * )payload );
250- if (json_parse_start (& jctx , (char * )payload , payload_len ) != 0 ) {
251- return ;
282+ if (json_parse_start (& jctx , data , data_len ) != 0 ) {
283+ return ESP_FAIL ;
252284 }
253285 _esp_rmaker_device_t * device = esp_rmaker_node_get_first_device (esp_rmaker_get_node ());
254286 while (device ) {
255287 if (json_obj_get_object (& jctx , device -> name ) == 0 ) {
256- esp_rmaker_handle_get_params (device , & jctx );
288+ esp_rmaker_device_set_params (device , & jctx , src );
257289 json_obj_leave_object (& jctx );
258290 }
259291 device = device -> next ;
260292 }
261-
262293 json_parse_end (& jctx );
294+ return ESP_OK ;
295+ }
296+
297+ static void esp_rmaker_set_params_callback (const char * topic , void * payload , size_t payload_len , void * priv_data )
298+ {
299+ esp_rmaker_handle_set_params ((char * )payload , payload_len , ESP_RMAKER_REQ_SRC_CLOUD );
263300}
264301
265302esp_err_t esp_rmaker_register_for_set_params (void )
0 commit comments