Protective Stop
Loading...
Searching...
No Matches
announce.hpp
1// SPDX-FileCopyrightText: 2026 Polymath Robotics, Inc.
2// SPDX-License-Identifier: Apache-2.0
3#pragma once
4
5#include <atomic>
6#include <cstdint>
7#include <cstdio>
8#include <functional>
9#include <string>
10#include <thread>
11
12#include "protective_stop_machine/backend.hpp"
13
14namespace protective_stop_machine
15{
16
20{
22 std::string url;
24 std::string key_file;
26 std::string name;
28 int interval_s{60};
30 double http_timeout_s{10.0};
31};
32
35inline std::string json_escape(const std::string & text)
36{
37 std::string escaped;
38 escaped.reserve(text.size() + 8);
39 for (char character : text) {
40 switch (character) {
41 case '"':
42 escaped += "\\\"";
43 break;
44 case '\\':
45 escaped += "\\\\";
46 break;
47 case '\n':
48 escaped += "\\n";
49 break;
50 case '\r':
51 escaped += "\\r";
52 break;
53 case '\t':
54 escaped += "\\t";
55 break;
56 default:
57 if (static_cast<unsigned char>(character) < 0x20) {
58 char escape_sequence[8];
59 std::snprintf(escape_sequence, sizeof(escape_sequence), "\\u%04x", character);
60 escaped += escape_sequence;
61 } else {
62 escaped += character;
63 }
64 }
65 }
66 return escaped;
67}
68
76inline std::string build_announce_payload(
77 const std::string & name, int port, uint32_t machine_id, const MachineSnapshot & snapshot)
78{
79 char machine_id_text[16];
80 std::snprintf(machine_id_text, sizeof(machine_id_text), "%08x", machine_id);
81
82 std::string payload = "{\"name\":\"";
83 payload += json_escape(name);
84 payload += "\",\"port\":";
85 payload += std::to_string(port);
86 payload += ",\"machine_id\":\"";
87 payload += machine_id_text;
88 payload += "\",\"device_type\":\"machine\",\"running\":";
89 payload += snapshot.running ? "true" : "false";
90 payload += ",\"active_remotes\":";
91 payload += std::to_string(snapshot.active_remotes);
92 payload += ",\"remotes\":[";
93 bool first = true;
94 for (const auto & remote : snapshot.remotes) {
95 if (!first) {
96 payload += ',';
97 }
98 first = false;
99 payload += "{\"id\":\"";
100 payload += json_escape(remote.device_id);
101 payload += "\",\"bond_state\":";
102 payload += std::to_string(static_cast<int>(remote.bond_state));
103 payload += ",\"stop_only\":";
104 payload += remote.stop_only ? "true" : "false";
105 payload += ",\"in_use\":";
106 payload += remote.in_use ? "true" : "false";
107 payload += '}';
108 }
109 payload += "]}";
110 return payload;
111}
112
118{
119public:
124 MachineAnnouncer(AnnounceConfig config, int port, uint32_t machine_id, std::function<MachineSnapshot()> snapshot_fn);
126
127 MachineAnnouncer(const MachineAnnouncer &) = delete;
128 MachineAnnouncer & operator=(const MachineAnnouncer &) = delete;
129
133 bool start();
134
136 void stop();
137
139 bool enabled() const
140 {
141 return enabled_.load();
142 }
143
144private:
145 void run();
146 bool post_once(const std::string & payload, const std::string & bearer_key);
147
148 AnnounceConfig config_;
149 int port_;
150 uint32_t machine_id_;
151 std::function<MachineSnapshot()> snapshot_fn_;
152
153 std::thread thread_;
154 std::atomic<bool> running_{false};
155 std::atomic<bool> enabled_{false};
156};
157
158} // namespace protective_stop_machine
bool enabled() const
True while the check-in thread is running.
Definition announce.hpp:139
bool start()
Launches the check-in thread.
void stop()
Stops the thread and joins. Idempotent.
MachineAnnouncer(AnnounceConfig config, int port, uint32_t machine_id, std::function< MachineSnapshot()> snapshot_fn)
Deployment values for the check-in.
Definition announce.hpp:20
int interval_s
Seconds between check-ins.
Definition announce.hpp:28
std::string url
http://host[:port]/path. Empty disables the announcer.
Definition announce.hpp:22
std::string key_file
Path whose first line is the bearer token, chmod 600.
Definition announce.hpp:24
std::string name
Display name on the console. Empty uses this host's hostname.
Definition announce.hpp:26
double http_timeout_s
HTTP request timeout, in seconds.
Definition announce.hpp:30
One immutable read of machine state, mapped straight onto the ROS messages.
Definition backend.hpp:64