gps_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 "gps_protocol.h"
26 
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <string.h>
30 
31 namespace livox_ros {
32 
37 const uint32_t kWrapperLen = 4;
40 
41 int32_t GpsProtocol::Pack(uint8_t *o_buf, uint32_t o_buf_size, uint32_t *o_len,
42  const CommPacket &i_packet) {
43  // GpsPacket* gps_packet = (GpsPacket*)o_buf;
44  return 0;
45 }
46 
48  CommPacket *o_packet) {
49  // GpsPacket *gps_packet = (GpsPacket *)i_buf;
50 
51  if (i_len < GetPacketWrapperLen()) {
52  return -1; // packet length error
53  }
54  memset((void *)o_packet, 0, sizeof(CommPacket));
55  o_packet->protocol = kGps;
56  o_packet->data = (uint8_t *)i_buf;
57  o_packet->data_len = i_len;
58 
59  return 0;
60 }
61 
63 
65  return kWrapperLen;
66 }
67 
69  uint32_t i = 0;
70  for (; (i < buf_length) && (i < kPacketLengthLmit); i++) {
71  if ((buf[i] == kGpsProtocolEof) && (buf[0] == kGpsProtocolSof)) {
72  found_length_ = i + 1 + 2; /* num = index + 1 + two bytes checksum */
73  return kFindLengthSuccess;
74  }
75  }
76  if (i < kPacketLengthLmit) {
77  return kFindLengthContinue;
78  } else {
79  return kFindLengthError;
80  }
81 }
82 
84 
86  GpsPreamble *preamble = (GpsPreamble *)buf;
87 
88  if (preamble->sof == kGpsProtocolSof) {
89  return 0;
90  } else {
91  return -1;
92  }
93 }
94 
96  uint8_t checksum =
97  CalcGpsPacketChecksum(&buf[1], found_length_ - kWrapperLen);
98  uint8_t raw_checksum = AscciiToHex(&buf[found_length_ - 2]);
99  if (checksum == raw_checksum) {
100  return 0;
101  } else {
102  return -1;
103  }
104 }
105 
107  uint32_t length) {
108  uint8_t result = buf[0];
109  for (uint32_t i = 1; i < length; i++) {
110  result ^= buf[i];
111  }
112  return result;
113 }
114 
115 uint8_t AscciiToHex(const uint8_t *TwoChar) {
116  uint8_t h = toupper(TwoChar[0]) - 0x30;
117  if (h > 9) h -= 7;
118 
119  uint8_t l = toupper(TwoChar[1]) - 0x30;
120  if (l > 9) l -= 7;
121 
122  return h * 16 + l;
123 }
124 
125 } // namespace livox_ros
uint8_t CalcGpsPacketChecksum(const uint8_t *buf, uint32_t length)
uint32_t GetPacketWrapperLen() override
const uint32_t kPreambleLen
int32_t Pack(uint8_t *o_buf, uint32_t o_buf_size, uint32_t *o_len, const CommPacket &i_packet) override
const uint8_t kGpsProtocolSof
unsigned char uint8_t
Definition: stdint.h:125
int32_t CheckPreamble(const uint8_t *buf) override
const uint32_t kWrapperLen
uint32_t GetPreambleLen() override
int32_t CheckPacket(const uint8_t *buf) override
uint32_t FindPacketLen(const uint8_t *buf, uint32_t buf_length) override
uint32_t GetPacketLen(const uint8_t *buf) override
unsigned int uint32_t
Definition: stdint.h:127
int32_t ParsePacket(const uint8_t *i_buf, uint32_t i_len, CommPacket *o_packet) override
const uint32_t kPacketLengthLmit
uint8_t AscciiToHex(const uint8_t *TwoChar)
const uint8_t kGpsProtocolEof
signed int int32_t
Definition: stdint.h:124


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