groot2_protocol.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <array>
5 #include <cstring>
6 #include <stdexcept>
7 #include <random>
8 #include <memory>
9 #include <condition_variable>
10 #include <mutex>
13 
14 namespace BT::Monitor
15 {
16 
17 /*
18  * All the messages exchange with the BT executor are multipart ZMQ request-replies.
19  *
20  * The first part of the request and the reply have fixed size and are described below.
21  * The request and reply must have the same value of the fields:
22  *
23  * - request_id
24  * - request_type
25  * - protocol_id
26  */
27 
28 enum RequestType : uint8_t
29 {
30  // Request the entire tree defintion as XML
31  FULLTREE = 'T',
32  // Request the staus of all the nodes
33  STATUS = 'S',
34  // retrieve the valus in a set of blackboards
35  BLACKBOARD = 'B',
36 
37  // Groot requests the insertion of a hook
38  HOOK_INSERT = 'I',
39  // Groot requests to remove a hook
40  HOOK_REMOVE = 'R',
41  // Notify Groot that we reached a breakpoint
43  // Groot will unlock a breakpoint
45  // receive the existing hooks in JSON format
46  HOOKS_DUMP = 'D',
47 
48  // Remove all hooks. To be done before disconnecting Groot
50 
52 
53  // start/stop recordong
55  // get all transitions when recording
57 
58  UNDEFINED = 0,
59 };
60 
61 inline const char* ToString(const RequestType& type)
62 {
63  switch(type)
64  {
66  return "full_tree";
68  return "status";
70  return "blackboard";
71 
73  return "hook_insert";
75  return "hook_remove";
77  return "breakpoint_reached";
79  return "breakpoint_unlock";
81  return "hooks_remove_all";
83  return "hooks_dump";
85  return "disable_hooks";
87  return "toggle_recording";
89  return "get_transitions";
90 
92  return "undefined";
93  }
94  return "undefined";
95 }
96 
97 constexpr uint8_t kProtocolID = 2;
98 using TreeUniqueUUID = std::array<char, 16>;
99 
101 {
102  uint32_t unique_id = 0;
105 
106  static size_t size()
107  {
108  return sizeof(uint32_t) + sizeof(uint8_t) + sizeof(uint8_t);
109  }
110 
111  RequestHeader() = default;
112 
114  {
115  // a random number for request_id will do
116  static std::random_device rd;
117  std::mt19937 mt(rd());
118  std::uniform_int_distribution<uint32_t> dist;
119  unique_id = dist(mt);
120  }
121 
122  bool operator==(const RequestHeader& other) const
123  {
124  return type == other.type && unique_id == other.unique_id;
125  }
126  bool operator!=(const RequestHeader& other) const
127  {
128  return !(*this == other);
129  }
130 };
131 
133 {
136 
137  static size_t size()
138  {
139  return RequestHeader::size() + 16;
140  }
141 
143  {
144  tree_id.fill(0);
145  }
146 };
147 
148 template <typename T>
149 inline unsigned Serialize(char* buffer, unsigned offset, T value)
150 {
151  memcpy(buffer + offset, &value, sizeof(T));
152  return sizeof(T);
153 }
154 
155 template <typename T>
156 inline unsigned Deserialize(const char* buffer, unsigned offset, T& value)
157 {
158  memcpy(reinterpret_cast<char*>(&value), buffer + offset, sizeof(T));
159  return sizeof(T);
160 }
161 
162 inline std::string SerializeHeader(const RequestHeader& header)
163 {
164  std::string buffer;
165  buffer.resize(6);
166  unsigned offset = 0;
167  offset += Serialize(buffer.data(), offset, header.protocol);
168  offset += Serialize(buffer.data(), offset, uint8_t(header.type));
169  offset += Serialize(buffer.data(), offset, header.unique_id);
170  return buffer;
171 }
172 
173 inline std::string SerializeHeader(const ReplyHeader& header)
174 {
175  // copy the first part directly (6 bytes)
176  std::string buffer = SerializeHeader(header.request);
177  // add the following 16 bytes
178  unsigned const offset = 6;
179  buffer.resize(offset + 16);
180  Serialize(buffer.data(), offset, header.tree_id);
181  return buffer;
182 }
183 
185 {
186  RequestHeader header;
187  unsigned offset = 0;
188  offset += Deserialize(buffer.data(), offset, header.protocol);
189  uint8_t type;
190  offset += Deserialize(buffer.data(), offset, type);
191  header.type = static_cast<Monitor::RequestType>(type);
192  offset += Deserialize(buffer.data(), offset, header.unique_id);
193  return header;
194 }
195 
196 inline ReplyHeader DeserializeReplyHeader(const std::string& buffer)
197 {
198  ReplyHeader header;
200  unsigned const offset = 6;
201  Deserialize(buffer.data(), offset, header.tree_id);
202  return header;
203 }
204 
205 struct Hook
206 {
207  using Ptr = std::shared_ptr<Hook>;
208 
209  // used to enable/disable the breakpoint
210  bool enabled = true;
211 
212  enum class Position
213  {
214  PRE = 0,
215  POST = 1
216  };
217 
219 
220  uint16_t node_uid = 0;
221 
222  enum class Mode
223  {
224  BREAKPOINT = 0,
225  REPLACE = 1
226  };
227 
228  // interactive breakpoints are unblocked using unlockBreakpoint()
230 
231  // used by interactive breakpoints to wait for unlocking
232  std::condition_variable wakeup;
233 
235 
236  // set to true to unlock an interactive breakpoint
237  bool ready = false;
238 
239  // once finished self-destroy
240  bool remove_when_done = false;
241 
242  // result to be returned
244 };
245 
246 inline void to_json(nlohmann::json& js, const Hook& bp)
247 {
248  js = nlohmann::json{ { "enabled", bp.enabled },
249  { "uid", bp.node_uid },
250  { "mode", int(bp.mode) },
251  { "once", bp.remove_when_done },
252  { "desired_status", toStr(bp.desired_status) },
253  { "position", int(bp.position) } };
254 }
255 
256 inline void from_json(const nlohmann::json& js, Hook& bp)
257 {
258  js.at("enabled").get_to(bp.enabled);
259  js.at("uid").get_to(bp.node_uid);
260  js.at("once").get_to(bp.remove_when_done);
261  bp.mode = static_cast<Hook::Mode>(js.at("mode").get<int>());
262  bp.position = static_cast<Hook::Position>(js.at("position").get<int>());
263 
264  const std::string desired_value = js.at("desired_status").get<std::string>();
265  bp.desired_status = convertFromString<NodeStatus>(desired_value);
266 }
267 
268 } // namespace BT::Monitor
BT::Monitor::Hook::Mode::BREAKPOINT
@ BREAKPOINT
BT::Monitor::Hook::Position::POST
@ POST
BT::Monitor::Hook::mutex
std::mutex mutex
Definition: groot2_protocol.h:234
BT::Monitor::Serialize
unsigned Serialize(char *buffer, unsigned offset, T value)
Definition: groot2_protocol.h:149
BT::Monitor::ReplyHeader::tree_id
TreeUniqueUUID tree_id
Definition: groot2_protocol.h:135
BT::Monitor::Hook::Ptr
std::shared_ptr< Hook > Ptr
Definition: groot2_protocol.h:207
BT::Monitor::RequestHeader::RequestHeader
RequestHeader()=default
BT::Monitor::UNDEFINED
@ UNDEFINED
Definition: groot2_protocol.h:58
BT::Monitor::ReplyHeader::size
static size_t size()
Definition: groot2_protocol.h:137
BT::Monitor::DISABLE_ALL_HOOKS
@ DISABLE_ALL_HOOKS
Definition: groot2_protocol.h:51
BT::Monitor::ReplyHeader::ReplyHeader
ReplyHeader()
Definition: groot2_protocol.h:142
BT::Monitor::BREAKPOINT_REACHED
@ BREAKPOINT_REACHED
Definition: groot2_protocol.h:42
basic_types.h
BT::Monitor::BLACKBOARD
@ BLACKBOARD
Definition: groot2_protocol.h:35
BT::Monitor::Hook::Mode
Mode
Definition: groot2_protocol.h:222
BT::Monitor::HOOK_REMOVE
@ HOOK_REMOVE
Definition: groot2_protocol.h:40
BT::Monitor::Hook::ready
bool ready
Definition: groot2_protocol.h:237
BT::Monitor::ToString
const char * ToString(const RequestType &type)
Definition: groot2_protocol.h:61
BT::Monitor::Hook::node_uid
uint16_t node_uid
Definition: groot2_protocol.h:220
BT::Monitor::RequestHeader
Definition: groot2_protocol.h:100
BT::Monitor::RequestHeader::RequestHeader
RequestHeader(RequestType type)
Definition: groot2_protocol.h:113
BT::Monitor::Hook::enabled
bool enabled
Definition: groot2_protocol.h:210
BT::Monitor::RequestHeader::unique_id
uint32_t unique_id
Definition: groot2_protocol.h:102
BT::Monitor::RequestHeader::operator!=
bool operator!=(const RequestHeader &other) const
Definition: groot2_protocol.h:126
BT::Monitor::Hook::remove_when_done
bool remove_when_done
Definition: groot2_protocol.h:240
BT::Monitor::Hook::Mode::REPLACE
@ REPLACE
mutex
static pthread_mutex_t mutex
Definition: minitrace.cpp:77
BT::Monitor::Hook::Position
Position
Definition: groot2_protocol.h:212
BT::Monitor::FULLTREE
@ FULLTREE
Definition: groot2_protocol.h:31
BT::Monitor::SerializeHeader
std::string SerializeHeader(const RequestHeader &header)
Definition: groot2_protocol.h:162
BT::Monitor
Definition: groot2_protocol.h:14
BT::Monitor::ReplyHeader
Definition: groot2_protocol.h:132
BT::NodeStatus::SKIPPED
@ SKIPPED
BT::Monitor::kProtocolID
constexpr uint8_t kProtocolID
Definition: groot2_protocol.h:97
lexy::buffer
buffer(const CharT *, const CharT *) -> buffer< deduce_encoding< CharT >>
BT::Monitor::Hook::wakeup
std::condition_variable wakeup
Definition: groot2_protocol.h:232
BT::Monitor::Hook::position
Position position
Definition: groot2_protocol.h:218
BT::Monitor::Hook
Definition: groot2_protocol.h:205
BT::Monitor::TOGGLE_RECORDING
@ TOGGLE_RECORDING
Definition: groot2_protocol.h:54
BT::Monitor::HOOK_INSERT
@ HOOK_INSERT
Definition: groot2_protocol.h:38
BT::Monitor::Hook::desired_status
NodeStatus desired_status
Definition: groot2_protocol.h:243
BT::Monitor::HOOKS_DUMP
@ HOOKS_DUMP
Definition: groot2_protocol.h:46
BT::Monitor::RequestType
RequestType
Definition: groot2_protocol.h:28
BT::Monitor::STATUS
@ STATUS
Definition: groot2_protocol.h:33
BT::Monitor::RequestHeader::protocol
uint8_t protocol
Definition: groot2_protocol.h:103
BT::Monitor::GET_TRANSITIONS
@ GET_TRANSITIONS
Definition: groot2_protocol.h:56
json.hpp
BT::Monitor::from_json
void from_json(const nlohmann::json &js, Hook &bp)
Definition: groot2_protocol.h:256
BT::Monitor::Hook::Position::PRE
@ PRE
BT::Monitor::REMOVE_ALL_HOOKS
@ REMOVE_ALL_HOOKS
Definition: groot2_protocol.h:49
json
basic_json<> json
default specialization
Definition: json.hpp:3422
BT::Monitor::Deserialize
unsigned Deserialize(const char *buffer, unsigned offset, T &value)
Definition: groot2_protocol.h:156
BT::Monitor::to_json
void to_json(nlohmann::json &js, const Hook &bp)
Definition: groot2_protocol.h:246
BT::Monitor::BREAKPOINT_UNLOCK
@ BREAKPOINT_UNLOCK
Definition: groot2_protocol.h:44
BT::Monitor::RequestHeader::type
RequestType type
Definition: groot2_protocol.h:104
BT::Monitor::Hook::mode
Mode mode
Definition: groot2_protocol.h:229
BT::Monitor::RequestHeader::operator==
bool operator==(const RequestHeader &other) const
Definition: groot2_protocol.h:122
BT::Monitor::ReplyHeader::request
RequestHeader request
Definition: groot2_protocol.h:134
BT::Monitor::DeserializeReplyHeader
ReplyHeader DeserializeReplyHeader(const std::string &buffer)
Definition: groot2_protocol.h:196
BT::convertFromString< NodeStatus >
NodeStatus convertFromString< NodeStatus >(StringView str)
Definition: basic_types.cpp:284
BT::Monitor::RequestHeader::size
static size_t size()
Definition: groot2_protocol.h:106
BT::Monitor::DeserializeRequestHeader
RequestHeader DeserializeRequestHeader(const std::string &buffer)
Definition: groot2_protocol.h:184
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33
BT::toStr
std::string toStr(const T &value)
toStr is the reverse operation of convertFromString.
Definition: basic_types.h:252
BT::Monitor::TreeUniqueUUID
std::array< char, 16 > TreeUniqueUUID
Definition: groot2_protocol.h:98


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07