nmealib 0.0.4
NMEA 0183/NMEA 2000 parsing library
Loading...
Searching...
No Matches
nmea0183.h
Go to the documentation of this file.
1#pragma once
2
3#include "message.h"
4#include "nmeaException.h"
5
6#include <string>
7
8namespace nmealib {
9namespace nmea0183 {
10
15public:
22 explicit TooLongSentenceException(const std::string& context, const std::string& details = "") :
23 NmeaException(context, "NMEA 0183 sentence exceeds maximum length of 82 characters", details) {}
24};
25
30public:
37 explicit InvalidStartCharacterException(const std::string& context, const std::string& details = "") :
38 NmeaException(context, "NMEA 0183 sentence must start with '$' or '!'", details) {}
39};
40
45public:
52 explicit NoChecksumException(const std::string& context, const std::string& details = "") :
53 NmeaException(context, "This sentence does not contain a checksum", details) {}
54};
55
60public:
67 explicit NoEndlineException(const std::string& context, const std::string& details = "") :
68 NmeaException(context, "NMEA 0183 sentence must end with <CR><LF>", details) {}
69};
70
75public:
82 explicit NotEnoughFieldsException(const std::string& context, const std::string& details = "") :
83 NmeaException(context, "NMEA 0183 sentence does not contain enough fields", details) {}
84};
85
99public:
103 Message0183(const Message0183&) = default;
104
109
113 Message0183(Message0183&&) noexcept = default;
114
118 Message0183& operator=(Message0183&&) noexcept = default;
119
123 ~Message0183() override = default;
124
130 std::unique_ptr<nmealib::Message> clone() const override;
131
137 char getStartChar() const noexcept;
138
144 std::string getTalker() const noexcept;
145
151 std::string getSentenceType() const noexcept;
152
161 std::string getPayload() const noexcept;
162
169 std::string getChecksumStr() const;
170
176 std::string getCalculatedChecksumStr() const noexcept;
177
184 virtual std::string getStringContent(bool verbose) const noexcept;
185
192 std::string serialize() const override;
193
201 bool operator==(const Message0183& other) const noexcept;
202
209 bool validate() const noexcept override;
210
217 static double convertNmeaCoordinateToDecimalDegrees(const std::string& nmeaCoordinate);
218
219protected:
221 std::string talker_;
222 std::string sentenceType_;
223 std::string payload_;
224 std::string checksumStr_;
226
239 static std::unique_ptr<Message0183> create(const std::string& raw, TimePoint ts = std::chrono::system_clock::now());
240
247 std::string toString(bool verbose) const noexcept;
248
249private:
250 explicit Message0183(std::string raw,
251 TimePoint ts,
252 char startChar,
253 std::string talker,
254 std::string sentenceType,
255 std::string payload) noexcept;
256 explicit Message0183(std::string raw,
257 TimePoint ts,
258 char startChar,
259 std::string talker,
260 std::string sentenceType,
261 std::string payload,
262 std::string checksumStr) noexcept;
263
264 static std::string computeChecksum(const std::string& payload) noexcept;
265 static bool isHexByte(const std::string& s) noexcept;
266 static bool validateFormat(const std::string& context, const std::string& raw);
267
268 friend class Nmea0183Factory;
269};
270
271} // namespace nmea0183
272} // namespace nmealib
Defines a base class for NMEA messages, encapsulating common properties and behaviors.
Definition message.h:14
std::chrono::system_clock::time_point TimePoint
Definition message.h:30
Base exception class for all NMEA library errors.
Exception thrown when an NMEA 0183 sentence does not start with '$' or '!'.
Definition nmea0183.h:29
InvalidStartCharacterException(const std::string &context, const std::string &details="")
Constructs an InvalidStartCharacterException.
Definition nmea0183.h:37
Represents an NMEA 0183 sentence.
Definition nmea0183.h:98
bool validate() const noexcept override
Returns whether the message is valid or not.
Definition nmea0183.cpp:172
std::string serialize() const override
Returns the wire-format representation of the NMEA 0183 sentence, that is, the raw information that w...
Definition nmea0183.cpp:168
std::string getChecksumStr() const
Get the checksum string extracted from the raw sentence.
Definition nmea0183.cpp:125
std::string getSentenceType() const noexcept
Returns the sentence type identifier extracted from the sentence.
Definition nmea0183.cpp:121
virtual std::string getStringContent(bool verbose) const noexcept
Returns a human-readable string representation of the message content.
Definition nmea0183.cpp:136
Message0183(Message0183 &&) noexcept=default
Move constructor.
std::unique_ptr< nmealib::Message > clone() const override
Creates a polymorphic deep copy of this Message0183.
Definition nmea0183.cpp:105
std::string getTalker() const noexcept
Returns the talker identifier extracted from the sentence.
Definition nmea0183.cpp:117
std::string getPayload() const noexcept
Returns the payload of the NMEA 0183 sentence.
Definition nmea0183.cpp:109
static double convertNmeaCoordinateToDecimalDegrees(const std::string &nmeaCoordinate)
Converts an NMEA coordinate in ddmm.mmmm / dddmm.mmmm format to decimal degrees.
Definition nmea0183.cpp:194
Message0183(const Message0183 &)=default
Copy constructor.
char getStartChar() const noexcept
Returns the start character of the NMEA 0183 sentence.
Definition nmea0183.cpp:113
std::string getCalculatedChecksumStr() const noexcept
Get the calculated checksum string for the sentence payload.
Definition nmea0183.cpp:132
std::string toString(bool verbose) const noexcept
Provides the common string representation for every NMEA 0183 message, which can be used by derived c...
Definition nmea0183.cpp:147
Message0183 & operator=(const Message0183 &)=default
Copy assignment operator.
static std::unique_ptr< Message0183 > create(const std::string &raw, TimePoint ts=std::chrono::system_clock::now())
Protected factory method to create a Message0183 from a raw sentence string.
Definition nmea0183.cpp:47
Factory for creating typed NMEA 0183 message objects from raw sentence strings.
Exception thrown when an operation requires a checksum but none is present in the sentence.
Definition nmea0183.h:44
NoChecksumException(const std::string &context, const std::string &details="")
Constructs a NoChecksumException.
Definition nmea0183.h:52
Exception thrown when an NMEA 0183 sentence is missing the required <CR><LF> terminator.
Definition nmea0183.h:59
NoEndlineException(const std::string &context, const std::string &details="")
Constructs a NoEndlineException.
Definition nmea0183.h:67
Exception thrown when an NMEA 0183 sentence does not contain enough comma-separated fields.
Definition nmea0183.h:74
NotEnoughFieldsException(const std::string &context, const std::string &details="")
Constructs a NotEnoughFieldsException.
Definition nmea0183.h:82
Exception thrown when an NMEA 0183 sentence exceeds the maximum allowed length of 82 characters.
Definition nmea0183.h:14
TooLongSentenceException(const std::string &context, const std::string &details="")
Constructs a TooLongSentenceException.
Definition nmea0183.h:22