shadow_client_json.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 IOT_SHADOW_CLIENT_JSON_H_
  16. #define IOT_SHADOW_CLIENT_JSON_H_
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #include <stdint.h>
  21. #include <stdbool.h>
  22. #include <stdarg.h>
  23. #include <stddef.h>
  24. #include "uiot_import.h"
  25. #include "shadow_client.h"
  26. /* 回复消息中的消息字段 */
  27. #define METHOD_FIELD "Method"
  28. #define PAYLOAD_RESULT_FIELD "Payload.RetCode"
  29. #define PAYLOAD_STATE_REPORTED_FIELD "Payload.State.Reported"
  30. #define PAYLOAD_STATE_DESIRED_FIELD "Payload.State.Desired"
  31. /* 设备影子文档中的字段 */
  32. #define STATE_FIELD "State"
  33. #define STATE_REPORTED_FIELD "State.Reported"
  34. #define STATE_DESIRED_FIELD "State.Desired"
  35. #define METADATA_FIELD "Metadata"
  36. #define METADATA_REPORTED_FIELD "Metadata.Reported"
  37. #define METADATA_DESIRED_FIELD "Metadata.Desired"
  38. #define VERSION_FIELD "Version"
  39. #define TIMESTAMP_FIELD "Timestamp"
  40. /* 消息类型字段 */
  41. #define METHOD_CONTROL "control"
  42. #define METHOD_GET "get"
  43. #define METHOD_UPDATE "update"
  44. #define METHOD_DELETE "delete"
  45. #define METHOD_REPLY "reply"
  46. #define METHOD_GET_REPLY "get_reply"
  47. /**
  48. * @brief 检查函数snprintf的返回值
  49. *
  50. * @param returnCode 函数snprintf的返回值
  51. * @param maxSizeOfWrite 可写最大字节数
  52. * @return 返回ERR_JSON, 表示出错; 返回ERR_JSON_BUFFER_TRUNCATED, 表示截断
  53. */
  54. int _check_snprintf_return(int32_t returnCode, size_t maxSizeOfWrite);
  55. /**
  56. * 将一个JSON节点写入到JSON串中
  57. *
  58. * @param jsonBuffer JSON串
  59. * @param sizeOfBuffer 可写入大小
  60. * @param pKey JSON节点的key
  61. * @param pData JSON节点的value
  62. * @param type JSON节点value的数据类型
  63. * @return 返回SUCCESS, 表示成功
  64. */
  65. int put_json_node(char *jsonBuffer, size_t sizeOfBuffer, const char *pKey, void *pData, JsonDataType type);
  66. /**
  67. * @brief 从JSON文档中解析出report字段
  68. *
  69. * @param pJsonDoc 待解析的JSON文档
  70. * @param pType 输出tyde字段
  71. * @return 返回true, 表示解析成功
  72. */
  73. bool parse_shadow_payload_state_reported_state(char *pJsonDoc, char **pState);
  74. /**
  75. * @brief 从JSON文档中解析出desired字段
  76. *
  77. * @param pJsonDoc 待解析的JSON文档
  78. * @param pType 输出tyde字段
  79. * @return 返回true, 表示解析成功
  80. */
  81. bool parse_shadow_payload_state_desired_state(char *pJsonDoc, char **pState);
  82. /**
  83. * @brief 从JSON文档中解析出type字段
  84. *
  85. * @param pJsonDoc 待解析的JSON文档
  86. * @param pType 输出tyde字段
  87. * @return 返回true, 表示解析成功
  88. */
  89. bool parse_shadow_method_type(char *pJsonDoc, char **pType);
  90. /**
  91. * @brief 从JSON文档中解析出recode字段
  92. *
  93. * @param pJsonDoc 待解析的JSON文档
  94. * @param pType 输出tyde字段
  95. * @return 返回true, 表示解析成功
  96. */
  97. bool parse_shadow_payload_retcode_type(char *pJsonDoc, uint32_t *pRetCode);
  98. /**
  99. * @brief 从JSON文档中解析出version字段
  100. *
  101. * @param pJsonDoc 待解析的JSON文档
  102. * @param pType 输出tyde字段
  103. * @return 返回true, 表示解析成功
  104. */
  105. bool parse_version_num(char *pJsonDoc, uint32_t *pVersionNumber);
  106. /**
  107. * @brief 从JSON文档中解析出reported字段
  108. *
  109. * @param pJsonDoc 待解析的JSON文档
  110. * @param pType 输出tyde字段
  111. * @return 返回true, 表示解析成功
  112. */
  113. bool parse_shadow_state_reported_type(char *pJsonDoc, char **pType);
  114. /**
  115. * @brief 从JSON文档中解析出desired字段
  116. *
  117. * @param pJsonDoc 待解析的JSON文档
  118. * @param pType 输出tyde字段
  119. * @return 返回true, 表示解析成功
  120. */
  121. bool parse_shadow_state_desired_type(char *pJsonDoc, char **pType);
  122. /**
  123. * @brief 从JSON文档中解析出state字段
  124. *
  125. * @param pJsonDoc 待解析的JSON文档
  126. * @param pType 输出tyde字段
  127. * @return 返回true, 表示解析成功
  128. */
  129. bool parse_shadow_state_type(char *pJsonDoc, char **pType);
  130. /**
  131. * @brief 为GET和DELETE请求构造一个只带有clientToken字段的JSON文档
  132. *
  133. * @param tokenNumber shadow的token值,函数内部每次执行完会自增
  134. * @param pJsonBuffer 存储JSON文档的字符串缓冲区
  135. */
  136. void build_empty_json(uint32_t *tokenNumber, char *pJsonBuffer);
  137. /**
  138. * @brief 如果JSON文档中的key与某个设备属性的key匹配的话, 则更新该设备属性, 该设备属性的值不能为OBJECT类型
  139. *
  140. * @param pJsonDoc JSON文档
  141. * @param pProperty 设备属性
  142. * @return 返回true, 表示成功
  143. */
  144. char *find_value_if_key_match(char *pJsonDoc, DeviceProperty *pProperty);
  145. #ifdef __cplusplus
  146. }
  147. #endif
  148. #endif //IOT_SHADOW_CLIENT_JSON_H_