sdk_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 "sdk_protocol.h"
26 
27 #include <stdio.h>
28 #include <string.h>
29 
30 namespace livox_ros {
31 const uint8_t kSdkProtocolSof = 0xAA;
32 const uint32_t kSdkPacketCrcSize = 4; // crc32
34 
36  : crc16_(seed16), crc32_(seed32) {}
37 
38 int32_t SdkProtocol::Pack(uint8_t *o_buf, uint32_t o_buf_size, uint32_t *o_len,
39  const CommPacket &i_packet) {
40  SdkPacket *sdk_packet = (SdkPacket *)o_buf;
41 
42  if (kLidarSdk != i_packet.protocol) {
43  return -1;
44  }
45 
46  sdk_packet->sof = kSdkProtocolSof;
47  sdk_packet->length = i_packet.data_len + GetPacketWrapperLen();
48 
49  if (sdk_packet->length > o_buf_size) {
50  return -1;
51  }
52 
53  sdk_packet->version = kSdkVer0;
54  sdk_packet->packet_type = i_packet.packet_type;
55  sdk_packet->seq_num = i_packet.seq_num & 0xFFFF;
56  sdk_packet->preamble_crc =
57  crc16_.mcrf4xx_calc(o_buf, GetPreambleLen() - kSdkPacketPreambleCrcSize);
58  sdk_packet->cmd_set = i_packet.cmd_set;
59  sdk_packet->cmd_id = i_packet.cmd_code;
60 
61  memcpy(sdk_packet->data, i_packet.data, i_packet.data_len);
62 
63  uint32_t crc =
64  crc32_.crc32_calc(o_buf, sdk_packet->length - kSdkPacketCrcSize);
65  o_buf[sdk_packet->length - 4] = crc & 0xFF;
66  o_buf[sdk_packet->length - 3] = (crc >> 8) & 0xFF;
67  o_buf[sdk_packet->length - 2] = (crc >> 16) & 0xFF;
68  o_buf[sdk_packet->length - 1] = (crc >> 24) & 0xFF;
69 
70  *o_len = sdk_packet->length;
71 
72  return 0;
73 }
74 
76  CommPacket *o_packet) {
77  SdkPacket *sdk_packet = (SdkPacket *)i_buf;
78 
79  if (i_len < GetPacketWrapperLen()) {
80  return -1; // packet lenth error
81  }
82 
83  memset((void *)o_packet, 0, sizeof(CommPacket));
84 
85  o_packet->packet_type = sdk_packet->packet_type;
86  o_packet->protocol = kLidarSdk;
87  o_packet->protocol_version = sdk_packet->version;
88 
89  o_packet->seq_num = sdk_packet->seq_num;
90  o_packet->cmd_set = sdk_packet->cmd_set;
91  o_packet->cmd_code = sdk_packet->cmd_id;
92  o_packet->data_len = sdk_packet->length - GetPacketWrapperLen();
93  o_packet->data = sdk_packet->data;
94 
95  return 0;
96 }
97 
99 
101  return sizeof(SdkPacket) - 1 + kSdkPacketCrcSize;
102 }
103 
105  SdkPreamble *preamble = (SdkPreamble *)buf;
106  return preamble->length;
107 }
108 
110  SdkPreamble *preamble = (SdkPreamble *)buf;
111 
112  if ((preamble->sof == kSdkProtocolSof) &&
113  (crc16_.mcrf4xx_calc(buf, GetPreambleLen()) == 0)) {
114  return 0;
115  } else {
116  return -1;
117  }
118 }
119 
121  SdkPacket *sdk_packet = (SdkPacket *)buf;
122 
123  uint32_t crc = ((uint32_t)(buf[sdk_packet->length - 4])) |
124  (((uint32_t)(buf[sdk_packet->length - 3])) << 8) |
125  (((uint32_t)(buf[sdk_packet->length - 2])) << 16) |
126  (((uint32_t)(buf[sdk_packet->length - 1])) << 24);
127 
128  if (crc32_.crc32_calc(buf, sdk_packet->length - kSdkPacketCrcSize) == crc) {
129  return 0;
130  } else {
131  return -1;
132  }
133 }
134 } // namespace livox_ros
SdkProtocol(uint16_t seed16, uint32_t seed32)
int32_t ParsePacket(const uint8_t *i_buf, uint32_t i_len, CommPacket *o_packet) override
uint32_t crc32_calc(const uint8_t *data, uint16_t len)
Definition: FastCRCsw.cpp:111
unsigned short uint16_t
Definition: stdint.h:126
const uint32_t kSdkPacketPreambleCrcSize
unsigned char uint8_t
Definition: stdint.h:125
uint16_t mcrf4xx_calc(const uint8_t *data, const uint16_t datalen)
Definition: FastCRCsw.cpp:57
uint32_t GetPacketWrapperLen() override
int32_t Pack(uint8_t *o_buf, uint32_t o_buf_size, uint32_t *o_len, const CommPacket &i_packet) override
const uint8_t kSdkProtocolSof
uint8_t protocol_version
Definition: protocol.h:52
unsigned int uint32_t
Definition: stdint.h:127
int32_t CheckPreamble(const uint8_t *buf) override
uint32_t GetPacketLen(const uint8_t *buf) override
const uint32_t kSdkPacketCrcSize
int32_t CheckPacket(const uint8_t *buf) override
uint32_t GetPreambleLen() override
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