Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backup and restore network #1439

Merged
merged 5 commits into from
Dec 7, 2024
Merged

Backup and restore network #1439

merged 5 commits into from
Dec 7, 2024

Conversation

cdjackson
Copy link
Member

This adds a more complete backup and restore implementation. Previously managing the node XML files was problematic since the library manages these files, meaning the user application couldn’t easily modify or replace them while the network was running. Conversely, replacing them with the network offline was problematic since the user much call ZigBeeNetworkManager.startup(true) to initialise the network, and this will remove all nodes and their XML files.

With this PR, the backup information is now serialised as a single entity through the ZigBeeDataStore. New methods to backup and restore have been added to the ZigBeeNetworkManager that manage the removal of nodes, and initialisation of the system in the correct order.

Summary of Changes at user level

ZigBeeNetworkDataStore has the following new methods -:

  • boolean writeBackup(ZigBeeNetworkBackupDao backup) - the user MUST implement this method to store a backup.
  • ZigBeeNetworkBackupDao readBackup(UUID uuid) - the user MUST implement this method to read a backup from the store.
  • Set<ZigBeeNetworkBackupDao> listBackups() - the user MAY implement this method to read all the current backups. This is currently only used in the console so users implementing their own system may not require this.

ZigBeeNetworkManager has the following new methods -:

  • UUID createBackup() - creates a backup of the current network. This will store all data, including the network configuration (eg keys, channel, PANID etc) and the devices attached to the network, and returns a UUID identifying the backup. Serialisation of the data is through the ZigBeeNetworkDataStore.writeBackup method.
  • ZigBeeStatus restoreBackup(UUID uuid) - restores a backup given the UUID. This will read data from the data store using the ZigBeeNetworkDataStore.readBackup method, and will restore all nodes, and reconfigure the coordinator. This has been tested when running with an existing network - calling at other times may require further updates.

Dongle changes

The following changes are made that do not directly impact users, but are added to lower level interfaces toward the coordinator -:

ZigBeeTransportTransmit

  • ZigBeeStatus setNetworkState(ZigBeeNetworkState networkState) - this method allows the ZigBeeNetworkManager to take the coordinator offline during the restoration of a backup.
  • boolean setIeeeAddress(IeeeAddress ieeeAddress) - this method allows the ZigBeeNetworkManager to set the IeeeAddress of the coordinator.
  • boolean setNwkAddress(int networkAddress) - this method allows the ZigBeeNetworkManager to set the network Address of the coordinator. Note that this is not used at this time, but was added to the interface for completeness.

Note that this PR only adds this support to the ZigBeeDongleEzsp implementation (ie Silabs Ember / Gecko chipset).

There is a limitation that the IEEE address can only be written to NVRAM once after which the token area needs erasing. Silabs introduced a function to do this in or around version 7.3 or 7.4. This is function ID 0x77, but in earlier versions of EZSP (prior to 7 I think) this ID was allocated to becomeTrustCenter. This PR removes the becomeTrustCenter function (it was not used in the library) and adds the tokenFactoryReset function as 0x77. Further work may be need for this to work on older firmware (if it’s even possible).
If the IEEE address doesn’t change (which should be the case if the network is backed up / restored using the same physical coordinator) then this will still work fine.

Console Application

The netback console command now has backup, restore UUID and list subcommands to manage backups. These are reasonably self explanatory and simply call the above methods in the ZigBeeNetworkManager.

@@ -135,10 +147,9 @@
File file = getFile(address);

ZigBeeNodeDao node = null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"))) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), CHARSET))) {

Check warning

Code scanning / CodeQL

Potential input resource leak Warning

This FileInputStream is not always closed on method exit.
@@ -151,10 +162,9 @@
XStream stream = openStream();
File file = getFile(node.getIeeeAddress());

try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"))) {
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), CHARSET))) {

Check warning

Code scanning / CodeQL

Potential output resource leak Warning

This FileOutputStream is not always closed on method exit.
@@ -173,10 +183,9 @@
XStream stream = openStream();
File file = getFile(key);

try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"))) {
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), CHARSET))) {

Check warning

Code scanning / CodeQL

Potential output resource leak Warning

This FileOutputStream is not always closed on method exit.
XStream stream = openStream();
File file = getFile(backup.getUuid());

try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), CHARSET))) {

Check warning

Code scanning / CodeQL

Potential output resource leak Warning

This FileOutputStream is not always closed on method exit.
File file = getFile(uuid);

ZigBeeNetworkBackupDao backup = null;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), CHARSET))) {

Check warning

Code scanning / CodeQL

Potential input resource leak Warning

This FileInputStream is not always closed on method exit.
* @param uuid the unique {@link UUID} referencing the backup to restore
* @return
*/
public boolean restoreBackup(UUID uuid) {

Check notice

Code scanning / CodeQL

Useless parameter Note

The parameter 'uuid' is never used.
* @param networkAddress the address to set
* @return true if the address was set
*/
default boolean setNwkAddress(int networkAddress) {

Check notice

Code scanning / CodeQL

Useless parameter Note

The parameter 'networkAddress' is never used.
@cdjackson cdjackson force-pushed the restore-backup branch 2 times, most recently from 8561dea to 5d07e9f Compare October 9, 2024 17:22
Signed-off-by: Chris Jackson <chris@cd-jackson.com>
Signed-off-by: Chris Jackson <chris@cd-jackson.com>
Signed-off-by: Chris Jackson <chris@cd-jackson.com>
Signed-off-by: Chris Jackson <chris@cd-jackson.com>
@cdjackson cdjackson merged commit 116700a into master Dec 7, 2024
4 checks passed
@cdjackson cdjackson deleted the restore-backup branch December 7, 2024 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants