bin_parser.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017, 2018 Simon Rasmussen (refactor)
3  *
4  * Copyright 2015, 2016 Thomas Timm Andersen (original version)
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include <assert.h>
22 #include <endian.h>
23 #include <inttypes.h>
24 #include <array>
25 #include <bitset>
26 #include <cstddef>
27 #include <cstring>
28 #include <string>
29 #include "ur_modern_driver/log.h"
30 #include "ur_modern_driver/types.h"
31 
32 class BinParser
33 {
34 private:
35  uint8_t *buf_pos_, *buf_end_;
37 
38 public:
39  BinParser(uint8_t* buffer, size_t buf_len) : buf_pos_(buffer), buf_end_(buffer + buf_len), parent_(*this)
40  {
41  assert(buf_pos_ <= buf_end_);
42  }
43 
44  BinParser(BinParser& parent, size_t sub_len)
45  : buf_pos_(parent.buf_pos_), buf_end_(parent.buf_pos_ + sub_len), parent_(parent)
46  {
47  assert(buf_pos_ <= buf_end_);
48  }
49 
51  {
52  parent_.buf_pos_ = buf_pos_;
53  }
54 
55  // Decode from network encoding (big endian) to host encoding
56  template <typename T>
57  T decode(T val)
58  {
59  return val;
60  }
61  uint16_t decode(uint16_t val)
62  {
63  return be16toh(val);
64  }
65  uint32_t decode(uint32_t val)
66  {
67  return be32toh(val);
68  }
69  uint64_t decode(uint64_t val)
70  {
71  return be64toh(val);
72  }
73  int16_t decode(int16_t val)
74  {
75  return be16toh(val);
76  }
77  int32_t decode(int32_t val)
78  {
79  return be32toh(val);
80  }
81  int64_t decode(int64_t val)
82  {
83  return be64toh(val);
84  }
85 
86  template <typename T>
87  T peek()
88  {
89  assert(buf_pos_ + sizeof(T) <= buf_end_);
90  T val;
91  std::memcpy(&val, buf_pos_, sizeof(T));
92  return decode(val);
93  }
94 
95  template <typename T>
96  void parse(T& val)
97  {
98  val = peek<T>();
99  buf_pos_ += sizeof(T);
100  }
101 
102  void parse(double& val)
103  {
104  uint64_t inner;
105  parse<uint64_t>(inner);
106  std::memcpy(&val, &inner, sizeof(double));
107  }
108  void parse(float& val)
109  {
110  uint32_t inner;
111  parse<uint32_t>(inner);
112  std::memcpy(&val, &inner, sizeof(float));
113  }
114 
115  // UR uses 1 byte for boolean values but sizeof(bool) is implementation
116  // defined so we must ensure they're parsed as uint8_t on all compilers
117  void parse(bool& val)
118  {
119  uint8_t inner;
120  parse<uint8_t>(inner);
121  val = inner != 0;
122  }
123 
124  // Explicit parsing order of fields to avoid issues with struct layout
125  void parse(double3_t& val)
126  {
127  parse(val.x);
128  parse(val.y);
129  parse(val.z);
130  }
131 
132  // Explicit parsing order of fields to avoid issues with struct layout
134  {
135  parse(val.position);
136  parse(val.rotation);
137  }
138 
139  void parse_remainder(std::string& val)
140  {
141  parse(val, size_t(buf_end_ - buf_pos_));
142  }
143 
144  void parse(std::string& val, size_t len)
145  {
146  val.assign(reinterpret_cast<char*>(buf_pos_), len);
147  buf_pos_ += len;
148  }
149 
150  // Special string parse function that assumes uint8_t len followed by chars
151  void parse(std::string& val)
152  {
153  uint8_t len;
154  parse(len);
155  parse(val, size_t(len));
156  }
157 
158  template <typename T, size_t N>
159  void parse(std::array<T, N>& array)
160  {
161  for (size_t i = 0; i < N; i++)
162  {
163  parse(array[i]);
164  }
165  }
166 
167  template <typename T, size_t N>
168  void parse(std::bitset<N>& set)
169  {
170  T val;
171  parse(val);
172  set = std::bitset<N>(val);
173  }
174 
175  void consume()
176  {
177  buf_pos_ = buf_end_;
178  }
179  void consume(size_t bytes)
180  {
181  buf_pos_ += bytes;
182  }
183 
184  bool checkSize(size_t bytes)
185  {
186  return bytes <= size_t(buf_end_ - buf_pos_);
187  }
188  template <typename T>
189  bool checkSize(void)
190  {
191  return checkSize(T::SIZE);
192  }
193 
194  bool empty()
195  {
196  return buf_pos_ == buf_end_;
197  }
198 
199  void debug()
200  {
201  LOG_DEBUG("BinParser: %p - %p (%zu bytes)", buf_pos_, buf_end_, buf_end_ - buf_pos_);
202  }
203 };
void parse(double3_t &val)
Definition: bin_parser.h:125
void debug()
Definition: bin_parser.h:199
void parse(bool &val)
Definition: bin_parser.h:117
double3_t rotation
Definition: types.h:31
double y
Definition: types.h:25
#define LOG_DEBUG(format,...)
Definition: log.h:33
double3_t position
Definition: types.h:30
T peek()
Definition: bin_parser.h:87
uint8_t * buf_pos_
Definition: bin_parser.h:35
int64_t decode(int64_t val)
Definition: bin_parser.h:81
void parse(std::string &val)
Definition: bin_parser.h:151
uint32_t decode(uint32_t val)
Definition: bin_parser.h:65
uint16_t decode(uint16_t val)
Definition: bin_parser.h:61
bool empty()
Definition: bin_parser.h:194
double x
Definition: types.h:25
void parse(std::bitset< N > &set)
Definition: bin_parser.h:168
BinParser(BinParser &parent, size_t sub_len)
Definition: bin_parser.h:44
double z
Definition: types.h:25
uint64_t decode(uint64_t val)
Definition: bin_parser.h:69
bool checkSize(size_t bytes)
Definition: bin_parser.h:184
int32_t decode(int32_t val)
Definition: bin_parser.h:77
void parse(double &val)
Definition: bin_parser.h:102
void parse(std::string &val, size_t len)
Definition: bin_parser.h:144
BinParser & parent_
Definition: bin_parser.h:36
int16_t decode(int16_t val)
Definition: bin_parser.h:73
void parse_remainder(std::string &val)
Definition: bin_parser.h:139
void parse(T &val)
Definition: bin_parser.h:96
void parse(float &val)
Definition: bin_parser.h:108
void parse(cartesian_coord_t &val)
Definition: bin_parser.h:133
void parse(std::array< T, N > &array)
Definition: bin_parser.h:159
bool checkSize(void)
Definition: bin_parser.h:189
BinParser(uint8_t *buffer, size_t buf_len)
Definition: bin_parser.h:39
~BinParser()
Definition: bin_parser.h:50
uint8_t * buf_end_
Definition: bin_parser.h:35
void consume(size_t bytes)
Definition: bin_parser.h:179
T decode(T val)
Definition: bin_parser.h:57
void consume()
Definition: bin_parser.h:175


ur_modern_driver
Author(s): Thomas Timm Andersen, Simon Rasmussen
autogenerated on Fri Jun 26 2020 03:37:00