NMEA: detectio of the optional tag block in front of a sentence, ignoring it for...
[marnav.git] / src / marnav / ais / message_01.cpp
blob00ccfb761792f62d27cd1be7506a5f1e4dcf11fa
1 #include "message_01.hpp"
2 #include <cmath>
3 #include <marnav/ais/angle.hpp>
5 namespace marnav
7 namespace ais
9 MARNAV_AIS_DEFINE_MESSAGE_PARSE_FUNC(message_01)
11 message_01::message_01()
12 : message_01(ID)
16 message_01::message_01(message_id id)
17 : message(id)
21 message_01::message_01(const raw & bits)
22 : message_01(ID)
24 if (bits.size() != SIZE_BITS)
25 throw std::invalid_argument{"invalid number of bits in ais/message_01"};
26 read_data(bits);
29 utils::optional<geo::longitude> message_01::get_longitude() const
31 if (longitude_minutes == longitude_not_available)
32 return utils::make_optional<geo::longitude>();
33 return to_geo_longitude(longitude_minutes, longitude_minutes.count, angle_scale::I4);
36 utils::optional<geo::latitude> message_01::get_latitude() const
38 if (latitude_minutes == latitude_not_available)
39 return utils::make_optional<geo::latitude>();
40 return to_geo_latitude(latitude_minutes, latitude_minutes.count, angle_scale::I4);
43 void message_01::set_longitude(const utils::optional<geo::longitude> & t)
45 longitude_minutes = t
46 ? to_longitude_minutes(t.value(), longitude_minutes.count, angle_scale::I4)
47 : longitude_not_available;
50 void message_01::set_latitude(const utils::optional<geo::latitude> & t)
52 latitude_minutes = t
53 ? to_latitude_minutes(t.value(), latitude_minutes.count, angle_scale::I4)
54 : latitude_not_available;
57 /// Returns speed in knots.
58 utils::optional<double> message_01::get_sog() const noexcept
60 if (sog == sog_not_available)
61 return {};
62 return 0.1 * sog;
65 void message_01::set_sog(utils::optional<double> t) noexcept
67 sog = !t ? sog_not_available : static_cast<uint32_t>(std::round(*t / 0.1));
70 /// Returns course over ground in degrees true north.
71 utils::optional<double> message_01::get_cog() const noexcept
73 if (cog == cog_not_available)
74 return {};
75 return 0.1 * cog;
78 void message_01::set_cog(utils::optional<double> t) noexcept
80 cog = !t ? cog_not_available : static_cast<uint32_t>(std::round(*t / 0.1));
83 /// Returns heading in degrees.
84 utils::optional<uint32_t> message_01::get_hdg() const noexcept
86 if (hdg == hdg_not_available)
87 return {};
88 return {hdg};
91 void message_01::set_hdg(utils::optional<uint32_t> t) noexcept
93 hdg = !t ? hdg_not_available : *t;
96 void message_01::read_data(const raw & bits)
98 get(bits, repeat_indicator);
99 get(bits, mmsi);
100 get(bits, nav_status);
101 get(bits, rot);
102 get(bits, sog);
103 get(bits, position_accuracy);
104 get(bits, longitude_minutes);
105 get(bits, latitude_minutes);
106 get(bits, cog);
107 get(bits, hdg);
108 get(bits, timestamp);
109 get(bits, maneuver_indicator);
110 get(bits, raim);
111 get(bits, radio_status);
114 raw message_01::get_data() const
116 raw bits{SIZE_BITS};
118 bits.set(type(), 0, 6);
119 set(bits, repeat_indicator);
120 set(bits, mmsi);
121 set(bits, nav_status);
122 set(bits, rot);
123 set(bits, sog);
124 set(bits, position_accuracy);
125 set(bits, longitude_minutes);
126 set(bits, latitude_minutes);
127 set(bits, cog);
128 set(bits, hdg);
129 set(bits, timestamp);
130 set(bits, maneuver_indicator);
131 set(bits, raim);
132 set(bits, radio_status);
134 return bits;