primary_parser.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019, FZI Forschungszentrum Informatik (refactor)
3  *
4  * Copyright 2017, 2018 Simon Rasmussen (refactor)
5  *
6  * Copyright 2015, 2016 Thomas Timm Andersen (original version)
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #pragma once
22 #include <vector>
31 
32 namespace urcl
33 {
34 namespace primary_interface
35 {
40 class PrimaryParser : public comm::Parser<PrimaryPackage>
41 {
42 public:
43  PrimaryParser() = default;
44  virtual ~PrimaryParser() = default;
45 
55  bool parse(comm::BinParser& bp, std::vector<std::unique_ptr<PrimaryPackage>>& results)
56  {
57  int32_t packet_size;
58  RobotPackageType type;
59  bp.parse(packet_size);
60  bp.parse(type);
61 
62  switch (type)
63  {
65  {
66  while (!bp.empty())
67  {
68  if (!bp.checkSize(sizeof(uint32_t)))
69  {
70  URCL_LOG_ERROR("Failed to read sub-package length, there's likely a parsing error");
71  return false;
72  }
73  uint32_t sub_size = bp.peek<uint32_t>();
74  if (!bp.checkSize(static_cast<size_t>(sub_size)))
75  {
76  URCL_LOG_WARN("Invalid sub-package size of %" PRIu32 " received!", sub_size);
77  return false;
78  }
79 
80  // deconstruction of a sub parser will increment the position of the parent parser
81  comm::BinParser sbp(bp, sub_size);
82  sbp.consume(sizeof(sub_size));
83  RobotStateType type;
84  sbp.parse(type);
85 
86  std::unique_ptr<PrimaryPackage> packet(stateFromType(type));
87 
88  if (packet == nullptr)
89  {
90  sbp.consume();
91 
92  // TODO: create robot state type here
93  continue;
94  }
95 
96  if (!packet->parseWith(sbp))
97  {
98  URCL_LOG_ERROR("Sub-package parsing of type %d failed!", static_cast<int>(type));
99  return false;
100  }
101 
102  results.push_back(std::move(packet));
103 
104  if (!sbp.empty())
105  {
106  URCL_LOG_ERROR("Sub-package of type %d was not parsed completely!", static_cast<int>(type));
107  sbp.debug();
108  return false;
109  }
110  }
111 
112  break;
113  }
114 
116  {
117  uint64_t timestamp;
118  uint8_t source;
119  RobotMessagePackageType message_type;
120 
121  bp.parse(timestamp);
122  bp.parse(source);
123  bp.parse(message_type);
124 
125  std::unique_ptr<PrimaryPackage> packet(messageFromType(message_type, timestamp, source));
126  if (!packet->parseWith(bp))
127  {
128  URCL_LOG_ERROR("Package parsing of type %d failed!", static_cast<int>(message_type));
129  return false;
130  }
131 
132  results.push_back(std::move(packet));
133  return true;
134  break;
135  }
136 
137  default:
138  {
139  URCL_LOG_DEBUG("Invalid robot package type recieved: %u", static_cast<uint8_t>(type));
140  bp.consume();
141  return true;
142  }
143  }
144  return true;
145  }
146 
147 private:
149  {
150  switch (type)
151  {
152  /*case robot_state_type::ROBOT_MODE_DATA:
153  // SharedRobotModeData* rmd = new SharedRobotModeData();
154 
155  //return new rmd;
156  case robot_state_type::MASTERBOARD_DATA:
157  return new MBD;*/
159  return new KinematicsInfo(type);
160  default:
161  return new RobotState(type);
162  }
163  }
164 
165  RobotMessage* messageFromType(RobotMessagePackageType type, uint64_t timestamp, uint8_t source)
166  {
167  switch (type)
168  {
169  /*case robot_state_type::ROBOT_MODE_DATA:
170  // SharedRobotModeData* rmd = new SharedRobotModeData();
171 
172  //return new rmd;
173  case robot_state_type::MASTERBOARD_DATA:
174  return new MBD;*/
176  return new VersionMessage(timestamp, source);
177  default:
178  return new RobotMessage(timestamp, source);
179  }
180  }
181 };
182 
183 } // namespace primary_interface
184 } // namespace urcl
RobotMessagePackageType
Possible RobotMessage types.
Definition: robot_message.h:40
void debug()
Logs debugging information about the BinParser object.
Definition: bin_parser.h:369
#define URCL_LOG_ERROR(...)
Definition: log.h:37
void parse(T &val)
Parses the next bytes as given type.
Definition: bin_parser.h:139
bool empty()
Checks if no unparsed bytes remain in the buffer.
Definition: bin_parser.h:361
The BinParser class handles a byte buffer and functionality to iteratively parse the content...
Definition: bin_parser.h:44
bool parse(comm::BinParser &bp, std::vector< std::unique_ptr< PrimaryPackage >> &results)
Uses the given BinParser to create package objects from the contained serialization.
The primary specific parser. Interprets a given byte stream as serialized primary packages and parses...
The parser is a general paser. The namsepace rtde_interface and primary_interface both iclude classes...
Definition: parser.h:36
RobotMessage * messageFromType(RobotMessagePackageType type, uint64_t timestamp, uint8_t source)
Base class for a RobotState data packages will be used directly.
Definition: robot_state.h:58
void consume()
Sets the current buffer position to the end of the buffer, finishing parsing.
Definition: bin_parser.h:318
The VersionMessage class handles the version messages sent via the primary UR interface.
bool checkSize(size_t bytes)
Checks if at least a given number of bytes is still remaining unparsed in the buffer.
Definition: bin_parser.h:339
This messages contains information about the robot&#39;s calibration. The DH parameters are a combination...
The RobotMessage class is a parent class for the different received robot messages.
Definition: robot_message.h:56
RobotState * stateFromType(RobotStateType type)
#define URCL_LOG_DEBUG(...)
Definition: log.h:34
RobotStateType
Possible RobotState types.
Definition: robot_state.h:41
#define URCL_LOG_WARN(...)
Definition: log.h:35
T peek()
Parses the next bytes as given type without moving the buffer pointer.
Definition: bin_parser.h:121
RobotPackageType
Possible RobotPackage types.


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Sun May 9 2021 02:16:26