1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 #include "iotx_dm_internal.h"
6 
7 const char DM_URI_SYS_PREFIX[] DM_READ_ONLY = "/sys/%s/%s/";
8 const char DM_URI_EXT_SESSION_PREFIX[] DM_READ_ONLY = "/ext/session/%s/%s/";
9 const char DM_URI_EXT_NTP_PREFIX[] DM_READ_ONLY = "/ext/ntp/%s/%s/";
10 const char DM_URI_EXT_ERROR_PREFIX[] DM_READ_ONLY = "/ext/error/%s/%s";
11 const char DM_URI_REPLY_SUFFIX[] DM_READ_ONLY = "_reply";
12 const char DM_URI_OTA_DEVICE_INFORM[] DM_READ_ONLY = "/ota/device/inform/%s/%s";
13 const char DM_URI_THING_EVENT_NOTIFY[] DM_READ_ONLY = "_thing/event/notify";
14 const char DM_URI_THING_EVENT_NOTIFY_REPLY[] DM_READ_ONLY =
15     "_thing/event/notify_reply";
16 
17 /* From Cloud To Local Request And Response*/
18 const char DM_URI_THING_MODEL_DOWN_RAW[] DM_READ_ONLY = "thing/model/down_raw";
19 const char DM_URI_THING_MODEL_DOWN_RAW_REPLY[] DM_READ_ONLY =
20     "thing/model/down_raw_reply";
21 
22 /* From Local To Cloud Request And Response*/
23 const char DM_URI_THING_MODEL_UP_RAW[] DM_READ_ONLY = "thing/model/up_raw";
24 const char DM_URI_THING_MODEL_UP_RAW_REPLY[] DM_READ_ONLY =
25     "thing/model/up_raw_reply";
26 
27 #if !defined(DEVICE_MODEL_RAWDATA_SOLO)
28 const char DM_URI_RRPC_REQUEST_WILDCARD[] DM_READ_ONLY = "rrpc/request/+";
29 
30 /* From Cloud To Local Request And Response*/
31 const char DM_URI_THING_SERVICE_PROPERTY_SET[] DM_READ_ONLY =
32     "thing/service/property/set";
33 const char DM_URI_THING_SERVICE_PROPERTY_SET_REPLY[] DM_READ_ONLY =
34     "thing/service/property/set_reply";
35 const char DM_URI_THING_SERVICE_PROPERTY_GET[] DM_READ_ONLY =
36     "thing/service/property/get";
37 const char DM_URI_THING_SERVICE_PROPERTY_GET_REPLY[] DM_READ_ONLY =
38     "thing/service/property/get_reply";
39 const char DM_URI_THING_SERVICE_REQUEST_WILDCARD[] DM_READ_ONLY =
40     "thing/service/+";
41 const char DM_URI_THING_SERVICE_REQUEST_WILDCARD2[] DM_READ_ONLY =
42     "thing/service/#";
43 const char DM_URI_THING_SERVICE_REQUEST[] DM_READ_ONLY = "thing/service/%s";
44 const char DM_URI_THING_SERVICE_RESPONSE[] DM_READ_ONLY =
45     "thing/service/%.*s_reply";
46 #ifdef DEVICE_MODEL_SHADOW
47 const char DM_URI_THING_PROPERTY_DESIRED_GET[] DM_READ_ONLY =
48     "thing/property/desired/get";
49 const char DM_URI_THING_PROPERTY_DESIRED_DELETE[] DM_READ_ONLY =
50     "thing/property/desired/delete";
51 const char DM_URI_THING_PROPERTY_DESIRED_GET_REPLY[] DM_READ_ONLY =
52     "thing/property/desired/get_reply";
53 const char DM_URI_THING_PROPERTY_DESIRED_DELETE_REPLY[] DM_READ_ONLY =
54     "thing/property/desired/delete_reply";
55 #endif
56 /* From Local To Cloud Request And Response*/
57 #ifdef LOG_REPORT_TO_CLOUD
58 const char DM_URI_THING_LOG_POST[] DM_READ_ONLY = "thing/log/post";
59 #endif
60 const char DM_URI_THING_EVENT_PROPERTY_POST[] DM_READ_ONLY =
61     "thing/event/property/post";
62 const char DM_URI_THING_EVENT_PROPERTY_POST_REPLY[] DM_READ_ONLY =
63     "thing/event/property/post_reply";
64 const char DM_URI_THING_EVENT_POST[] DM_READ_ONLY = "thing/event/%.*s/post";
65 const char DM_URI_THING_EVENT_POST_REPLY[] DM_READ_ONLY =
66     "thing/event/%s/post_reply";
67 const char DM_URI_THING_EVENT_POST_REPLY_WILDCARD[] DM_READ_ONLY =
68     "thing/event/+/post_reply";
69 const char DM_URI_THING_DEVICEINFO_UPDATE[] DM_READ_ONLY =
70     "thing/deviceinfo/update";
71 const char DM_URI_THING_DEVICEINFO_UPDATE_REPLY[] DM_READ_ONLY =
72     "thing/deviceinfo/update_reply";
73 const char DM_URI_THING_DEVICEINFO_DELETE[] DM_READ_ONLY =
74     "thing/deviceinfo/delete";
75 const char DM_URI_THING_DEVICEINFO_DELETE_REPLY[] DM_READ_ONLY =
76     "thing/deviceinfo/delete_reply";
77 const char DM_URI_THING_DSLTEMPLATE_GET[] DM_READ_ONLY =
78     "thing/dsltemplate/get";
79 const char DM_URI_THING_DSLTEMPLATE_GET_REPLY[] DM_READ_ONLY =
80     "thing/dsltemplate/get_reply";
81 const char DM_URI_THING_DYNAMICTSL_GET[] DM_READ_ONLY = "thing/dynamicTsl/get";
82 const char DM_URI_THING_DYNAMICTSL_GET_REPLY[] DM_READ_ONLY =
83     "thing/dynamicTsl/get_reply";
84 const char DM_URI_NTP_REQUEST[] DM_READ_ONLY = "request";
85 const char DM_URI_NTP_RESPONSE[] DM_READ_ONLY = "response";
86 #endif
87 
88 const char DM_URI_DEV_CORE_SERVICE_DEV[] DM_READ_ONLY = "/dev/core/service/dev";
89 
90 #ifdef DEVICE_MODEL_GATEWAY
91 /* From Cloud To Local Request And Response*/
92 const char DM_URI_THING_TOPO_ADD_NOTIFY[] DM_READ_ONLY =
93     "thing/topo/add/notify";
94 const char DM_URI_THING_TOPO_ADD_NOTIFY_REPLY[] DM_READ_ONLY =
95     "thing/topo/add/notify_reply";
96 const char DM_URI_THING_DELETE[] DM_READ_ONLY = "thing/delete";
97 const char DM_URI_THING_DELETE_REPLY[] DM_READ_ONLY = "thing/delete_reply";
98 const char DM_URI_THING_DISABLE[] DM_READ_ONLY = "thing/disable";
99 const char DM_URI_THING_DISABLE_REPLY[] DM_READ_ONLY = "thing/disable_reply";
100 const char DM_URI_THING_ENABLE[] DM_READ_ONLY = "thing/enable";
101 const char DM_URI_THING_ENABLE_REPLY[] DM_READ_ONLY = "thing/enable_reply";
102 const char DM_URI_THING_GATEWAY_PERMIT[] DM_READ_ONLY = "thing/gateway/permit";
103 const char DM_URI_THING_GATEWAY_PERMIT_REPLY[] DM_READ_ONLY =
104     "thing/gateway/permit_reply";
105 
106 /* From Local To Cloud Request And Response*/
107 const char DM_URI_THING_SUB_REGISTER[] DM_READ_ONLY = "thing/sub/register";
108 const char DM_URI_THING_SUB_REGISTER_REPLY[] DM_READ_ONLY =
109     "thing/sub/register_reply";
110 const char DM_URI_THING_PROXY_PRODUCT_REGISTER[] DM_READ_ONLY =
111     "thing/proxy/provisioning/product_register";
112 const char DM_URI_THING_PROXY_PRODUCT_REGISTER_REPLY[] DM_READ_ONLY =
113     "thing/proxy/provisioning/product_register_reply";
114 const char DM_URI_THING_SUB_UNREGISTER[] DM_READ_ONLY = "thing/sub/unregister";
115 const char DM_URI_THING_SUB_UNREGISTER_REPLY[] DM_READ_ONLY =
116     "thing/sub/unregister_reply";
117 const char DM_URI_THING_TOPO_ADD[] DM_READ_ONLY = "thing/topo/add";
118 const char DM_URI_THING_TOPO_ADD_REPLY[] DM_READ_ONLY = "thing/topo/add_reply";
119 const char DM_URI_THING_TOPO_DELETE[] DM_READ_ONLY = "thing/topo/delete";
120 const char DM_URI_THING_TOPO_DELETE_REPLY[] DM_READ_ONLY =
121     "thing/topo/delete_reply";
122 const char DM_URI_THING_TOPO_GET[] DM_READ_ONLY = "thing/topo/get";
123 const char DM_URI_THING_TOPO_GET_REPLY[] DM_READ_ONLY = "thing/topo/get_reply";
124 const char DM_URI_THING_LIST_FOUND[] DM_READ_ONLY = "thing/list/found";
125 const char DM_URI_THING_LIST_FOUND_REPLY[] DM_READ_ONLY =
126     "thing/list/found_reply";
127 const char DM_URI_COMBINE_LOGIN[] DM_READ_ONLY = "combine/login";
128 const char DM_URI_COMBINE_LOGIN_REPLY[] DM_READ_ONLY = "combine/login_reply";
129 const char DM_URI_COMBINE_LOGOUT[] DM_READ_ONLY = "combine/logout";
130 const char DM_URI_COMBINE_LOGOUT_REPLY[] DM_READ_ONLY = "combine/logout_reply";
131 #endif
132 
dm_msg_proc_thing_model_down_raw(_IN_ dm_msg_source_t * source)133 int dm_msg_proc_thing_model_down_raw(_IN_ dm_msg_source_t *source)
134 {
135     int res = 0;
136     char product_key[IOTX_PRODUCT_KEY_LEN + 1] = { 0 };
137     char device_name[IOTX_DEVICE_NAME_LEN + 1] = { 0 };
138 
139     /* Parse Product Key And Device Name */
140     res = dm_msg_uri_parse_pkdn((char *)source->uri, strlen(source->uri),
141                                 2 + DM_URI_OFFSET, 4 + DM_URI_OFFSET,
142                                 product_key, device_name);
143     if (res != SUCCESS_RETURN) {
144         return FAIL_RETURN;
145     }
146 
147     return dm_msg_thing_model_down_raw(
148         product_key, device_name, (char *)source->payload, source->payload_len);
149 }
150 
dm_msg_proc_thing_model_up_raw_reply(_IN_ dm_msg_source_t * source)151 int dm_msg_proc_thing_model_up_raw_reply(_IN_ dm_msg_source_t *source)
152 {
153     int res = 0;
154     char product_key[IOTX_PRODUCT_KEY_LEN + 1] = { 0 };
155     char device_name[IOTX_DEVICE_NAME_LEN + 1] = { 0 };
156 
157     dm_log_info(DM_URI_THING_MODEL_UP_RAW_REPLY);
158 
159     res = dm_msg_uri_parse_pkdn((char *)source->uri, strlen(source->uri),
160                                 2 + DM_URI_OFFSET, 4 + DM_URI_OFFSET,
161                                 product_key, device_name);
162     if (res != SUCCESS_RETURN) {
163         return FAIL_RETURN;
164     }
165 
166     /* Operation */
167     res = dm_msg_thing_model_up_raw_reply(
168         product_key, device_name, (char *)source->payload, source->payload_len);
169     if (res != SUCCESS_RETURN) {
170         return FAIL_RETURN;
171     }
172 
173     return SUCCESS_RETURN;
174 }
175 
176 #if !defined(DEVICE_MODEL_RAWDATA_SOLO)
dm_msg_proc_thing_service_property_set(_IN_ dm_msg_source_t * source,_IN_ dm_msg_dest_t * dest,_OU_ dm_msg_request_payload_t * request,_OU_ dm_msg_response_t * response)177 int dm_msg_proc_thing_service_property_set(
178     _IN_ dm_msg_source_t *source, _IN_ dm_msg_dest_t *dest,
179     _OU_ dm_msg_request_payload_t *request, _OU_ dm_msg_response_t *response)
180 {
181     int res = 0, devid = 0;
182     char product_key[IOTX_PRODUCT_KEY_LEN + 1] = { 0 };
183     char device_name[IOTX_DEVICE_NAME_LEN + 1] = { 0 };
184 
185     dm_log_info(DM_URI_THING_SERVICE_PROPERTY_SET);
186 
187     /* Request */
188     res = dm_msg_uri_parse_pkdn((char *)source->uri, strlen(source->uri),
189                                 2 + DM_URI_OFFSET, 4 + DM_URI_OFFSET,
190                                 product_key, device_name);
191     if (res < SUCCESS_RETURN) {
192         return res;
193     }
194 
195     res = dm_mgr_search_device_by_pkdn(product_key, device_name, &devid);
196     if (res < SUCCESS_RETURN) {
197         return res;
198     }
199 
200     res = dm_msg_request_parse((char *)source->payload, source->payload_len,
201                                request);
202     if (res < SUCCESS_RETURN) {
203         return res;
204     }
205 
206     /* Operation */
207     res = dm_msg_property_set(devid, request);
208 
209     /* Response */
210     response->service_prefix = DM_URI_SYS_PREFIX;
211     response->service_name = dest->uri_name;
212     memcpy(response->product_key, product_key, strlen(product_key));
213     memcpy(response->device_name, device_name, strlen(device_name));
214     response->code = (res == SUCCESS_RETURN) ? (IOTX_DM_ERR_CODE_SUCCESS)
215                                              : (IOTX_DM_ERR_CODE_REQUEST_ERROR);
216 
217     return SUCCESS_RETURN;
218 }
219 
dm_msg_proc_thing_event_notify(_IN_ dm_msg_source_t * source,_IN_ dm_msg_dest_t * dest,_OU_ dm_msg_request_payload_t * request,_OU_ dm_msg_response_t * response)220 int dm_msg_proc_thing_event_notify(_IN_ dm_msg_source_t *source,
221                                    _IN_ dm_msg_dest_t *dest,
222                                    _OU_ dm_msg_request_payload_t *request,
223                                    _OU_ dm_msg_response_t *response)
224 {
225     int res = 0, devid = 0;
226     char product_key[IOTX_PRODUCT_KEY_LEN] = { 0 };
227     char device_name[IOTX_DEVICE_NAME_LEN] = { 0 };
228 
229     dm_log_info(DM_URI_THING_EVENT_NOTIFY);
230 
231     /* Request */
232     res = dm_msg_uri_parse_pkdn((char *)source->uri, strlen(source->uri),
233                                 2 + DM_URI_OFFSET, 4 + DM_URI_OFFSET,
234                                 product_key, device_name);
235     if (res < SUCCESS_RETURN) {
236         return res;
237     }
238 
239     res = dm_mgr_search_device_by_pkdn(product_key, device_name, &devid);
240     if (res < SUCCESS_RETURN) {
241         return res;
242     }
243 
244     res = dm_msg_request_parse((char *)source->payload, source->payload_len,
245                                request);
246     if (res < SUCCESS_RETURN) {
247         return res;
248     }
249 
250     /* Operation */
251     res = dm_msg_thing_event_notify(devid, request);
252 
253     /* Response */
254     response->service_prefix = DM_URI_SYS_PREFIX;
255     response->service_name = dest->uri_name;
256     memcpy(response->product_key, product_key, strlen(product_key));
257     memcpy(response->device_name, device_name, strlen(device_name));
258     response->code = (res == SUCCESS_RETURN) ? (IOTX_DM_ERR_CODE_SUCCESS)
259                                              : (IOTX_DM_ERR_CODE_REQUEST_ERROR);
260 
261     return SUCCESS_RETURN;
262 }
263 
dm_msg_proc_thing_service_property_get(_IN_ dm_msg_source_t * source,_IN_ dm_msg_dest_t * dest,_OU_ dm_msg_request_payload_t * request,_OU_ dm_msg_response_t * response,_OU_ unsigned char ** data,int * data_len)264 int dm_msg_proc_thing_service_property_get(
265     _IN_ dm_msg_source_t *source, _IN_ dm_msg_dest_t *dest,
266     _OU_ dm_msg_request_payload_t *request, _OU_ dm_msg_response_t *response,
267     _OU_ unsigned char **data, int *data_len)
268 {
269     int res = 0, devid = 0;
270     char product_key[IOTX_PRODUCT_KEY_LEN + 1] = { 0 };
271     char device_name[IOTX_DEVICE_NAME_LEN + 1] = { 0 };
272 
273     dm_log_info(DM_URI_THING_SERVICE_PROPERTY_GET);
274 
275     /* Request */
276     res = dm_msg_uri_parse_pkdn((char *)source->uri, strlen(source->uri),
277                                 2 + DM_URI_OFFSET, 4 + DM_URI_OFFSET,
278                                 product_key, device_name);
279     if (res < SUCCESS_RETURN) {
280         return res;
281     }
282 
283     res = dm_mgr_search_device_by_pkdn(product_key, device_name, &devid);
284     if (res < SUCCESS_RETURN) {
285         return res;
286     }
287 
288     res = dm_msg_request_parse((char *)source->payload, source->payload_len,
289                                request);
290     if (res < SUCCESS_RETURN) {
291         return res;
292     }
293 
294     /* Operation */
295 #ifndef DEPRECATED_LINKKIT
296     res = dm_msg_property_get(devid, request, source->context);
297 #else
298     res = dm_msg_property_get(devid, request, (char **)data, data_len);
299 #endif
300 
301 #ifdef DEPRECATED_LINKKIT
302     /* Response */
303     response->service_prefix = DM_URI_SYS_PREFIX;
304     response->service_name = dest->uri_name;
305     memcpy(response->product_key, product_key, strlen(product_key));
306     memcpy(response->device_name, device_name, strlen(device_name));
307     response->code = (res == SUCCESS_RETURN) ? (IOTX_DM_ERR_CODE_SUCCESS)
308                                              : (IOTX_DM_ERR_CODE_REQUEST_ERROR);
309 
310     if (res != SUCCESS_RETURN) {
311         *data = DM_malloc(strlen("{}") + 1);
312         if (*data == NULL) {
313             return FAIL_RETURN;
314         }
315         memset(*data, 0, strlen("{}") + 1);
316         memcpy(*data, "{}", strlen("{}"));
317 
318         *data_len = strlen((char *)*data);
319     }
320 #endif
321 
322     if (res != SUCCESS_RETURN) {
323         dm_log_err("DM Property Get Failed");
324     }
325 
326     return res;
327 }
328 
dm_msg_proc_thing_service_property_post(_IN_ dm_msg_source_t * source,_IN_ dm_msg_dest_t * dest,_OU_ dm_msg_request_payload_t * request,_OU_ dm_msg_response_t * response)329 int dm_msg_proc_thing_service_property_post(
330     _IN_ dm_msg_source_t *source, _IN_ dm_msg_dest_t *dest,
331     _OU_ dm_msg_request_payload_t *request, _OU_ dm_msg_response_t *response)
332 {
333     int res = 0;
334     char product_key[IOTX_PRODUCT_KEY_LEN + 1] = { 0 };
335     char device_name[IOTX_DEVICE_NAME_LEN + 1] = { 0 };
336 
337     dm_log_info(DM_URI_THING_EVENT_PROPERTY_POST);
338 
339     /* Request */
340     res = dm_msg_uri_parse_pkdn((char *)source->uri, strlen(source->uri),
341                                 2 + DM_URI_OFFSET, 4 + DM_URI_OFFSET,
342                                 product_key, device_name);
343     if (res != SUCCESS_RETURN) {
344         return FAIL_RETURN;
345     }
346 
347     res = dm_msg_request_parse((char *)source->payload, source->payload_len,
348                                request);
349 
350     /* Response */
351     response->service_prefix = DM_URI_SYS_PREFIX;
352     response->service_name = dest->uri_name;
353     memcpy(response->product_key, product_key, strlen(product_key));
354     memcpy(response->device_name, device_name, strlen(device_name));
355     response->code = (res == SUCCESS_RETURN) ? (IOTX_DM_ERR_CODE_SUCCESS)
356                                              : (IOTX_DM_ERR_CODE_REQUEST_ERROR);
357 
358     return SUCCESS_RETURN;
359 }
360 
dm_msg_proc_thing_service_request(_IN_ dm_msg_source_t * source)361 int dm_msg_proc_thing_service_request(_IN_ dm_msg_source_t *source)
362 {
363     int res = 0, serviceid_pos = 0;
364     char product_key[IOTX_PRODUCT_KEY_LEN + 1] = { 0 };
365     char device_name[IOTX_DEVICE_NAME_LEN + 1] = { 0 };
366     dm_msg_request_payload_t request;
367 
368     memset(&request, 0, sizeof(dm_msg_request_payload_t));
369 
370     res = dm_utils_memtok((char *)source->uri, strlen(source->uri),
371                           DM_URI_SERVICE_DELIMITER, 6, &serviceid_pos);
372     if (res != SUCCESS_RETURN) {
373         return FAIL_RETURN;
374     }
375     dm_log_info("Service Identifier: %.*s",
376                 (int)(strlen(source->uri) - serviceid_pos - 1),
377                 source->uri + serviceid_pos + 1);
378 
379     /* Parse Product Key And Device Name */
380     res = dm_msg_uri_parse_pkdn((char *)source->uri, strlen(source->uri),
381                                 2 + DM_URI_OFFSET, 4 + DM_URI_OFFSET,
382                                 product_key, device_name);
383     if (res != SUCCESS_RETURN) {
384         return FAIL_RETURN;
385     }
386 
387     /* Request */
388     res = dm_msg_request_parse((char *)source->payload, source->payload_len,
389                                &request);
390     if (res != SUCCESS_RETURN) {
391         return FAIL_RETURN;
392     }
393 
394     /* Operation */
395     return dm_msg_thing_service_request(
396         product_key, device_name, (char *)source->uri + serviceid_pos + 1,
397         strlen(source->uri) - serviceid_pos - 1, &request, source->context);
398 }
399 
dm_msg_proc_thing_event_post_reply(_IN_ dm_msg_source_t * source)400 int dm_msg_proc_thing_event_post_reply(_IN_ dm_msg_source_t *source)
401 {
402     int res = 0, eventid_start_pos = 0, eventid_end_pos = 0;
403     dm_msg_response_payload_t response;
404 #if !defined(DM_MESSAGE_CACHE_DISABLED)
405     char int_id[DM_UTILS_UINT32_STRLEN + 1] = { 0 };
406 #endif
407 
408     res = dm_utils_memtok((char *)source->uri, strlen(source->uri),
409                           DM_URI_SERVICE_DELIMITER, 6 + DM_URI_OFFSET,
410                           &eventid_start_pos);
411     if (res != SUCCESS_RETURN) {
412         return FAIL_RETURN;
413     }
414 
415     res = dm_utils_memtok((char *)source->uri, strlen(source->uri),
416                           DM_URI_SERVICE_DELIMITER, 7 + DM_URI_OFFSET,
417                           &eventid_end_pos);
418     if (res != SUCCESS_RETURN) {
419         return FAIL_RETURN;
420     }
421     dm_log_info("Event Id: %.*s", eventid_end_pos - eventid_start_pos - 1,
422                 source->uri + eventid_start_pos + 1);
423 
424     /* Response */
425     res = dm_msg_response_parse((char *)source->payload, source->payload_len,
426                                 &response);
427     if (res != SUCCESS_RETURN) {
428         return FAIL_RETURN;
429     }
430 
431     /* Operation */
432     if ((strlen("property") == eventid_end_pos - eventid_start_pos - 1) &&
433         (memcmp("property", source->uri + eventid_start_pos + 1,
434                 eventid_end_pos - eventid_start_pos - 1) == 0)) {
435         dm_msg_thing_event_property_post_reply(&response);
436     } else {
437         dm_msg_thing_event_post_reply(
438             (char *)source->uri + eventid_start_pos + 1,
439             eventid_end_pos - eventid_start_pos - 1, &response);
440     }
441 
442     /* Remove Message From Cache */
443 #if !defined(DM_MESSAGE_CACHE_DISABLED)
444     if (response.id.value_length > DM_UTILS_UINT32_STRLEN) {
445         return FAIL_RETURN;
446     }
447     memcpy(int_id, response.id.value, response.id.value_length);
448     dm_msg_cache_remove(atoi(int_id));
449 #endif
450     return SUCCESS_RETURN;
451 }
452 
453 #ifdef DEVICE_MODEL_SHADOW
dm_msg_proc_thing_property_desired_get_reply(_IN_ dm_msg_source_t * source)454 int dm_msg_proc_thing_property_desired_get_reply(_IN_ dm_msg_source_t *source)
455 {
456     int res = 0;
457     dm_msg_response_payload_t response;
458 #if !defined(DM_MESSAGE_CACHE_DISABLED)
459     char int_id[DM_UTILS_UINT32_STRLEN + 1] = { 0 };
460 #endif
461 
462     dm_log_info(DM_URI_THING_PROPERTY_DESIRED_GET_REPLY);
463 
464     /* Response */
465     res = dm_msg_response_parse((char *)source->payload, source->payload_len,
466                                 &response);
467     if (res != SUCCESS_RETURN) {
468         return FAIL_RETURN;
469     }
470 
471     /* Operation */
472     res = dm_msg_thing_property_desired_get_reply(&response);
473 
474     /* Remove Message From Cache */
475 #if !defined(DM_MESSAGE_CACHE_DISABLED)
476     if (response.id.value_length > DM_UTILS_UINT32_STRLEN) {
477         return FAIL_RETURN;
478     }
479     memcpy(int_id, response.id.value, response.id.value_length);
480     dm_msg_cache_remove(atoi(int_id));
481 #endif
482     return SUCCESS_RETURN;
483 }
484 
dm_msg_proc_thing_property_desired_delete_reply(_IN_ dm_msg_source_t * source)485 int dm_msg_proc_thing_property_desired_delete_reply(
486     _IN_ dm_msg_source_t *source)
487 {
488     int res = 0;
489     dm_msg_response_payload_t response;
490 #if !defined(DM_MESSAGE_CACHE_DISABLED)
491     char int_id[DM_UTILS_UINT32_STRLEN + 1] = { 0 };
492 #endif
493 
494     dm_log_info(DM_URI_THING_PROPERTY_DESIRED_DELETE_REPLY);
495 
496     /* Response */
497     res = dm_msg_response_parse((char *)source->payload, source->payload_len,
498                                 &response);
499     if (res != SUCCESS_RETURN) {
500         return FAIL_RETURN;
501     }
502 
503     /* Operation */
504     res = dm_msg_thing_property_desired_delete_reply(&response);
505 
506     /* Remove Message From Cache */
507 #if !defined(DM_MESSAGE_CACHE_DISABLED)
508     if (response.id.value_length > DM_UTILS_UINT32_STRLEN) {
509         return FAIL_RETURN;
510     }
511     memcpy(int_id, response.id.value, response.id.value_length);
512     dm_msg_cache_remove(atoi(int_id));
513 #endif
514     return SUCCESS_RETURN;
515 }
516 #endif
517 
dm_msg_proc_thing_deviceinfo_update_reply(_IN_ dm_msg_source_t * source)518 int dm_msg_proc_thing_deviceinfo_update_reply(_IN_ dm_msg_source_t *source)
519 {
520     int res = 0;
521     dm_msg_response_payload_t response;
522 #if !defined(DM_MESSAGE_CACHE_DISABLED)
523     char int_id[DM_UTILS_UINT32_STRLEN + 1] = { 0 };
524 #endif
525 
526     dm_log_info(DM_URI_THING_DEVICEINFO_UPDATE_REPLY);
527 
528     /* Response */
529     res = dm_msg_response_parse((char *)source->payload, source->payload_len,
530                                 &response);
531     if (res != SUCCESS_RETURN) {
532         return FAIL_RETURN;
533     }
534 
535     /* Operation */
536     dm_msg_thing_deviceinfo_update_reply(&response);
537 
538     /* Remove Message From Cache */
539 #if !defined(DM_MESSAGE_CACHE_DISABLED)
540     if (response.id.value_length > DM_UTILS_UINT32_STRLEN) {
541         return FAIL_RETURN;
542     }
543     memcpy(int_id, response.id.value, response.id.value_length);
544     dm_msg_cache_remove(atoi(int_id));
545 #endif
546     return SUCCESS_RETURN;
547 }
548 
dm_msg_proc_thing_deviceinfo_delete_reply(_IN_ dm_msg_source_t * source)549 int dm_msg_proc_thing_deviceinfo_delete_reply(_IN_ dm_msg_source_t *source)
550 {
551     int res = 0;
552     dm_msg_response_payload_t response;
553 #if !defined(DM_MESSAGE_CACHE_DISABLED)
554     char int_id[DM_UTILS_UINT32_STRLEN + 1] = { 0 };
555 #endif
556 
557     dm_log_info(DM_URI_THING_DEVICEINFO_DELETE_REPLY);
558 
559     /* Response */
560     res = dm_msg_response_parse((char *)source->payload, source->payload_len,
561                                 &response);
562     if (res != SUCCESS_RETURN) {
563         return FAIL_RETURN;
564     }
565 
566     /* Operation */
567     dm_msg_thing_deviceinfo_delete_reply(&response);
568 
569     /* Remove Message From Cache */
570 #if !defined(DM_MESSAGE_CACHE_DISABLED)
571     if (response.id.value_length > DM_UTILS_UINT32_STRLEN) {
572         return FAIL_RETURN;
573     }
574     memcpy(int_id, response.id.value, response.id.value_length);
575     dm_msg_cache_remove(atoi(int_id));
576 #endif
577     return SUCCESS_RETURN;
578 }
579 
dm_msg_proc_thing_dynamictsl_get_reply(_IN_ dm_msg_source_t * source)580 int dm_msg_proc_thing_dynamictsl_get_reply(_IN_ dm_msg_source_t *source)
581 {
582     int res = 0;
583     dm_msg_response_payload_t response;
584 #if !defined(DM_MESSAGE_CACHE_DISABLED)
585     char int_id[DM_UTILS_UINT32_STRLEN + 1] = { 0 };
586 #endif
587 
588     dm_log_info(DM_URI_THING_DYNAMICTSL_GET_REPLY);
589 
590     /* Response */
591     res = dm_msg_response_parse((char *)source->payload, source->payload_len,
592                                 &response);
593     if (res != SUCCESS_RETURN) {
594         return FAIL_RETURN;
595     }
596 
597     /* Operation */
598     dm_msg_thing_dynamictsl_get_reply(&response);
599 
600     /* Remove Message From Cache */
601 #if !defined(DM_MESSAGE_CACHE_DISABLED)
602     if (response.id.value_length > DM_UTILS_UINT32_STRLEN) {
603         return FAIL_RETURN;
604     }
605     memcpy(int_id, response.id.value, response.id.value_length);
606     dm_msg_cache_remove(atoi(int_id));
607 #endif
608     return SUCCESS_RETURN;
609 }
610 
dm_msg_proc_rrpc_request(_IN_ dm_msg_source_t * source)611 int dm_msg_proc_rrpc_request(_IN_ dm_msg_source_t *source)
612 {
613     int res = 0, rrpcid_pos = 0;
614     char product_key[IOTX_PRODUCT_KEY_LEN + 1] = { 0 };
615     char device_name[IOTX_DEVICE_NAME_LEN + 1] = { 0 };
616     dm_msg_request_payload_t request;
617 
618     memset(&request, 0, sizeof(dm_msg_request_payload_t));
619 
620     res = dm_utils_memtok((char *)source->uri, strlen(source->uri),
621                           DM_URI_SERVICE_DELIMITER, 6, &rrpcid_pos);
622     if (res != SUCCESS_RETURN) {
623         return FAIL_RETURN;
624     }
625     dm_log_info("Rrpc Id: %.*s", (int)(strlen(source->uri) - rrpcid_pos - 1),
626                 source->uri + rrpcid_pos + 1);
627 
628     /* Parse Product Key And Device Name */
629     res = dm_msg_uri_parse_pkdn((char *)source->uri, strlen(source->uri),
630                                 2 + DM_URI_OFFSET, 4 + DM_URI_OFFSET,
631                                 product_key, device_name);
632     if (res != SUCCESS_RETURN) {
633         return FAIL_RETURN;
634     }
635 
636     /* Request */
637     res = dm_msg_request_parse((char *)source->payload, source->payload_len,
638                                &request);
639     if (res != SUCCESS_RETURN) {
640         return FAIL_RETURN;
641     }
642 
643     /* Operation */
644     return dm_msg_rrpc_request(product_key, device_name,
645                                (char *)source->uri + rrpcid_pos + 1,
646                                strlen(source->uri) - rrpcid_pos - 1, &request);
647 }
648 
dm_disp_ntp_response(_IN_ dm_msg_source_t * source)649 int dm_disp_ntp_response(_IN_ dm_msg_source_t *source)
650 {
651     dm_log_info(DM_URI_NTP_RESPONSE);
652 
653     /* Operation */
654     return dm_msg_ntp_response((char *)source->payload, source->payload_len);
655 }
656 
dm_disp_ext_error_response(_IN_ dm_msg_source_t * source)657 int dm_disp_ext_error_response(_IN_ dm_msg_source_t *source)
658 {
659     return dm_msg_ext_error_response((char *)source->payload,
660                                      source->payload_len);
661 }
662 #endif
663 
664 #ifdef DEVICE_MODEL_GATEWAY
dm_msg_proc_thing_topo_add_notify(_IN_ dm_msg_source_t * source,_IN_ dm_msg_dest_t * dest,_OU_ dm_msg_request_payload_t * request,_OU_ dm_msg_response_t * response)665 int dm_msg_proc_thing_topo_add_notify(_IN_ dm_msg_source_t *source,
666                                       _IN_ dm_msg_dest_t *dest,
667                                       _OU_ dm_msg_request_payload_t *request,
668                                       _OU_ dm_msg_response_t *response)
669 {
670     int res = 0;
671     char product_key[IOTX_PRODUCT_KEY_LEN + 1] = { 0 };
672     char device_name[IOTX_DEVICE_NAME_LEN + 1] = { 0 };
673 
674     dm_log_info(DM_URI_THING_TOPO_ADD_NOTIFY);
675 
676     /* Request */
677     res = dm_msg_uri_parse_pkdn((char *)source->uri, strlen(source->uri),
678                                 2 + DM_URI_OFFSET, 4 + DM_URI_OFFSET,
679                                 product_key, device_name);
680     if (res < SUCCESS_RETURN) {
681         return res;
682     }
683 
684     res = dm_msg_request_parse((char *)source->payload, source->payload_len,
685                                request);
686     if (res < SUCCESS_RETURN) {
687         return res;
688     }
689 
690     /* Operation */
691     res = dm_msg_topo_add_notify(request->params.value,
692                                  request->params.value_length);
693     if (res < SUCCESS_RETURN) {
694         return res;
695     }
696 
697     /* Response */
698     response->service_prefix = DM_URI_SYS_PREFIX;
699     response->service_name = dest->uri_name;
700     memcpy(response->product_key, product_key, strlen(product_key));
701     memcpy(response->device_name, device_name, strlen(device_name));
702     response->code = (res == SUCCESS_RETURN) ? (IOTX_DM_ERR_CODE_SUCCESS)
703                                              : (IOTX_DM_ERR_CODE_REQUEST_ERROR);
704 
705     return SUCCESS_RETURN;
706 }
707 
dm_msg_proc_thing_disable(_IN_ dm_msg_source_t * source,_IN_ dm_msg_dest_t * dest,_OU_ dm_msg_request_payload_t * request,_OU_ dm_msg_response_t * response)708 int dm_msg_proc_thing_disable(_IN_ dm_msg_source_t *source,
709                               _IN_ dm_msg_dest_t *dest,
710                               _OU_ dm_msg_request_payload_t *request,
711                               _OU_ dm_msg_response_t *response)
712 {
713     int res = 0;
714     char product_key[IOTX_PRODUCT_KEY_LEN + 1] = { 0 };
715     char device_name[IOTX_DEVICE_NAME_LEN + 1] = { 0 };
716 
717     dm_log_info(DM_URI_THING_DISABLE);
718 
719     /* Request */
720     res = dm_msg_uri_parse_pkdn((char *)source->uri, strlen(source->uri),
721                                 2 + DM_URI_OFFSET, 4 + DM_URI_OFFSET,
722                                 product_key, device_name);
723     if (res < SUCCESS_RETURN) {
724         return res;
725     }
726 
727     res = dm_msg_request_parse((char *)source->payload, source->payload_len,
728                                request);
729     if (res != SUCCESS_RETURN) {
730         return res;
731     }
732 
733     /* Operation */
734     res = dm_msg_thing_disable(product_key, device_name);
735 
736     /* Response */
737     response->service_prefix = DM_URI_SYS_PREFIX;
738     response->service_name = dest->uri_name;
739     memcpy(response->product_key, product_key, strlen(product_key));
740     memcpy(response->device_name, device_name, strlen(device_name));
741     response->code = (res == SUCCESS_RETURN) ? (IOTX_DM_ERR_CODE_SUCCESS)
742                                              : (IOTX_DM_ERR_CODE_REQUEST_ERROR);
743 
744     return SUCCESS_RETURN;
745 }
746 
dm_msg_proc_thing_enable(_IN_ dm_msg_source_t * source,_IN_ dm_msg_dest_t * dest,_OU_ dm_msg_request_payload_t * request,_OU_ dm_msg_response_t * response)747 int dm_msg_proc_thing_enable(_IN_ dm_msg_source_t *source,
748                              _IN_ dm_msg_dest_t *dest,
749                              _OU_ dm_msg_request_payload_t *request,
750                              _OU_ dm_msg_response_t *response)
751 {
752     int res = 0;
753     char product_key[IOTX_PRODUCT_KEY_LEN + 1] = { 0 };
754     char device_name[IOTX_DEVICE_NAME_LEN + 1] = { 0 };
755 
756     dm_log_info(DM_URI_THING_DISABLE);
757 
758     /* Request */
759     res = dm_msg_uri_parse_pkdn((char *)source->uri, strlen(source->uri),
760                                 2 + DM_URI_OFFSET, 4 + DM_URI_OFFSET,
761                                 product_key, device_name);
762     if (res < SUCCESS_RETURN) {
763         return res;
764     }
765 
766     res = dm_msg_request_parse((char *)source->payload, source->payload_len,
767                                request);
768     if (res != SUCCESS_RETURN) {
769         return res;
770     }
771 
772     /* Operation */
773     res = dm_msg_thing_enable(product_key, device_name);
774 
775     /* Response */
776     response->service_prefix = DM_URI_SYS_PREFIX;
777     response->service_name = dest->uri_name;
778     memcpy(response->product_key, product_key, strlen(product_key));
779     memcpy(response->device_name, device_name, strlen(device_name));
780     response->code = (res == SUCCESS_RETURN) ? (IOTX_DM_ERR_CODE_SUCCESS)
781                                              : (IOTX_DM_ERR_CODE_REQUEST_ERROR);
782 
783     return SUCCESS_RETURN;
784 }
785 
dm_msg_proc_thing_delete(_IN_ dm_msg_source_t * source,_IN_ dm_msg_dest_t * dest,_OU_ dm_msg_request_payload_t * request,_OU_ dm_msg_response_t * response)786 int dm_msg_proc_thing_delete(_IN_ dm_msg_source_t *source,
787                              _IN_ dm_msg_dest_t *dest,
788                              _OU_ dm_msg_request_payload_t *request,
789                              _OU_ dm_msg_response_t *response)
790 {
791     int res = 0;
792     char product_key[IOTX_PRODUCT_KEY_LEN + 1] = { 0 };
793     char device_name[IOTX_DEVICE_NAME_LEN + 1] = { 0 };
794 
795     dm_log_info(DM_URI_THING_DELETE);
796 
797     /* Request */
798     res = dm_msg_uri_parse_pkdn((char *)source->uri, strlen(source->uri),
799                                 2 + DM_URI_OFFSET, 4 + DM_URI_OFFSET,
800                                 product_key, device_name);
801     if (res < SUCCESS_RETURN) {
802         return res;
803     }
804 
805     res = dm_msg_request_parse((char *)source->payload, source->payload_len,
806                                request);
807     if (res != SUCCESS_RETURN) {
808         return res;
809     }
810 
811     /* Operation */
812     res = dm_msg_thing_delete(product_key, device_name);
813 
814     /* Response */
815     response->service_prefix = DM_URI_SYS_PREFIX;
816     response->service_name = dest->uri_name;
817     memcpy(response->product_key, product_key, strlen(product_key));
818     memcpy(response->device_name, device_name, strlen(device_name));
819     response->code = (res == SUCCESS_RETURN) ? (IOTX_DM_ERR_CODE_SUCCESS)
820                                              : (IOTX_DM_ERR_CODE_REQUEST_ERROR);
821 
822     return SUCCESS_RETURN;
823 }
824 
dm_msg_proc_thing_gateway_permit(_IN_ dm_msg_source_t * source,_IN_ dm_msg_dest_t * dest,_OU_ dm_msg_request_payload_t * request,_OU_ dm_msg_response_t * response)825 int dm_msg_proc_thing_gateway_permit(_IN_ dm_msg_source_t *source,
826                                      _IN_ dm_msg_dest_t *dest,
827                                      _OU_ dm_msg_request_payload_t *request,
828                                      _OU_ dm_msg_response_t *response)
829 {
830     int res = 0;
831     char product_key[IOTX_PRODUCT_KEY_LEN + 1] = { 0 };
832     char device_name[IOTX_DEVICE_NAME_LEN + 1] = { 0 };
833 
834     dm_log_info(DM_URI_THING_DELETE);
835 
836     /* Request */
837     res = dm_msg_uri_parse_pkdn((char *)source->uri, strlen(source->uri),
838                                 2 + DM_URI_OFFSET, 4 + DM_URI_OFFSET,
839                                 product_key, device_name);
840     if (res < SUCCESS_RETURN) {
841         return res;
842     }
843 
844     res = dm_msg_request_parse((char *)source->payload, source->payload_len,
845                                request);
846     if (res != SUCCESS_RETURN) {
847         return res;
848     }
849 
850     /* Operation */
851     res = dm_msg_thing_gateway_permit(request->params.value,
852                                       request->params.value_length);
853 
854     /* Response */
855     response->service_prefix = DM_URI_SYS_PREFIX;
856     response->service_name = dest->uri_name;
857     memcpy(response->product_key, product_key, strlen(product_key));
858     memcpy(response->device_name, device_name, strlen(device_name));
859     response->code = (res == SUCCESS_RETURN) ? (IOTX_DM_ERR_CODE_SUCCESS)
860                                              : (IOTX_DM_ERR_CODE_REQUEST_ERROR);
861 
862     return SUCCESS_RETURN;
863 }
864 
dm_msg_proc_thing_sub_register_reply(_IN_ dm_msg_source_t * source)865 int dm_msg_proc_thing_sub_register_reply(_IN_ dm_msg_source_t *source)
866 {
867     int res = 0;
868     dm_msg_response_payload_t response;
869 #if !defined(DM_MESSAGE_CACHE_DISABLED)
870     char int_id[DM_UTILS_UINT32_STRLEN + 1] = { 0 };
871 #endif
872 
873     dm_log_info(DM_URI_THING_SUB_REGISTER_REPLY);
874 
875     memset(&response, 0, sizeof(dm_msg_response_payload_t));
876 
877     /* Response */
878     res = dm_msg_response_parse((char *)source->payload, source->payload_len,
879                                 &response);
880     if (res != SUCCESS_RETURN) {
881         return FAIL_RETURN;
882     }
883 
884     /* Operation */
885     dm_msg_thing_sub_register_reply(&response);
886 
887     /* Remove Message From Cache */
888 #if !defined(DM_MESSAGE_CACHE_DISABLED)
889     if (response.id.value_length > DM_UTILS_UINT32_STRLEN) {
890         return FAIL_RETURN;
891     }
892     memcpy(int_id, response.id.value, response.id.value_length);
893     dm_msg_cache_remove(atoi(int_id));
894 #endif
895 
896     return SUCCESS_RETURN;
897 }
898 
dm_msg_proc_thing_proxy_product_register_reply(_IN_ dm_msg_source_t * source)899 int dm_msg_proc_thing_proxy_product_register_reply(_IN_ dm_msg_source_t *source)
900 {
901     int res = 0;
902     dm_msg_response_payload_t response;
903 #if !defined(DM_MESSAGE_CACHE_DISABLED)
904     char int_id[DM_UTILS_UINT32_STRLEN + 1] = { 0 };
905 #endif
906 
907     dm_log_info(DM_URI_THING_PROXY_PRODUCT_REGISTER_REPLY);
908 
909     memset(&response, 0, sizeof(dm_msg_response_payload_t));
910 
911     /* Response */
912     res = dm_msg_response_parse((char *)source->payload, source->payload_len,
913                                 &response);
914     if (res != SUCCESS_RETURN) {
915         return FAIL_RETURN;
916     }
917 
918     /* Operation */
919     dm_msg_thing_proxy_product_register_reply(&response);
920 
921     /* Remove Message From Cache */
922 #if !defined(DM_MESSAGE_CACHE_DISABLED)
923     if (response.id.value_length > DM_UTILS_UINT32_STRLEN) {
924         return FAIL_RETURN;
925     }
926     memcpy(int_id, response.id.value, response.id.value_length);
927     dm_msg_cache_remove(atoi(int_id));
928 #endif
929     return SUCCESS_RETURN;
930 
931     return 0;
932 }
933 
dm_msg_proc_thing_sub_unregister_reply(_IN_ dm_msg_source_t * source)934 int dm_msg_proc_thing_sub_unregister_reply(_IN_ dm_msg_source_t *source)
935 {
936     int res = 0;
937     dm_msg_response_payload_t response;
938 #if !defined(DM_MESSAGE_CACHE_DISABLED)
939     char int_id[DM_UTILS_UINT32_STRLEN + 1] = { 0 };
940 #endif
941 
942     dm_log_info(DM_URI_THING_SUB_UNREGISTER_REPLY);
943 
944     memset(&response, 0, sizeof(dm_msg_response_payload_t));
945 
946     /* Response */
947     res = dm_msg_response_parse((char *)source->payload, source->payload_len,
948                                 &response);
949     if (res != SUCCESS_RETURN) {
950         return FAIL_RETURN;
951     }
952 
953     /* Operation */
954     dm_msg_thing_sub_unregister_reply(&response);
955 
956     /* Remove Message From Cache */
957 #if !defined(DM_MESSAGE_CACHE_DISABLED)
958     if (response.id.value_length > DM_UTILS_UINT32_STRLEN) {
959         return FAIL_RETURN;
960     }
961     memcpy(int_id, response.id.value, response.id.value_length);
962     dm_msg_cache_remove(atoi(int_id));
963 #endif
964     return SUCCESS_RETURN;
965 }
966 
dm_msg_proc_thing_topo_add_reply(_IN_ dm_msg_source_t * source)967 int dm_msg_proc_thing_topo_add_reply(_IN_ dm_msg_source_t *source)
968 {
969     int res = 0;
970     dm_msg_response_payload_t response;
971 #if !defined(DM_MESSAGE_CACHE_DISABLED)
972     char int_id[DM_UTILS_UINT32_STRLEN + 1] = { 0 };
973 #endif
974 
975     dm_log_info(DM_URI_THING_TOPO_ADD_REPLY);
976 
977     memset(&response, 0, sizeof(dm_msg_response_payload_t));
978 
979     /* Response */
980     res = dm_msg_response_parse((char *)source->payload, source->payload_len,
981                                 &response);
982     if (res != SUCCESS_RETURN) {
983         return FAIL_RETURN;
984     }
985 
986     /* Operation */
987     dm_msg_thing_topo_add_reply(&response);
988 
989     /* Remove Message From Cache */
990 #if !defined(DM_MESSAGE_CACHE_DISABLED)
991     if (response.id.value_length > DM_UTILS_UINT32_STRLEN) {
992         return FAIL_RETURN;
993     }
994     memcpy(int_id, response.id.value, response.id.value_length);
995     dm_msg_cache_remove(atoi(int_id));
996 #endif
997     return SUCCESS_RETURN;
998 }
999 
dm_msg_proc_thing_topo_delete_reply(_IN_ dm_msg_source_t * source)1000 int dm_msg_proc_thing_topo_delete_reply(_IN_ dm_msg_source_t *source)
1001 {
1002     int res = 0;
1003     dm_msg_response_payload_t response;
1004 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1005     char int_id[DM_UTILS_UINT32_STRLEN + 1] = { 0 };
1006 #endif
1007 
1008     dm_log_info(DM_URI_THING_TOPO_DELETE_REPLY);
1009 
1010     memset(&response, 0, sizeof(dm_msg_response_payload_t));
1011 
1012     /* Response */
1013     res = dm_msg_response_parse((char *)source->payload, source->payload_len,
1014                                 &response);
1015     if (res != SUCCESS_RETURN) {
1016         return FAIL_RETURN;
1017     }
1018 
1019     /* Operation */
1020     dm_msg_thing_topo_delete_reply(&response);
1021 
1022     /* Remove Message From Cache */
1023 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1024     if (response.id.value_length > DM_UTILS_UINT32_STRLEN) {
1025         return FAIL_RETURN;
1026     }
1027     memcpy(int_id, response.id.value, response.id.value_length);
1028     dm_msg_cache_remove(atoi(int_id));
1029 #endif
1030     return SUCCESS_RETURN;
1031 }
1032 
dm_msg_proc_thing_topo_get_reply(_IN_ dm_msg_source_t * source)1033 int dm_msg_proc_thing_topo_get_reply(_IN_ dm_msg_source_t *source)
1034 {
1035     int res = 0;
1036     dm_msg_response_payload_t response;
1037 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1038     char int_id[DM_UTILS_UINT32_STRLEN + 1] = { 0 };
1039 #endif
1040 
1041     dm_log_info(DM_URI_THING_TOPO_GET_REPLY);
1042 
1043     memset(&response, 0, sizeof(dm_msg_response_payload_t));
1044 
1045     /* Response */
1046     res = dm_msg_response_parse((char *)source->payload, source->payload_len,
1047                                 &response);
1048     if (res != SUCCESS_RETURN) {
1049         return FAIL_RETURN;
1050     }
1051 
1052     /* Operation */
1053     dm_msg_topo_get_reply(&response);
1054 
1055     /* Remove Message From Cache */
1056 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1057     if (response.id.value_length > DM_UTILS_UINT32_STRLEN) {
1058         return FAIL_RETURN;
1059     }
1060     memcpy(int_id, response.id.value, response.id.value_length);
1061     dm_msg_cache_remove(atoi(int_id));
1062 #endif
1063     return SUCCESS_RETURN;
1064 }
1065 
dm_msg_proc_thing_list_found_reply(_IN_ dm_msg_source_t * source)1066 int dm_msg_proc_thing_list_found_reply(_IN_ dm_msg_source_t *source)
1067 {
1068     int res = 0;
1069     dm_msg_response_payload_t response;
1070 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1071     char int_id[DM_UTILS_UINT32_STRLEN + 1] = { 0 };
1072 #endif
1073 
1074     dm_log_info(DM_URI_THING_TOPO_GET_REPLY);
1075 
1076     memset(&response, 0, sizeof(dm_msg_response_payload_t));
1077 
1078     /* Response */
1079     res = dm_msg_response_parse((char *)source->payload, source->payload_len,
1080                                 &response);
1081     if (res != SUCCESS_RETURN) {
1082         return FAIL_RETURN;
1083     }
1084 
1085     /* Operation */
1086     dm_msg_thing_list_found_reply(&response);
1087 
1088     /* Remove Message From Cache */
1089 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1090     if (response.id.value_length > DM_UTILS_UINT32_STRLEN) {
1091         return FAIL_RETURN;
1092     }
1093     memcpy(int_id, response.id.value, response.id.value_length);
1094     dm_msg_cache_remove(atoi(int_id));
1095 #endif
1096     return SUCCESS_RETURN;
1097 }
1098 
dm_msg_proc_combine_login_reply(_IN_ dm_msg_source_t * source)1099 int dm_msg_proc_combine_login_reply(_IN_ dm_msg_source_t *source)
1100 {
1101     int res = 0;
1102     dm_msg_response_payload_t response;
1103 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1104     char int_id[DM_UTILS_UINT32_STRLEN + 1] = { 0 };
1105 #endif
1106 
1107     dm_log_info(DM_URI_THING_TOPO_GET_REPLY);
1108 
1109     memset(&response, 0, sizeof(dm_msg_response_payload_t));
1110 
1111     /* Response */
1112     res = dm_msg_response_parse((char *)source->payload, source->payload_len,
1113                                 &response);
1114     if (res != SUCCESS_RETURN) {
1115         return FAIL_RETURN;
1116     }
1117 
1118     /* Operation */
1119     dm_msg_combine_login_reply(&response);
1120 
1121     /* Remove Message From Cache */
1122 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1123     if (response.id.value_length > DM_UTILS_UINT32_STRLEN) {
1124         return FAIL_RETURN;
1125     }
1126     memcpy(int_id, response.id.value, response.id.value_length);
1127     dm_msg_cache_remove(atoi(int_id));
1128 #endif
1129     return SUCCESS_RETURN;
1130 }
1131 
dm_msg_proc_combine_logout_reply(_IN_ dm_msg_source_t * source)1132 int dm_msg_proc_combine_logout_reply(_IN_ dm_msg_source_t *source)
1133 {
1134     int res = 0;
1135     dm_msg_response_payload_t response;
1136 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1137     char int_id[DM_UTILS_UINT32_STRLEN + 1] = { 0 };
1138 #endif
1139 
1140     dm_log_info(DM_URI_THING_TOPO_GET_REPLY);
1141 
1142     memset(&response, 0, sizeof(dm_msg_response_payload_t));
1143 
1144     /* Response */
1145     res = dm_msg_response_parse((char *)source->payload, source->payload_len,
1146                                 &response);
1147     if (res != SUCCESS_RETURN) {
1148         return FAIL_RETURN;
1149     }
1150 
1151     /* Operation */
1152     dm_msg_combine_logout_reply(&response);
1153 
1154     /* Remove Message From Cache */
1155 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1156     if (response.id.value_length > DM_UTILS_UINT32_STRLEN) {
1157         return FAIL_RETURN;
1158     }
1159     memcpy(int_id, response.id.value, response.id.value_length);
1160     dm_msg_cache_remove(atoi(int_id));
1161 #endif
1162     return SUCCESS_RETURN;
1163 }
1164 #endif
1165 
1166 #ifdef ALCS_ENABLED
dm_msg_proc_thing_dev_core_service_dev(_IN_ dm_msg_source_t * source,_IN_ dm_msg_dest_t * dest,_OU_ dm_msg_request_payload_t * request,_OU_ dm_msg_response_t * response,_OU_ unsigned char ** data,int * data_len)1167 int dm_msg_proc_thing_dev_core_service_dev(
1168     _IN_ dm_msg_source_t *source, _IN_ dm_msg_dest_t *dest,
1169     _OU_ dm_msg_request_payload_t *request, _OU_ dm_msg_response_t *response,
1170     _OU_ unsigned char **data, int *data_len)
1171 {
1172     int res = 0;
1173 
1174     dm_log_info(DM_URI_DEV_CORE_SERVICE_DEV);
1175 
1176     /* Request */
1177     res = dm_msg_request_parse((char *)source->payload, source->payload_len,
1178                                request);
1179     if (res < SUCCESS_RETURN) {
1180         return res;
1181     }
1182 
1183     /* Operation */
1184     res = dm_msg_dev_core_service_dev((char **)data, data_len);
1185     if (res < SUCCESS_RETURN) {
1186         return res;
1187     }
1188 
1189     /* Response */
1190     response->service_prefix = NULL;
1191     response->service_name = dest->uri_name;
1192     response->code = (res == SUCCESS_RETURN) ? (IOTX_DM_ERR_CODE_SUCCESS)
1193                                              : (IOTX_DM_ERR_CODE_REQUEST_ERROR);
1194 
1195     return SUCCESS_RETURN;
1196 }
1197 #endif
1198 
dm_msg_proc_thing_model_user_sub(_IN_ dm_msg_source_t * source)1199 int dm_msg_proc_thing_model_user_sub(_IN_ dm_msg_source_t *source)
1200 {
1201     int res = 0;
1202     char product_key[IOTX_PRODUCT_KEY_LEN] = { 0 };
1203     char device_name[IOTX_DEVICE_NAME_LEN] = { 0 };
1204 
1205     /* Parse Product Key And Device Name */
1206     res = dm_msg_uri_parse_pkdn((char *)source->uri, strlen(source->uri),
1207                                 2 + DM_URI_OFFSET, 4 + DM_URI_OFFSET,
1208                                 product_key, device_name);
1209     if (res != SUCCESS_RETURN) {
1210         return FAIL_RETURN;
1211     }
1212     return dm_msg_thing_model_user_sub(
1213         product_key, device_name, (char *)source->payload, source->payload_len);
1214 }
1215