Skip to content

Commit

Permalink
actual changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed Jul 27, 2024
1 parent 14cb20a commit 8f3d6b6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
41 changes: 21 additions & 20 deletions src/adapter/z-stack/adapter/zStackAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {logger} from '../../../utils/logger';
import {BroadcastAddress} from '../../../zspec/enums';
import * as Zcl from '../../../zspec/zcl';
import {Status as ZdoStatus} from '../../../zspec/zdo';

Check failure on line 9 in src/adapter/z-stack/adapter/zStackAdapter.ts

View workflow job for this annotation

GitHub Actions / ci

'ZdoStatus' is defined but never used
import {BuffaloZdo} from '../../../zspec/zdo/buffaloZdo';
import {SimpleDescriptorResponse, ActiveEndpointsResponse} from '../../../zspec/zdo/definition/tstypes';
import Adapter from '../../adapter';
import * as Events from '../../events';
import {
Expand All @@ -29,7 +31,7 @@ import {
import * as Constants from '../constants';
import {Constants as UnpiConstants} from '../unpi';
import {Znp, ZpiObject} from '../znp';
import {ZpiObjectPayload} from '../znp/tstype';
import {MtCmdZdo, ZpiObjectPayload} from '../znp/tstype';
import {ZnpAdapterManager} from './manager';
import {ZnpVersion} from './tstype';

Expand Down Expand Up @@ -176,24 +178,24 @@ class ZStackAdapter extends Adapter {
public async getCoordinator(): Promise<Coordinator> {
return this.queue.execute<Coordinator>(async () => {
this.checkInterpanLock();
const activeEpRsp = this.waitForAreqZdo('activeEpRsp');
const activeEpRsp = this.waitForAreqZdo<ActiveEndpointsResponse>('activeEpRsp');
await this.znp.request(Subsystem.ZDO, 'activeEpReq', {dstaddr: 0, nwkaddrofinterest: 0}, activeEpRsp.ID);
const activeEp = await activeEpRsp.start();

const deviceInfo = await this.znp.request(Subsystem.UTIL, 'getDeviceInfo', {});

const endpoints = [];
for (const endpoint of activeEp.payload.activeeplist) {
const simpleDescRsp = this.waitForAreqZdo('simpleDescRsp', {endpoint});
for (const endpoint of activeEp.endpointList) {
const simpleDescRsp = this.waitForAreqZdo<SimpleDescriptorResponse>('simpleDescRsp', {endpoint});
await this.znp.request(Subsystem.ZDO, 'simpleDescReq', {dstaddr: 0, nwkaddrofinterest: 0, endpoint}, simpleDescRsp.ID);
const simpleDesc = await simpleDescRsp.start();

endpoints.push({
ID: simpleDesc.payload.endpoint,
profileID: simpleDesc.payload.profileid,
deviceID: simpleDesc.payload.deviceid,
inputClusters: simpleDesc.payload.inclusterlist,
outputClusters: simpleDesc.payload.outclusterlist,
ID: simpleDesc.endpoint,
profileID: simpleDesc.profileId,
deviceID: simpleDesc.deviceId,
inputClusters: simpleDesc.inClusterList,
outputClusters: simpleDesc.outClusterList,
});
}

Expand Down Expand Up @@ -827,7 +829,7 @@ class ZStackAdapter extends Adapter {
): Promise<void> {
return this.queue.execute<void>(async () => {
this.checkInterpanLock();
const response = this.waitForAreqZdo(`${bindType}Rsp`, {srcaddr: destinationNetworkAddress});
const response = this.waitForAreqZdo<void>(`${bindType}Rsp`, {srcaddr: destinationNetworkAddress});

const payload = {
dstaddr: destinationNetworkAddress,
Expand Down Expand Up @@ -1096,20 +1098,19 @@ class ZStackAdapter extends Adapter {
});
}

private waitForAreqZdo(command: string, payload?: ZpiObjectPayload): {start: () => Promise<ZpiObject>; ID: number} {
private waitForAreqZdo<T>(command: string, payload?: ZpiObjectPayload): {start: () => Promise<T>; ID: number} {
const result = this.znp.waitFor(UnpiConstants.Type.AREQ, Subsystem.ZDO, command, payload);
const start = (): Promise<ZpiObject> => {
const start = (): Promise<T> => {
const startResult = result.start();
return new Promise<ZpiObject>((resolve, reject) => {
return new Promise<T>((resolve, reject) => {
startResult.promise
.then((response) => {
// Even though according to the Z-Stack docs the status is `0` or `1`, the actual code
// shows it sets the `zstack_ZdpStatus` which contains the ZDO status.
const code = response.payload.status;
if (code !== ZdoStatus.SUCCESS) {
reject(new Error(`ZDO error: ${command.replace('Rsp', '')} failed with status '${ZdoStatus[code]}' (${code})`));
} else {
resolve(response);
const cmd: MtCmdZdo = response.command;
try {
const zdoResponse = BuffaloZdo.readResponse(cmd.zdo.cluterId, response.unpiFrame.data.subarray(cmd.zdo.skip)) as T;
resolve(zdoResponse);
} catch (error) {
reject(error);
}
})
.catch(reject);
Expand Down
2 changes: 1 addition & 1 deletion src/adapter/z-stack/znp/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1539,7 +1539,7 @@ const Definition: {
name: 'unbindRsp',
ID: 162,
type: CommandType.AREQ,
zdo: {cluterId: ZdoClusterId.BIND_RESPONSE},
zdo: {cluterId: ZdoClusterId.BIND_RESPONSE, skip: 2},

This comment has been minimized.

Copy link
@Koenkk

Koenkk Jul 27, 2024

Author Owner

@Nerivec unfortunately the command IDs do not always match the ZdoClusterIds so some manual mapping is needed. skip indicates the number of bytes that should be skipped (e.g. srcaddr is skipped here)

request: [
{name: 'srcaddr', parameterType: ParameterType.UINT16},
{name: 'status', parameterType: ParameterType.UINT8},
Expand Down
2 changes: 1 addition & 1 deletion src/adapter/z-stack/znp/tstype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface MtCmd {
}

interface MtCmdZdo extends MtCmd {
zdo: {cluterId: ZdoClusterId};
zdo: {cluterId: ZdoClusterId; skip: number};
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down

0 comments on commit 8f3d6b6

Please sign in to comment.