sdk_protocol.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "sdk_protocol.h"
  25. #include <stdio.h>
  26. #include <string.h>
  27. namespace livox_ros {
  28. const uint8_t kSdkProtocolSof = 0xAA;
  29. const uint32_t kSdkPacketCrcSize = 4; // crc32
  30. const uint32_t kSdkPacketPreambleCrcSize = 2; // crc16
  31. SdkProtocol::SdkProtocol(uint16_t seed16, uint32_t seed32)
  32. : crc16_(seed16), crc32_(seed32) {}
  33. int32_t SdkProtocol::Pack(uint8_t *o_buf, uint32_t o_buf_size, uint32_t *o_len,
  34. const CommPacket &i_packet) {
  35. SdkPacket *sdk_packet = (SdkPacket *)o_buf;
  36. if (kLidarSdk != i_packet.protocol) {
  37. return -1;
  38. }
  39. sdk_packet->sof = kSdkProtocolSof;
  40. sdk_packet->length = i_packet.data_len + GetPacketWrapperLen();
  41. if (sdk_packet->length > o_buf_size) {
  42. return -1;
  43. }
  44. sdk_packet->version = kSdkVer0;
  45. sdk_packet->packet_type = i_packet.packet_type;
  46. sdk_packet->seq_num = i_packet.seq_num & 0xFFFF;
  47. sdk_packet->preamble_crc =
  48. crc16_.mcrf4xx_calc(o_buf, GetPreambleLen() - kSdkPacketPreambleCrcSize);
  49. sdk_packet->cmd_set = i_packet.cmd_set;
  50. sdk_packet->cmd_id = i_packet.cmd_code;
  51. memcpy(sdk_packet->data, i_packet.data, i_packet.data_len);
  52. uint32_t crc =
  53. crc32_.crc32_calc(o_buf, sdk_packet->length - kSdkPacketCrcSize);
  54. o_buf[sdk_packet->length - 4] = crc & 0xFF;
  55. o_buf[sdk_packet->length - 3] = (crc >> 8) & 0xFF;
  56. o_buf[sdk_packet->length - 2] = (crc >> 16) & 0xFF;
  57. o_buf[sdk_packet->length - 1] = (crc >> 24) & 0xFF;
  58. *o_len = sdk_packet->length;
  59. return 0;
  60. }
  61. int32_t SdkProtocol::ParsePacket(const uint8_t *i_buf, uint32_t i_len,
  62. CommPacket *o_packet) {
  63. SdkPacket *sdk_packet = (SdkPacket *)i_buf;
  64. if (i_len < GetPacketWrapperLen()) {
  65. return -1; // packet lenth error
  66. }
  67. memset((void *)o_packet, 0, sizeof(CommPacket));
  68. o_packet->packet_type = sdk_packet->packet_type;
  69. o_packet->protocol = kLidarSdk;
  70. o_packet->protocol_version = sdk_packet->version;
  71. o_packet->seq_num = sdk_packet->seq_num;
  72. o_packet->cmd_set = sdk_packet->cmd_set;
  73. o_packet->cmd_code = sdk_packet->cmd_id;
  74. o_packet->data_len = sdk_packet->length - GetPacketWrapperLen();
  75. o_packet->data = sdk_packet->data;
  76. return 0;
  77. }
  78. uint32_t SdkProtocol::GetPreambleLen() { return sizeof(SdkPreamble); }
  79. uint32_t SdkProtocol::GetPacketWrapperLen() {
  80. return sizeof(SdkPacket) - 1 + kSdkPacketCrcSize;
  81. }
  82. uint32_t SdkProtocol::GetPacketLen(const uint8_t *buf) {
  83. SdkPreamble *preamble = (SdkPreamble *)buf;
  84. return preamble->length;
  85. }
  86. int32_t SdkProtocol::CheckPreamble(const uint8_t *buf) {
  87. SdkPreamble *preamble = (SdkPreamble *)buf;
  88. if ((preamble->sof == kSdkProtocolSof) &&
  89. (crc16_.mcrf4xx_calc(buf, GetPreambleLen()) == 0)) {
  90. return 0;
  91. } else {
  92. return -1;
  93. }
  94. }
  95. int32_t SdkProtocol::CheckPacket(const uint8_t *buf) {
  96. SdkPacket *sdk_packet = (SdkPacket *)buf;
  97. uint32_t crc = ((uint32_t)(buf[sdk_packet->length - 4])) |
  98. (((uint32_t)(buf[sdk_packet->length - 3])) << 8) |
  99. (((uint32_t)(buf[sdk_packet->length - 2])) << 16) |
  100. (((uint32_t)(buf[sdk_packet->length - 1])) << 24);
  101. if (crc32_.crc32_calc(buf, sdk_packet->length - kSdkPacketCrcSize) == crc) {
  102. return 0;
  103. } else {
  104. return -1;
  105. }
  106. }
  107. } // namespace livox_ros