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

ZStack: fix request network address blocking all requests #1256

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/adapter/z-stack/adapter/zStackAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class ZStackAdapter extends Adapter {
const clusterId = Zdo.ClusterId.NETWORK_ADDRESS_REQUEST;
const zdoPayload = Zdo.Buffalo.buildRequest(this.hasZdoMessageOverhead, clusterId, ieeeAddr as EUI64, false, 0);

const result = await this.sendZdo(ieeeAddr, ZSpec.NULL_NODE_ID, clusterId, zdoPayload, false);
const result = await this.sendZdoInternal(ieeeAddr, ZSpec.NULL_NODE_ID, clusterId, zdoPayload, false, true);

/* istanbul ignore else */
if (Zdo.Buffalo.checkStatus(result)) {
Expand Down Expand Up @@ -308,7 +308,35 @@ class ZStackAdapter extends Adapter {
payload: Buffer,
disableResponse: boolean,
): Promise<ZdoTypes.RequestToResponseMap[K] | void> {
return await this.queue.execute(async () => {
// @ts-expect-error TODO fix
return await this.sendZdoInternal(ieeeAddress, networkAddress, clusterId, payload, disableResponse, false);
}

private async sendZdoInternal(
ieeeAddress: string,
networkAddress: number,
clusterId: Zdo.ClusterId,
payload: Buffer,
disableResponse: true,
skipQueue: boolean,
): Promise<void>;
private async sendZdoInternal<K extends keyof ZdoTypes.RequestToResponseMap>(
ieeeAddress: string,
networkAddress: number,
clusterId: K,
payload: Buffer,
disableResponse: false,
skipQueue: boolean,
): Promise<ZdoTypes.RequestToResponseMap[K]>;
private async sendZdoInternal<K extends keyof ZdoTypes.RequestToResponseMap>(
ieeeAddress: string,
networkAddress: number,
clusterId: K,
payload: Buffer,
disableResponse: boolean,
skipQueue: boolean,
): Promise<ZdoTypes.RequestToResponseMap[K] | void> {
const func = async (): Promise<ZdoTypes.RequestToResponseMap[K] | void> => {
this.checkInterpanLock();

// stack-specific requirements
Expand Down Expand Up @@ -404,7 +432,8 @@ class ZStackAdapter extends Adapter {

return response.payload.zdo;
}
}, networkAddress);
};
return skipQueue ? await func() : await this.queue.execute(func, networkAddress);
}

public async sendZclFrameToEndpoint(
Expand All @@ -417,6 +446,7 @@ class ZStackAdapter extends Adapter {
disableRecovery: boolean,
sourceEndpoint?: number,
): Promise<Events.ZclPayload | void> {
logger.debug(`== sendZclFrameToEndpoint add to queue - ${ieeeAddr}/${networkAddress}`, NS);
return await this.queue.execute<Events.ZclPayload | void>(async () => {
this.checkInterpanLock();
return await this.sendZclFrameToEndpointInternal(
Expand Down
7 changes: 7 additions & 0 deletions src/utils/queue.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import {logger} from './logger';

interface Job {
key?: string | number;
running: boolean;
start?: () => void;
}

const NS = 'zh:queue';

class Queue {
private jobs: Job[];
private readonly concurrent: number;
Expand All @@ -14,6 +18,7 @@ class Queue {
}

public async execute<T>(func: () => Promise<T>, key?: string | number): Promise<T> {
logger.debug(`== queue add - ${key} (keys=${this.jobs.map((j) => j.key)})`, NS);
const job: Job = {key, running: false};
this.jobs.push(job);

Expand All @@ -33,9 +38,11 @@ class Queue {
}

try {
logger.debug(`== queue execute func - ${key} (keys=${this.jobs.map((j) => j.key)})`, NS);
return await func();
} finally {
this.jobs.splice(this.jobs.indexOf(job), 1);
logger.debug(`== queue remove - ${key} (keys=${this.jobs.map((j) => j.key)})`, NS);
this.executeNext();
}
}
Expand Down
Loading