1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 #include "iotx_dm_internal.h"
6 
7 static dm_mgr_ctx g_dm_mgr = { 0 };
8 
_dm_mgr_get_ctx(void)9 static dm_mgr_ctx *_dm_mgr_get_ctx(void)
10 {
11     return &g_dm_mgr;
12 }
13 
_dm_mgr_mutex_lock(void)14 static void _dm_mgr_mutex_lock(void)
15 {
16     dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
17     if (ctx->mutex) {
18         HAL_MutexLock(ctx->mutex);
19     }
20 }
21 
_dm_mgr_mutex_unlock(void)22 static void _dm_mgr_mutex_unlock(void)
23 {
24     dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
25     if (ctx->mutex) {
26         HAL_MutexUnlock(ctx->mutex);
27     }
28 }
29 
_dm_mgr_next_devid(void)30 static int _dm_mgr_next_devid(void)
31 {
32     dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
33 
34     return ctx->global_devid++;
35 }
36 
_dm_mgr_search_dev_by_devid(_IN_ int devid,_OU_ dm_mgr_dev_node_t ** node)37 static int _dm_mgr_search_dev_by_devid(_IN_ int devid,
38                                        _OU_ dm_mgr_dev_node_t **node)
39 {
40     dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
41     dm_mgr_dev_node_t *search_node = NULL;
42 
43     list_for_each_entry(search_node, &ctx->dev_list, linked_list,
44                         dm_mgr_dev_node_t)
45     {
46         if (search_node->devid == devid) {
47             /* dm_log_debug("Device Found, devid: %d", devid); */
48             if (node) {
49                 *node = search_node;
50             }
51             return SUCCESS_RETURN;
52         }
53     }
54 
55     dm_log_debug("Device Not Found, devid: %d", devid);
56     return FAIL_RETURN;
57 }
58 
_dm_mgr_search_dev_by_pkdn(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN+1],_IN_ char device_name[IOTX_DEVICE_NAME_LEN+1],_OU_ dm_mgr_dev_node_t ** node)59 static int _dm_mgr_search_dev_by_pkdn(
60     _IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
61     _IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
62     _OU_ dm_mgr_dev_node_t **node)
63 {
64     dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
65     dm_mgr_dev_node_t *search_node = NULL;
66 
67     list_for_each_entry(search_node, &ctx->dev_list, linked_list,
68                         dm_mgr_dev_node_t)
69     {
70         if ((strlen(search_node->product_key) == strlen(product_key)) &&
71             (memcmp(search_node->product_key, product_key,
72                     strlen(product_key)) == 0) &&
73             (strlen(search_node->device_name) == strlen(device_name)) &&
74             (memcmp(search_node->device_name, device_name,
75                     strlen(device_name)) == 0)) {
76             /* dm_log_debug("Device Found, Product Key: %s, Device Name: %s",
77              * product_key, device_name); */
78             if (node) {
79                 *node = search_node;
80             }
81             return SUCCESS_RETURN;
82         }
83     }
84 
85     dm_log_debug("Device Not Found, Product Key: %s, Device Name: %s",
86                  product_key, device_name);
87     return FAIL_RETURN;
88 }
89 
_dm_mgr_insert_dev(_IN_ int devid,_IN_ int dev_type,char product_key[IOTX_PRODUCT_KEY_LEN+1],char device_name[IOTX_DEVICE_NAME_LEN+1])90 static int _dm_mgr_insert_dev(_IN_ int devid, _IN_ int dev_type,
91                               char product_key[IOTX_PRODUCT_KEY_LEN + 1],
92                               char device_name[IOTX_DEVICE_NAME_LEN + 1])
93 {
94     int res = 0;
95     dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
96     dm_mgr_dev_node_t *node = NULL;
97 
98     if (devid < 0 || product_key == NULL ||
99         strlen(product_key) >= IOTX_PRODUCT_KEY_LEN + 1 ||
100         device_name == NULL ||
101         strlen(device_name) >= IOTX_DEVICE_NAME_LEN + 1) {
102         return DM_INVALID_PARAMETER;
103     }
104 
105     res = _dm_mgr_search_dev_by_devid(devid, NULL);
106     if (res == SUCCESS_RETURN) {
107         return FAIL_RETURN;
108     }
109 
110     node = DM_malloc(sizeof(dm_mgr_dev_node_t));
111     if (node == NULL) {
112         return DM_MEMORY_NOT_ENOUGH;
113     }
114     memset(node, 0, sizeof(dm_mgr_dev_node_t));
115 
116     node->devid = devid;
117     node->dev_type = dev_type;
118     memcpy(node->product_key, product_key, strlen(product_key));
119     memcpy(node->device_name, device_name, strlen(device_name));
120     INIT_LIST_HEAD(&node->linked_list);
121 
122     list_add_tail(&node->linked_list, &ctx->dev_list);
123 
124     return SUCCESS_RETURN;
125 }
126 
_dm_mgr_destroy_devlist(void)127 static void _dm_mgr_destroy_devlist(void)
128 {
129     dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
130     dm_mgr_dev_node_t *del_node = NULL;
131     dm_mgr_dev_node_t *next_node = NULL;
132 
133     list_for_each_entry_safe(del_node, next_node, &ctx->dev_list, linked_list,
134                              dm_mgr_dev_node_t)
135     {
136         list_del(&del_node->linked_list);
137 #ifdef DEPRECATED_LINKKIT
138         dm_shw_destroy(&del_node->dev_shadow);
139 #endif
140         DM_free(del_node);
141     }
142 }
143 
144 #ifdef DEPRECATED_LINKKIT
_dm_mgr_legacy_thing_created(int devid)145 static int _dm_mgr_legacy_thing_created(int devid)
146 {
147     int res = 0, message_len = 0;
148     const char *thing_created_fmt = "{\"devid\":%d}";
149     char *message = NULL;
150 
151     message_len = strlen(thing_created_fmt) + DM_UTILS_UINT32_STRLEN + 1;
152     message = DM_malloc(message_len);
153     if (message == NULL) {
154         return DM_MEMORY_NOT_ENOUGH;
155     }
156     memset(message, 0, message_len);
157     HAL_Snprintf(message, message_len, thing_created_fmt, devid);
158 
159     res = _dm_msg_send_to_user(IOTX_DM_EVENT_LEGACY_THING_CREATED, message);
160     if (res != SUCCESS_RETURN) {
161         DM_free(message);
162         return FAIL_RETURN;
163     }
164 
165     return SUCCESS_RETURN;
166 }
167 #endif
168 
dm_mgr_init(void)169 int dm_mgr_init(void)
170 {
171     int res = 0;
172     dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
173     char product_key[IOTX_PRODUCT_KEY_LEN + 1] = { 0 };
174     char device_name[IOTX_DEVICE_NAME_LEN + 1] = { 0 };
175 
176     memset(ctx, 0, sizeof(dm_mgr_ctx));
177 
178     /* Create Mutex */
179     ctx->mutex = HAL_MutexCreate();
180     if (ctx->mutex == NULL) {
181         goto ERROR;
182     }
183 
184     /* Init Device Id*/
185     ctx->global_devid = IOTX_DM_LOCAL_NODE_DEVID + 1;
186 
187     /* Init Device List */
188     INIT_LIST_HEAD(&ctx->dev_list);
189 
190     /* Local Node */
191     HAL_GetProductKey(product_key);
192     HAL_GetDeviceName(device_name);
193     res = _dm_mgr_insert_dev(IOTX_DM_LOCAL_NODE_DEVID, IOTX_DM_DEVICE_TYPE,
194                              product_key, device_name);
195     if (res != SUCCESS_RETURN) {
196         goto ERROR;
197     }
198 
199 #ifdef DEPRECATED_LINKKIT
200     _dm_mgr_legacy_thing_created(IOTX_DM_LOCAL_NODE_DEVID);
201 #endif
202 
203     return SUCCESS_RETURN;
204 
205 ERROR:
206     if (ctx->mutex) {
207         HAL_MutexDestroy(ctx->mutex);
208     }
209     memset(ctx, 0, sizeof(dm_mgr_ctx));
210     return FAIL_RETURN;
211 }
212 
dm_mgr_deinit(void)213 int dm_mgr_deinit(void)
214 {
215     dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
216 
217     _dm_mgr_mutex_lock();
218     _dm_mgr_destroy_devlist();
219     _dm_mgr_mutex_unlock();
220 
221     if (ctx->mutex) {
222         HAL_MutexDestroy(ctx->mutex);
223     }
224 
225     return SUCCESS_RETURN;
226 }
227 
dm_mgr_device_query(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN+1],_IN_ char device_name[IOTX_DEVICE_NAME_LEN+1],_OU_ int * devid)228 int dm_mgr_device_query(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
229                         _IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
230                         _OU_ int *devid)
231 {
232     int res = 0;
233     dm_mgr_dev_node_t *node = NULL;
234 
235     /* duplicated parameters check is removed */
236 
237     res = _dm_mgr_search_dev_by_pkdn(product_key, device_name, &node);
238     if (res == SUCCESS_RETURN) {
239         if (devid) {
240             *devid = node->devid;
241         }
242         return SUCCESS_RETURN;
243     }
244 
245     return FAIL_RETURN;
246 }
247 
dm_mgr_device_create(_IN_ int dev_type,_IN_ char product_key[IOTX_PRODUCT_KEY_LEN+1],_IN_ char product_secret[IOTX_PRODUCT_SECRET_LEN+1],_IN_ char device_name[IOTX_DEVICE_NAME_LEN+1],_IN_ char device_secret[IOTX_DEVICE_SECRET_LEN+1],_OU_ int * devid)248 int dm_mgr_device_create(_IN_ int dev_type,
249                          _IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
250                          _IN_ char product_secret[IOTX_PRODUCT_SECRET_LEN + 1],
251                          _IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
252                          _IN_ char device_secret[IOTX_DEVICE_SECRET_LEN + 1],
253                          _OU_ int *devid)
254 {
255     int res = 0;
256     dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
257     dm_mgr_dev_node_t *node = NULL;
258 
259     if (product_key == NULL || product_secret == NULL || device_name == NULL ||
260         strlen(product_key) >= IOTX_PRODUCT_KEY_LEN + 1 ||
261         strlen(product_secret) >= IOTX_PRODUCT_SECRET_LEN + 1 ||
262         strlen(device_name) >= IOTX_DEVICE_NAME_LEN + 1) {
263         return DM_INVALID_PARAMETER;
264     }
265 
266     if (device_secret != NULL &&
267         strlen(device_secret) >= IOTX_DEVICE_SECRET_LEN + 1) {
268         return DM_INVALID_PARAMETER;
269     }
270 
271     res = _dm_mgr_search_dev_by_pkdn(product_key, device_name, &node);
272     if (res == SUCCESS_RETURN) {
273         if (devid) {
274             *devid = node->devid;
275         }
276         return FAIL_RETURN;
277     }
278 
279     node = DM_malloc(sizeof(dm_mgr_dev_node_t));
280     if (node == NULL) {
281         return DM_MEMORY_NOT_ENOUGH;
282     }
283     memset(node, 0, sizeof(dm_mgr_dev_node_t));
284 
285     node->devid = _dm_mgr_next_devid();
286     node->dev_type = dev_type;
287 #if defined(DEPRECATED_LINKKIT)
288     node->dev_shadow = NULL;
289     node->tsl_source = IOTX_DM_TSL_SOURCE_CLOUD;
290 #endif
291     memcpy(node->product_key, product_key, strlen(product_key));
292     memcpy(node->product_secret, product_secret, strlen(product_secret));
293     memcpy(node->device_name, device_name, strlen(device_name));
294     if (device_secret != NULL) {
295         memcpy(node->device_secret, device_secret, strlen(device_secret));
296     }
297     node->dev_status = IOTX_DM_DEV_STATUS_AUTHORIZED;
298     INIT_LIST_HEAD(&node->linked_list);
299 
300     list_add_tail(&node->linked_list, &ctx->dev_list);
301 
302     if (devid) {
303         *devid = node->devid;
304     }
305 
306     return SUCCESS_RETURN;
307 }
308 
dm_mgr_device_destroy(_IN_ int devid)309 int dm_mgr_device_destroy(_IN_ int devid)
310 {
311     int res = 0;
312     dm_mgr_dev_node_t *node = NULL;
313 
314     if (devid < 0) {
315         return DM_INVALID_PARAMETER;
316     }
317 
318     res = _dm_mgr_search_dev_by_devid(devid, &node);
319     if (res != SUCCESS_RETURN) {
320         return FAIL_RETURN;
321     }
322 
323     if (node->devid == IOTX_DM_LOCAL_NODE_DEVID) {
324         return FAIL_RETURN;
325     }
326 
327     list_del(&node->linked_list);
328 
329 #if defined(DEPRECATED_LINKKIT)
330     if (node->dev_shadow) {
331         dm_shw_destroy(&node->dev_shadow);
332     }
333 #endif
334 
335 #ifdef DEVICE_MODEL_GATEWAY
336     dm_client_subdev_unsubscribe(node->product_key, node->device_name);
337 #endif
338 
339     DM_free(node);
340 
341     return SUCCESS_RETURN;
342 }
343 
dm_mgr_device_number(void)344 int dm_mgr_device_number(void)
345 {
346     int index = 0;
347     dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
348     dm_mgr_dev_node_t *search_node = NULL;
349 
350     list_for_each_entry(search_node, &ctx->dev_list, linked_list,
351                         dm_mgr_dev_node_t)
352     {
353         index++;
354     }
355 
356     return index;
357 }
358 
dm_mgr_get_devid_by_index(_IN_ int index,_OU_ int * devid)359 int dm_mgr_get_devid_by_index(_IN_ int index, _OU_ int *devid)
360 {
361     int search_index = 0;
362     dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
363     dm_mgr_dev_node_t *search_node = NULL;
364 
365     if (index < 0 || devid == NULL) {
366         return DM_INVALID_PARAMETER;
367     }
368 
369     list_for_each_entry(search_node, &ctx->dev_list, linked_list,
370                         dm_mgr_dev_node_t)
371     {
372         if (search_index == index) {
373             *devid = search_node->devid;
374             return SUCCESS_RETURN;
375         }
376         search_index++;
377     }
378 
379     return FAIL_RETURN;
380 }
381 
dm_mgr_get_next_devid(_IN_ int devid,_OU_ int * devid_next)382 int dm_mgr_get_next_devid(_IN_ int devid, _OU_ int *devid_next)
383 {
384     dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
385     dm_mgr_dev_node_t *search_node = NULL;
386     dm_mgr_dev_node_t *next_node = NULL;
387 
388     if (devid < 0 || devid_next == NULL) {
389         return DM_INVALID_PARAMETER;
390     }
391 
392     list_for_each_entry(next_node, &ctx->dev_list, linked_list,
393                         dm_mgr_dev_node_t)
394     {
395         if (search_node && search_node->devid == devid) {
396             *devid_next = next_node->devid;
397             return SUCCESS_RETURN;
398         }
399 
400         if (next_node->devid == devid) {
401             search_node = next_node;
402         }
403     }
404 
405     return FAIL_RETURN;
406 }
407 
dm_mgr_search_device_by_devid(_IN_ int devid,_OU_ char product_key[IOTX_PRODUCT_KEY_LEN+1],_OU_ char device_name[IOTX_DEVICE_NAME_LEN+1],_OU_ char device_secret[IOTX_DEVICE_SECRET_LEN+1])408 int dm_mgr_search_device_by_devid(
409     _IN_ int devid, _OU_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
410     _OU_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
411     _OU_ char device_secret[IOTX_DEVICE_SECRET_LEN + 1])
412 {
413     int res = 0;
414     dm_mgr_dev_node_t *node = NULL;
415 
416     if (product_key == NULL || device_name == NULL || device_secret == NULL) {
417         return DM_INVALID_PARAMETER;
418     }
419 
420     res = _dm_mgr_search_dev_by_devid(devid, &node);
421     if (res != SUCCESS_RETURN) {
422         return FAIL_RETURN;
423     }
424 
425     memcpy(product_key, node->product_key, strlen(node->product_key));
426     memcpy(device_name, node->device_name, strlen(node->device_name));
427     memcpy(device_secret, node->device_secret, strlen(node->device_secret));
428 
429     return SUCCESS_RETURN;
430 }
431 
dm_mgr_search_device_by_pkdn(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN+1],_IN_ char device_name[IOTX_DEVICE_NAME_LEN+1],_OU_ int * devid)432 int dm_mgr_search_device_by_pkdn(
433     _IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
434     _IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1], _OU_ int *devid)
435 {
436     int res = 0;
437     dm_mgr_dev_node_t *node = NULL;
438 
439     if (product_key == NULL || device_name == NULL) {
440         return DM_INVALID_PARAMETER;
441     }
442 
443     res = _dm_mgr_search_dev_by_pkdn(product_key, device_name, &node);
444     if (res != SUCCESS_RETURN) {
445         return FAIL_RETURN;
446     }
447 
448     if (devid) {
449         *devid = node->devid;
450     }
451 
452     return SUCCESS_RETURN;
453 }
454 
dm_mgr_search_device_node_by_devid(_IN_ int devid,_OU_ void ** node)455 int dm_mgr_search_device_node_by_devid(_IN_ int devid, _OU_ void **node)
456 {
457     int res = 0;
458     dm_mgr_dev_node_t *search_node = NULL;
459 
460     res = _dm_mgr_search_dev_by_devid(devid, &search_node);
461     if (res != SUCCESS_RETURN) {
462         return FAIL_RETURN;
463     }
464 
465     if (node) {
466         *node = (void *)search_node;
467     }
468 
469     return SUCCESS_RETURN;
470 }
471 
dm_mgr_get_dev_type(_IN_ int devid,_OU_ int * dev_type)472 int dm_mgr_get_dev_type(_IN_ int devid, _OU_ int *dev_type)
473 {
474     int res = 0;
475     dm_mgr_dev_node_t *node = NULL;
476 
477     if (devid < 0 || dev_type == NULL) {
478         return DM_INVALID_PARAMETER;
479     }
480 
481     res = _dm_mgr_search_dev_by_devid(devid, &node);
482     if (res != SUCCESS_RETURN) {
483         return FAIL_RETURN;
484     }
485 
486     *dev_type = node->dev_type;
487 
488     return SUCCESS_RETURN;
489 }
490 
dm_mgr_set_dev_enable(_IN_ int devid)491 int dm_mgr_set_dev_enable(_IN_ int devid)
492 {
493     int res = 0;
494     dm_mgr_dev_node_t *node = NULL;
495 
496     if (devid < 0) {
497         return DM_INVALID_PARAMETER;
498     }
499 
500     res = _dm_mgr_search_dev_by_devid(devid, &node);
501     if (res != SUCCESS_RETURN) {
502         return FAIL_RETURN;
503     }
504 
505     node->status = IOTX_DM_DEV_AVAIL_ENABLE;
506 
507     return SUCCESS_RETURN;
508 }
509 
dm_mgr_set_dev_disable(_IN_ int devid)510 int dm_mgr_set_dev_disable(_IN_ int devid)
511 {
512     int res = 0;
513     dm_mgr_dev_node_t *node = NULL;
514 
515     if (devid < 0) {
516         return DM_INVALID_PARAMETER;
517     }
518 
519     res = _dm_mgr_search_dev_by_devid(devid, &node);
520     if (res != SUCCESS_RETURN) {
521         return FAIL_RETURN;
522     }
523 
524     node->status = IOTX_DM_DEV_AVAIL_DISABLE;
525 
526     return SUCCESS_RETURN;
527 }
528 
dm_mgr_get_dev_avail(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN+1],_IN_ char device_name[IOTX_DEVICE_NAME_LEN+1],_OU_ iotx_dm_dev_avail_t * status)529 int dm_mgr_get_dev_avail(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
530                          _IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
531                          _OU_ iotx_dm_dev_avail_t *status)
532 {
533     int res = 0;
534     dm_mgr_dev_node_t *node = NULL;
535 
536     if (product_key == NULL || device_name == NULL || status == NULL ||
537         (strlen(product_key) >= IOTX_PRODUCT_KEY_LEN + 1) ||
538         (strlen(device_name) >= IOTX_DEVICE_NAME_LEN + 1)) {
539         return DM_INVALID_PARAMETER;
540     }
541 
542     res = _dm_mgr_search_dev_by_pkdn(product_key, device_name, &node);
543     if (res != SUCCESS_RETURN) {
544         return FAIL_RETURN;
545     }
546 
547     *status = node->status;
548 
549     return SUCCESS_RETURN;
550 }
551 
dm_mgr_set_dev_status(_IN_ int devid,_IN_ iotx_dm_dev_status_t status)552 int dm_mgr_set_dev_status(_IN_ int devid, _IN_ iotx_dm_dev_status_t status)
553 {
554     int res = 0;
555     dm_mgr_dev_node_t *node = NULL;
556 
557     if (devid < 0) {
558         return DM_INVALID_PARAMETER;
559     }
560 
561     res = _dm_mgr_search_dev_by_devid(devid, &node);
562     if (res != SUCCESS_RETURN) {
563         return FAIL_RETURN;
564     }
565 
566     node->dev_status = status;
567 
568     return SUCCESS_RETURN;
569 }
570 
dm_mgr_get_dev_status(_IN_ int devid,_OU_ iotx_dm_dev_status_t * status)571 int dm_mgr_get_dev_status(_IN_ int devid, _OU_ iotx_dm_dev_status_t *status)
572 {
573     int res = 0;
574     dm_mgr_dev_node_t *node = NULL;
575 
576     if (devid < 0 || status == NULL) {
577         return DM_INVALID_PARAMETER;
578     }
579 
580     res = _dm_mgr_search_dev_by_devid(devid, &node);
581     if (res != SUCCESS_RETURN) {
582         return FAIL_RETURN;
583     }
584 
585     *status = node->dev_status;
586 
587     return SUCCESS_RETURN;
588 }
589 
dm_mgr_set_device_secret(_IN_ int devid,_IN_ char device_secret[IOTX_DEVICE_SECRET_LEN+1])590 int dm_mgr_set_device_secret(
591     _IN_ int devid, _IN_ char device_secret[IOTX_DEVICE_SECRET_LEN + 1])
592 {
593     int res = 0;
594     dm_mgr_dev_node_t *node = NULL;
595 
596     if (devid < 0 || device_secret == NULL ||
597         strlen(device_secret) >= IOTX_DEVICE_SECRET_LEN + 1) {
598         return DM_INVALID_PARAMETER;
599     }
600 
601     res = _dm_mgr_search_dev_by_devid(devid, &node);
602     if (res != SUCCESS_RETURN) {
603         return FAIL_RETURN;
604     }
605 
606     memset(node->device_secret, 0, IOTX_DEVICE_SECRET_LEN + 1);
607     memcpy(node->device_secret, device_secret, strlen(device_secret));
608 
609     return SUCCESS_RETURN;
610 }
611 
dm_mgr_dev_initialized(int devid)612 int dm_mgr_dev_initialized(int devid)
613 {
614     int res = 0, message_len = 0;
615     char *message = NULL;
616     const char *fmt = "{\"devid\":%d}";
617 
618     message_len = strlen(fmt) + DM_UTILS_UINT32_STRLEN + 1;
619     message = DM_malloc(message_len);
620     if (message == NULL) {
621         return DM_MEMORY_NOT_ENOUGH;
622     }
623     memset(message, 0, message_len);
624     HAL_Snprintf(message, message_len, fmt, devid);
625 
626     res = _dm_msg_send_to_user(IOTX_DM_EVENT_INITIALIZED, message);
627     if (res != SUCCESS_RETURN) {
628         DM_free(message);
629         return FAIL_RETURN;
630     }
631 
632     return SUCCESS_RETURN;
633 }
634 
635 #ifdef DEVICE_MODEL_GATEWAY
dm_mgr_upstream_thing_sub_register(_IN_ int devid)636 int dm_mgr_upstream_thing_sub_register(_IN_ int devid)
637 {
638     int res = 0;
639     dm_mgr_dev_node_t *node = NULL;
640     dm_msg_request_t request;
641 
642     if (devid < 0) {
643         return DM_INVALID_PARAMETER;
644     }
645 
646     res = _dm_mgr_search_dev_by_devid(devid, &node);
647     if (res != SUCCESS_RETURN) {
648         return FAIL_RETURN;
649     }
650 
651     memset(&request, 0, sizeof(dm_msg_request_t));
652     request.service_prefix = DM_URI_SYS_PREFIX;
653     request.service_name = DM_URI_THING_SUB_REGISTER;
654     HAL_GetProductKey(request.product_key);
655     HAL_GetDeviceName(request.device_name);
656 
657     /* Get Params And Method */
658     res = dm_msg_thing_sub_register(node->product_key, node->device_name,
659                                     &request);
660     if (res != SUCCESS_RETURN) {
661         return FAIL_RETURN;
662     }
663 
664     /* Get Msg ID */
665     request.msgid = iotx_report_id();
666 
667     /* Get Dev ID */
668     request.devid = devid;
669 
670     /* Callback */
671     request.callback = dm_client_thing_sub_register_reply;
672 
673 #if !defined(DM_MESSAGE_CACHE_DISABLED)
674     dm_msg_cache_insert(request.msgid, request.devid,
675                         IOTX_DM_EVENT_SUBDEV_REGISTER_REPLY, NULL);
676 #endif
677 
678     /* Send Message To Cloud */
679     res = dm_msg_request(DM_MSG_DEST_CLOUD, &request);
680 #if !defined(DM_MESSAGE_CACHE_DISABLED)
681     if (res != SUCCESS_RETURN) {
682         dm_msg_cache_remove(request.msgid);
683     } else {
684         res = request.msgid;
685     }
686 #endif
687     DM_free(request.params);
688 
689     return res;
690 }
691 
dm_mgr_upstream_thing_proxy_product_register(_IN_ int devid)692 int dm_mgr_upstream_thing_proxy_product_register(_IN_ int devid)
693 {
694     int res = 0;
695     dm_mgr_dev_node_t *node = NULL;
696     dm_msg_request_t request;
697 
698     if (devid < 0) {
699         return DM_INVALID_PARAMETER;
700     }
701 
702     res = _dm_mgr_search_dev_by_devid(devid, &node);
703     if (res != SUCCESS_RETURN) {
704         return FAIL_RETURN;
705     }
706 
707     memset(&request, 0, sizeof(dm_msg_request_t));
708     request.service_prefix = DM_URI_SYS_PREFIX;
709     request.service_name = DM_URI_THING_PROXY_PRODUCT_REGISTER;
710     HAL_GetProductKey(request.product_key);
711     HAL_GetDeviceName(request.device_name);
712 
713     /* Get Params And Method */
714     res = dm_msg_thing_proxy_product_register(
715         node->product_key, node->product_secret, node->device_name, &request);
716     if (res != SUCCESS_RETURN) {
717         return FAIL_RETURN;
718     }
719 
720     /* Get Msg ID */
721     request.msgid = iotx_report_id();
722 
723     /* Get Dev ID */
724     request.devid = devid;
725 
726     /* Callback */
727     request.callback = dm_client_thing_proxy_product_register_reply;
728 #if !defined(DM_MESSAGE_CACHE_DISABLED)
729     dm_msg_cache_insert(request.msgid, request.devid,
730                         IOTX_DM_EVENT_PROXY_PRODUCT_REGISTER_REPLY, NULL);
731 #endif
732     /* Send Message To Cloud */
733     res = dm_msg_request(DM_MSG_DEST_CLOUD, &request);
734 #if !defined(DM_MESSAGE_CACHE_DISABLED)
735     if (res != SUCCESS_RETURN) {
736         dm_msg_cache_remove(request.msgid);
737     } else {
738         res = request.msgid;
739     }
740 #endif
741     DM_free(request.params);
742 
743     return res;
744 }
745 
dm_mgr_upstream_thing_sub_unregister(_IN_ int devid)746 int dm_mgr_upstream_thing_sub_unregister(_IN_ int devid)
747 {
748     int res = 0;
749     dm_mgr_dev_node_t *node = NULL;
750     dm_msg_request_t request;
751 
752     if (devid < 0) {
753         return DM_INVALID_PARAMETER;
754     }
755 
756     res = _dm_mgr_search_dev_by_devid(devid, &node);
757     if (res != SUCCESS_RETURN) {
758         return FAIL_RETURN;
759     }
760 
761     memset(&request, 0, sizeof(dm_msg_request_t));
762     request.service_prefix = DM_URI_SYS_PREFIX;
763     request.service_name = DM_URI_THING_SUB_UNREGISTER;
764     HAL_GetProductKey(request.product_key);
765     HAL_GetDeviceName(request.device_name);
766 
767     /* Get Params And Method */
768     res = dm_msg_thing_sub_unregister(node->product_key, node->device_name,
769                                       &request);
770     if (res != SUCCESS_RETURN) {
771         return FAIL_RETURN;
772     }
773 
774     /* Get Msg ID */
775     request.msgid = iotx_report_id();
776 
777     /* Get Dev ID */
778     request.devid = devid;
779 
780     /* Callback */
781     request.callback = dm_client_thing_sub_unregister_reply;
782 #if !defined(DM_MESSAGE_CACHE_DISABLED)
783     dm_msg_cache_insert(request.msgid, request.devid,
784                         IOTX_DM_EVENT_SUBDEV_UNREGISTER_REPLY, NULL);
785 #endif
786 
787     /* Send Message To Cloud */
788     res = dm_msg_request(DM_MSG_DEST_CLOUD, &request);
789 #if !defined(DM_MESSAGE_CACHE_DISABLED)
790     if (res != SUCCESS_RETURN) {
791         dm_msg_cache_remove(request.msgid);
792     } else {
793         res = request.msgid;
794     }
795 #endif
796     DM_free(request.params);
797 
798     return res;
799 }
800 
dm_mgr_upstream_thing_topo_add(_IN_ int devid)801 int dm_mgr_upstream_thing_topo_add(_IN_ int devid)
802 {
803     int res = 0;
804     dm_mgr_dev_node_t *node = NULL;
805     dm_msg_request_t request;
806 
807     if (devid < 0) {
808         return DM_INVALID_PARAMETER;
809     }
810 
811     res = _dm_mgr_search_dev_by_devid(devid, &node);
812     if (res != SUCCESS_RETURN) {
813         return FAIL_RETURN;
814     }
815 
816     memset(&request, 0, sizeof(dm_msg_request_t));
817     request.service_prefix = DM_URI_SYS_PREFIX;
818     request.service_name = DM_URI_THING_TOPO_ADD;
819     HAL_GetProductKey(request.product_key);
820     HAL_GetDeviceName(request.device_name);
821 
822     /* Get Params And Method */
823     res = dm_msg_thing_topo_add(node->product_key, node->device_name,
824                                 node->device_secret, &request);
825     if (res != SUCCESS_RETURN) {
826         return FAIL_RETURN;
827     }
828 
829     /* Get Msg ID */
830     request.msgid = iotx_report_id();
831 
832     /* Get Dev ID */
833     request.devid = devid;
834 
835     /* Callback */
836     request.callback = dm_client_thing_topo_add_reply;
837 #if !defined(DM_MESSAGE_CACHE_DISABLED)
838     dm_msg_cache_insert(request.msgid, request.devid,
839                         IOTX_DM_EVENT_TOPO_ADD_REPLY, NULL);
840 #endif
841     /* Send Message To Cloud */
842     res = dm_msg_request(DM_MSG_DEST_CLOUD, &request);
843 #if !defined(DM_MESSAGE_CACHE_DISABLED)
844     if (res != SUCCESS_RETURN) {
845         dm_msg_cache_remove(request.msgid);
846     } else {
847         res = request.msgid;
848     }
849 #endif
850     DM_free(request.params);
851 
852     return res;
853 }
854 
dm_mgr_upstream_thing_topo_delete(_IN_ int devid)855 int dm_mgr_upstream_thing_topo_delete(_IN_ int devid)
856 {
857     int res = 0;
858     dm_mgr_dev_node_t *node = NULL;
859     dm_msg_request_t request;
860 
861     if (devid < 0) {
862         return DM_INVALID_PARAMETER;
863     }
864 
865     res = _dm_mgr_search_dev_by_devid(devid, &node);
866     if (res != SUCCESS_RETURN) {
867         return FAIL_RETURN;
868     }
869 
870     memset(&request, 0, sizeof(dm_msg_request_t));
871     request.service_prefix = DM_URI_SYS_PREFIX;
872     request.service_name = DM_URI_THING_TOPO_DELETE;
873     HAL_GetProductKey(request.product_key);
874     HAL_GetDeviceName(request.device_name);
875 
876     /* Get Params And Method */
877     res = dm_msg_thing_topo_delete(node->product_key, node->device_name,
878                                    &request);
879     if (res != SUCCESS_RETURN) {
880         return FAIL_RETURN;
881     }
882 
883     /* Get Msg ID */
884     request.msgid = iotx_report_id();
885 
886     /* Get Dev ID */
887     request.devid = devid;
888 
889     /* Callback */
890     request.callback = dm_client_thing_topo_delete_reply;
891 #if !defined(DM_MESSAGE_CACHE_DISABLED)
892     dm_msg_cache_insert(request.msgid, request.devid,
893                         IOTX_DM_EVENT_TOPO_DELETE_REPLY, NULL);
894 #endif
895     /* Send Message To Cloud */
896     res = dm_msg_request(DM_MSG_DEST_CLOUD, &request);
897 #if !defined(DM_MESSAGE_CACHE_DISABLED)
898     if (res != SUCCESS_RETURN) {
899         dm_msg_cache_remove(request.msgid);
900     } else {
901         res = request.msgid;
902     }
903 #endif
904     DM_free(request.params);
905 
906     return res;
907 }
908 
dm_mgr_upstream_thing_topo_get(void)909 int dm_mgr_upstream_thing_topo_get(void)
910 {
911     int res = 0;
912     dm_mgr_dev_node_t *node = NULL;
913     dm_msg_request_t request;
914 
915     memset(&request, 0, sizeof(dm_msg_request_t));
916     request.service_prefix = DM_URI_SYS_PREFIX;
917     request.service_name = DM_URI_THING_TOPO_GET;
918     HAL_GetProductKey(request.product_key);
919     HAL_GetDeviceName(request.device_name);
920 
921     res = _dm_mgr_search_dev_by_pkdn(request.product_key, request.device_name,
922                                      &node);
923     if (res != SUCCESS_RETURN) {
924         return FAIL_RETURN;
925     }
926 
927     /* Get Params And Method */
928     res = dm_msg_thing_topo_get(&request);
929     if (res != SUCCESS_RETURN) {
930         return FAIL_RETURN;
931     }
932 
933     /* Get Msg ID */
934     request.msgid = iotx_report_id();
935 
936     /* Get Dev ID */
937     request.devid = node->devid;
938 
939     /* Callback */
940     request.callback = dm_client_thing_topo_get_reply;
941 #if !defined(DM_MESSAGE_CACHE_DISABLED)
942     dm_msg_cache_insert(request.msgid, request.devid,
943                         IOTX_DM_EVENT_TOPO_GET_REPLY, NULL);
944 #endif
945     /* Send Message To Cloud */
946     res = dm_msg_request(DM_MSG_DEST_CLOUD, &request);
947 #if !defined(DM_MESSAGE_CACHE_DISABLED)
948     if (res != SUCCESS_RETURN) {
949         dm_msg_cache_remove(request.msgid);
950     } else {
951         res = request.msgid;
952     }
953 #endif
954     DM_free(request.params);
955 
956     return res;
957 }
958 
dm_mgr_upstream_thing_list_found(_IN_ int devid)959 int dm_mgr_upstream_thing_list_found(_IN_ int devid)
960 {
961     int res = 0;
962     dm_mgr_dev_node_t *node = NULL;
963     dm_msg_request_t request;
964 
965     if (devid < 0) {
966         return DM_INVALID_PARAMETER;
967     }
968 
969     res = _dm_mgr_search_dev_by_devid(devid, &node);
970     if (res != SUCCESS_RETURN) {
971         return FAIL_RETURN;
972     }
973 
974     memset(&request, 0, sizeof(dm_msg_request_t));
975     request.service_prefix = DM_URI_SYS_PREFIX;
976     request.service_name = DM_URI_THING_LIST_FOUND;
977     HAL_GetProductKey(request.product_key);
978     HAL_GetDeviceName(request.device_name);
979 
980     /* Get Params And Method */
981     res =
982         dm_msg_thing_list_found(node->product_key, node->device_name, &request);
983     if (res != SUCCESS_RETURN) {
984         return FAIL_RETURN;
985     }
986 
987     /* Get Msg ID */
988     request.msgid = iotx_report_id();
989 
990     /* Get Dev ID */
991     request.devid = devid;
992 
993     /* Callback */
994     request.callback = dm_client_thing_list_found_reply;
995 #if !defined(DM_MESSAGE_CACHE_DISABLED)
996     dm_msg_cache_insert(request.msgid, request.devid,
997                         IOTX_DM_EVENT_TOPO_ADD_NOTIFY_REPLY, NULL);
998 #endif
999     /* Send Message To Cloud */
1000     res = dm_msg_request(DM_MSG_DEST_CLOUD, &request);
1001 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1002     if (res != SUCCESS_RETURN) {
1003         dm_msg_cache_remove(request.msgid);
1004     } else {
1005         res = request.msgid;
1006     }
1007 #endif
1008     DM_free(request.params);
1009 
1010     return res;
1011 }
1012 
dm_mgr_upstream_combine_login(_IN_ int devid)1013 int dm_mgr_upstream_combine_login(_IN_ int devid)
1014 {
1015     int res = 0;
1016     dm_mgr_dev_node_t *node = NULL;
1017     dm_msg_request_t request;
1018 
1019     if (devid < 0) {
1020         return DM_INVALID_PARAMETER;
1021     }
1022 
1023     res = _dm_mgr_search_dev_by_devid(devid, &node);
1024     if (res != SUCCESS_RETURN) {
1025         return FAIL_RETURN;
1026     }
1027 
1028     memset(&request, 0, sizeof(dm_msg_request_t));
1029     request.service_prefix = DM_URI_EXT_SESSION_PREFIX;
1030     request.service_name = DM_URI_COMBINE_LOGIN;
1031     HAL_GetProductKey(request.product_key);
1032     HAL_GetDeviceName(request.device_name);
1033 
1034     /* Get Params And Method */
1035     res = dm_msg_combine_login(node->product_key, node->device_name,
1036                                node->device_secret, &request);
1037     if (res != SUCCESS_RETURN) {
1038         return FAIL_RETURN;
1039     }
1040 
1041     /* Get Msg ID */
1042     request.msgid = iotx_report_id();
1043 
1044     /* Get Dev ID */
1045     request.devid = devid;
1046 
1047     /* Callback */
1048     request.callback = dm_client_combine_login_reply;
1049 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1050     dm_msg_cache_insert(request.msgid, request.devid,
1051                         IOTX_DM_EVENT_COMBINE_LOGIN_REPLY, NULL);
1052 #endif
1053     /* Send Message To Cloud */
1054     res = dm_msg_request(DM_MSG_DEST_CLOUD, &request);
1055 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1056     if (res != SUCCESS_RETURN) {
1057         dm_msg_cache_remove(request.msgid);
1058     } else {
1059         res = request.msgid;
1060     }
1061 #endif
1062     DM_free(request.params);
1063 
1064     return res;
1065 }
1066 
dm_mgr_upstream_combine_logout(_IN_ int devid)1067 int dm_mgr_upstream_combine_logout(_IN_ int devid)
1068 {
1069     int res = 0;
1070     dm_mgr_dev_node_t *node = NULL;
1071     dm_msg_request_t request;
1072 
1073     if (devid < 0) {
1074         return DM_INVALID_PARAMETER;
1075     }
1076 
1077     res = _dm_mgr_search_dev_by_devid(devid, &node);
1078     if (res != SUCCESS_RETURN) {
1079         return FAIL_RETURN;
1080     }
1081 
1082     if (node->dev_status < IOTX_DM_DEV_STATUS_LOGINED) {
1083         return FAIL_RETURN;
1084     }
1085 
1086     memset(&request, 0, sizeof(dm_msg_request_t));
1087     request.service_prefix = DM_URI_EXT_SESSION_PREFIX;
1088     request.service_name = DM_URI_COMBINE_LOGOUT;
1089     HAL_GetProductKey(request.product_key);
1090     HAL_GetDeviceName(request.device_name);
1091 
1092     /* Get Params And Method */
1093     res = dm_msg_combine_logout(node->product_key, node->device_name, &request);
1094     if (res != SUCCESS_RETURN) {
1095         return FAIL_RETURN;
1096     }
1097 
1098     /* Get Msg ID */
1099     request.msgid = iotx_report_id();
1100 
1101     /* Get Dev ID */
1102     request.devid = devid;
1103 
1104     /* Callback */
1105     request.callback = dm_client_combine_logout_reply;
1106 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1107     dm_msg_cache_insert(request.msgid, request.devid,
1108                         IOTX_DM_EVENT_COMBINE_LOGOUT_REPLY, NULL);
1109 #endif
1110 
1111     /* Send Message To Cloud */
1112     res = dm_msg_request(DM_MSG_DEST_CLOUD, &request);
1113 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1114     if (res != SUCCESS_RETURN) {
1115         dm_msg_cache_remove(request.msgid);
1116     } else {
1117         res = request.msgid;
1118     }
1119 #endif
1120     DM_free(request.params);
1121 
1122     return res;
1123 }
1124 
1125 #ifdef DEVICE_MODEL_SUBDEV_OTA
dm_mgr_upstream_thing_firmware_version_update(_IN_ int devid,_IN_ char * payload,_IN_ int payload_len)1126 int dm_mgr_upstream_thing_firmware_version_update(_IN_ int devid,
1127                                                   _IN_ char *payload,
1128                                                   _IN_ int payload_len)
1129 {
1130     int res = 0, res1 = 0;
1131     dm_mgr_dev_node_t *node = NULL;
1132     char *uri = NULL;
1133     dm_msg_request_t request;
1134 
1135     if (devid < 0 || payload == NULL || payload_len <= 0) {
1136         return DM_INVALID_PARAMETER;
1137     }
1138 
1139     res = _dm_mgr_search_dev_by_devid(devid, &node);
1140     if (res != SUCCESS_RETURN) {
1141         return FAIL_RETURN;
1142     }
1143 
1144     memset(&request, 0, sizeof(dm_msg_request_t));
1145     request.service_prefix = DM_URI_OTA_DEVICE_INFORM;
1146     request.service_name = NULL;
1147     memcpy(request.product_key, node->product_key, strlen(node->product_key));
1148     memcpy(request.device_name, node->device_name, strlen(node->device_name));
1149 
1150     /* Request URI */
1151     res = dm_utils_service_name(request.service_prefix, request.service_name,
1152                                 request.product_key, request.device_name, &uri);
1153     if (res != SUCCESS_RETURN) {
1154         return FAIL_RETURN;
1155     }
1156 
1157     dm_log_info("DM Send Raw Data:");
1158     HEXDUMP_INFO(payload, payload_len);
1159 
1160     res = dm_client_publish(uri, (unsigned char *)payload, strlen(payload),
1161                             dm_client_thing_model_up_raw_reply);
1162 
1163     if (res < SUCCESS_RETURN || res1 < SUCCESS_RETURN) {
1164         dm_log_info("res of pub is %d:", res);
1165         DM_free(uri);
1166         return FAIL_RETURN;
1167     }
1168 
1169     DM_free(uri);
1170     return SUCCESS_RETURN;
1171 }
1172 #endif
1173 #endif
1174 
dm_mgr_upstream_thing_model_up_raw(_IN_ int devid,_IN_ char * payload,_IN_ int payload_len)1175 int dm_mgr_upstream_thing_model_up_raw(_IN_ int devid, _IN_ char *payload,
1176                                        _IN_ int payload_len)
1177 {
1178     int res = 0, res1 = 0;
1179     dm_mgr_dev_node_t *node = NULL;
1180     char *uri = NULL;
1181     dm_msg_request_t request;
1182 
1183     if (devid < 0 || payload == NULL || payload_len <= 0) {
1184         return DM_INVALID_PARAMETER;
1185     }
1186 
1187     res = _dm_mgr_search_dev_by_devid(devid, &node);
1188     if (res != SUCCESS_RETURN) {
1189         return FAIL_RETURN;
1190     }
1191 
1192     memset(&request, 0, sizeof(dm_msg_request_t));
1193     request.service_prefix = DM_URI_SYS_PREFIX;
1194     request.service_name = DM_URI_THING_MODEL_UP_RAW;
1195     memcpy(request.product_key, node->product_key, strlen(node->product_key));
1196     memcpy(request.device_name, node->device_name, strlen(node->device_name));
1197 
1198     /* Request URI */
1199     res = dm_utils_service_name(request.service_prefix, request.service_name,
1200                                 request.product_key, request.device_name, &uri);
1201     if (res != SUCCESS_RETURN) {
1202         return FAIL_RETURN;
1203     }
1204 
1205     dm_log_info("DM Send Raw Data:");
1206     HEXDUMP_INFO(payload, payload_len);
1207 
1208     res = dm_client_publish(uri, (unsigned char *)payload, payload_len,
1209                             dm_client_thing_model_up_raw_reply);
1210 #ifdef ALCS_ENABLED
1211     res1 = dm_server_send(uri, (unsigned char *)payload, payload_len, NULL);
1212 #endif
1213 
1214     if (res < SUCCESS_RETURN || res1 < SUCCESS_RETURN) {
1215         DM_free(uri);
1216         return FAIL_RETURN;
1217     }
1218 
1219     DM_free(uri);
1220     return SUCCESS_RETURN;
1221 }
1222 
1223 #if !defined(DEVICE_MODEL_RAWDATA_SOLO)
_dm_mgr_upstream_request_assemble(_IN_ int msgid,_IN_ int devid,_IN_ const char * service_prefix,_IN_ const char * service_name,_IN_ char * params,_IN_ int params_len,_IN_ char * method,_OU_ dm_msg_request_t * request)1224 static int _dm_mgr_upstream_request_assemble(
1225     _IN_ int msgid, _IN_ int devid, _IN_ const char *service_prefix,
1226     _IN_ const char *service_name, _IN_ char *params, _IN_ int params_len,
1227     _IN_ char *method, _OU_ dm_msg_request_t *request)
1228 {
1229     int res = 0;
1230     dm_mgr_dev_node_t *node = NULL;
1231 
1232     res = _dm_mgr_search_dev_by_devid(devid, &node);
1233     if (res != SUCCESS_RETURN) {
1234         return FAIL_RETURN;
1235     }
1236 
1237     request->msgid = msgid;
1238     request->devid = devid;
1239     request->service_prefix = service_prefix;
1240     request->service_name = service_name;
1241     memcpy(request->product_key, node->product_key, strlen(node->product_key));
1242     memcpy(request->device_name, node->device_name, strlen(node->device_name));
1243     request->params = params;
1244     request->params_len = params_len;
1245     request->method = method;
1246 
1247     return SUCCESS_RETURN;
1248 }
1249 #ifdef DEVICE_MODEL_SHADOW
dm_mgr_upstream_thing_property_desired_get(_IN_ int devid,_IN_ char * payload,_IN_ int payload_len)1250 int dm_mgr_upstream_thing_property_desired_get(_IN_ int devid,
1251                                                _IN_ char *payload,
1252                                                _IN_ int payload_len)
1253 {
1254     int res = 0;
1255     dm_msg_request_t request;
1256     int prop_desired_get_reply = 0;
1257 
1258     if (devid < 0 || payload == NULL || payload_len <= 0) {
1259         return DM_INVALID_PARAMETER;
1260     }
1261 
1262     memset(&request, 0, sizeof(dm_msg_request_t));
1263     res = _dm_mgr_upstream_request_assemble(
1264         iotx_report_id(), devid, DM_URI_SYS_PREFIX,
1265         DM_URI_THING_PROPERTY_DESIRED_GET, payload, payload_len,
1266         "thing.property.desired.get", &request);
1267     if (res != SUCCESS_RETURN) {
1268         return FAIL_RETURN;
1269     }
1270 
1271     /* Callback */
1272     request.callback = dm_client_thing_property_desired_get_reply;
1273 
1274 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1275     res = dm_opt_get(DM_OPT_DOWNSTREAM_EVENT_PROPERTY_DESIRED_GET_REPLY,
1276                      &prop_desired_get_reply);
1277     if (res == SUCCESS_RETURN && prop_desired_get_reply) {
1278         dm_msg_cache_insert(request.msgid, request.devid,
1279                             IOTX_DM_EVENT_PROPERTY_DESIRED_GET_REPLY, NULL);
1280     }
1281 #endif
1282 
1283     /* Send Message To Cloud */
1284     res = dm_msg_request(DM_MSG_DEST_CLOUD, &request);
1285     /*TODO */
1286 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1287     if (res != SUCCESS_RETURN) {
1288         dm_msg_cache_remove(request.msgid);
1289     } else {
1290         res = request.msgid;
1291     }
1292 #endif
1293     return res;
1294 }
1295 
dm_mgr_upstream_thing_property_desired_delete(_IN_ int devid,_IN_ char * payload,_IN_ int payload_len)1296 int dm_mgr_upstream_thing_property_desired_delete(_IN_ int devid,
1297                                                   _IN_ char *payload,
1298                                                   _IN_ int payload_len)
1299 {
1300     int res = 0;
1301     dm_msg_request_t request;
1302     int prop_desired_delete_reply = 0;
1303     if (devid < 0 || payload == NULL || payload_len <= 0) {
1304         return DM_INVALID_PARAMETER;
1305     }
1306 
1307     memset(&request, 0, sizeof(dm_msg_request_t));
1308     res = _dm_mgr_upstream_request_assemble(
1309         iotx_report_id(), devid, DM_URI_SYS_PREFIX,
1310         DM_URI_THING_PROPERTY_DESIRED_DELETE, payload, payload_len,
1311         "thing.property.desired.delete", &request);
1312     if (res != SUCCESS_RETURN) {
1313         return FAIL_RETURN;
1314     }
1315 
1316     /* Callback */
1317     request.callback = dm_client_thing_property_desired_delete_reply;
1318 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1319     res = dm_opt_get(DM_OPT_DOWNSTREAM_EVENT_PROPERTY_DESIRED_DELETE_REPLY,
1320                      &prop_desired_delete_reply);
1321     if (res == SUCCESS_RETURN && prop_desired_delete_reply) {
1322         dm_msg_cache_insert(request.msgid, request.devid,
1323                             IOTX_DM_EVENT_PROPERTY_DESIRED_DELETE_REPLY, NULL);
1324     }
1325 #endif
1326     /* Send Message To Cloud */
1327     res = dm_msg_request(DM_MSG_DEST_CLOUD, &request);
1328 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1329     if (res != SUCCESS_RETURN) {
1330         dm_msg_cache_remove(request.msgid);
1331     } else {
1332         res = request.msgid;
1333     }
1334 #endif
1335     return res;
1336 }
1337 #endif
1338 
dm_mgr_upstream_thing_property_post(_IN_ int devid,_IN_ char * payload,_IN_ int payload_len)1339 int dm_mgr_upstream_thing_property_post(_IN_ int devid, _IN_ char *payload,
1340                                         _IN_ int payload_len)
1341 {
1342     int res = 0;
1343     dm_msg_request_t request;
1344     int prop_post_reply = 0;
1345 
1346     if (devid < 0 || payload == NULL || payload_len <= 0) {
1347         return DM_INVALID_PARAMETER;
1348     }
1349 
1350     memset(&request, 0, sizeof(dm_msg_request_t));
1351     res = _dm_mgr_upstream_request_assemble(
1352         iotx_report_id(), devid, DM_URI_SYS_PREFIX,
1353         DM_URI_THING_EVENT_PROPERTY_POST, payload, payload_len,
1354         "thing.event.property.post", &request);
1355     if (res != SUCCESS_RETURN) {
1356         return FAIL_RETURN;
1357     }
1358 
1359     /* Callback */
1360     request.callback = dm_client_thing_event_post_reply;
1361 
1362 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1363     res = dm_opt_get(DM_OPT_DOWNSTREAM_EVENT_POST_REPLY, &prop_post_reply);
1364     if (res == SUCCESS_RETURN && prop_post_reply) {
1365         dm_msg_cache_insert(request.msgid, request.devid,
1366                             IOTX_DM_EVENT_EVENT_PROPERTY_POST_REPLY, NULL);
1367     }
1368 #endif
1369     /* Send Message To Cloud */
1370     res = dm_msg_request(DM_MSG_DEST_ALL, &request);
1371 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1372     if (res != SUCCESS_RETURN) {
1373         dm_msg_cache_remove(request.msgid);
1374     } else {
1375         res = request.msgid;
1376     }
1377 #endif
1378     return res;
1379 }
1380 
1381 #ifdef LOG_REPORT_TO_CLOUD
1382 static unsigned int log_size = 0;
dm_mgr_upstream_thing_log_post(_IN_ int devid,_IN_ char * payload,_IN_ int payload_len,int force_upload)1383 int dm_mgr_upstream_thing_log_post(_IN_ int devid, _IN_ char *payload,
1384                                    _IN_ int payload_len, int force_upload)
1385 {
1386     int res = 0;
1387     dm_msg_request_t request;
1388     extern REPORT_STATE g_report_status;
1389     extern char *g_log_poll;
1390 
1391     if (0 == force_upload) {
1392         if (devid < 0 || payload == NULL || payload_len <= 0) {
1393             return DM_INVALID_PARAMETER;
1394         }
1395 
1396         if (log_size + payload_len < OVERFLOW_LEN) {
1397             log_size = push_log(payload, payload_len);
1398         } else {
1399             /* it should NOT happen; it means that it is too late to upload log
1400              * files */
1401             reset_log_poll();
1402             dm_log_err("it it too late to upload log, reset pool");
1403             return FAIL_RETURN;
1404         }
1405 
1406         dm_log_info("push log, len is %d, log_size is %d\n", payload_len,
1407                     log_size);
1408         if (!(log_size > REPORT_LEN && DONE == g_report_status)) {
1409             return SUCCESS_RETURN;
1410         }
1411     }
1412 
1413     log_size = add_tail();
1414     memset(&request, 0, sizeof(dm_msg_request_t));
1415     res = _dm_mgr_upstream_request_assemble(
1416         iotx_report_id(), devid, DM_URI_SYS_PREFIX, DM_URI_THING_LOG_POST,
1417         g_log_poll, log_size + 1, "thing.log.post", &request);
1418 
1419     if (res != SUCCESS_RETURN) {
1420         reset_log_poll();
1421         return FAIL_RETURN;
1422     }
1423     /* Send Message To Cloud */
1424     res = dm_msg_request(DM_MSG_DEST_CLOUD, &request);
1425     reset_log_poll();
1426     return res;
1427 }
1428 #endif
1429 
dm_mgr_upstream_thing_event_post(_IN_ int devid,_IN_ char * identifier,_IN_ int identifier_len,_IN_ char * method,_IN_ char * payload,_IN_ int payload_len)1430 int dm_mgr_upstream_thing_event_post(_IN_ int devid, _IN_ char *identifier,
1431                                      _IN_ int identifier_len, _IN_ char *method,
1432                                      _IN_ char *payload, _IN_ int payload_len)
1433 {
1434     int res = 0, service_name_len = 0;
1435     char *service_name = NULL;
1436     dm_msg_request_t request;
1437     int event_post_reply = 0;
1438     if (devid < 0 || identifier == NULL || identifier_len <= 0 ||
1439         method == NULL || payload == NULL || payload_len <= 0) {
1440         return DM_INVALID_PARAMETER;
1441     }
1442 
1443     service_name_len = strlen(DM_URI_THING_EVENT_POST) + identifier_len + 1;
1444     service_name = DM_malloc(service_name_len);
1445     if (service_name == NULL) {
1446         return DM_MEMORY_NOT_ENOUGH;
1447     }
1448     memset(service_name, 0, service_name_len);
1449     HAL_Snprintf(service_name, service_name_len, DM_URI_THING_EVENT_POST,
1450                  identifier_len, identifier);
1451 
1452     memset(&request, 0, sizeof(dm_msg_request_t));
1453     res = _dm_mgr_upstream_request_assemble(
1454         iotx_report_id(), devid, DM_URI_SYS_PREFIX, service_name, payload,
1455         payload_len, method, &request);
1456     if (res != SUCCESS_RETURN) {
1457         return FAIL_RETURN;
1458     }
1459 
1460     /* Callback */
1461     request.callback = dm_client_thing_event_post_reply;
1462 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1463     res = dm_opt_get(DM_OPT_DOWNSTREAM_EVENT_POST_REPLY, &event_post_reply);
1464     if (res == SUCCESS_RETURN && event_post_reply) {
1465         dm_msg_cache_insert(request.msgid, request.devid,
1466                             IOTX_DM_EVENT_EVENT_PROPERTY_POST_REPLY, NULL);
1467     }
1468 #endif
1469     /* Send Message To Cloud */
1470     res = dm_msg_request(DM_MSG_DEST_ALL, &request);
1471 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1472     if (res != SUCCESS_RETURN) {
1473         dm_msg_cache_remove(request.msgid);
1474     } else {
1475         res = request.msgid;
1476     }
1477 #endif
1478     DM_free(service_name);
1479 
1480     return res;
1481 }
1482 
dm_mgr_upstream_thing_deviceinfo_update(_IN_ int devid,_IN_ char * payload,_IN_ int payload_len)1483 int dm_mgr_upstream_thing_deviceinfo_update(_IN_ int devid, _IN_ char *payload,
1484                                             _IN_ int payload_len)
1485 {
1486     int res = 0;
1487     dm_msg_request_t request;
1488 
1489     if (devid < 0 || payload == NULL || payload_len <= 0) {
1490         return DM_INVALID_PARAMETER;
1491     }
1492 
1493     memset(&request, 0, sizeof(dm_msg_request_t));
1494     res = _dm_mgr_upstream_request_assemble(
1495         iotx_report_id(), devid, DM_URI_SYS_PREFIX,
1496         DM_URI_THING_DEVICEINFO_UPDATE, payload, payload_len,
1497         "thing.deviceinfo.update", &request);
1498     if (res != SUCCESS_RETURN) {
1499         return FAIL_RETURN;
1500     }
1501 
1502     /* Callback */
1503     request.callback = dm_client_thing_deviceinfo_update_reply;
1504 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1505     dm_msg_cache_insert(request.msgid, request.devid,
1506                         IOTX_DM_EVENT_DEVICEINFO_UPDATE_REPLY, NULL);
1507 #endif
1508     /* Send Message To Cloud */
1509     res = dm_msg_request(DM_MSG_DEST_CLOUD, &request);
1510 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1511     if (res != SUCCESS_RETURN) {
1512         dm_msg_cache_remove(request.msgid);
1513     } else {
1514         res = request.msgid;
1515     }
1516 #endif
1517     return res;
1518 }
1519 
dm_mgr_upstream_thing_deviceinfo_delete(_IN_ int devid,_IN_ char * payload,_IN_ int payload_len)1520 int dm_mgr_upstream_thing_deviceinfo_delete(_IN_ int devid, _IN_ char *payload,
1521                                             _IN_ int payload_len)
1522 {
1523     int res = 0;
1524     dm_msg_request_t request;
1525 
1526     if (devid < 0 || payload == NULL || payload_len <= 0) {
1527         return DM_INVALID_PARAMETER;
1528     }
1529 
1530     memset(&request, 0, sizeof(dm_msg_request_t));
1531     res = _dm_mgr_upstream_request_assemble(
1532         iotx_report_id(), devid, DM_URI_SYS_PREFIX,
1533         DM_URI_THING_DEVICEINFO_DELETE, payload, payload_len,
1534         "thing.deviceinfo.delete", &request);
1535     if (res != SUCCESS_RETURN) {
1536         return FAIL_RETURN;
1537     }
1538 
1539     /* Callback */
1540     request.callback = dm_client_thing_deviceinfo_delete_reply;
1541 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1542     dm_msg_cache_insert(request.msgid, request.devid,
1543                         IOTX_DM_EVENT_DEVICEINFO_DELETE_REPLY, NULL);
1544 #endif
1545     /* Send Message To Cloud */
1546     res = dm_msg_request(DM_MSG_DEST_CLOUD, &request);
1547 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1548     if (res != SUCCESS_RETURN) {
1549         dm_msg_cache_remove(request.msgid);
1550     } else {
1551         res = request.msgid;
1552     }
1553 #endif
1554     return res;
1555 }
1556 
dm_mgr_upstream_thing_dsltemplate_get(_IN_ int devid)1557 int dm_mgr_upstream_thing_dsltemplate_get(_IN_ int devid)
1558 {
1559     int res = 0;
1560     char *params = "{}";
1561     int params_len = strlen(params);
1562     dm_msg_request_t request;
1563 
1564     if (devid < 0) {
1565         return DM_INVALID_PARAMETER;
1566     }
1567 
1568     memset(&request, 0, sizeof(dm_msg_request_t));
1569     res = _dm_mgr_upstream_request_assemble(
1570         iotx_report_id(), devid, DM_URI_SYS_PREFIX,
1571         DM_URI_THING_DSLTEMPLATE_GET, params, params_len,
1572         "thing.dsltemplate.get", &request);
1573     if (res != SUCCESS_RETURN) {
1574         return FAIL_RETURN;
1575     }
1576 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1577     dm_msg_cache_insert(request.msgid, request.devid,
1578                         IOTX_DM_EVENT_DSLTEMPLATE_GET_REPLY, NULL);
1579 #endif
1580 
1581     /* Send Message To Cloud */
1582     res = dm_msg_request(DM_MSG_DEST_CLOUD, &request);
1583 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1584     if (res != SUCCESS_RETURN) {
1585         dm_msg_cache_remove(request.msgid);
1586     } else {
1587         res = request.msgid;
1588     }
1589 #endif
1590     return res;
1591 }
1592 
dm_mgr_upstream_thing_dynamictsl_get(_IN_ int devid)1593 int dm_mgr_upstream_thing_dynamictsl_get(_IN_ int devid)
1594 {
1595     int res = 0;
1596     char *params = "{\"nodes\":[\"type\",\"identifier\"],\"addDefault\":false}";
1597     int params_len = strlen(params);
1598     dm_msg_request_t request;
1599 
1600     if (devid < 0) {
1601         return DM_INVALID_PARAMETER;
1602     }
1603 
1604     memset(&request, 0, sizeof(dm_msg_request_t));
1605     res = _dm_mgr_upstream_request_assemble(
1606         iotx_report_id(), devid, DM_URI_SYS_PREFIX, DM_URI_THING_DYNAMICTSL_GET,
1607         params, params_len, "thing.dynamicTsl.get", &request);
1608     if (res != SUCCESS_RETURN) {
1609         return FAIL_RETURN;
1610     }
1611 
1612     /* Callback */
1613     request.callback = dm_client_thing_dynamictsl_get_reply;
1614 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1615     dm_msg_cache_insert(request.msgid, request.devid,
1616                         IOTX_DM_EVENT_DSLTEMPLATE_GET_REPLY, NULL);
1617 #endif
1618     /* Send Message To Cloud */
1619     res = dm_msg_request(DM_MSG_DEST_CLOUD, &request);
1620 #if !defined(DM_MESSAGE_CACHE_DISABLED)
1621     if (res != SUCCESS_RETURN) {
1622         dm_msg_cache_remove(request.msgid);
1623     } else {
1624         res = request.msgid;
1625     }
1626 #endif
1627     return res;
1628 }
1629 
dm_mgr_upstream_ntp_request(void)1630 int dm_mgr_upstream_ntp_request(void)
1631 {
1632     int res = 0;
1633     const char *ntp_request_fmt = "{\"deviceSendTime\":\"1234\"}";
1634     char *uri = NULL;
1635     dm_msg_request_t request;
1636 
1637     memset(&request, 0, sizeof(dm_msg_request_t));
1638     request.service_prefix = DM_URI_EXT_NTP_PREFIX;
1639     request.service_name = DM_URI_NTP_REQUEST;
1640     HAL_GetProductKey(request.product_key);
1641     HAL_GetDeviceName(request.device_name);
1642 
1643     /* Request URI */
1644     res = dm_utils_service_name(request.service_prefix, request.service_name,
1645                                 request.product_key, request.device_name, &uri);
1646     if (res != SUCCESS_RETURN) {
1647         return FAIL_RETURN;
1648     }
1649 
1650     res = dm_client_publish(uri, (unsigned char *)ntp_request_fmt,
1651                             strlen(ntp_request_fmt), dm_client_ntp_response);
1652     if (res != SUCCESS_RETURN) {
1653         DM_free(uri); /* DM_free(cloud_payload); */
1654         return FAIL_RETURN;
1655     }
1656 
1657     DM_free(uri); /* DM_free(cloud_payload); */
1658     return SUCCESS_RETURN;
1659 }
1660 
_dm_mgr_upstream_response_assemble(_IN_ int devid,_IN_ char * msgid,_IN_ int msgid_len,_IN_ const char * prefix,_IN_ const char * service_name,_IN_ int code,_OU_ dm_msg_request_payload_t * request,_OU_ dm_msg_response_t * response)1661 static int _dm_mgr_upstream_response_assemble(
1662     _IN_ int devid, _IN_ char *msgid, _IN_ int msgid_len,
1663     _IN_ const char *prefix, _IN_ const char *service_name, _IN_ int code,
1664     _OU_ dm_msg_request_payload_t *request, _OU_ dm_msg_response_t *response)
1665 {
1666     int res = 0;
1667     dm_mgr_dev_node_t *node = NULL;
1668 
1669     res = _dm_mgr_search_dev_by_devid(devid, &node);
1670     if (res != SUCCESS_RETURN) {
1671         return FAIL_RETURN;
1672     }
1673 
1674     request->id.value = msgid;
1675     request->id.value_length = msgid_len;
1676 
1677     response->service_prefix = DM_URI_SYS_PREFIX;
1678     response->service_name = service_name;
1679     memcpy(response->product_key, node->product_key, strlen(node->product_key));
1680     memcpy(response->device_name, node->device_name, strlen(node->device_name));
1681     response->code = (iotx_dm_error_code_t)code;
1682 
1683     return SUCCESS_RETURN;
1684 }
1685 
dm_mgr_upstream_thing_service_response(_IN_ int devid,_IN_ char * msgid,_IN_ int msgid_len,_IN_ iotx_dm_error_code_t code,_IN_ char * identifier,_IN_ int identifier_len,_IN_ char * payload,_IN_ int payload_len,void * ctx)1686 int dm_mgr_upstream_thing_service_response(_IN_ int devid, _IN_ char *msgid,
1687                                            _IN_ int msgid_len,
1688                                            _IN_ iotx_dm_error_code_t code,
1689                                            _IN_ char *identifier,
1690                                            _IN_ int identifier_len,
1691                                            _IN_ char *payload,
1692                                            _IN_ int payload_len, void *ctx)
1693 {
1694     int res = 0, service_name_len = 0;
1695     char *service_name = NULL;
1696     dm_msg_request_payload_t request;
1697     dm_msg_response_t response;
1698 
1699     memset(&request, 0, sizeof(dm_msg_request_payload_t));
1700     memset(&response, 0, sizeof(dm_msg_response_t));
1701 
1702     if (devid < 0 || msgid == NULL || msgid_len <= 0 || identifier == NULL ||
1703         identifier_len <= 0 || payload == NULL || payload_len <= 0) {
1704         return DM_INVALID_PARAMETER;
1705     }
1706 
1707     /* Service Name */
1708     service_name_len =
1709         strlen(DM_URI_THING_SERVICE_RESPONSE) + identifier_len + 1;
1710     service_name = DM_malloc(service_name_len);
1711     if (service_name == NULL) {
1712         return DM_MEMORY_NOT_ENOUGH;
1713     }
1714     memset(service_name, 0, service_name_len);
1715     HAL_Snprintf(service_name, service_name_len, DM_URI_THING_SERVICE_RESPONSE,
1716                  identifier_len, identifier);
1717 
1718     res = _dm_mgr_upstream_response_assemble(devid, msgid, msgid_len,
1719                                              DM_URI_SYS_PREFIX, service_name,
1720                                              code, &request, &response);
1721     if (res != SUCCESS_RETURN) {
1722         return FAIL_RETURN;
1723     }
1724 
1725     dm_log_debug("Current Service Name: %s", service_name);
1726     if (ctx != NULL) {
1727         dm_msg_response(DM_MSG_DEST_LOCAL, &request, &response, payload,
1728                         payload_len, ctx);
1729     } else {
1730         dm_msg_response(DM_MSG_DEST_CLOUD, &request, &response, payload,
1731                         payload_len, ctx);
1732     }
1733 
1734     DM_free(service_name);
1735     return SUCCESS_RETURN;
1736 }
1737 
dm_mgr_upstream_thing_property_get_response(_IN_ int devid,_IN_ char * msgid,_IN_ int msgid_len,_IN_ iotx_dm_error_code_t code,_IN_ char * payload,_IN_ int payload_len,_IN_ void * ctx)1738 int dm_mgr_upstream_thing_property_get_response(
1739     _IN_ int devid, _IN_ char *msgid, _IN_ int msgid_len,
1740     _IN_ iotx_dm_error_code_t code, _IN_ char *payload, _IN_ int payload_len,
1741     _IN_ void *ctx)
1742 {
1743     int res = 0;
1744     dm_msg_request_payload_t request;
1745     dm_msg_response_t response;
1746     const char *reply_service_name = NULL;
1747     dm_msg_dest_type_t reply_msg_type;
1748 #ifdef ALCS_ENABLED
1749     dm_server_alcs_context_t *alcs_context = NULL;
1750 #endif
1751 
1752     if (devid < 0 || msgid == NULL || msgid_len <= 0 || payload == NULL ||
1753         payload_len <= 0) {
1754         return DM_INVALID_PARAMETER;
1755     }
1756 
1757     memset(&request, 0, sizeof(dm_msg_request_payload_t));
1758     memset(&response, 0, sizeof(dm_msg_response_t));
1759 
1760     /* Send Property Get Response Message To Local */
1761     reply_service_name = DM_URI_THING_SERVICE_PROPERTY_GET;
1762     reply_msg_type = DM_MSG_DEST_LOCAL;
1763 
1764     /* Send Property Get Response Message To Cloud */
1765     if (NULL == ctx) {
1766         reply_service_name = DM_URI_THING_SERVICE_PROPERTY_GET_REPLY;
1767         reply_msg_type = DM_MSG_DEST_CLOUD;
1768     }
1769 
1770     res = _dm_mgr_upstream_response_assemble(
1771         devid, msgid, msgid_len, DM_URI_SYS_PREFIX, reply_service_name, code,
1772         &request, &response);
1773     if (res != SUCCESS_RETURN) {
1774         return FAIL_RETURN;
1775     }
1776     dm_log_debug("Current Service Name: %s", reply_service_name);
1777     dm_msg_response(reply_msg_type, &request, &response, payload, payload_len,
1778                     ctx);
1779 
1780 #ifdef ALCS_ENABLED
1781     alcs_context = (dm_server_alcs_context_t *)ctx;
1782 
1783     if (alcs_context) {
1784         DM_free(alcs_context->ip);
1785         DM_free(alcs_context->token);
1786         DM_free(alcs_context);
1787     }
1788 #endif
1789 
1790     return SUCCESS_RETURN;
1791 }
1792 
dm_mgr_upstream_rrpc_response(_IN_ int devid,_IN_ char * msgid,_IN_ int msgid_len,_IN_ iotx_dm_error_code_t code,_IN_ char * rrpcid,_IN_ int rrpcid_len,_IN_ char * payload,_IN_ int payload_len)1793 int dm_mgr_upstream_rrpc_response(_IN_ int devid, _IN_ char *msgid,
1794                                   _IN_ int msgid_len,
1795                                   _IN_ iotx_dm_error_code_t code,
1796                                   _IN_ char *rrpcid, _IN_ int rrpcid_len,
1797                                   _IN_ char *payload, _IN_ int payload_len)
1798 {
1799     int res = 0, service_name_len = 0;
1800     const char *rrpc_response_service_name = "rrpc/response/%.*s";
1801     char *service_name = NULL;
1802     dm_msg_request_payload_t request;
1803     dm_msg_response_t response;
1804 
1805     memset(&request, 0, sizeof(dm_msg_request_payload_t));
1806     memset(&response, 0, sizeof(dm_msg_response_t));
1807 
1808     if (devid < 0 || msgid == NULL || msgid_len <= 0 || rrpcid == NULL ||
1809         rrpcid_len <= 0 || payload == NULL || payload_len <= 0) {
1810         return DM_INVALID_PARAMETER;
1811     }
1812 
1813     /* Service Name */
1814     service_name_len = strlen(rrpc_response_service_name) + rrpcid_len + 1;
1815     service_name = DM_malloc(service_name_len);
1816     if (service_name == NULL) {
1817         return DM_MEMORY_NOT_ENOUGH;
1818     }
1819     memset(service_name, 0, service_name_len);
1820     HAL_Snprintf(service_name, service_name_len, rrpc_response_service_name,
1821                  rrpcid_len, rrpcid);
1822 
1823     res = _dm_mgr_upstream_response_assemble(devid, msgid, msgid_len,
1824                                              DM_URI_SYS_PREFIX, service_name,
1825                                              code, &request, &response);
1826     if (res != SUCCESS_RETURN) {
1827         return FAIL_RETURN;
1828     }
1829 
1830     dm_log_debug("Current Service Name: %s", service_name);
1831     dm_msg_response(DM_MSG_DEST_ALL, &request, &response, payload, payload_len,
1832                     NULL);
1833 
1834     DM_free(service_name);
1835 
1836     return SUCCESS_RETURN;
1837 }
1838 #endif
1839 
1840 #ifdef DEPRECATED_LINKKIT
dm_mgr_deprecated_set_tsl_source(_IN_ int devid,_IN_ iotx_dm_tsl_source_t tsl_source)1841 int dm_mgr_deprecated_set_tsl_source(_IN_ int devid,
1842                                      _IN_ iotx_dm_tsl_source_t tsl_source)
1843 {
1844     int res = 0;
1845     dm_mgr_dev_node_t *node = NULL;
1846 
1847     if (devid < 0) {
1848         return DM_INVALID_PARAMETER;
1849     }
1850 
1851     res = _dm_mgr_search_dev_by_devid(devid, &node);
1852     if (res != SUCCESS_RETURN) {
1853         return FAIL_RETURN;
1854     }
1855 
1856     node->tsl_source = tsl_source;
1857 
1858     return SUCCESS_RETURN;
1859 }
1860 
dm_mgr_deprecated_get_tsl_source(_IN_ int devid,_IN_ iotx_dm_tsl_source_t * tsl_source)1861 int dm_mgr_deprecated_get_tsl_source(_IN_ int devid,
1862                                      _IN_ iotx_dm_tsl_source_t *tsl_source)
1863 {
1864     int res = 0;
1865     dm_mgr_dev_node_t *node = NULL;
1866 
1867     if (devid < 0 || tsl_source == NULL) {
1868         return DM_INVALID_PARAMETER;
1869     }
1870 
1871     res = _dm_mgr_search_dev_by_devid(devid, &node);
1872     if (res != SUCCESS_RETURN) {
1873         return FAIL_RETURN;
1874     }
1875 
1876     *tsl_source = node->tsl_source;
1877 
1878     return SUCCESS_RETURN;
1879 }
1880 
dm_mgr_deprecated_search_devid_by_node(_IN_ dm_mgr_dev_node_t * node,_OU_ int * devid)1881 static int dm_mgr_deprecated_search_devid_by_node(_IN_ dm_mgr_dev_node_t *node,
1882                                                   _OU_ int *devid)
1883 {
1884     dm_mgr_ctx *ctx = _dm_mgr_get_ctx();
1885     dm_mgr_dev_node_t *search_node = NULL;
1886 
1887     list_for_each_entry(search_node, &ctx->dev_list, linked_list,
1888                         dm_mgr_dev_node_t)
1889     {
1890         if (search_node == node) {
1891             /* dm_log_debug("Device Found, node: %p", node); */
1892             if (devid) {
1893                 *devid = search_node->devid;
1894             }
1895             return SUCCESS_RETURN;
1896         }
1897     }
1898 
1899     dm_log_debug("Device Not Found, node: %p", node);
1900     return FAIL_RETURN;
1901 }
1902 
dm_mgr_deprecated_search_devid_by_device_node(_IN_ void * node,_OU_ int * devid)1903 int dm_mgr_deprecated_search_devid_by_device_node(_IN_ void *node,
1904                                                   _OU_ int *devid)
1905 {
1906     int res = 0;
1907 
1908     if (node == NULL || devid == NULL) {
1909         return DM_INVALID_PARAMETER;
1910     }
1911 
1912     res = dm_mgr_deprecated_search_devid_by_node((dm_mgr_dev_node_t *)node,
1913                                                  devid);
1914     if (res != SUCCESS_RETURN) {
1915         return FAIL_RETURN;
1916     }
1917 
1918     return SUCCESS_RETURN;
1919 }
1920 
dm_mgr_deprecated_set_tsl(int devid,iotx_dm_tsl_type_t tsl_type,const char * tsl,int tsl_len)1921 int dm_mgr_deprecated_set_tsl(int devid, iotx_dm_tsl_type_t tsl_type,
1922                               const char *tsl, int tsl_len)
1923 {
1924     int res = 0;
1925     dm_mgr_dev_node_t *node = NULL;
1926 
1927     if (tsl == NULL || tsl_len <= 0) {
1928         return DM_INVALID_PARAMETER;
1929     }
1930 
1931     res = _dm_mgr_search_dev_by_devid(devid, &node);
1932     if (res != SUCCESS_RETURN) {
1933         return FAIL_RETURN;
1934     }
1935 
1936     res = dm_shw_create(tsl_type, tsl, tsl_len, &node->dev_shadow);
1937     if (res != SUCCESS_RETURN) {
1938         return FAIL_RETURN;
1939     }
1940 
1941     return SUCCESS_RETURN;
1942 }
1943 
dm_mgr_deprecated_get_property_data(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_OU_ void ** data)1944 int dm_mgr_deprecated_get_property_data(_IN_ int devid, _IN_ char *key,
1945                                         _IN_ int key_len, _OU_ void **data)
1946 {
1947     int res = 0;
1948     dm_mgr_dev_node_t *node = NULL;
1949 
1950     if (key == NULL || key_len <= 0) {
1951         return DM_INVALID_PARAMETER;
1952     }
1953 
1954     res = _dm_mgr_search_dev_by_devid(devid, &node);
1955     if (res != SUCCESS_RETURN) {
1956         return FAIL_RETURN;
1957     }
1958 
1959     res = dm_shw_get_property_data(node->dev_shadow, key, key_len, data);
1960     if (res != SUCCESS_RETURN) {
1961         return FAIL_RETURN;
1962     }
1963 
1964     return SUCCESS_RETURN;
1965 }
1966 
dm_mgr_deprecated_get_service_input_data(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_OU_ void ** data)1967 int dm_mgr_deprecated_get_service_input_data(_IN_ int devid, _IN_ char *key,
1968                                              _IN_ int key_len, _OU_ void **data)
1969 {
1970     int res = 0;
1971     dm_mgr_dev_node_t *node = NULL;
1972 
1973     if (key == NULL || key_len <= 0) {
1974         return DM_INVALID_PARAMETER;
1975     }
1976 
1977     res = _dm_mgr_search_dev_by_devid(devid, &node);
1978     if (res != SUCCESS_RETURN) {
1979         return FAIL_RETURN;
1980     }
1981 
1982     res = dm_shw_get_service_input_output_data(
1983         DM_SHW_DATA_TARGET_SERVICE_INPUT_DATA, node->dev_shadow, key, key_len,
1984         data);
1985     if (res != SUCCESS_RETURN) {
1986         return FAIL_RETURN;
1987     }
1988 
1989     return SUCCESS_RETURN;
1990 }
1991 
dm_mgr_deprecated_get_service_output_data(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_OU_ void ** data)1992 int dm_mgr_deprecated_get_service_output_data(_IN_ int devid, _IN_ char *key,
1993                                               _IN_ int key_len,
1994                                               _OU_ void **data)
1995 {
1996     int res = 0;
1997     dm_mgr_dev_node_t *node = NULL;
1998 
1999     if (key == NULL || key_len <= 0) {
2000         return DM_INVALID_PARAMETER;
2001     }
2002 
2003     res = _dm_mgr_search_dev_by_devid(devid, &node);
2004     if (res != SUCCESS_RETURN) {
2005         return FAIL_RETURN;
2006     }
2007 
2008     res = dm_shw_get_service_input_output_data(
2009         DM_SHW_DATA_TARGET_SERVICE_OUTPUT_DATA, node->dev_shadow, key, key_len,
2010         data);
2011     if (res != SUCCESS_RETURN) {
2012         return FAIL_RETURN;
2013     }
2014 
2015     return SUCCESS_RETURN;
2016 }
2017 
dm_mgr_deprecated_get_event_output_data(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_OU_ void ** data)2018 int dm_mgr_deprecated_get_event_output_data(_IN_ int devid, _IN_ char *key,
2019                                             _IN_ int key_len, _OU_ void **data)
2020 {
2021     int res = 0;
2022     dm_mgr_dev_node_t *node = NULL;
2023 
2024     if (key == NULL || key_len <= 0) {
2025         return DM_INVALID_PARAMETER;
2026     }
2027 
2028     res = _dm_mgr_search_dev_by_devid(devid, &node);
2029     if (res != SUCCESS_RETURN) {
2030         return FAIL_RETURN;
2031     }
2032 
2033     res = dm_shw_get_event_output_data(node->dev_shadow, key, key_len, data);
2034     if (res != SUCCESS_RETURN) {
2035         return FAIL_RETURN;
2036     }
2037 
2038     return SUCCESS_RETURN;
2039 }
2040 
dm_mgr_deprecated_get_data_type(_IN_ void * data,_OU_ dm_shw_data_type_e * type)2041 int dm_mgr_deprecated_get_data_type(_IN_ void *data,
2042                                     _OU_ dm_shw_data_type_e *type)
2043 {
2044     if (data == NULL || type == NULL) {
2045         return DM_INVALID_PARAMETER;
2046     }
2047 
2048     return dm_shw_get_data_type(data, type);
2049 }
2050 
dm_mgr_deprecated_get_property_number(_IN_ int devid,_OU_ int * number)2051 int dm_mgr_deprecated_get_property_number(_IN_ int devid, _OU_ int *number)
2052 {
2053     int res = 0;
2054     dm_mgr_dev_node_t *node = NULL;
2055 
2056     if (devid < 0 || number == NULL) {
2057         return DM_INVALID_PARAMETER;
2058     }
2059 
2060     res = _dm_mgr_search_dev_by_devid(devid, &node);
2061     if (res != SUCCESS_RETURN) {
2062         return FAIL_RETURN;
2063     }
2064 
2065     return dm_shw_get_property_number(node->dev_shadow, number);
2066 }
2067 
dm_mgr_deprecated_get_service_number(_IN_ int devid,_OU_ int * number)2068 int dm_mgr_deprecated_get_service_number(_IN_ int devid, _OU_ int *number)
2069 {
2070     int res = 0;
2071     dm_mgr_dev_node_t *node = NULL;
2072 
2073     if (devid < 0 || number == NULL) {
2074         return DM_INVALID_PARAMETER;
2075     }
2076 
2077     res = _dm_mgr_search_dev_by_devid(devid, &node);
2078     if (res != SUCCESS_RETURN) {
2079         return FAIL_RETURN;
2080     }
2081 
2082     return dm_shw_get_service_number(node->dev_shadow, number);
2083 }
2084 
dm_mgr_deprecated_get_event_number(_IN_ int devid,_OU_ int * number)2085 int dm_mgr_deprecated_get_event_number(_IN_ int devid, _OU_ int *number)
2086 {
2087     int res = 0;
2088     dm_mgr_dev_node_t *node = NULL;
2089 
2090     if (devid < 0 || number == NULL) {
2091         return DM_INVALID_PARAMETER;
2092     }
2093 
2094     res = _dm_mgr_search_dev_by_devid(devid, &node);
2095     if (res != SUCCESS_RETURN) {
2096         return FAIL_RETURN;
2097     }
2098 
2099     return dm_shw_get_event_number(node->dev_shadow, number);
2100 }
2101 
dm_mgr_deprecated_get_property_by_index(_IN_ int devid,_IN_ int index,_OU_ void ** property)2102 int dm_mgr_deprecated_get_property_by_index(_IN_ int devid, _IN_ int index,
2103                                             _OU_ void **property)
2104 {
2105     int res = 0;
2106     dm_mgr_dev_node_t *node = NULL;
2107 
2108     if (devid < 0 || index < 0) {
2109         return DM_INVALID_PARAMETER;
2110     }
2111 
2112     res = _dm_mgr_search_dev_by_devid(devid, &node);
2113     if (res != SUCCESS_RETURN) {
2114         return FAIL_RETURN;
2115     }
2116 
2117     return dm_shw_get_property_by_index(node->dev_shadow, index, property);
2118 }
2119 
dm_mgr_deprecated_get_service_by_index(_IN_ int devid,_IN_ int index,_OU_ void ** service)2120 int dm_mgr_deprecated_get_service_by_index(_IN_ int devid, _IN_ int index,
2121                                            _OU_ void **service)
2122 {
2123     int res = 0;
2124     dm_mgr_dev_node_t *node = NULL;
2125 
2126     if (devid < 0 || index < 0) {
2127         return DM_INVALID_PARAMETER;
2128     }
2129 
2130     res = _dm_mgr_search_dev_by_devid(devid, &node);
2131     if (res != SUCCESS_RETURN) {
2132         return FAIL_RETURN;
2133     }
2134 
2135     return dm_shw_get_service_by_index(node->dev_shadow, index, service);
2136 }
2137 
dm_mgr_deprecated_get_event_by_index(_IN_ int devid,_IN_ int index,_OU_ void ** event)2138 int dm_mgr_deprecated_get_event_by_index(_IN_ int devid, _IN_ int index,
2139                                          _OU_ void **event)
2140 {
2141     int res = 0;
2142     dm_mgr_dev_node_t *node = NULL;
2143 
2144     if (devid < 0 || index < 0) {
2145         return DM_INVALID_PARAMETER;
2146     }
2147 
2148     res = _dm_mgr_search_dev_by_devid(devid, &node);
2149     if (res != SUCCESS_RETURN) {
2150         return FAIL_RETURN;
2151     }
2152 
2153     return dm_shw_get_event_by_index(node->dev_shadow, index, event);
2154 }
2155 
dm_mgr_deprecated_get_service_by_identifier(_IN_ int devid,_IN_ char * identifier,_OU_ void ** service)2156 int dm_mgr_deprecated_get_service_by_identifier(_IN_ int devid,
2157                                                 _IN_ char *identifier,
2158                                                 _OU_ void **service)
2159 {
2160     int res = 0;
2161     dm_mgr_dev_node_t *node = NULL;
2162 
2163     if (devid < 0 || identifier == NULL || service == NULL ||
2164         *service != NULL) {
2165         return DM_INVALID_PARAMETER;
2166     }
2167     res = _dm_mgr_search_dev_by_devid(devid, &node);
2168     if (res != SUCCESS_RETURN) {
2169         return FAIL_RETURN;
2170     }
2171 
2172     return dm_shw_get_service_by_identifier(node->dev_shadow, identifier,
2173                                             service);
2174 }
2175 
dm_mgr_deprecated_get_event_by_identifier(_IN_ int devid,_IN_ char * identifier,_OU_ void ** event)2176 int dm_mgr_deprecated_get_event_by_identifier(_IN_ int devid,
2177                                               _IN_ char *identifier,
2178                                               _OU_ void **event)
2179 {
2180     int res = 0;
2181     dm_mgr_dev_node_t *node = NULL;
2182 
2183     if (devid < 0 || identifier == NULL || event == NULL || *event != NULL) {
2184         return DM_INVALID_PARAMETER;
2185     }
2186 
2187     res = _dm_mgr_search_dev_by_devid(devid, &node);
2188     if (res != SUCCESS_RETURN) {
2189         return FAIL_RETURN;
2190     }
2191 
2192     return dm_shw_get_event_by_identifier(node->dev_shadow, identifier, event);
2193 }
2194 
dm_mgr_deprecated_get_property_identifier(_IN_ void * property,_OU_ char ** identifier)2195 int dm_mgr_deprecated_get_property_identifier(_IN_ void *property,
2196                                               _OU_ char **identifier)
2197 {
2198     if (property == NULL || identifier == NULL) {
2199         return DM_INVALID_PARAMETER;
2200     }
2201 
2202     return dm_shw_get_property_identifier(property, identifier);
2203 }
2204 
dm_mgr_deprecated_get_service_method(_IN_ void * service,_OU_ char ** method)2205 int dm_mgr_deprecated_get_service_method(_IN_ void *service, _OU_ char **method)
2206 {
2207     if (service == NULL || method == NULL || *method != NULL) {
2208         return DM_INVALID_PARAMETER;
2209     }
2210 
2211     return dm_shw_get_service_method(service, method);
2212 }
2213 
dm_mgr_deprecated_get_event_method(_IN_ void * event,_OU_ char ** method)2214 int dm_mgr_deprecated_get_event_method(_IN_ void *event, _OU_ char **method)
2215 {
2216     if (event == NULL || method == NULL) {
2217         return DM_INVALID_PARAMETER;
2218     }
2219 
2220     return dm_shw_get_event_method(event, method);
2221 }
2222 
dm_mgr_deprecated_set_property_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value,_IN_ int value_len)2223 int dm_mgr_deprecated_set_property_value(_IN_ int devid, _IN_ char *key,
2224                                          _IN_ int key_len, _IN_ void *value,
2225                                          _IN_ int value_len)
2226 {
2227     int res = 0;
2228     dm_mgr_dev_node_t *node = NULL;
2229 
2230     if (key == NULL || key_len <= 0 || value == NULL) {
2231         return DM_INVALID_PARAMETER;
2232     }
2233 
2234     res = _dm_mgr_search_dev_by_devid(devid, &node);
2235     if (res != SUCCESS_RETURN) {
2236         return FAIL_RETURN;
2237     }
2238 
2239     res = dm_shw_set_property_value(node->dev_shadow, key, key_len, value,
2240                                     value_len);
2241     if (res != SUCCESS_RETURN) {
2242         return FAIL_RETURN;
2243     }
2244 
2245     return SUCCESS_RETURN;
2246 }
2247 
dm_mgr_deprecated_get_property_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value)2248 int dm_mgr_deprecated_get_property_value(_IN_ int devid, _IN_ char *key,
2249                                          _IN_ int key_len, _IN_ void *value)
2250 {
2251     int res = 0;
2252     dm_mgr_dev_node_t *node = NULL;
2253 
2254     if (key == NULL || key_len <= 0 || value == NULL) {
2255         return DM_INVALID_PARAMETER;
2256     }
2257 
2258     res = _dm_mgr_search_dev_by_devid(devid, &node);
2259     if (res != SUCCESS_RETURN) {
2260         return FAIL_RETURN;
2261     }
2262 
2263     res = dm_shw_get_property_value(node->dev_shadow, key, key_len, value);
2264     if (res != SUCCESS_RETURN) {
2265         return FAIL_RETURN;
2266     }
2267 
2268     return SUCCESS_RETURN;
2269 }
2270 
dm_mgr_deprecated_set_event_output_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value,_IN_ int value_len)2271 int dm_mgr_deprecated_set_event_output_value(_IN_ int devid, _IN_ char *key,
2272                                              _IN_ int key_len, _IN_ void *value,
2273                                              _IN_ int value_len)
2274 {
2275     int res = 0;
2276     dm_mgr_dev_node_t *node = NULL;
2277 
2278     if (key == NULL || key_len <= 0 || value == NULL) {
2279         return DM_INVALID_PARAMETER;
2280     }
2281 
2282     res = _dm_mgr_search_dev_by_devid(devid, &node);
2283     if (res != SUCCESS_RETURN) {
2284         return FAIL_RETURN;
2285     }
2286 
2287     res = dm_shw_set_event_output_value(node->dev_shadow, key, key_len, value,
2288                                         value_len);
2289     if (res != SUCCESS_RETURN) {
2290         return FAIL_RETURN;
2291     }
2292 
2293     return SUCCESS_RETURN;
2294 }
2295 
dm_mgr_deprecated_get_event_output_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value)2296 int dm_mgr_deprecated_get_event_output_value(_IN_ int devid, _IN_ char *key,
2297                                              _IN_ int key_len, _IN_ void *value)
2298 {
2299     int res = 0;
2300     dm_mgr_dev_node_t *node = NULL;
2301 
2302     if (key == NULL || key_len <= 0 || value == NULL) {
2303         return DM_INVALID_PARAMETER;
2304     }
2305 
2306     res = _dm_mgr_search_dev_by_devid(devid, &node);
2307     if (res != SUCCESS_RETURN) {
2308         return FAIL_RETURN;
2309     }
2310 
2311     res = dm_shw_get_event_output_value(node->dev_shadow, key, key_len, value);
2312     if (res != SUCCESS_RETURN) {
2313         return FAIL_RETURN;
2314     }
2315 
2316     return SUCCESS_RETURN;
2317 }
2318 
dm_mgr_deprecated_set_service_input_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value,_IN_ int value_len)2319 int dm_mgr_deprecated_set_service_input_value(_IN_ int devid, _IN_ char *key,
2320                                               _IN_ int key_len,
2321                                               _IN_ void *value,
2322                                               _IN_ int value_len)
2323 {
2324     int res = 0;
2325     dm_mgr_dev_node_t *node = NULL;
2326 
2327     if (key == NULL || key_len <= 0 || value == NULL) {
2328         return DM_INVALID_PARAMETER;
2329     }
2330 
2331     res = _dm_mgr_search_dev_by_devid(devid, &node);
2332     if (res != SUCCESS_RETURN) {
2333         return FAIL_RETURN;
2334     }
2335 
2336     res = dm_shw_set_service_input_output_value(
2337         DM_SHW_DATA_TARGET_SERVICE_INPUT_DATA, node->dev_shadow, key, key_len,
2338         value, value_len);
2339     if (res != SUCCESS_RETURN) {
2340         return FAIL_RETURN;
2341     }
2342 
2343     return SUCCESS_RETURN;
2344 }
2345 
dm_mgr_deprecated_get_service_input_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value)2346 int dm_mgr_deprecated_get_service_input_value(_IN_ int devid, _IN_ char *key,
2347                                               _IN_ int key_len,
2348                                               _IN_ void *value)
2349 {
2350     int res = 0;
2351     dm_mgr_dev_node_t *node = NULL;
2352 
2353     if (key == NULL || key_len <= 0 || value == NULL) {
2354         return DM_INVALID_PARAMETER;
2355     }
2356 
2357     res = _dm_mgr_search_dev_by_devid(devid, &node);
2358     if (res != SUCCESS_RETURN) {
2359         return FAIL_RETURN;
2360     }
2361 
2362     res = dm_shw_get_service_input_output_value(
2363         DM_SHW_DATA_TARGET_SERVICE_INPUT_DATA, node->dev_shadow, key, key_len,
2364         value);
2365     if (res != SUCCESS_RETURN) {
2366         return FAIL_RETURN;
2367     }
2368 
2369     return SUCCESS_RETURN;
2370 }
2371 
dm_mgr_deprecated_set_service_output_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value,_IN_ int value_len)2372 int dm_mgr_deprecated_set_service_output_value(_IN_ int devid, _IN_ char *key,
2373                                                _IN_ int key_len,
2374                                                _IN_ void *value,
2375                                                _IN_ int value_len)
2376 {
2377     int res = 0;
2378     dm_mgr_dev_node_t *node = NULL;
2379 
2380     if (key == NULL || key_len <= 0 || value == NULL) {
2381         return DM_INVALID_PARAMETER;
2382     }
2383 
2384     res = _dm_mgr_search_dev_by_devid(devid, &node);
2385     if (res != SUCCESS_RETURN) {
2386         return FAIL_RETURN;
2387     }
2388 
2389     res = dm_shw_set_service_input_output_value(
2390         DM_SHW_DATA_TARGET_SERVICE_OUTPUT_DATA, node->dev_shadow, key, key_len,
2391         value, value_len);
2392     if (res != SUCCESS_RETURN) {
2393         return FAIL_RETURN;
2394     }
2395 
2396     return SUCCESS_RETURN;
2397 }
2398 
dm_mgr_deprecated_get_service_output_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value)2399 int dm_mgr_deprecated_get_service_output_value(_IN_ int devid, _IN_ char *key,
2400                                                _IN_ int key_len,
2401                                                _IN_ void *value)
2402 {
2403     int res = 0;
2404     dm_mgr_dev_node_t *node = NULL;
2405 
2406     if (devid < 0 || key == NULL || key_len <= 0 || value == NULL) {
2407         return DM_INVALID_PARAMETER;
2408     }
2409 
2410     res = _dm_mgr_search_dev_by_devid(devid, &node);
2411     if (res != SUCCESS_RETURN) {
2412         return FAIL_RETURN;
2413     }
2414 
2415     res = dm_shw_get_service_input_output_value(
2416         DM_SHW_DATA_TARGET_SERVICE_OUTPUT_DATA, node->dev_shadow, key, key_len,
2417         value);
2418     if (res != SUCCESS_RETURN) {
2419         return FAIL_RETURN;
2420     }
2421 
2422     return SUCCESS_RETURN;
2423 }
2424 
dm_mgr_deprecated_assemble_property(_IN_ int devid,_IN_ char * identifier,_IN_ int identifier_len,_IN_ lite_cjson_item_t * lite)2425 int dm_mgr_deprecated_assemble_property(_IN_ int devid, _IN_ char *identifier,
2426                                         _IN_ int identifier_len,
2427                                         _IN_ lite_cjson_item_t *lite)
2428 {
2429     int res = 0;
2430     dm_mgr_dev_node_t *node = NULL;
2431 
2432     if (devid < 0 || identifier == NULL || identifier_len <= 0 ||
2433         lite == NULL || lite->type != cJSON_Object) {
2434         return DM_INVALID_PARAMETER;
2435     }
2436 
2437     res = _dm_mgr_search_dev_by_devid(devid, &node);
2438     if (res != SUCCESS_RETURN) {
2439         return FAIL_RETURN;
2440     }
2441 
2442     res = dm_shw_assemble_property(node->dev_shadow, identifier, identifier_len,
2443                                    lite);
2444     if (res != SUCCESS_RETURN) {
2445         return FAIL_RETURN;
2446     }
2447 
2448     return SUCCESS_RETURN;
2449 }
2450 
dm_mgr_deprecated_assemble_event_output(_IN_ int devid,_IN_ char * identifier,_IN_ int identifier_len,_IN_ lite_cjson_item_t * lite)2451 int dm_mgr_deprecated_assemble_event_output(_IN_ int devid,
2452                                             _IN_ char *identifier,
2453                                             _IN_ int identifier_len,
2454                                             _IN_ lite_cjson_item_t *lite)
2455 {
2456     int res = 0;
2457     dm_mgr_dev_node_t *node = NULL;
2458 
2459     if (devid < 0 || identifier == NULL || identifier_len <= 0 ||
2460         lite == NULL || lite->type != cJSON_Object) {
2461         return DM_INVALID_PARAMETER;
2462     }
2463 
2464     res = _dm_mgr_search_dev_by_devid(devid, &node);
2465     if (res != SUCCESS_RETURN) {
2466         return FAIL_RETURN;
2467     }
2468 
2469     res = dm_shw_assemble_event_output(node->dev_shadow, identifier,
2470                                        identifier_len, lite);
2471     if (res != SUCCESS_RETURN) {
2472         return FAIL_RETURN;
2473     }
2474 
2475     return SUCCESS_RETURN;
2476 }
2477 
dm_mgr_deprecated_assemble_service_output(_IN_ int devid,_IN_ char * identifier,_IN_ int identifier_len,_IN_ lite_cjson_item_t * lite)2478 int dm_mgr_deprecated_assemble_service_output(_IN_ int devid,
2479                                               _IN_ char *identifier,
2480                                               _IN_ int identifier_len,
2481                                               _IN_ lite_cjson_item_t *lite)
2482 {
2483     int res = 0;
2484     dm_mgr_dev_node_t *node = NULL;
2485 
2486     if (devid < 0 || identifier == NULL || identifier_len <= 0 ||
2487         lite == NULL || lite->type != cJSON_Object) {
2488         return DM_INVALID_PARAMETER;
2489     }
2490 
2491     res = _dm_mgr_search_dev_by_devid(devid, &node);
2492     if (res != SUCCESS_RETURN) {
2493         return FAIL_RETURN;
2494     }
2495 
2496     res = dm_shw_assemble_service_output(node->dev_shadow, identifier,
2497                                          identifier_len, lite);
2498     if (res != SUCCESS_RETURN) {
2499         return FAIL_RETURN;
2500     }
2501 
2502     return SUCCESS_RETURN;
2503 }
2504 
dm_mgr_deprecated_upstream_thing_service_response(_IN_ int devid,_IN_ int msgid,_IN_ iotx_dm_error_code_t code,_IN_ char * identifier,_IN_ int identifier_len,_IN_ char * payload,_IN_ int payload_len)2505 int dm_mgr_deprecated_upstream_thing_service_response(
2506     _IN_ int devid, _IN_ int msgid, _IN_ iotx_dm_error_code_t code,
2507     _IN_ char *identifier, _IN_ int identifier_len, _IN_ char *payload,
2508     _IN_ int payload_len)
2509 {
2510     int res = 0, service_name_len = 0;
2511     char *msgid_str = NULL, *service_name = NULL;
2512     dm_msg_request_payload_t request;
2513     dm_msg_response_t response;
2514 
2515     memset(&request, 0, sizeof(dm_msg_request_payload_t));
2516     memset(&response, 0, sizeof(dm_msg_response_t));
2517 
2518     if (devid < 0 || msgid < 0 || identifier == NULL || identifier_len <= 0 ||
2519         payload == NULL || payload_len <= 0) {
2520         return DM_INVALID_PARAMETER;
2521     }
2522 
2523     /* Response Msg ID */
2524     res = dm_utils_itoa(msgid, &msgid_str);
2525     if (res != SUCCESS_RETURN) {
2526         return FAIL_RETURN;
2527     }
2528     request.id.value = msgid_str;
2529     request.id.value_length = strlen(msgid_str);
2530 
2531     /* Service Name */
2532     service_name_len =
2533         strlen(DM_URI_THING_SERVICE_RESPONSE) + identifier_len + 1;
2534     service_name = DM_malloc(service_name_len);
2535     if (service_name == NULL) {
2536         DM_free(msgid_str);
2537         return DM_MEMORY_NOT_ENOUGH;
2538     }
2539     memset(service_name, 0, service_name_len);
2540     HAL_Snprintf(service_name, service_name_len, DM_URI_THING_SERVICE_RESPONSE,
2541                  identifier_len, identifier);
2542 
2543     res = _dm_mgr_upstream_response_assemble(
2544         devid, msgid_str, strlen(msgid_str), DM_URI_SYS_PREFIX, service_name,
2545         code, &request, &response);
2546     if (res != SUCCESS_RETURN) {
2547         return FAIL_RETURN;
2548     }
2549 
2550     dm_log_debug("Current Service Name: %s", service_name);
2551     dm_msg_response(DM_MSG_DEST_ALL, &request, &response, payload, payload_len,
2552                     NULL);
2553 
2554     DM_free(msgid_str);
2555     DM_free(service_name);
2556     return SUCCESS_RETURN;
2557 }
2558 #endif
2559