Skip to content

Commit

Permalink
EZSP: fixes (Koenkk#335)
Browse files Browse the repository at this point in the history
* take rtscts option from configuration

* detect device type as Router

* fix node descriptor response

* fix bind/unbind
  • Loading branch information
kirovilya authored Mar 22, 2021
1 parent 6edaeb9 commit 1d9968b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 19 deletions.
20 changes: 13 additions & 7 deletions src/adapter/ezsp/adapter/ezspAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class EZSPAdapter extends Adapter {
public async start(): Promise<StartResult> {
await this.driver.startup(this.port.path, {
baudRate: this.port.baudRate || 115200,
rtscts: this.port.rtscts,
parity: 'none',
stopBits: 1,
xon: true,
Expand Down Expand Up @@ -184,7 +185,7 @@ class EZSPAdapter extends Adapter {

public async getCoordinatorVersion(): Promise<CoordinatorVersion> {
// todo
return {type: 'EmberZNet', meta: this.driver.version};
return {type: `EZSP v${this.driver.version.product}`, meta: this.driver.version};
}

public async reset(type: 'soft' | 'hard'): Promise<void> {
Expand Down Expand Up @@ -265,9 +266,10 @@ class EZSPAdapter extends Adapter {
networkAddress, EmberZDOCmd.Node_Desc_req, EmberZDOCmd.Node_Desc_rsp,
networkAddress
);
const logicaltype = descriptor[3].byte1 & 0x07;
return {
manufacturerCode: descriptor[2].manufacturer_code,
type: (descriptor[1] == 0) ? 'Coordinator' : 'EndDevice'
manufacturerCode: descriptor[3].manufacturer_code,
type: (logicaltype == 0) ? 'Coordinator' : (logicaltype == 1) ? 'Router' : 'EndDevice'
};
});
}
Expand Down Expand Up @@ -396,11 +398,13 @@ class EZSPAdapter extends Adapter {
): Promise<void> {
return this.driver.queue.execute<void>(async () => {
const ieee = new EmberEUI64(sourceIeeeAddress);
const ieeeDst = new EmberEUI64(destinationAddressOrGroup as string);
const addrmode = (type === 'group') ? 1 : 3;
const ieeeDst = (type === 'group') ? destinationAddressOrGroup :
new EmberEUI64(destinationAddressOrGroup as string);
await this.driver.zdoRequest(
destinationNetworkAddress, EmberZDOCmd.Bind_req, EmberZDOCmd.Bind_rsp,
ieee, sourceEndpoint, clusterID,
{addrmode: 0x03, ieee: ieeeDst, endpoint: destinationEndpoint}
{addrmode: addrmode, ieee: ieeeDst, endpoint: destinationEndpoint}
);
}, destinationNetworkAddress);
}
Expand All @@ -412,11 +416,13 @@ class EZSPAdapter extends Adapter {
): Promise<void> {
return this.driver.queue.execute<void>(async () => {
const ieee = new EmberEUI64(sourceIeeeAddress);
const ieeeDst = new EmberEUI64(destinationAddressOrGroup as string);
const addrmode = (type === 'group') ? 1 : 3;
const ieeeDst = (type === 'group') ? destinationAddressOrGroup :
new EmberEUI64(destinationAddressOrGroup as string);
await this.driver.zdoRequest(
destinationNetworkAddress, EmberZDOCmd.Unbind_req, EmberZDOCmd.Unbind_rsp,
ieee, sourceEndpoint, clusterID,
{addrmode: 0x03, ieee: ieeeDst, endpoint: destinationEndpoint}
{addrmode: addrmode, ieee: ieeeDst, endpoint: destinationEndpoint}
);
}, destinationNetworkAddress);
}
Expand Down
2 changes: 1 addition & 1 deletion src/adapter/ezsp/driver/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ export const COMMANDS: { [key: string]: [number, any[], any[]] } = {
/* eslint-disable-next-line @typescript-eslint/no-explicit-any*/
export const ZDO_COMMANDS: { [key: string]: [number, any[], any[]] } = {
"Node_Desc_req": [0x0002, [uint8_t, EmberNodeId], [EmberStatus]],
"Node_Desc_rsp": [0x8002, [EmberStatus, EmberNodeId, EmberNodeDescriptor], []],
"Node_Desc_rsp": [0x8002, [uint8_t, EmberStatus, EmberNodeId, EmberNodeDescriptor], []],
"Simple_Desc_req": [0x0004, [uint8_t, EmberNodeId, uint8_t], [EmberStatus]],
"Simple_Desc_rsp": [0x8004, [uint8_t, EmberStatus, EmberNodeId, uint8_t, EmberSimpleDescriptor], []],
"Active_EP_req": [0x0005, [uint8_t, EmberNodeId], [EmberStatus]],
Expand Down
9 changes: 5 additions & 4 deletions src/adapter/ezsp/driver/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import Debug from "debug";
import equals from 'fast-deep-equal/es6';

const debug = {
error: Debug('zigbee-herdsman:adapter:driver:error'),
log: Debug('zigbee-herdsman:adapter:driver:log'),
error: Debug('zigbee-herdsman:adapter:ezsp:driver:error'),
log: Debug('zigbee-herdsman:adapter:ezsp:driver'),
};

interface AddEndpointParameters {
Expand Down Expand Up @@ -372,8 +372,9 @@ export class Driver extends EventEmitter {
frame.sourceEndpoint = 0;
frame.destinationEndpoint = 0;
frame.groupId = 0;
frame.options = EmberApsOption.APS_OPTION_ENABLE_ROUTE_DISCOVERY|EmberApsOption.APS_OPTION_RETRY;
//frame.options = EmberApsOption.APS_OPTION_NONE;
//frame.options = EmberApsOption.APS_OPTION_ENABLE_ROUTE_DISCOVERY;
//frame.options = EmberApsOption.APS_OPTION_ENABLE_ROUTE_DISCOVERY|EmberApsOption.APS_OPTION_RETRY;
frame.options = EmberApsOption.APS_OPTION_NONE;
return frame;
}

Expand Down
2 changes: 1 addition & 1 deletion src/adapter/ezsp/driver/ezsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class Ezsp extends EventEmitter {
this.serialDriver.on('received', this.onFrameReceived.bind(this));
}

public async connect(path: string, options: Record<string, number>): Promise<void> {
public async connect(path: string, options: Record<string, number|boolean>): Promise<void> {
await this.serialDriver.connect(path, options);
this.watchdogTimer = setInterval(
this.watchdogHandler.bind(this),
Expand Down
20 changes: 18 additions & 2 deletions src/adapter/ezsp/driver/types/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,11 +626,27 @@ export class EmberSimpleDescriptor extends EzspStruct {
}

export class EmberMultiAddress extends EzspStruct {
static _fields = [
['addrmode', basic.uint8_t], // 0x3
static fields3 = [
['addrmode', basic.uint8_t],
['ieee', named.EmberEUI64],
['endpoint', basic.uint8_t],
];
static fields1 = [
['addrmode', basic.uint8_t],
['nwk', named.EmberNodeId],
];
/* eslint-disable-next-line @typescript-eslint/no-explicit-any*/
static serialize(cls: any, obj: any): Buffer {
const addrmode = obj['addrmode'];
/* eslint-disable-next-line @typescript-eslint/no-explicit-any*/
const fields = (addrmode == 3) ? cls.fields3 : cls.fields1;
/* eslint-disable-next-line @typescript-eslint/no-explicit-any*/
return Buffer.concat(fields.map((field: any[]) => {
const value = obj[field[0]];
console.assert(field[1]);
return field[1].serialize(field[1], value);
}));
}
}

export class EmberNeighbor extends EzspStruct {
Expand Down
12 changes: 8 additions & 4 deletions src/adapter/ezsp/driver/uart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class SerialDriver extends EventEmitter {
this.waitressValidator, this.waitressTimeoutFormatter);
}

async connect(path: string, options: Record<string, number>): Promise<void> {
async connect(path: string, options: Record<string, number|boolean>): Promise<void> {
this.portType = SocketPortUtils.isTcpPath(path) ? 'socket' : 'serial';
if (this.portType === 'serial') {
await this.openSerialPort(path, options);
Expand All @@ -176,8 +176,12 @@ export class SerialDriver extends EventEmitter {
}
}

private async openSerialPort(path: string, opt: Record<string, number>): Promise<void> {
const options = {baudRate: opt.baudRate, rtscts: false, autoOpen: false};
private async openSerialPort(path: string, opt: Record<string, number|boolean>): Promise<void> {
const options = {
baudRate: typeof opt.baudRate === 'number' ? opt.baudRate : 115200,
rtscts: typeof opt.rtscts === 'boolean' ? opt.rtscts : false,
autoOpen: false
};

debug(`Opening SerialPort with ${path} and ${JSON.stringify(options)}`);
this.serialPort = new SerialPort(path, options);
Expand Down Expand Up @@ -415,8 +419,8 @@ export class SerialDriver extends EventEmitter {
/* Construct a reset frame */
const rst_frame = Buffer.concat([Buffer.from([CANCEL]), this.make_frame([0xC0])]);
debug(`Write reset`);
this.writer.writeBuffer(rst_frame);
this.resetDeferred = new Deferred<void>();
this.writer.writeBuffer(rst_frame);
return this.resetDeferred.promise;
}

Expand Down

0 comments on commit 1d9968b

Please sign in to comment.