Skip to content

Commit

Permalink
Fix missing zdo event. Add typing for EventEmitters. Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerivec committed Jul 1, 2024
1 parent bd90b83 commit e9f7aa4
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 113 deletions.
74 changes: 21 additions & 53 deletions src/adapter/ember/adapter/emberAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ const DEFAULT_STACK_CONFIG: Readonly<StackConfig> = {
END_DEVICE_POLL_TIMEOUT: 8,// zigpc: 8
TRANSIENT_KEY_TIMEOUT_S: 300,// zigpc: 65535
};
/** Default behavior is to disable app key requests, so reduce key table to zero as well */
const DEFAULT_KEY_TABLE_SIZE = 0;
/** Default behavior is to disable app key requests */
const ALLOW_APP_KEY_REQUESTS = false;

/**
* NOTE: This from SDK is currently ignored here because of issues in below links:
Expand Down Expand Up @@ -320,15 +320,13 @@ export class EmberAdapter extends Adapter {

this.ezsp = new Ezsp(serialPortOptions);

this.ezsp.on(EzspEvents.STACK_STATUS, this.onStackStatus.bind(this));
this.ezsp.on(EzspEvents.MESSAGE_SENT, this.onMessageSent.bind(this));
this.ezsp.on(EzspEvents.ZDO_RESPONSE, this.onZDOResponse.bind(this));
this.ezsp.on(EzspEvents.END_DEVICE_ANNOUNCE, this.onEndDeviceAnnounce.bind(this));
this.ezsp.on(EzspEvents.INCOMING_MESSAGE, this.onIncomingMessage.bind(this));
this.ezsp.on(EzspEvents.TOUCHLINK_MESSAGE, this.onTouchlinkMessage.bind(this));
this.ezsp.on(EzspEvents.GREENPOWER_MESSAGE, this.onGreenpowerMessage.bind(this));

this.ezsp.on(EzspEvents.STACK_STATUS, this.onStackStatus.bind(this));
this.ezsp.on(EzspEvents.TRUST_CENTER_JOIN, this.onTrustCenterJoin.bind(this));
this.ezsp.on(EzspEvents.MESSAGE_SENT, this.onMessageSent.bind(this));
this.ezsp.on(EzspEvents.GREENPOWER_MESSAGE, this.onGreenpowerMessage.bind(this));
}

private loadStackConfig(): StackConfig {
Expand Down Expand Up @@ -535,29 +533,17 @@ export class EmberAdapter extends Adapter {
networkAddress: (payload as ZdoTypes.NetworkAddressResponse).nwkAddress,
ieeeAddr: (payload as ZdoTypes.NetworkAddressResponse).eui64
} as NetworkAddressPayload);
} else if (apsFrame.clusterId === Zdo.ClusterId.END_DEVICE_ANNOUNCE) {
this.emit(Events.deviceAnnounce, {
networkAddress: (payload as ZdoTypes.EndDeviceAnnounce).nwkAddress,
ieeeAddr: (payload as ZdoTypes.EndDeviceAnnounce).eui64
} as DeviceAnnouncePayload);
}
} catch (error) {
this.oneWaitress.resolveZDO(sender, apsFrame, error);
}
}

/**
* Emitted from @see Ezsp.ezspIncomingMessageHandler
*
* @param sender
* @param nodeId
* @param eui64
* @param macCapFlags
*/
private async onEndDeviceAnnounce(sender: NodeId, apsFrame: EmberApsFrame, payload: ZdoTypes.EndDeviceAnnounce): Promise<void> {
// reduced function device
// if ((payload.capabilities.deviceType === 0)) {

// }

this.emit(Events.deviceAnnounce, {networkAddress: payload.nwkAddress, ieeeAddr: payload.eui64} as DeviceAnnouncePayload);
}

