2
0

upacker.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #ifndef _DRV_PACKER_H_
  2. #define _DRV_PACKER_H_
  3. #include "stdint.h"
  4. #include <functional>
  5. using namespace std;
  6. #define USE_DYNAMIC_MEM 0 //使用动态内存
  7. #if USE_DYNAMIC_MEM
  8. #define UP_MALLOC
  9. #define UP_FREE
  10. #endif
  11. #define MAX_PACK_SIZE 16384 //最长消息长度,最大可用14位即16384
  12. #define STX_L 0X55 //数据包头
  13. typedef std::function<void(uint8_t *, uint16_t)> PACKER_CB;
  14. class Upacker
  15. {
  16. public:
  17. #if !USE_DYNAMIC_MEM
  18. uint8_t data[MAX_PACK_SIZE]; //用来做payload序列化的内存
  19. #else
  20. uint8_t *data; //用来做payload序列化的内存
  21. #endif
  22. uint16_t flen; //frame长度
  23. uint8_t calc; //frame校验计算值
  24. uint8_t check; //frame校验值
  25. uint8_t state; //frame解析状态
  26. uint16_t cnt; //frame数据接收cnt
  27. PACKER_CB cb; //frame数据接收cnt
  28. PACKER_CB send; //数据发送回调
  29. int upacker_init(PACKER_CB handler, PACKER_CB s);
  30. void upacker_pack(uint8_t *buff, uint16_t size);
  31. void upacker_unpack(uint8_t *buff, uint16_t size);
  32. private:
  33. uint8_t frame_decode(uint8_t d);
  34. uint8_t frame_encode(uint8_t *data, uint16_t size);
  35. } ;
  36. #endif