Improve ACLK message parsing (#20838)
No need to allocate/free type_id, check immediately and set has_type, is_http
Stelios Fragkakis committed
Aug 18, 2025 at 19:06 UTC
00cb44c2496db4c80b9c29ef2094982f9c357c76
1 file changed
+8
-9
src/aclk/aclk_rx_msgs.c
+8
-9
@@ -15,7 +15,8 @@
15
#define ACLK_V_COMPRESSION 2
16
17
struct aclk_request {
18
- char *type_id;
18
+ bool has_type;
19
+ bool is_http;
20
char *msg_id;
21
char *callback_topic;
22
char *payload;
@@ -35,15 +36,16 @@ static int cloud_to_agent_parse(JSON_ENTRY *e)
36
break;
37
case JSON_STRING:
38
if (!strcmp(e->name, "msg-id")) {
38
- data->msg_id = strdupz(e->data.string);
39
+ data->msg_id = e->data.string ? strdupz(e->data.string) : NULL;
40
break;
41
}
42
if (!strcmp(e->name, "type")) {
42
- data->type_id = strdupz(e->data.string);
43
+ data->has_type = true;
44
+ data->is_http = (e->data.string && (strcmp(e->data.string, "http") == 0));
45
break;
46
}
47
if (!strcmp(e->name, "callback-topic")) {
46
- data->callback_topic = strdupz(e->data.string);
48
+ data->callback_topic = e->data.string ? strdupz(e->data.string) : NULL;
49
break;
50
}
51
if (!strcmp(e->name, "payload")) {
@@ -190,14 +192,14 @@ int aclk_handle_cloud_cmd_message(char *payload)
192
goto err_cleanup;
193
}
194
193
- if (!cloud_to_agent.type_id) {
195
+ if (!cloud_to_agent.has_type) {
196
error_report("Cloud message is missing compulsory key \"type\"");
197
goto err_cleanup;
198
}
199
200
// Originally we were expecting to have multiple types of 'cmd' message,
201
// but after the new protocol was designed we will ever only have 'http'
200
- if (strcmp(cloud_to_agent.type_id, "http") != 0) {
202
+ if (!cloud_to_agent.is_http) {
203
error_report("Only 'http' cmd message is supported");
204
goto err_cleanup;
205
}
@@ -205,15 +207,12 @@ int aclk_handle_cloud_cmd_message(char *payload)
207
if (likely(!aclk_handle_cloud_http_request_v2(&cloud_to_agent, payload))) {
208
// aclk_handle_cloud_request takes ownership of the pointers
209
// (to avoid copying) in case of success
208
- freez(cloud_to_agent.type_id);
210
return 0;
211
}
212
213
err_cleanup:
214
if (cloud_to_agent.payload)
215
freez(cloud_to_agent.payload);
215
- if (cloud_to_agent.type_id)
216
- freez(cloud_to_agent.type_id);
216
if (cloud_to_agent.msg_id)
217
freez(cloud_to_agent.msg_id);
218
if (cloud_to_agent.callback_topic)