comm_protocol.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #include "comm_protocol.h"
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <iostream>
  28. namespace livox_ros {
  29. CommProtocol::CommProtocol(ProtocolConfig &config) : config_(config) {
  30. ResetParser();
  31. ResetCache();
  32. offset_to_read_index_ = 0;
  33. packet_length_ = 0;
  34. if (kGps == config.type) {
  35. is_length_known = false;
  36. protocol_ = new GpsProtocol();
  37. } else {
  38. is_length_known = true;
  39. protocol_ = nullptr;
  40. }
  41. }
  42. CommProtocol::~CommProtocol() {
  43. if (protocol_) {
  44. delete protocol_;
  45. }
  46. }
  47. uint8_t *CommProtocol::FetchCacheFreeSpace(uint32_t *o_len) {
  48. UpdateCache();
  49. if (cache_.wr_idx < cache_.size) {
  50. *o_len = cache_.size - cache_.wr_idx;
  51. return &cache_.buf[cache_.wr_idx];
  52. } else {
  53. *o_len = 0;
  54. return nullptr;
  55. }
  56. }
  57. int32_t CommProtocol::UpdateCacheWrIdx(uint32_t used_size) {
  58. if ((cache_.wr_idx + used_size) < cache_.size) {
  59. cache_.wr_idx += used_size;
  60. return 0;
  61. } else {
  62. return -1;
  63. }
  64. }
  65. uint32_t CommProtocol::GetCacheTailSize() {
  66. if (cache_.wr_idx < cache_.size) {
  67. return cache_.size - cache_.wr_idx;
  68. } else {
  69. return 0;
  70. }
  71. }
  72. uint32_t CommProtocol::GetValidDataSize() {
  73. if (cache_.wr_idx > cache_.rd_idx) {
  74. return cache_.wr_idx - cache_.rd_idx;
  75. } else {
  76. return 0;
  77. }
  78. }
  79. void CommProtocol::UpdateCache(void) {
  80. if (GetCacheTailSize() <
  81. kMoveCacheLimit) { /* move unused data to cache head */
  82. uint32_t valid_data_size = GetValidDataSize();
  83. if (valid_data_size) {
  84. memmove(cache_.buf, &cache_.buf[cache_.rd_idx], valid_data_size);
  85. cache_.rd_idx = 0;
  86. cache_.wr_idx = valid_data_size;
  87. } else if (cache_.rd_idx) {
  88. cache_.rd_idx = 0;
  89. cache_.wr_idx = 0;
  90. }
  91. }
  92. }
  93. int32_t CommProtocol::Pack(uint8_t *o_buf, uint32_t o_buf_size, uint32_t *o_len,
  94. const CommPacket &i_packet) {
  95. return protocol_->Pack(o_buf, o_buf_size, o_len, i_packet);
  96. }
  97. void CommProtocol::ResetParser() { fsm_parse_step_ = kSearchPacketPreamble; }
  98. int32_t CommProtocol::ParseCommStream(CommPacket *o_pack) {
  99. int32_t ret = kParseFail;
  100. while ((GetValidDataSize() > protocol_->GetPreambleLen()) &&
  101. (GetValidDataSize() > offset_to_read_index_)) {
  102. switch (fsm_parse_step_) {
  103. case kSearchPacketPreamble: {
  104. FsmSearchPacketPreamble();
  105. break;
  106. }
  107. case kFindPacketLength: {
  108. FsmFindPacketLength();
  109. break;
  110. }
  111. case kGetPacketData: {
  112. ret = FsmGetPacketData(o_pack);
  113. break;
  114. }
  115. default: { FsmParserStateTransfer(kSearchPacketPreamble); }
  116. }
  117. /* Must exit when in the below case */
  118. if ((ret == kParseSuccess) || (ret == kParseNeedMoreData)) break;
  119. }
  120. return ret;
  121. }
  122. uint16_t CommProtocol::GetAndUpdateSeqNum() {
  123. /* add lock here for thread safe */
  124. uint16_t seq = seq_num_;
  125. seq_num_++;
  126. return seq;
  127. }
  128. int32_t CommProtocol::FsmSearchPacketPreamble() {
  129. do {
  130. if (!protocol_->CheckPreamble(GetCacheReadPos())) {
  131. if (!is_length_known) {
  132. offset_to_read_index_ = 0;
  133. packet_length_ = 0;
  134. FsmParserStateTransfer(kFindPacketLength);
  135. break;
  136. } else {
  137. packet_length_ = protocol_->GetPacketLen(GetCacheReadPos());
  138. if ((packet_length_ < cache_.size) &&
  139. (packet_length_ >
  140. protocol_
  141. ->GetPacketWrapperLen())) { /* check the legality of length */
  142. FsmParserStateTransfer(kGetPacketData);
  143. break;
  144. }
  145. }
  146. }
  147. // printf("|%2x", cache_.buf[cache_.rd_idx]);
  148. ++cache_.rd_idx; /* skip one byte */
  149. } while (0);
  150. return 0;
  151. }
  152. int32_t CommProtocol::FsmFindPacketLength() {
  153. uint32_t valid_data_size = GetValidDataSize();
  154. uint32_t ret = protocol_->FindPacketLen(GetCacheReadPos(), valid_data_size);
  155. if (ret == kFindLengthSuccess) {
  156. packet_length_ = protocol_->GetPacketLen(GetCacheReadPos());
  157. FsmParserStateTransfer(kGetPacketData);
  158. offset_to_read_index_ = 0;
  159. } else if (ret == kFindLengthContinue) { /* continue to find next time */
  160. offset_to_read_index_ = valid_data_size;
  161. } else { /* find length error */
  162. offset_to_read_index_ = 0;
  163. cache_.rd_idx += valid_data_size;
  164. FsmParserStateTransfer(kSearchPacketPreamble);
  165. printf("Continue to find length error\n");
  166. }
  167. return 0;
  168. }
  169. int32_t CommProtocol::FsmGetPacketData(CommPacket *o_pack) {
  170. int32_t ret = kParseFail;
  171. do {
  172. if (GetValidDataSize() < packet_length_) {
  173. ret = kParseNeedMoreData;
  174. break;
  175. }
  176. if (protocol_->CheckPacket(GetCacheReadPos())) {
  177. cache_.rd_idx += protocol_->GetPreambleLen();
  178. FsmParserStateTransfer(kSearchPacketPreamble);
  179. printf("Check packet error\n");
  180. break;
  181. }
  182. if (protocol_->ParsePacket(GetCacheReadPos(), packet_length_, o_pack)) {
  183. cache_.rd_idx += protocol_->GetPacketLen(GetCacheReadPos());
  184. FsmParserStateTransfer(kSearchPacketPreamble);
  185. printf("Parse packet error\n");
  186. break;
  187. }
  188. cache_.rd_idx += protocol_->GetPacketLen(GetCacheReadPos());
  189. FsmParserStateTransfer(kSearchPacketPreamble);
  190. return kParseSuccess;
  191. } while (0);
  192. return ret;
  193. }
  194. } // namespace livox_ros