nmealib 0.0.4
NMEA 0183/NMEA 2000 parsing library
Loading...
Searching...
No Matches
nmea2000.h
Go to the documentation of this file.
1#pragma once
2
3#include "message.h"
4#include "nmeaException.h"
5
6#include <chrono>
7#include <cstdint>
8#include <memory>
9#include <string>
10#include <vector>
11
12namespace nmealib {
13namespace nmea2000 {
14
19public:
20 explicit InvalidPgnException(const std::string& context, const std::string& details = "") :
21 NmeaException(context, "NMEA 2000 PGN is invalid", details) {}
22};
23
28public:
29 explicit InvalidCanFrameException(const std::string& context, const std::string& details = "") :
30 NmeaException(context, "NMEA 2000 CAN frame is invalid", details) {}
31};
32
37public:
38 explicit FrameTooLongException(const std::string& context, const std::string& details = "") :
39 NmeaException(context, "NMEA 2000 CAN frame exceeds maximum length of 223 bytes", details) {}
40};
41
63public:
64 Message2000(const Message2000&) = default;
65 Message2000& operator=(const Message2000&) = default;
66 Message2000(Message2000&&) noexcept = default;
67 Message2000& operator=(Message2000&&) = default;
68 ~Message2000() override = default;
69
73 std::unique_ptr<nmealib::Message> clone() const override;
74
75 // -------------------------------------------------------------------------
76 // Raw data accessors
77 // -------------------------------------------------------------------------
78
82 const std::vector<uint8_t>& getCanId() const noexcept;
83
87 const std::vector<uint8_t>& getCanFrame() const noexcept;
88
92 uint8_t getCanFrameLength() const noexcept;
93
94 // -------------------------------------------------------------------------
95 // CAN Id field accessors
96 //
97 // canId_[0]: [ 0 0 0 P3 P2 P1 R1 DP ]
98 // canId_[1]: PDU Format (PF8..PF1)
99 // canId_[2]: PDU Specific (PS8..PS1)
100 // canId_[3]: Source Address (SA8..SA1)
101 // -------------------------------------------------------------------------
102
106 uint8_t getPriority() const noexcept;
107
111 bool getPriority3() const noexcept;
112 bool getPriority2() const noexcept;
113 bool getPriority1() const noexcept;
114
118 bool getHeaderReserved() const noexcept;
119
123 bool getDataPage() const noexcept;
124
132 uint8_t getPDUFormat() const noexcept;
133
139 uint8_t getPDUSpecific() const noexcept;
140
146 uint8_t getSourceAddress() const noexcept;
147
154 uint8_t getDestinationAddress() const noexcept;
155
156 // NOTE: getRemoteTransmissionRequest() and getDataLengthCode() have been
157 // removed. RTR and DLC are CAN bus framing fields handled by hardware/drivers
158 // and are not encoded in the 29-bit CAN Id.
159
160 // -------------------------------------------------------------------------
161 // PGN accessor
162 // -------------------------------------------------------------------------
163
169 uint32_t getPgn() const noexcept;
170
171 // -------------------------------------------------------------------------
172 // Output
173 // -------------------------------------------------------------------------
174
181 virtual std::string getStringContent(bool verbose) const noexcept;
182
186 std::string serialize() const override;
187
188 // -------------------------------------------------------------------------
189 // Comparison and validation
190 // -------------------------------------------------------------------------
191
192 virtual bool operator==(const Message2000& other) const noexcept;
193
197 bool validate() const override;
198
199protected:
200 std::vector<uint8_t> canId_;
201 std::vector<uint8_t> canFrame_;
202
210 static std::unique_ptr<Message2000> create(std::string raw,
211 TimePoint ts = std::chrono::system_clock::now());
212
216 std::string toString(bool verbose) const noexcept;
217
218private:
219 explicit Message2000(std::string raw,
220 TimePoint ts,
221 std::vector<uint8_t> canId,
222 std::vector<uint8_t> canFrame) noexcept;
223
229 static uint32_t extractPgnFromCanId(const std::vector<uint8_t>& canId) noexcept;
230
234 static bool isValidPgn(uint32_t pgn) noexcept;
235
236 friend class Nmea2000Factory;
237};
238
239} // namespace nmea2000
240} // 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 a CAN frame exceeds the maximum length of 223 bytes.
Definition nmea2000.h:36
FrameTooLongException(const std::string &context, const std::string &details="")
Definition nmea2000.h:38
Exception thrown when the CAN frame is invalid.
Definition nmea2000.h:27
InvalidCanFrameException(const std::string &context, const std::string &details="")
Definition nmea2000.h:29
Exception thrown when an NMEA 2000 PGN is outside the valid range.
Definition nmea2000.h:18
InvalidPgnException(const std::string &context, const std::string &details="")
Definition nmea2000.h:20
Represents a generic NMEA 2000 message encapsulating a CAN frame.
Definition nmea2000.h:62
uint8_t getSourceAddress() const noexcept
Returns the source address of the transmitting device.
Definition nmea2000.cpp:279
Message2000(const Message2000 &)=default
std::vector< uint8_t > canId_
29-bit CAN Id stored as 4 bytes (big-endian)
Definition nmea2000.h:200
Message2000(Message2000 &&) noexcept=default
const std::vector< uint8_t > & getCanFrame() const noexcept
Returns the raw CAN frame payload (0-223 bytes).
Definition nmea2000.cpp:308
bool getPriority2() const noexcept
Bit 3 of canId_[0].
Definition nmea2000.cpp:262
std::string serialize() const override
Serializes the message to "CANID:data" hex format.
Definition nmea2000.cpp:380
std::vector< uint8_t > canFrame_
CAN frame payload (0-223 bytes)
Definition nmea2000.h:201
uint32_t getPgn() const noexcept
Returns the PGN (Parameter Group Number) extracted from the CAN Id.
Definition nmea2000.cpp:296
bool getPriority3() const noexcept
Returns individual priority bits (P3 is the MSB).
Definition nmea2000.cpp:261
uint8_t getPDUSpecific() const noexcept
Returns the PDU Specific byte (PS).
Definition nmea2000.cpp:276
bool validate() const override
Returns true if the PGN fits in 18 bits and the frame is 0-223 bytes.
Definition nmea2000.cpp:407
bool getHeaderReserved() const noexcept
Returns the Reserved bit (R1).
Definition nmea2000.cpp:266
bool getDataPage() const noexcept
Returns the Data Page bit (DP).
Definition nmea2000.cpp:269
std::string toString(bool verbose) const noexcept
Returns the base string common to all PGNs (used by getStringContent).
Definition nmea2000.cpp:337
std::unique_ptr< nmealib::Message > clone() const override
Creates a polymorphic deep copy of this Message2000.
Definition nmea2000.cpp:242
uint8_t getPDUFormat() const noexcept
Returns the PDU Format byte (PF).
Definition nmea2000.cpp:272
static std::unique_ptr< Message2000 > create(std::string raw, TimePoint ts=std::chrono::system_clock::now())
Protected factory — parses "CANID:data" (and variant formats) into a Message2000.
Definition nmea2000.cpp:109
virtual std::string getStringContent(bool verbose) const noexcept
Returns a human-readable string representation of the message.
Definition nmea2000.cpp:320
const std::vector< uint8_t > & getCanId() const noexcept
Returns the 29-bit CAN identifier as 4 bytes (big-endian).
Definition nmea2000.cpp:304
uint8_t getCanFrameLength() const noexcept
Returns the number of bytes in the CAN frame payload.
Definition nmea2000.cpp:312
Message2000 & operator=(const Message2000 &)=default
bool getPriority1() const noexcept
Bit 2 of canId_[0].
Definition nmea2000.cpp:263
uint8_t getDestinationAddress() const noexcept
Returns the destination address.
Definition nmea2000.cpp:283
uint8_t getPriority() const noexcept
Returns the 3-bit message priority (0 = highest, 7 = lowest).
Definition nmea2000.cpp:256