Skip to content

Commit

Permalink
zStack 3.x.0 <-> zStack 3.0.x bidirectional migration (#235)
Browse files Browse the repository at this point in the history
* Fix tests

* Fix LEGACY_TCLK_TABLE_START 511 -> 273 (mistake during convert from hex)

* Added ZCD_NV_TCLK_SEED to backup. Previously it was ZCD_NV_LEGACY_TCLK_TABLE_START with wrong ID

* Fix tests

* Fix ZCD_NV_EX_TCLK_TABLE and ZCD_NV_LEGACY_TCLK_TABLE_START tables in backup data in restore tests

* Added LEGACY_TCLK_TABLE_START_12 to avoid break on 1.2 Stack

* Remove TCLK_SEED from backup

* Added ZStack 3.0 <-> 3.1 migration

* Added tests

* Remove extra blank line

* Added test to cover restore ZStack 1.2 error

* Backup: Compare version inside backup, not only for current adapter
  • Loading branch information
hacker-cb authored Oct 12, 2020
1 parent a2d915f commit 370dc74
Show file tree
Hide file tree
Showing 2 changed files with 314 additions and 64 deletions.
48 changes: 41 additions & 7 deletions src/adapter/z-stack/adapter/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {Subsystem} from '../unpi/constants';
import fs from 'fs';
import equals from 'fast-deep-equal/es6';
import Items from './nvItems';
import debug from "debug";

const {NvItemsIds, NvSystemIds, ZnpCommandStatus} = Constants.COMMON;

Expand Down Expand Up @@ -141,11 +142,43 @@ async function Restore(znp: Znp, backupPath: string, options: NetworkOptions): P
throw new Error(`Cannot restore backup, backup is for '${backup.adapterType}', current is 'zStack'`);
}

if (backup.meta.product != product) {
throw new Error(
`Cannot restore backup, backup is for '${ZnpVersion[backup.meta.product]}', ` +
`current is '${ZnpVersion[product]}'`
);
if ((product !== ZnpVersion.zStack30x && product !== ZnpVersion.zStack3x0)
|| (backup.meta.product !== ZnpVersion.zStack30x && backup.meta.product !== ZnpVersion.zStack3x0 )) {
throw new Error('Backup is only supported for Z-Stack 3');
}

// 3.0 3.x
// (0x0111)ZCD_NV_LEGACY_TCLK_TABLE_START -> (0x0004)ZCD_NV_EX_TCLK_TABLE
// (0x0075)ZCD_NV_LEGACY_NWK_SEC_MATERIAL_TABLE_START -> (0x0007)ZCD_NV_EX_NWK_SEC_MATERIAL_TABLE
if (backup.meta.product == ZnpVersion.zStack30x && product == ZnpVersion.zStack3x0) {
debug(`Migrate '${ZnpVersion[backup.meta.product]}' -> '${ZnpVersion[product]}'`);

backup.data.ZCD_NV_EX_TCLK_TABLE = {
...items.ZCD_NV_EX_TCLK_TABLE,
value: backup.data.ZCD_NV_LEGACY_TCLK_TABLE_START.value,
len: backup.data.ZCD_NV_LEGACY_TCLK_TABLE_START.len,
};

backup.data.ZCD_NV_EX_NWK_SEC_MATERIAL_TABLE = {
...items.ZCD_NV_EX_NWK_SEC_MATERIAL_TABLE,
value: backup.data.ZCD_NV_LEGACY_NWK_SEC_MATERIAL_TABLE_START.value,
len: backup.data.ZCD_NV_LEGACY_NWK_SEC_MATERIAL_TABLE_START.len,
};

} else if (backup.meta.product == ZnpVersion.zStack3x0 && product == ZnpVersion.zStack30x) {
debug(`Migrate '${ZnpVersion[backup.meta.product]}' -> '${ZnpVersion[product]}'`);

backup.data.ZCD_NV_LEGACY_TCLK_TABLE_START = {
...items.ZCD_NV_LEGACY_TCLK_TABLE_START,
value: backup.data.ZCD_NV_EX_TCLK_TABLE.value,
len: backup.data.ZCD_NV_EX_TCLK_TABLE.len,
};

backup.data.ZCD_NV_LEGACY_NWK_SEC_MATERIAL_TABLE_START = {
...items.ZCD_NV_LEGACY_NWK_SEC_MATERIAL_TABLE_START,
value: backup.data.ZCD_NV_EX_NWK_SEC_MATERIAL_TABLE.value,
len: backup.data.ZCD_NV_EX_NWK_SEC_MATERIAL_TABLE.len,
};
}

if (!equals(backup.data.ZCD_NV_CHANLIST.value, Constants.Utils.getChannelMask(options.channelList))) {
Expand Down Expand Up @@ -181,7 +214,8 @@ async function Restore(znp: Znp, backupPath: string, options: NetworkOptions): P

await znp.request(Subsystem.SYS, 'osalNvWrite', backup.data.ZCD_NV_EXTADDR);
await znp.request(Subsystem.SYS, 'osalNvItemInit', ZCD_NV_NIB, null,
[ZnpCommandStatus.NV_ITEM_INITIALIZED]);
[ZnpCommandStatus.SUCCESS, ZnpCommandStatus.NV_ITEM_INITIALIZED]);
await znp.request(Subsystem.SYS, 'osalNvWrite', backup.data.ZCD_NV_NIB);
await znp.request(Subsystem.SYS, 'osalNvWrite', backup.data.ZCD_NV_PANID);
await znp.request(Subsystem.SYS, 'osalNvWrite', backup.data.ZCD_NV_EXTENDED_PAN_ID);
await znp.request(Subsystem.SYS, 'osalNvWrite', backup.data.ZCD_NV_NWK_ACTIVE_KEY_INFO);
Expand All @@ -202,7 +236,7 @@ async function Restore(znp: Znp, backupPath: string, options: NetworkOptions): P
}

await znp.request(Subsystem.SYS, 'osalNvItemInit', Items.znpHasConfiguredInit(product), null,
[ZnpCommandStatus.NV_ITEM_INITIALIZED]);
[ZnpCommandStatus.SUCCESS, ZnpCommandStatus.NV_ITEM_INITIALIZED]);
await znp.request(Subsystem.SYS, 'osalNvWrite', Items.znpHasConfigured(product));
await znp.request(Subsystem.SYS, 'osalNvItemInit', bdbNodeIsOnANetwork, null,
[ZnpCommandStatus.SUCCESS, ZnpCommandStatus.NV_ITEM_INITIALIZED]);
Expand Down
Loading

0 comments on commit 370dc74

Please sign in to comment.