3#include "behaviortree_cpp/basic_types.h"
4#ifdef BTCPP_SYSTEM_JSON
5#include <nlohmann/json.hpp>
7#include "behaviortree_cpp/contrib/json.hpp"
11#include <condition_variable>
23
24
25
26
27
28
29
30
31
33enum RequestType : uint8_t
47 BREAKPOINT_REACHED =
'N',
49 BREAKPOINT_UNLOCK =
'U',
54 REMOVE_ALL_HOOKS =
'A',
56 DISABLE_ALL_HOOKS =
'X',
59 TOGGLE_RECORDING =
'r',
61 GET_TRANSITIONS =
't',
66inline const char* ToString(
const RequestType& type)
70 case RequestType::FULLTREE:
72 case RequestType::STATUS:
74 case RequestType::BLACKBOARD:
77 case RequestType::HOOK_INSERT:
79 case RequestType::HOOK_REMOVE:
81 case RequestType::BREAKPOINT_REACHED:
82 return "breakpoint_reached";
83 case RequestType::BREAKPOINT_UNLOCK:
84 return "breakpoint_unlock";
85 case RequestType::REMOVE_ALL_HOOKS:
86 return "hooks_remove_all";
87 case RequestType::HOOKS_DUMP:
89 case RequestType::DISABLE_ALL_HOOKS:
90 return "disable_hooks";
91 case RequestType::TOGGLE_RECORDING:
92 return "toggle_recording";
93 case RequestType::GET_TRANSITIONS:
94 return "get_transitions";
96 case RequestType::UNDEFINED:
102constexpr uint8_t kProtocolID = 2;
103using TreeUniqueUUID = std::array<
char, 16>;
107 uint32_t unique_id = 0;
108 uint8_t protocol = kProtocolID;
109 RequestType type = RequestType::UNDEFINED;
113 return sizeof(uint32_t) +
sizeof(uint8_t) +
sizeof(uint8_t);
116 RequestHeader() =
default;
118 RequestHeader(RequestType type) : type(type)
121 static std::random_device rd;
122 std::mt19937 mt(rd());
123 std::uniform_int_distribution<uint32_t> dist;
124 unique_id = dist(mt);
129 return type == other.type && unique_id == other.unique_id;
133 return !(*
this == other);
140 TreeUniqueUUID tree_id = {};
144 return RequestHeader::size() + 16;
149inline unsigned Serialize(
char* buffer,
unsigned offset, T value)
151 memcpy(buffer + offset, &value,
sizeof(T));
156inline unsigned Deserialize(
const char* buffer,
unsigned offset, T& value)
158 memcpy(&value, buffer + offset,
sizeof(T));
162inline std::string SerializeHeader(
const RequestHeader& header)
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);
173inline std::string SerializeHeader(
const ReplyHeader& header)
176 std::string buffer = SerializeHeader(header.request);
178 unsigned const offset = 6;
179 buffer.resize(offset + 16);
180 Serialize(buffer.data(), offset, header.tree_id);
184inline RequestHeader DeserializeRequestHeader(
const std::string& buffer)
188 offset += Deserialize(buffer.data(), offset, header.protocol);
190 offset += Deserialize(buffer.data(), offset, type);
191 header.type =
static_cast<Monitor::RequestType>(type);
192 offset += Deserialize(buffer.data(), offset, header.unique_id);
196inline ReplyHeader DeserializeReplyHeader(
const std::string& buffer)
199 header.request = DeserializeRequestHeader(buffer);
200 unsigned const offset = 6;
201 Deserialize(buffer.data(), offset, header.tree_id);
207 using Ptr = std::shared_ptr<
Hook>;
218 Position position = Position::PRE;
220 uint16_t node_uid = 0;
229 Mode mode = Mode::BREAKPOINT;
232 std::condition_variable wakeup;
240 bool remove_when_done =
false;
246inline void to_json(nlohmann::json& js,
const Hook& bp)
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) } };
256inline void from_json(
const nlohmann::json& js,
Hook& bp)
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>());
264 const std::string desired_value = js.at(
"desired_status").get<std::string>();
265 bp.desired_status = convertFromString<NodeStatus>(desired_value);
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
Definition: groot2_protocol.h:206