3#include "behaviortree_cpp/basic_types.h"
4#include "behaviortree_cpp/contrib/json.hpp"
7#include <condition_variable>
19
20
21
22
23
24
25
26
27
29enum RequestType : uint8_t
43 BREAKPOINT_REACHED =
'N',
45 BREAKPOINT_UNLOCK =
'U',
50 REMOVE_ALL_HOOKS =
'A',
52 DISABLE_ALL_HOOKS =
'X',
55 TOGGLE_RECORDING =
'r',
57 GET_TRANSITIONS =
't',
62inline const char* ToString(
const RequestType& type)
66 case RequestType::FULLTREE:
68 case RequestType::STATUS:
70 case RequestType::BLACKBOARD:
73 case RequestType::HOOK_INSERT:
75 case RequestType::HOOK_REMOVE:
77 case RequestType::BREAKPOINT_REACHED:
78 return "breakpoint_reached";
79 case RequestType::BREAKPOINT_UNLOCK:
80 return "breakpoint_unlock";
81 case RequestType::REMOVE_ALL_HOOKS:
82 return "hooks_remove_all";
83 case RequestType::HOOKS_DUMP:
85 case RequestType::DISABLE_ALL_HOOKS:
86 return "disable_hooks";
87 case RequestType::TOGGLE_RECORDING:
88 return "toggle_recording";
89 case RequestType::GET_TRANSITIONS:
90 return "get_transitions";
92 case RequestType::UNDEFINED:
98constexpr uint8_t kProtocolID = 2;
99using TreeUniqueUUID = std::array<
char, 16>;
103 uint32_t unique_id = 0;
104 uint8_t protocol = kProtocolID;
105 RequestType type = RequestType::UNDEFINED;
109 return sizeof(uint32_t) +
sizeof(uint8_t) +
sizeof(uint8_t);
112 RequestHeader() =
default;
114 RequestHeader(RequestType type) : type(type)
117 static std::random_device rd;
118 std::mt19937 mt(rd());
119 std::uniform_int_distribution<uint32_t> dist;
120 unique_id = dist(mt);
125 return type == other.type && unique_id == other.unique_id;
129 return !(*
this == other);
136 TreeUniqueUUID tree_id = {};
140 return RequestHeader::size() + 16;
145inline unsigned Serialize(
char* buffer,
unsigned offset, T value)
147 memcpy(buffer + offset, &value,
sizeof(T));
152inline unsigned Deserialize(
const char* buffer,
unsigned offset, T& value)
154 memcpy(&value, buffer + offset,
sizeof(T));
158inline std::string SerializeHeader(
const RequestHeader& header)
163 offset += Serialize(buffer.data(), offset, header.protocol);
164 offset += Serialize(buffer.data(), offset, uint8_t(header.type));
165 offset += Serialize(buffer.data(), offset, header.unique_id);
169inline std::string SerializeHeader(
const ReplyHeader& header)
172 std::string buffer = SerializeHeader(header.request);
174 unsigned const offset = 6;
175 buffer.resize(offset + 16);
176 Serialize(buffer.data(), offset, header.tree_id);
180inline RequestHeader DeserializeRequestHeader(
const std::string& buffer)
184 offset += Deserialize(buffer.data(), offset, header.protocol);
186 offset += Deserialize(buffer.data(), offset, type);
187 header.type =
static_cast<Monitor::RequestType>(type);
188 offset += Deserialize(buffer.data(), offset, header.unique_id);
192inline ReplyHeader DeserializeReplyHeader(
const std::string& buffer)
195 header.request = DeserializeRequestHeader(buffer);
196 unsigned const offset = 6;
197 Deserialize(buffer.data(), offset, header.tree_id);
203 using Ptr = std::shared_ptr<
Hook>;
214 Position position = Position::PRE;
216 uint16_t node_uid = 0;
225 Mode mode = Mode::BREAKPOINT;
228 std::condition_variable wakeup;
236 bool remove_when_done =
false;
242inline void to_json(nlohmann::json& js,
const Hook& bp)
244 js = nlohmann::json{ {
"enabled", bp.enabled },
245 {
"uid", bp.node_uid },
246 {
"mode",
int(bp.mode) },
247 {
"once", bp.remove_when_done },
248 {
"desired_status", toStr(bp.desired_status) },
249 {
"position",
int(bp.position) } };
252inline void from_json(
const nlohmann::json& js,
Hook& bp)
254 js.at(
"enabled").get_to(bp.enabled);
255 js.at(
"uid").get_to(bp.node_uid);
256 js.at(
"once").get_to(bp.remove_when_done);
257 bp.mode =
static_cast<Hook::Mode>(js.at(
"mode").get<
int>());
258 bp.position =
static_cast<Hook::Position>(js.at(
"position").get<
int>());
260 const std::string desired_value = js.at(
"desired_status").get<std::string>();
261 bp.desired_status = convertFromString<NodeStatus>(desired_value);
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
Definition: groot2_protocol.h:202