DCCL v4
ccl_compatibility.h
1 // Copyright 2012-2023:
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 // File authors:
6 // Toby Schneider <toby@gobysoft.org>
7 //
8 //
9 // This file is part of the Dynamic Compact Control Language Library
10 // ("DCCL").
11 //
12 // DCCL is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as published by
14 // the Free Software Foundation, either version 2.1 of the License, or
15 // (at your option) any later version.
16 //
17 // DCCL is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public License
23 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
24 #ifndef DCCLCCLCOMPATIBILITY20120426H
25 #define DCCLCCLCOMPATIBILITY20120426H
26 
27 #include "../codecs2/field_codec_default.h"
28 #include "../field_codec_id.h"
29 #include "dccl/ccl/protobuf/ccl.pb.h"
30 #include "dccl/ccl/protobuf/ccl_extensions.pb.h"
31 
32 extern "C"
33 {
34  void dccl3_load(dccl::Codec* dccl);
35 }
36 
37 namespace dccl
38 {
40 namespace legacyccl
41 {
42 const unsigned char DCCL_CCL_HEADER = 32;
43 
45 {
46  private:
47  dccl::Bitset encode() override { return encode(0); }
48 
49  dccl::Bitset encode(const dccl::uint32& wire_value) override
50  {
51  if ((wire_value & 0xFFFF0000) == CCL_DCCL_ID_PREFIX)
52  {
53  // CCL message
54  return dccl::Bitset(dccl::BITS_IN_BYTE, wire_value & 0x0000FFFF);
55  }
56  else
57  {
58  // DCCL message
59  return dccl::DefaultIdentifierCodec::encode(wire_value)
60  .prepend(dccl::Bitset(dccl::BITS_IN_BYTE, DCCL_CCL_HEADER));
61  }
62  }
63 
64  dccl::uint32 decode(dccl::Bitset* bits) override
65  {
66  unsigned ccl_id = bits->to_ulong();
67 
68  if (ccl_id == DCCL_CCL_HEADER)
69  {
70  // DCCL message
72  (*bits) >>= dccl::BITS_IN_BYTE;
75  }
76  else
77  {
78  // CCL message
79  return CCL_DCCL_ID_PREFIX + ccl_id;
80  }
81  }
82 
83  unsigned size() override { return size(0); }
84 
85  unsigned size(const dccl::uint32& field_value) override
86  {
87  if ((field_value & 0xFFFF0000) == CCL_DCCL_ID_PREFIX)
88  {
89  // CCL message
90  return dccl::BITS_IN_BYTE;
91  }
92  else
93  {
94  return dccl::BITS_IN_BYTE + dccl::DefaultIdentifierCodec::size(field_value);
95  }
96  }
97 
98  unsigned max_size() override
99  {
100  return dccl::BITS_IN_BYTE + dccl::DefaultIdentifierCodec::max_size();
101  }
102 
103  unsigned min_size() override { return dccl::BITS_IN_BYTE; }
104 
105  // prefixes (dccl.msg).id to indicate that this DCCL
106  // message is an encoding of a legacy CCL message
107  enum
108  {
109  CCL_DCCL_ID_PREFIX = 0x0CC10000
110  };
111 };
112 
114 {
115  private:
116  dccl::Bitset encode() override;
117  dccl::Bitset encode(const double& wire_value) override;
118  double decode(dccl::Bitset* bits) override;
119  unsigned size() override;
120  enum
121  {
122  LATLON_COMPRESSED_BYTE_SIZE = 3
123  };
124 };
125 
127 {
128  private:
129  dccl::Bitset encode() override { return encode((dccl::uint32)max()); }
130 
131  dccl::Bitset encode(const dccl::uint32& wire_value) override
132  {
134  (dccl::uint32)std::min<unsigned char>((dccl::uint32)max(), wire_value / SCALE_FACTOR));
135  }
136 
137  dccl::uint32 decode(dccl::Bitset* bits) override
138  {
140  }
141 
142  double max() override { return (1 << dccl::BITS_IN_BYTE) - 1; }
143  double min() override { return 0; }
144  void validate() override {}
145 
146  enum
147  {
148  SCALE_FACTOR = 4
149  };
150 };
151 
152 class TimeDateCodec : public dccl::TypedFixedFieldCodec<dccl::uint64>
153 {
154  public:
155  static dccl::uint64 to_uint64_time(const std::time_t& time_date);
156 
157  private:
158  dccl::Bitset encode() override;
159  dccl::Bitset encode(const dccl::uint64& wire_value) override;
160  dccl::uint64 decode(dccl::Bitset* bits) override;
161  unsigned size() override;
162 
163  enum
164  {
165  MICROSECONDS_IN_SECOND = 1000000
166  };
167  enum
168  {
169  TIME_DATE_COMPRESSED_BYTE_SIZE = 3
170  };
171 };
172 
174 {
175  private:
176  dccl::Bitset encode() override { return encode(0); }
177  dccl::Bitset encode(const float& wire_value) override;
178  float decode(dccl::Bitset* bits) override;
179  unsigned size() override { return dccl::BITS_IN_BYTE; }
180 };
181 
183 {
184  private:
185  dccl::Bitset encode() override { return encode(0); }
186  dccl::Bitset encode(const float& wire_value) override;
187  float decode(dccl::Bitset* bits) override;
188  unsigned size() override { return HI_RES_ALTITUDE_COMPRESSED_BYTE_SIZE * dccl::BITS_IN_BYTE; }
189  enum
190  {
191  HI_RES_ALTITUDE_COMPRESSED_BYTE_SIZE = 2
192  };
193 };
194 
196 {
197  private:
198  dccl::Bitset encode() override { return encode(0); }
199  dccl::Bitset encode(const float& wire_value) override;
200  float decode(dccl::Bitset* bits) override;
201  unsigned size() override
202  {
203  return FieldCodecBase::dccl_field_options().GetExtension(ccl).bit_size();
204  }
205 
206  void validate() override
207  {
209  FieldCodecBase::dccl_field_options().GetExtension(ccl).has_bit_size(),
210  "missing (dccl.field).ccl.bit_size");
211  }
212 };
213 
215 {
216  private:
217  dccl::Bitset encode() override { return encode(0); }
218  dccl::Bitset encode(const float& wire_value) override;
219  float decode(dccl::Bitset* bits) override;
220  unsigned size() override { return dccl::BITS_IN_BYTE; }
221 };
222 
224 {
225  private:
226  dccl::Bitset encode() override { return encode(0); }
227  dccl::Bitset encode(const float& wire_value) override;
228  float decode(dccl::Bitset* bits) override;
229  unsigned size() override { return dccl::BITS_IN_BYTE; }
230 
231  void validate() override
232  {
234  FieldCodecBase::dccl_field_options().GetExtension(ccl).has_thrust_mode_tag(),
235  "missing (dccl.field).ccl.thrust_mode_tag");
236  }
237 };
238 
240 {
241  private:
242  dccl::Bitset encode() override { return encode(0); }
243  dccl::Bitset encode(const float& wire_value) override;
244  float decode(dccl::Bitset* bits) override;
245  unsigned size() override { return dccl::BITS_IN_BYTE; }
246 };
247 
248 class GFIPitchOilCodec : public dccl::TypedFixedFieldCodec<protobuf::CCLMDATState::GFIPitchOil>
249 {
250  private:
251  dccl::Bitset encode() override { return encode(protobuf::CCLMDATState::GFIPitchOil()); }
252  dccl::Bitset encode(const protobuf::CCLMDATState::GFIPitchOil& wire_value) override;
253  protobuf::CCLMDATState::GFIPitchOil decode(dccl::Bitset* bits) override;
254  unsigned size() override { return GFI_PITCH_OIL_COMPRESSED_BYTE_SIZE * dccl::BITS_IN_BYTE; }
255  enum
256  {
257  GFI_PITCH_OIL_COMPRESSED_BYTE_SIZE = 2
258  };
259 };
260 
262 {
263  private:
264  dccl::Bitset encode() override { return encode(0); }
265  dccl::Bitset encode(const float& wire_value) override;
266  float decode(dccl::Bitset* bits) override;
267  unsigned size() override { return dccl::BITS_IN_BYTE; }
268 };
269 
271 {
272  private:
273  dccl::Bitset encode() override { return encode(0); }
274  dccl::Bitset encode(const float& wire_value) override;
275  float decode(dccl::Bitset* bits) override;
276  unsigned size() override { return dccl::BITS_IN_BYTE; }
277 };
278 
280 {
281  private:
282  dccl::Bitset encode() override { return encode(0); }
283  dccl::Bitset encode(const float& wire_value) override;
284  float decode(dccl::Bitset* bits) override;
285  unsigned size() override { return dccl::BITS_IN_BYTE; }
286 };
287 
288 } // namespace legacyccl
289 } // namespace dccl
290 
291 #endif
dccl::legacyccl::IdentifierCodec
Definition: ccl_compatibility.h:44
dccl::legacyccl::HiResAltitudeCodec
Definition: ccl_compatibility.h:182
dccl::legacyccl::SpeedCodec
Definition: ccl_compatibility.h:223
dccl::v2::DefaultNumericFieldCodec::encode
Bitset encode() override
Encode an empty field.
Definition: field_codec_default.h:163
dccl::DefaultIdentifierCodec::size
unsigned size() override
Calculate the size (in bits) of an empty field.
Definition: field_codec_id.cpp:68
dccl::legacyccl::VelocityCodec
Definition: ccl_compatibility.h:214
dccl::uint64
google::protobuf::uint64 uint64
an unsigned 64 bit integer
Definition: common.h:60
dccl::Bitset::to_ulong
unsigned long to_ulong() const
Returns the value of the Bitset as an unsigned long integer. Equivalent to to<unsigned long>().
Definition: bitset.h:275
dccl::Bitset::get_more_bits
void get_more_bits(size_type num_bits)
Retrieve more bits from the parent Bitset.
Definition: bitset.h:420
dccl
Dynamic Compact Control Language namespace.
Definition: any.h:46
dccl::legacyccl::TemperatureCodec
Definition: ccl_compatibility.h:270
dccl::legacyccl::DepthCodec
Definition: ccl_compatibility.h:195
dccl::TypedFixedFieldCodec
Base class for static-typed field encoders/decoders that use a fixed number of bits on the wire regar...
Definition: field_codec_fixed.h:36
dccl::DefaultIdentifierCodec::min_size
unsigned min_size() override
Calculate minimum size of the field in bits.
Definition: field_codec_id.cpp:85
dccl::legacyccl::TimeDateCodec
Definition: ccl_compatibility.h:152
dccl::uint32
google::protobuf::uint32 uint32
an unsigned 32 bit integer
Definition: common.h:56
dccl::FieldCodecBase::require
void require(bool b, const std::string &description)
Essentially an assertion to be used in the validate() virtual method.
Definition: field_codec.h:348
dccl::Codec
The Dynamic CCL enCODer/DECoder. This is the main class you will use to load, encode and decode DCCL ...
Definition: codec.h:62
dccl::DefaultIdentifierCodec::max_size
unsigned max_size() override
Calculate maximum size of the field in bits.
Definition: field_codec_id.cpp:83
dccl::DefaultIdentifierCodec::decode
uint32 decode(Bitset *bits) override
Decode a field. If the field is empty (i.e. was encoded using the zero-argument encode()),...
Definition: field_codec_id.cpp:49
dccl::legacyccl::LatLonCompressedCodec
Definition: ccl_compatibility.h:113
dccl::legacyccl::HeadingCodec
Definition: ccl_compatibility.h:173
dccl::DefaultIdentifierCodec::encode
Bitset encode() override
Encode an empty field.
Definition: field_codec_id.cpp:30
dccl::Bitset
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy....
Definition: bitset.h:42
dccl::legacyccl::GFIPitchOilCodec
Definition: ccl_compatibility.h:248
dccl::DefaultIdentifierCodec
Provides the default 1 byte or 2 byte DCCL ID codec.
Definition: field_codec_id.h:29
dccl::Bitset::prepend
Bitset & prepend(const Bitset &bits)
Adds the bitset to the little end.
Definition: bitset.h:353
dccl::legacyccl::FixAgeCodec
Definition: ccl_compatibility.h:126
dccl::legacyccl::WattsCodec
Definition: ccl_compatibility.h:239
dccl::legacyccl::SalinityCodec
Definition: ccl_compatibility.h:261
dccl::v2::DefaultNumericFieldCodec
Provides a basic bounded arbitrary length numeric (double, float, uint32, uint64, int32,...
Definition: field_codec_default.h:51
dccl::legacyccl::SoundSpeedCodec
Definition: ccl_compatibility.h:279
dccl::v2::DefaultNumericFieldCodec::decode
WireType decode(Bitset *bits) override
Decode a field. If the field is empty (i.e. was encoded using the zero-argument encode()),...
Definition: field_codec_default.h:206
dccl::FieldCodecBase::dccl_field_options
dccl::DCCLFieldOptions dccl_field_options() const
Get the DCCL field option extension value for the current field.
Definition: field_codec.h:334
dccl::legacyccl::protobuf::CCLMDATState_GFIPitchOil
Definition: ccl.pb.h:1381