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
14#include <cstddef>
15#include <iterator>
16
17namespace esphome {
18namespace home_io_control {
19
20// === Numeric parameters (15) ===================================================
21// Each row narrows the incoming float to the field's storage type, matching the
22// original static_cast in update_tuning_number().
23static constexpr TuningNumberParam NUMBER_PARAMS[] = {
24 {"sx1262_response_preamble", [](const TuningConfig &t) { return static_cast<float>(t.sx1262_response_preamble); },
25 [](TuningConfig &t, float v) { t.sx1262_response_preamble = static_cast<uint16_t>(v); }, true},
26 {"sx1262_post_tx_settle_us", [](const TuningConfig &t) { return static_cast<float>(t.sx1262_post_tx_settle_us); },
27 [](TuningConfig &t, float v) { t.sx1262_post_tx_settle_us = static_cast<uint16_t>(v); }, true},
28 {"sx1276_response_preamble", [](const TuningConfig &t) { return static_cast<float>(t.sx1276_response_preamble); },
29 [](TuningConfig &t, float v) { t.sx1276_response_preamble = static_cast<uint16_t>(v); }, true},
30 {"sx1276_discovery_hop_slice_ms",
31 [](const TuningConfig &t) { return static_cast<float>(t.sx1276_discovery_hop_slice_ms); },
32 [](TuningConfig &t, float v) { t.sx1276_discovery_hop_slice_ms = static_cast<uint16_t>(v); }, false},
33 {"sx1262_discovery_hop_slice_ms",
34 [](const TuningConfig &t) { return static_cast<float>(t.sx1262_discovery_hop_slice_ms); },
35 [](TuningConfig &t, float v) { t.sx1262_discovery_hop_slice_ms = static_cast<uint16_t>(v); }, false},
36 {"lr1121_response_preamble", [](const TuningConfig &t) { return static_cast<float>(t.lr1121_response_preamble); },
37 [](TuningConfig &t, float v) { t.lr1121_response_preamble = static_cast<uint16_t>(v); }, true},
38 {"lr1121_post_tx_settle_us", [](const TuningConfig &t) { return static_cast<float>(t.lr1121_post_tx_settle_us); },
39 [](TuningConfig &t, float v) { t.lr1121_post_tx_settle_us = static_cast<uint16_t>(v); }, true},
40 {"lr1121_discovery_hop_slice_ms",
41 [](const TuningConfig &t) { return static_cast<float>(t.lr1121_discovery_hop_slice_ms); },
42 [](TuningConfig &t, float v) { t.lr1121_discovery_hop_slice_ms = static_cast<uint16_t>(v); }, false},
43 {"lbt_max_retries", [](const TuningConfig &t) { return static_cast<float>(t.lbt_max_retries); },
44 [](TuningConfig &t, float v) { t.lbt_max_retries = static_cast<uint8_t>(v); }, false},
45 {"lbt_rssi_threshold_dbm", [](const TuningConfig &t) { return static_cast<float>(t.lbt_rssi_threshold_dbm); },
46 [](TuningConfig &t, float v) { t.lbt_rssi_threshold_dbm = static_cast<int16_t>(v); }, false},
47 {"exchange_start_response_wait_ms",
48 [](const TuningConfig &t) { return static_cast<float>(t.exchange_start_response_wait_ms); },
49 [](TuningConfig &t, float v) { t.exchange_start_response_wait_ms = static_cast<uint16_t>(v); }, false},
50 {"exchange_response_wait_ms", [](const TuningConfig &t) { return static_cast<float>(t.exchange_response_wait_ms); },
51 [](TuningConfig &t, float v) { t.exchange_response_wait_ms = static_cast<uint16_t>(v); }, false},
52 {"pairing_discovery_wait_ms", [](const TuningConfig &t) { return static_cast<float>(t.pairing_discovery_wait_ms); },
53 [](TuningConfig &t, float v) { t.pairing_discovery_wait_ms = static_cast<uint16_t>(v); }, false},
54 {"pairing_discovery_initial_dwell_ms",
55 [](const TuningConfig &t) { return static_cast<float>(t.pairing_discovery_initial_dwell_ms); },
56 [](TuningConfig &t, float v) { t.pairing_discovery_initial_dwell_ms = static_cast<uint16_t>(v); }, false},
57 {"pairing_key_exchange_retries",
58 [](const TuningConfig &t) { return static_cast<float>(t.pairing_key_exchange_retries); },
59 [](TuningConfig &t, float v) { t.pairing_key_exchange_retries = static_cast<uint8_t>(v); }, false},
60};
61
62// === Select parameters (7) =====================================================
63// The setters return false only when the option string is unparseable AND leaving the
64// value untouched is the intended behavior (currently just the bandwidth enum). The
65// destination/payload setters return false for unrecognized values so no radio re-apply
66// is triggered; that has no observable effect since neither applies to the radio.
67static constexpr TuningSelectParam SELECT_PARAMS[] = {
68 {"sx1262_rx_bandwidth", [](const TuningConfig &t) { return sx1262_bandwidth_to_string(t.sx1262_rx_bandwidth); },
69 [](TuningConfig &t, const std::string &v) -> bool {
71 if (!bw.has_value())
72 return false;
73 t.sx1262_rx_bandwidth = bw.value();
74 return true;
75 },
76 true},
77 {"sx1276_rx_bandwidth", [](const TuningConfig &t) { return sx1276_bandwidth_to_string(t.sx1276_rx_bandwidth); },
78 [](TuningConfig &t, const std::string &v) -> bool {
80 if (!bw.has_value())
81 return false;
82 t.sx1276_rx_bandwidth = bw.value();
83 return true;
84 },
85 true},
86 {"lr1121_rx_bandwidth", [](const TuningConfig &t) { return lr1121_bandwidth_to_string(t.lr1121_rx_bandwidth); },
87 [](TuningConfig &t, const std::string &v) -> bool {
89 if (!bw.has_value())
90 return false;
91 t.lr1121_rx_bandwidth = bw.value();
92 return true;
93 },
94 true},
95 {"pairing_discovery_commands",
96 [](const TuningConfig &t) { return discovery_commands_to_csv(t.pairing_discovery_commands); },
97 [](TuningConfig &t, const std::string &v) -> bool {
99 // Support both individual commands and comma-separated preset strings.
100 std::size_t start = 0;
101 while (start < v.size()) {
102 const std::size_t comma = v.find(',', start);
103 std::string token = v.substr(start, comma - start);
104 // Trim whitespace.
105 token.erase(0, token.find_first_not_of(" \t"));
106 token.erase(token.find_last_not_of(" \t") + 1);
107 auto cmd = discovery_command_from_string(token);
108 if (cmd.has_value())
109 t.pairing_discovery_commands.push_back(cmd.value());
110 if (comma == std::string::npos)
111 break;
112 start = comma + 1;
113 }
114 return true;
115 },
116 false},
117 {"pairing_discovery_destination",
118 [](const TuningConfig &t) {
119 return discovery_destination_to_string(t.pairing_discovery_destination_auto,
120 t.pairing_discovery_destination.data());
121 },
122 [](TuningConfig &t, const std::string &v) -> bool {
123 if (v == "auto") {
125 return true;
126 }
127 if (v == "0x00003B") {
130 return true;
131 }
132 if (v == "0x00003F") {
135 return true;
136 }
137 return false;
138 },
139 false},
140 {"pairing_discovery_payload",
141 [](const TuningConfig &t) {
142 return discovery_payload_to_string(t.pairing_discovery_payload_enabled, t.pairing_discovery_payload);
143 },
144 [](TuningConfig &t, const std::string &v) -> bool {
145 if (v == "none") {
147 return true;
148 }
149 if (v == "0x00") {
152 return true;
153 }
154 return false;
155 },
156 false},
157 {"pairing_discovery_low_power",
158 [](const TuningConfig &t) -> std::string { return t.pairing_discovery_low_power ? "On" : "Off"; },
159 [](TuningConfig &t, const std::string &v) -> bool {
160 t.pairing_discovery_low_power = (v == "On");
161 return true;
162 },
163 false},
164};
165
166const TuningNumberParam *find_tuning_number(const std::string &name) {
167 for (const auto &param : NUMBER_PARAMS) {
168 if (name == param.name)
169 return &param;
170 }
171 return nullptr;
172}
173
174const TuningSelectParam *find_tuning_select(const std::string &name) {
175 for (const auto &param : SELECT_PARAMS) {
176 if (name == param.name)
177 return &param;
178 }
179 return nullptr;
180}
181
186
187} // namespace home_io_control
188} // 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()
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.
static constexpr uint8_t ADDRESS_SUFFIX_BROADCAST
Suffix for "all devices of this type" broadcast.
std::optional< DiscoveryCommand > discovery_command_from_string(const std::string &value)
Parse a discovery command string (e.g., "0x28") into the enum.
static constexpr uint8_t ADDRESS_SUFFIX_DISCOVERY
Suffix for discovery-related broadcasts.
std::string lr1121_bandwidth_to_string(LR1121RxBandwidth bw)
Format an LR1121 bandwidth enum as its YAML/UI option string (bare kHz number).
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).
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()
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.
Device-name, address-classification and 1W-frame codecs.
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.
int16_t lbt_rssi_threshold_dbm
LBT channel-free threshold (dBm).
SX1262RxBandwidth sx1262_rx_bandwidth
SX1262 RX bandwidth selector.
std::vector< uint8_t > pairing_discovery_destination
Destination when auto=false.
std::vector< DiscoveryCommand > pairing_discovery_commands
Ordered discovery commands.
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.
bool pairing_discovery_low_power
Set LOW_POWER flag in discovery frames.
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_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 lr1121_response_preamble
LR1121 response preamble in bytes.
uint8_t pairing_discovery_payload
Optional payload byte (used for 0x2E).
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.