Initial commit: Livox SDK2 source (excluding build artifacts)

This commit is contained in:
gardentech
2026-08-26 01:38:14 +09:00
commit 34a9a52cd4
221 changed files with 54102 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2022 Livox. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#include "comm/comm_port.h"
#include <stdio.h>
#include <string.h>
#include <iostream>
#include "sdk_protocol.h"
namespace livox {
namespace lidar {
CommPort::CommPort() {
protocol_ = new SdkProtocol();
}
CommPort::~CommPort() {
if (protocol_) {
delete protocol_;
}
}
int32_t CommPort::Pack(uint8_t *o_buf, uint32_t o_buf_size, uint32_t *o_len, const CommPacket &i_packet) {
return protocol_->Pack(o_buf, o_buf_size, o_len, i_packet);
}
bool CommPort::ParseCommStream(uint8_t *buf, uint32_t buf_size, CommPacket *o_pack) {
if (!(protocol_->CheckPreamble(buf, buf_size))) {
printf("Comm Port Check Preamble error.\n");
return false;
}
return protocol_->ParsePacket(buf, buf_size, o_pack);
}
} // namespace lidar
} // namespace livox
+50
View File
@@ -0,0 +1,50 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2022 Livox. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#ifndef COMM_COMM_PORT_H_
#define COMM_COMM_PORT_H_
#include <stdint.h>
#include "protocol.h"
namespace livox {
namespace lidar {
const uint32_t kCacheSize = 8192;
class CommPort {
public:
CommPort();
~CommPort();
int32_t Pack(uint8_t *o_buf, uint32_t o_buf_size, uint32_t *o_len, const CommPacket &i_packet);
bool ParseCommStream(uint8_t *o_buf, uint32_t buf_size, CommPacket *o_pack);
private:
Protocol *protocol_;
};
} // namespace lidar
} // namespace livox
#endif // COMM_COMM_PORT_H_
+382
View File
@@ -0,0 +1,382 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2022 Livox. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#ifndef LIVOX_DEFINE_H_
#define LIVOX_DEFINE_H_
#include <stdio.h>
#include <string>
#include <memory>
#include <functional>
#include <vector>
#include <atomic>
#include "livox_lidar_def.h"
namespace livox {
namespace lidar {
#pragma pack(1)
const uint16_t KDefaultTimeOut = 1000;
static const uint32_t kMaxCommandBufferSize = 1400;
typedef struct {
std::string lidar_ipaddr;
std::string lidar_subnet_mask;
std::string lidar_gateway;
uint16_t cmd_data_port;
uint16_t push_msg_port;
uint16_t point_data_port;
uint16_t imu_data_port;
uint16_t log_data_port;
} LivoxLidarNetInfo;
typedef struct {
std::string host_ip;
std::string multicast_ip;
uint16_t cmd_data_port;
uint16_t push_msg_port;
uint16_t point_data_port;
uint16_t imu_data_port;
uint16_t log_data_port;
} HostNetInfo;
typedef struct {
std::vector<uint16_t> cmd_key_set;
} GeneralCfgInfo;
typedef struct {
uint8_t device_type;
LivoxLidarNetInfo lidar_net_info;
HostNetInfo host_net_info;
GeneralCfgInfo general_cfg_info;
} LivoxLidarCfg;
typedef struct {
bool lidar_log_enable;
uint64_t lidar_log_cache_size;
std::string lidar_log_path;
} LivoxLidarLoggerCfg;
typedef struct {
bool master_sdk;
} LivoxLidarSdkFrameworkCfg;
typedef enum {
/**
* Lidar command set, set the working mode and sub working mode of a LiDAR.
*/
kCommandIDLidarSearch = 0x0000,
// kCommandIDLidarPreconfig = 0x01,
kCommandIDLidarWorkModeControl = 0x0100,
kCommandIDLidarGetInternalInfo = 0x0101,
kCommandIDLidarPushMsg = 0x0102,
kCommandIDLidarRebootDevice = 0x0200,
kCommandIDLidarResetDevice = 0x0201,
kCommandIDLidarSetPPSSync = 0x0202,
kCommandIDLidarPushLog = 0x0300,
kCommandIDLidarCollectionLog = 0x0301,
kCommandIDLidarLogSysTimeSync = 0x0302,
kCommandIDLidarDebugPointCloudControl = 0x0303,
kCommandIDGeneralRequestUpgrade = 0x0400,
kCommandIDGeneralXferFirmware = 0x0401,
kCommandIDGeneralCompleteXferFirmware = 0x0402,
kCommandIDGeneralRequestUpgradeProgress = 0x0403,
kCommandIDGeneralRequestFirmwareInfo = 0xFF,
kCommandIDLidarCommandCount
} LidarCommandID;
typedef enum {
/** command type, which requires response from the receiver. */
kCommandTypeCmd = 0,
/** acknowledge type, which is the response of command type. */
kCommandTypeAck = 1,
} CommandType;
typedef enum {
/** command type, which requires response from the receiver. */
kHostSend = 0,
/** acknowledge type, which is the response of command type. */
kLidarSend = 1,
} SendType;
typedef struct {
uint8_t ret_code;
uint8_t dev_type;
char sn[16];
uint8_t lidar_ip[4];
uint16_t cmd_port;
} DetectionData;
typedef struct {
uint32_t handle;
uint16_t cmd_port;
uint8_t dev_type;
std::atomic<bool> is_get={false};
std::atomic<bool> is_set={false};
} ViewDevice;
typedef struct {
uint32_t handle;
uint8_t dev_type;
std::string host_ip;
uint16_t lidar_cmd_port;
uint16_t lidar_point_port;
uint16_t lidar_imu_data_port;
uint16_t host_point_port;
uint16_t host_imu_data_port;
} ViewLidarIpInfo;
typedef struct {
uint8_t lidar_ipaddr[4];
uint8_t lidar_subnet_mask[4];
uint8_t lidar_gateway[4];
} LivoxLidarIpInfoValue;
typedef struct {
uint8_t host_ip[4];
uint16_t host_port;
uint16_t lidar_port;
} HostIpInfoValue;
static const uint16_t kDetectionPort = 56000;
static const uint16_t kDetectionListenPort = 56001;
static const uint16_t kHostDebugPointCloudPort = 44332;
static const uint16_t kHAPCmdPort = 56000;
static const uint16_t kHAPPushMsgPort = 56000;
static const uint16_t kHAPPointDataPort = 57000;
static const uint16_t kHAPIMUPort = 58000;
static const uint16_t kHAPLogPort = 59000;
static const uint16_t kHAPDebugPointCloudPort = 60000;
/** kLogPort, which is the log port to be banned. */
static const uint16_t kLogPort = 0;
static const uint16_t kHAPLidarCmdPort = 56000;
static const uint16_t kMid360LidarCmdPort = 56100;
static const uint16_t kMid360LidarPushMsgPort = 56200;
static const uint16_t kMid360LidarPointCloudPort = 56300;
static const uint16_t kMid360LidarImuDataPort = 56400;
static const uint16_t kMid360LidarLogPort = 56500;
static const uint16_t kMid360LidarDebugPointCloudPort = 60301;
static const uint16_t kMid360HostCmdPort = 56101;
static const uint16_t kMid360HostPushMsgPort = 56201;
static const uint16_t kMid360HostPointCloudPort = 56301;
static const uint16_t kMid360HostImuDataPort = 56401;
static const uint16_t kMid360HostLogPort = 56501;
static const uint16_t kPaLidarCmdPort = 9347;
static const uint16_t kPaLidarPointCloudPort = 10000;
static const uint16_t kPaLidarFaultPort = 10001;
static const uint16_t kPaLidarLogPort = 1002;
static const uint16_t kPaHostFaultPort = 42867;
static const uint16_t kMid360sLidarCmdPort = 56100;
static const uint16_t kMid360sLidarPushMsgPort = 56200;
static const uint16_t kMid360sLidarPointCloudPort = 56300;
static const uint16_t kMid360sLidarImuDataPort = 56400;
static const uint16_t kMid360sLidarLogPort = 56500;
static const uint16_t kMid360sLidarDebugPointCloudPort = 60301;
static const uint16_t kMid360sHostCmdPort = 56101;
static const uint16_t kMid360sHostPushMsgPort = 56201;
static const uint16_t kMid360sHostPointCloudPort = 56301;
static const uint16_t kMid360sHostImuDataPort = 56401;
static const uint16_t kMid360sHostLogPort = 56501;
typedef enum {
kCmd = 0,
kPush = 1,
kPointCloud = 2,
kImuData = 3,
kLog = 4,
kFault = 5
} HostSocketType;
typedef enum {
kLidarCmdPort = 56100,
kLidarPushCmdPort = 56200,
kLidarPointDataPort = 56300,
kLidarImuDataPort = 56400,
kLidarLogPort = 56500
} LidarPort;
typedef struct {
uint8_t log_type;
uint8_t enable;
} EnableDeviceLoggerRequest;
typedef struct {
std::string sn;
std::uint8_t dev_type;
std::string lidar_ip;
std::uint16_t cmd_port;
} LidarDeviceInfo;
typedef struct {
uint8_t log_type; // 0
uint8_t file_index; // file index
uint8_t file_num;
uint8_t flag;
uint32_t timestamp;
uint16_t rsvd;
uint32_t trans_index;
uint16_t data_length; // log data length
uint8_t data[1]; //data of log
} DeviceLoggerFilePushRequest;
typedef struct {
uint8_t ret_code; // 0
uint8_t log_type; // file index
uint8_t file_index; // 0 for cfg, 1 for log
uint32_t trans_index; //sequence of trans file
} DeviceLoggerFilePushReponse;
enum class Flag : uint8_t {
kNull,
kCreateFile,
kEndFile,
kTransferData
};
using DataCallback = std::function<void(const uint32_t handle, const uint8_t dev_type, LivoxLidarEthernetPacket *data, void *client_data)>;
using LidarInfoCallback = std::function<void(const uint32_t, const uint8_t, const char*, void*)>;
typedef struct {
uint8_t firmware_type; /**< firmware type. */
uint8_t encrypt_type; /**< encrypt type. */
uint32_t firmware_length; /**< the length of firmware. */
uint8_t dev_type; /**< the device type of the firmware. */
} LivoxLidarStartUpgradeRequest;
typedef struct {
uint8_t firmware_type; /**< firmware type. */
uint8_t encrypt_type; /**< encrypt type. */
uint32_t firmware_length; /**< the length of firmware. */
uint8_t dev_type; /**< the device type of the firmware. */
uint32_t firmware_version; /**< the version of this firmware. */
uint64_t firmware_buildtime; /**< the buildtime of this firmware. */
uint8_t hw_whitelist[32]; /**< the hardware version list that this firmware can be used for. */
} LivoxLidarStartUpgradeRequestV3;
typedef struct {
uint8_t ret_code; /**< Return code. */
} LivoxLidarStartUpgradeResponse;
typedef struct {
uint32_t offset; /**< Return code. */
uint32_t length; /**< Working state. */
uint8_t encrypt_type;
uint8_t rsvd[3];
uint8_t data[1]; /**< LiDAR feature. */
} LivoxLidarXferFirmwareResquest;
typedef struct {
uint8_t ret_code; /**< Return code. */
uint32_t offset; /**< Return code. */
uint32_t length; /**< Working state. */
} LivoxLidarXferFirmwareResponse;
typedef struct {
uint8_t checksum_type; /**< Return code. */
uint8_t checksum_length; /**< Working state. */
uint8_t checksum[1]; /**< LiDAR feature. */
} LivoxLidarCompleteXferFirmwareResquest;
typedef struct {
uint8_t ret_code; /**< Return code. */
} LivoxLidarCompleteXferFirmwareResponse;
typedef struct {
uint8_t ret_code; /**< Return code. */
uint8_t progress; /**< progress of upgrade. */
} LivoxLidarGetUpgradeProgressResponse;
typedef struct {
uint8_t ret_code; /**< Return code. */
uint16_t length; /**< The length of firmware info string, include '\0'. */
uint8_t info[1]; /**< Firmware info string, include '\0'. */
} LivoxLidarRequestFirmwareInfoResponse;
typedef struct {
uint8_t file_ver; /**< file format version. */
uint8_t dev_type; /**< the device type of the firmware. */
uint8_t data_type; /**< type of data. */
uint8_t sn[16];
uint8_t rsvd[107];
uint16_t crc16;
} LivoxLidarDebugPointCloudFileHeader;
typedef struct {
uint8_t enable;
uint8_t host_ip_addr[4];
uint16_t host_port;
uint16_t bandwidth;
} LivoxLidarDebugPointCloudRequest;
typedef struct {
enum class SyncTimeType : std::uint8_t {
kRmcSyncTime = 2,
} type;
uint64_t ns;
} LivoxLidarRmcSyncTimeRequest;
typedef void(*LivoxLidarStartUpgradeCallback)(livox_status status, uint32_t handle,
LivoxLidarStartUpgradeResponse* response, void* client_data);
typedef void(*LivoxLidarXferFirmwareCallback)(livox_status status, uint32_t handle,
LivoxLidarXferFirmwareResponse* response, void* client_data);
typedef void(*LivoxLidarCompleteXferFirmwareCallback)(livox_status status, uint32_t handle,
LivoxLidarCompleteXferFirmwareResponse* response, void* client_data);
typedef void(*LivoxLidarGetUpgradeProgressCallback)(livox_status status,
uint32_t handle, LivoxLidarGetUpgradeProgressResponse* response, void* client_data);
typedef void(*LivoxLidarRequestFirmwareInfoCallback)(livox_status status,
uint32_t handle, LivoxLidarRequestFirmwareInfoResponse* response, void* client_data);
#pragma pack()
} // namespace lidar
} // namespace livox
# endif // DEFINE_H_
+47
View File
@@ -0,0 +1,47 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2022 Livox. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#include "generate_seq.h"
#include <cstdint>
namespace livox {
namespace lidar {
uint32_t GenerateSeq::GetSeq() {
static std::atomic<std::uint32_t> seq(1);
uint32_t value = seq.load();
uint32_t desired = 0;
do {
if (value == UINT16_MAX) {
desired = 1;
} else {
desired = value + 1;
}
} while (!seq.compare_exchange_weak(value, desired));
return desired;
}
} // namespace lidar
} // namespace livox
+45
View File
@@ -0,0 +1,45 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2022 Livox. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#ifndef LIVOX_GENERATE_SEQ_H_
#define LIVOX_GENERATE_SEQ_H_
#include <cstdint>
#include <memory>
#include <atomic>
namespace livox {
namespace lidar {
class GenerateSeq {
public:
static uint32_t GetSeq();
};
} // namespace lidar
} // namespace livox
#endif // GENERATE_SEQ_H_
+92
View File
@@ -0,0 +1,92 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2022 Livox. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#ifndef COMM_PROTOCOL_H_
#define COMM_PROTOCOL_H_
#include <stdint.h>
namespace livox {
namespace lidar {
typedef struct CommPacket CommPacket;
typedef int (*RequestPackCb)(CommPacket *packet);
typedef enum { kRequestPack, kAckPack, kMsgPack } PacketType;
typedef enum { kLidarSdk, kRsvd1, kProtocolUndef } ProtocolType;
typedef enum { kNoNeed, kNeedAck, kDelayAck } NeedAckType;
typedef enum { kParseSuccess, kParseFail } ParseResult;
typedef struct LogCommPacket {
uint8_t packet_type;
uint8_t protocol;
uint8_t protocol_version;
uint8_t cmd_set;
uint32_t cmd_code;
uint32_t sender;
uint32_t sub_sender;
uint32_t receiver;
uint32_t sub_receiver;
uint32_t seq_num;
uint8_t *data;
uint16_t data_len;
uint32_t padding;
} LogCommPacket;
typedef struct CommPacket {
uint8_t protocol;
uint8_t version;
uint32_t seq_num;
uint16_t cmd_id;
uint8_t cmd_type;
uint8_t sender_type;
uint8_t* data;
uint16_t data_len;
} CommPacket;
class Protocol {
public:
virtual ~Protocol(){};
virtual bool ParsePacket(uint8_t *i_buf, uint32_t i_len, CommPacket *o_packet) = 0;
virtual int32_t Pack(uint8_t *o_buf, uint32_t o_buf_size, uint32_t *o_len, const CommPacket &i_packet) = 0;
virtual uint32_t GetPreambleLen() = 0;
virtual uint32_t GetPacketWrapperLen() = 0;
virtual uint32_t GetPacketLen(uint8_t *buf) = 0;
virtual bool CheckPreamble(uint8_t *buf, uint32_t buf_size) = 0;
};
} // namespace lidar
} // namespace livox
#endif // COMM_PROTOCOL_H_
+148
View File
@@ -0,0 +1,148 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2022 Livox. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#include "sdk_protocol.h"
#include <stdio.h>
#include <string.h>
namespace livox {
namespace lidar {
const uint8_t kSdkProtocolSof = 0xAA;
const uint8_t kSdkVer = 0;
const uint32_t kSdkPacketCrcSize = 4; // crc32
const uint32_t kSdkPacketPreambleCrcSize = 2; // crc16
SdkProtocol::SdkProtocol() {}
SdkProtocol::~SdkProtocol() {
}
int32_t SdkProtocol::Pack(uint8_t *o_buf, uint32_t o_buf_size, uint32_t *o_len, const CommPacket &i_packet) {
if (kLidarSdk != i_packet.protocol) {
return -1;
}
SdkPacket *sdk_packet = (SdkPacket *)o_buf;
sdk_packet->sof = kSdkProtocolSof;
sdk_packet->version = kSdkVer;
sdk_packet->length = i_packet.data_len + GetPacketWrapperLen();
if (sdk_packet->length > o_buf_size) {
return -1;
}
sdk_packet->seq_num = i_packet.seq_num & 0xFFFF;
sdk_packet->cmd_id = i_packet.cmd_id;
sdk_packet->cmd_type = i_packet.cmd_type;
sdk_packet->sender_type = i_packet.sender_type;
sdk_packet->crc16_h = crc_16_.ccitt(o_buf, 18);
if (i_packet.data_len == 0) {
sdk_packet->crc32_d = 0;
} else {
sdk_packet->crc32_d = crc_32_.crc32(i_packet.data, i_packet.data_len);
}
memcpy(sdk_packet->data, i_packet.data, i_packet.data_len);
*o_len = sdk_packet->length;
return 0;
}
bool SdkProtocol::ParsePacket(uint8_t *i_buf, uint32_t buf_size, CommPacket *o_packet) {
SdkPacket *sdk_packet = (SdkPacket *)i_buf;
if (buf_size < GetPacketWrapperLen()) {
return false;
}
memset((void *)o_packet, 0, sizeof(CommPacket));
o_packet->protocol = kLidarSdk;
o_packet->version = sdk_packet->version;
o_packet->seq_num = sdk_packet->seq_num;
o_packet->cmd_id = sdk_packet->cmd_id;
o_packet->cmd_type = sdk_packet->cmd_type;
o_packet->sender_type = sdk_packet->sender_type;
o_packet->data = sdk_packet->data;
o_packet->data_len = sdk_packet->length - GetPacketWrapperLen();
return true;
}
uint32_t SdkProtocol::GetPreambleLen() {
return sizeof(SdkPreamble);
}
uint32_t SdkProtocol::GetPacketWrapperLen() {
return sizeof(SdkPacket) - 1;
}
uint32_t SdkProtocol::GetPacketLen(uint8_t *buf) {
SdkPreamble *preamble = (SdkPreamble *)buf;
return preamble->length;
}
bool SdkProtocol::CheckPreamble(uint8_t *buf, uint32_t buf_size) {
if (buf_size < GetPreambleLen()) {
return false;
}
SdkPacket *packet = (SdkPacket *)buf;
if (packet->sof != kSdkProtocolSof) {
return false;
}
if (packet->version != kSdkVer) {
return false;
}
if (packet->length < GetPreambleLen()) {
return false;
}
uint16_t crc16_h = crc_16_.ccitt(buf, 18);
if (packet->crc16_h != crc16_h) {
return false;
}
uint32_t crc32_d = 0;
if (packet->length - GetPacketWrapperLen() == 0) {
crc32_d = 0;
} else {
crc32_d = crc_32_.crc32(packet->data, packet->length - GetPacketWrapperLen());
}
if (packet->crc32_d != crc32_d) {
return false;
}
return true;
}
} // namespace lidar
} // namespace livox
+91
View File
@@ -0,0 +1,91 @@
//
// The MIT License (MIT)
//
// Copyright (c) 2022 Livox. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#ifndef LIVOX_SDK_PROTOCOL_H_
#define LIVOX_SDK_PROTOCOL_H_
#include <stdint.h>
#include "comm/protocol.h"
#include "FastCRC/FastCRC.h"
namespace livox {
namespace lidar {
typedef enum { kSdkVerNone, kSdkVer0, kSdkVer1 } SdkVersion;
#pragma pack(1)
typedef struct {
uint8_t sof;
uint8_t version;
uint16_t length;
uint32_t seq_num;
uint16_t cmd_id;
uint8_t cmd_type;
uint8_t sender_type;
char rsvd[6];
uint16_t crc16_h;
uint32_t crc32_d;
} SdkPreamble;
typedef struct {
uint8_t sof;
uint8_t version;
uint16_t length;
uint32_t seq_num;
uint16_t cmd_id;
uint8_t cmd_type;
uint8_t sender_type;
char rsvd[6];
uint16_t crc16_h;
uint32_t crc32_d;
uint8_t data[1];
} SdkPacket;
#pragma pack()
class SdkProtocol : public Protocol {
public:
SdkProtocol();
~SdkProtocol();
bool ParsePacket(uint8_t *i_buf, uint32_t buf_size, CommPacket *o_packet);
int32_t Pack(uint8_t *o_buf, uint32_t o_buf_size, uint32_t *o_len, const CommPacket &i_packet);
uint32_t GetPreambleLen();
uint32_t GetPacketWrapperLen();
uint32_t GetPacketLen(uint8_t *buf);
bool CheckPreamble(uint8_t *buf, uint32_t buf_size);
private:
FastCRC16 crc_16_;
FastCRC32 crc_32_;
};
} // namespace lidar
} // namespace livox
#endif // LIVOX_SDK_PROTOCOL_H_