pando_protocol.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*******************************************************
  2. * File name: pando_protocol.h
  3. * Author: LIJIAN
  4. * Versions: 0.1
  5. * Description: APIs for access device layer to manipulate packets.
  6. * History:
  7. * 1.Date:
  8. * Author:
  9. * Modification:
  10. *********************************************************/
  11. #ifndef PANDP_PROTOCOL_TOOL_H
  12. #define PANDP_PROTOCOL_TOOL_H
  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif
  17. #include "common_functions.h"
  18. #include "sub_device_protocol.h"
  19. #include "pando_endian.h"
  20. #define MAX_PANDO_TOKEN_SIZE 16
  21. #define MAGIC_HEAD_PANDO 0x7064
  22. #define BIN_PANDO_TAG 0x0001
  23. #define EMPTY_HEART_BEAT 0xffff
  24. #define GATE_HEADER_LEN (sizeof(struct mqtt_bin_header))
  25. #pragma pack(1)
  26. struct mqtt_bin_header
  27. {
  28. uint8_t flags;
  29. uint64_t timestamp;
  30. uint8_t token[MAX_PANDO_TOKEN_SIZE];
  31. };
  32. /* Payload must begin with sub device id, for access device can modify it directly */
  33. struct pando_payload
  34. {
  35. uint16_t sub_device_id;
  36. };
  37. /* pando_buffer is descriptor of buffer.
  38. * This struct is used in access device, buff_len show the size of buffer, buffer is
  39. * address of memory, offset is the valid start position of packet in the buffer.
  40. * Once you want to visit a packet, you should start from buffer+offset, end with buffer+buf_len.
  41. * This way need less malloc operation.
  42. */
  43. struct pando_buffer
  44. {
  45. uint16_t buff_len; /* size of buffer */
  46. uint16_t offset; /* begin of valid packet */
  47. uint8_t *buffer; /* addr of buffer */
  48. };
  49. /* basic data of access device, some member of this struct is unuseful. */
  50. struct protocol_base
  51. {
  52. uint64_t device_id; /* unique id of access device */
  53. uint64_t event_sequence; /* event sequence of access device */
  54. uint64_t data_sequence; /* data sequence of access device */
  55. uint64_t command_sequence; /* command sequence of access device */
  56. uint32_t sub_device_cmd_seq; /* command sequence between access device and sub device */
  57. uint8_t token[MAX_PANDO_TOKEN_SIZE]; /* token */
  58. };
  59. #pragma pack()
  60. /* Init basic params of access device */
  61. int pando_protocol_init(struct protocol_base init_params);
  62. uint8_t *pando_get_package_begin(struct pando_buffer *buf);
  63. uint16_t pando_get_package_length(struct pando_buffer *buf);
  64. /* malloc a buffer, size equals length, valid package is from offset. offset usually is 0 */
  65. struct pando_buffer* pando_buffer_create(int length, int offset);
  66. /* release memory of buffer and buffer descriptor */
  67. void pando_buffer_delete(struct pando_buffer *pdbuf);
  68. /* encode packet from sub device into accss device, then send to cloud server */
  69. int pando_protocol_encode(struct pando_buffer *pdbuf, uint16_t *payload_type);
  70. /* decode packet from cloud server, then send to sub device */
  71. int pando_protocol_decode(struct pando_buffer *pdbuf, uint16_t payload_type);
  72. /* get sequence of command, this api seems unused now */
  73. uint64_t pando_protocol_get_cmd_sequence(void);
  74. /* get or set sub device id directly */
  75. int pando_protocol_get_sub_device_id(struct pando_buffer *buf, uint16_t *sub_device_id);
  76. int pando_protocol_set_sub_device_id(struct pando_buffer *buf, uint16_t sub_device_id);
  77. /* get command type after gateway completes decoding the command from server. */
  78. uint16_t pando_protocol_get_payload_type(struct pando_buffer *pdbuf);
  79. #ifdef __cplusplus
  80. }
  81. #endif
  82. #endif //PANDP_PROTOCOL_TOOL_H