Fixposition SDK 0.0.0-heads/main-0-g7b59b93
Collection of c++ libraries and apps for use with Fixposition products
Loading...
Searching...
No Matches
fpl.hpp
Go to the documentation of this file.
1/**
2 * \verbatim
3 * ___ ___
4 * \ \ / /
5 * \ \/ / Copyright (c) Fixposition AG (www.fixposition.com) and contributors
6 * / /\ \ License: see the LICENSE file
7 * /__/ \__\
8 * \endverbatim
9 *
10 * @file
11 * @brief Fixposition SDK: .fpl logfile helpers
12 *
13 * @page FPSDK_COMMON_FPL .fpl logfile utilities
14 *
15 * **API**: fpsdk_common/fpl.hpp and fpsdk::common::fpl
16 *
17 */
18#ifndef __FPSDK_COMMON_FPL_HPP__
19#define __FPSDK_COMMON_FPL_HPP__
20
21/* LIBC/STL */
22#include <cstdint>
23#include <iostream>
24#include <memory>
25#include <string>
26#include <vector>
27
28/* EXTERNAL */
29#include <boost/iostreams/filtering_stream.hpp>
30#include <boost/iostreams/stream.hpp>
31
32/* PACKAGE */
33#include "fpsdk_common/time.hpp"
34
35namespace fpsdk {
36namespace common {
37/**
38 * @brief .fpl logfile utilities
39 */
40namespace fpl {
41/* ****************************************************************************************************************** */
42
43/**
44 * @brief FplMessage types
45 */
46enum class FplType : uint16_t
47{ // clang-format off
48 UNSPECIFIED = 0x0000, //!< Invalid
49 ROSMSGDEF = 0x0101, //!< Message definition for a topic (a.k.a. "connection header" in rosbag speak)
50 ROSMSGBIN = 0x0102, //!< Serialised ROS message with timestamp
51 LOGMETA = 0x0105, //!< Logfile meta data
52 STREAMMSG = 0x0106, //!< Stream message raw data with timestamp
53 LOGSTATUS = 0x0107, //!< Logging status
54 BLOB = 0xbaad, //!< Arbitrary data, not FplMessage framing
55 INT_D = 0xffaa, //!< Fixposition internal use only
56 INT_F = 0xffbb, //!< Fixposition internal use only
57 INT_X = 0xffcc, //!< Fixposition internal use only
58}; // clang-format on
59
60/**
61 * @brief Stringify type
62 *
63 * @param[in] type The type
64 *
65 * @returns a unique string identifying the type
66 */
67const char* FplTypeStr(const FplType type);
68
69// ---------------------------------------------------------------------------------------------------------------------
70
71/**
72 * @brief One message in the log
73 */
75{
76 public:
77 FplMessage();
78
79 /**
80 * @brief Parse message from buffer
81 *
82 * @param[in] data Pointer to start of message data
83 * @param[in] size Size of message data
84 *
85 * @returns 0 if the data is not a valid message,
86 * -1 if the size is too small to decide, or
87 * the message size (>0) if a valid message was found and loaded
88 */
89 int Parse(const uint8_t* data, const uint32_t size);
90
91 /**
92 * @brief Get message
93 *
94 * @returns a reference to the message data
95 */
96 const std::vector<uint8_t>& Raw() const;
97 /**
98 * @brief Get message data
99 *
100 * @returns a pointer to the message data (size = Size())
101 */
102 const uint8_t* RawData() const;
103
104 /**
105 * @brief Get message size
106 *
107 * @returns the size of the message (data = Data())
108 */
109 uint32_t RawSize() const;
110
111 /**
112 * @brief Get payload (log) type
113 *
114 * @returns type of the payload
115 */
117
118 /**
119 * @brief Get payload data
120 *
121 * @returns a pointer to the payload data
122 */
123 const uint8_t* PayloadData() const;
124
125 /**
126 * @brief Get payload size
127 *
128 * @returns size of the payload data
129 */
130 uint32_t PayloadSize() const;
131
132 // Debugging stuff, only with FplParser. For debugging.
133 uint64_t file_pos_; //!< Offset in logfile of the log message
134 uint64_t file_size_; //!< Size in the logfile of the log message
135 uint64_t file_seq_; //!< Message sequence counter in logfile
136
137 static constexpr std::uint32_t MAX_SIZE = 2000000; //!< Maximum message size
138 static constexpr std::size_t MAX_NAME_LEN = 100; //!< Maximum length of (file, topic, ...) names
139
140 /**
141 * @brief Make message a BLOB
142 *
143 * @param[in] data The data
144 * @param[in] size The size of the data
145 */
146 void SetBlob(const uint8_t* data, const uint32_t size);
147
148 private:
149 static constexpr uint16_t MAGIC = 0x55aa; //!< Start of message marker
150 static constexpr uint32_t HEADER_SIZE = 8; //!< Message header size
151 FplType payload_type_; //!< Payload type
152 uint32_t payload_size_; //!< Payload size
153 std::vector<uint8_t> message_; //!< Entire message
154};
155
156// ---------------------------------------------------------------------------------------------------------------------
157
158/**
159 * @brief Logfile parser
160 */
162{
163 public:
164 FplParser();
165
166 /**
167 * @brief Add data to parser
168 *
169 * @param[in] data Pointer to data, can be NULL, in which case \c size is ignored
170 * @param[in] size Size of data, can be <= 0, in which case \c data is ignored
171 *
172 * The suggested chunk size to add to the parser is FplMessage::MAX_SIZE
173 *
174 * @returns true if data was added to the parser, false if there was not enough space left (parser overflow)
175 */
176 bool Add(const uint8_t* data, const std::size_t size);
177
178 /**
179 * @brief Reset parser
180 *
181 * Resets the parser state and discards all collected data.
182 */
183 void Reset();
184
185 /**
186 * @brief Process data in parser, return message
187 *
188 * @param[out] log_msg The detected message
189 *
190 * @returns true if a message was detected, false otherwise (meaning: not enough data in parser). Note that also in
191 * the false case the \c log_msg may be modified.
192 */
193 bool Process(FplMessage& log_msg);
194
195 private:
196 std::vector<uint8_t> buf_; //!< Working buffer
197 std::size_t offs_; //!< Current offset into buf_
198 std::size_t size_; //!< Current size of useful data in buf_ (counted from offs_)
199 std::size_t file_pos_; //!< Offset into "file", i.e. total number of bytes processed so far
200 std::size_t file_seq_; //!< Count of messages from "file"
201 void EmitBlob(FplMessage& log_msg); //!< Helper for Process()
202};
203
204// ---------------------------------------------------------------------------------------------------------------------
205
206/**
207 * @brief Read log messages from logfile
208 */
210{
211 public:
213
214 /**
215 * @brief Open logfile
216 *
217 * @param[in] path The logfile
218 *
219 * @returns true on success, false otherwise
220 */
221 bool Open(const std::string& path);
222
223 /**
224 * @brief Get next log message
225 *
226 * @param[out] log_msg The log message
227 *
228 * @returns true if a next log message was found, false at end of file (or other problems)
229 */
230 bool Next(FplMessage& log_msg);
231
232 /**
233 * @brief Get progress report
234 *
235 * @param[out] progress Progress in [%] (approximate with gzipped files)
236 * @param[out] rate Rate in [MiB/s]
237 *
238 * @returns true if is a good time to print the progress report, false otherwise
239 */
240 bool GetProgress(double& progress, double& rate);
241
242 private:
243 std::string path_; //!< Logfile path
244 std::unique_ptr<std::istream> fh_; //!< File handle
245 FplParser parser_; //!< Parser
246 std::vector<uint8_t> buf_; //!< Read buffer
247 std::size_t pos_; //!< Current position in logfile
248 std::size_t count_; //!< Number of messages
249 std::size_t size_; //!< Size of logfile
250 bool is_gz_; //!< File is gzipped
251 double last_progress_; //!< GetProgress() state
252 uint64_t last_pos_; //!< GetProgress() state
253 double last_secs_; //!< GetProgress() state
254};
255
256// ---------------------------------------------------------------------------------------------------------------------
257
258/**
259 * @brief Helper for extracting meta data
260 */
262{
263 /**
264 * @brief Constructor
265 *
266 * @param[in] log_msg .fpl log message
267 */
268 LogMeta(const FplMessage& log_msg);
269 bool valid_; //!< Data valid, successfully extracted from message
270 std::string info_; //!< Stringification of (some of the) data, for debugging
271 std::string hw_uid_; //!< Hardware UID
272 std::string hw_product_; //!< Product
273 std::string sw_version_; //!< Software version
274 uint32_t log_start_time_posix_; //!< Start time of logging
275 std::string log_start_time_iso_; //!< Start time of logging
276 std::string log_profile_; //!< Logging configuration profile name
277 std::string log_target_; //!< Loging target name
278 std::string log_filename_; //!< Logfile name
279 std::string yaml_; //!< Raw meta data YAML
280};
281
282// ---------------------------------------------------------------------------------------------------------------------
283
284/**
285 * @brief Helper for extracting recording status data
286 */
288{
289 /**
290 * @brief Constructor
291 *
292 * @param[in] log_msg .fpl log message
293 */
294 LogStatus(const FplMessage& log_msg);
295 bool valid_; //!< Data valid, successfully extracted from message
296
297 // Version 1 and later
298 std::string info_; //!< Stringification of (some of the) data, for debugging
299 std::string state_; //!< Logging state: "stopped", "logging", "stopping"
300 uint32_t queue_size_; //!< Queue size
301 uint32_t queue_peak_; //!< Queue peak size
302 uint32_t queue_skip_; //!< Number of skipped messages (queue full)
303 uint32_t log_count_; //!< Number of logged messages
304 uint32_t log_errors_; //!< Number of messages failed to log (failed to write/send)
305 uint64_t log_size_; //!< Total size of logged messages [bytes]
306 uint32_t log_duration_; //!< Duration of logging [s]
307
308 // Version 2 and later
309 uint32_t log_time_posix_; //!< Approximate time
310 std::string log_time_iso_; //!< Approximate time
311 int8_t pos_source_; //!< Approximate sensor position source (see POS_SOURCE_... below)
312 int8_t pos_fix_type_; //!< Approximate sensor position fix type (see fpsdk::common::types::GnssFixType)
313 double pos_lat_; //!< Approximate sensor position latitude [deg]
314 double pos_lon_; //!< Approximate sensor position longitude [deg]
315 double pos_height_; //!< Approximate sensor position height [m]
316
317 static constexpr int8_t POS_SOURCE_UNKNOWN = 0; //!< Position source unknown resp. not available
318 static constexpr int8_t POS_SOURCE_GNSS = 1; //!< Position source is GNSS
319 static constexpr int8_t POS_SOURCE_FUSION = 2; //!< Position source is Fusion
320
321 std::string yaml_; //!< Raw status data YAML
322};
323
324// ---------------------------------------------------------------------------------------------------------------------
325
326/**
327 * @brief Helper for extracting ROS message definition (the relevant fields from the "connection header")
328 */
330{
331 /**
332 * @brief Constructor
333 *
334 * @param[in] log_msg .fpl log message
335 */
336 RosMsgDef(const FplMessage& log_msg);
337 bool valid_; //!< Data valid, successfully extracted from message
338 std::string info_; //!< Stringification of (some of the) data, for debugging
339 std::string topic_name_; //!< The topic name
340 std::string msg_name_; //!< The message name (a.k.a. data type)
341 std::string msg_md5_; //!< The message definition MD5 sum
342 std::string msg_def_; //!< The message definition (the .msg file and also the defs for all the types)
343};
344
345// ---------------------------------------------------------------------------------------------------------------------
346
347/**
348 * @brief Helper for extracting a serialised ROS message
349 */
351{
352 /**
353 * @brief Constructor
354 *
355 * @param[in] log_msg .fpl log message
356 */
357 RosMsgBin(const FplMessage& log_msg);
359 bool valid_; //!< Data valid, successfully extracted from message
360 std::string info_; //!< Stringification of (some of the) data, for debugging
361 std::string topic_name_; //!< The topic name
362 RosTime rec_time_; //!< Recording timestamp
363 std::vector<uint8_t> msg_data_; //!< Serialised ROS message data
364};
365
366// ---------------------------------------------------------------------------------------------------------------------
367
368/**
369 * @brief Helper for extracting data of a stream message (NMEA, RTCM3, etc.)
370 */
372{
373 /**
374 * @brief Constructor
375 *
376 * @param[in] log_msg .fpl log message
377 */
378 StreamMsg(const FplMessage& log_msg);
380 bool valid_; //!< Data valid, successfully extracted from message
381 std::string info_; //!< Stringification of (some of the) data, for debugging
382 RosTime rec_time_; //!< Recording timestamp
383 std::string stream_name_; //!< Stream name
384 std::vector<uint8_t> msg_data_; //!< Message data
385};
386
387/* ****************************************************************************************************************** */
388} // namespace fpl
389} // namespace common
390} // namespace fpsdk
391#endif // __FPSDK_COMMON_FPL_HPP__
Read log messages from logfile.
Definition fpl.hpp:210
bool Next(FplMessage &log_msg)
Get next log message.
bool GetProgress(double &progress, double &rate)
Get progress report.
bool Open(const std::string &path)
Open logfile.
One message in the log.
Definition fpl.hpp:75
const std::vector< uint8_t > & Raw() const
Get message.
const uint8_t * RawData() const
Get message data.
int Parse(const uint8_t *data, const uint32_t size)
Parse message from buffer.
void SetBlob(const uint8_t *data, const uint32_t size)
Make message a BLOB.
uint64_t file_size_
Size in the logfile of the log message.
Definition fpl.hpp:134
static constexpr std::size_t MAX_NAME_LEN
Maximum length of (file, topic, ...) names.
Definition fpl.hpp:138
uint32_t PayloadSize() const
Get payload size.
uint64_t file_seq_
Message sequence counter in logfile.
Definition fpl.hpp:135
const uint8_t * PayloadData() const
Get payload data.
uint32_t RawSize() const
Get message size.
uint64_t file_pos_
Offset in logfile of the log message.
Definition fpl.hpp:133
FplType PayloadType() const
Get payload (log) type.
static constexpr std::uint32_t MAX_SIZE
Maximum message size.
Definition fpl.hpp:137
bool Process(FplMessage &log_msg)
Process data in parser, return message.
bool Add(const uint8_t *data, const std::size_t size)
Add data to parser.
void Reset()
Reset parser.
FplType
FplMessage types.
Definition fpl.hpp:47
@ INT_X
Fixposition internal use only.
@ BLOB
Arbitrary data, not FplMessage framing.
@ LOGSTATUS
Logging status.
@ ROSMSGBIN
Serialised ROS message with timestamp.
@ ROSMSGDEF
Message definition for a topic (a.k.a. "connection header" in rosbag speak)
@ LOGMETA
Logfile meta data.
@ INT_F
Fixposition internal use only.
@ INT_D
Fixposition internal use only.
@ STREAMMSG
Stream message raw data with timestamp.
const char * FplTypeStr(const FplType type)
Stringify type.
Fixposition SDK.
Definition fpsdk_doc.hpp:20
Helper for extracting meta data.
Definition fpl.hpp:262
uint32_t log_start_time_posix_
Start time of logging.
Definition fpl.hpp:274
std::string info_
Stringification of (some of the) data, for debugging.
Definition fpl.hpp:270
std::string yaml_
Raw meta data YAML.
Definition fpl.hpp:279
std::string log_filename_
Logfile name.
Definition fpl.hpp:278
std::string sw_version_
Software version.
Definition fpl.hpp:273
LogMeta(const FplMessage &log_msg)
Constructor.
std::string log_target_
Loging target name.
Definition fpl.hpp:277
std::string hw_product_
Product.
Definition fpl.hpp:272
std::string log_start_time_iso_
Start time of logging.
Definition fpl.hpp:275
bool valid_
Data valid, successfully extracted from message.
Definition fpl.hpp:269
std::string log_profile_
Logging configuration profile name.
Definition fpl.hpp:276
std::string hw_uid_
Hardware UID.
Definition fpl.hpp:271
Helper for extracting recording status data.
Definition fpl.hpp:288
static constexpr int8_t POS_SOURCE_GNSS
Position source is GNSS.
Definition fpl.hpp:318
std::string state_
Logging state: "stopped", "logging", "stopping".
Definition fpl.hpp:299
bool valid_
Data valid, successfully extracted from message.
Definition fpl.hpp:295
static constexpr int8_t POS_SOURCE_FUSION
Position source is Fusion.
Definition fpl.hpp:319
uint32_t queue_size_
Queue size.
Definition fpl.hpp:300
uint32_t log_count_
Number of logged messages.
Definition fpl.hpp:303
std::string yaml_
Raw status data YAML.
Definition fpl.hpp:321
uint32_t queue_peak_
Queue peak size.
Definition fpl.hpp:301
uint32_t queue_skip_
Number of skipped messages (queue full)
Definition fpl.hpp:302
static constexpr int8_t POS_SOURCE_UNKNOWN
Position source unknown resp. not available.
Definition fpl.hpp:317
int8_t pos_source_
Approximate sensor position source (see POS_SOURCE_... below)
Definition fpl.hpp:311
uint32_t log_duration_
Duration of logging [s].
Definition fpl.hpp:306
uint64_t log_size_
Total size of logged messages [bytes].
Definition fpl.hpp:305
std::string info_
Stringification of (some of the) data, for debugging.
Definition fpl.hpp:298
int8_t pos_fix_type_
Approximate sensor position fix type (see fpsdk::common::types::GnssFixType)
Definition fpl.hpp:312
LogStatus(const FplMessage &log_msg)
Constructor.
double pos_height_
Approximate sensor position height [m].
Definition fpl.hpp:315
uint32_t log_errors_
Number of messages failed to log (failed to write/send)
Definition fpl.hpp:304
double pos_lon_
Approximate sensor position longitude [deg].
Definition fpl.hpp:314
uint32_t log_time_posix_
Approximate time.
Definition fpl.hpp:309
std::string log_time_iso_
Approximate time.
Definition fpl.hpp:310
double pos_lat_
Approximate sensor position latitude [deg].
Definition fpl.hpp:313
Helper for extracting a serialised ROS message.
Definition fpl.hpp:351
std::string topic_name_
The topic name.
Definition fpl.hpp:361
std::string info_
Stringification of (some of the) data, for debugging.
Definition fpl.hpp:360
RosTime rec_time_
Recording timestamp.
Definition fpl.hpp:362
std::vector< uint8_t > msg_data_
Serialised ROS message data.
Definition fpl.hpp:363
bool valid_
Data valid, successfully extracted from message.
Definition fpl.hpp:359
RosMsgBin(const FplMessage &log_msg)
Constructor.
Helper for extracting ROS message definition (the relevant fields from the "connection header")
Definition fpl.hpp:330
RosMsgDef(const FplMessage &log_msg)
Constructor.
std::string info_
Stringification of (some of the) data, for debugging.
Definition fpl.hpp:338
std::string msg_name_
The message name (a.k.a. data type)
Definition fpl.hpp:340
bool valid_
Data valid, successfully extracted from message.
Definition fpl.hpp:337
std::string msg_md5_
The message definition MD5 sum.
Definition fpl.hpp:341
std::string topic_name_
The topic name.
Definition fpl.hpp:339
std::string msg_def_
The message definition (the .msg file and also the defs for all the types)
Definition fpl.hpp:342
Helper for extracting data of a stream message (NMEA, RTCM3, etc.)
Definition fpl.hpp:372
bool valid_
Data valid, successfully extracted from message.
Definition fpl.hpp:380
std::string stream_name_
Stream name.
Definition fpl.hpp:383
std::string info_
Stringification of (some of the) data, for debugging.
Definition fpl.hpp:381
StreamMsg(const FplMessage &log_msg)
Constructor.
std::vector< uint8_t > msg_data_
Message data.
Definition fpl.hpp:384
RosTime rec_time_
Recording timestamp.
Definition fpl.hpp:382
Minimal ros::Time() / rplcpp::Time implementation (that doesn't throw)
Definition time.hpp:67
Fixposition SDK: Time utilities.