/**
* Emitted from @see Ezsp.ezspIncomingMessageHandler
*
Expand Down Expand Up @@ -801,8 +787,6 @@ export class EmberAdapter extends Adapter {
// after network UP, as per SDK, ensures clean slate
await this.initNCPConcentrator();

// await (this.emberStartEnergyScan());// TODO: via config of some kind, better off waiting for UI supports though

// populate network cache info
const [status, , parameters] = (await this.ezsp.ezspGetNetworkParameters());

Expand Down Expand Up @@ -927,11 +911,11 @@ export class EmberAdapter extends Adapter {
+ `with status=${SLStatus[status]}.`);
}

const appKeyPolicy = DEFAULT_KEY_TABLE_SIZE > 0 ? EzspDecisionId.ALLOW_APP_KEY_REQUESTS : EzspDecisionId.DENY_APP_KEY_REQUESTS;
status = (await this.emberSetEzspPolicy(EzspPolicyId.APP_KEY_REQUEST_POLICY, appKeyPolicy));
const appKeyRequestsPolicy = ALLOW_APP_KEY_REQUESTS ? EzspDecisionId.ALLOW_APP_KEY_REQUESTS : EzspDecisionId.DENY_APP_KEY_REQUESTS;
status = await this.emberSetEzspPolicy(EzspPolicyId.APP_KEY_REQUEST_POLICY, appKeyRequestsPolicy);

if (status !== SLStatus.OK) {
throw new Error(`[INIT TC] Failed to set EzspPolicyId APP_KEY_REQUEST_POLICY to ${EzspDecisionId[appKeyPolicy]} `
throw new Error(`[INIT TC] Failed to set EzspPolicyId APP_KEY_REQUEST_POLICY to ${EzspDecisionId[appKeyRequestsPolicy]} `
+ `with status=${SLStatus[status]}.`);
}

Expand Down Expand Up @@ -966,7 +950,6 @@ export class EmberAdapter extends Adapter {

const [npStatus, nodeType, netParams] = (await this.ezsp.ezspGetNetworkParameters());

logger.debug(`[INIT TC] Current network config=${JSON.stringify(this.networkOptions)}`, NS);
logger.debug(`[INIT TC] Current adapter network: nodeType=${EmberNodeType[nodeType]} params=${JSON.stringify(netParams)}`, NS);

if ((npStatus === SLStatus.OK) && (nodeType === EmberNodeType.COORDINATOR) && (this.networkOptions.panID === netParams.panId)
Expand All @@ -981,8 +964,6 @@ export class EmberAdapter extends Adapter {
throw new Error(`[BACKUP] Failed to export Network Key with status=${SLStatus[nkStatus]}.`);
}

logger.debug(`[INIT TC] Current adapter network: networkKey=${networkKey.contents.toString('hex')}`, NS);

// config doesn't match adapter anymore
if (!networkKey.contents.equals(configNetworkKey)) {
action = NetworkInitAction.LEAVE;
Expand Down Expand Up @@ -1044,13 +1025,13 @@ export class EmberAdapter extends Adapter {
logger.info(`[INIT TC] Forming from backup.`, NS);
const keyList: LinkKeyBackupData[] = backup.devices.map((device) => {
const octets = Array.from(device.ieeeAddress.reverse());
const key: LinkKeyBackupData = {

return {
deviceEui64: `0x${octets.map(octet => octet.toString(16).padStart(2, '0')).join('')}`,
key: {contents: device.linkKey.key},
outgoingFrameCounter: device.linkKey.txCounter,
incomingFrameCounter: device.linkKey.rxCounter,
};
return key;
});

// before forming
Expand Down Expand Up @@ -1152,11 +1133,11 @@ export class EmberAdapter extends Adapter {
throw new Error(`[INIT FORM] Failed to set extended security bitmask to ${extended} with status=${SLStatus[status]}.`);
}

if (!fromBackup && DEFAULT_KEY_TABLE_SIZE > 0) {
if (!fromBackup) {
status = await this.ezsp.ezspClearKeyTable();

if (status !== SLStatus.OK) {
throw new Error(`[INIT FORM] Failed to clear key table with status=${SLStatus[status]}.`);
logger.error(`[INIT FORM] Failed to clear key table with status=${SLStatus[status]}.`, NS);
}
}

Expand Down Expand Up @@ -1305,12 +1286,9 @@ export class EmberAdapter extends Adapter {
let status: SLStatus;

for (let i = 0; i < keyTableSize; i++) {
if (i >= backupData.length) {
// erase any key index not present in backup but available on the NCP
status = (await this.ezsp.ezspEraseKeyTableEntry(i));
} else {
status = (await this.ezsp.ezspImportLinkKey(i, backupData[i].deviceEui64, backupData[i].key));
}
// erase any key index not present in backup but available on the NCP
status = (i >= backupData.length) ? await this.ezsp.ezspEraseKeyTableEntry(i) :
await this.ezsp.ezspImportLinkKey(i, backupData[i].deviceEui64, backupData[i].key);

if (status !== SLStatus.OK) {
throw new Error(`[BACKUP] Failed to ${((i >= backupData.length) ? "erase" : "set")} key table entry at index ${i} `
Expand Down Expand Up @@ -1976,17 +1954,7 @@ export class EmberAdapter extends Adapter {
throw new Error(`[BACKUP] No network key set.`);
}

let keyList: LinkKeyBackupData[] = [];

if (DEFAULT_KEY_TABLE_SIZE > 0) {
keyList = (await this.exportLinkKeys());
}

// XXX: this only makes sense on stop (if that), not hourly/on start, plus network needs to be at near-standstill @see AN1387
// const tokensBuf = (await EmberTokensManager.saveTokens(
// this.ezsp,
// Buffer.from(this.networkCache.eui64.substring(2/*0x*/), 'hex').reverse()
// ));
const keyList: LinkKeyBackupData[] = ALLOW_APP_KEY_REQUESTS ? await this.exportLinkKeys() : [];

