Home IO Control
ESPHome add-on for IO-Homecontrol devices
Loading...
Searching...
No Matches
hub_pairing.h
Go to the documentation of this file.
1#pragma once
2
3/// @file hub_pairing.h
4/// @brief Internal pairing-state model for hub‑owned discovery and key‑exchange flows.
5/// @ingroup hioc_hub
6///
7/// This header defines the state machine enum and context structures for the
8/// three‑phase pairing procedure (the flow itself is implemented by PairingEngine),
9/// which temporarily tracks a newly paired device in the controller's runtime
10/// registry and installs the system key on the device:
11///
12/// Phase 1 — Discovery (broadcast 0x28 → device responds 0x29):
13/// Controller broadcasts a discovery packet on the primary channel. A device in
14/// pairing mode (PROG button pressed) responds with its node ID and type/subtype.
15/// The controller validates the response and extracts device metadata.
16///
17/// Phase 2 — Authenticated Key Exchange (0x31 → 0x3C → 0x32 → 0x33):
18/// The controller sends CMD_KEY_INIT (0x31) to the device. The device challenges
19/// with CMD_CHALLENGE_REQ (0x3C). The controller proves knowledge of the system
20/// key with CMD_CHALLENGE_RESP (0x3D) and simultaneously sends the encrypted system
21/// key via CMD_KEY_TRANSFER (0x32). The device confirms with CMD_KEY_CONFIRM (0x33).
22///
23/// Phase 3 — Configuration (CMD_SET_CONFIG1):
24/// The controller sends a configuration frame (0x6F) to enable automatic status
25/// updates from the device. This phase completes even if the config frame fails;
26/// the device will still operate in polled mode.
27///
28/// All frames use the standard authenticated exchange flow (state types in
29/// hub_exchange.h, implementation in ExchangeEngine). PairingEngine serializes these
30/// phases, logs the YAML metadata the user should add, and keeps the paired device
31/// in the current runtime registry until reboot.
32
33#include "proto_device_model.h"
34#include "proto_frame.h"
35#include "radio_interface.h"
36#include <cstdint>
37#include <string>
38
39namespace esphome {
40namespace home_io_control {
41
42namespace pairing {
43
44/// @brief State machine for the three‑phase pairing flow.
45enum class PairingState : uint8_t {
46 IDLE, ///< No pairing in progress; idle state.
47 TX_DISCOVER, ///< Discovery broadcast (0x28) sent; awaiting device response.
48 WAIT_DISCOVER_RESPONSE, ///< Listening for discovery response (0x29) from a device in pairing mode.
49 TX_KEY_INIT, ///< Key‑init (0x31) sent to the discovered device.
50 WAIT_KEY_CHALLENGE, ///< Waiting for challenge (0x3C) from device as part of key transfer.
51 TX_KEY_TRANSFER, ///< Key‑transfer (0x32) sent with encrypted system key.
52 WAIT_KEY_CONFIRM, ///< Waiting for key‑confirm (0x33) from device (key receipt acknowledgement).
53 REGISTER_DEVICE, ///< Registering device in the runtime registry for the current boot.
54 COMPLETE, ///< Pairing completed successfully; device ready for use.
55 FAILED, ///< Pairing failed (timeout, radio error, or protocol violation).
56};
57
58/// @brief Context object that lives for the duration of a single pairing attempt.
60 PairingState state{PairingState::IDLE}; ///< Current state machine state.
61 IoDevice device{}; ///< Resolved device metadata after discovery (node_id, type, subtype, etc.).
62 IoFrame req{}; ///< Outbound frame buffer (reused across all phases).
63 IoFrame resp{}; ///< Inbound frame buffer (holds key‑confirm response).
64 IoFrame rx{}; ///< Raw RX frame during waiting phases (discovery, challenge, confirm).
65 IoFrame key_init{}; ///< Key‑init frame retained for key‑transfer IV derivation.
66 RadioRxPacket packet{}; ///< Raw radio capture for the current phase.
67 std::string device_id; ///< Hex string representation of the paired node ID.
68 bool discovery_metadata_complete{false}; ///< True when discovery carried type/subtype bytes.
69};
70
71} // namespace pairing
72
73/// @brief Get a short, log/telemetry-friendly name for a pairing state.
74/// @param state Pairing state.
75/// @return Null-terminated lowercase string such as "wait_key_challenge".
76inline const char *pairing_stage_name(pairing::PairingState state) {
77 switch (state) {
79 return "idle";
81 return "tx_discover";
83 return "wait_discover_response";
85 return "tx_key_init";
87 return "wait_key_challenge";
89 return "tx_key_transfer";
91 return "wait_key_confirm";
93 return "register_device";
95 return "complete";
97 default:
98 return "failed";
99 }
100}
101
102} // namespace home_io_control
103} // namespace esphome
PairingState
State machine for the three‑phase pairing flow.
Definition hub_pairing.h:45
@ REGISTER_DEVICE
Registering device in the runtime registry for the current boot.
Definition hub_pairing.h:53
@ TX_DISCOVER
Discovery broadcast (0x28) sent; awaiting device response.
Definition hub_pairing.h:47
@ WAIT_DISCOVER_RESPONSE
Listening for discovery response (0x29) from a device in pairing mode.
Definition hub_pairing.h:48
@ COMPLETE
Pairing completed successfully; device ready for use.
Definition hub_pairing.h:54
@ WAIT_KEY_CHALLENGE
Waiting for challenge (0x3C) from device as part of key transfer.
Definition hub_pairing.h:50
@ WAIT_KEY_CONFIRM
Waiting for key‑confirm (0x33) from device (key receipt acknowledgement).
Definition hub_pairing.h:52
@ TX_KEY_INIT
Key‑init (0x31) sent to the discovered device.
Definition hub_pairing.h:49
@ TX_KEY_TRANSFER
Key‑transfer (0x32) sent with encrypted system key.
Definition hub_pairing.h:51
@ IDLE
No pairing in progress; idle state.
Definition hub_pairing.h:46
@ FAILED
Pairing failed (timeout, radio error, or protocol violation).
Definition hub_pairing.h:55
const char * pairing_stage_name(pairing::PairingState state)
Get a short, log/telemetry-friendly name for a pairing state.
Definition hub_pairing.h:76
IO-Homecontrol device-type model, capabilities and runtime device state.
IO-Homecontrol 2W frame container: control bytes, IoFrame and (de)serialization.
Radio abstraction layer for IO-Homecontrol.
Runtime state of a paired IO‑Homecontrol device.
Parsed IO‑Homecontrol frame (CTRL0/1 + addresses + command + data).
Definition proto_frame.h:71
Raw packet received from the radio.
Context object that lives for the duration of a single pairing attempt.
Definition hub_pairing.h:59
IoFrame req
Outbound frame buffer (reused across all phases).
Definition hub_pairing.h:62
PairingState state
Current state machine state.
Definition hub_pairing.h:60
IoFrame resp
Inbound frame buffer (holds key‑confirm response).
Definition hub_pairing.h:63
RadioRxPacket packet
Raw radio capture for the current phase.
Definition hub_pairing.h:66
IoDevice device
Resolved device metadata after discovery (node_id, type, subtype, etc.).
Definition hub_pairing.h:61
IoFrame key_init
Key‑init frame retained for key‑transfer IV derivation.
Definition hub_pairing.h:65
std::string device_id
Hex string representation of the paired node ID.
Definition hub_pairing.h:67
bool discovery_metadata_complete
True when discovery carried type/subtype bytes.
Definition hub_pairing.h:68
IoFrame rx
Raw RX frame during waiting phases (discovery, challenge, confirm).
Definition hub_pairing.h:64