mirror of https://github.com/arendst/Tasmota.git
Merge pull request #9965 from s-hadinger/zigbee_devices_eeprom
Zigbee store devices information in EEPROM
This commit is contained in:
commit
0e41e0b9fe
|
@ -190,11 +190,6 @@ class SBuffer hibernateDevices(void) {
|
|||
buf.addBuffer(buf_device);
|
||||
}
|
||||
|
||||
size_t buf_len = buf.len();
|
||||
if (buf_len > 2040) {
|
||||
AddLog_P(LOG_LEVEL_ERROR, PSTR(D_LOG_ZIGBEE "Devices list too big to fit in Flash (%d)"), buf_len);
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
@ -296,6 +291,11 @@ void hydrateDevices(const SBuffer &buf, uint32_t version) {
|
|||
|
||||
// dump = true, only dump to logs, don't actually load
|
||||
void loadZigbeeDevices(bool dump_only = false) {
|
||||
#ifdef USE_ZIGBEE_EZSP
|
||||
if (loadZigbeeDevicesFromEEPROM()) {
|
||||
return; // we succesfully loaded from EEPROM, skip the read from Flash
|
||||
}
|
||||
#endif
|
||||
#ifdef ESP32
|
||||
// first copy SPI buffer into ram
|
||||
uint8_t *spi_buffer = (uint8_t*) malloc(z_spi_len);
|
||||
|
@ -319,7 +319,7 @@ void loadZigbeeDevices(bool dump_only = false) {
|
|||
// parse what seems to be a valid entry
|
||||
SBuffer buf(buf_len);
|
||||
buf.addBuffer(z_dev_start + sizeof(Z_Flashentry), buf_len);
|
||||
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "Zigbee device information in Flash (%d bytes)"), buf_len);
|
||||
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "Zigbee device information in %s (%d bytes)"), PSTR("Flash"), buf_len);
|
||||
|
||||
if (dump_only) {
|
||||
size_t buf_len = buf.len();
|
||||
|
@ -333,7 +333,7 @@ void loadZigbeeDevices(bool dump_only = false) {
|
|||
zigbee_devices.clean(); // don't write back to Flash what we just loaded
|
||||
}
|
||||
} else {
|
||||
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "No Zigbee device information in Flash"));
|
||||
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "No Zigbee device information in %s"), PSTR("Flash"));
|
||||
}
|
||||
#ifdef ESP32
|
||||
free(spi_buffer);
|
||||
|
@ -341,9 +341,16 @@ void loadZigbeeDevices(bool dump_only = false) {
|
|||
}
|
||||
|
||||
void saveZigbeeDevices(void) {
|
||||
#ifdef USE_ZIGBEE_EZSP
|
||||
if (zigbee.eeprom_ready) {
|
||||
if (hibernateDevicesInEEPROM()) {
|
||||
return; // saved in EEPROM successful, non need to write in Flash
|
||||
}
|
||||
}
|
||||
#endif
|
||||
SBuffer buf = hibernateDevices();
|
||||
size_t buf_len = buf.len();
|
||||
if (buf_len > Z_MAX_FLASH) {
|
||||
if (buf_len > 2040) {
|
||||
AddLog_P(LOG_LEVEL_ERROR, PSTR(D_LOG_ZIGBEE "Buffer too big to fit in Flash (%d bytes)"), buf_len);
|
||||
return;
|
||||
}
|
||||
|
@ -376,7 +383,7 @@ void saveZigbeeDevices(void) {
|
|||
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "Zigbee Devices Data store in Flash (0x%08X - %d bytes)"), z_dev_start, buf_len);
|
||||
#else // ESP32
|
||||
ZigbeeWrite(&spi_buffer, z_spi_len);
|
||||
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "Zigbee Devices Data saved (%d bytes)"), buf_len);
|
||||
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "Zigbee Devices Data saved in %s (%d bytes)"), PSTR("Flash"), buf_len);
|
||||
#endif // ESP8266 - ESP32
|
||||
free(spi_buffer);
|
||||
}
|
||||
|
|
|
@ -20,7 +20,6 @@
|
|||
#ifdef USE_ZIGBEE
|
||||
#ifdef USE_ZIGBEE_EZSP
|
||||
|
||||
#include <memory>
|
||||
#define Z_EEPROM_DEBUG
|
||||
|
||||
// The EEPROM is 64KB in size with individually writable bytes.
|
||||
|
@ -274,7 +273,7 @@ public:
|
|||
static void erase(void); // erase EEPROM
|
||||
|
||||
// read file
|
||||
static int32_t readBytes(uint32_t name, uint8_t* buffer, size_t buffer_len, uint16_t start, uint16_t len);
|
||||
static int32_t readBytes(uint32_t name, void* buffer, size_t buffer_len, uint16_t start, uint16_t len);
|
||||
};
|
||||
|
||||
/*********************************************************************************************\
|
||||
|
@ -322,7 +321,7 @@ void ZFS::erase(void) {
|
|||
* Reading a file
|
||||
*
|
||||
\*********************************************************************************************/
|
||||
int32_t ZFS::readBytes(uint32_t name, uint8_t* buffer, size_t buffer_len, uint16_t read_start, uint16_t read_len) {
|
||||
int32_t ZFS::readBytes(uint32_t name, void* buffer, size_t buffer_len, uint16_t read_start, uint16_t read_len) {
|
||||
if (!zigbee.eeprom_ready) { return -1; }
|
||||
#ifdef Z_EEPROM_DEBUG
|
||||
// AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "readBytes name=%08X, buffer_len=%d, read_start=0x%04X, read_len=%d"), name, buffer_len, read_start, read_len);
|
||||
|
@ -365,9 +364,9 @@ void ZFS::initOrFormat(void) {
|
|||
char hex_char[(256 * 2) + 2];
|
||||
zigbee.eeprom.readBytes(0x0000, 256, map);
|
||||
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "BLK 00 %s"), ToHex_P(map, sizeof(map), hex_char, sizeof(hex_char)));
|
||||
zigbee.eeprom.readBytes(0x0100, 256, map);
|
||||
// zigbee.eeprom.readBytes(0x0100, 256, map);
|
||||
// AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "BLK 01 %s"), ToHex_P(map, sizeof(map), hex_char, sizeof(hex_char)));
|
||||
// zigbee.eeprom.readBytes(0x0200, 256, map);
|
||||
zigbee.eeprom.readBytes(0x0200, 256, map);
|
||||
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "BLK 02 %s"), ToHex_P(map, sizeof(map), hex_char, sizeof(hex_char)));
|
||||
zigbee.eeprom.readBytes(0x2100, 256, map);
|
||||
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "BLK 21 %s"), ToHex_P(map, sizeof(map), hex_char, sizeof(hex_char)));
|
||||
|
|
|
@ -235,14 +235,83 @@ const uint32_t Z_SAVE_DATA_TIMER = 60 * 60 * 1000; // save data every 60 m
|
|||
//
|
||||
// Callback for setting the timer to save Zigbee Data in x seconds
|
||||
//
|
||||
int32_t Z_Set_Save_Data_Timer(uint8_t value) {
|
||||
int32_t Z_Set_Save_Data_Timer_EEPROM(uint8_t value) {
|
||||
zigbee_devices.setTimer(0x0000, 0, Z_SAVE_DATA_TIMER, 0, 0, Z_CAT_ALWAYS, 0 /* value */, &Z_SaveDataTimer);
|
||||
return 0; // continue
|
||||
}
|
||||
|
||||
void Z_SaveDataTimer(uint16_t shortaddr, uint16_t groupaddr, uint16_t cluster, uint8_t endpoint, uint32_t value) {
|
||||
hibernateAllData();
|
||||
Z_Set_Save_Data_Timer(0); // set a new timer
|
||||
Z_Set_Save_Data_Timer_EEPROM(0); // set a new timer
|
||||
}
|
||||
|
||||
/*********************************************************************************************\
|
||||
* Write Devices in EEPROM
|
||||
\*********************************************************************************************/
|
||||
// EEPROM variant that writes one item at a time and is not limited to 2KB
|
||||
bool hibernateDevicesInEEPROM(void) {
|
||||
if (Rtc.utc_time < START_VALID_TIME) { return false; }
|
||||
if (!zigbee.eeprom_ready) { return false; }
|
||||
|
||||
ZFS_Write_File write_data(ZIGB_NAME2);
|
||||
|
||||
// first prefix is number of devices
|
||||
uint8_t devices_size = zigbee_devices.devicesSize();
|
||||
if (devices_size > 64) { devices_size = 64; } // arbitrarily limit to 64 devices in EEPROM instead of 32 in Flash
|
||||
write_data.addBytes(&devices_size, sizeof(devices_size));
|
||||
|
||||
for (const auto & device : zigbee_devices.getDevices()) {
|
||||
const SBuffer buf = hibernateDevicev2(device);
|
||||
if (buf.len() > 0) {
|
||||
write_data.addBytes(buf.getBuffer(), buf.len());
|
||||
}
|
||||
}
|
||||
int32_t ret = write_data.close();
|
||||
|
||||
if (ret < 0) {
|
||||
AddLog_P(LOG_LEVEL_ERROR, PSTR(D_LOG_ZIGBEE "Error writing Devices to EEPROM"));
|
||||
return false;
|
||||
} else {
|
||||
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "Zigbee Devices Data saved in %s (%d bytes)"), PSTR("EEPROM"), ret);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// dump = true, only dump to logs, don't actually load
|
||||
bool loadZigbeeDevicesFromEEPROM(void) {
|
||||
if (!zigbee.eeprom_ready) { return false; }
|
||||
uint16_t file_len = ZFS::getLength(ZIGB_NAME2);
|
||||
|
||||
uint8_t num_devices = 0;
|
||||
ZFS::readBytes(ZIGB_NAME2, &num_devices, sizeof(num_devices), 0, sizeof(num_devices));
|
||||
|
||||
if ((file_len < 10) || (num_devices == 0x00) || (num_devices == 0xFF)) { // No data
|
||||
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "No Zigbee device information in %s"), PSTR("EEPROM"));
|
||||
return false;
|
||||
}
|
||||
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "Zigbee device information in %s (%d bytes)"), PSTR("EEPROM"), file_len);
|
||||
|
||||
uint32_t k = 1; // byte index in global buffer
|
||||
for (uint32_t i = 0; (i < num_devices) && (k < file_len); i++) {
|
||||
uint8_t dev_record_len = 0;
|
||||
int32_t ret = ZFS::readBytes(ZIGB_NAME2, &dev_record_len, 1, k, 1);
|
||||
SBuffer buf(dev_record_len);
|
||||
buf.setLen(dev_record_len);
|
||||
ret = ZFS::readBytes(ZIGB_NAME2, buf.getBuffer(), dev_record_len, k, dev_record_len);
|
||||
if (ret != dev_record_len) {
|
||||
AddLog_P(LOG_LEVEL_INFO, PSTR(D_LOG_ZIGBEE "File too short when reading EEPROM"));
|
||||
return false;
|
||||
}
|
||||
|
||||
hydrateSingleDevice(buf, 2);
|
||||
|
||||
// next iteration
|
||||
k += dev_record_len;
|
||||
}
|
||||
|
||||
zigbee_devices.clean(); // don't write back to Flash what we just loaded
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif // USE_ZIGBEE
|
||||
|
|
|
@ -369,7 +369,7 @@ static const Zigbee_Instruction zb_prog[] PROGMEM = {
|
|||
ZI_ON_ERROR_GOTO(ZIGBEE_LABEL_ABORT)
|
||||
ZI_ON_TIMEOUT_GOTO(ZIGBEE_LABEL_ABORT)
|
||||
ZI_ON_RECV_UNEXPECTED(&ZNP_Recv_Default)
|
||||
ZI_WAIT(10500) // wait for 10 seconds for Tasmota to stabilize
|
||||
ZI_WAIT(15500) // wait for 15 seconds for Tasmota to stabilize
|
||||
|
||||
//ZI_MQTT_STATE(ZIGBEE_STATUS_BOOT, "Booting")
|
||||
ZI_LOG(LOG_LEVEL_INFO, D_LOG_ZIGBEE "rebooting CC2530 device")
|
||||
|
@ -769,7 +769,7 @@ static const Zigbee_Instruction zb_prog[] PROGMEM = {
|
|||
ZI_ON_ERROR_GOTO(ZIGBEE_LABEL_ABORT)
|
||||
ZI_ON_TIMEOUT_GOTO(ZIGBEE_LABEL_ABORT)
|
||||
ZI_ON_RECV_UNEXPECTED(&EZ_Recv_Default)
|
||||
ZI_WAIT(10500) // wait for 10 seconds for Tasmota to stabilize
|
||||
ZI_WAIT(15500) // wait for 15 seconds for Tasmota to stabilize
|
||||
|
||||
// Hardware reset
|
||||
ZI_LOG(LOG_LEVEL_INFO, kResettingDevice) // Log Debug: resetting EZSP device
|
||||
|
@ -869,8 +869,8 @@ static const Zigbee_Instruction zb_prog[] PROGMEM = {
|
|||
ZI_CALL(&Z_State_Ready, 1) // Now accept incoming messages
|
||||
ZI_CALL(&Z_Prepare_EEPROM, 0)
|
||||
ZI_CALL(&Z_Load_Devices, 0)
|
||||
ZI_CALL(&Z_Load_Data, 0)
|
||||
ZI_CALL(&Z_Set_Save_Data_Timer, 0)
|
||||
ZI_CALL(&Z_Load_Data_EEPROM, 0)
|
||||
ZI_CALL(&Z_Set_Save_Data_Timer_EEPROM, 0)
|
||||
ZI_CALL(&Z_Query_Bulbs, 0)
|
||||
|
||||
ZI_LABEL(ZIGBEE_LABEL_MAIN_LOOP)
|
||||
|
|
|
@ -1891,7 +1891,7 @@ int32_t Z_Load_Devices(uint8_t value) {
|
|||
//
|
||||
// Callback for loading Zigbee data from EEPROM, called by the state machine
|
||||
//
|
||||
int32_t Z_Load_Data(uint8_t value) {
|
||||
int32_t Z_Load_Data_EEPROM(uint8_t value) {
|
||||
hydrateDevicesDataFromEEPROM();
|
||||
return 0; // continue
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue