protocol.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // The MIT License (MIT)
  3. //
  4. // Copyright (c) 2019 Livox. All rights reserved.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. // SOFTWARE.
  23. //
  24. #ifndef COMM_PROTOCOL_H_
  25. #define COMM_PROTOCOL_H_
  26. #include <stdint.h>
  27. namespace livox_ros {
  28. typedef struct CommPacket CommPacket;
  29. typedef int (*RequestPackCb)(CommPacket *packet);
  30. typedef enum { kRequestPack, kAckPack, kMsgPack } PacketType;
  31. typedef enum { kLidarSdk, kRsvd1, kGps, kProtocolUndef } ProtocolType;
  32. typedef enum { kNoNeed, kNeedAck, kDelayAck } NeedAckType;
  33. typedef enum { kParseSuccess, kParseFail, kParseNeedMoreData } ParseResult;
  34. typedef enum {
  35. kFindLengthSuccess,
  36. kFindLengthContinue,
  37. kFindLengthError
  38. } FindLengthResult;
  39. typedef struct CommPacket {
  40. uint8_t packet_type;
  41. uint8_t protocol;
  42. uint8_t protocol_version;
  43. uint8_t cmd_set;
  44. uint32_t cmd_code;
  45. uint32_t sender;
  46. uint32_t sub_sender;
  47. uint32_t receiver;
  48. uint32_t sub_receiver;
  49. uint32_t seq_num;
  50. uint8_t *data;
  51. uint16_t data_len;
  52. uint32_t padding;
  53. } CommPacket;
  54. /** SDK Protocol info config */
  55. typedef struct {
  56. uint16_t seed16;
  57. uint16_t seed32;
  58. } SdkProtocolConfig;
  59. /** NAME-0183 Protocol info config for gps */
  60. typedef struct { void *data; } GpsProtocolConfig;
  61. typedef struct {
  62. uint8_t type;
  63. union {
  64. SdkProtocolConfig sdk;
  65. GpsProtocolConfig gps;
  66. } config;
  67. } ProtocolConfig;
  68. class Protocol {
  69. public:
  70. virtual ~Protocol() = default;
  71. virtual int32_t ParsePacket(const uint8_t *i_buf, uint32_t i_len,
  72. CommPacket *o_packet) = 0;
  73. virtual int32_t Pack(uint8_t *o_buf, uint32_t o_buf_size, uint32_t *o_len,
  74. const CommPacket &i_packet) = 0;
  75. virtual uint32_t GetPreambleLen() = 0;
  76. virtual uint32_t GetPacketWrapperLen() = 0;
  77. virtual uint32_t FindPacketLen(const uint8_t *buf, uint32_t buf_length) = 0;
  78. virtual uint32_t GetPacketLen(const uint8_t *buf) = 0;
  79. virtual int32_t CheckPreamble(const uint8_t *buf) = 0;
  80. virtual int32_t CheckPacket(const uint8_t *buf) = 0;
  81. };
  82. } // namespace livox_ros
  83. #endif // COMM_PROTOCOL_H_