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


livox_ros_driver
Author(s): Livox Dev Team
autogenerated on Mon Mar 15 2021 02:40:45