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 {"lbt_max_retries", [](const TuningConfig &t) { return static_cast<float>(t.lbt_max_retries); },
57 [](TuningConfig &t, float v) { t.lbt_max_retries = static_cast<uint8_t>(v); }, false},
58 {"lbt_rssi_threshold_dbm", [](const TuningConfig &t) { return static_cast<float>(t.lbt_rssi_threshold_dbm); },
59 [](TuningConfig &t, float v) { t.lbt_rssi_threshold_dbm = static_cast<int16_t>(v); }, false},
60 {"exchange_start_response_wait_ms",
61 [](const TuningConfig &t) { return static_cast<float>(t.exchange_start_response_wait_ms); },
62 [](TuningConfig &t, float v) { t.exchange_start_response_wait_ms = static_cast<uint16_t>(v); }, false},
63 {"exchange_response_wait_ms", [](const TuningConfig &t) { return static_cast<float>(t.exchange_response_wait_ms); },
64 [](TuningConfig &t, float v) { t.exchange_response_wait_ms = static_cast<uint16_t>(v); }, false},
65 {"exchange_total_budget_ms", [](const TuningConfig &t) { return static_cast<float>(t.exchange_total_budget_ms); },
66 [](TuningConfig &t, float v) { t.exchange_total_budget_ms = static_cast<uint16_t>(v); }, false},
67 // false: read straight out of TuningConfig at TX time by PairingEngine::run_discovery_phase_(),
68 // the same as normal_start_preamble/cold_broadcast_reply_preamble above.
69 {"pairing_discovery_preamble",
70 [](const TuningConfig &t) { return static_cast<float>(t.pairing_discovery_preamble); },
71 [](TuningConfig &t, float v) { t.pairing_discovery_preamble = static_cast<uint16_t>(v); }, false},
72 {"pairing_discovery_wait_ms", [](const TuningConfig &t) { return static_cast<float>(t.pairing_discovery_wait_ms); },
73 [](TuningConfig &t, float v) { t.pairing_discovery_wait_ms = static_cast<uint16_t>(v); }, false},
74 {"pairing_discovery_initial_dwell_ms",
75 [](const TuningConfig &t) { return static_cast<float>(t.pairing_discovery_initial_dwell_ms); },
76 [](TuningConfig &t, float v) { t.pairing_discovery_initial_dwell_ms = static_cast<uint16_t>(v); }, false},
77 {"pairing_key_exchange_retries",
78 [](const TuningConfig &t) { return static_cast<float>(t.pairing_key_exchange_retries); },
79 [](TuningConfig &t, float v) { t.pairing_key_exchange_retries = static_cast<uint8_t>(v); }, false},
80 // false: read straight out of TuningConfig by PairingEngine::run_discover_confirm_step_() at
81 // the point of the pause, the same as pairing_discovery_preamble/pairing_discovery_wait_ms above.
82 {"pairing_key_init_delay_ms", [](const TuningConfig &t) { return static_cast<float>(t.pairing_key_init_delay_ms); },
83 [](TuningConfig &t, float v) { t.pairing_key_init_delay_ms = static_cast<uint16_t>(v); }, false},
84};
85
86// Boolean select rows share one get/set shape (an On/Off toggle over a single TuningConfig
87// field). Template functions, not lambdas: a TuningSelectParam's get/set are plain function
88// pointers with no room for a per-row capture, and instantiating on the member pointer keeps
89// each row below a one-liner without duplicating the On/Off parse.
90template<bool TuningConfig::*Field> std::string bool_select_get(const TuningConfig &t) {
91 return t.*Field ? "On" : "Off";
92}
93template<bool TuningConfig::*Field> bool bool_select_set(TuningConfig &t, const std::string &v) {
94 if (v != "On" && v != "Off")
95 return false;
96 t.*Field = (v == "On");
97 return true;
98}
99
100// === Select parameters (10) ====================================================
101// The setters return false only when the option string is unparseable AND leaving the
102// value untouched is the intended behavior (currently the bandwidth enum and the boolean
103// rows). The destination/payload setters return false for unrecognized values so no radio
104// re-apply is triggered; that has no observable effect since neither applies to the radio.
105static constexpr TuningSelectParam SELECT_PARAMS[] = {
106 {"sx1262_rx_bandwidth", [](const TuningConfig &t) { return sx1262_bandwidth_to_string(t.sx1262_rx_bandwidth); },
107 [](TuningConfig &t, const std::string &v) -> bool {
108 auto bw = sx1262_bandwidth_from_string(v);
109 if (!bw.has_value())
110 return false;
111 t.sx1262_rx_bandwidth = bw.value();
112 return true;
113 },
114 true},
115 {"sx1276_rx_bandwidth", [](const TuningConfig &t) { return sx1276_bandwidth_to_string(t.sx1276_rx_bandwidth); },
116 [](TuningConfig &t, const std::string &v) -> bool {
117 auto bw = sx1276_bandwidth_from_string(v);
118 if (!bw.has_value())
119 return false;
120 t.sx1276_rx_bandwidth = bw.value();
121 return true;
122 },
123 true},
124 {"lr1121_rx_bandwidth", [](const TuningConfig &t) { return lr1121_bandwidth_to_string(t.lr1121_rx_bandwidth); },
125 [](TuningConfig &t, const std::string &v) -> bool {
126 auto bw = lr1121_bandwidth_from_string(v);
127 if (!bw.has_value())
128 return false;
129 t.lr1121_rx_bandwidth = bw.value();
130 return true;
131 },
132 true},
135 {"pairing_discovery_commands",
136 [](const TuningConfig &t) { return discovery_commands_to_csv(t.pairing_discovery_commands); },
137 [](TuningConfig &t, const std::string &v) -> bool {
139 // Support both individual commands and comma-separated preset strings.
140 std::size_t start = 0;
141 while (start < v.size()) {
142 const std::size_t comma = v.find(',', start);
143 std::string token = v.substr(start, comma - start);
144 // Trim whitespace.
145 token.erase(0, token.find_first_not_of(" \t"));
146 token.erase(token.find_last_not_of(" \t") + 1);
147 auto cmd = discovery_command_from_string(token);
148 if (cmd.has_value())
149 t.pairing_discovery_commands.push_back(cmd.value());
150 if (comma == std::string::npos)
151 break;
152 start = comma + 1;
153 }
154 return true;
155 },
156 false},
157 {"pairing_discovery_destination",
158 [](const TuningConfig &t) {
159 return discovery_destination_to_string(t.pairing_discovery_destination_auto,
160 t.pairing_discovery_destination.data());
161 },
162 [](TuningConfig &t, const std::string &v) -> bool {
163 if (v == "auto") {
165 return true;
166 }
167 if (v == "0x00003B") {
169 t.pairing_discovery_destination = {0x00, 0x00, ADDRESS_SUFFIX_DISCOVERY};
170 return true;
171 }
172 if (v == "0x00003F") {
174 t.pairing_discovery_destination = {0x00, 0x00, ADDRESS_SUFFIX_BROADCAST};
175 return true;
176 }
177 // Typed lighting-class broadcasts. encode_broadcast_address() builds the ADDRESS_SUFFIX_
178 // BROADCAST form; the discovery form differs only in that suffix, so it is re-masked rather
179 // than hand-written as literal bytes.
180 if (v == "0x0001BF" || v == "0x0001BB") {
181 uint8_t typed[NODE_ID_SIZE] = {0};
183 if (v == "0x0001BB")
184 typed[2] = static_cast<uint8_t>((typed[2] & ~DEVICE_SUBTYPE_MASK) | ADDRESS_SUFFIX_DISCOVERY);
186 t.pairing_discovery_destination.assign(std::begin(typed), std::end(typed));
187 return true;
188 }
189 return false;
190 },
191 false},
192 {"pairing_discovery_payload",
193 [](const TuningConfig &t) {
194 return discovery_payload_to_string(t.pairing_discovery_payload_enabled, t.pairing_discovery_payload);
195 },
196 [](TuningConfig &t, const std::string &v) -> bool {
197 if (v == "none") {
199 return true;
200 }
201 if (v == "0x00") {
204 return true;
205 }
206 return false;
207 },
208 false},
213 {"scan_power_classes", [](const TuningConfig &t) { return scan_power_classes_to_string(t.scan_power_classes); },
214 [](TuningConfig &t, const std::string &v) -> bool {
215 auto selection = scan_power_classes_from_string(v);
216 if (!selection.has_value())
217 return false;
218 t.scan_power_classes = selection.value();
219 return true;
220 },
221 false},
222 {"pairing_discover_confirm",
223 [](const TuningConfig &t) { return discover_confirm_mode_to_string(t.pairing_discover_confirm); },
224 [](TuningConfig &t, const std::string &v) -> bool {
226 if (!mode.has_value())
227 return false;
228 t.pairing_discover_confirm = mode.value();
229 return true;
230 },
231 false},
232 {"pairing_discovery_listen_channels",
233 [](const TuningConfig &t) { return discovery_listen_channels_to_string(t.pairing_discovery_listen_channels); },
234 [](TuningConfig &t, const std::string &v) -> bool {
235 auto channels = discovery_listen_channels_from_string(v);
236 if (!channels.has_value())
237 return false;
238 t.pairing_discovery_listen_channels = channels.value();
239 return true;
240 },
241 false},
242};
243
244const TuningNumberParam *find_tuning_number(const std::string &name) {
245 for (const auto &param : NUMBER_PARAMS) {
246 if (name == param.name)
247 return &param;
248 }
249 return nullptr;
250}
251
252const TuningSelectParam *find_tuning_select(const std::string &name) {
253 for (const auto &param : SELECT_PARAMS) {
254 if (name == param.name)
255 return &param;
256 }
257 return nullptr;
258}
259
264
265} // namespace home_io_control
266} // 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::optional< ScanPowerClasses > scan_power_classes_from_string(const std::string &value)
Parse a scan_power_classes string into the enum.
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 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).
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.