uiot_export_dm.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #ifndef C_SDK_UIOT_EXPORT_DM_H_
  16. #define C_SDK_UIOT_EXPORT_DM_H_
  17. #if defined(__cplusplus)
  18. extern "C" {
  19. #endif
  20. #include "uiot_defs.h"
  21. /* 设备物模型消息类型 */
  22. typedef enum _dm_type {
  23. PROPERTY_RESTORE, //设备恢复属性
  24. PROPERTY_POST, //设备上报属性
  25. PROPERTY_SET, //云端下发属性
  26. PROPERTY_DESIRED_GET, //设备获取desire属性
  27. PROPERTY_DESIRED_DELETE, //删除云端desire属性
  28. EVENT_POST, //设备上报事件
  29. COMMAND, //命令下发
  30. DM_TYPE_MAX
  31. }DM_Type;
  32. typedef enum{
  33. TYPE_INT,
  34. TYPE_FLOAT,
  35. TYPE_DOUBLE,
  36. TYPE_BOOL,
  37. TYPE_ENUM,
  38. TYPE_STRING,
  39. TYPE_DATE,
  40. } DM_Base_Type;
  41. typedef enum{
  42. TYPE_NODE,
  43. TYPE_STRUCT,
  44. TYPE_ARRAY_BASE,
  45. TYPE_ARRAY_STRUCT,
  46. } DM_Parse_Type;
  47. typedef union{
  48. int int32_value;
  49. float float32_value;
  50. double float64_value;
  51. bool bool_value;
  52. int enum_value;
  53. char *string_value;
  54. long date_value;
  55. }DM_Base_Value_U;
  56. typedef struct{
  57. DM_Base_Type base_type;
  58. char *key;
  59. DM_Base_Value_U value;
  60. } DM_Node_t;
  61. typedef struct{
  62. char *key;
  63. DM_Node_t *value;
  64. int num;
  65. } DM_Type_Struct_t;
  66. typedef struct{
  67. char *key;
  68. DM_Node_t *value;
  69. int num;
  70. } DM_Array_Base_t;
  71. typedef struct{
  72. char *key;
  73. DM_Type_Struct_t *value;
  74. int num;
  75. } DM_Array_Struct_t;
  76. typedef union{
  77. DM_Node_t *dm_node;
  78. DM_Type_Struct_t *dm_struct;
  79. DM_Array_Base_t *dm_array_base;
  80. DM_Array_Struct_t *dm_array_struct;
  81. }DM_Property_Value_U;
  82. typedef struct{
  83. DM_Parse_Type parse_type;
  84. DM_Property_Value_U value;
  85. int desired_ver;
  86. } DM_Property_t;
  87. typedef struct{
  88. char *event_identy;
  89. DM_Property_t *dm_property;
  90. int property_num;
  91. } DM_Event_t;
  92. typedef struct{
  93. DM_Property_t *input;
  94. int input_num;
  95. DM_Property_t *output;
  96. int output_num;
  97. } DM_Command_t;
  98. typedef int (* PropertyRestoreCB)(const char *request_id, const int ret_code, const char *property);
  99. typedef int (* PropertySetCB)(const char *request_id, const char *property);
  100. typedef int (* PropertyDesiredGetCB)(const char *request_id, const int ret_code, const char *desired);
  101. typedef int (* CommandCB)(const char *request_id, const char *identifier, const char *input, char *output);
  102. typedef int (* CommonReplyCB)(const char *request_id, const int ret_code);
  103. #define DECLARE_DM_CALLBACK(type, cb) int uiot_register_for_##type(void*, cb);
  104. DECLARE_DM_CALLBACK(PROPERTY_RESTORE, PropertyRestoreCB)
  105. DECLARE_DM_CALLBACK(PROPERTY_POST, CommonReplyCB)
  106. DECLARE_DM_CALLBACK(PROPERTY_SET, PropertySetCB)
  107. DECLARE_DM_CALLBACK(PROPERTY_DESIRED_GET, PropertyDesiredGetCB)
  108. DECLARE_DM_CALLBACK(PROPERTY_DESIRED_DELETE, CommonReplyCB)
  109. DECLARE_DM_CALLBACK(EVENT_POST, CommonReplyCB)
  110. DECLARE_DM_CALLBACK(COMMAND, CommandCB)
  111. /**
  112. * @brief 注册消息回调函数的宏
  113. *
  114. * @param type: 消息类型,七种DM_Type之一
  115. * @param handle: IOT_DM_Init返回的句柄
  116. * @param cb: 回调函数指针,函数类型必须与DECLARE_DM_CALLBACK声明的类型相同,
  117. * 例如如果type是PROPERTY_RESTORE,cb则必须为PropertyRestoreCB类型
  118. *
  119. * @retval 0 : 成功
  120. * @retval < 0 : 失败,返回具体错误码
  121. */
  122. #define IOT_DM_RegisterCallback(type, handle, cb) uiot_register_for_##type(handle, cb);
  123. /**
  124. * @brief 初始化dev_model模块和返回句柄
  125. *
  126. * @param product_sn: 指定产品序列号
  127. * @param device_sn: 指定设备序列号
  128. * @param ch_signal: 指定的信号通道.
  129. *
  130. * @retval 成功返回句柄,失败返回NULL.
  131. */
  132. void *IOT_DM_Init(const char *product_sn, const char *device_sn, void *ch_signal);
  133. /**
  134. * @brief 释放dev_model相关的资源
  135. *
  136. * @param handle: IOT_DM_Init返回的句柄
  137. *
  138. * @retval 0 : 成功
  139. * @retval < 0 : 失败,返回具体错误码
  140. */
  141. int IOT_DM_Destroy(void *handle);
  142. /**
  143. * @brief 属性有关的消息上报
  144. *
  145. * @param handle: IOT_DM_Init返回的句柄
  146. * @param type: 消息类型,此处为
  147. PROPERTY_RESTORE,
  148. PROPERTY_POST,
  149. PROPERTY_DESIRED_GET,
  150. PROPERTY_DESIRED_DELETE
  151. 四种消息类型之一
  152. * @param request_id: 消息的request_id
  153. * @param payload: 消息体
  154. *
  155. * @retval 0 : 成功
  156. * @retval < 0 : 失败,返回具体错误码
  157. */
  158. int IOT_DM_Property_Report(void *handle, DM_Type type, int request_id, const char *payload);
  159. /**
  160. * @brief 属性有关的消息上报,拓展接口
  161. *
  162. * @param handle: IOT_DM_Init返回的句柄
  163. * @param type: 消息类型,此处为
  164. PROPERTY_RESTORE,
  165. PROPERTY_POST,
  166. PROPERTY_DESIRED_GET,
  167. PROPERTY_DESIRED_DELETE
  168. 四种消息类型之一
  169. * @param request_id: 消息的request_id
  170. * @param property_num: 属性个数
  171. * @param ...: 属性
  172. *
  173. * @retval 0 : 成功
  174. * @retval < 0 : 失败,返回具体错误码
  175. */
  176. int IOT_DM_Property_ReportEx(void *handle, DM_Type type, int request_id, int property_num, ...);
  177. /**
  178. * @brief 事件消息上报
  179. *
  180. * @param handle: IOT_DM_Init返回的句柄
  181. * @param request_id: 消息的request_id
  182. * @param identifier: 事件标识符
  183. * @param payload: 事件Output消息体
  184. *
  185. * @retval 0 : 成功
  186. * @retval < 0 : 失败,返回具体错误码
  187. */
  188. int IOT_DM_TriggerEvent(void *handle, int request_id, const char *identifier, const char *payload);
  189. /**
  190. * @brief 事件消息上报,拓展接口
  191. *
  192. * @param handle: IOT_DM_Init返回的句柄
  193. * @param request_id: 消息的request_id
  194. * @param event: 事件的句柄
  195. *
  196. * @retval 0 : 成功
  197. * @retval < 0 : 失败,返回具体错误码
  198. */
  199. int IOT_DM_TriggerEventEx(void *handle, int request_id, DM_Event_t *event);
  200. /**
  201. * @brief 命令消息输出参数键值对生成
  202. *
  203. * @param output: 生成的输出参数键值对
  204. * @param property_num: 属性个数
  205. * @param ...: 属性
  206. *
  207. * @retval 0 : 成功
  208. * @retval < 0 : 失败,返回具体错误码
  209. */
  210. int IOT_DM_GenCommandOutput(char *output, int property_num, ...);
  211. /**
  212. * @brief 在当前线程为底层MQTT客户端让出一定CPU执行时间,让其接收网络报文并将消息分发到用户的回调函数中
  213. *
  214. * @param handle: IOT_DM_Init返回的句柄
  215. * @param timeout_ms: 超时时间,单位ms
  216. *
  217. * @retval 0 : 成功
  218. * @retval < 0 : 失败,返回具体错误码
  219. */
  220. int IOT_DM_Yield(void *handle, uint32_t timeout_ms);
  221. #if defined(__cplusplus)
  222. }
  223. #endif
  224. #endif //C_SDK_UIOT_EXPORT_DM_H_