comm_protocol.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_COMM_PROTOCOL_H_
  25. #define COMM_COMM_PROTOCOL_H_
  26. #include <stdint.h>
  27. #include "gps_protocol.h"
  28. #include "protocol.h"
  29. #include "sdk_protocol.h"
  30. namespace livox_ros {
  31. const uint32_t kCacheSize = 8192;
  32. const uint32_t kMoveCacheLimit = 1536;
  33. enum FsmParseState {
  34. kSearchPacketPreamble = 0,
  35. kFindPacketLength = 1,
  36. kGetPacketData = 2,
  37. kParseStepUndef
  38. };
  39. /** Communication data cache define */
  40. typedef struct {
  41. uint8_t buf[kCacheSize];
  42. uint32_t rd_idx;
  43. uint32_t wr_idx;
  44. uint32_t size;
  45. } CommCache;
  46. class CommProtocol {
  47. public:
  48. CommProtocol(ProtocolConfig &config);
  49. ~CommProtocol();
  50. int32_t Pack(uint8_t *o_buf, uint32_t o_buf_size, uint32_t *o_len,
  51. const CommPacket &i_packet);
  52. int32_t ParseCommStream(CommPacket *o_pack);
  53. uint8_t *FetchCacheFreeSpace(uint32_t *o_len);
  54. int32_t UpdateCacheWrIdx(uint32_t used_size);
  55. uint16_t GetAndUpdateSeqNum();
  56. void ResetParser();
  57. private:
  58. uint32_t GetCacheTailSize();
  59. uint32_t GetValidDataSize();
  60. void UpdateCache(void);
  61. uint8_t *GetCacheReadPos() { return &cache_.buf[cache_.rd_idx]; }
  62. void ResetCache() {
  63. cache_.wr_idx = 0;
  64. cache_.rd_idx = 0;
  65. cache_.size = kCacheSize;
  66. }
  67. ProtocolConfig config_;
  68. Protocol *protocol_;
  69. CommCache cache_;
  70. uint16_t seq_num_;
  71. bool is_length_known;
  72. bool IsLengthKnown() { return is_length_known; }
  73. volatile uint32_t offset_to_read_index_;
  74. uint32_t packet_length_;
  75. volatile uint32_t fsm_parse_step_;
  76. int32_t FsmSearchPacketPreamble();
  77. int32_t FsmFindPacketLength();
  78. int32_t FsmGetPacketData(CommPacket *o_pack);
  79. void FsmParserStateTransfer(uint32_t new_state) {
  80. if (new_state < kParseStepUndef) {
  81. fsm_parse_step_ = new_state;
  82. } else {
  83. fsm_parse_step_ = kSearchPacketPreamble;
  84. }
  85. }
  86. };
  87. } // namespace livox_ros
  88. #endif // COMM_COMM_PROTOCOL_H_