Goby3  3.1.5
2024.05.14
waveglider_sv2_codecs.h
Go to the documentation of this file.
1 // Copyright 2017-2021:
2 // GobySoft, LLC (2013-)
3 // Community contributors (see AUTHORS file)
4 // File authors:
5 // Toby Schneider <toby@gobysoft.org>
6 //
7 //
8 // This file is part of the Goby Underwater Autonomy Project Libraries
9 // ("The Goby Libraries").
10 //
11 // The Goby Libraries are free software: you can redistribute them and/or modify
12 // them under the terms of the GNU Lesser General Public License as published by
13 // the Free Software Foundation, either version 2.1 of the License, or
14 // (at your option) any later version.
15 //
16 // The Goby Libraries are distributed in the hope that they will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU Lesser General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public License
22 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
23 
24 #ifndef GOBY_MIDDLEWARE_FRONTSEAT_WAVEGLIDER_WAVEGLIDER_SV2_CODECS_H
25 #define GOBY_MIDDLEWARE_FRONTSEAT_WAVEGLIDER_WAVEGLIDER_SV2_CODECS_H
26 
27 #include <dccl/bitset.h> // for Bitset
28 #include <dccl/common.h> // for uint32, BITS_IN_BYTE, uint64
29 #include <dccl/field_codec.h> // for FieldCodecBase
30 #include <dccl/field_codec_fixed.h> // for TypedFixedFieldCodec
31 #include <dccl/field_codec_id.h> // for DefaultIdentifierCodec
32 
33 namespace dccl
34 {
35 class Codec;
36 } // namespace dccl
37 
38 extern "C"
39 {
40  void dccl3_load(dccl::Codec* dccl);
41  void dccl3_unload(dccl::Codec* dccl);
42 }
43 
44 namespace goby
45 {
46 namespace middleware
47 {
48 namespace frontseat
49 {
50 class SV2IdentifierCodec : public dccl::DefaultIdentifierCodec
51 {
52  private:
53  dccl::Bitset encode() override { return encode(0); }
54 
55  dccl::Bitset encode(const dccl::uint32& wire_value) override
56  {
57  return dccl::Bitset(size(), wire_value - 0x7E0000);
58  }
59 
60  dccl::uint32 decode(dccl::Bitset* bits) override { return 0x7E0000 + bits->to<dccl::uint32>(); }
61 
62  unsigned size() override { return 2 * dccl::BITS_IN_BYTE; }
63 
64  unsigned size(const dccl::uint32& /*field_value*/) override { return size(); }
65 
66  unsigned max_size() override { return size(); }
67 
68  unsigned min_size() override { return size(); }
69 };
70 
71 template <typename Integer> class SV2NumericCodec : public dccl::TypedFixedFieldCodec<Integer>
72 {
73  private:
74  unsigned size() override
75  {
76  dccl::uint64 v = dccl::FieldCodecBase::dccl_field_options().max() + 1;
77  unsigned r = 0;
78  while (v >>= 1) r++;
79  return r;
80  }
81 
82  dccl::Bitset encode() override { return dccl::Bitset(size()); }
83 
84  // this works because both DCCL and the SV2 protocol uses little-endian representation
85  dccl::Bitset encode(const Integer& i) override
86  {
87  dccl::Bitset b;
88  b.from<Integer>(i, size());
89  return b;
90  }
91 
92  Integer decode(dccl::Bitset* bits) override { return bits->to<Integer>(); }
93 
94  void validate() override {}
95 };
96 } // namespace frontseat
97 } // namespace middleware
98 } // namespace goby
99 
100 #endif
goby::acomms::Bitset
dccl::Bitset Bitset
Definition: dccl.h:126
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
dccl
Definition: mm_driver.h:44
dccl3_unload
void dccl3_unload(dccl::Codec *dccl)
dccl3_load
void dccl3_load(dccl::Codec *dccl)
goby::uint64
std::uint64_t uint64
Definition: primitive_types.h:34
goby::uint32
std::uint32_t uint32
Definition: primitive_types.h:32
goby::acomms::BITS_IN_BYTE
constexpr unsigned BITS_IN_BYTE
Definition: acomms_constants.h:38
goby::middleware::frontseat::SV2NumericCodec
Definition: waveglider_sv2_codecs.h:71
goby::middleware::frontseat::SV2IdentifierCodec
Definition: waveglider_sv2_codecs.h:50