parsing_utilities.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // © Copyright 2020, Septentrio NV/SA.
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 1. Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // 3. Neither the name of the copyright holder nor the names of its
14 // contributors may be used to endorse or promote products derived
15 // from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 // *****************************************************************************
30 
31 // ROSaic includes
34 // C++ library includes
35 #include <limits>
36 // Boost
37 #include <boost/spirit/include/qi_binary.hpp>
38 
45 namespace parsing_utilities {
46 
47  namespace qi = boost::spirit::qi;
48 
49  [[nodiscard]] double parseDouble(const uint8_t* buffer)
50  {
51  double val;
52  qi::parse(buffer, buffer + 8, qi::little_bin_double, val);
53  return val;
54  }
55 
61  [[nodiscard]] bool parseDouble(const std::string& string, double& value)
62  {
63  return string_utilities::toDouble(string, value) || string.empty();
64  }
65 
66  [[nodiscard]] float parseFloat(const uint8_t* buffer)
67  {
68  float val;
69  qi::parse(buffer, buffer + 4, qi::little_bin_float, val);
70  return val;
71  }
72 
78  [[nodiscard]] bool parseFloat(const std::string& string, float& value)
79  {
80  return string_utilities::toFloat(string, value) || string.empty();
81  }
82 
90  [[nodiscard]] int16_t parseInt16(const uint8_t* buffer)
91  {
92  int16_t val;
93  qi::parse(buffer, buffer + 2, qi::little_word, val);
94  return val;
95  }
96 
102  [[nodiscard]] bool parseInt16(const std::string& string, int16_t& value,
103  int32_t base)
104  {
105  value = 0;
106  if (string.empty())
107  {
108  return true;
109  }
110 
111  int32_t intermd;
112  if (string_utilities::toInt32(string, intermd, base) &&
113  intermd <= std::numeric_limits<int16_t>::max() &&
114  intermd >= std::numeric_limits<int16_t>::min())
115  {
116  value = static_cast<int16_t>(intermd);
117  return true;
118  }
119 
120  return false;
121  }
122 
123  [[nodiscard]] int32_t parseInt32(const uint8_t* buffer)
124  {
125  int32_t val;
126  qi::parse(buffer, buffer + 4, qi::little_dword, val);
127  return val;
128  }
129 
135  [[nodiscard]] bool parseInt32(const std::string& string, int32_t& value,
136  int32_t base)
137  {
138  return string_utilities::toInt32(string, value, base) || string.empty();
139  }
140 
146  [[nodiscard]] bool parseUInt8(const std::string& string, uint8_t& value,
147  int32_t base)
148  {
149  value = 0;
150  if (string.empty())
151  {
152  return true;
153  }
154 
155  uint32_t intermd;
156  if (string_utilities::toUInt32(string, intermd, base) &&
157  intermd <= std::numeric_limits<uint8_t>::max())
158  {
159  value = static_cast<uint8_t>(intermd);
160  return true;
161  }
162 
163  return false;
164  }
165 
166  [[nodiscard]] uint16_t parseUInt16(const uint8_t* buffer)
167  {
168  uint16_t val;
169  qi::parse(buffer, buffer + 2, qi::little_word, val);
170  return val;
171  }
172 
178  [[nodiscard]] bool parseUInt16(const std::string& string, uint16_t& value,
179  int32_t base)
180  {
181  value = 0;
182  if (string.empty())
183  {
184  return true;
185  }
186 
187  uint32_t intermd;
188  if (string_utilities::toUInt32(string, intermd, base) &&
189  intermd <= std::numeric_limits<uint16_t>::max())
190  {
191  value = static_cast<uint16_t>(intermd);
192  return true;
193  }
194 
195  return false;
196  }
197 
198  [[nodiscard]] uint32_t parseUInt32(const uint8_t* buffer)
199  {
200  uint32_t val;
201  qi::parse(buffer, buffer + 4, qi::little_dword, val);
202  return val;
203  }
204 
210  [[nodiscard]] bool parseUInt32(const std::string& string, uint32_t& value,
211  int32_t base)
212  {
213  return string_utilities::toUInt32(string, value, base) || string.empty();
214  }
215 
221  [[nodiscard]] double convertUTCDoubleToSeconds(double utc_double)
222  {
223  uint32_t hours = static_cast<uint32_t>(utc_double) / 10000;
224  uint32_t minutes = (static_cast<uint32_t>(utc_double) - hours * 10000) / 100;
225 
226  return utc_double - static_cast<double>(hours * 10000 + minutes * 100) +
227  static_cast<double>(hours * 3600 + minutes * 60);
228  }
229 
235  [[nodiscard]] double convertDMSToDegrees(double dms)
236  {
237  uint32_t whole_degrees = static_cast<uint32_t>(dms) / 100;
238  double minutes = dms - static_cast<double>(whole_degrees * 100);
239  return static_cast<double>(whole_degrees) + minutes / 60.0;
240  }
241 
258  [[nodiscard]] time_t convertUTCtoUnix(double utc_double)
259  {
260  time_t time_now = time(0);
261  struct tm* timeinfo;
262 
263  // The function gmtime uses the value at &time_now to fill a tm structure
264  // with the values that represent the corresponding time, expressed as a UTC
265  // time.
266  timeinfo = gmtime(&time_now);
267 
268  uint32_t hours = static_cast<uint32_t>(utc_double) / 10000;
269  uint32_t minutes = (static_cast<uint32_t>(utc_double) - hours * 10000) / 100;
270  uint32_t seconds =
271  (static_cast<uint32_t>(utc_double) - hours * 10000 - minutes * 100);
272 
273  // Overwriting timeinfo with UTC time as extracted from utc_double..
274  timeinfo->tm_hour = hours; // hours since midnight - [0,23]
275  timeinfo->tm_min = minutes; // minutes after the hour - [0,59]
276  timeinfo->tm_sec = seconds; // seconds after the minute - [0,59]
277 
278  /* // If you are doing a simulation, add year, month and day here:
279  uint32_t year; // year, starting from 1900
280  uint32_t month; // months since January - [0,11]
281  uint32_t day; //day of the month - [1,31]
282  timeinfo->tm_year = year;
283  timeinfo->tm_mon = month;
284  timeinfo->tm_mday = day;
285  */
286 
287  // Inverse of gmtime, the latter converts time_t (Unix time) to tm (UTC time)
288  return timegm(timeinfo);
289  }
290 
291  [[nodiscard]] std::string convertUserPeriodToRxCommand(uint32_t period_user)
292  {
293  std::string cmd;
294 
295  if (period_user == 0)
296  return "OnChange";
297  else if (period_user < 1000)
298  return "msec" + std::to_string(period_user);
299  else if (period_user <= 60000)
300  return "sec" + std::to_string(period_user / 1000);
301  else
302  return "min" + std::to_string(period_user / 60000);
303  }
304 
305  [[nodiscard]] uint16_t getCrc(const std::vector<uint8_t>& message)
306  {
307  return parseUInt16(message.data() + 2);
308  }
309 
310  [[nodiscard]] uint16_t getId(const std::vector<uint8_t>& message)
311  {
312  // Defines bit mask..
313  // Highest three bits are for revision and rest for block number
314  constexpr uint16_t mask = 8191;
315  // Bitwise AND gives us all but highest 3 bits set to zero, rest unchanged
316 
317  return parseUInt16(message.data() + 4) & mask;
318  }
319 
320  [[nodiscard]] uint16_t getLength(const std::vector<uint8_t>& message)
321  {
322  return parseUInt16(message.data() + 6);
323  }
324 
325  [[nodiscard]] uint32_t getTow(const std::vector<uint8_t>& message)
326  {
327  return parseUInt32(message.data() + 8);
328  }
329 
330  [[nodiscard]] uint16_t getWnc(const std::vector<uint8_t>& message)
331  {
332  return parseUInt16(message.data() + 12);
333  }
334 
335 } // namespace parsing_utilities
string_utilities::toInt32
bool toInt32(const std::string &string, int32_t &value, int32_t base=10)
Interprets the contents of "string" as a floating point number of whatever integer type your system h...
Definition: string_utilities.cpp:104
string_utilities::toDouble
bool toDouble(const std::string &string, double &value)
Interprets the contents of "string" as a floating point number of type double It stores the "string"'...
Definition: string_utilities.cpp:53
parsing_utilities::getTow
uint32_t getTow(const std::vector< uint8_t > &message)
Get the time of week in ms of the SBF message.
Definition: parsing_utilities.cpp:325
parsing_utilities::convertUTCtoUnix
std::time_t convertUTCtoUnix(double utc_double)
Converts UTC time from the without-colon-delimiter format to Unix Epoch time (a number-of-seconds-sin...
Definition: parsing_utilities.cpp:258
parsing_utilities::convertUserPeriodToRxCommand
std::string convertUserPeriodToRxCommand(uint32_t period_user)
Transforms the input polling period [milliseconds] into a std::string number that can be appended to ...
Definition: parsing_utilities.cpp:291
parsing_utilities::getWnc
uint16_t getWnc(const std::vector< uint8_t > &message)
Get the GPS week counter of the SBF message.
Definition: parsing_utilities.cpp:330
parsing_utilities::parseInt32
int32_t parseInt32(const uint8_t *buffer)
Converts a 4-byte-buffer into a signed 32-bit integer.
Definition: parsing_utilities.cpp:123
parsing_utilities::parseFloat
float parseFloat(const uint8_t *buffer)
Converts a 4-byte-buffer into a float.
Definition: parsing_utilities.cpp:66
parsing_utilities
Definition: parsing_utilities.hpp:57
parsing_utilities::getLength
uint16_t getLength(const std::vector< uint8_t > &message)
Get the length of the SBF message.
Definition: parsing_utilities.cpp:320
parsing_utilities::convertUTCDoubleToSeconds
double convertUTCDoubleToSeconds(double utc_double)
Converts UTC time from the without-colon-delimiter format to the number-of-seconds-since-midnight for...
Definition: parsing_utilities.cpp:221
parsing_utilities::getCrc
uint16_t getCrc(const std::vector< uint8_t > &message)
Get the CRC of the SBF message.
Definition: parsing_utilities.cpp:305
parsing_utilities::parseUInt32
uint32_t parseUInt32(const uint8_t *buffer)
Converts a 4-byte-buffer into an unsigned 32-bit integer.
Definition: parsing_utilities.cpp:198
parsing_utilities::parseUInt16
uint16_t parseUInt16(const uint8_t *buffer)
Converts a 2-byte-buffer into an unsigned 16-bit integer.
Definition: parsing_utilities.cpp:166
parsing_utilities.hpp
Declares utility functions used when parsing messages.
parsing_utilities::getId
uint16_t getId(const std::vector< uint8_t > &message)
Get the ID of the SBF message.
Definition: parsing_utilities.cpp:310
parsing_utilities::convertDMSToDegrees
double convertDMSToDegrees(double dms)
Converts latitude or longitude from the DMS notation (in the without-colon-delimiter format),...
Definition: parsing_utilities.cpp:235
string_utilities::toUInt32
bool toUInt32(const std::string &string, uint32_t &value, int32_t base=10)
Interprets the contents of "string" as a floating point number of whatever unsigned integer type your...
Definition: string_utilities.cpp:136
string_utilities.hpp
Declares lower-level string utility functions used when parsing messages.
parsing_utilities::parseUInt8
bool parseUInt8(const std::string &string, uint8_t &value, int32_t base=10)
Interprets the contents of "string" as a unsigned integer number of type uint8_t.
Definition: parsing_utilities.cpp:146
parsing_utilities::parseInt16
int16_t parseInt16(const uint8_t *buffer)
Converts a 2-byte-buffer into a signed 16-bit integer.
Definition: parsing_utilities.cpp:90
parsing_utilities::parseDouble
double parseDouble(const uint8_t *buffer)
Converts an 8-byte-buffer into a double.
Definition: parsing_utilities.cpp:49
string_utilities::toFloat
bool toFloat(const std::string &string, float &value)
Interprets the contents of "string" as a floating point number of type float.
Definition: string_utilities.cpp:79


septentrio_gnss_driver
Author(s): Tibor Dome, Thomas Emter
autogenerated on Sat May 10 2025 03:03:10