Home IO Control
ESPHome add-on for IO-Homecontrol devices
Loading...
Searching...
No Matches
tuning_registry.cpp
Go to the documentation of this file.
1/// @file tuning_registry.cpp
2/// @brief Parameter tables and lookup helpers for the tuning registry.
3/// @ingroup hioc_tuning
4///
5/// The getter/setter lambdas below carry all per-parameter parsing (bandwidth enum
6/// decode, the comma-separated command list with whitespace trimming, the
7/// destination/payload/low-power mapping). Keeping them here makes the hub's tuning
8/// dispatch methods pure table lookups.
9
10#include "tuning_registry.h"
11
12#include "proto_codecs.h" // ADDRESS_SUFFIX_DISCOVERY / ADDRESS_SUFFIX_BROADCAST
13#include "proto_device_model.h" // DeviceType / DEVICE_SUBTYPE_MASK / encode_broadcast_address()
14
15#include <cstddef>
16#include <iterator>
17
18namespace esphome {
19namespace home_io_control {
20
21// === Numeric parameters (20) ===================================================
22// Each row narrows the incoming float to the field's storage type, matching the
23// original static_cast in update_tuning_number().
24static constexpr TuningNumberParam NUMBER_PARAMS[] = {
25 {"sx1262_response_preamble", [](const TuningConfig &t) { return static_cast<float>(t.sx1262_response_preamble); },
26 [](TuningConfig &t, float v) { t.sx1262_response_preamble = static_cast<uint16_t>(v); }, true},
27 {"sx1262_post_tx_settle_us", [](const TuningConfig &t) { return static_cast<float>(t.sx1262_post_tx_settle_us); },
28 [](TuningConfig &t, float v) { t.sx1262_post_tx_settle_us = static_cast<uint16_t>(v); }, true},
29 {"sx1276_response_preamble", [](const TuningConfig &t) { return static_cast<float>(t.sx1276_response_preamble); },
30 [](TuningConfig &t, float v) { t.sx1276_response_preamble = static_cast<uint16_t>(v); }, true},
31 {"sx1276_discovery_hop_slice_ms",
32 [](const TuningConfig &t) { return static_cast<float>(t.sx1276_discovery_hop_slice_ms); },
33 [](TuningConfig &t, float v) { t.sx1276_discovery_hop_slice_ms = static_cast<uint16_t>(v); }, false},
34 {"sx1262_discovery_hop_slice_ms",
35 [](const TuningConfig &t) { return static_cast<float>(t.sx1262_discovery_hop_slice_ms); },
36 [](TuningConfig &t, float v) { t.sx1262_discovery_hop_slice_ms = static_cast<uint16_t>(v); }, false},
37 {"lr1121_response_preamble", [](const TuningConfig &t) { return static_cast<float>(t.lr1121_response_preamble); },
38 [](TuningConfig &t, float v) { t.lr1121_response_preamble = static_cast<uint16_t>(v); }, true},
39 {"lr1121_post_tx_settle_us", [](const TuningConfig &t) { return static_cast<float>(t.lr1121_post_tx_settle_us); },
40 [](TuningConfig &t, float v) { t.lr1121_post_tx_settle_us = static_cast<uint16_t>(v); }, true},
41 {"lr1121_discovery_hop_slice_ms",
42 [](const TuningConfig &t) { return static_cast<float>(t.lr1121_discovery_hop_slice_ms); },
43 [](TuningConfig &t, float v) { t.lr1121_discovery_hop_slice_ms = static_cast<uint16_t>(v); }, false},
44 // false: unlike sx1262/sx1276/lr1121_response_preamble, this field is not chip-specific and
45 // is never cached into a radio driver via apply_tuning() — key_extraction_responder.cpp reads it
46 // straight out of TuningConfig at TX time, the same way the *_discovery_hop_slice_ms fields
47 // above are read live rather than applied.
48 {"cold_broadcast_reply_preamble",
49 [](const TuningConfig &t) { return static_cast<float>(t.cold_broadcast_reply_preamble); },
50 [](TuningConfig &t, float v) { t.cold_broadcast_reply_preamble = static_cast<uint16_t>(v); }, false},
51 // false: not chip-specific and never cached into a radio driver via apply_tuning() — the
52 // exchange engine reads it straight out of TuningConfig at TX time, the same as
53 // cold_broadcast_reply_preamble above.
54 {"normal_start_preamble", [](const TuningConfig &t) { return static_cast<float>(t.normal_start_preamble); },
55 [](TuningConfig &t, float v) { t.normal_start_preamble = static_cast<uint16_t>(v); }, false},
56 {"low_power_wake_preamble", [](const TuningConfig &t) { return static_cast<float>(t.low_power_wake_preamble); },
57 [](TuningConfig &t, float v) { t.low_power_wake_preamble = static_cast<uint16_t>(v); }, false},
58 {"lbt_max_retries", [](const TuningConfig &t) { return static_cast<float>(t.lbt_max_retries); },
59 [](TuningConfig &t, float v) { t.lbt_max_retries = static_cast<uint8_t>(v); }, false},
60 {"lbt_rssi_threshold_dbm", [](const TuningConfig &t) { return static_cast<float>(t.lbt_rssi_threshold_dbm); },
61 [](TuningConfig &t, float v) { t.lbt_rssi_threshold_dbm = static_cast<int16_t>(v); }, false},
62 {"exchange_start_response_wait_ms",
63 [](const TuningConfig &t) { return static_cast<float>(t.exchange_start_response_wait_ms); },
64 [](TuningConfig &t, float v) { t.exchange_start_response_wait_ms = static_cast<uint16_t>(v); }, false},
65 {"exchange_response_wait_ms", [](const TuningConfig &t) { return static_cast<float>(t.exchange_response_wait_ms); },
66 [](TuningConfig &t, float v) { t.exchange_response_wait_ms = static_cast<uint16_t>(v); }, false},
67 {"exchange_total_budget_ms", [](const TuningConfig &t) { return static_cast<float>(t.exchange_total_budget_ms); },
68 [](TuningConfig &t, float v) { t.exchange_total_budget_ms = static_cast<uint16_t>(v); }, false},
69 // false: read straight out of TuningConfig at TX time by PairingEngine::run_discovery_phase_(),
70 // the same as normal_start_preamble/cold_broadcast_reply_preamble above.
71 {"pairing_discovery_preamble",
72 [](const TuningConfig &t) { return static_cast<float>(t.pairing_discovery_preamble); },
73 [](TuningConfig &t, float v) { t.pairing_discovery_preamble = static_cast<uint16_t>(v); }, false},
74 {"pairing_discovery_wait_ms", [](const TuningConfig &t) { return static_cast<float>(t.pairing_discovery_wait_ms); },
75 [](TuningConfig &t, float v) { t.pairing_discovery_wait_ms = static_cast<uint16_t>(v); }, false},
76 {"pairing_discovery_initial_dwell_ms",
77 [](const TuningConfig &t) { return static_cast<float>(t.pairing_discovery_initial_dwell_ms); },
78 [](TuningConfig &t, float v) { t.pairing_discovery_initial_dwell_ms = static_cast<uint16_t>(v); }, false},
79 {"pairing_key_exchange_retries",
80 [](const TuningConfig &t) { return static_cast<float>(t.pairing_key_exchange_retries); },
81 [](TuningConfig &t, float v) { t.pairing_key_exchange_retries = static_cast<uint8_t>(v); }, false},
82 // false: read straight out of TuningConfig by PairingEngine::run_discover_confirm_step_() at
83 // the point of the pause, the same as pairing_discovery_preamble/pairing_discovery_wait_ms above.
84 {"pairing_key_init_delay_ms", [](const TuningConfig &t) { return static_cast<float>(t.pairing_key_init_delay_ms); },
85 [](TuningConfig &t, float v) { t.pairing_key_init_delay_ms = static_cast<uint16_t>(v); }, false},
86};
87
88// Boolean select rows share one get/set shape (an On/Off toggle over a single TuningConfig
89// field). Template functions, not lambdas: a TuningSelectParam's get/set are plain function
90// pointers with no room for a per-row capture, and instantiating on the member pointer keeps
91// each row below a one-liner without duplicating the On/Off parse.
92template<bool TuningConfig::*Field> std::string bool_select_get(const TuningConfig &t) {
93 return t.*Field ? "On" : "Off";
94}
95template<bool TuningConfig::*Field> bool bool_select_set(TuningConfig &t, const std::string &v) {
96 if (v != "On" && v != "Off")
97 return false;
98 t.*Field = (v == "On");
99 return true;
100}
101
102// === Select parameters (10) ====================================================
103// The setters return false only when the option string is unparseable AND leaving the
104// value untouched is the intended behavior (currently the bandwidth enum and the boolean
105// rows). The destination/payload setters return false for unrecognized values so no radio
106// re-apply is triggered; that has no observable effect since neither applies to the radio.
107static constexpr TuningSelectParam SELECT_PARAMS[] = {
108 {"sx1262_rx_bandwidth", [](const TuningConfig &t) { return sx1262_bandwidth_to_string(t.sx1262_rx_bandwidth); },
109 [](TuningConfig &t, const std::string &v) -> bool {
110 auto bw = sx1262_bandwidth_from_string(v);
111 if (!bw.has_value())
112 return false;
113 t.sx1262_rx_bandwidth = bw.value();
114 return true;
115 },
116 true},
117 {"sx1276_rx_bandwidth", [](const TuningConfig &t) { return sx1276_bandwidth_to_string(t.sx1276_rx_bandwidth); },
118 [](TuningConfig &t, const std::string &v) -> bool {
119 auto bw = sx1276_bandwidth_from_string(v);
120 if (!bw.has_value())
121 return false;
122 t.sx1276_rx_bandwidth = bw.value();
123 return true;
124 },
125 true},
126 {"lr1121_rx_bandwidth", [](const TuningConfig &t) { return lr1121_bandwidth_to_string(t.lr1121_rx_bandwidth); },
127 [](TuningConfig &t, const std::string &v) -> bool {
128 auto bw = lr1121_bandwidth_from_string(v);
129 if (!bw.has_value())
130 return false;
131 t.lr1121_rx_bandwidth = bw.value();
132 return true;
133 },
134 true},
137 {"pairing_discovery_commands",
138 [](const TuningConfig &t) { return discovery_commands_to_csv(t.pairing_discovery_commands); },
139 [](TuningConfig &t, const std::string &v) -> bool {
141 // Support both individual commands and comma-separated preset strings.
142 std::size_t start = 0;
143 while (start < v.size()) {
144 const std::size_t comma = v.find(',', start);
145 std::string token = v.substr(start, comma - start);
146 // Trim whitespace.
147 token.erase(0, token.find_first_not_of(" \t"));
148 token.erase(token.find_last_not_of(" \t") + 1);
149 auto cmd = discovery_command_from_string(token);
150 if (cmd.has_value())
151 t.pairing_discovery_commands.push_back(cmd.value());
152 if (comma == std::string::npos)
153 break;
154 start = comma + 1;
155 }
156 return true;
157 },
158 false},
159 {"pairing_discovery_destination",
160 [](const TuningConfig &t) {
161 return discovery_destination_to_string(t.pairing_discovery_destination_auto,
162 t.pairing_discovery_destination.data());
163 },
164 [](TuningConfig &t, const std::string &v) -> bool {
165 if (v == "auto") {
167 return true;
168 }
169 if (v == "0x00003B") {
171 t.pairing_discovery_destination = {0x00, 0x00, ADDRESS_SUFFIX_DISCOVERY};
172 return true;
173 }
174 if (v == "0x00003F") {
176 t.pairing_discovery_destination = {0x00, 0x00, ADDRESS_SUFFIX_BROADCAST};
177 return true;
178 }
179 // Typed lighting-class broadcasts. encode_broadcast_address() builds the ADDRESS_SUFFIX_
180 // BROADCAST form; the discovery form differs only in that suffix, so it is re-masked rather
181 // than hand-written as literal bytes.
182 if (v == "0x0001BF" || v == "0x0001BB") {
183 uint8_t typed[NODE_ID_SIZE] = {0};
185 if (v == "0x0001BB")
186 typed[2] = static_cast<uint8_t>((typed[2] & ~DEVICE_SUBTYPE_MASK) | ADDRESS_SUFFIX_DISCOVERY);
188 t.pairing_discovery_destination.assign(std::begin(typed), std::end(typed));
189 return true;
190 }
191 return false;
192 },
193 false},
194 {"pairing_discovery_payload",
195 [](const TuningConfig &t) {
196 return discovery_payload_to_string(t.pairing_discovery_payload_enabled, t.pairing_discovery_payload);
197 },
198 [](TuningConfig &t, const std::string &v) -> bool {
199 if (v == "none") {
201 return true;
202 }
203 if (v == "0x00") {
206 return true;
207 }
208 return false;
209 },
210 false},
215 {"scan_power_classes", [](const TuningConfig &t) { return scan_power_classes_to_string(t.scan_power_classes); },
216 [](TuningConfig &t, const std::string &v) -> bool {
217 auto selection = scan_power_classes_from_string(v);
218 if (!selection.has_value())
219 return false;
220 t.scan_power_classes = selection.value();
221 return true;
222 },
223 false},
224 {"pairing_discover_confirm",
225 [](const TuningConfig &t) { return discover_confirm_mode_to_string(t.pairing_discover_confirm); },
226 [](TuningConfig &t, const std::string &v) -> bool {
228 if (!mode.has_value())
229 return false;
230 t.pairing_discover_confirm = mode.value();
231 return true;
232 },
233 false},
234 {"pairing_discovery_listen_channels",
235 [](const TuningConfig &t) { return discovery_listen_channels_to_string(t.pairing_discovery_listen_channels); },
236 [](TuningConfig &t, const std::string &v) -> bool {
237 auto channels = discovery_listen_channels_from_string(v);
238 if (!channels.has_value())
239 return false;
240 t.pairing_discovery_listen_channels = channels.value();
241 return true;
242 },
243 false},
244 // false: read straight out of TuningConfig by PairingEngine::discover_and_pair() when the key
245 // step starts, so a change applies to the next pairing attempt without a re-apply.
246 {"pairing_key_transfer", [](const TuningConfig &t) { return key_transfer_mode_to_string(t.pairing_key_transfer); },
247 [](TuningConfig &t, const std::string &v) -> bool {
248 auto mode = key_transfer_mode_from_string(v);
249 if (!mode.has_value())
250 return false;
251 t.pairing_key_transfer = mode.value();
252 return true;
253 },
254 false},
255};
256
257const TuningNumberParam *find_tuning_number(const std::string &name) {
258 for (const auto &param : NUMBER_PARAMS) {
259 if (name == param.name)
260 return &param;
261 }
262 return nullptr;
263}
264
265const TuningSelectParam *find_tuning_select(const std::string &name) {
266 for (const auto &param : SELECT_PARAMS) {
267 if (name == param.name)
268 return &param;
269 }
270 return nullptr;
271}
272
277
278} // namespace home_io_control
279} // namespace esphome
std::string discovery_commands_to_csv(const std::vector< DiscoveryCommand > &commands)
Format the ordered discovery command list as a UI/preset option string.
std::optional< LR1121RxBandwidth > lr1121_bandwidth_from_string(const std::string &value)
Convert a YAML LR1121 bandwidth string to the enum value.
const TuningNumberParam * tuning_number_params_end()
void encode_broadcast_address(DeviceType type, uint8_t out[NODE_ID_SIZE])
Encode a device type into its typed-broadcast destination address — the exact inverse of broadcast_ta...
std::string bool_select_get(const TuningConfig &t)
const TuningSelectParam * tuning_select_params_end()
std::string discovery_payload_to_string(bool payload_enabled, uint8_t payload)
Format a payload option for YAML/logs.
std::optional< DiscoveryCommand > discovery_command_from_string(const std::string &value)
Parse a discovery command string (e.g., "0x28") into the enum.
std::optional< DiscoverConfirmMode > discover_confirm_mode_from_string(const std::string &value)
Parse a pairing_discover_confirm string into the enum.
std::string lr1121_bandwidth_to_string(LR1121RxBandwidth bw)
Format an LR1121 bandwidth enum as its YAML/UI option string (bare kHz number).
std::string key_transfer_mode_to_string(KeyTransferMode value)
Render a KeyTransferMode as its YAML/select option string.
std::optional< ScanPowerClasses > scan_power_classes_from_string(const std::string &value)
Parse a scan_power_classes string into the enum.
std::optional< KeyTransferMode > key_transfer_mode_from_string(const std::string &value)
Parse a KeyTransferMode from its YAML/select option string.
bool bool_select_set(TuningConfig &t, const std::string &v)
std::optional< SX1262RxBandwidth > sx1262_bandwidth_from_string(const std::string &value)
Convert a YAML bandwidth string to the enum value.
std::string sx1276_bandwidth_to_string(SX1276RxBandwidth bw)
Format an SX1276 bandwidth enum as its YAML/UI option string (bare kHz number).
std::optional< DiscoveryListenChannels > discovery_listen_channels_from_string(const std::string &value)
Parse a DiscoveryListenChannels from its YAML/select option string.
const TuningNumberParam * tuning_number_params_begin()
std::string sx1262_bandwidth_to_string(SX1262RxBandwidth bw)
Format a bandwidth enum as its YAML/UI option string (bare kHz number, e.g.
const TuningSelectParam * find_tuning_select(const std::string &name)
Look up a select tuning parameter by name; returns nullptr if unknown.
const TuningSelectParam * tuning_select_params_begin()
std::string discovery_listen_channels_to_string(DiscoveryListenChannels value)
Render a DiscoveryListenChannels as its YAML/select option string.
static constexpr TuningSelectParam SELECT_PARAMS[]
const TuningNumberParam * find_tuning_number(const std::string &name)
Look up a numeric tuning parameter by name; returns nullptr if unknown.
static constexpr TuningNumberParam NUMBER_PARAMS[]
std::optional< SX1276RxBandwidth > sx1276_bandwidth_from_string(const std::string &value)
Convert a YAML SX1276 bandwidth string to the enum value.
std::string discovery_destination_to_string(bool destination_auto, const uint8_t destination[NODE_ID_SIZE])
Format a destination option for YAML/logs.
std::string discover_confirm_mode_to_string(DiscoverConfirmMode value)
Format a DiscoverConfirmMode value for YAML/logs.
std::string scan_power_classes_to_string(ScanPowerClasses value)
Format a ScanPowerClasses value for YAML/logs.
Device-name, address-classification and 1W-frame codecs.
IO-Homecontrol device-type model, capabilities and runtime device state.
All runtime tunable parameters for pairing and radio diagnostics.
uint16_t pairing_discovery_initial_dwell_ms
Initial dwell on CH2 before discovery hopping begins.
bool pairing_discovery_destination_auto
When true, map commands to conventional addresses.
SX1276RxBandwidth sx1276_rx_bandwidth
SX1276 RX bandwidth selector.
uint16_t normal_start_preamble
Preamble for a directed start frame to a non-low-power target.
int16_t lbt_rssi_threshold_dbm
LBT channel-free threshold (dBm).
SX1262RxBandwidth sx1262_rx_bandwidth
SX1262 RX bandwidth selector.
uint16_t low_power_wake_preamble
Wake-up preamble (bytes) on a directed start frame to a low_power target believed at rest.
uint16_t cold_broadcast_reply_preamble
Preamble for a start-flagged key-extraction broadcast reply (0x29).
std::vector< uint8_t > pairing_discovery_destination
Destination when auto=false.
std::vector< DiscoveryCommand > pairing_discovery_commands
Ordered discovery commands.
DiscoveryListenChannels pairing_discovery_listen_channels
Channels the discovery response wait covers.
uint16_t lr1121_discovery_hop_slice_ms
Per-channel dwell while LR1121 discovery hops.
uint16_t sx1276_response_preamble
SX1276 response preamble in bytes.
uint16_t exchange_response_wait_ms
Reply window for continuation frames and the post-auth final response.
uint16_t sx1262_post_tx_settle_us
Delay after SX1262 TX before RX (µs).
uint8_t pairing_key_exchange_retries
Retries for the authenticated key exchange phase.
uint16_t pairing_key_init_delay_ms
Pause after the discover-confirm step, before CMD_KEY_INIT (0x31).
DiscoverConfirmMode pairing_discover_confirm
Whether/how to send CMD_DISCOVER_CONFIRM (0x2C) during pairing.
uint16_t sx1276_discovery_hop_slice_ms
Per-channel dwell while SX1276 discovery hops.
uint16_t exchange_start_response_wait_ms
Reply window for a start frame (wakes a sleeping device).
KeyTransferMode pairing_key_transfer
Push our key, or pull the device's key.
uint16_t sx1262_response_preamble
SX1262 response preamble in bytes.
uint16_t pairing_discovery_preamble
Preamble for the discovery broadcast (0x28/0x2E) start frame.
uint16_t pairing_discovery_wait_ms
Total wait window after sending discovery commands.
bool pairing_discovery_payload_enabled
Whether the optional payload is enabled.
LR1121RxBandwidth lr1121_rx_bandwidth
LR1121 RX bandwidth selector.
uint16_t lr1121_post_tx_settle_us
Delay after LR1121 TX before RX (µs).
uint8_t lbt_max_retries
LBT retries before forced TX.
uint16_t sx1262_discovery_hop_slice_ms
Per-channel dwell while SX1262 discovery hops.
uint16_t exchange_total_budget_ms
Ceiling on one whole exchange, retries included.
uint16_t lr1121_response_preamble
LR1121 response preamble in bytes.
uint8_t pairing_discovery_payload
Optional payload byte (used for 0x2E).
ScanPowerClasses scan_power_classes
Power classes the scan_paired_devices roll-call calls.
One numeric tuning parameter: its wire name plus accessors over TuningConfig.
One select tuning parameter: its wire name plus string accessors over TuningConfig.
Table-driven registry of runtime tuning parameters.