diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2022-12-18 06:31:24 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2022-12-24 12:57:55 +0300 |
commit | 0a065f48be99d4ebae49de622a335f23e50c6ca0 (patch) | |
tree | b591d91fac26e5bf7a4dd6d37178b978061ef060 /platformio/relayctl/src/config.cpp | |
parent | 022ec129bb8f511a7bf8cf537f165afce2303262 (diff) |
pump-mqtt-bot: wip; relayctl: somewhat stable
Diffstat (limited to 'platformio/relayctl/src/config.cpp')
-rw-r--r-- | platformio/relayctl/src/config.cpp | 30 |
1 files changed, 13 insertions, 17 deletions
diff --git a/platformio/relayctl/src/config.cpp b/platformio/relayctl/src/config.cpp index d143a7f..796adf0 100644 --- a/platformio/relayctl/src/config.cpp +++ b/platformio/relayctl/src/config.cpp @@ -1,7 +1,6 @@ #include <EEPROM.h> #include <strings.h> #include "config.h" -#include "config.def.h" #include "logging.h" #define GET_DATA_CRC(data) \ @@ -10,7 +9,7 @@ namespace homekit::config { static const uint32_t magic = 0xdeadbeef; -static const uint32_t crc_table[16] = { +static const uint32_t crc_table[16] PROGMEM = { 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, @@ -20,15 +19,15 @@ static const uint32_t crc_table[16] = { static uint32_t eeprom_crc(const uint8_t* data, size_t len) { uint32_t crc = ~0L; for (size_t index = 0; index < len; index++) { - crc = crc_table[(crc ^ data[index]) & 0x0f] ^ (crc >> 4); - crc = crc_table[(crc ^ (data[index] >> 4)) & 0x0f] ^ (crc >> 4); + crc = pgm_read_word(&crc_table[(crc ^ data[index]) & 0x0f]) ^ (crc >> 4); + crc = pgm_read_word(&crc_table[(crc ^ (data[index] >> 4)) & 0x0f]) ^ (crc >> 4); crc = ~crc; } return crc; } ConfigData read() { - ConfigData data {0}; + ConfigData data; EEPROM.begin(sizeof(ConfigData)); EEPROM.get(0, data); EEPROM.end(); @@ -40,25 +39,22 @@ ConfigData read() { return data; } -bool write(ConfigData& data) { +void write(ConfigData& data) { EEPROM.begin(sizeof(ConfigData)); data.magic = magic; data.crc = GET_DATA_CRC(data); EEPROM.put(0, data); - return EEPROM.end(); + EEPROM.end(); } -bool erase() { +void erase() { ConfigData data; - return erase(data); + erase(data); } -bool erase(ConfigData& data) { +void erase(ConfigData& data) { bzero(reinterpret_cast<uint8_t*>(&data), sizeof(data)); - data.magic = magic; - EEPROM.begin(sizeof(data)); - EEPROM.put(0, data); - return EEPROM.end(); + write(data); } bool isValid(ConfigData& data) { @@ -69,11 +65,11 @@ bool isDirty(ConfigData& data) { return data.magic != magic; } -char* ConfigData::escapeNodeId(char* buf, size_t len) { +char* ConfigData::escapeHomeId(char* buf, size_t len) { if (len < 32) return nullptr; - size_t id_len = strlen(node_id); - char* c = node_id; + size_t id_len = strlen(home_id); + char* c = home_id; char* dst = buf; for (size_t i = 0; i < id_len; i++) { if (*c == '"') |