STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
arm_gate.cpp
Go to the documentation of this file.
1#include "arm_gate.h"
2
3namespace radio_link {
4
5namespace {
6
7// True if a is strictly newer than b, treating the u16 as a wrapping counter.
8bool seq_newer(uint16_t a, uint16_t b) {
9 return static_cast<int16_t>(static_cast<uint16_t>(a - b)) > 0;
10}
11
12} // namespace
13
14void ArmGate::tick(uint32_t now_ms) {
15 if (pending_ &&
16 static_cast<uint32_t>(now_ms - pending_ms_) > kArmConfirmWindowMs) {
17 pending_ = false;
18 }
19}
20
21void ArmGate::note_seq(uint16_t seq) {
22 if (!have_last_ || seq_newer(seq, last_seq_)) {
23 last_seq_ = seq;
24 have_last_ = true;
25 }
26}
27
28ArmAction ArmGate::on_frame(const Frame& f, uint32_t now_ms) {
29 tick(now_ms);
30
31 if (f.type == FrameType::UnArm) {
32 pending_ = false;
33 note_seq(f.seq);
34 return ArmAction::Disarm;
35 }
36 if (f.type != FrameType::Arm) {
37 return ArmAction::None;
38 }
39
40 // From here on, any rejection also cancels a pending first ARM.
41 const uint32_t key = get_u32(f.payload);
42 const uint32_t key_inv = get_u32(f.payload + 4);
43 const uint8_t target = f.payload[8];
44
45 if (key != kArmKey || key_inv != static_cast<uint32_t>(~kArmKey)) {
46 pending_ = false;
48 }
49 if (target != target_id_) {
50 pending_ = false;
52 }
53 if (have_last_ && !seq_newer(f.seq, last_seq_)) {
54 pending_ = false;
56 }
57 note_seq(f.seq);
58
59 if (pending_ && f.seq == static_cast<uint16_t>(pending_seq_ + 1u)) {
60 pending_ = false;
61 return ArmAction::Arm;
62 }
63 // First valid ARM, or the second one was not consecutive: start over.
64 pending_ = true;
65 pending_seq_ = f.seq;
66 pending_ms_ = now_ms;
67 return ArmAction::Pending;
68}
69
70} // namespace radio_link
Decides whether a valid frame is allowed to ARM or UN-ARM the rocket.
long target
Definition main.cpp:38
*brief create JSON pointer *sa reference_tokens const string_t const string_t & b
Definition json.hpp:13878
*brief create JSON pointer *sa reference_tokens const string_t & a
Definition json.hpp:13877