16#include "esphome/core/application.h"
17#include "esphome/core/hal.h"
18#include "esphome/core/log.h"
28static const char *
const TAG =
"home_io_control.exchange";
36 : radio_ptr_(radio_ptr), node_id_(node_id), system_key_(system_key), tuning_(tuning) {}
47void ExchangeEngine::note_final_wait_(
const ListenStats &stats) {
48 DebugInfo &d = this->debug_;
49 const auto add = [](uint8_t &total, uint8_t n) {
50 total =
static_cast<uint8_t
>(std::min<unsigned>(total + n, UINT8_MAX));
52 add(d.final_waits, 1);
60 this->debug_.stage = stage;
61 this->debug_.tries = tries;
62 this->debug_.saw_challenge = this->debug_.saw_challenge || saw_challenge;
74 if (!capture.
valid && this->debug_.capture_valid)
76 this->debug_.capture_valid = capture.
valid;
77 this->debug_.capture_rx_done = capture.
rx_done;
78 this->debug_.capture_crc_error = capture.
crc_error;
79 this->debug_.capture_freq_hz = capture.
freq_hz;
80 this->debug_.capture_irq_status = capture.
irq_status;
82 this->debug_.capture_reported_len = capture.
reported_len;
83 this->debug_.capture_frame_len = capture.
frame_len;
84 this->debug_.capture_rssi_dbm = capture.
rssi_dbm;
88 return snprintf(buf, buf_size,
89 "device=%s cmd=%s(0x%02X) stage=%s tries=%u max_tries=%u saw_challenge=%u cap_valid=%u "
90 "cap_rx_done=%u cap_crc_err=%u cap_freq=%" PRIu32
91 " cap_irq=0x%04X cap_pkt=0x%02X cap_reported_len=%u cap_frame_len=%u cap_rssi=%d belief=%s "
92 "last_preamble=%u final_waits=%u final_rx_ignored=%u final_rx_failed=%u final_rx_irq=0x%04X",
106 char fields[EXCHANGE_DEBUG_LINE_SIZE];
108 ESP_LOGW(
TAG,
"Exchange failed: %s", fields);
112 char fields[EXCHANGE_DEBUG_LINE_SIZE];
119 ESP_LOGI(
TAG,
"Exchange accepted without a closing reply: %s", fields);
145 }
while (next == skip_freq);
147 this->last_hop_us_ = micros();
151 if ((micros() - this->last_hop_us_) <= HOP_TIME_US)
158 if ((*this->radio_ptr_)->reception_in_progress())
173 uint8_t buf[FRAME_MAX_WIRE_SIZE];
174 uint8_t
const len =
serialize(frame, buf,
sizeof(buf));
176 ESP_LOGW(
TAG,
"tx: serialize_failed cmd=0x%02X", frame.
cmd);
179 for (uint8_t lbt = 0; lbt < this->tuning_->lbt_max_retries; lbt++) {
181 if (rssi < this->tuning_->lbt_rssi_threshold_dbm)
183 ESP_LOGD(
TAG,
"LBT: channel busy (RSSI %d dBm), retry %u/%u", rssi, lbt + 1, this->tuning_->lbt_max_retries);
184 this->counters_.lbt_retries++;
185 if (this->transmit_observer_ !=
nullptr)
186 this->transmit_observer_->on_lbt_defer(rssi);
187 delay(LBT_RETRY_DELAY_MS);
193 ESP_LOGW(
TAG,
"tx: send_failed cmd=0x%02X", frame.
cmd);
196 if (this->transmit_observer_ !=
nullptr)
197 this->transmit_observer_->on_transmit(frame, tx_config, len);
219 return "wait_first_response";
221 return "build_auth_response";
223 return "tx_auth_response";
225 return "wait_final_response";
240 return "tx_challenge";
242 return "wait_challenge_response";
252bool frame_is_challenge_response(
const IoFrame &frame) {
return frame.cmd == CMD_CHALLENGE_RESP; }
260void log_unparsable_frame(
const char *stage,
int tries,
const RadioRxPacket &packet) {
265 ESP_LOGW(
TAG,
"%s try=%d: %u bytes did not parse as a frame on %" PRIu32
" Hz", stage, tries, packet.len,
267#ifdef IOHOME_FRAME_LOG
270 ESP_LOGD(
TAG,
" raw: %s", hex);
275void log_exchange_frame(
const char *stage,
int tries,
const IoFrame &frame, uint8_t len) {
276 ESP_LOGD(
TAG,
"%s try=%d cmd=0x%02X src=%02X%02X%02X dst=%02X%02X%02X len=%u", stage, tries, frame.cmd, frame.src[0],
277 frame.src[1], frame.src[2], frame.dst[0], frame.dst[1], frame.dst[2], len);
281constexpr size_t TRY_AGE_BUFFER_SIZE = 12;
290 if (ctx.target_last_seen_ms == 0) {
291 snprintf(buf,
sizeof(buf),
"n/a");
294 snprintf(buf,
sizeof(buf),
"%" PRIu32, ctx.try_start_ms - ctx.target_last_seen_ms);
299bool is_low_power_start(
const IoFrame &request) {
return is_start(request) && (request.ctrl1 & CTRL1_LOW_POWER) != 0; }
302bool is_valid_final_response(
const IoFrame &candidate,
const IoFrame &request) {
310 uint8_t max_tries, uint16_t request_preamble_override) {
318 (*this->radio_ptr_)->clear_last_capture();
321 uint8_t tries_allowed = std::max<uint8_t>(1, std::min<uint8_t>(max_tries, EXCHANGE_RETRY_COUNT));
322 this->debug_.max_tries = tries_allowed;
323 const PreamblePlan preamble_plan = this->plan_request_preamble_(request, request_preamble_override);
324 this->debug_.wake_belief_use = preamble_plan.use;
326 this->debug_.wake_belief = preamble_plan.belief;
332 const uint32_t exchange_begin_ms = millis();
333 uint8_t unconfirmed_tries = 0;
335 for (uint8_t tries = 0; tries < tries_allowed; tries++) {
339 is_start(request) ? this->tuning_->exchange_start_response_wait_ms : this->tuning_->exchange_response_wait_ms;
346 if (millis() - exchange_begin_ms >= this->tuning_->exchange_total_budget_ms) {
347 this->
record_debug(
"retry_budget_exhausted", tries,
false);
348 ESP_LOGI(
TAG,
"Exchange budget exhausted after %u tries for cmd=%s(0x%02X) (%" PRIu32
" of %u ms)", tries,
350 this->tuning_->exchange_total_budget_ms);
354 delay(EXCHANGE_RETRY_DELAY_MS);
355 this->counters_.retransmits++;
364 if (!this->transmit_request_(request, freq, context.
request_preamble, context))
369 auto first_disp = this->wait_for_first_response_(request, context);
377 char age[TRY_AGE_BUFFER_SIZE];
378 format_try_age(context, age);
379 ESP_LOGI(
TAG,
"Try %d answered: cmd=%s(0x%02X) wait_ms=%" PRIu32
" preamble=%u age_ms=%s", context.
try_index,
382 response = context.
rx;
386 if (!this->handle_authentication_(request, freq, context))
391 auto final_disp = this->wait_for_final_response_(request, context);
407 const uint8_t further_tries =
408 this->tries_after_unconfirmed_(request, unconfirmed_tries, context.
try_index, millis() - exchange_begin_ms);
409 if (further_tries == 0)
411 tries_allowed = std::min<uint8_t>(tries_allowed, tries + 1 + further_tries);
417 response = context.
rx;
436 return (*this->radio_ptr_)->response_preamble();
437 return is_low_power_start(request) ? LONG_PREAMBLE : this->tuning_->normal_start_preamble;
447 return "no_provider";
452 return "not_low_power";
456ExchangeEngine::PreamblePlan ExchangeEngine::plan_request_preamble_(
const IoFrame &request,
457 uint16_t override_preamble)
const {
468 if (override_preamble != 0) {
470 }
else if (!is_low_power_start(request)) {
474 }
else if (!this->target_evidence_provider_) {
483 decisions::TargetEvidence evidence{};
484 const bool known = this->target_evidence_provider_(request.
dst, evidence);
485 plan.short_preamble = this->tuning_->normal_start_preamble;
486 plan.last_seen_ms = known ? evidence.last_seen_ms : 0;
492bool ExchangeEngine::transmit_request_(
const IoFrame &request, uint32_t freq, uint16_t preamble,
496 this->
record_debug(
"tx_request_failed", ctx.try_index,
false);
505 spec.window_ms = ctx.wait_ms;
513 RadioRxPacket packet{};
514 auto outcome = this->
listen(spec, packet, ctx.rx, [&](
const IoFrame *parsed,
const RadioRxPacket &pkt) {
515 if (parsed == nullptr) {
516 this->record_debug(
"first_parse_fail", ctx.try_index, false);
517 this->counters_.parse_failures++;
518 log_unparsable_frame(
"Unparsable first response", ctx.try_index, pkt);
519 return ReplyDisposition::IGNORE;
523 this->
record_debug(
"first_wrong_exchange", ctx.try_index,
false);
524 log_exchange_frame(
"Ignored first response", ctx.try_index, *parsed, pkt.len);
527 ctx.first_response_ms = millis();
535 this->record_debug(
"wait_first_timeout", ctx.try_index,
false);
536 char age[TRY_AGE_BUFFER_SIZE];
537 format_try_age(ctx, age);
538 ESP_LOGI(
TAG,
"Try %d ended: no first response for cmd=%s(0x%02X) within %" PRIu32
" ms preamble=%u age_ms=%s",
539 ctx.try_index,
command_name(request.cmd), request.cmd, ctx.wait_ms, ctx.request_preamble, age);
543bool ExchangeEngine::handle_authentication_(
const IoFrame &request, uint32_t freq,
544 exchange::OutboundExchangeContext &ctx) {
545 ctx.saw_challenge =
true;
546 ctx.state = exchange::OutboundExchangeState::BUILD_AUTH_RESPONSE;
547 this->record_debug(outbound_stage_name(ctx.state), ctx.try_index,
true);
552 char age[TRY_AGE_BUFFER_SIZE];
553 format_try_age(ctx, age);
554 ESP_LOGI(TAG,
"Auth challenge try=%d wait_ms=%" PRIu32
" req_cmd=0x%02X req_len=%u preamble=%u age_ms=%s",
555 ctx.try_index, ctx.first_response_ms - ctx.try_start_ms, request.cmd, request.data_len, ctx.request_preamble,
558 ctx.state = exchange::OutboundExchangeState::TX_AUTH_RESPONSE;
559 this->record_debug(outbound_stage_name(ctx.state), ctx.try_index,
true);
562 if (!this->answer_challenge(request, ctx.rx, freq)) {
563 ctx.state = exchange::OutboundExchangeState::FAILED;
564 this->record_debug(
"auth_response_failed", ctx.try_index,
true);
567 this->counters_.challenge_round_trips++;
575 return this->
transmit_frame(auth_resp, freq, (*this->radio_ptr_)->response_preamble());
578uint8_t ExchangeEngine::tries_after_unconfirmed_(
const IoFrame &request, uint8_t unconfirmed_tries, uint8_t try_index,
579 uint32_t elapsed_ms) {
583 if (request.
cmd == CMD_EXECUTE && this->target_evidence_provider_)
584 this->target_evidence_provider_(request.
dst, evidence);
585 if (!decisions::retry_after_unconfirmed_accept_is_safe(request, evidence.
confirms_execute, unconfirmed_tries))
587 if (request.
cmd != CMD_EXECUTE)
588 return EXCHANGE_RETRY_COUNT;
590 if (elapsed_ms + UNCONFIRMED_EXECUTE_RESEND_DELAY_MS >= this->tuning_->exchange_total_budget_ms)
595 "Try %u accepted without a closing reply for cmd=%s(0x%02X): re-sending, the device normally "
596 "confirms (final_rx_ignored=%u final_rx_failed=%u)",
597 try_index, command_name(request.
cmd), request.
cmd, this->debug_.final_rx_ignored,
598 this->debug_.final_rx_failed);
602 delay(UNCONFIRMED_EXECUTE_RESEND_DELAY_MS - EXCHANGE_RETRY_DELAY_MS);
605 return UNCONFIRMED_EXECUTE_MAX_RESENDS;
608decisions::ExchangeFinalResponseDisposition ExchangeEngine::wait_for_final_response_(
609 const IoFrame &request, exchange::OutboundExchangeContext &ctx) {
612 const uint32_t auth_wait_ms = this->tuning_->exchange_response_wait_ms;
615 spec.window_ms = auth_wait_ms;
619 spec.policy = ListenPolicy::HOLD_REQUEST_CHANNEL;
624 RadioRxPacket packet{};
625 auto outcome = this->listen(spec, packet, ctx.rx, [&](
const IoFrame *parsed,
const RadioRxPacket &pkt) {
626 if (parsed == nullptr) {
627 this->record_debug(
"final_parse_fail", ctx.try_index, true);
628 this->counters_.parse_failures++;
629 log_unparsable_frame(
"Unparsable final response", ctx.try_index, pkt);
630 return ReplyDisposition::IGNORE;
632 if (is_valid_final_response(*parsed, request))
633 return ReplyDisposition::ACCEPT;
634 this->record_debug(
"final_wrong_exchange", ctx.try_index,
true);
635 log_exchange_frame(
"Ignored final response", ctx.try_index, *parsed, pkt.len);
636 return ReplyDisposition::IGNORE;
639 this->note_final_wait_(stats);
640 if (outcome == ListenOutcome::ACCEPTED)
641 return decisions::ExchangeFinalResponseDisposition::ACCEPT;
643 ctx.state = exchange::OutboundExchangeState::FAILED;
644 this->record_debug(
"wait_final_timeout", ctx.try_index,
true);
645 ESP_LOGI(TAG,
"Try %d ended: no matching final response for cmd=%s(0x%02X) within %" PRIu32
" ms", ctx.try_index,
647 return decisions::ExchangeFinalResponseDisposition::IGNORE_UNRELATED;
663 const uint32_t tx_done_ms = millis();
688 if (parsed ==
nullptr || parsed->
cmd != expected_cmd || memcmp(parsed->
dst, this->node_id_, NODE_ID_SIZE) != 0)
695 on_reply(*parsed, info);
696 if (count < UINT8_MAX)
722void count_saturating(uint8_t &counter) {
723 if (counter < UINT8_MAX)
732bool dispatch_received_packet(
const ReplyHandler &on_frame,
const RadioRxPacket &packet, IoFrame &frame,
733 ListenOutcome &outcome, ListenStats *stats) {
734 const bool parsed = parse(packet.data, packet.len, frame);
735 switch (on_frame(parsed ? &frame :
nullptr, packet)) {
736 case ReplyDisposition::ACCEPT:
737 outcome = ListenOutcome::ACCEPTED;
739 case ReplyDisposition::ABORT:
740 outcome = ListenOutcome::ABORTED;
742 case ReplyDisposition::IGNORE:
745 if (stats !=
nullptr)
746 count_saturating(stats->frames_ignored);
752void count_failed_reception(ListenStats *stats,
const RadioDriver *radio) {
753 if (stats ==
nullptr)
755 count_saturating(stats->failed_receptions);
756 stats->last_failed_irq = radio->get_last_capture().irq_status;
761bool preamble_or_sync_incoming(RadioDriver *radio,
const ListenSpec &spec) {
762 return spec.linger_on_preamble && (radio->is_preamble_detected() || radio->is_sync_detected());
771uint32_t resolve_dwell_ms(
const ListenSpec &spec,
bool rotating, RadioDriver *radio,
const TuningConfig &tuning) {
774 if (spec.dwell_ms != 0)
775 return spec.dwell_ms;
776 return radio->hop_dwell_ms(tuning);
781void ExchangeEngine::listen_hop_(uint32_t skip,
const ListenSpec &spec) {
782 this->hop_frequency(skip);
790 const uint32_t deadline = millis() + spec.
window_ms;
797 const uint32_t dwell = resolve_dwell_ms(spec, rotating, radio, *this->tuning_);
803 this->listen_hop_(skip, spec);
805 while ((int32_t) (deadline - millis()) > 0) {
806 const uint32_t remaining = deadline - millis();
810 const uint32_t slice = rotating ? std::min(remaining, dwell) : remaining;
814 if (dispatch_received_packet(on_frame, packet, frame, outcome, spec.
stats))
820 !preamble_or_sync_incoming(radio, spec))
821 this->listen_hop_(skip, spec);
825 if ((int32_t) (deadline - millis()) <= 0)
828 count_failed_reception(spec.
stats, radio);
831 if (!preamble_or_sync_incoming(radio, spec)) {
832 this->listen_hop_(skip, spec);
837 const uint32_t ext = std::min((uint32_t) (deadline - millis()), spec.
linger_dwell_ms);
839 if (radio->
wait_for_packet(packet, ext) && dispatch_received_packet(on_frame, packet, frame, outcome, spec.
stats))
870 if (!radio->
wait_for_packet(packet, this->tuning_->exchange_response_wait_ms)) {
877 if (!
parse(packet.
data, packet.
len, rx) || !frame_is_challenge_response(rx)) {
887 uint8_t frame_data[FRAME_MAX_SIZE];
888 frame_data[0] = request.
cmd;
898 this->counters_.challenge_round_trips++;
ListenOutcome listen(const ListenSpec &spec, RadioRxPacket &packet, IoFrame &frame, const ReplyHandler &on_frame)
The one listen primitive every radio wait loop in this project is built on.
WakeBeliefUse
Whether an exchange's start preamble followed the low-power wake belief, and if not,...
@ OVERRIDE
The caller forced a preamble (pairing's directed frames).
@ SWITCHED_OFF
The low_power_wake_belief tuning switch is off.
@ NO_PROVIDER
No evidence source installed (set_target_evidence_provider()).
@ NOT_LOW_POWER
Not a low-power start frame: there is no wake-up preamble to reorder.
@ APPLIED
The tries followed the belief in DebugInfo::wake_belief.
uint8_t collect_broadcast_responses(const IoFrame &request, uint32_t freq, uint8_t expected_cmd, uint32_t window_ms, const BroadcastReplyHandler &on_reply, ListenPolicy policy=ListenPolicy::ROTATE_SKIPPING_REQUEST)
Transmit request once and hand every matching broadcast reply to on_reply within window_ms.
void maybe_hop()
Hop only if the minimum dwell has elapsed and no frame is currently arriving on this channel — RadioD...
void reset_debug(uint8_t request_cmd)
Clear the debug snapshot and record the upcoming request command.
void log_debug_unconfirmed(const char *device_id) const
Log the debug snapshot for an exchange that ended accepted-but-unconfirmed, at INFO.
void log_debug(const char *device_id) const
Log the debug snapshot as a WARN-level structured line.
std::function< void(const IoFrame &frame, const BroadcastReplyInfo &info)> BroadcastReplyHandler
Invoked for each matching broadcast reply, as it arrives.
uint16_t request_preamble_for(const IoFrame &request) const
Preamble length for an outbound request frame.
ExchangeEngine(RadioDriver **radio_ptr, const uint8_t *node_id, const uint8_t *system_key, const TuningConfig *tuning)
Construct the engine with double-pointer indirection into the hub's RadioDriver pointer and direct po...
static const char * wake_belief_use_name(WakeBeliefUse use)
Log label for a WakeBeliefUse that is not APPLIED (an applied one logs the belief itself).
bool answer_challenge(const IoFrame &request, const IoFrame &challenge, uint32_t freq)
Send a 0x3D challenge response over request's transcript, proving knowledge of the system key to whoe...
bool transmit_frame(const IoFrame &frame, uint32_t freq, uint16_t preamble)
Transmit a raw IoFrame with LBT and the given preamble length.
bool authenticate_request(const IoFrame &request, uint32_t freq)
Authenticate an inbound device command via 0x3C challenge / 0x3D HMAC.
ExchangeOutcome send_and_receive(const IoFrame &request, IoFrame &response, uint32_t freq, uint8_t max_tries=EXCHANGE_RETRY_COUNT, uint16_t request_preamble_override=0)
Execute an outbound authenticated exchange with retry.
void reset_hop_timestamp()
Reset the hop-timer (called after radio init in hub setup()).
void hop_frequency(uint32_t skip_freq=0)
Advance the receiver one step along the protocol's channel rotation (CH1→CH2→CH3→CH1).
void record_debug(const char *stage, uint8_t tries, bool saw_challenge)
Update the debug snapshot with the current stage and radio capture.
Abstract radio driver for IO-Homecontrol.
uint32_t get_current_freq() const
Get the current RF frequency.
virtual uint16_t response_preamble() const
Return the preamble length for response/continuation frames.
virtual void change_frequency(uint32_t freq_hz)=0
Change the carrier frequency using fast hop (no standby transition needed).
const RadioCaptureInfo & get_last_capture() const
Get the most recent radio capture info.
virtual bool send_packet(const uint8_t *data, uint8_t len, const RadioTxConfig &tx_config)=0
Send a packet using the specified carrier frequency and preamble settings.
virtual int16_t read_rssi()=0
Read instantaneous RSSI (in dBm) while in RX mode.
virtual bool wait_for_packet(RadioRxPacket &packet, uint32_t timeout_ms)=0
Wait (blocking) for a packet with timeout.
Self-contained authenticated exchange engine for IO-Homecontrol 2W.
Pure transition helpers for hub-owned exchange and pairing frame decisions.
Shared frame logging helpers for IO-Homecontrol.
bool verify_hmac(const uint8_t *data, uint8_t len, const uint8_t hmac[HMAC_SIZE], const uint8_t challenge[HMAC_SIZE], const uint8_t key[AES_KEY_SIZE])
Verify a received HMAC using constant-time comparison.
ExchangeFirstResponseDisposition classify_exchange_first_response(const IoFrame &request, const IoFrame &candidate)
Decide how to handle the first response packet in an authenticated exchange.
@ ACCEPT
Frame matches expected response — exchange succeeds.
bool is_stop_request(const IoFrame &request)
True for a CMD_EXECUTE whose main byte is POS_STOP.
ExchangeFirstResponseDisposition
Disposition for the first response in an authenticated exchange.
@ IGNORE_UNRELATED
Frame doesn't match endpoints or failed parse — keep waiting.
@ COMPLETE_DIRECT
Matching non-challenge frame — operation complete, no auth needed.
const char * wake_belief_name(WakeBelief belief)
Lowercase name of a belief for log lines.
WakeBelief
How likely a low-power receiver is to be awake right now, judged from what this hub has seen of it.
@ ASLEEP
No recent sign of life — lead with the wake-up preamble.
WakeBelief wake_belief(const TargetEvidence &evidence, uint32_t now, bool is_stop)
Judge how awake a low-power receiver is.
ExchangeFinalResponseDisposition classify_exchange_final_response(const IoFrame &request, const IoFrame &candidate)
Decide if a candidate frame is an acceptable final response after authentication.
InboundAuthState
Progress stages of inbound authentication (device‑initiated commands).
@ WAIT_CHALLENGE_RESPONSE
Timer running; waiting for device's HMAC proof (0x3D).
@ VERIFIED
Device successfully authenticated; command is trusted.
@ TX_CHALLENGE
Challenge (0x3C) sent to device; awaiting 0x3D response.
@ IDLE
No inbound authentication in progress.
@ FAILED
Authentication failed (timeout or HMAC mismatch).
OutboundExchangeState
Progress stages of an outbound authenticated exchange (non‑pairing).
@ TX_REQUEST
Request frame transmitted; awaiting first response from device.
@ TX_AUTH_RESPONSE
Auth response (0x3D) transmitted; awaiting device's final reply.
@ IDLE
No active exchange; idle state.
@ BUILD_AUTH_RESPONSE
Building the 0x3D challenge response after receiving 0x3C.
@ FAILED
Exchange failed (timeout, retries exhausted, or radio error).
@ SUCCESS
Exchange completed successfully; device acknowledged.
@ WAIT_FIRST_RESPONSE
Listening for first response. This may be a challenge (0x3C) or the final response.
@ WAIT_FINAL_RESPONSE
Listening for the authenticated final response (e.g., status frame).
static constexpr const char * TAG
bool is_start(const IoFrame &f)
Check START flag.
int render_exchange_debug(char *buf, size_t buf_size, const char *device_id, const ExchangeEngine::DebugInfo &d)
Render the structured field list shared by both exchange-debug log lines.
const char * command_name(uint8_t cmd)
Get a human-readable name for any IO-Homecontrol command ID.
std::function< ReplyDisposition(const IoFrame *parsed, const RadioRxPacket &packet)> ReplyHandler
Invoked for every packet the radio delivers during a listen, before the listen decides whether to kee...
@ ACCEPT
This is the frame the caller was waiting for — stop listening, return ACCEPTED.
@ IGNORE
Unparsable / not ours / wrong exchange — keep listening.
bool create_challenge_resp(IoFrame &f, const uint8_t *dst, const uint8_t *src, const uint8_t challenge[HMAC_SIZE], const IoFrame &origin, const uint8_t *key)
Build a challenge response (0x3D) proving we know the system key.
void render_frame_hex_redacted(const uint8_t *data, uint8_t len, char *out, size_t out_size)
Render a frame's bytes as spaced hex text, masking the payload when the command carries key material ...
ExchangeOutcome
Authenticated exchange engine — outbound and inbound protocol flows.
@ SUCCESS_WITH_RESPONSE
Device replied; the caller's response frame is populated.
@ SUCCESS_UNCONFIRMED
Device authenticated the request — so it received and accepted it — but sent no final response.
@ FAILED
No usable reply; the device may never have heard the request.
constexpr size_t FRAME_LOG_HEX_BUFFER_SIZE
Fits a full 32-byte frame rendered as spaced hex text.
ListenPolicy
Which channels a listen covers.
@ ROTATE_SKIPPING_REQUEST
The two channels that are not the request channel.
@ HOLD_REQUEST_CHANNEL
Never retunes, never slices. Unicast replies.
bool parse(const uint8_t *buf, uint8_t buf_len, IoFrame &f)
Parse a wire buffer into a parsed IoFrame (validates length and CTRL0).
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.
ListenOutcome
How one call to ExchangeEngine::listen() ended.
@ ACCEPTED
The handler returned ReplyDisposition::ACCEPT for some received frame.
@ TIMED_OUT
spec.window_ms elapsed with no ACCEPT/ABORT.
bool create_challenge_req(IoFrame &f, const uint8_t *dst, const uint8_t *src, const uint8_t challenge[HMAC_SIZE])
Build a challenge request (0x3C) using a caller-supplied challenge.
uint8_t serialize(const IoFrame &f, uint8_t *buf, uint8_t buf_size)
Serialize a parsed frame into a wire buffer (without CRC).
static const char *const TAG
Command builders for the IO‑Homecontrol protocol.
IO-Homecontrol command IDs, result codes and protocol enumerations.
Cryptographic helpers for the IO‑Homecontrol protocol.
Per-reply facts collect_broadcast_responses() hands its caller alongside the frame.
uint32_t rx_freq_hz
Channel the reply was received on (RadioRxPacket::freq_hz).
int16_t rssi_dbm
RSSI of this reply (from the radio's last capture).
uint32_t after_tx_ms
Milliseconds from the request's transmit completing to this reply's delivery.
Snapshot of the last exchange attempt for diagnostics.
bool saw_challenge
True if a 0x3C was seen during this exchange.
uint16_t final_rx_irq
Radio IRQ status at the most recent of those failed receptions.
uint8_t capture_reported_len
Length reported by radio packet engine.
int16_t capture_rssi_dbm
RSSI of the captured packet (dBm).
bool capture_crc_error
True if CRC error flagged; see RadioCaptureInfo::crc_error.
uint8_t tries
Retry count (1-based).
uint16_t capture_irq_status
Raw IRQ register value.
uint8_t capture_frame_len
Parsed protocol frame length.
decisions::WakeBelief wake_belief
Belief the tries followed; only meaningful when wake_belief_use is WakeBeliefUse::APPLIED.
uint8_t capture_packet_status
Chip packet-status byte.
uint8_t final_rx_ignored
Frames received during those waits and not accepted as the reply.
bool capture_valid
True if radio capture is meaningful.
uint8_t request_cmd
Command ID of the original request.
uint16_t last_try_preamble
Preamble (bytes) of the most recent request transmit attempt, 0 = none.
bool capture_rx_done
True if RxDone IRQ fired.
WakeBeliefUse wake_belief_use
Whether the tries followed wake_belief, and why not if they did not.
uint8_t final_rx_failed
Receptions started during those waits that could not be decoded.
const char * stage
Last recorded stage label.
uint32_t capture_freq_hz
RF frequency of the captured packet.
uint8_t max_tries
Attempt cap this exchange was budgeted for.
uint8_t final_waits
Final-reply waits run (one per try that got as far as our 0x3D).
Parsed IO‑Homecontrol frame (CTRL0/1 + addresses + command + data).
uint8_t data[FRAME_MAX_DATA_SIZE]
Command parameters (0–23 bytes). Never includes mac.
uint8_t src[NODE_ID_SIZE]
Source node ID (3 bytes).
uint8_t dst[NODE_ID_SIZE]
Destination node ID (3 bytes).
uint8_t data_len
Actual length of data.
How one listen window is to be spent — everything ExchangeEngine::listen() needs; everything else is ...
uint32_t request_freq
Channel the request went out on (Hz).
uint32_t window_ms
Total time budget for this listen, in milliseconds.
uint32_t linger_dwell_ms
Length of the preamble/sync extension, in milliseconds.
ListenStats * stats
Where to count what this listen heard without accepting it; null (the default) counts nothing.
ListenPolicy policy
Which channels this listen covers.
bool linger_on_preamble
Extend the listen instead of hopping while the chip reports a preamble or sync word,...
bool hop_after_ignored_frame
Hop after a frame the handler ignored.
What a listen heard without accepting it — filled by ExchangeEngine::listen() when a caller passes on...
uint16_t last_failed_irq
Radio IRQ status captured at the most recent failed reception.
uint8_t frames_ignored
Frames the radio delivered that the handler did not accept (wrong exchange, unrelated traffic,...
uint8_t failed_receptions
Receptions the radio started but could not deliver: a holding listen's early return before its deadli...
Diagnostic capture from a radio operation.
uint8_t frame_len
Number of valid bytes in frame[].
uint16_t irq_status
Raw IRQ status register value.
uint8_t packet_status
Packet status byte (chip-specific).
bool crc_error
True if a CRC error was detected.
bool valid
True if capture is valid.
uint8_t reported_len
Length reported by the radio chip.
bool rx_done
True if RxDone IRQ fired.
uint32_t freq_hz
RF frequency of capture (Hz).
int16_t rssi_dbm
Received signal strength (dBm).
Raw packet received from the radio.
uint8_t len
Length of packet in bytes.
uint32_t freq_hz
Frequency the packet was received on (Hz).
uint8_t data[RADIO_PACKET_BUFFER_SIZE]
Raw packet data buffer.
Configuration for transmitting a packet: carrier frequency and preamble length.
uint16_t preamble_len
Preamble length in symbol periods (bytes).
uint32_t freq_hz
Carrier frequency in Hz.
All runtime tunable parameters for pairing and radio diagnostics.
bool low_power_wake_belief
Order a low_power device's start-frame tries by its wake belief (short preamble first when it is beli...
What the hub knows about the device an exchange is addressed to, as the exchange engine sees it.
bool confirms_execute
The device has closed a CMD_EXECUTE exchange with a reply before.
Context for a single inbound authentication (device‑initiated command).
IoFrame challenge
The 0x3C challenge frame we sent (needed to verify 0x3D response).
InboundAuthState state
Current authentication state.
Context carried across one outbound authenticated exchange.
uint32_t first_response_ms
Timestamp when the first valid response arrived (for RTT/timing).
uint16_t request_preamble
Start preamble (bytes) this try's request went out with.
uint8_t try_index
Current retry attempt (1‑based within EXCHANGE_RETRY_COUNT).
uint32_t try_start_ms
millis() when this try's request transmit began, after any retry gap.
IoFrame rx
Most recent candidate frame received during the exchange.
uint32_t wait_ms
Current timeout window for the active wait (ms).
OutboundExchangeState state
Current state machine state.
uint32_t target_last_seen_ms
When the target was last heard (millis), 0 = unknown; logged as the try's age_ms= so field logs can r...