sub_device_protocol.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*******************************************************
  2. * File name:sub_device_protocol.h
  3. * Author: LIJIAN
  4. * Versions: 0.1
  5. * Description: APIs for sub device.
  6. * History:
  7. * 1.Date:
  8. * Author:
  9. * Modification:
  10. *********************************************************/
  11. #ifndef SUB_DEVICE_PROTOCOL_TOOL_H
  12. #define SUB_DEVICE_PROTOCOL_TOOL_H
  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif
  17. #include "common_functions.h"
  18. #include "pando_endian.h"
  19. #define MAGIC_HEAD_SUB_DEVICE 0x34
  20. #define PAYLOAD_TYPE_COMMAND 1
  21. #define PAYLOAD_TYPE_EVENT 2
  22. #define PAYLOAD_TYPE_DATA 3
  23. #define TLV_TYPE_FLOAT64 1
  24. #define TLV_TYPE_FLOAT32 2
  25. #define TLV_TYPE_INT8 3
  26. #define TLV_TYPE_INT16 4
  27. #define TLV_TYPE_INT32 5
  28. #define TLV_TYPE_INT64 6
  29. #define TLV_TYPE_UINT8 7
  30. #define TLV_TYPE_UINT16 8
  31. #define TLV_TYPE_UINT32 9
  32. #define TLV_TYPE_UINT64 10
  33. #define TLV_TYPE_BYTES 11
  34. #define TLV_TYPE_URI 12
  35. #define TLV_TYPE_BOOL 13
  36. #define DEV_HEADER_LEN (sizeof(struct device_header))
  37. #pragma pack(1)
  38. /* a device packet is made up with header and payload */
  39. struct device_header
  40. {
  41. uint8_t magic; /* magic number (0x34) */
  42. uint8_t crc; /* crc of whole packet with crc is zero */
  43. uint16_t payload_type; /* type of packet, event, command, data */
  44. uint16_t payload_len; /* length of packet without header */
  45. uint16_t flags; /* flags for extending protocol */
  46. uint32_t frame_seq; /* frame sequence between device and gateway */
  47. };
  48. /* TLV contains the real information of payload.
  49. There are 13 types, such as int8, unint16, length is sizeof the type.
  50. If type such as int32 can present length of itself, TLV is made up of type
  51. and value.
  52. */
  53. struct TLV
  54. {
  55. uint16_t type;
  56. uint16_t length;
  57. uint8_t value[];
  58. };
  59. /* Count presents packet how many tlv informations in a packet */
  60. struct TLVs
  61. {
  62. uint16_t count;
  63. //struct TLV tlv[];
  64. };
  65. /* Payload of command */
  66. struct pando_command
  67. {
  68. uint16_t sub_device_id; /* ID of device */
  69. uint16_t command_num; /* different command number presents different operation */
  70. uint16_t priority; /* device should handle high priority command first */
  71. struct TLVs params[1]; /* params for command operation */
  72. };
  73. /* Payload of event */
  74. struct pando_event
  75. {
  76. uint16_t sub_device_id; /* ID of device */
  77. uint16_t event_num; /* Different event number presents different type event */
  78. uint16_t priority; /* Device should report high priority event first */
  79. struct TLVs params[1]; /* Params for event to report */
  80. };
  81. /* Payload of data */
  82. struct pando_property
  83. {
  84. uint16_t sub_device_id; /* ID of device */
  85. uint16_t property_num; /* Different property number presents different type data property */
  86. struct TLVs params[1]; /* Params for data to report */
  87. };
  88. struct sub_device_buffer
  89. {
  90. uint16_t buffer_length;
  91. uint8_t *buffer;
  92. };
  93. struct sub_device_base_params
  94. {
  95. uint32_t event_sequence;
  96. uint32_t data_sequence;
  97. uint32_t command_sequence;
  98. };
  99. #pragma pack()
  100. /*******************************************************
  101. * Description: Initialize sequence number to 0, it's unnecessary now.
  102. * param
  103. base_params: sequence of data, event, command.
  104. * return: 0 if success, -1 if failed.
  105. *********************************************************/
  106. int init_sub_device(struct sub_device_base_params base_params);
  107. /*******************************************************
  108. * Description: Create a block buffer before adding tlv params.
  109. It's necessary to delete the buffer when complete creating package.
  110. * param
  111. * return: Buffer of params block.
  112. *********************************************************/
  113. struct TLVs *create_params_block(void);
  114. /*******************************************************
  115. * Description: Create package buffer, need add params block to finish package.
  116. * param
  117. flags: Extend for new features.
  118. * return: Buffer of package.
  119. *********************************************************/
  120. struct sub_device_buffer *create_command_package(uint16_t flags);
  121. /*******************************************************
  122. * Description: Create package buffer, need add params block to finish package.
  123. * param
  124. flags: Extend for new features.
  125. * return: Buffer of package.
  126. *********************************************************/
  127. struct sub_device_buffer *create_event_package(uint16_t flags);
  128. /*******************************************************
  129. * Description: Create package buffer, need add params block to finish package.
  130. * param
  131. flags: Extend for new features.
  132. * return: Buffer of package.
  133. *********************************************************/
  134. struct sub_device_buffer *create_data_package(uint16_t flags);
  135. /*******************************************************
  136. * Description: Calculate payload length and crc to finish package construct.
  137. * param
  138. package_buf: buffer contains device package.
  139. * return:
  140. *********************************************************/
  141. int finish_package(struct sub_device_buffer *package_buf);
  142. /*******************************************************
  143. * Description: Add data params block and property number to package.
  144. * param
  145. data_package: buffer contains device package.
  146. property_num: property number, indicate data type.
  147. next_data_params: all data params, count means how many params.
  148. * return: 0 if success, -1 if failed.
  149. *********************************************************/
  150. int add_next_property(struct sub_device_buffer *data_package, uint16_t property_num,
  151. struct TLVs *next_data_params);
  152. /*******************************************************
  153. * Description: Add command params block, priority and command number to package.
  154. * param
  155. command_package: buffer contains device package.
  156. command_num: command number, indicate command type.
  157. priority: high priority command should be processed first.
  158. command_params: all command params, count means how many params.
  159. * return: 0 if success, -1 if failed.
  160. *********************************************************/
  161. int add_command(struct sub_device_buffer *command_package, uint16_t command_num,
  162. uint16_t priority, struct TLVs *command_params);
  163. /*******************************************************
  164. * Description: Add event params block, priority and event number to package.
  165. * param
  166. out:
  167. event_package: buffer contains device package.
  168. in:
  169. event_num: event number, indicate event type.
  170. priority: high priority event should be uploaded to server first.
  171. event_params: all event params, count means how many params.
  172. * return: 0 if success, -1 if failed.
  173. *********************************************************/
  174. int add_event(struct sub_device_buffer *event_package, uint16_t event_num,
  175. uint16_t priority, struct TLVs *event_params);
  176. /*******************************************************
  177. * Description: Get command from device buffer directly.
  178. * param
  179. in:
  180. device_buffer: buffer contains device package.
  181. out:
  182. command_body: include command number, priority and sub device id.
  183. * return: 0 if success, -1 if failed.
  184. *********************************************************/
  185. struct TLVs *get_sub_device_command(struct sub_device_buffer *device_buffer, struct pando_command *command_body);
  186. /*******************************************************
  187. * Description: Get event from device buffer directly.
  188. * param
  189. in:
  190. device_buffer: buffer contains device package.
  191. out:
  192. event_body: include event number, priority and sub device id.
  193. * return: 0 if success, -1 if failed.
  194. *********************************************************/
  195. struct TLVs *get_sub_device_event(struct sub_device_buffer *device_buffer, struct pando_event *event_body);
  196. /*******************************************************
  197. * Description: Get data from device buffer directly, this function should be called
  198. repeatedly to get all data property.
  199. * param
  200. in:
  201. device_buffer: buffer contains device package.
  202. out:
  203. property_body: include data property number and sub device id.
  204. * return: If all properties have been processed, return NULL, else return data params block.
  205. *********************************************************/
  206. struct TLVs *get_sub_device_property(struct sub_device_buffer *device_buffer, struct pando_property *property_body);
  207. /*******************************************************
  208. * Description: Get type of package from device buffer directly.
  209. * param
  210. in:
  211. device_buffer: buffer contains device package.
  212. out:
  213. * return: Type of package.
  214. *********************************************************/
  215. uint16_t get_sub_device_payloadtype(struct sub_device_buffer *package);
  216. /*******************************************************
  217. * Description: Delete device buffer after package has been sent to server.
  218. * param
  219. in:
  220. device_buffer: buffer contains device package.
  221. out:
  222. * return:
  223. *********************************************************/
  224. void delete_device_package(struct sub_device_buffer *device_buffer);
  225. /*******************************************************
  226. * Description:
  227. * param
  228. * return:
  229. *********************************************************/
  230. void delete_params_block(struct TLVs *params_block);
  231. /*******************************************************
  232. * Description: Judge the command has file to process.
  233. * param
  234. in:
  235. device_buffer: buffer contains device package.
  236. * return: 1 if command with file, else 0.
  237. *********************************************************/
  238. int is_device_file_command(struct sub_device_buffer *device_buffer);
  239. /*******************************************************
  240. * Description: Functions to get param from params block, all params must be decode
  241. in order, it's not reentrant.
  242. * param
  243. in:
  244. params: params block.
  245. * return: param to be process.
  246. *********************************************************/
  247. uint8_t get_next_uint8(struct TLVs *params);
  248. uint16_t get_next_uint16(struct TLVs *params);
  249. uint32_t get_next_uint32(struct TLVs *params);
  250. uint64_t get_next_uint64(struct TLVs *params);
  251. int8_t get_next_int8(struct TLVs *params);
  252. int16_t get_next_int16(struct TLVs *params);
  253. int32_t get_next_int32(struct TLVs *params);
  254. int64_t get_next_int64(struct TLVs *params);
  255. float get_next_float32(struct TLVs *params);
  256. double get_next_float64(struct TLVs *params);
  257. uint8_t get_next_bool(struct TLVs *params);
  258. void *get_next_uri(struct TLVs *params, uint16_t *length);
  259. void *get_next_bytes(struct TLVs *params, uint16_t *length);
  260. /*******************************************************
  261. * Description: Functions to add param into params block.
  262. * param
  263. in:
  264. next_value: value to be added.
  265. out:
  266. params_block:
  267. * return: 0 if success, -1 if failed.
  268. *********************************************************/
  269. int add_next_param(struct TLVs *params_block, uint16_t next_type, uint16_t next_length, void *next_value);
  270. int add_next_uint8(struct TLVs *params, uint8_t next_value);
  271. int add_next_uint16(struct TLVs *params, uint16_t next_value);
  272. int add_next_uint32(struct TLVs *params, uint32_t next_value);
  273. int add_next_uint64(struct TLVs *params, uint64_t next_value);
  274. int add_next_int8(struct TLVs *params, int8_t next_value);
  275. int add_next_int16(struct TLVs *params, int16_t next_value);
  276. int add_next_int32(struct TLVs *params, int32_t next_value);
  277. int add_next_int64(struct TLVs *params, int64_t next_value);
  278. int add_next_float32(struct TLVs *params, float next_value);
  279. int add_next_float64(struct TLVs *params, double next_value);
  280. int add_next_bool(struct TLVs *params, uint8_t next_value);
  281. int add_next_uri(struct TLVs *params, uint16_t length, void *next_value);
  282. int add_next_bytes(struct TLVs *params, uint16_t length, void *next_value);
  283. #ifdef __cplusplus
  284. }
  285. #endif
  286. #endif