nmealib 0.0.4
NMEA 0183/NMEA 2000 parsing library
Loading...
Searching...
No Matches
PGN129025.cpp
Go to the documentation of this file.
2
4
5namespace nmealib {
6namespace nmea2000 {
7
8std::unique_ptr<PGN129025> PGN129025::create(std::unique_ptr<Message2000> baseMessage) {
9 if (baseMessage->getCanFrameLength() != 8) {
10 std::string context = "PGN129025::create()";
11 NMEALIB_RETURN_ERROR(InvalidCanFrameException(context, "CAN frame must be 8 bytes for PGN129025"));
12 }
13
14 const int32_t latitudeRaw = static_cast<int32_t>(
15 static_cast<uint32_t>(baseMessage->getCanFrame()[0]) |
16 (static_cast<uint32_t>(baseMessage->getCanFrame()[1]) << 8) |
17 (static_cast<uint32_t>(baseMessage->getCanFrame()[2]) << 16) |
18 (static_cast<uint32_t>(baseMessage->getCanFrame()[3]) << 24));
19
20 const int32_t longitudeRaw = static_cast<int32_t>(
21 static_cast<uint32_t>(baseMessage->getCanFrame()[4]) |
22 (static_cast<uint32_t>(baseMessage->getCanFrame()[5]) << 8) |
23 (static_cast<uint32_t>(baseMessage->getCanFrame()[6]) << 16) |
24 (static_cast<uint32_t>(baseMessage->getCanFrame()[7]) << 24));
25
26 Latitude latitude = Latitude::fromRaw(latitudeRaw);
27 Longitude longitude = Longitude::fromRaw(longitudeRaw);
28
29 return std::unique_ptr<PGN129025>(new PGN129025(std::move(*baseMessage), latitude, longitude));
30}
31
32PGN129025::PGN129025(Message2000 baseMessage,
33 Latitude latitude,
34 Longitude longitude) noexcept :
35 Message2000(std::move(baseMessage)),
36 latitude_(latitude),
37 longitude_(longitude) {}
38
39PGN129025::PGN129025(Latitude latitude,
40 Longitude longitude) noexcept :
41 Message2000(*Message2000::create(rawPayload(latitude, longitude))),
42 latitude_(latitude),
43 longitude_(longitude) {}
44
45std::unique_ptr<Message> PGN129025::clone() const {
46 return std::unique_ptr<PGN129025>(new PGN129025(*this));
47}
48
49std::string PGN129025::getStringContent(bool verbose) const noexcept {
50 std::ostringstream oss;
51
52 if (verbose) {
53 oss << this->toString(true);
54 oss << "\n";
55 oss << "Fields:\n";
56 oss << "\tLatitude: " << latitude_.toString() << "°\n";
57 oss << "\tLongitude: " << longitude_.toString() << "°\n";
58 } else {
59 oss << this->toString(false);
60 oss << "Latitude=" << latitude_.toString() << "°"
61 << " Longitude=" << longitude_.toString() << "°";
62 }
63
64 return oss.str();
65}
66
67std::string PGN129025::rawPayload(Latitude latitude,
68 Longitude longitude) {
69 std::vector<uint8_t> canFrame(8, 0);
70
71 const auto latitudeRaw = static_cast<uint32_t>(latitude.getRaw());
72 canFrame[0] = static_cast<uint8_t>(latitudeRaw & 0xFF);
73 canFrame[1] = static_cast<uint8_t>((latitudeRaw >> 8) & 0xFF);
74 canFrame[2] = static_cast<uint8_t>((latitudeRaw >> 16) & 0xFF);
75 canFrame[3] = static_cast<uint8_t>((latitudeRaw >> 24) & 0xFF);
76
77 const auto longitudeRaw = static_cast<uint32_t>(longitude.getRaw());
78 canFrame[4] = static_cast<uint8_t>(longitudeRaw & 0xFF);
79 canFrame[5] = static_cast<uint8_t>((longitudeRaw >> 8) & 0xFF);
80 canFrame[6] = static_cast<uint8_t>((longitudeRaw >> 16) & 0xFF);
81 canFrame[7] = static_cast<uint8_t>((longitudeRaw >> 24) & 0xFF);
82
83 const uint32_t canId = (129025U << 8U);
84 std::ostringstream oss;
85 oss << std::hex << std::uppercase << std::setfill('0');
86 oss << std::setw(8) << canId << ":";
87 for (uint8_t b : canFrame) {
88 oss << std::setw(2) << static_cast<int>(b);
89 }
90 return oss.str();
91}
92
94 return latitude_;
95}
96
98 return longitude_;
99}
100
101bool PGN129025::operator==(const PGN129025& other) const noexcept {
102 return Message2000::operator==(other) &&
103 latitude_ == other.latitude_ &&
104 longitude_ == other.longitude_;
105}
106
107} // namespace nmea2000
108} // namespace nmealib
RawType getRaw() const noexcept
Converts physical value to raw integer representation.
Definition dataTypes.h:128
static constexpr DataType fromRaw(RawType raw) noexcept
Constructs from a raw value.
Definition dataTypes.h:97
Represents a generic NMEA 2000 message encapsulating a CAN frame.
Definition nmea2000.h:62
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 bool operator==(const Message2000 &other) const noexcept
Definition nmea2000.cpp:401
Strongly-typed class representing PGN 129025 - Position, Rapid Update.
Definition PGN129025.h:20
std::string getStringContent(bool verbose) const noexcept override
Returns a human-readable string representation of the message.
Definition PGN129025.cpp:49
bool operator==(const PGN129025 &other) const noexcept
Longitude getLongitude() const noexcept
Definition PGN129025.cpp:97
PGN129025(Latitude latitude, Longitude longitude) noexcept
Definition PGN129025.cpp:39
Latitude getLatitude() const noexcept
Definition PGN129025.cpp:93
std::unique_ptr< nmealib::Message > clone() const override
Creates a polymorphic deep copy of this Message2000.
Definition PGN129025.cpp:45
#define NMEALIB_RETURN_ERROR(exceptionExpr)
DataType< LongitudeTraits > Longitude
Custom type representing longitudes in degrees.
Definition dataTypes.h:382
DataType< LatitudeTraits > Latitude
Custom type representing latitudes in degrees.
Definition dataTypes.h:369