lvx_file.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #ifndef LIVOX_FILE_H_
  25. #define LIVOX_FILE_H_
  26. #include <fstream>
  27. #include <ios>
  28. #include <list>
  29. #include <memory>
  30. #include <mutex>
  31. #include <vector>
  32. #include "livox_sdk.h"
  33. namespace livox_ros {
  34. #define kMaxPointSize 1500
  35. #define kDefaultFrameDurationTime 50
  36. const uint32_t kMaxFrameSize = 2048 * 1024;
  37. typedef enum {
  38. kDeviceStateDisconnect = 0,
  39. kDeviceStateConnect = 1,
  40. kDeviceStateSampling = 2,
  41. } DeviceState;
  42. typedef enum {
  43. kLvxFileOk = 0,
  44. kLvxFileNotExist,
  45. kLvxFileSizeFault,
  46. kLvxFileHeaderFault,
  47. kLvxFileDeviceInfoFault,
  48. kLvxFileDataInfoFault,
  49. kLvxFileAtEnd,
  50. kLvxFileReadFail,
  51. kLvxFileFrameHeaderError,
  52. kLvxFileUndefFault
  53. } LvxFileState;
  54. typedef enum {
  55. kLvxFileV0 = 0,
  56. kLvxFileV1 = 1,
  57. kLvxFileVersionUndef = 2,
  58. } LvxFileVersion;
  59. typedef struct {
  60. uint8_t handle;
  61. DeviceState device_state;
  62. DeviceInfo info;
  63. } DeviceItem;
  64. #pragma pack(1)
  65. typedef struct {
  66. uint8_t signature[16];
  67. uint8_t version[4];
  68. uint32_t magic_code;
  69. } LvxFilePublicHeader;
  70. typedef struct {
  71. uint32_t frame_duration;
  72. uint8_t device_count;
  73. } LvxFilePrivateHeader;
  74. typedef struct {
  75. uint8_t lidar_broadcast_code[16];
  76. uint8_t hub_broadcast_code[16];
  77. uint8_t device_index;
  78. uint8_t device_type;
  79. uint8_t extrinsic_enable;
  80. float roll;
  81. float pitch;
  82. float yaw;
  83. float x;
  84. float y;
  85. float z;
  86. } LvxFileDeviceInfo;
  87. typedef struct {
  88. uint8_t device_index;
  89. uint8_t version;
  90. uint8_t port_id;
  91. uint8_t lidar_index;
  92. uint8_t rsvd;
  93. uint32_t error_code;
  94. uint8_t timestamp_type;
  95. uint8_t data_type;
  96. uint8_t timestamp[8];
  97. uint8_t raw_point[kMaxPointSize];
  98. uint32_t pack_size;
  99. } LvxFilePacket;
  100. typedef struct {
  101. uint64_t current_offset;
  102. uint64_t next_offset;
  103. uint64_t frame_index;
  104. } FrameHeader;
  105. typedef struct {
  106. FrameHeader header;
  107. LvxFilePacket *packet;
  108. } LvxFileFrame;
  109. typedef struct { uint8_t device_count; } LvxFilePrivateHeaderV0;
  110. typedef struct {
  111. uint8_t lidar_broadcast_code[16];
  112. uint8_t hub_broadcast_code[16];
  113. uint8_t device_index;
  114. uint8_t device_type;
  115. float roll;
  116. float pitch;
  117. float yaw;
  118. float x;
  119. float y;
  120. float z;
  121. } LvxFileDeviceInfoV0;
  122. typedef struct {
  123. uint8_t device_index;
  124. uint8_t version;
  125. uint8_t port_id;
  126. uint8_t lidar_index;
  127. uint8_t rsvd;
  128. uint32_t error_code;
  129. uint8_t timestamp_type;
  130. uint8_t data_type;
  131. uint8_t timestamp[8];
  132. LivoxPoint raw_point[100];
  133. } LvxFilePacketV0;
  134. typedef struct {
  135. uint64_t current_offset;
  136. uint64_t next_offset;
  137. uint64_t frame_index;
  138. uint64_t packet_count;
  139. } FrameHeaderV0;
  140. typedef struct {
  141. FrameHeaderV0 header;
  142. LvxFilePacketV0 *packet;
  143. } LvxFileFrameV0;
  144. typedef struct {
  145. uint32_t buffer_capacity; /* max buffer size */
  146. uint32_t data_size; /* frame data erea size */
  147. uint8_t *packet; /* packet data erea */
  148. } OutPacketBuffer;
  149. #pragma pack()
  150. class LvxFileHandle {
  151. public:
  152. LvxFileHandle();
  153. ~LvxFileHandle() = default;
  154. int Open(const char *filename, std::ios_base::openmode mode);
  155. bool Eof();
  156. int InitLvxFile();
  157. void InitLvxFileHeader();
  158. void SaveFrameToLvxFile(std::list<LvxFilePacket> &point_packet_list_temp);
  159. void BasePointsHandle(LivoxEthPacket *data, LvxFilePacket &packet);
  160. void CloseLvxFile();
  161. void AddDeviceInfo(LvxFileDeviceInfo &info) {
  162. device_info_list_.push_back(info);
  163. }
  164. int GetDeviceInfoListSize() { return device_info_list_.size(); }
  165. int GetDeviceCount() { return device_count_; }
  166. int GetDeviceInfo(uint8_t idx, LvxFileDeviceInfo *info);
  167. int GetFileState(void) { return state_; };
  168. int GetPacketsOfFrame(OutPacketBuffer *PacketsOfFrame);
  169. int GetLvxFileReadProgress();
  170. int GetFileVersion() { return file_ver_; }
  171. private:
  172. std::fstream lvx_file_;
  173. std::vector<LvxFileDeviceInfo> device_info_list_;
  174. uint8_t file_ver_;
  175. uint8_t device_count_;
  176. LvxFilePublicHeader public_header_;
  177. LvxFilePrivateHeader private_header_;
  178. LvxFilePrivateHeaderV0 private_header_v0_;
  179. uint32_t cur_frame_index_;
  180. uint64_t cur_offset_;
  181. uint32_t frame_duration_;
  182. uint64_t data_start_offset_;
  183. uint64_t size_;
  184. int mode_;
  185. int state_;
  186. uint64_t MiniFileSize();
  187. uint64_t PrivateHeaderOffset();
  188. uint64_t DataStartOffset();
  189. uint32_t PacketNumOfFrame();
  190. bool ReadAndCheckHeader();
  191. bool AddAndCheckDeviceInfo();
  192. bool PrepareDataRead();
  193. uint64_t DataSizeOfFrame(FrameHeader &frame_header) {
  194. return (frame_header.next_offset - frame_header.current_offset -
  195. sizeof(frame_header));
  196. }
  197. uint64_t DataSizeOfFrame(FrameHeaderV0 &frame_header_v0) {
  198. return (frame_header_v0.next_offset - frame_header_v0.current_offset -
  199. sizeof(frame_header_v0));
  200. }
  201. };
  202. } // namespace livox_ros
  203. #endif