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 {
56  dccl_hex
57 };
60 {
65  cpp_double
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 {
142  HEAD_UNUSED = 7
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 {
159  HEAD_UNUSED_SIZE = 2
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
dccl::Exception DCCLException
Definition: dccl.h:69
std::string hex_string2binary_string(const std::string &bs)
converts a hex string ("8AAA") into a binary string ("1000101010101010")
bool hex_string2number(const std::string &s, T &t)
attempts to convert a hex string into a numerical representation (of type T)
std::string to_str(DCCLHeaderPart p)
std::string type_to_string(DCCLType type)
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
unsigned bytes2nibs(unsigned bytes)
const unsigned DCCL_NUM_HEADER_BYTES
bool char_array2hex_string(const unsigned char *c, std::string &s, const unsigned int n)
converts a char (byte) array into a hex string
DCCLType
Enumeration of DCCL types used for sending messages. dccl_enum and dccl_string primarily map to cpp_s...
unsigned bytes2bits(unsigned bytes)
DCCLCppType
Enumeration of C++ types used in DCCL.
unsigned bits2bytes(unsigned bits)
std::string type_to_protobuf_type(DCCLType type)
bool number2hex_string(std::string &s, const T &t, unsigned int width=2)
converts a decimal number of type T into a hex string
unsigned nibs2bytes(unsigned nibs)
std::string binary_string2hex_string(const std::string &bs)
converts a binary string ("1000101010101010") into a hex string ("8AAA")
const std::string DCCL_HEADER_NAMES[]
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,...
const unsigned DCCL_NUM_HEADER_PARTS
The global namespace for the Goby project.
type
Generic JSON types used in JWTs.
Definition: jwt.h:2072