Protective Stop
Loading...
Searching...
No Matches
pstop_aux_channel.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Polymath Robotics, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4#ifndef PSTOP_AUX_CHANNEL_H
5#define PSTOP_AUX_CHANNEL_H
6
13
14#include <stdbool.h>
15#include <stdint.h>
16
17#include "pstop/machine.h"
18#include "pstop/pstop_msg.h"
19#include "pstop/pstop_remote_data.h"
20
21/* padding1: bits 0..7 schema version, bits 8..15 role, bits 16..31 zero. */
22
24#define PSTOP_AUX_UP_VERSION 0x01u
26#define PSTOP_AUX_UP_ROLE_SHIFT 8u
28#define PSTOP_AUX_BYTE_MASK 0xFFu
29
40
41static inline void pstop_aux_encode_role(pstop_msg_t * msg, pstop_aux_role_t role)
42{
43 uint32_t v = (uint32_t)PSTOP_AUX_UP_VERSION;
44 v |= ((uint32_t)((uint8_t)role & PSTOP_AUX_BYTE_MASK)) << PSTOP_AUX_UP_ROLE_SHIFT;
45 msg->padding1 = v;
46}
47
48static inline pstop_aux_role_t pstop_aux_decode_role(const pstop_msg_t * msg)
49{
50 if (msg->version != PSTOP_VERSION) {
52 }
55 }
56
57 uint8_t role = (uint8_t)((msg->padding1 >> PSTOP_AUX_UP_ROLE_SHIFT) & PSTOP_AUX_BYTE_MASK);
58
59 switch (role) {
65 default:
67 }
68}
69
70static inline bool pstop_aux_role_is_operator(pstop_aux_role_t role)
71{
72 return role == PSTOP_AUX_ROLE_OPERATOR;
73}
74
75static inline const char * pstop_aux_role_str(pstop_aux_role_t role)
76{
77 switch (role) {
79 return "operator";
81 return "stop_only";
83 default:
84 return "unspecified";
85 }
86}
87
88/*
89 * Live role enforcement — shared by every machine wrapper (machn, ROS 2, host)
90 * so the policy is identical everywhere. pstop_c is untouched; these use only
91 * its public structs. Call _pre right before machine_process_message() and
92 * _post right after, for every frame from an already-bonded remote (a NEW
93 * remote's is_stop_only is seeded by remote_details_cb at BOND).
94 *
95 * Why both halves are needed: pstop_c consults is_stop_only only when a STOP
96 * ACQUIRES ownership of the arming cycle (remote_stop_id == 0). Once a remote
97 * owns the cycle (it armed the machine), its later STOP re-opens the cycle
98 * unconditionally and its OK re-arms. And a STOP from a non-owner stop-only
99 * remote while armed leaves restart_state at OK, so its release would re-arm
100 * too. The two hooks close both holes:
101 * _pre : a remote announcing stop-only can never OWN the cycle — release
102 * ownership; a half-open cycle it owned is voided (NEED_STOP).
103 * _post : an accepted STOP from a stop-only remote never opens an arming
104 * cycle — force NEED_STOP so its OK cannot complete the gesture.
105 * Net effect: a remote that demotes itself while the machine is armed keeps
106 * it running; the next STOP from it stops the machine and it cannot re-arm
107 * until some remote announcing OPERATOR performs STOP -> OK.
108 */
109static inline void pstop_aux_apply_role_pre(pstop_machine_t * machine, const pstop_msg_t * req)
110{
111 pstop_remote_data_t * c = pstop_remote_get(&machine->remotes, &req->id);
112 if (c == NULL) {
113 return;
114 }
115 c->is_stop_only = !pstop_aux_role_is_operator(pstop_aux_decode_role(req));
116 if (c->is_stop_only && (machine->robot_state.remote_stop_id == c->local_remote_id)) {
117 machine->robot_state.remote_stop_id = 0U;
118 if (machine->robot_state.restart_state == ROBOT_RESTART_STATE_STOP_RECEIVED) {
119 machine->robot_state.restart_state = ROBOT_RESTART_STATE_NEED_STOP;
120 }
121 }
122}
123
124static inline void pstop_aux_apply_role_post(pstop_machine_t * machine, const pstop_msg_t * req, pstop_error_t err)
125{
126 if ((err != PSTOP_OK) || (req->message != PSTOP_MESSAGE_STOP)) {
127 return;
128 }
129 const pstop_remote_data_t * c = pstop_remote_get(&machine->remotes, &req->id);
130 if ((c != NULL) && c->is_stop_only) {
131 machine->robot_state.restart_state = ROBOT_RESTART_STATE_NEED_STOP;
132 }
133}
134
135#endif /* PSTOP_AUX_CHANNEL_H */
pstop_aux_role_t
Remote role, declared by the remote on every frame; wire-stable values.
Definition pstop_aux_channel.h:32
@ PSTOP_AUX_ROLE_OPERATOR
May re-arm with a STOP -> OK gesture.
Definition pstop_aux_channel.h:38
@ PSTOP_AUX_ROLE_STOP_ONLY
May only STOP, never re-arm.
Definition pstop_aux_channel.h:36
@ PSTOP_AUX_ROLE_UNSPECIFIED
No role claim (unprovisioned or version mismatch); treated as non-operator.
Definition pstop_aux_channel.h:34
#define PSTOP_AUX_UP_ROLE_SHIFT
Bit position of the role in padding1.
Definition pstop_aux_channel.h:26
#define PSTOP_AUX_BYTE_MASK
Mask for one byte of padding1.
Definition pstop_aux_channel.h:28
#define PSTOP_AUX_UP_VERSION
Schema version stored in padding1 bits 0..7.
Definition pstop_aux_channel.h:24
The machine side of the protocol: connected remotes, application callbacks, and robot state.
Definition machine.h:69
robot_state_t robot_state
Current stop/restart state.
Definition machine.h:78
pstop_remotes_t remotes
Remote slots tracked by this machine.
Definition machine.h:72
A PSTOP message object.
Definition pstop_msg.h:25
message_type_t message
Type of message.
Definition pstop_msg.h:34
device_id_t id
The UUID of this device.
Definition pstop_msg.h:50
uint32_t padding1
extra padding bytes
Definition pstop_msg.h:77
uint8_t version
Version of this message.
Definition pstop_msg.h:29
State tracked for one remote.
Definition pstop_remote_data.h:26
bool is_stop_only
True if the remote can stop but not restart the robot.
Definition pstop_remote_data.h:38
uint32_t local_remote_id
Local identifier of the remote.
Definition pstop_remote_data.h:29
int restart_state
0 = everything is OK 1 = need stop message 2 = stop received
Definition machine.h:60
uint32_t remote_stop_id
The client ID that caused the robot to stop if stopped.
Definition machine.h:53