let context: SecManContext = initSecurityManagerContext();
context.coreKeyType = SecManKeyType.TC_LINK;
Expand Down
24 changes: 12 additions & 12 deletions src/adapter/ember/ezsp/buffalo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,8 @@ export class EzspBuffalo extends Buffalo {
}

public readSecManNetworkKeyInfo(): SecManNetworkKeyInfo {
const networkKeySet = this.readUInt8() === 1 ? true : false;
const alternateNetworkKeySet = this.readUInt8() === 1 ? true : false;
const networkKeySet = this.readUInt8() !== 0;
const alternateNetworkKeySet = this.readUInt8() !== 0;
const networkKeySequenceNumber = this.readUInt8();
const altNetworkKeySequenceNumber = this.readUInt8();
const networkKeyFrameCounter = this.readUInt32();
Expand Down Expand Up @@ -632,7 +632,7 @@ export class EzspBuffalo extends Buffalo {
const channel = this.readUInt8();
const panId = this.readUInt16();
const extendedPanId = this.readListUInt8(EXTENDED_PAN_ID_SIZE);
const allowingJoin = this.readUInt8();
const allowingJoin = this.readUInt8() !== 0;
const stackProfile = this.readUInt8();
const nwkUpdateId = this.readUInt8();

Expand All @@ -650,7 +650,7 @@ export class EzspBuffalo extends Buffalo {
this.writeUInt8(value.channel);
this.writeUInt16(value.panId);
this.writeListUInt8(value.extendedPanId);
this.writeUInt8(value.allowingJoin);
this.writeUInt8(value.allowingJoin ? 1 : 0);
this.writeUInt8(value.stackProfile);
this.writeUInt8(value.nwkUpdateId);
}
Expand Down Expand Up @@ -1186,9 +1186,9 @@ export class EzspBuffalo extends Buffalo {
const nwkUpdateId = this.readUInt8();
const power = this.readUInt8();
const parentPriority = this.readUInt8();
const enhanced = this.readUInt8() === 1 ? true : false;
const permitJoin = this.readUInt8() === 1 ? true : false;
const hasCapacity = this.readUInt8() === 1 ? true : false;
const enhanced = this.readUInt8() !== 0;
const permitJoin = this.readUInt8() !== 0;
const hasCapacity = this.readUInt8() !== 0;
const panId = this.readUInt16();
const sender = this.readUInt16();
const extendedPanId = this.readListUInt8(EXTENDED_PAN_ID_SIZE);
Expand Down Expand Up @@ -1245,9 +1245,9 @@ export class EzspBuffalo extends Buffalo {
const nwkUpdateId = this.readUInt8();
const power = this.readUInt8();
const parentPriority = this.readUInt8();
const enhanced = this.readUInt8() === 1 ? true : false;
const permitJoin = this.readUInt8() === 1 ? true : false;
const hasCapacity = this.readUInt8() === 1 ? true : false;
const enhanced = this.readUInt8() !== 0;
const permitJoin = this.readUInt8() !== 0;
const hasCapacity = this.readUInt8() !== 0;
const panId = this.readUInt16();
const sender = this.readUInt16();
const extendedPanId = this.readListUInt8(EXTENDED_PAN_ID_SIZE);
Expand Down Expand Up @@ -1290,8 +1290,8 @@ export class EzspBuffalo extends Buffalo {

public readEmberTokenInfo(): EmberTokenInfo {
const nvm3Key = this.readUInt32();
const isCnt = this.readUInt8() === 1 ? true : false;
const isIdx = this.readUInt8() === 1 ? true : false;
const isCnt = this.readUInt8() !== 0;
const isIdx = this.readUInt8() !== 0;
const size = this.readUInt8();
const arraySize = this.readUInt8();

Expand Down
Loading

0 comments on commit e9f7aa4

Please sign in to comment.