STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
AbortBroadcaster.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <netinet/in.h>
4
5#include <atomic>
6#include <cstdint>
7#include <mutex>
8#include <string>
9#include <thread>
10
11namespace sequencer {
12
13/**
14 * Sends ABORT / ABORT_DONE / CLEAR_ABORT UDP broadcast packets
15 * (PacketHeader only — 6 bytes) to the configured broadcast address and port.
16 *
17 * On abort entry:
18 * 1. sendAbort() — fires immediately
19 * 2. scheduleAbortDone() — fires ABORT_DONE after abort_done_delay_ms
20 *
21 * Thread-safe.
22 */
24public:
25 explicit AbortBroadcaster(uint16_t port = 5005, uint32_t abort_done_delay_ms = 3000);
27
28 /**
29 * Adopt the destination and source NIC from config. Call once, before anything can abort.
30 *
31 * This class used to be default-constructed and never told anything: port 5005 by ctor
32 * default, destination hardcoded to the limited broadcast 255.255.255.255, source interface
33 * left to the kernel. Every other sender in the system takes its destination from config, and
34 * a limited broadcast is the one destination that does not resolve to a single route — so on
35 * a host with more than one NIC all four repeats could leave on the wrong wire together.
36 *
37 * @param broadcast_ip Subnet-directed broadcast for the board LAN, e.g. 192.168.2.255.
38 * @param port Board listen port.
39 * @param done_delay_ms Delay before ABORT_DONE.
40 * @param bind_address Local address to send from, or "0.0.0.0" to leave unpinned.
41 * @return false if broadcast_ip is not a valid IPv4 address (caller should fail startup).
42 */
43 bool configure(const std::string& broadcast_ip, uint16_t port, uint32_t done_delay_ms,
44 const std::string& bind_address);
45
46 /** Send ABORT broadcast and schedule ABORT_DONE after abort_done_delay_ms. */
47 void triggerAbort();
48
49 /** Send CLEAR_ABORT broadcast (called when leaving abort states). */
50 void sendClearAbort();
51
52private:
53 uint16_t port_;
55 // Resolved once in configure(), not per send: the abort path is the wrong place to discover
56 // that an address does not parse.
57 struct in_addr dest_addr_{};
58 std::string dest_ip_{"255.255.255.255"};
59 std::string bind_address_{"0.0.0.0"};
60 std::atomic<bool> done_thread_running_{false};
61 std::thread done_thread_;
62 // triggerAbort() joins done_thread_ and then move-assigns a new thread onto it. Two callers
63 // doing that concurrently is either a double join (UB) or a move onto a joinable thread,
64 // which is a std::terminate — the same hazard FireManager::stop() documents. Abort is
65 // reachable from any TCP client thread and from the fire-expiry thread, so it needs its own
66 // lock regardless of what serializes the state machine above it.
68
69 /** How many times each broadcast is repeated, and the gap between repeats. A single UDP
70 * datagram is not a delivery guarantee: one dropped frame on a congested field network and
71 * no board hears the abort. ActuatorCommander::sendBatch already sends ordinary state
72 * changes 3x for exactly this reason — the abort path must not be weaker than the routine
73 * one. */
74 static constexpr int kBroadcastRepeats = 4;
75 static constexpr int kBroadcastGapUs = 2000;
76
77 void sendPacket(uint8_t packet_type);
79};
80
81} // namespace sequencer
void sendPacket(uint8_t packet_type)
void triggerAbort()
Send ABORT broadcast and schedule ABORT_DONE after abort_done_delay_ms.
std::atomic< bool > done_thread_running_
static constexpr int kBroadcastGapUs
static constexpr int kBroadcastRepeats
How many times each broadcast is repeated, and the gap between repeats.
bool configure(const std::string &broadcast_ip, uint16_t port, uint32_t done_delay_ms, const std::string &bind_address)
Adopt the destination and source NIC from config.
AbortBroadcaster(uint16_t port=5005, uint32_t abort_done_delay_ms=3000)
void sendClearAbort()
Send CLEAR_ABORT broadcast (called when leaving abort states).
static const emmc_port_t port
Definition emmc_stm32.c:210