From ca5fa57732d248aa6cb10b0cc54b875a659c906c Mon Sep 17 00:00:00 2001 From: Nerivec <62446222+Nerivec@users.noreply.github.com> Date: Fri, 29 Mar 2024 20:55:27 +0100 Subject: [PATCH] fix: Ember: bugfixes (#993) --- src/adapter/ember/adapter/emberAdapter.ts | 12 ++++-------- src/adapter/ember/ezsp/ezsp.ts | 6 +++--- src/adapter/ember/uart/ash.ts | 15 +++++++++------ 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/adapter/ember/adapter/emberAdapter.ts b/src/adapter/ember/adapter/emberAdapter.ts index 0543e7f387..689d049ad5 100644 --- a/src/adapter/ember/adapter/emberAdapter.ts +++ b/src/adapter/ember/adapter/emberAdapter.ts @@ -797,15 +797,11 @@ export class EmberAdapter extends Adapter { await this.onNCPPreReset(); - try { - // NOTE: something deep in this call can throw too - const result = (await this.ezsp.start()); + // NOTE: something deep in this call can throw too + const startResult = (await this.ezsp.start()); - if (result !== EzspStatus.SUCCESS) { - throw new Error(`Failed to start EZSP layer with status=${EzspStatus[result]}.`); - } - } catch (err) { - throw err; + if (startResult !== EzspStatus.SUCCESS) { + throw new Error(`Failed to start EZSP layer with status=${EzspStatus[startResult]}.`); } // call before any other command, else fails diff --git a/src/adapter/ember/ezsp/ezsp.ts b/src/adapter/ember/ezsp/ezsp.ts index e1eee9eace..0d14f42fee 100644 --- a/src/adapter/ember/ezsp/ezsp.ts +++ b/src/adapter/ember/ezsp/ezsp.ts @@ -298,7 +298,7 @@ export class Ezsp extends EventEmitter { this.buffalo = new EzspBuffalo(this.frameContents); this.ash = new UartAsh(options); - this.ash.on(AshEvents.rxError, this.onAshRxError.bind(this)); + this.ash.on(AshEvents.fatalError, this.onAshFatalError.bind(this)); this.ash.on(AshEvents.frame, this.onAshFrame.bind(this)); } @@ -389,8 +389,8 @@ export class Ezsp extends EventEmitter { return this.ash.connected; } - private onAshRxError(status: EzspStatus): void { - this.ezspErrorHandler(status); + private onAshFatalError(status: EzspStatus): void { + this.emit(EzspEvents.ncpNeedsResetAndInit, status); } private onAshFrame(): void { diff --git a/src/adapter/ember/uart/ash.ts b/src/adapter/ember/uart/ash.ts index bb7bcd7a16..b17a0de7d2 100644 --- a/src/adapter/ember/uart/ash.ts +++ b/src/adapter/ember/uart/ash.ts @@ -61,8 +61,8 @@ const ashGetACKNum = (ctrl: number): number => ((ctrl & ASH_ACKNUM_MASK) >> ASH_ export enum AshEvents { - /** When the ASH protocol detects an error while receiving a frame. NOTE: TX errors are handled by the EZSP layer. */ - rxError = 'rxError', + /** When the ASH protocol detects a fatal error (bubbles up to restart adapter). */ + fatalError = 'fatalError', /** When a frame has been parsed and queued in the rxQueue. */ frame = 'frame', } @@ -318,7 +318,11 @@ export class UartAsh extends EventEmitter { return false; } - return SocketPortUtils.isTcpPath(this.portOptions.path) ? !this.socketPort?.closed : !!this.serialPort?.isOpen; + if (SocketPortUtils.isTcpPath(this.portOptions.path)) { + return this.socketPort && !this.socketPort.closed; + } else { + return this.serialPort && this.serialPort.isOpen; + } } /** @@ -572,8 +576,7 @@ export class UartAsh extends EventEmitter { */ private async onPortError(error: Error): Promise { console.log(`Port error: ${error}`); - this.hostDisconnect(EzspStatus.ERROR_SERIAL_INIT); - await this.stop(); + this.emit(AshEvents.fatalError, EzspStatus.ERROR_SERIAL_INIT); } /** @@ -608,7 +611,7 @@ export class UartAsh extends EventEmitter { if (this.flags & Flag.CONNECTED) { console.error(`Error while parsing received frame, status=${EzspStatus[status]}.`); // if we're connected (not in reset) and get here, we need to reset - this.emit(AshEvents.rxError, EzspStatus.HOST_FATAL_ERROR); + this.emit(AshEvents.fatalError, EzspStatus.HOST_FATAL_ERROR); return; } else { debug(`Error while parsing received frame in NOT_CONNECTED state (flags=${this.flags}), status=${EzspStatus[status]}.`);