Goby v2
dccl_constants.h
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them 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 // The Goby Libraries are distributed in the hope that they 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 Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef DCCLTransitionalConstants20091211H
24 #define DCCLTransitionalConstants20091211H
25 
26 #include <bitset>
27 #include <cmath>
28 #include <iomanip>
29 #include <limits>
30 #include <sstream>
31 #include <string>
32 #include <vector>
33 
34 //#include <boost/dynamic_bitset.hpp>
35 
36 #include "goby/acomms/acomms_constants.h"
37 #include "goby/acomms/dccl.h"
38 #include "goby/moos/protobuf/transitional.pb.h"
39 
40 namespace goby
41 {
42 namespace transitional
43 {
46 {
54 };
57 {
63 };
64 
65 // 2^3 = 8
66 enum
67 {
68  POWER2_BITS_IN_BYTE = 3
69 };
70 inline unsigned bits2bytes(unsigned bits) { return bits >> POWER2_BITS_IN_BYTE; }
71 inline unsigned bytes2bits(unsigned bytes) { return bytes << POWER2_BITS_IN_BYTE; }
72 
73 // 2^1 = 2
74 enum
75 {
76  POWER2_NIBS_IN_BYTE = 1
77 };
78 inline unsigned bytes2nibs(unsigned bytes) { return bytes << POWER2_NIBS_IN_BYTE; }
79 inline unsigned nibs2bytes(unsigned nibs) { return nibs >> POWER2_NIBS_IN_BYTE; }
80 
81 inline std::string type_to_string(DCCLType type)
82 {
83  switch (type)
84  {
85  case dccl_int: return "int";
86  case dccl_hex: return "hex";
87  case dccl_bool: return "bool";
88  case dccl_string: return "string";
89  case dccl_float: return "float";
90  case dccl_static: return "static";
91  case dccl_enum: return "enum";
92  }
93  return "notype";
94 }
95 
96 inline std::string type_to_protobuf_type(DCCLType type)
97 {
98  switch (type)
99  {
100  case dccl_int: return "int32";
101  case dccl_hex: return "bytes";
102  case dccl_bool: return "bool";
103  case dccl_string: return "string";
104  case dccl_float: return "double";
105  case dccl_static: return "string";
106  default:
107  throw(goby::acomms::DCCLException(
108  "cannot directly map DCCLv1 XML type to Protobuf Type"));
109  }
110  return "notype";
111 }
112 
113 inline std::string type_to_string(DCCLCppType type)
114 {
115  switch (type)
116  {
117  case cpp_long: return "long";
118  case cpp_double: return "double";
119  case cpp_string: return "string";
120  case cpp_bool: return "bool";
121  case cpp_notype: return "no_type";
122  }
123  return "notype";
124 }
125 
126 const unsigned DCCL_NUM_HEADER_BYTES = 6;
127 
128 const unsigned DCCL_NUM_HEADER_PARTS = 8;
129 
130 enum DCCLHeaderPart
131 {
132  HEAD_CCL_ID = 0,
133  HEAD_DCCL_ID = 1,
134  HEAD_TIME = 2,
135  HEAD_SRC_ID = 3,
136  HEAD_DEST_ID = 4,
137  HEAD_MULTIMESSAGE_FLAG = 5,
138  HEAD_BROADCAST_FLAG = 6,
139  HEAD_UNUSED = 7
140 };
141 
142 const std::string DCCL_HEADER_NAMES[] = {
143  "_ccl_id", "_id", "_time", "_src_id", "_dest_id", "_multimessage_flag",
144  "_broadcast_flag", "_unused",
145 };
146 inline std::string to_str(DCCLHeaderPart p) { return DCCL_HEADER_NAMES[p]; }
147 
148 enum DCCLHeaderBits
149 {
150  HEAD_CCL_ID_SIZE = 8,
151  HEAD_DCCL_ID_SIZE = 9,
152  HEAD_TIME_SIZE = 17,
153  HEAD_SRC_ID_SIZE = 5,
154  HEAD_DEST_ID_SIZE = 5,
155  HEAD_FLAG_SIZE = 1,
156  HEAD_UNUSED_SIZE = 2
157 };
158 
160 
161 
168 inline bool char_array2hex_string(const unsigned char* c, std::string& s, const unsigned int n)
169 {
170  std::stringstream ss;
171  for (unsigned int i = 0; i < n; i++)
172  ss << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned int>(c[i]);
173 
174  s = ss.str();
175  return true;
176 }
177 
179 inline bool hex_string2char_array(unsigned char* c, const std::string& s, const unsigned int n)
180 {
181  for (unsigned int i = 0; i < n; i++)
182  {
183  std::stringstream ss;
184  unsigned int in;
185  ss << s.substr(2 * i, 2);
186  ss >> std::hex >> in;
187  c[i] = static_cast<unsigned char>(in);
188  }
189  return true;
190 }
191 
193 inline std::string long2binary_string(unsigned long l, unsigned short bits)
194 {
195  char s[bits + 1];
196  for (unsigned int i = 0; i < bits; i++)
197  {
198  s[bits - i - 1] = (l & 1) ? '1' : '0';
199  l >>= 1;
200  }
201  s[bits] = '\0';
202  return (std::string)s;
203 }
204 
206 inline std::string binary_string2hex_string(const std::string& bs)
207 {
208  std::string hs;
209  unsigned int bytes = (unsigned int)(std::ceil(bs.length() / 8.0));
210  unsigned char c[bytes];
211 
212  for (size_t i = 0; i < bytes; ++i)
213  {
214  std::bitset<8> b(bs.substr(i * 8, 8));
215  c[i] = (char)b.to_ulong();
216  }
217 
218  char_array2hex_string(c, hs, bytes);
219 
220  return hs;
221 }
222 
224 /* inline std::string dyn_bitset2hex_string(const boost::dynamic_bitset<unsigned char>& bits, unsigned trim_to_bytes_size = 0) */
225 /* { */
226 /* std::stringstream binary; */
227 /* binary << bits; */
228 /* std::string out = binary_string2hex_string(binary.str()); */
229 
230 /* if(trim_to_bytes_size) */
231 /* return out.substr(out.length() - trim_to_bytes_size*2); */
232 /* else */
233 /* return out; */
234 /* } */
235 
239 inline std::string hex_string2binary_string(const std::string& bs)
240 {
241  int bytes = bs.length() / 2;
242  unsigned char c[bytes];
243 
244  hex_string2char_array(c, bs, bytes);
245 
246  std::string hs;
247 
248  for (size_t i = 0; i < (size_t)bytes; i++) hs += long2binary_string((unsigned long)c[i], 8);
249 
250  return hs;
251 }
252 
254 /* inline boost::dynamic_bitset<unsigned char> hex_string2dyn_bitset(const std::string & hs, unsigned bits_size = 0) */
255 /* { */
256 /* boost::dynamic_bitset<unsigned char> bits; */
257 /* std::stringstream bin_str; */
258 /* bin_str << hex_string2binary_string(hs); */
259 /* bin_str >> bits; */
260 
261 /* if(bits_size) bits.resize(bits_size); */
262 /* return bits; */
263 /* } */
264 
268 template <typename T> bool hex_string2number(const std::string& s, T& t)
269 {
270  std::stringstream ss;
271  ss << s;
272  ss >> std::hex >> t;
273  return !ss.fail();
274 }
275 
282 template <typename T> bool number2hex_string(std::string& s, const T& t, unsigned int width = 2)
283 {
284  std::stringstream ss;
285  ss << std::hex << std::setw(width) << std::setfill('0') << static_cast<unsigned int>(t);
286  s = ss.str();
287  return !ss.fail();
288 }
289 
295 template <typename T> std::string number2hex_string(const T& t, unsigned int width = 2)
296 {
297  std::string s;
298  number2hex_string(s, t, width);
299  return s;
300 }
301 
302 /* class ManipulatorManager */
303 /* { */
304 /* public: */
305 /* void add(unsigned id, goby::transitional::protobuf::MessageFile::Manipulator manip) */
306 /* { */
307 /* manips_.insert(std::make_pair(id, manip)); */
308 /* } */
309 
310 /* bool has(unsigned id, goby::transitional::protobuf::MessageFile::Manipulator manip) const */
311 /* { */
312 /* typedef std::multimap<unsigned, goby::transitional::protobuf::MessageFile::Manipulator>::const_iterator iterator; */
313 /* std::pair<iterator,iterator> p = manips_.equal_range(id); */
314 
315 /* for(iterator it = p.first; it != p.second; ++it) */
316 /* { */
317 /* if(it->second == manip) */
318 /* return true; */
319 /* } */
320 
321 /* return false; */
322 /* } */
323 
324 /* void clear() */
325 /* { */
326 /* manips_.clear(); */
327 /* } */
328 
329 /* private: */
330 /* // manipulator multimap (no_encode, no_decode, etc) */
331 /* // maps DCCL ID (unsigned) onto Manipulator enumeration (xml_config.proto) */
332 /* std::multimap<unsigned, goby::transitional::protobuf::MessageFile::Manipulator> manips_; */
333 
334 /* }; */
335 
336 } // namespace transitional
337 } // namespace goby
338 #endif
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...
bool number2hex_string(std::string &s, const T &t, unsigned int width=2)
converts a decimal number of type T into a hex string
DCCLCppType
Enumeration of C++ types used in DCCL.
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, 0xEF, etc.
std::string binary_string2hex_string(const std::string &bs)
converts a binary string ("1000101010101010") into a hex string ("8AAA")
std::string hex_string2binary_string(const std::string &bs)
converts a boost::dynamic_bitset (similar to std::bitset but without compile time size requirements) ...
bool hex_string2number(const std::string &s, T &t)
converts a hex string ("8AAA") into a dynamic_bitset
The global namespace for the Goby project.
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 ...