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