AIS: initialization of message fields modernized.
[marnav.git] / src / marnav / ais / message_24.cpp
blob180a39be17c09202decd71a5bdf0749d5806f266
1 #include "message_24.hpp"
2 #include <type_traits>
3 #include <marnav/utils/unique.hpp>
4 #include <marnav/utils/mmsi.hpp>
6 namespace marnav
8 namespace ais
11 message_24::message_24()
12 : message_24(ID)
16 message_24::message_24(message_id id)
17 : message(id)
18 , shipname("@@@@@@@@@@@@@@@@@@@@")
19 , vendor_id("@@@")
20 , callsign("@@@@@@@")
24 std::unique_ptr<message> message_24::parse(const raw & bits)
26 if ((bits.size() != SIZE_BITS) && (bits.size() != SIZE_BITS_IGNORED_SPARES_OF_TYPE_A))
27 throw std::invalid_argument{"invalid number of bits in message_24::parse"};
29 uint32_t part = 3;
30 bits.get(part, 38, 2);
32 if (part > 1)
33 throw std::invalid_argument{"invalid part number message_24::parse"};
35 std::unique_ptr<message> result = utils::make_unique<message_24>();
36 message_24 & msg = static_cast<message_24 &>(*result);
38 msg.read_data(bits);
40 return result;
43 bool message_24::is_auxiliary_vessel() const { return utils::mmsi{mmsi}.is_auxiliary(); }
45 void message_24::read_data(const raw & bits)
47 bits.get(repeat_indicator, 6, 2);
48 bits.get(mmsi, 8, 30);
50 std::underlying_type<part>::type tmp = 3;
51 bits.get(tmp, 38, 2);
52 part_number = static_cast<part>(tmp);
54 if (part_number == part::A) {
55 shipname = read_string(bits, 40, 20);
56 // spare: 160 - 167
57 } else {
58 bits.get(shiptype, 40, 8);
59 vendor_id = read_string(bits, 48, 3);
60 bits.get(model, 66, 4);
61 bits.get(serial, 70, 20);
62 callsign = read_string(bits, 90, 7);
63 if (is_auxiliary_vessel()) {
64 bits.get(mothership_mmsi, 132, 30);
65 } else {
66 bits.get(to_bow, 132, 9);
67 bits.get(to_stern, 141, 9);
68 bits.get(to_port, 150, 6);
69 bits.get(to_starboard, 156, 6);
71 // spare: 162 - 167
75 raw message_24::get_data() const
77 raw bits{SIZE_BITS};
79 bits.set(type(), 0, 6);
80 bits.set(repeat_indicator, 6, 2);
81 bits.set(mmsi, 8, 30);
82 bits.set(static_cast<std::underlying_type<part>::type>(part_number), 38, 2);
84 if (part_number == part::A) {
85 write_string(bits, 40, 20, shipname);
86 } else {
87 bits.set(shiptype, 40, 8);
88 write_string(bits, 48, 3, vendor_id);
89 bits.set(model, 66, 4);
90 bits.set(serial, 70, 20);
91 write_string(bits, 90, 7, callsign);
92 if (is_auxiliary_vessel()) {
93 bits.set(mothership_mmsi, 132, 30);
94 } else {
95 bits.set(to_bow, 132, 9);
96 bits.set(to_stern, 141, 9);
97 bits.set(to_port, 150, 6);
98 bits.set(to_starboard, 156, 6);
102 return bits;