state_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 #include <vector>
22 #include "ur_modern_driver/log.h"
28 
29 template <typename RMD, typename MBD>
30 class URStateParser : public URParser<StatePacket>
31 {
32 private:
34  {
35  switch (type)
36  {
38  return new RMD;
40  return new MBD;
41  default:
42  return nullptr;
43  }
44  }
45 
46 public:
47  bool parse(BinParser& bp, std::vector<std::unique_ptr<StatePacket>>& results)
48  {
49  int32_t packet_size;
50  message_type type;
51  bp.parse(packet_size);
52  bp.parse(type);
53 
54  if (type != message_type::ROBOT_STATE)
55  {
56  // quietly ignore the intial version message
57  if (type != message_type::ROBOT_MESSAGE)
58  {
59  LOG_WARN("Invalid state message type recieved: %u", static_cast<uint8_t>(type));
60  }
61 
62  bp.consume();
63  return true;
64  }
65 
66  while (!bp.empty())
67  {
68  if (!bp.checkSize(sizeof(uint32_t)))
69  {
70  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  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  BinParser sbp(bp, sub_size);
82  sbp.consume(sizeof(sub_size));
83  package_type type;
84  sbp.parse(type);
85 
86  std::unique_ptr<StatePacket> packet(from_type(type));
87 
88  if (packet == nullptr)
89  {
90  sbp.consume();
91  continue;
92  }
93 
94  if (!packet->parseWith(sbp))
95  {
96  LOG_ERROR("Sub-package parsing of type %d failed!", static_cast<int>(type));
97  return false;
98  }
99 
100  results.push_back(std::move(packet));
101 
102  if (!sbp.empty())
103  {
104  LOG_ERROR("Sub-package of type %d was not parsed completely!", static_cast<int>(type));
105  sbp.debug();
106  return false;
107  }
108  }
109 
110  return true;
111  }
112 };
113 
void debug()
Definition: bin_parser.h:199
URStateParser< RobotModeData_V3_0__1, MasterBoardData_V3_0__1 > URStateParser_V3_0__1
Definition: state_parser.h:115
message_type
Definition: state.h:41
T peek()
Definition: bin_parser.h:87
bool parse(BinParser &bp, std::vector< std::unique_ptr< StatePacket >> &results)
Definition: state_parser.h:47
bool empty()
Definition: bin_parser.h:194
package_type
Definition: state.h:27
bool checkSize(size_t bytes)
Definition: bin_parser.h:184
URStateParser< RobotModeData_V1_X, MasterBoardData_V1_X > URStateParser_V1_X
Definition: state_parser.h:114
void parse(T &val)
Definition: bin_parser.h:96
StatePacket * from_type(package_type type)
Definition: state_parser.h:33
URStateParser< RobotModeData_V3_2, MasterBoardData_V3_2 > URStateParser_V3_2
Definition: state_parser.h:116
URStateParser< RobotModeData_V3_5, MasterBoardData_V3_2 > URStateParser_V3_5
Definition: state_parser.h:117
#define LOG_ERROR(format,...)
Definition: log.h:36
void consume()
Definition: bin_parser.h:175
#define LOG_WARN(format,...)
Definition: log.h:34


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