Store rolling code in flash rather than exposing it

This commit is contained in:
2026-05-22 21:25:36 +00:00
parent 48f5760a17
commit f503e5a66d
8 changed files with 19 additions and 59 deletions
-1
View File
@@ -16,7 +16,6 @@ NumberType = ratgdo_ns.enum("NumberType")
CONF_TYPE = "type"
TYPES = {
"client_id": NumberType.RATGDO_CLIENT_ID,
"rolling_code_counter": NumberType.RATGDO_ROLLING_CODE_COUNTER,
"opening_duration": NumberType.RATGDO_OPENING_DURATION,
"closing_duration": NumberType.RATGDO_CLOSING_DURATION,
}
@@ -5,7 +5,6 @@
namespace esphome::ratgdo {
using protocol::SetClientID;
using protocol::SetRollingCodeCounter;
float normalize_client_id(float client_id)
{
@@ -25,9 +24,6 @@ void RATGDONumber::dump_config()
case RATGDO_CLIENT_ID:
ESP_LOGCONFIG(TAG, " Type: Client ID");
break;
case RATGDO_ROLLING_CODE_COUNTER:
ESP_LOGCONFIG(TAG, " Type: Rolling Code Counter");
break;
case RATGDO_OPENING_DURATION:
ESP_LOGCONFIG(TAG, " Type: Opening Duration");
break;
@@ -61,11 +57,6 @@ void RATGDONumber::setup()
this->control(value);
switch (this->number_type_) {
case RATGDO_ROLLING_CODE_COUNTER:
this->parent_->subscribe_rolling_code_counter([this](uint32_t value) {
this->update_state(value);
});
break;
case RATGDO_OPENING_DURATION:
this->parent_->subscribe_opening_duration([this](float value) {
this->update_state(value);
@@ -91,9 +82,6 @@ void RATGDONumber::set_number_type(NumberType number_type_)
this->traits.set_min_value(0.0);
this->traits.set_max_value(180.0);
break;
case RATGDO_ROLLING_CODE_COUNTER:
this->traits.set_max_value(0xfffffff);
break;
case RATGDO_CLIENT_ID:
this->traits.set_step(0x1000);
this->traits.set_min_value(0x539);
@@ -116,9 +104,6 @@ void RATGDONumber::update_state(float value)
void RATGDONumber::control(float value)
{
switch (this->number_type_) {
case RATGDO_ROLLING_CODE_COUNTER:
this->parent_->call_protocol(SetRollingCodeCounter { static_cast<uint32_t>(value) });
break;
case RATGDO_OPENING_DURATION:
this->parent_->set_opening_duration(value);
break;
-1
View File
@@ -10,7 +10,6 @@ namespace esphome::ratgdo {
enum NumberType {
RATGDO_CLIENT_ID,
RATGDO_ROLLING_CODE_COUNTER,
RATGDO_OPENING_DURATION,
RATGDO_CLOSING_DURATION,
};
+1 -13
View File
@@ -16,11 +16,6 @@ class RATGDOComponent;
namespace protocol {
struct SetRollingCodeCounter {
uint32_t counter;
};
struct GetRollingCodeCounter {
};
struct SetClientID {
uint64_t client_id;
};
@@ -31,18 +26,11 @@ namespace protocol {
// a poor man's sum-type, because C++
SUM_TYPE(Args,
(SetRollingCodeCounter, set_rolling_code_counter),
(GetRollingCodeCounter, get_rolling_code_counter),
(SetClientID, set_client_id),
(QueryStatus, query_status),
(QueryOpenings, query_openings), )
struct RollingCodeCounter {
single_observable<uint32_t>* value;
};
SUM_TYPE(Result,
(RollingCodeCounter, rolling_code_counter), )
SUM_TYPE(Result, )
class Protocol {
public:
+1 -17
View File
@@ -166,8 +166,6 @@ public:
// (3 * sizeof(void*)), e.g. [this] or [this, f] lambdas.
// Enforced at compile time by Callback::create().
template <typename F>
void subscribe_rolling_code_counter(F&& f);
template <typename F>
void subscribe_opening_duration(F&& f);
template <typename F>
void subscribe_closing_duration(F&& f);
@@ -230,8 +228,7 @@ namespace scheduler_ids {
// Single-subscriber IDs
enum : uint32_t {
DEFER_ROLLING_CODE = DEFER_DISTANCE_END,
DEFER_OPENING_DURATION,
DEFER_OPENING_DURATION = DEFER_DISTANCE_END,
DEFER_CLOSING_DURATION,
DEFER_OPENINGS,
DEFER_LIGHT_STATE,
@@ -251,19 +248,6 @@ namespace scheduler_ids {
// fires multiple times during one loop iteration, only the last value
// is dispatched to the child component.
template <typename F>
void RATGDOComponent::subscribe_rolling_code_counter(F&& f)
{
// change update to children is defered until after component loop
// if multiple changes occur during component loop, only the last one is notified
auto counter = this->protocol_->call(protocol::GetRollingCodeCounter { });
if (counter.tag == protocol::Result::Tag::rolling_code_counter) {
counter.value.rolling_code_counter.value->subscribe([this, f](uint32_t state) {
defer(scheduler_ids::DEFER_ROLLING_CODE, [f, state] { f(state); });
});
}
}
template <typename F>
void RATGDOComponent::subscribe_opening_duration(F&& f)
{
+15 -5
View File
@@ -35,6 +35,17 @@ namespace secplus2 {
this->tx_pin_ = tx_pin;
this->rx_pin_ = rx_pin;
this->rolling_code_pref_ = global_preferences->make_preference<uint32_t>(1868352652U); // fnv1_hash("ratgdo_rolling_code")
uint32_t rolling_code;
if (this->rolling_code_pref_.load(&rolling_code)) {
this->rolling_code_counter_ = rolling_code;
ESP_LOGI(TAG, "Restored rolling code from flash: %u", rolling_code);
} else {
rolling_code = 1;
this->rolling_code_counter_ = rolling_code;
this->rolling_code_pref_.save(&rolling_code);
}
this->uart_.begin(9600, RATGDO_UART_8N1, rx_pin->get_pin(), tx_pin->get_pin(), true);
this->uart_.enableIntTx(false);
this->uart_.enableAutoBaud(true);
@@ -138,10 +149,6 @@ namespace secplus2 {
this->send_command(CommandType::GET_STATUS);
} else if (args.tag == Tag::query_openings) {
this->send_command(CommandType::GET_OPENINGS);
} else if (args.tag == Tag::get_rolling_code_counter) {
return Result(RollingCodeCounter { std::addressof(this->rolling_code_counter_) });
} else if (args.tag == Tag::set_rolling_code_counter) {
this->set_rolling_code_counter(args.value.set_rolling_code_counter.counter);
} else if (args.tag == Tag::set_client_id) {
this->set_client_id(args.value.set_client_id.client_id);
}
@@ -348,13 +355,16 @@ namespace secplus2 {
void Secplus2::increment_rolling_code_counter(int delta)
{
this->rolling_code_counter_ = (*this->rolling_code_counter_ + delta) & 0xfffffff;
uint32_t counter = (*this->rolling_code_counter_ + delta) & 0xfffffff;
this->rolling_code_counter_ = counter;
this->rolling_code_pref_.save(&counter);
}
void Secplus2::set_rolling_code_counter(uint32_t counter)
{
ESP_LOGV(TAG, "Set rolling code counter to %d", counter);
this->rolling_code_counter_ = counter;
this->rolling_code_pref_.save(&counter);
}
void Secplus2::set_client_id(uint64_t client_id)
+2
View File
@@ -1,6 +1,7 @@
#pragma once
#include "esphome/core/optional.h"
#include "esphome/core/preferences.h"
#include "ratgdo_uart.h"
#include "callbacks.h"
@@ -136,6 +137,7 @@ namespace secplus2 {
// Larger structures
single_observable<uint32_t> rolling_code_counter_ { 0 };
ESPPreferenceObject rolling_code_pref_;
OnceCallbacks<void()> on_command_sent_;
RatgdoUART uart_;
-7
View File
@@ -102,13 +102,6 @@ switch:
entity_category: config
number:
- platform: ratgdo
id: garage_gate_rolling_code_counter
type: rolling_code_counter
entity_category: config
name: "Rolling code counter"
mode: box
unit_of_measurement: "codes"
- platform: ratgdo
id: garage_gate_opening_duration
type: opening_duration