Home IO Control
ESPHome add-on for IO-Homecontrol devices
Loading...
Searching...
No Matches
pairing_advisor.cpp
Go to the documentation of this file.
1/// @file pairing_advisor.cpp
2/// @brief Read-only advisor that turns PairingTelemetry into actionable diagnostics.
3/// @ingroup hioc_hub
4
5#include "pairing_advisor.h"
6
7#include "hub_decisions.h"
8#include "proto_constants.h"
9#include "proto_timing.h"
10
11#include <cstdio>
12#include <cstring>
13
14namespace esphome {
15namespace home_io_control {
16namespace advisor {
17
18namespace {
19
20/// Buffer size for the rendered advice message.
21constexpr size_t ADVICE_MESSAGE_BUFFER_SIZE = 224;
22
23bool is_rx_kind(PairingTelemetryEventKind kind) {
25}
26
27/// A seeded pre-window sighting (PairingTelemetry::record_recent_one_way_sighting()) rather than a
28/// live in-window capture — see PairingTelemetryEvent::aux's doc comment. Excluded from
29/// channel_busy specifically: that advice is about *live* channel contention during this attempt,
30/// and a sighting from up to PAIRING_RECENT_ONE_WAY_SIGHTING_WINDOW_MS earlier carries no evidence
31/// about that. Still eligible for ONE_WAY_PAIRING_TRAFFIC, which is about the gesture itself.
32bool is_seeded_sighting(const PairingTelemetryEvent &event) {
33 return event.kind == PairingTelemetryEventKind::RX && event.aux == 1;
34}
35
36/// 1W pairing traffic (issue #27 case). Thin wrapper over the shared predicate (hub_decisions.h)
37/// so the hub's passive RX path can classify the same frame shape without duplicating it — see
38/// decisions::is_one_way_pairing_gesture()'s doc comment for the frame shape itself.
39bool is_oneway_pairing_frame(const PairingTelemetryEvent &event) {
40 return decisions::is_one_way_pairing_gesture(event.oneway, event.dst_node, event.cmd);
41}
42
43/// Count how many *live* RX/RX_REJECT events share `src_node` — excludes a seeded pre-window
44/// sighting, which is not evidence of live channel contention (see is_seeded_sighting()).
45uint8_t count_occurrences_of_source(const PairingTelemetryEvent *events, uint8_t event_count,
46 const uint8_t src_node[NODE_ID_SIZE]) {
47 uint8_t occurrences = 0;
48 for (uint8_t i = 0; i < event_count; i++) {
49 if (is_rx_kind(events[i].kind) && !is_seeded_sighting(events[i]) &&
50 memcmp(events[i].src_node, src_node, NODE_ID_SIZE) == 0)
51 occurrences++;
52 }
53 return occurrences;
54}
55
56void append_one_way_pairing_advice(const PairingTelemetryEvent *events, uint8_t event_count, PairingAdvice out[],
57 uint8_t &count) {
58 for (uint8_t i = 0; i < event_count && count < PAIRING_ADVICE_MAX; i++) {
59 const PairingTelemetryEvent &event = events[i];
60 if (!is_rx_kind(event.kind) || !is_oneway_pairing_frame(event))
61 continue;
62 PairingAdvice &advice = out[count++];
64 memcpy(advice.subject_node, event.src_node, NODE_ID_SIZE);
65 advice.rssi = event.rssi;
66 return;
67 }
68}
69
70void append_channel_busy_advice(const PairingTelemetry &telemetry, const PairingTelemetryEvent *events,
71 uint8_t event_count, PairingAdvice out[], uint8_t &count) {
72 if (count >= PAIRING_ADVICE_MAX || telemetry.lbt_retries() < LBT_MAX_RETRIES)
73 return;
74 for (uint8_t i = 0; i < event_count; i++) {
75 const PairingTelemetryEvent &event = events[i];
76 if (!is_rx_kind(event.kind) || is_seeded_sighting(event))
77 continue;
78 if (count_occurrences_of_source(events, event_count, event.src_node) < 2)
79 continue;
80 PairingAdvice &advice = out[count++];
82 memcpy(advice.subject_node, event.src_node, NODE_ID_SIZE);
83 advice.rssi = event.rssi;
84 return;
85 }
86}
87
88void append_foreign_controller_advice(const uint8_t own_node_id[NODE_ID_SIZE], const PairingTelemetryEvent *events,
89 uint8_t event_count, PairingAdvice out[], uint8_t &count) {
90 static const uint8_t ZERO_NODE_ID[NODE_ID_SIZE] = {0, 0, 0};
91 // An all-zero own_node_id means the controller's node ID isn't provisioned yet — every
92 // discovery response would look "foreign" by the memcmp below, so skip the check entirely
93 // rather than emit a false advisory during first-time setup.
94 if (memcmp(own_node_id, ZERO_NODE_ID, NODE_ID_SIZE) == 0)
95 return;
96 for (uint8_t i = 0; i < event_count && count < PAIRING_ADVICE_MAX; i++) {
97 const PairingTelemetryEvent &event = events[i];
98 if (!is_rx_kind(event.kind) || event.cmd != CMD_DISCOVER_RESP)
99 continue;
100 if (memcmp(event.dst_node, own_node_id, NODE_ID_SIZE) == 0)
101 continue;
102 PairingAdvice &advice = out[count++];
104 memcpy(advice.subject_node, event.src_node, NODE_ID_SIZE);
105 advice.rssi = event.rssi;
106 return;
107 }
108}
109
110} // namespace
111
112uint8_t analyze_pairing_telemetry(const PairingTelemetry &telemetry, const uint8_t own_node_id[NODE_ID_SIZE],
114 uint8_t count = 0;
115 const PairingTelemetryEvent *events = telemetry.events();
116 const uint8_t event_count = telemetry.event_count();
117
118 append_one_way_pairing_advice(events, event_count, out, count);
119 append_channel_busy_advice(telemetry, events, event_count, out, count);
120 append_foreign_controller_advice(own_node_id, events, event_count, out, count);
121
122 if (count == 0 && telemetry.heard_count() == 0 && count < PAIRING_ADVICE_MAX) {
123 out[count++].code = PairingAdviceCode::RF_SILENT;
124 }
125
126 return count;
127}
128
130 switch (code) {
132 return "1w_traffic";
134 return "channel_busy";
136 return "foreign_controller";
138 return "rf_silent";
140 default:
141 return "none";
142 }
143}
144
145std::string pairing_advice_message(const PairingAdvice &advice) {
146 char buf[ADVICE_MESSAGE_BUFFER_SIZE];
147 switch (advice.code) {
149 snprintf(buf, sizeof(buf),
150 "A 1W remote (src %s) is performing 1W pairing. The motor is NOT in 2W learning mode - a PROG "
151 "press on a 1W remote does not enable 2W discovery. Use the Double Power Cut procedure to force "
152 "2W learning mode.",
153 node_id_to_string(advice.subject_node).c_str());
154 return std::string(buf);
156 snprintf(buf, sizeof(buf),
157 "Channel busy (device %s beaconing at %d dBm); discovery TX was delayed by listen-before-talk.",
158 node_id_to_string(advice.subject_node).c_str(), advice.rssi);
159 return std::string(buf);
161 snprintf(buf, sizeof(buf), "Another controller is pairing device %s right now.",
162 node_id_to_string(advice.subject_node).c_str());
163 return std::string(buf);
165 return "Nothing heard on any channel during the whole discovery window - check the antenna/RF path before "
166 "assuming the device isn't in pairing mode.";
168 default:
169 return "";
170 }
171}
172
173} // namespace advisor
174} // namespace home_io_control
175} // namespace esphome
Fixed-size per-attempt telemetry recorder for the pairing flow.
const PairingTelemetryEvent * events() const
Pure transition helpers for hub-owned exchange and pairing frame decisions.
std::string pairing_advice_message(const PairingAdvice &advice)
static constexpr uint8_t PAIRING_ADVICE_MAX
Maximum number of advice entries a single attempt can produce (one slot per non-NONE code).
const char * pairing_advice_code_name(PairingAdviceCode code)
PairingAdviceCode
Actionable diagnosis derived from a completed pairing attempt's telemetry.
@ RF_SILENT
Nothing at all was heard during the whole window.
@ FOREIGN_CONTROLLER_PAIRING
A discovery response was seen addressed to another controller.
@ ONE_WAY_PAIRING_TRAFFIC
A 1W remote is pairing to the target — not 2W learning mode.
@ CHANNEL_BUSY_LBT_DELAYED
LBT retries were consumed while a source kept repeating; the reported subject_node is a best-effort c...
uint8_t analyze_pairing_telemetry(const PairingTelemetry &telemetry, const uint8_t own_node_id[NODE_ID_SIZE], PairingAdvice out[PAIRING_ADVICE_MAX])
Inspect a completed pairing attempt's telemetry and produce actionable advice.
bool is_one_way_pairing_gesture(bool oneway, const uint8_t dst[NODE_ID_SIZE], uint8_t cmd)
True if a frame's shape matches a 1W remote's pairing gesture (issue #27/#65): CTRL0 1W bit set,...
static constexpr uint8_t NODE_ID_SIZE
Device/node addresses are 3 bytes (e.g., "123ABC").
Definition proto_sizes.h:20
PairingTelemetryEventKind
Kind of a recorded telemetry event.
@ RX_REJECT
We received a frame that parsed but was rejected (wrong source, wrong command, etc....
@ RX
We received and accepted a frame for the current wait (including a seeded pre-window sighting — see P...
static constexpr uint8_t LBT_MAX_RETRIES
Max carrier-sense attempts before TX anyway.
std::string node_id_to_string(const uint8_t id[NODE_ID_SIZE])
Format a 3‑byte node ID as a 6‑character uppercase hex string.
static constexpr uint8_t CMD_DISCOVER_RESP
Device responds with its ID and type.
Read-only advisor that turns PairingTelemetry into actionable diagnostics.
IO-Homecontrol command IDs, result codes and protocol enumerations.
Physical-layer radio and timing parameters for the IO-Homecontrol protocol.
uint8_t cmd
Frame command byte (TX/RX/RX_REJECT); unused otherwise.
PairingTelemetryEventKind kind
What happened.
bool oneway
CTRL0 1W-protocol bit (RX/RX_REJECT only); false otherwise.
uint8_t dst_node[NODE_ID_SIZE]
Destination node ID (RX/RX_REJECT only); zero otherwise.
uint8_t src_node[NODE_ID_SIZE]
Sender node ID (RX/RX_REJECT only); zero otherwise.
One piece of advice, with the node/RSSI it pertains to (if any).
uint8_t subject_node[NODE_ID_SIZE]
Node the advice is about; zero if not applicable.
int16_t rssi
RSSI associated with the observation; zero if not applicable.