dm_client.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include <string.h>
  16. #include "uiot_defs.h"
  17. #include "uiot_internal.h"
  18. #include "dm_internal.h"
  19. void *IOT_DM_Init(const char *product_sn, const char *device_sn, void *ch_signal)
  20. {
  21. POINTER_VALID_CHECK(product_sn, NULL);
  22. POINTER_VALID_CHECK(device_sn, NULL);
  23. POINTER_VALID_CHECK(ch_signal, NULL);
  24. DM_Struct_t *h_dm = NULL;
  25. if (NULL == (h_dm = HAL_Malloc(sizeof(DM_Struct_t)))) {
  26. LOG_ERROR("allocate failed");
  27. return NULL;
  28. }
  29. memset(h_dm, 0, sizeof(DM_Struct_t));
  30. h_dm->ch_signal = dsc_init(product_sn, device_sn, ch_signal, h_dm);
  31. if (NULL == h_dm->ch_signal) {
  32. LOG_ERROR("initialize signal channel failed");
  33. HAL_Free(h_dm);
  34. return NULL;
  35. }
  36. return h_dm;
  37. }
  38. int IOT_DM_Destroy(void *handle)
  39. {
  40. POINTER_VALID_CHECK(handle, FAILURE_RET);
  41. DM_Struct_t *h_dm = (DM_Struct_t*) handle;
  42. dsc_deinit(h_dm->ch_signal);
  43. HAL_Free(h_dm);
  44. return SUCCESS_RET;
  45. }
  46. int IOT_DM_Property_Report(void *handle, DM_Type type, int request_id, const char *payload)
  47. {
  48. POINTER_VALID_CHECK(handle, FAILURE_RET);
  49. DM_Struct_t *h_dm = (DM_Struct_t*) handle;
  50. return dm_mqtt_property_report_publish(h_dm->ch_signal, type, request_id, payload);
  51. }
  52. int IOT_DM_Property_ReportEx(void *handle, DM_Type type, int request_id, int property_num, ...)
  53. {
  54. POINTER_VALID_CHECK(handle, FAILURE_RET);
  55. int loop = 0;
  56. int ret = 0;
  57. DM_Struct_t *h_dm = (DM_Struct_t*) handle;
  58. va_list pArgs;
  59. va_start(pArgs, property_num);
  60. DM_Property_t *property = (DM_Property_t *)HAL_Malloc(property_num * sizeof(DM_Property_t));
  61. for(loop = 0; loop < property_num; loop++)
  62. {
  63. DM_Property_t *property_node;
  64. property_node = va_arg(pArgs, DM_Property_t *);
  65. property[loop] = *property_node;
  66. }
  67. va_end(pArgs);
  68. ret = dm_mqtt_property_report_publish_Ex(h_dm->ch_signal, type, request_id, property, property_num);
  69. HAL_Free(property);
  70. return ret;
  71. }
  72. int IOT_DM_TriggerEvent(void *handle, int request_id, const char *identifier, const char *payload)
  73. {
  74. POINTER_VALID_CHECK(handle, FAILURE_RET);
  75. DM_Struct_t *h_dm = (DM_Struct_t*) handle;
  76. return dm_mqtt_event_publish(h_dm->ch_signal, request_id, identifier, payload);
  77. }
  78. int IOT_DM_TriggerEventEx(void *handle, int request_id, DM_Event_t *event)
  79. {
  80. POINTER_VALID_CHECK(handle, FAILURE_RET);
  81. DM_Struct_t *h_dm = (DM_Struct_t*) handle;
  82. return dm_mqtt_event_publish_Ex(h_dm->ch_signal, request_id, event);
  83. }
  84. int IOT_DM_GenCommandOutput(char *output, int property_num, ...)
  85. {
  86. POINTER_VALID_CHECK(output, FAILURE_RET);
  87. int ret = 0;
  88. int loop = 0;
  89. va_list pArgs;
  90. va_start(pArgs, property_num);
  91. DM_Property_t *property = (DM_Property_t *)HAL_Malloc(property_num * sizeof(DM_Property_t));
  92. for(loop = 0; loop < property_num; loop++)
  93. {
  94. DM_Property_t *property_node;
  95. property_node = va_arg(pArgs, DM_Property_t *);
  96. property[loop] = *property_node;
  97. }
  98. va_end(pArgs);
  99. ret = dm_gen_properties_payload(property, property_num, PROPERTY_POST, false, output);
  100. HAL_Free(property);
  101. return ret;
  102. }
  103. int IOT_DM_Yield(void *handle, uint32_t timeout_ms)
  104. {
  105. POINTER_VALID_CHECK(handle, FAILURE_RET);
  106. DM_Struct_t *h_dm = (DM_Struct_t*) handle;
  107. return IOT_MQTT_Yield(((DM_MQTT_Struct_t *)h_dm->ch_signal)->mqtt, timeout_ms);
  108. }