1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 
6 
7 #include "iotx_dm_internal.h"
8 #include "dm_opt.h"
9 
10 static dm_api_ctx_t g_dm_api_ctx;
11 
_dm_api_get_ctx(void)12 static dm_api_ctx_t *_dm_api_get_ctx(void)
13 {
14     return &g_dm_api_ctx;
15 }
16 
_dm_api_lock(void)17 static void _dm_api_lock(void)
18 {
19     dm_api_ctx_t *ctx = _dm_api_get_ctx();
20     if (ctx->mutex) {
21         HAL_MutexLock(ctx->mutex);
22     }
23 }
24 
_dm_api_unlock(void)25 static void _dm_api_unlock(void)
26 {
27     dm_api_ctx_t *ctx = _dm_api_get_ctx();
28     if (ctx->mutex) {
29         HAL_MutexUnlock(ctx->mutex);
30     }
31 }
32 
iotx_dm_open(void)33 int iotx_dm_open(void)
34 {
35     int res = 0;
36     dm_api_ctx_t *ctx = _dm_api_get_ctx();
37 #if defined(ALCS_ENABLED) || defined(DEPRECATED_LINKKIT)
38     lite_cjson_hooks hooks;
39 #endif
40     memset(ctx, 0, sizeof(dm_api_ctx_t));
41 
42 #if defined(ALCS_ENABLED) || defined(DEPRECATED_LINKKIT)
43     /* lite-cjson Hooks Init */
44     hooks.malloc_fn = dm_utils_malloc;
45     hooks.free_fn = dm_utils_free;
46     lite_cjson_init_hooks(&hooks);
47 #endif
48 
49     /* DM Mutex Create*/
50     ctx->mutex = HAL_MutexCreate();
51     if (ctx->mutex == NULL) {
52         return DM_MEMORY_NOT_ENOUGH;
53     }
54 
55 #if defined(OTA_ENABLED) && !defined(BUILD_AOS)
56     /* DM OTA Module Init */
57     res = dm_ota_init();
58     if (res != SUCCESS_RETURN) {
59         goto ERROR;
60     }
61 #endif
62 
63 #if !defined(DM_MESSAGE_CACHE_DISABLED)
64     /* DM Message Cache Init */
65     res = dm_msg_cache_init();
66     if (res != SUCCESS_RETURN) {
67         goto ERROR;
68     }
69 #endif
70     /* DM Cloud Message Parse And Assemble Module Init */
71     res = dm_msg_init();
72     if (res != SUCCESS_RETURN) {
73         goto ERROR;
74     }
75 
76     /* DM IPC Module Init */
77     res = dm_ipc_init(CONFIG_DISPATCH_QUEUE_MAXLEN);
78     if (res != SUCCESS_RETURN) {
79         goto ERROR;
80     }
81 
82     /* DM Manager Module Init */
83     res = dm_mgr_init();
84     if (res != SUCCESS_RETURN) {
85         goto ERROR;
86     }
87 
88 #ifdef ALCS_ENABLED
89     /* Open Local Connection */
90     res = dm_server_open();
91     if (res < SUCCESS_RETURN) {
92         goto ERROR;
93     }
94 #endif
95 
96     /* Open Cloud Connection */
97     res = dm_client_open();
98     if (res < SUCCESS_RETURN) {
99         goto ERROR;
100     }
101 
102     return SUCCESS_RETURN;
103 
104 ERROR:
105     dm_client_close();
106 #ifdef ALCS_ENABLED
107     dm_server_close();
108 #endif
109     dm_mgr_deinit();
110     dm_ipc_deinit();
111     dm_msg_deinit();
112 #if !defined(DM_MESSAGE_CACHE_DISABLED)
113     dm_msg_cache_deinit();
114 #endif
115 #if defined(OTA_ENABLED) && !defined(BUILD_AOS)
116     dm_ota_deinit();
117 #endif
118 
119     if (ctx->mutex) {
120         HAL_MutexDestroy(ctx->mutex);
121     }
122     return FAIL_RETURN;
123 }
124 
iotx_dm_connect(_IN_ iotx_dm_init_params_t * init_params)125 int iotx_dm_connect(_IN_ iotx_dm_init_params_t *init_params)
126 {
127     int res = 0;
128     dm_api_ctx_t *ctx = _dm_api_get_ctx();
129 
130     if (init_params == NULL) {
131         return DM_INVALID_PARAMETER;
132     }
133 
134     /* DM Event Callback */
135     if (init_params->event_callback != NULL) {
136         ctx->event_callback = init_params->event_callback;
137     }
138 
139     res = dm_client_connect(IOTX_DM_CLIENT_CONNECT_TIMEOUT_MS);
140     if (res != SUCCESS_RETURN) {
141         return FAIL_RETURN;
142     }
143 
144 #if defined(OTA_ENABLED) && !defined(BUILD_AOS)
145     /* DM OTA Module Init */
146     res = dm_ota_sub();
147     if (res == SUCCESS_RETURN) {
148         /* DM Config OTA Module Init */
149         dm_cota_init();
150 
151         /* DM Firmware OTA Mudule Init */
152         dm_fota_init();
153     }
154 #endif
155 
156 #ifdef ALCS_ENABLED
157     /* DM Connect Local */
158     res = dm_server_connect();
159     if (res != SUCCESS_RETURN) {
160         return FAIL_RETURN;
161     }
162 #endif
163 
164     return SUCCESS_RETURN;
165 }
166 
iotx_dm_subscribe(_IN_ int devid)167 int iotx_dm_subscribe(_IN_ int devid)
168 {
169     int res = 0, dev_type = 0;
170     char product_key[IOTX_PRODUCT_KEY_LEN + 1] = { 0 };
171     char device_name[IOTX_DEVICE_NAME_LEN + 1] = { 0 };
172     char device_secret[IOTX_DEVICE_SECRET_LEN + 1] = { 0 };
173 
174     if (devid < 0) {
175         return DM_INVALID_PARAMETER;
176     }
177 
178     _dm_api_lock();
179     res = dm_mgr_search_device_by_devid(devid, product_key, device_name,
180                                         device_secret);
181     if (res < SUCCESS_RETURN) {
182         _dm_api_unlock();
183         return res;
184     }
185 
186     res = dm_mgr_get_dev_type(devid, &dev_type);
187     if (res < SUCCESS_RETURN) {
188         _dm_api_unlock();
189         return res;
190     }
191 
192 #ifdef ALCS_ENABLED
193     if (devid > 0) {
194         dm_server_add_device(product_key, device_name);
195     }
196 
197     res = dm_server_subscribe_all(product_key, device_name);
198     if (res < SUCCESS_RETURN) {
199         _dm_api_unlock();
200         return res;
201     }
202 #endif
203 
204     res = dm_client_subscribe_all(devid, product_key, device_name, dev_type);
205     if (res < SUCCESS_RETURN) {
206         _dm_api_unlock();
207         return res;
208     }
209 
210     _dm_api_unlock();
211     dm_log_info("Devid %d Sub Completed", devid);
212 
213     return SUCCESS_RETURN;
214 }
215 
iotx_dm_close(void)216 int iotx_dm_close(void)
217 {
218     dm_api_ctx_t *ctx = _dm_api_get_ctx();
219 
220     dm_client_close();
221 #ifdef ALCS_ENABLED
222     dm_server_close();
223 #endif
224     dm_mgr_deinit();
225     dm_ipc_deinit();
226     dm_msg_deinit();
227 #if !defined(DM_MESSAGE_CACHE_DISABLED)
228     dm_msg_cache_deinit();
229 #endif
230 #if defined(OTA_ENABLED) && !defined(BUILD_AOS)
231     dm_cota_deinit();
232     dm_fota_deinit();
233     dm_ota_deinit();
234 #endif
235 
236     if (ctx->mutex) {
237         HAL_MutexDestroy(ctx->mutex);
238     }
239 
240 #if defined(LOG_REPORT_TO_CLOUD) && !defined(DEVICE_MODEL_RAWDATA_SOLO)
241     remove_log_poll();
242 #endif
243 
244     return SUCCESS_RETURN;
245 }
246 
iotx_dm_yield(int timeout_ms)247 int iotx_dm_yield(int timeout_ms)
248 {
249     if (timeout_ms <= 0) {
250         return DM_INVALID_PARAMETER;
251     }
252 
253     dm_client_yield(timeout_ms);
254 #ifdef ALCS_ENABLED
255     dm_server_yield();
256 #endif
257 
258     return SUCCESS_RETURN;
259 }
260 
iotx_dm_dispatch(void)261 void iotx_dm_dispatch(void)
262 {
263     int count = 0;
264     void *data = NULL;
265     dm_api_ctx_t *ctx = _dm_api_get_ctx();
266 
267 #if !defined(DM_MESSAGE_CACHE_DISABLED)
268     dm_msg_cache_tick();
269 #endif
270 
271 #if !defined(DEVICE_MODEL_RAWDATA_SOLO) && !defined(DEPRECATED_LINKKIT)
272     iotx_linkkit_service_list_overtime_handle();
273 #endif
274 
275 #if defined(OTA_ENABLED) && !defined(BUILD_AOS)
276     dm_cota_status_check();
277     dm_fota_status_check();
278 #endif
279     while (CONFIG_DISPATCH_QUEUE_MAXLEN == 0 ||
280            count++ < CONFIG_DISPATCH_QUEUE_MAXLEN) {
281         if (dm_ipc_msg_next(&data) == SUCCESS_RETURN) {
282             dm_ipc_msg_t *msg = (dm_ipc_msg_t *)data;
283 
284             if (ctx->event_callback) {
285                 ctx->event_callback(msg->type, msg->data);
286             }
287 
288             if (msg->data) {
289                 DM_free(msg->data);
290             }
291             DM_free(msg);
292             data = NULL;
293         } else {
294             break;
295         }
296     }
297 }
298 
299 void *g_user_topic_callback = NULL;
300 
iotx_dm_subscribe_user_topic(char * topic,void * user_callback)301 int iotx_dm_subscribe_user_topic(char *topic, void *user_callback)
302 {
303     char *uri = NULL;
304     char product_key[IOTX_PRODUCT_KEY_LEN + 1] = { 0 };
305     char device_name[IOTX_DEVICE_NAME_LEN + 1] = { 0 };
306     int res = 0;
307 
308     HAL_GetProductKey(product_key);
309     HAL_GetDeviceName(device_name);
310 
311     res = dm_utils_service_name((char *)DM_URI_SYS_PREFIX, topic, product_key,
312                                 device_name, &uri);
313     g_user_topic_callback = user_callback;
314 
315     if (res < SUCCESS_RETURN) {
316         return -1;
317     }
318 
319     {
320         int retry_cnt = IOTX_DM_CLIENT_SUB_RETRY_MAX_COUNTS;
321         int local_sub = 0;
322         do {
323             res = dm_client_subscribe(uri, dm_client_user_sub_request,
324                                       &local_sub);
325         } while (res < SUCCESS_RETURN && --retry_cnt);
326         DM_free(uri);
327     }
328     return 0;
329 }
330 
iotx_dm_post_rawdata(_IN_ int devid,_IN_ char * payload,_IN_ int payload_len)331 int iotx_dm_post_rawdata(_IN_ int devid, _IN_ char *payload,
332                          _IN_ int payload_len)
333 {
334     int res = 0;
335 
336     if (devid < 0 || payload == NULL || payload_len <= 0) {
337         return DM_INVALID_PARAMETER;
338     }
339 
340     _dm_api_lock();
341 
342     res = dm_mgr_upstream_thing_model_up_raw(devid, payload, payload_len);
343     if (res != SUCCESS_RETURN) {
344         _dm_api_unlock();
345         return FAIL_RETURN;
346     }
347 
348     _dm_api_unlock();
349     return SUCCESS_RETURN;
350 }
351 
iotx_dm_set_opt(int opt,void * data)352 int iotx_dm_set_opt(int opt, void *data)
353 {
354     return dm_opt_set((dm_opt_t)opt, data);
355 }
356 
iotx_dm_get_opt(int opt,void * data)357 int iotx_dm_get_opt(int opt, void *data)
358 {
359     if (data == NULL) {
360         return FAIL_RETURN;
361     }
362 
363     return dm_opt_get((dm_opt_t)opt, data);
364 }
365 
366 #if !defined(DEVICE_MODEL_RAWDATA_SOLO)
367 #ifdef DEVICE_MODEL_SHADOW
iotx_dm_property_desired_get(_IN_ int devid,_IN_ char * payload,_IN_ int payload_len)368 int iotx_dm_property_desired_get(_IN_ int devid, _IN_ char *payload,
369                                  _IN_ int payload_len)
370 {
371     int res = 0;
372 
373     _dm_api_lock();
374 
375     res =
376         dm_mgr_upstream_thing_property_desired_get(devid, payload, payload_len);
377     if (res < SUCCESS_RETURN) {
378         _dm_api_unlock();
379         return FAIL_RETURN;
380     }
381 
382     _dm_api_unlock();
383     return res;
384 }
385 
iotx_dm_property_desired_delete(_IN_ int devid,_IN_ char * payload,_IN_ int payload_len)386 int iotx_dm_property_desired_delete(_IN_ int devid, _IN_ char *payload,
387                                     _IN_ int payload_len)
388 {
389     int res = 0;
390 
391     _dm_api_lock();
392 
393     res = dm_mgr_upstream_thing_property_desired_delete(devid, payload,
394                                                         payload_len);
395     if (res < SUCCESS_RETURN) {
396         _dm_api_unlock();
397         return FAIL_RETURN;
398     }
399 
400     _dm_api_unlock();
401     return res;
402 }
403 #endif
404 
iotx_dm_post_property(_IN_ int devid,_IN_ char * payload,_IN_ int payload_len)405 int iotx_dm_post_property(_IN_ int devid, _IN_ char *payload,
406                           _IN_ int payload_len)
407 {
408     int res = 0;
409 
410     _dm_api_lock();
411 
412     res = dm_mgr_upstream_thing_property_post(devid, payload, payload_len);
413     if (res < SUCCESS_RETURN) {
414         _dm_api_unlock();
415         return FAIL_RETURN;
416     }
417 
418     _dm_api_unlock();
419     return res;
420 }
421 
422 #ifdef LOG_REPORT_TO_CLOUD
iotx_dm_log_post(_IN_ int devid,_IN_ char * payload,_IN_ int payload_len)423 int iotx_dm_log_post(_IN_ int devid, _IN_ char *payload, _IN_ int payload_len)
424 {
425     int res = 0;
426 
427     _dm_api_lock();
428 
429     res = dm_mgr_upstream_thing_log_post(devid, payload, payload_len, 0);
430     if (res < SUCCESS_RETURN) {
431         _dm_api_unlock();
432         return FAIL_RETURN;
433     }
434 
435     _dm_api_unlock();
436     return res;
437 }
438 #endif
439 
iotx_dm_post_event(_IN_ int devid,_IN_ char * identifier,_IN_ int identifier_len,_IN_ char * payload,_IN_ int payload_len)440 int iotx_dm_post_event(_IN_ int devid, _IN_ char *identifier,
441                        _IN_ int identifier_len, _IN_ char *payload,
442                        _IN_ int payload_len)
443 {
444     int res = 0, method_len = 0;
445     const char *method_fmt = "thing.event.%.*s.post";
446     char *method = NULL;
447 
448     if (devid < 0 || identifier == NULL || identifier_len == 0 ||
449         payload == NULL || payload_len <= 0) {
450         return DM_INVALID_PARAMETER;
451     }
452 
453     _dm_api_lock();
454 
455     method_len = strlen(method_fmt) + strlen(identifier) + 1;
456     method = DM_malloc(method_len);
457     if (method == NULL) {
458         _dm_api_unlock();
459         return DM_MEMORY_NOT_ENOUGH;
460     }
461     memset(method, 0, method_len);
462     HAL_Snprintf(method, method_len, method_fmt, identifier_len, identifier);
463 
464     res = dm_mgr_upstream_thing_event_post(devid, identifier, identifier_len,
465                                            method, payload, payload_len);
466     if (res < SUCCESS_RETURN) {
467         DM_free(method);
468         _dm_api_unlock();
469         return FAIL_RETURN;
470     }
471 
472     DM_free(method);
473     _dm_api_unlock();
474     return res;
475 }
476 
iotx_dm_send_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)477 int iotx_dm_send_service_response(_IN_ int devid, _IN_ char *msgid,
478                                   _IN_ int msgid_len,
479                                   _IN_ iotx_dm_error_code_t code,
480                                   _IN_ char *identifier,
481                                   _IN_ int identifier_len, _IN_ char *payload,
482                                   _IN_ int payload_len, void *ctx)
483 {
484     int res = 0;
485 
486     if (devid < 0 || msgid == NULL || msgid_len <= 0 || identifier == NULL ||
487         identifier_len <= 0 || payload == NULL || payload_len <= 0) {
488         return DM_INVALID_PARAMETER;
489     }
490 
491     _dm_api_lock();
492 
493     dm_log_debug("Current Service Response Payload, Length: %d, Payload: %.*s",
494                  payload_len, payload_len, payload);
495 
496     res = dm_mgr_upstream_thing_service_response(devid, msgid, msgid_len, code,
497                                                  identifier, identifier_len,
498                                                  payload, payload_len, ctx);
499 
500     _dm_api_unlock();
501     return res;
502 }
503 
iotx_dm_send_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,void * ctx)504 int iotx_dm_send_property_get_response(_IN_ int devid, _IN_ char *msgid,
505                                        _IN_ int msgid_len,
506                                        _IN_ iotx_dm_error_code_t code,
507                                        _IN_ char *payload, _IN_ int payload_len,
508                                        void *ctx)
509 {
510     int res = 0;
511 
512     if (devid < 0 || msgid == NULL || msgid_len <= 0 || payload == NULL ||
513         payload_len <= 0) {
514         return DM_INVALID_PARAMETER;
515     }
516 
517     _dm_api_lock();
518 
519     dm_log_debug(
520         "Current Property Get Response Payload, Length: %d, Payload: %.*s",
521         payload_len, payload_len, payload);
522 
523     res = dm_mgr_upstream_thing_property_get_response(
524         devid, msgid, msgid_len, code, payload, payload_len, ctx);
525 
526     _dm_api_unlock();
527     return res;
528 }
529 
iotx_dm_deviceinfo_update(_IN_ int devid,_IN_ char * payload,_IN_ int payload_len)530 int iotx_dm_deviceinfo_update(_IN_ int devid, _IN_ char *payload,
531                               _IN_ int payload_len)
532 {
533     int res = 0;
534 
535     if (devid < 0 || payload == NULL || payload_len <= 0) {
536         return DM_INVALID_PARAMETER;
537     }
538 
539     _dm_api_lock();
540 
541     res = dm_mgr_upstream_thing_deviceinfo_update(devid, payload, payload_len);
542     if (res < SUCCESS_RETURN) {
543         _dm_api_unlock();
544         return FAIL_RETURN;
545     }
546 
547     _dm_api_unlock();
548     return res;
549 }
550 
iotx_dm_deviceinfo_delete(_IN_ int devid,_IN_ char * payload,_IN_ int payload_len)551 int iotx_dm_deviceinfo_delete(_IN_ int devid, _IN_ char *payload,
552                               _IN_ int payload_len)
553 {
554     int res = 0;
555 
556     if (devid < 0 || payload == NULL || payload_len <= 0) {
557         return DM_INVALID_PARAMETER;
558     }
559 
560     _dm_api_lock();
561 
562     res = dm_mgr_upstream_thing_deviceinfo_delete(devid, payload, payload_len);
563     if (res < SUCCESS_RETURN) {
564         _dm_api_unlock();
565         return FAIL_RETURN;
566     }
567 
568     _dm_api_unlock();
569     return res;
570 }
571 
iotx_dm_qurey_ntp(void)572 int iotx_dm_qurey_ntp(void)
573 {
574     int res = 0;
575 
576     _dm_api_lock();
577 
578     res = dm_mgr_upstream_ntp_request();
579     if (res < SUCCESS_RETURN) {
580         _dm_api_unlock();
581         return FAIL_RETURN;
582     }
583 
584     _dm_api_unlock();
585     return res;
586 }
587 
iotx_dm_send_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)588 int iotx_dm_send_rrpc_response(_IN_ int devid, _IN_ char *msgid,
589                                _IN_ int msgid_len,
590                                _IN_ iotx_dm_error_code_t code,
591                                _IN_ char *rrpcid, _IN_ int rrpcid_len,
592                                _IN_ char *payload, _IN_ int payload_len)
593 {
594     int res = 0;
595 
596     if (devid < 0 || msgid == NULL || msgid_len <= 0 || rrpcid == NULL ||
597         rrpcid_len <= 0 || payload == NULL || payload_len <= 0) {
598         return DM_INVALID_PARAMETER;
599     }
600 
601     _dm_api_lock();
602 
603     res = dm_mgr_upstream_rrpc_response(devid, msgid, msgid_len, code, rrpcid,
604                                         rrpcid_len, payload, payload_len);
605 
606     _dm_api_unlock();
607     return res;
608 }
609 #endif
610 
iotx_dm_cota_perform_sync(_OU_ char * buffer,_IN_ int buffer_len)611 int iotx_dm_cota_perform_sync(_OU_ char *buffer, _IN_ int buffer_len)
612 {
613 #if defined(OTA_ENABLED) && !defined(BUILD_AOS)
614     return dm_cota_perform_sync(buffer, buffer_len);
615 #else
616     return -1;
617 #endif
618 }
619 
iotx_dm_cota_get_config(_IN_ const char * config_scope,const char * get_type,const char * attribute_keys)620 int iotx_dm_cota_get_config(_IN_ const char *config_scope, const char *get_type,
621                             const char *attribute_keys)
622 {
623 #if defined(OTA_ENABLED) && !defined(BUILD_AOS)
624     return dm_cota_get_config(config_scope, get_type, attribute_keys);
625 #else
626     return -1;
627 #endif
628 }
629 
iotx_dm_fota_perform_sync(_OU_ char * buffer,_IN_ int buffer_len)630 int iotx_dm_fota_perform_sync(_OU_ char *buffer, _IN_ int buffer_len)
631 {
632 #if defined(OTA_ENABLED) && !defined(BUILD_AOS)
633     return dm_fota_perform_sync(buffer, buffer_len);
634 #else
635     return -1;
636 #endif
637 }
638 
iotx_dm_fota_request_image(const char * version,int buffer_len)639 int iotx_dm_fota_request_image(const char *version, int buffer_len)
640 {
641 #if defined(OTA_ENABLED) && !defined(BUILD_AOS)
642     return dm_fota_request_image(version, buffer_len);
643 #else
644     return -1;
645 #endif
646 }
647 
648 #ifdef DEVICE_MODEL_GATEWAY
iotx_dm_query_topo_list(void)649 int iotx_dm_query_topo_list(void)
650 {
651     int res = 0;
652 
653     _dm_api_lock();
654 
655     res = dm_mgr_upstream_thing_topo_get();
656     if (res < SUCCESS_RETURN) {
657         _dm_api_unlock();
658         return FAIL_RETURN;
659     }
660 
661     _dm_api_unlock();
662     return res;
663 }
664 
iotx_dm_subdev_query(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN+1],_IN_ char device_name[IOTX_DEVICE_NAME_LEN+1],_OU_ int * devid)665 int iotx_dm_subdev_query(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
666                          _IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
667                          _OU_ int *devid)
668 {
669     int res = 0;
670 
671     if (product_key == NULL || device_name == NULL ||
672         (strlen(product_key) >= IOTX_PRODUCT_KEY_LEN + 1) ||
673         (strlen(device_name) >= IOTX_DEVICE_NAME_LEN + 1) || devid == NULL) {
674         return DM_INVALID_PARAMETER;
675     }
676 
677     _dm_api_lock();
678     res = dm_mgr_device_query(product_key, device_name, devid);
679     if (res != SUCCESS_RETURN) {
680         _dm_api_unlock();
681         return FAIL_RETURN;
682     }
683     _dm_api_unlock();
684     return SUCCESS_RETURN;
685 }
686 
iotx_dm_subdev_create(_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)687 int iotx_dm_subdev_create(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
688                           _IN_ char product_secret[IOTX_PRODUCT_SECRET_LEN + 1],
689                           _IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1],
690                           _IN_ char device_secret[IOTX_DEVICE_SECRET_LEN + 1],
691                           _OU_ int *devid)
692 {
693     int res = 0;
694 
695     if (product_key == NULL || device_name == NULL ||
696         (strlen(product_key) >= IOTX_PRODUCT_KEY_LEN + 1) ||
697         (strlen(device_name) >= IOTX_DEVICE_NAME_LEN + 1) || devid == NULL) {
698         return DM_INVALID_PARAMETER;
699     }
700 
701     if (device_secret != NULL &&
702         strlen(device_secret) >= IOTX_DEVICE_SECRET_LEN + 1) {
703         return DM_INVALID_PARAMETER;
704     }
705 
706     _dm_api_lock();
707     res =
708         dm_mgr_device_create(IOTX_DM_DEVICE_SUBDEV, product_key, product_secret,
709                              device_name, device_secret, devid);
710     if (res != SUCCESS_RETURN) {
711         _dm_api_unlock();
712         return FAIL_RETURN;
713     }
714     _dm_api_unlock();
715     return SUCCESS_RETURN;
716 }
717 
iotx_dm_subdev_destroy(_IN_ int devid)718 int iotx_dm_subdev_destroy(_IN_ int devid)
719 {
720     int res = 0;
721 
722     if (devid < 0) {
723         return DM_INVALID_PARAMETER;
724     }
725 
726     _dm_api_lock();
727     res = dm_mgr_device_destroy(devid);
728     if (res != SUCCESS_RETURN) {
729         _dm_api_unlock();
730         return FAIL_RETURN;
731     }
732 
733     _dm_api_unlock();
734     return SUCCESS_RETURN;
735 }
736 
iotx_dm_subdev_number(void)737 int iotx_dm_subdev_number(void)
738 {
739     int number = 0;
740 
741     _dm_api_lock();
742     number = dm_mgr_device_number();
743     _dm_api_unlock();
744 
745     return number;
746 }
747 
iotx_dm_subdev_register(_IN_ int devid)748 int iotx_dm_subdev_register(_IN_ int devid)
749 {
750     int res = 0;
751     dm_mgr_dev_node_t *search_node = NULL;
752 
753     if (devid < 0) {
754         return DM_INVALID_PARAMETER;
755     }
756 
757     _dm_api_lock();
758     res = dm_mgr_search_device_node_by_devid(devid, (void **)&search_node);
759     if (res != SUCCESS_RETURN) {
760         _dm_api_unlock();
761         return FAIL_RETURN;
762     }
763 
764     if ((strlen(search_node->device_secret) > 0) &&
765         (strlen(search_node->device_secret) < IOTX_DEVICE_SECRET_LEN + 1)) {
766         _dm_api_unlock();
767         return SUCCESS_RETURN;
768     }
769 
770     res = dm_mgr_upstream_thing_sub_register(devid);
771 
772     _dm_api_unlock();
773     return res;
774 }
775 
iotx_dm_subdev_proxy_register(_IN_ int devid)776 int iotx_dm_subdev_proxy_register(_IN_ int devid)
777 {
778     int res = 0;
779     dm_mgr_dev_node_t *search_node = NULL;
780 
781     if (devid < 0) {
782         return DM_INVALID_PARAMETER;
783     }
784 
785     _dm_api_lock();
786     res = dm_mgr_search_device_node_by_devid(devid, (void **)&search_node);
787     if (res != SUCCESS_RETURN) {
788         _dm_api_unlock();
789         return FAIL_RETURN;
790     }
791 
792     if ((strlen(search_node->device_secret) > 0) &&
793         (strlen(search_node->device_secret) < IOTX_DEVICE_SECRET_LEN + 1)) {
794         _dm_api_unlock();
795         return SUCCESS_RETURN;
796     }
797 
798     res = dm_mgr_upstream_thing_proxy_product_register(devid);
799 
800     _dm_api_unlock();
801     return res;
802 }
803 
iotx_dm_subdev_unregister(_IN_ int devid)804 int iotx_dm_subdev_unregister(_IN_ int devid)
805 {
806     int res = 0;
807 
808     if (devid < 0) {
809         return DM_INVALID_PARAMETER;
810     }
811 
812     _dm_api_lock();
813 
814     res = dm_mgr_upstream_thing_sub_unregister(devid);
815 
816     _dm_api_unlock();
817     return res;
818 }
819 
iotx_dm_subdev_topo_add(_IN_ int devid)820 int iotx_dm_subdev_topo_add(_IN_ int devid)
821 {
822     int res = 0;
823 
824     if (devid < 0) {
825         return DM_INVALID_PARAMETER;
826     }
827 
828     _dm_api_lock();
829 
830     res = dm_mgr_upstream_thing_topo_add(devid);
831 
832     _dm_api_unlock();
833     return res;
834 }
835 
iotx_dm_subdev_topo_del(_IN_ int devid)836 int iotx_dm_subdev_topo_del(_IN_ int devid)
837 {
838     int res = 0;
839 
840     if (devid < 0) {
841         return DM_INVALID_PARAMETER;
842     }
843 
844     _dm_api_lock();
845 
846     res = dm_mgr_upstream_thing_topo_delete(devid);
847 
848     _dm_api_unlock();
849     return res;
850 }
851 
iotx_dm_subdev_login(_IN_ int devid)852 int iotx_dm_subdev_login(_IN_ int devid)
853 {
854     int res = 0;
855 
856     if (devid < 0) {
857         return DM_INVALID_PARAMETER;
858     }
859 
860     _dm_api_lock();
861 
862     res = dm_mgr_upstream_combine_login(devid);
863 
864     _dm_api_unlock();
865     return res;
866 }
867 
iotx_dm_subdev_logout(_IN_ int devid)868 int iotx_dm_subdev_logout(_IN_ int devid)
869 {
870     int res = 0;
871 
872     if (devid < 0) {
873         return DM_INVALID_PARAMETER;
874     }
875 
876     _dm_api_lock();
877 
878     res = dm_mgr_upstream_combine_logout(devid);
879 
880     _dm_api_unlock();
881     return res;
882 }
883 
iotx_dm_get_device_type(_IN_ int devid,_OU_ int * type)884 int iotx_dm_get_device_type(_IN_ int devid, _OU_ int *type)
885 {
886     int res = 0;
887 
888     if (devid < 0 || type == NULL) {
889         return DM_INVALID_PARAMETER;
890     }
891 
892     _dm_api_lock();
893     res = dm_mgr_get_dev_type(devid, type);
894     if (res != SUCCESS_RETURN) {
895         _dm_api_unlock();
896         return FAIL_RETURN;
897     }
898 
899     _dm_api_unlock();
900     return SUCCESS_RETURN;
901 }
902 
iotx_dm_get_device_avail_status(_IN_ int devid,_OU_ iotx_dm_dev_avail_t * status)903 int iotx_dm_get_device_avail_status(_IN_ int devid,
904                                     _OU_ iotx_dm_dev_avail_t *status)
905 {
906     int res = 0;
907     char product_key[IOTX_PRODUCT_KEY_LEN + 1] = { 0 };
908     char device_name[IOTX_DEVICE_NAME_LEN + 1] = { 0 };
909     char device_secret[IOTX_DEVICE_SECRET_LEN + 1] = { 0 };
910 
911     if (devid < 0 || status == NULL) {
912         return DM_INVALID_PARAMETER;
913     }
914 
915     _dm_api_lock();
916     res = dm_mgr_search_device_by_devid(devid, product_key, device_name,
917                                         device_secret);
918     if (res != SUCCESS_RETURN) {
919         _dm_api_unlock();
920         return FAIL_RETURN;
921     }
922 
923     res = dm_mgr_get_dev_avail(product_key, device_name, status);
924     if (res != SUCCESS_RETURN) {
925         _dm_api_unlock();
926         return FAIL_RETURN;
927     }
928 
929     _dm_api_unlock();
930     return SUCCESS_RETURN;
931 }
932 
iotx_dm_get_device_status(_IN_ int devid,_OU_ iotx_dm_dev_status_t * status)933 int iotx_dm_get_device_status(_IN_ int devid, _OU_ iotx_dm_dev_status_t *status)
934 {
935     int res = 0;
936 
937     if (devid < 0 || status == NULL) {
938         return DM_INVALID_PARAMETER;
939     }
940 
941     _dm_api_lock();
942     res = dm_mgr_get_dev_status(devid, status);
943     _dm_api_unlock();
944 
945     return res;
946 }
947 
948 #ifdef DEVICE_MODEL_SUBDEV_OTA
iotx_dm_firmware_version_update(_IN_ int devid,_IN_ char * payload,_IN_ int payload_len)949 int iotx_dm_firmware_version_update(_IN_ int devid, _IN_ char *payload,
950                                     _IN_ int payload_len)
951 {
952     int res = 0;
953 
954     if (devid < 0 || payload == NULL || payload_len <= 0) {
955         return DM_INVALID_PARAMETER;
956     }
957 
958     _dm_api_lock();
959     res = dm_mgr_upstream_thing_firmware_version_update(devid, payload,
960                                                         payload_len);
961     if (res < SUCCESS_RETURN) {
962         _dm_api_unlock();
963         return FAIL_RETURN;
964     }
965 
966     _dm_api_unlock();
967     return res;
968 }
969 
iotx_dm_send_firmware_version(int devid,const char * version)970 int iotx_dm_send_firmware_version(int devid, const char *version)
971 {
972     char msg[FIRMWARE_VERSION_MSG_LEN] = { 0 };
973     int msg_len = 0;
974     /* firmware report message json data generate */
975     int ret = HAL_Snprintf(msg, FIRMWARE_VERSION_MSG_LEN,
976                            "{\"id\":\"%d\",\"params\":{\"version\":\"%s\"}}",
977                            iotx_report_id(), version);
978     if (ret <= 0) {
979         printf("firmware report message json data generate err");
980         return FAIL_RETURN;
981     }
982 
983     msg_len = strlen(msg);
984 
985     ret = iotx_dm_firmware_version_update(devid, msg, msg_len);
986     return SUCCESS_RETURN;
987 }
988 
iotx_dm_ota_switch_device(_IN_ int devid)989 int iotx_dm_ota_switch_device(_IN_ int devid)
990 {
991     return dm_ota_switch_device(devid);
992 }
993 #endif
994 #endif
995 
996 #ifdef DEPRECATED_LINKKIT
iotx_dm_deprecated_set_tsl(_IN_ int devid,_IN_ iotx_dm_tsl_source_t source,_IN_ const char * tsl,_IN_ int tsl_len)997 int iotx_dm_deprecated_set_tsl(_IN_ int devid, _IN_ iotx_dm_tsl_source_t source,
998                                _IN_ const char *tsl, _IN_ int tsl_len)
999 {
1000     int res = 0;
1001 
1002     if (devid < 0) {
1003         return DM_INVALID_PARAMETER;
1004     }
1005 
1006     _dm_api_lock();
1007     if (source == IOTX_DM_TSL_SOURCE_CLOUD) {
1008         res = dm_mgr_upstream_thing_dynamictsl_get(devid);
1009 
1010         _dm_api_unlock();
1011         return res;
1012     }
1013 
1014     if (source == IOTX_DM_TSL_SOURCE_LOCAL) {
1015         if (tsl == NULL || tsl_len <= 0) {
1016             _dm_api_unlock();
1017             return DM_INVALID_PARAMETER;
1018         }
1019 
1020         res = dm_mgr_deprecated_set_tsl(devid, IOTX_DM_TSL_TYPE_ALINK, tsl,
1021                                         tsl_len);
1022         if (res != SUCCESS_RETURN) {
1023             _dm_api_unlock();
1024             return FAIL_RETURN;
1025         }
1026 
1027         _dm_api_unlock();
1028         return SUCCESS_RETURN;
1029     }
1030 
1031     _dm_api_unlock();
1032     return FAIL_RETURN;
1033 }
1034 
iotx_dm_deprecated_set_property_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value,_IN_ int value_len)1035 int iotx_dm_deprecated_set_property_value(_IN_ int devid, _IN_ char *key,
1036                                           _IN_ int key_len, _IN_ void *value,
1037                                           _IN_ int value_len)
1038 {
1039     int res = 0;
1040 
1041     if (devid < 0 || key == NULL || key_len <= 0 || value == NULL) {
1042         return DM_INVALID_PARAMETER;
1043     }
1044 
1045     _dm_api_lock();
1046     res = dm_mgr_deprecated_set_property_value(devid, key, key_len, value,
1047                                                value_len);
1048     if (res != SUCCESS_RETURN) {
1049         _dm_api_unlock();
1050         return FAIL_RETURN;
1051     }
1052 
1053     _dm_api_unlock();
1054     return SUCCESS_RETURN;
1055 }
1056 
iotx_dm_deprecated_get_property_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value)1057 int iotx_dm_deprecated_get_property_value(_IN_ int devid, _IN_ char *key,
1058                                           _IN_ int key_len, _IN_ void *value)
1059 {
1060     int res = 0;
1061 
1062     if (devid < 0 || key == NULL || key_len <= 0 || value == NULL) {
1063         return DM_INVALID_PARAMETER;
1064     }
1065 
1066     _dm_api_lock();
1067     res = dm_mgr_deprecated_get_property_value(devid, key, key_len, value);
1068     if (res != SUCCESS_RETURN) {
1069         _dm_api_unlock();
1070         return FAIL_RETURN;
1071     }
1072 
1073     _dm_api_unlock();
1074     return SUCCESS_RETURN;
1075 }
1076 
iotx_dm_deprecated_set_event_output_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value,_IN_ int value_len)1077 int iotx_dm_deprecated_set_event_output_value(_IN_ int devid, _IN_ char *key,
1078                                               _IN_ int key_len,
1079                                               _IN_ void *value,
1080                                               _IN_ int value_len)
1081 {
1082     int res = 0;
1083 
1084     if (devid < 0 || key == NULL || key_len <= 0 || value == NULL) {
1085         return DM_INVALID_PARAMETER;
1086     }
1087 
1088     _dm_api_lock();
1089     res = dm_mgr_deprecated_set_event_output_value(devid, key, key_len, value,
1090                                                    value_len);
1091     if (res != SUCCESS_RETURN) {
1092         _dm_api_unlock();
1093         return FAIL_RETURN;
1094     }
1095 
1096     _dm_api_unlock();
1097     return SUCCESS_RETURN;
1098 }
1099 
iotx_dm_deprecated_get_event_output_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value)1100 int iotx_dm_deprecated_get_event_output_value(_IN_ int devid, _IN_ char *key,
1101                                               _IN_ int key_len,
1102                                               _IN_ void *value)
1103 {
1104     int res = 0;
1105 
1106     if (devid < 0 || key == NULL || key_len <= 0 || value == NULL) {
1107         return DM_INVALID_PARAMETER;
1108     }
1109 
1110     _dm_api_lock();
1111     res = dm_mgr_deprecated_get_event_output_value(devid, key, key_len, value);
1112     if (res != SUCCESS_RETURN) {
1113         _dm_api_unlock();
1114         return FAIL_RETURN;
1115     }
1116 
1117     _dm_api_unlock();
1118     return SUCCESS_RETURN;
1119 }
1120 
iotx_dm_deprecated_get_service_input_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value)1121 int iotx_dm_deprecated_get_service_input_value(_IN_ int devid, _IN_ char *key,
1122                                                _IN_ int key_len,
1123                                                _IN_ void *value)
1124 {
1125     int res = 0;
1126 
1127     if (devid < 0 || key == NULL || key_len <= 0 || value == NULL) {
1128         return DM_INVALID_PARAMETER;
1129     }
1130 
1131     _dm_api_lock();
1132     res = dm_mgr_deprecated_get_service_input_value(devid, key, key_len, value);
1133     if (res != SUCCESS_RETURN) {
1134         _dm_api_unlock();
1135         return FAIL_RETURN;
1136     }
1137 
1138     _dm_api_unlock();
1139     return SUCCESS_RETURN;
1140 }
1141 
iotx_dm_deprecated_set_service_output_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value,_IN_ int value_len)1142 int iotx_dm_deprecated_set_service_output_value(_IN_ int devid, _IN_ char *key,
1143                                                 _IN_ int key_len,
1144                                                 _IN_ void *value,
1145                                                 _IN_ int value_len)
1146 {
1147     int res = 0;
1148 
1149     if (devid < 0 || key == NULL || key_len <= 0 || value == NULL) {
1150         return DM_INVALID_PARAMETER;
1151     }
1152 
1153     _dm_api_lock();
1154     res = dm_mgr_deprecated_set_service_output_value(devid, key, key_len, value,
1155                                                      value_len);
1156     if (res != SUCCESS_RETURN) {
1157         _dm_api_unlock();
1158         return FAIL_RETURN;
1159     }
1160 
1161     _dm_api_unlock();
1162     return SUCCESS_RETURN;
1163 }
1164 
iotx_dm_deprecated_get_service_output_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value)1165 int iotx_dm_deprecated_get_service_output_value(_IN_ int devid, _IN_ char *key,
1166                                                 _IN_ int key_len,
1167                                                 _IN_ void *value)
1168 {
1169     int res = 0;
1170 
1171     if (devid < 0 || key == NULL || key_len <= 0 || value == NULL) {
1172         return DM_INVALID_PARAMETER;
1173     }
1174 
1175     _dm_api_lock();
1176     res =
1177         dm_mgr_deprecated_get_service_output_value(devid, key, key_len, value);
1178     if (res != SUCCESS_RETURN) {
1179         _dm_api_unlock();
1180         return FAIL_RETURN;
1181     }
1182 
1183     _dm_api_unlock();
1184     return SUCCESS_RETURN;
1185 }
1186 
iotx_dm_deprecated_post_property_start(_IN_ int devid,_OU_ void ** handle)1187 int iotx_dm_deprecated_post_property_start(_IN_ int devid, _OU_ void **handle)
1188 {
1189     dm_api_property_t *dapi_property = NULL;
1190 
1191     if (devid < 0 || handle == NULL || *handle != NULL) {
1192         return DM_INVALID_PARAMETER;
1193     }
1194 
1195     _dm_api_lock();
1196     dapi_property = DM_malloc(sizeof(dm_api_property_t));
1197     if (dapi_property == NULL) {
1198         _dm_api_unlock();
1199         return DM_MEMORY_NOT_ENOUGH;
1200     }
1201     memset(dapi_property, 0, sizeof(dm_api_property_t));
1202 
1203     /* Create Mutex */
1204     dapi_property->mutex = HAL_MutexCreate();
1205     if (dapi_property->mutex == NULL) {
1206         DM_free(dapi_property);
1207         _dm_api_unlock();
1208         return DM_MEMORY_NOT_ENOUGH;
1209     }
1210 
1211     /* Set Devid */
1212     dapi_property->devid = devid;
1213 
1214     /* Init Json Object */
1215     dapi_property->lite = lite_cjson_create_object();
1216     if (dapi_property->lite == NULL) {
1217         DM_free(dapi_property->mutex);
1218         DM_free(dapi_property);
1219         _dm_api_unlock();
1220         return FAIL_RETURN;
1221     }
1222 
1223     *handle = (void *)dapi_property;
1224 
1225     _dm_api_unlock();
1226     return SUCCESS_RETURN;
1227 }
1228 
_iotx_dm_deprecated_post_property_add(_IN_ void * handle,_IN_ char * identifier,_IN_ int identifier_len)1229 static int _iotx_dm_deprecated_post_property_add(_IN_ void *handle,
1230                                                  _IN_ char *identifier,
1231                                                  _IN_ int identifier_len)
1232 {
1233     int res = 0;
1234     dm_api_property_t *dapi_property = NULL;
1235 
1236     if (handle == NULL || identifier == NULL || identifier_len <= 0) {
1237         return DM_INVALID_PARAMETER;
1238     }
1239     dapi_property = (dm_api_property_t *)handle;
1240 
1241     /* Assemble Property Payload */
1242     res = dm_mgr_deprecated_assemble_property(
1243         dapi_property->devid, identifier, identifier_len, dapi_property->lite);
1244     if (res != SUCCESS_RETURN) {
1245         return FAIL_RETURN;
1246     }
1247 
1248     return SUCCESS_RETURN;
1249 }
1250 
iotx_dm_deprecated_post_property_add(_IN_ void * handle,_IN_ char * identifier,_IN_ int identifier_len)1251 int iotx_dm_deprecated_post_property_add(_IN_ void *handle,
1252                                          _IN_ char *identifier,
1253                                          _IN_ int identifier_len)
1254 {
1255     int ret = SUCCESS_RETURN, res = 0, index = 0, number = 0;
1256     void *property_refer = NULL;
1257     char *identifier_refer = NULL;
1258     dm_api_property_t *dapi_property = NULL;
1259 
1260     if (handle == NULL) {
1261         return DM_INVALID_PARAMETER;
1262     }
1263 
1264     _dm_api_lock();
1265     dapi_property = (dm_api_property_t *)handle;
1266 
1267     if (identifier != IOTX_DM_POST_PROPERTY_ALL) {
1268         if (identifier_len <= 0) {
1269             _dm_api_unlock();
1270             return FAIL_RETURN;
1271         }
1272         ret = _iotx_dm_deprecated_post_property_add(handle, identifier,
1273                                                     identifier_len);
1274 
1275         _dm_api_unlock();
1276         return ret;
1277     }
1278 
1279     res = dm_mgr_deprecated_get_property_number(dapi_property->devid, &number);
1280     if (res != SUCCESS_RETURN) {
1281         _dm_api_unlock();
1282         return FAIL_RETURN;
1283     }
1284 
1285     for (index = 0; index < number; index++) {
1286         property_refer = NULL;
1287         identifier_refer = NULL;
1288 
1289         res = dm_mgr_deprecated_get_property_by_index(dapi_property->devid,
1290                                                       index, &property_refer);
1291         if (res != SUCCESS_RETURN) {
1292             continue;
1293         }
1294 
1295         res = dm_mgr_deprecated_get_property_identifier(property_refer,
1296                                                         &identifier_refer);
1297         if (res != SUCCESS_RETURN) {
1298             continue;
1299         }
1300 
1301         res = _iotx_dm_deprecated_post_property_add(handle, identifier_refer,
1302                                                     strlen(identifier_refer));
1303         if (res != SUCCESS_RETURN) {
1304             ret = FAIL_RETURN;
1305         }
1306     }
1307 
1308     _dm_api_unlock();
1309     return ret;
1310 }
1311 
iotx_dm_deprecated_post_property_end(_IN_ void ** handle)1312 int iotx_dm_deprecated_post_property_end(_IN_ void **handle)
1313 {
1314     int res = 0;
1315     char *payload = NULL;
1316     dm_api_property_t *dapi_property = NULL;
1317 
1318     if (handle == NULL) {
1319         return DM_INVALID_PARAMETER;
1320     }
1321 
1322     _dm_api_lock();
1323     dapi_property = *((dm_api_property_t **)handle);
1324 
1325     payload = lite_cjson_print_unformatted(dapi_property->lite);
1326     if (payload == NULL) {
1327         lite_cjson_delete(dapi_property->lite);
1328         if (dapi_property->mutex) {
1329             HAL_MutexDestroy(dapi_property->mutex);
1330         }
1331         DM_free(dapi_property);
1332         _dm_api_unlock();
1333         return DM_MEMORY_NOT_ENOUGH;
1334     }
1335 
1336     dm_log_debug("Current Property Post Payload, Length: %d, Payload: %s",
1337                  strlen(payload), payload);
1338 
1339     res = dm_mgr_upstream_thing_property_post(dapi_property->devid, payload,
1340                                               strlen(payload));
1341 
1342     DM_free(payload);
1343     lite_cjson_delete(dapi_property->lite);
1344     if (dapi_property->mutex) {
1345         HAL_MutexDestroy(dapi_property->mutex);
1346     }
1347     DM_free(dapi_property);
1348     *handle = NULL;
1349 
1350     _dm_api_unlock();
1351     return res;
1352 }
1353 
iotx_dm_deprecated_post_event(_IN_ int devid,_IN_ char * identifier,_IN_ int identifier_len)1354 int iotx_dm_deprecated_post_event(_IN_ int devid, _IN_ char *identifier,
1355                                   _IN_ int identifier_len)
1356 {
1357     int res = 0;
1358     void *event = NULL;
1359     lite_cjson_item_t *lite = NULL;
1360     char *method = NULL, *payload = NULL;
1361 
1362     if (devid < 0 || identifier == NULL || identifier_len <= 0) {
1363         return DM_INVALID_PARAMETER;
1364     }
1365 
1366     _dm_api_lock();
1367     lite = lite_cjson_create_object();
1368     if (lite == NULL) {
1369         _dm_api_unlock();
1370         return DM_MEMORY_NOT_ENOUGH;
1371     }
1372 
1373     res = dm_mgr_deprecated_assemble_event_output(devid, identifier,
1374                                                   identifier_len, lite);
1375     if (res != SUCCESS_RETURN) {
1376         lite_cjson_delete(lite);
1377         _dm_api_unlock();
1378         return FAIL_RETURN;
1379     }
1380 
1381     payload = lite_cjson_print_unformatted(lite);
1382     lite_cjson_delete(lite);
1383     if (payload == NULL) {
1384         _dm_api_unlock();
1385         return DM_MEMORY_NOT_ENOUGH;
1386     }
1387 
1388     dm_log_debug("Current Event Post Payload, Length: %d, Payload: %s",
1389                  strlen(payload), payload);
1390 
1391     res = dm_mgr_deprecated_get_event_by_identifier(devid, identifier, &event);
1392     if (res != SUCCESS_RETURN) {
1393         DM_free(payload);
1394         _dm_api_unlock();
1395         return FAIL_RETURN;
1396     }
1397 
1398     res = dm_mgr_deprecated_get_event_method(event, &method);
1399     if (res != SUCCESS_RETURN) {
1400         DM_free(payload);
1401         _dm_api_unlock();
1402         return FAIL_RETURN;
1403     }
1404 
1405     dm_log_debug("Current Event Method: %s", method);
1406 
1407     res = dm_mgr_upstream_thing_event_post(devid, identifier, identifier_len,
1408                                            method, payload, strlen(payload));
1409 
1410     DM_free(payload);
1411     DM_free(method);
1412 
1413     _dm_api_unlock();
1414     return res;
1415 }
1416 
iotx_dm_deprecated_legacy_set_property_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value,_IN_ char * value_str)1417 int iotx_dm_deprecated_legacy_set_property_value(_IN_ int devid, _IN_ char *key,
1418                                                  _IN_ int key_len,
1419                                                  _IN_ void *value,
1420                                                  _IN_ char *value_str)
1421 {
1422     int res = 0;
1423     void *data = NULL;
1424     dm_shw_data_type_e type;
1425 
1426     if (devid < 0 || key == NULL || key_len <= 0 ||
1427         ((value == NULL) && (value_str == NULL))) {
1428         return DM_INVALID_PARAMETER;
1429     }
1430 
1431     _dm_api_lock();
1432     res = dm_mgr_deprecated_get_property_data(devid, key, key_len, &data);
1433     if (res != SUCCESS_RETURN) {
1434         _dm_api_unlock();
1435         return FAIL_RETURN;
1436     }
1437 
1438     res = dm_mgr_deprecated_get_data_type(data, &type);
1439     if (res != SUCCESS_RETURN) {
1440         _dm_api_unlock();
1441         return FAIL_RETURN;
1442     }
1443 
1444     switch (type) {
1445     case DM_SHW_DATA_TYPE_INT:
1446     case DM_SHW_DATA_TYPE_ENUM:
1447     case DM_SHW_DATA_TYPE_BOOL:
1448         {
1449             int value_int =
1450                 (value == NULL) ? (atoi(value_str)) : (*(int *)value);
1451             res = dm_mgr_deprecated_set_property_value(devid, key, key_len,
1452                                                        &value_int, sizeof(int));
1453         }
1454         break;
1455     case DM_SHW_DATA_TYPE_FLOAT:
1456         {
1457             float value_float =
1458                 (value == NULL) ? (atof(value_str)) : (*(float *)value);
1459             res = dm_mgr_deprecated_set_property_value(
1460                 devid, key, key_len, &value_float, sizeof(float));
1461         }
1462         break;
1463     case DM_SHW_DATA_TYPE_DOUBLE:
1464         {
1465             double value_double =
1466                 (value == NULL) ? (atof(value_str)) : (*(double *)value);
1467             res = dm_mgr_deprecated_set_property_value(
1468                 devid, key, key_len, &value_double, sizeof(double));
1469         }
1470         break;
1471     case DM_SHW_DATA_TYPE_TEXT:
1472     case DM_SHW_DATA_TYPE_DATE:
1473         {
1474             char *value_string = (value == NULL) ? (value_str) : (value);
1475             res = dm_mgr_deprecated_set_property_value(
1476                 devid, key, key_len, value_string, strlen(value_string));
1477         }
1478         break;
1479     default:
1480         {
1481             res = FAIL_RETURN;
1482         }
1483         break;
1484     }
1485 
1486     if (res != SUCCESS_RETURN) {
1487         _dm_api_unlock();
1488         return FAIL_RETURN;
1489     }
1490 
1491     _dm_api_unlock();
1492     return SUCCESS_RETURN;
1493 }
1494 
iotx_dm_deprecated_legacy_get_property_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value,_IN_ char ** value_str)1495 int iotx_dm_deprecated_legacy_get_property_value(_IN_ int devid, _IN_ char *key,
1496                                                  _IN_ int key_len,
1497                                                  _IN_ void *value,
1498                                                  _IN_ char **value_str)
1499 {
1500     int res = 0;
1501     void *data = NULL;
1502     dm_shw_data_type_e type;
1503 
1504     if (devid < 0 || key == NULL || key_len <= 0 ||
1505         ((value == NULL) && (value_str == NULL))) {
1506         return DM_INVALID_PARAMETER;
1507     }
1508 
1509     _dm_api_lock();
1510     res = dm_mgr_deprecated_get_property_data(devid, key, key_len, &data);
1511     if (res != SUCCESS_RETURN) {
1512         _dm_api_unlock();
1513         return FAIL_RETURN;
1514     }
1515 
1516     res = dm_mgr_deprecated_get_data_type(data, &type);
1517     if (res != SUCCESS_RETURN) {
1518         _dm_api_unlock();
1519         return FAIL_RETURN;
1520     }
1521 
1522     switch (type) {
1523     case DM_SHW_DATA_TYPE_INT:
1524     case DM_SHW_DATA_TYPE_ENUM:
1525     case DM_SHW_DATA_TYPE_BOOL:
1526         {
1527             int value_int = 0;
1528             res = dm_mgr_deprecated_get_property_value(devid, key, key_len,
1529                                                        (void *)&value_int);
1530             if (res == SUCCESS_RETURN) {
1531                 if (value) {
1532                     *(int *)value = value_int;
1533                 }
1534                 if (value_str) {
1535                     res = dm_utils_itoa_direct(value_int, value_str);
1536                 }
1537             }
1538         }
1539         break;
1540     case DM_SHW_DATA_TYPE_FLOAT:
1541         {
1542             float value_float = 0;
1543             res = dm_mgr_deprecated_get_property_value(devid, key, key_len,
1544                                                        (void *)&value_float);
1545             if (res == SUCCESS_RETURN) {
1546                 if (value) {
1547                     *(float *)value = value_float;
1548                 }
1549                 if (value_str) {
1550                     res = dm_utils_ftoa_direct(value_float, value_str);
1551                 }
1552             }
1553         }
1554         break;
1555     case DM_SHW_DATA_TYPE_DOUBLE:
1556         {
1557             double value_double = 0;
1558             res = dm_mgr_deprecated_get_property_value(devid, key, key_len,
1559                                                        (void *)&value_double);
1560             if (res == SUCCESS_RETURN) {
1561                 if (value) {
1562                     *(double *)value = value_double;
1563                 }
1564                 if (value_str) {
1565                     res = dm_utils_ftoa_direct(value_double, value_str);
1566                 }
1567             }
1568         }
1569         break;
1570     case DM_SHW_DATA_TYPE_TEXT:
1571     case DM_SHW_DATA_TYPE_DATE:
1572         {
1573             char *value_string = NULL;
1574             res = dm_mgr_deprecated_get_property_value(devid, key, key_len,
1575                                                        (void *)&value_string);
1576             if (res == SUCCESS_RETURN) {
1577                 if (value) {
1578                     memcpy(value, value_string, strlen(value_string));
1579                 }
1580                 if (value_str) {
1581                     *value_str = value_string;
1582                 } else {
1583                     HAL_Free(value_string);
1584                 }
1585             }
1586         }
1587         break;
1588     default:
1589         {
1590             res = FAIL_RETURN;
1591         }
1592         break;
1593     }
1594 
1595     if (res != SUCCESS_RETURN) {
1596         _dm_api_unlock();
1597         return FAIL_RETURN;
1598     }
1599 
1600     _dm_api_unlock();
1601     return SUCCESS_RETURN;
1602 }
1603 
iotx_dm_deprecated_legacy_set_event_output_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value,_IN_ char * value_str)1604 int iotx_dm_deprecated_legacy_set_event_output_value(_IN_ int devid,
1605                                                      _IN_ char *key,
1606                                                      _IN_ int key_len,
1607                                                      _IN_ void *value,
1608                                                      _IN_ char *value_str)
1609 {
1610     int res = 0;
1611     void *data = NULL;
1612     dm_shw_data_type_e type;
1613 
1614     if (devid < 0 || key == NULL || key_len <= 0 ||
1615         ((value == NULL) && (value_str == NULL))) {
1616         return DM_INVALID_PARAMETER;
1617     }
1618 
1619     _dm_api_lock();
1620     res = dm_mgr_deprecated_get_event_output_data(devid, key, key_len, &data);
1621     if (res != SUCCESS_RETURN) {
1622         _dm_api_unlock();
1623         return FAIL_RETURN;
1624     }
1625 
1626     res = dm_mgr_deprecated_get_data_type(data, &type);
1627     if (res != SUCCESS_RETURN) {
1628         _dm_api_unlock();
1629         return FAIL_RETURN;
1630     }
1631 
1632     switch (type) {
1633     case DM_SHW_DATA_TYPE_INT:
1634     case DM_SHW_DATA_TYPE_ENUM:
1635     case DM_SHW_DATA_TYPE_BOOL:
1636         {
1637             int value_int =
1638                 (value == NULL) ? (atoi(value_str)) : (*(int *)value);
1639             res = dm_mgr_deprecated_set_event_output_value(
1640                 devid, key, key_len, &value_int, sizeof(int));
1641         }
1642         break;
1643     case DM_SHW_DATA_TYPE_FLOAT:
1644         {
1645             float value_float =
1646                 (value == NULL) ? (atof(value_str)) : (*(float *)value);
1647             res = dm_mgr_deprecated_set_event_output_value(
1648                 devid, key, key_len, &value_float, sizeof(float));
1649         }
1650         break;
1651     case DM_SHW_DATA_TYPE_DOUBLE:
1652         {
1653             double value_double =
1654                 (value == NULL) ? (atof(value_str)) : (*(double *)value);
1655             res = dm_mgr_deprecated_set_event_output_value(
1656                 devid, key, key_len, &value_double, sizeof(double));
1657         }
1658         break;
1659     case DM_SHW_DATA_TYPE_TEXT:
1660     case DM_SHW_DATA_TYPE_DATE:
1661         {
1662             char *value_string = (value == NULL) ? (value_str) : (value);
1663             res = dm_mgr_deprecated_set_event_output_value(
1664                 devid, key, key_len, value_string, strlen(value_string));
1665         }
1666         break;
1667     default:
1668         {
1669             res = FAIL_RETURN;
1670         }
1671         break;
1672     }
1673 
1674     if (res != SUCCESS_RETURN) {
1675         _dm_api_unlock();
1676         return FAIL_RETURN;
1677     }
1678 
1679     _dm_api_unlock();
1680     return SUCCESS_RETURN;
1681 }
1682 
iotx_dm_deprecated_legacy_get_event_output_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value,_IN_ char ** value_str)1683 int iotx_dm_deprecated_legacy_get_event_output_value(_IN_ int devid,
1684                                                      _IN_ char *key,
1685                                                      _IN_ int key_len,
1686                                                      _IN_ void *value,
1687                                                      _IN_ char **value_str)
1688 {
1689     int res = 0;
1690     void *data = NULL;
1691     dm_shw_data_type_e type;
1692 
1693     if (devid < 0 || key == NULL || key_len <= 0 ||
1694         ((value == NULL) && (value_str == NULL))) {
1695         return DM_INVALID_PARAMETER;
1696     }
1697 
1698     _dm_api_lock();
1699     res = dm_mgr_deprecated_get_event_output_data(devid, key, key_len, &data);
1700     if (res != SUCCESS_RETURN) {
1701         _dm_api_unlock();
1702         return FAIL_RETURN;
1703     }
1704 
1705     res = dm_mgr_deprecated_get_data_type(data, &type);
1706     if (res != SUCCESS_RETURN) {
1707         _dm_api_unlock();
1708         return FAIL_RETURN;
1709     }
1710 
1711     switch (type) {
1712     case DM_SHW_DATA_TYPE_INT:
1713     case DM_SHW_DATA_TYPE_ENUM:
1714     case DM_SHW_DATA_TYPE_BOOL:
1715         {
1716             int value_int = 0;
1717             res = dm_mgr_deprecated_get_event_output_value(devid, key, key_len,
1718                                                            (void *)&value_int);
1719             if (res == SUCCESS_RETURN) {
1720                 if (value) {
1721                     *(int *)value = value_int;
1722                 }
1723                 if (value_str) {
1724                     res = dm_utils_itoa_direct(value_int, value_str);
1725                 }
1726             }
1727         }
1728         break;
1729     case DM_SHW_DATA_TYPE_FLOAT:
1730         {
1731             float value_float = 0;
1732             res = dm_mgr_deprecated_get_event_output_value(
1733                 devid, key, key_len, (void *)&value_float);
1734             if (res == SUCCESS_RETURN) {
1735                 if (value) {
1736                     *(float *)value = value_float;
1737                 }
1738                 if (value_str) {
1739                     res = dm_utils_ftoa_direct(value_float, value_str);
1740                 }
1741             }
1742         }
1743         break;
1744     case DM_SHW_DATA_TYPE_DOUBLE:
1745         {
1746             double value_double = 0;
1747             res = dm_mgr_deprecated_get_event_output_value(
1748                 devid, key, key_len, (void *)&value_double);
1749             if (res == SUCCESS_RETURN) {
1750                 if (value) {
1751                     *(double *)value = value_double;
1752                 }
1753                 if (value_str) {
1754                     res = dm_utils_ftoa_direct(value_double, value_str);
1755                 }
1756             }
1757         }
1758         break;
1759     case DM_SHW_DATA_TYPE_TEXT:
1760     case DM_SHW_DATA_TYPE_DATE:
1761         {
1762             char *value_string = NULL;
1763             res = dm_mgr_deprecated_get_event_output_value(
1764                 devid, key, key_len, (void *)&value_string);
1765             if (res == SUCCESS_RETURN) {
1766                 if (value) {
1767                     memcpy(value, value_string, strlen(value_string));
1768                 }
1769                 if (value_str) {
1770                     *value_str = value_string;
1771                 } else {
1772                     HAL_Free(value_string);
1773                 }
1774             }
1775         }
1776         break;
1777     default:
1778         {
1779             res = FAIL_RETURN;
1780         }
1781         break;
1782     }
1783 
1784     if (res != SUCCESS_RETURN) {
1785         _dm_api_unlock();
1786         return FAIL_RETURN;
1787     }
1788 
1789     _dm_api_unlock();
1790     return SUCCESS_RETURN;
1791 }
1792 
iotx_dm_deprecated_legacy_get_service_input_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value,_IN_ char ** value_str)1793 int iotx_dm_deprecated_legacy_get_service_input_value(_IN_ int devid,
1794                                                       _IN_ char *key,
1795                                                       _IN_ int key_len,
1796                                                       _IN_ void *value,
1797                                                       _IN_ char **value_str)
1798 {
1799     int res = 0;
1800     void *data = NULL;
1801     dm_shw_data_type_e type;
1802 
1803     if (devid < 0 || key == NULL || key_len <= 0 ||
1804         ((value == NULL) && (value_str == NULL))) {
1805         return DM_INVALID_PARAMETER;
1806     }
1807 
1808     _dm_api_lock();
1809 
1810     res = dm_mgr_deprecated_get_service_input_data(devid, key, key_len, &data);
1811     if (res != SUCCESS_RETURN) {
1812         _dm_api_unlock();
1813         return FAIL_RETURN;
1814     }
1815 
1816     res = dm_mgr_deprecated_get_data_type(data, &type);
1817     if (res != SUCCESS_RETURN) {
1818         _dm_api_unlock();
1819         return FAIL_RETURN;
1820     }
1821 
1822     switch (type) {
1823     case DM_SHW_DATA_TYPE_INT:
1824     case DM_SHW_DATA_TYPE_ENUM:
1825     case DM_SHW_DATA_TYPE_BOOL:
1826         {
1827             int value_int = 0;
1828             res = dm_mgr_deprecated_get_service_input_value(devid, key, key_len,
1829                                                             (void *)&value_int);
1830             if (res == SUCCESS_RETURN) {
1831                 if (value) {
1832                     *(int *)value = value_int;
1833                 }
1834                 if (value_str) {
1835                     res = dm_utils_itoa_direct(value_int, value_str);
1836                 }
1837             }
1838         }
1839         break;
1840     case DM_SHW_DATA_TYPE_FLOAT:
1841         {
1842             float value_float = 0;
1843             res = dm_mgr_deprecated_get_service_input_value(
1844                 devid, key, key_len, (void *)&value_float);
1845             if (res == SUCCESS_RETURN) {
1846                 if (value) {
1847                     *(float *)value = value_float;
1848                 }
1849                 if (value_str) {
1850                     res = dm_utils_ftoa_direct(value_float, value_str);
1851                 }
1852             }
1853         }
1854         break;
1855     case DM_SHW_DATA_TYPE_DOUBLE:
1856         {
1857             double value_double = 0;
1858             res = dm_mgr_deprecated_get_service_input_value(
1859                 devid, key, key_len, (void *)&value_double);
1860             if (res == SUCCESS_RETURN) {
1861                 if (value) {
1862                     *(double *)value = value_double;
1863                 }
1864                 if (value_str) {
1865                     res = dm_utils_ftoa_direct(value_double, value_str);
1866                 }
1867             }
1868         }
1869         break;
1870     case DM_SHW_DATA_TYPE_TEXT:
1871     case DM_SHW_DATA_TYPE_DATE:
1872         {
1873             char *value_string = NULL;
1874             res = dm_mgr_deprecated_get_service_input_value(
1875                 devid, key, key_len, (void *)&value_string);
1876             if (res == SUCCESS_RETURN) {
1877                 if (value) {
1878                     memcpy(value, value_string, strlen(value_string));
1879                 }
1880                 if (value_str) {
1881                     *value_str = value_string;
1882                 } else {
1883                     HAL_Free(value_string);
1884                 }
1885             }
1886         }
1887         break;
1888     default:
1889         {
1890             res = FAIL_RETURN;
1891         }
1892         break;
1893     }
1894 
1895     if (res != SUCCESS_RETURN) {
1896         _dm_api_unlock();
1897         return FAIL_RETURN;
1898     }
1899 
1900     _dm_api_unlock();
1901     return SUCCESS_RETURN;
1902 }
1903 
iotx_dm_deprecated_legacy_set_service_output_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value,_IN_ char * value_str)1904 int iotx_dm_deprecated_legacy_set_service_output_value(_IN_ int devid,
1905                                                        _IN_ char *key,
1906                                                        _IN_ int key_len,
1907                                                        _IN_ void *value,
1908                                                        _IN_ char *value_str)
1909 {
1910     int res = 0;
1911     void *data = NULL;
1912     dm_shw_data_type_e type;
1913 
1914     if (devid < 0 || key == NULL || key_len <= 0 ||
1915         ((value == NULL) && (value_str == NULL))) {
1916         return DM_INVALID_PARAMETER;
1917     }
1918 
1919     _dm_api_lock();
1920     res = dm_mgr_deprecated_get_service_output_data(devid, key, key_len, &data);
1921     if (res != SUCCESS_RETURN) {
1922         _dm_api_unlock();
1923         return FAIL_RETURN;
1924     }
1925 
1926     res = dm_mgr_deprecated_get_data_type(data, &type);
1927     if (res != SUCCESS_RETURN) {
1928         _dm_api_unlock();
1929         return FAIL_RETURN;
1930     }
1931 
1932     switch (type) {
1933     case DM_SHW_DATA_TYPE_INT:
1934     case DM_SHW_DATA_TYPE_ENUM:
1935     case DM_SHW_DATA_TYPE_BOOL:
1936         {
1937             int value_int =
1938                 (value == NULL) ? (atoi(value_str)) : (*(int *)value);
1939             res = dm_mgr_deprecated_set_service_output_value(
1940                 devid, key, key_len, &value_int, sizeof(int));
1941         }
1942         break;
1943     case DM_SHW_DATA_TYPE_FLOAT:
1944         {
1945             float value_float =
1946                 (value == NULL) ? (atof(value_str)) : (*(float *)value);
1947             res = dm_mgr_deprecated_set_service_output_value(
1948                 devid, key, key_len, &value_float, sizeof(float));
1949         }
1950         break;
1951     case DM_SHW_DATA_TYPE_DOUBLE:
1952         {
1953             double value_double =
1954                 (value == NULL) ? (atof(value_str)) : (*(double *)value);
1955             res = dm_mgr_deprecated_set_service_output_value(
1956                 devid, key, key_len, &value_double, sizeof(double));
1957         }
1958         break;
1959     case DM_SHW_DATA_TYPE_TEXT:
1960     case DM_SHW_DATA_TYPE_DATE:
1961         {
1962             char *value_string = (value == NULL) ? (value_str) : (value);
1963             res = dm_mgr_deprecated_set_service_output_value(
1964                 devid, key, key_len, value_string, strlen(value_string));
1965         }
1966         break;
1967     default:
1968         {
1969             res = FAIL_RETURN;
1970         }
1971         break;
1972     }
1973 
1974     if (res != SUCCESS_RETURN) {
1975         _dm_api_unlock();
1976         return FAIL_RETURN;
1977     }
1978 
1979     _dm_api_unlock();
1980     return SUCCESS_RETURN;
1981 }
1982 
iotx_dm_deprecated_legacy_get_service_output_value(_IN_ int devid,_IN_ char * key,_IN_ int key_len,_IN_ void * value,_IN_ char ** value_str)1983 int iotx_dm_deprecated_legacy_get_service_output_value(_IN_ int devid,
1984                                                        _IN_ char *key,
1985                                                        _IN_ int key_len,
1986                                                        _IN_ void *value,
1987                                                        _IN_ char **value_str)
1988 {
1989     int res = 0;
1990     void *data = NULL;
1991     dm_shw_data_type_e type;
1992 
1993     if (devid < 0 || key == NULL || key_len <= 0 ||
1994         ((value == NULL) && (value_str == NULL))) {
1995         return DM_INVALID_PARAMETER;
1996     }
1997 
1998     _dm_api_lock();
1999 
2000     res = dm_mgr_deprecated_get_service_output_data(devid, key, key_len, &data);
2001     if (res != SUCCESS_RETURN) {
2002         _dm_api_unlock();
2003         return FAIL_RETURN;
2004     }
2005 
2006     res = dm_mgr_deprecated_get_data_type(data, &type);
2007     if (res != SUCCESS_RETURN) {
2008         _dm_api_unlock();
2009         return FAIL_RETURN;
2010     }
2011 
2012     switch (type) {
2013     case DM_SHW_DATA_TYPE_INT:
2014     case DM_SHW_DATA_TYPE_ENUM:
2015     case DM_SHW_DATA_TYPE_BOOL:
2016         {
2017             int value_int = 0;
2018             res = dm_mgr_deprecated_get_service_output_value(
2019                 devid, key, key_len, (void *)&value_int);
2020             if (res == SUCCESS_RETURN) {
2021                 if (value) {
2022                     *(int *)value = value_int;
2023                 }
2024                 if (value_str) {
2025                     res = dm_utils_itoa_direct(value_int, value_str);
2026                 }
2027             }
2028         }
2029         break;
2030     case DM_SHW_DATA_TYPE_FLOAT:
2031         {
2032             float value_float = 0;
2033             res = dm_mgr_deprecated_get_service_output_value(
2034                 devid, key, key_len, (void *)&value_float);
2035             if (res == SUCCESS_RETURN) {
2036                 if (value) {
2037                     *(float *)value = value_float;
2038                 }
2039                 if (value_str) {
2040                     res = dm_utils_ftoa_direct(value_float, value_str);
2041                 }
2042             }
2043         }
2044         break;
2045     case DM_SHW_DATA_TYPE_DOUBLE:
2046         {
2047             double value_double = 0;
2048             res = dm_mgr_deprecated_get_service_output_value(
2049                 devid, key, key_len, (void *)&value_double);
2050             if (res == SUCCESS_RETURN) {
2051                 if (value) {
2052                     *(double *)value = value_double;
2053                 }
2054                 if (value_str) {
2055                     res = dm_utils_ftoa_direct(value_double, value_str);
2056                 }
2057             }
2058         }
2059         break;
2060     case DM_SHW_DATA_TYPE_TEXT:
2061     case DM_SHW_DATA_TYPE_DATE:
2062         {
2063             char *value_string = NULL;
2064             res = dm_mgr_deprecated_get_service_output_value(
2065                 devid, key, key_len, (void *)&value_string);
2066             if (res == SUCCESS_RETURN) {
2067                 if (value) {
2068                     memcpy(value, value_string, strlen(value_string));
2069                 }
2070                 if (value_str) {
2071                     *value_str = value_string;
2072                 } else {
2073                     HAL_Free(value_string);
2074                 }
2075             }
2076         }
2077         break;
2078     default:
2079         {
2080             res = FAIL_RETURN;
2081         }
2082         break;
2083     }
2084 
2085     if (res != SUCCESS_RETURN) {
2086         _dm_api_unlock();
2087         return FAIL_RETURN;
2088     }
2089 
2090     _dm_api_unlock();
2091     return SUCCESS_RETURN;
2092 }
2093 
iotx_dm_deprecated_legacy_get_pkdn_by_devid(_IN_ int devid,_OU_ char product_key[IOTX_PRODUCT_KEY_LEN+1],_OU_ char device_name[IOTX_DEVICE_NAME_LEN+1])2094 int iotx_dm_deprecated_legacy_get_pkdn_by_devid(
2095     _IN_ int devid, _OU_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
2096     _OU_ char device_name[IOTX_DEVICE_NAME_LEN + 1])
2097 {
2098     int res = 0;
2099     char device_secret[IOTX_DEVICE_SECRET_LEN + 1] = { 0 };
2100 
2101     if (devid < 0 || product_key == NULL || device_name == NULL) {
2102         return DM_INVALID_PARAMETER;
2103     }
2104 
2105     _dm_api_lock();
2106     res = dm_mgr_search_device_by_devid(devid, product_key, device_name,
2107                                         device_secret);
2108     if (res != SUCCESS_RETURN) {
2109         _dm_api_unlock();
2110         return FAIL_RETURN;
2111     }
2112 
2113     _dm_api_unlock();
2114     return SUCCESS_RETURN;
2115 }
2116 
iotx_dm_deprecated_legacy_get_devid_by_pkdn(_IN_ char product_key[IOTX_PRODUCT_KEY_LEN+1],_IN_ char device_name[IOTX_DEVICE_NAME_LEN+1],_OU_ int * devid)2117 int iotx_dm_deprecated_legacy_get_devid_by_pkdn(
2118     _IN_ char product_key[IOTX_PRODUCT_KEY_LEN + 1],
2119     _IN_ char device_name[IOTX_DEVICE_NAME_LEN + 1], _OU_ int *devid)
2120 {
2121     int res = 0;
2122 
2123     if (devid == NULL || product_key == NULL || device_name == NULL ||
2124         (strlen(product_key) >= IOTX_PRODUCT_KEY_LEN + 1) ||
2125         (strlen(device_name) >= IOTX_DEVICE_NAME_LEN + 1)) {
2126         return DM_INVALID_PARAMETER;
2127     }
2128 
2129     _dm_api_lock();
2130     res = dm_mgr_search_device_by_pkdn(product_key, device_name, devid);
2131     if (res != SUCCESS_RETURN) {
2132         _dm_api_unlock();
2133         return FAIL_RETURN;
2134     }
2135 
2136     _dm_api_unlock();
2137     return SUCCESS_RETURN;
2138 }
2139 
iotx_dm_deprecated_legacy_get_thingid_by_devid(_IN_ int devid,_OU_ void ** thing_id)2140 int iotx_dm_deprecated_legacy_get_thingid_by_devid(_IN_ int devid,
2141                                                    _OU_ void **thing_id)
2142 {
2143     int res = 0;
2144 
2145     if (devid < 0 || thing_id == NULL || *thing_id != NULL) {
2146         return DM_INVALID_PARAMETER;
2147     }
2148 
2149     _dm_api_lock();
2150     res = dm_mgr_search_device_node_by_devid(devid, thing_id);
2151     if (res != SUCCESS_RETURN) {
2152         _dm_api_unlock();
2153         return FAIL_RETURN;
2154     }
2155 
2156     _dm_api_unlock();
2157     return SUCCESS_RETURN;
2158 }
2159 
iotx_dm_deprecated_legacy_get_devid_by_thingid(_IN_ void * thing_id,_OU_ int * devid)2160 int iotx_dm_deprecated_legacy_get_devid_by_thingid(_IN_ void *thing_id,
2161                                                    _OU_ int *devid)
2162 {
2163     int res = 0;
2164 
2165     if (thing_id == NULL || devid == NULL) {
2166         return DM_INVALID_PARAMETER;
2167     }
2168 
2169     _dm_api_lock();
2170     res = dm_mgr_deprecated_search_devid_by_device_node(thing_id, devid);
2171     if (res != SUCCESS_RETURN) {
2172         _dm_api_unlock();
2173         return FAIL_RETURN;
2174     }
2175 
2176     _dm_api_unlock();
2177     return SUCCESS_RETURN;
2178 }
2179 
iotx_dm_deprecated_legacy_get_pkdn_ptr_by_devid(_IN_ int devid,_OU_ char ** product_key,_OU_ char ** device_name)2180 int iotx_dm_deprecated_legacy_get_pkdn_ptr_by_devid(_IN_ int devid,
2181                                                     _OU_ char **product_key,
2182                                                     _OU_ char **device_name)
2183 {
2184     int res = 0;
2185     dm_mgr_dev_node_t *node = NULL;
2186 
2187     if (devid < 0 || product_key == NULL || *product_key != NULL ||
2188         device_name == NULL || *device_name != NULL) {
2189         return DM_INVALID_PARAMETER;
2190     }
2191 
2192     _dm_api_lock();
2193     res = dm_mgr_search_device_node_by_devid(devid, (void **)&node);
2194     if (res != SUCCESS_RETURN) {
2195         _dm_api_unlock();
2196         return FAIL_RETURN;
2197     }
2198 
2199     *product_key = node->product_key;
2200     *device_name = node->device_name;
2201 
2202     _dm_api_unlock();
2203     return SUCCESS_RETURN;
2204 }
2205 
iotx_dm_deprecated_legacy_send_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)2206 int iotx_dm_deprecated_legacy_send_service_response(
2207     _IN_ int devid, _IN_ int msgid, _IN_ iotx_dm_error_code_t code,
2208     _IN_ char *identifier, _IN_ int identifier_len, _IN_ char *payload,
2209     _IN_ int payload_len)
2210 {
2211     int res = 0;
2212 
2213     _dm_api_lock();
2214 
2215     res = dm_mgr_deprecated_upstream_thing_service_response(
2216         devid, msgid, code, identifier, identifier_len, payload, payload_len);
2217 
2218     _dm_api_unlock();
2219     return res;
2220 }
2221 
iotx_dm_deprecated_send_service_response(_IN_ int devid,_IN_ int msgid,_IN_ iotx_dm_error_code_t code,_IN_ char * identifier,_IN_ int identifier_len)2222 int iotx_dm_deprecated_send_service_response(_IN_ int devid, _IN_ int msgid,
2223                                              _IN_ iotx_dm_error_code_t code,
2224                                              _IN_ char *identifier,
2225                                              _IN_ int identifier_len)
2226 {
2227     int res = 0;
2228     lite_cjson_item_t *lite = NULL;
2229     char *payload = NULL;
2230 
2231     if (devid < 0 || msgid < 0 || identifier == NULL || identifier_len <= 0) {
2232         return DM_INVALID_PARAMETER;
2233     }
2234 
2235     _dm_api_lock();
2236     lite = lite_cjson_create_object();
2237     if (lite == NULL) {
2238         _dm_api_unlock();
2239         return DM_MEMORY_NOT_ENOUGH;
2240     }
2241 
2242     res = dm_mgr_deprecated_assemble_service_output(devid, identifier,
2243                                                     identifier_len, lite);
2244     if (res != SUCCESS_RETURN) {
2245         lite_cjson_delete(lite);
2246         _dm_api_unlock();
2247         return FAIL_RETURN;
2248     }
2249 
2250     payload = lite_cjson_print_unformatted(lite);
2251     lite_cjson_delete(lite);
2252     if (payload == NULL) {
2253         _dm_api_unlock();
2254         return DM_MEMORY_NOT_ENOUGH;
2255     }
2256 
2257     dm_log_debug("Current Service Response Payload, Length: %d, Payload: %s",
2258                  strlen(payload), payload);
2259 
2260     res = dm_mgr_deprecated_upstream_thing_service_response(
2261         devid, msgid, code, identifier, identifier_len, payload,
2262         strlen(payload));
2263 
2264     DM_free(payload);
2265 
2266     _dm_api_unlock();
2267     return res;
2268 }
2269 
2270 #ifdef DEVICE_MODEL_GATEWAY
iotx_dm_deprecated_subdev_register(_IN_ int devid,_IN_ char device_secret[IOTX_DEVICE_SECRET_LEN+1])2271 int iotx_dm_deprecated_subdev_register(
2272     _IN_ int devid, _IN_ char device_secret[IOTX_DEVICE_SECRET_LEN + 1])
2273 {
2274     int res = 0;
2275     dm_mgr_dev_node_t *search_node = NULL;
2276 
2277     if (devid < 0) {
2278         return DM_INVALID_PARAMETER;
2279     }
2280 
2281     _dm_api_lock();
2282     if ((device_secret != NULL) && (strlen(device_secret) > 0) &&
2283         (strlen(device_secret) < IOTX_DEVICE_SECRET_LEN + 1)) {
2284         res = dm_mgr_search_device_node_by_devid(devid, (void **)&search_node);
2285         if (res != SUCCESS_RETURN) {
2286             _dm_api_unlock();
2287             return FAIL_RETURN;
2288         }
2289         memset(search_node->device_secret, 0, IOTX_DEVICE_SECRET_LEN + 1);
2290         memcpy(search_node->device_secret, device_secret,
2291                strlen(device_secret));
2292         _dm_api_unlock();
2293         return SUCCESS_RETURN;
2294     }
2295 
2296     res = dm_mgr_upstream_thing_sub_register(devid);
2297 
2298     _dm_api_unlock();
2299     return res;
2300 }
2301 #endif
2302 #endif
2303