General: reorganization of header files
[marnav.git] / src / marnav / ais / message_12.cpp
blobe5c21f08e0f6dbed16e82f679150d0e1e1cc9a9d
1 #include <marnav/ais/message_12.hpp>
3 namespace marnav
5 namespace ais
7 constexpr message_id message_12::ID;
8 constexpr std::size_t message_12::SIZE_BITS_HEAD;
9 constexpr std::size_t message_12::SIZE_BITS_MAX;
10 constexpr std::size_t message_12::SIZE_TEXT_MAX;
12 message_12::message_12()
13 : message(ID)
17 message_12::message_12(const raw & bits)
18 : message(ID)
20 if ((bits.size() < SIZE_BITS_HEAD) || (bits.size() > SIZE_BITS_MAX))
21 throw std::invalid_argument{"invalid number of bits in ais/message_12"};
22 read_data(bits);
25 void message_12::read_data(const raw & bits)
27 get(bits, repeat_indicator);
28 get(bits, mmsi);
29 get(bits, seqno);
30 get(bits, dest_mmsi);
31 get(bits, retransmit);
33 auto rest = bits.size() - SIZE_BITS_HEAD;
34 if (rest > 0) {
35 rest = std::min(rest, SIZE_BITS_MAX - SIZE_BITS_HEAD);
36 text = read_string(bits, 72, rest / 6);
40 raw message_12::get_data() const
42 raw bits(SIZE_BITS_HEAD);
44 bits.set(type(), 0, 6);
45 set(bits, repeat_indicator);
46 set(bits, mmsi);
47 set(bits, seqno);
48 set(bits, dest_mmsi);
49 set(bits, retransmit);
51 if (text.size() > 0) {
52 // compute number of bits, must be on a 8-bit boundary
53 auto size = text.size() * 6;
54 if ((size % 8) != 0)
55 size += 8 - (size % 8);
57 if (size <= SIZE_BITS_MAX - SIZE_BITS_HEAD) {
58 // append a temporary bitset to the real one
59 raw t(size);
60 write_string(t, 0, text.size(), text);
61 bits.append(t);
65 return bits;
68 void message_12::set_seqno(uint32_t t)
70 if (t > 3u)
71 throw std::invalid_argument{"set_seqno out of range [0..3]"};
72 seqno = t;
75 void message_12::set_text(const std::string & t)
77 text = t.substr(0, std::min(t.size(), SIZE_TEXT_MAX));