Goby3  3.1.5
2024.05.14
dccl_constants.h
Go to the documentation of this file.
1 // Copyright 2011-2021:
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 Goby Underwater Autonomy Project Libraries
10 // ("The Goby Libraries").
11 //
12 // The Goby Libraries are free software: you can redistribute them and/or modify
13 // them 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 // The Goby Libraries are distributed in the hope that they 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 Goby. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef GOBY_MOOS_TRANSITIONAL_DCCL_CONSTANTS_H
26 #define GOBY_MOOS_TRANSITIONAL_DCCL_CONSTANTS_H
27 
28 #include <bitset>
29 #include <cmath>
30 #include <iomanip>
31 #include <limits>
32 #include <sstream>
33 #include <string>
34 #include <vector>
35 
36 //#include <boost/dynamic_bitset.hpp>
37 
39 #include "goby/acomms/dccl.h"
40 
41 namespace goby
42 {
43 namespace moos
44 {
45 namespace transitional
46 {
49 {
57 };
60 {
66 };
67 
68 // 2^3 = 8
69 enum
70 {
72 };
73 inline unsigned bits2bytes(unsigned bits) { return bits >> POWER2_BITS_IN_BYTE; }
74 inline unsigned bytes2bits(unsigned bytes) { return bytes << POWER2_BITS_IN_BYTE; }
75 
76 // 2^1 = 2
77 enum
78 {
80 };
81 inline unsigned bytes2nibs(unsigned bytes) { return bytes << POWER2_NIBS_IN_BYTE; }
82 inline unsigned nibs2bytes(unsigned nibs) { return nibs >> POWER2_NIBS_IN_BYTE; }
83 
84 inline std::string type_to_string(DCCLType type)
85 {
86  switch (type)
87  {
88  case dccl_int: return "int";
89  case dccl_hex: return "hex";
90  case dccl_bool: return "bool";
91  case dccl_string: return "string";
92  case dccl_float: return "float";
93  case dccl_static: return "static";
94  case dccl_enum: return "enum";
95  }
96  return "notype";
97 }
98 
99 inline std::string type_to_protobuf_type(DCCLType type)
100 {
101  switch (type)
102  {
103  case dccl_int: return "int32";
104  case dccl_hex: return "bytes";
105  case dccl_bool: return "bool";
106  case dccl_string: return "string";
107  case dccl_float: return "double";
108  case dccl_static: return "string";
109  default:
111  "cannot directly map DCCLv1 XML type to Protobuf Type"));
112  }
113  return "notype";
114 }
115 
116 inline std::string type_to_string(DCCLCppType type)
117 {
118  switch (type)
119  {
120  case cpp_long: return "long";
121  case cpp_double: return "double";
122  case cpp_string: return "string";
123  case cpp_bool: return "bool";
124  case cpp_notype: return "no_type";
125  }
126  return "notype";
127 }
128 
129 const unsigned DCCL_NUM_HEADER_BYTES = 6;
130 
131 const unsigned DCCL_NUM_HEADER_PARTS = 8;
132 
134 {
143 };
144 
145 const std::string DCCL_HEADER_NAMES[] = {
146  "_ccl_id", "_id", "_time", "_src_id", "_dest_id", "_multimessage_flag",
147  "_broadcast_flag", "_unused",
148 };
149 inline std::string to_str(DCCLHeaderPart p) { return DCCL_HEADER_NAMES[p]; }
150 
152 {
160 };
161 
163 
164 
171 inline bool char_array2hex_string(const unsigned char* c, std::string& s, const unsigned int n)
172 {
173  std::stringstream ss;
174  for (unsigned int i = 0; i < n; i++)
175  ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(c[i]);
176 
177  s = ss.str();
178  return true;
179 }
180 
182 inline bool hex_string2char_array(unsigned char* c, const std::string& s, const unsigned int n)
183 {
184  for (unsigned int i = 0; i < n; i++)
185  {
186  std::stringstream ss;
187  unsigned int in;
188  ss << s.substr(2 * i, 2);
189  ss >> std::hex >> in;
190  c[i] = static_cast<unsigned char>(in);
191  }
192  return true;
193 }
194 
196 inline std::string long2binary_string(unsigned long l, unsigned short bits)
197 {
198  char s[bits + 1];
199  for (unsigned int i = 0; i < bits; i++)
200  {
201  s[bits - i - 1] = (l & 1) ? '1' : '0';
202  l >>= 1;
203  }
204  s[bits] = '\0';
205  return (std::string)s;
206 }
207 
209 inline std::string binary_string2hex_string(const std::string& bs)
210 {
211  std::string hs;
212  auto bytes = (unsigned int)(std::ceil(bs.length() / 8.0));
213  unsigned char c[bytes];
214 
215  for (size_t i = 0; i < bytes; ++i)
216  {
217  std::bitset<8> b(bs.substr(i * 8, 8));
218  c[i] = (char)b.to_ulong();
219  }
220 
221  char_array2hex_string(c, hs, bytes);
222 
223  return hs;
224 }
225 
229 inline std::string hex_string2binary_string(const std::string& bs)
230 {
231  int bytes = bs.length() / 2;
232  unsigned char c[bytes];
233 
234  hex_string2char_array(c, bs, bytes);
235 
236  std::string hs;
237 
238  for (size_t i = 0; i < (size_t)bytes; i++) hs += long2binary_string((unsigned long)c[i], 8);
239 
240  return hs;
241 }
242 
246 template <typename T> bool hex_string2number(const std::string& s, T& t)
247 {
248  std::stringstream ss;
249  ss << s;
250  ss >> std::hex >> t;
251  return !ss.fail();
252 }
253 
260 template <typename T> bool number2hex_string(std::string& s, const T& t, unsigned int width = 2)
261 {
262  std::stringstream ss;
263  ss << std::hex << std::setw(width) << std::setfill('0') << static_cast<unsigned int>(t);
264  s = ss.str();
265  return !ss.fail();
266 }
267 
273 template <typename T> std::string number2hex_string(const T& t, unsigned int width = 2)
274 {
275  std::string s;
276  number2hex_string(s, t, width);
277  return s;
278 }
279 
280 } // namespace transitional
281 } // namespace moos
282 } // namespace goby
283 #endif
goby::moos::transitional::HEAD_SRC_ID_SIZE
@ HEAD_SRC_ID_SIZE
Definition: dccl_constants.h:156
goby::moos::transitional::dccl_bool
@ dccl_bool
Definition: dccl_constants.h:51
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::moos::transitional::DCCL_HEADER_NAMES
const std::string DCCL_HEADER_NAMES[]
Definition: dccl_constants.h:145
goby::moos::transitional::hex_string2binary_string
std::string hex_string2binary_string(const std::string &bs)
converts a hex string ("8AAA") into a binary string ("1000101010101010")
Definition: dccl_constants.h:229
goby::moos::transitional::cpp_double
@ cpp_double
Definition: dccl_constants.h:65
goby::moos::transitional::cpp_string
@ cpp_string
Definition: dccl_constants.h:63
goby::moos::transitional::HEAD_DEST_ID_SIZE
@ HEAD_DEST_ID_SIZE
Definition: dccl_constants.h:157
goby::moos::transitional::DCCLHeaderPart
DCCLHeaderPart
Definition: dccl_constants.h:133
goby::moos::transitional::HEAD_CCL_ID_SIZE
@ HEAD_CCL_ID_SIZE
Definition: dccl_constants.h:153
goby::moos::transitional::dccl_int
@ dccl_int
Definition: dccl_constants.h:52
goby::moos::transitional::nibs2bytes
unsigned nibs2bytes(unsigned nibs)
Definition: dccl_constants.h:82
goby::moos::transitional::to_str
std::string to_str(DCCLHeaderPart p)
Definition: dccl_constants.h:149
goby::moos::transitional::long2binary_string
std::string long2binary_string(unsigned long l, unsigned short bits)
return a string represented the binary value of l for bits number of bits which reads MSB -> LSB
Definition: dccl_constants.h:196
goby::moos::transitional::HEAD_FLAG_SIZE
@ HEAD_FLAG_SIZE
Definition: dccl_constants.h:158
goby::moos::transitional::HEAD_DCCL_ID_SIZE
@ HEAD_DCCL_ID_SIZE
Definition: dccl_constants.h:154
goby::moos::transitional::dccl_hex
@ dccl_hex
Definition: dccl_constants.h:56
char
goby::moos::transitional::hex_string2char_array
bool hex_string2char_array(unsigned char *c, const std::string &s, const unsigned int n)
turns a string of hex chars ABCDEF into a character array reading each byte 0xAB,0xCD,...
Definition: dccl_constants.h:182
goby::moos::transitional::DCCLType
DCCLType
Enumeration of DCCL types used for sending messages. dccl_enum and dccl_string primarily map to cpp_s...
Definition: dccl_constants.h:48
goby::moos::transitional::cpp_bool
@ cpp_bool
Definition: dccl_constants.h:62
goby::moos::transitional::bytes2nibs
unsigned bytes2nibs(unsigned bytes)
Definition: dccl_constants.h:81
dccl.h
goby::moos::transitional::HEAD_CCL_ID
@ HEAD_CCL_ID
Definition: dccl_constants.h:135
goby::moos::transitional::DCCLHeaderBits
DCCLHeaderBits
Definition: dccl_constants.h:151
goby::moos::transitional::HEAD_MULTIMESSAGE_FLAG
@ HEAD_MULTIMESSAGE_FLAG
Definition: dccl_constants.h:140
goby::moos::transitional::dccl_string
@ dccl_string
Definition: dccl_constants.h:55
goby::moos::transitional::DCCL_NUM_HEADER_PARTS
const unsigned DCCL_NUM_HEADER_PARTS
Definition: dccl_constants.h:131
goby::moos::transitional::POWER2_BITS_IN_BYTE
@ POWER2_BITS_IN_BYTE
Definition: dccl_constants.h:71
goby::moos::transitional::hex_string2number
bool hex_string2number(const std::string &s, T &t)
attempts to convert a hex string into a numerical representation (of type T)
Definition: dccl_constants.h:246
goby::moos::transitional::DCCL_NUM_HEADER_BYTES
const unsigned DCCL_NUM_HEADER_BYTES
Definition: dccl_constants.h:129
goby::moos::transitional::bits2bytes
unsigned bits2bytes(unsigned bits)
Definition: dccl_constants.h:73
goby::moos::transitional::bytes2bits
unsigned bytes2bits(unsigned bytes)
Definition: dccl_constants.h:74
goby::moos::transitional::HEAD_TIME_SIZE
@ HEAD_TIME_SIZE
Definition: dccl_constants.h:155
goby::moos::transitional::HEAD_BROADCAST_FLAG
@ HEAD_BROADCAST_FLAG
Definition: dccl_constants.h:141
goby::moos::transitional::HEAD_UNUSED_SIZE
@ HEAD_UNUSED_SIZE
Definition: dccl_constants.h:159
goby::moos::transitional::POWER2_NIBS_IN_BYTE
@ POWER2_NIBS_IN_BYTE
Definition: dccl_constants.h:79
goby::moos::transitional::type_to_protobuf_type
std::string type_to_protobuf_type(DCCLType type)
Definition: dccl_constants.h:99
jwt::json::type
type
Generic JSON types used in JWTs.
Definition: jwt.h:2071
goby::moos::transitional::HEAD_DCCL_ID
@ HEAD_DCCL_ID
Definition: dccl_constants.h:136
goby::moos::transitional::HEAD_UNUSED
@ HEAD_UNUSED
Definition: dccl_constants.h:142
goby::moos::transitional::binary_string2hex_string
std::string binary_string2hex_string(const std::string &bs)
converts a binary string ("1000101010101010") into a hex string ("8AAA")
Definition: dccl_constants.h:209
goby::moos::transitional::number2hex_string
bool number2hex_string(std::string &s, const T &t, unsigned int width=2)
converts a decimal number of type T into a hex string
Definition: dccl_constants.h:260
goby::moos::transitional::type_to_string
std::string type_to_string(DCCLType type)
Definition: dccl_constants.h:84
goby::moos::transitional::DCCLCppType
DCCLCppType
Enumeration of C++ types used in DCCL.
Definition: dccl_constants.h:59
goby::moos::transitional::HEAD_DEST_ID
@ HEAD_DEST_ID
Definition: dccl_constants.h:139
goby::moos::transitional::cpp_long
@ cpp_long
Definition: dccl_constants.h:64
goby::moos::transitional::char_array2hex_string
bool char_array2hex_string(const unsigned char *c, std::string &s, const unsigned int n)
converts a char (byte) array into a hex string
Definition: dccl_constants.h:171
acomms_constants.h
goby::moos::transitional::HEAD_TIME
@ HEAD_TIME
Definition: dccl_constants.h:137
goby::moos::transitional::dccl_float
@ dccl_float
Definition: dccl_constants.h:53
goby::moos::transitional::dccl_static
@ dccl_static
Definition: dccl_constants.h:50
goby::moos::transitional::HEAD_SRC_ID
@ HEAD_SRC_ID
Definition: dccl_constants.h:138
goby::acomms::DCCLException
dccl::Exception DCCLException
Definition: dccl.h:69
goby::moos::transitional::cpp_notype
@ cpp_notype
Definition: dccl_constants.h:61
int
goby::moos::transitional::dccl_enum
@ dccl_enum
Definition: dccl_constants.h:54