Skip to content

Commit

Permalink
fix: Cleanup SerialPort async wrappers (#949)
Browse files Browse the repository at this point in the history
* Cleanup SerialPort async wrappers.

* Fix tests (bypass start flush).
  • Loading branch information
Nerivec authored Mar 4, 2024
1 parent 795c944 commit cbd68df
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
14 changes: 9 additions & 5 deletions src/adapter/ember/uart/ash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ export class UartAsh extends EventEmitter {

try {
await this.serialPort.asyncOpen();
debug('Serial port opened');
debug(`Serial port opened: ${JSON.stringify(await this.serialPort.asyncGet())}`);

this.serialPort.once('close', this.onPortClose.bind(this));
this.serialPort.on('error', this.onPortError.bind(this));
Expand Down Expand Up @@ -631,10 +631,14 @@ export class UartAsh extends EventEmitter {

console.log(`======== ASH starting ========`);

if (this.serialPort != null) {
this.serialPort.flush();// clear read/write buffers
} else {
// XXX: Socket equiv?
try {
if (this.serialPort != null) {
await this.serialPort.asyncFlush();// clear read/write buffers
} else {
// XXX: Socket equiv?
}
} catch (err) {
console.error(`Error while flushing before start: ${err}`);
}

this.sendExec();
Expand Down
39 changes: 19 additions & 20 deletions src/adapter/serialPort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,30 @@ export class SerialPort<T extends AutoDetectTypes = AutoDetectTypes> extends Ser

public async asyncOpen(): Promise<void> {
return new Promise((resolve, reject): void => {
this.open((error: Error | null): void => {
if (error) {
reject(new Error(`Error while opening serialport '${error}'`));
} else {
resolve();
}
});
this.open((err): void => (err ? reject(err) : resolve()));
});
}

public async asyncClose(): Promise<void> {
return new Promise((resolve, reject): void => {
this.close((err): void => (err ? reject(err) : resolve()));
});
}

public async asyncFlush(): Promise<void> {
return new Promise((resolve, reject): void => {
this.flush((err): void => (err ? reject(err) : resolve()));
});
}

public async asyncFlushAndClose(): Promise<void> {
await this.asyncFlush();
await this.asyncClose();
}

public async asyncGet(): Promise<object> {
return new Promise((resolve, reject): void => {
this.flush((flushError: Error | null): void => {
if (flushError) {
reject(new Error(`Error while flushing serialport '${flushError}'`));
} else {
this.close((closeError: Error | null): void => {
if (closeError) {
reject(new Error(`Error while closing serialport '${closeError}'`));
} else {
resolve();
}
});
}
});
this.get((err, options?): void => (err ? reject(err) : resolve(options)));
})
}
}
6 changes: 6 additions & 0 deletions test/adapter/ember/ash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ describe('Ember UART ASH Protocol', () => {
expect(uartAsh.parser).toBeDefined();
expect(uartAsh.portOpen).toBeTruthy();

//@ts-expect-error private
jest.spyOn(uartAsh.serialPort, 'asyncFlush').mockImplementationOnce(jest.fn());
//@ts-expect-error private
uartAsh.serialPort.port.emitData(Buffer.from(RECD_RSTACK_BYTES));
const startResult = (await uartAsh.start());
Expand Down Expand Up @@ -221,6 +223,8 @@ describe('Ember UART ASH Protocol', () => {
const badCrcRSTACK = Buffer.from(RECD_RSTACK_BYTES);
badCrcRSTACK[badCrcRSTACK.length - 2] = 0;// throw CRC low

//@ts-expect-error private
jest.spyOn(uartAsh.serialPort, 'asyncFlush').mockImplementationOnce(jest.fn());
//@ts-expect-error private
uartAsh.serialPort.port.emitData(badCrcRSTACK);
const startResult = (await uartAsh.start());
Expand All @@ -240,6 +244,8 @@ describe('Ember UART ASH Protocol', () => {
beforeEach(async () => {
const resetResult = (await uartAsh.resetNcp());
//@ts-expect-error private
jest.spyOn(uartAsh.serialPort, 'asyncFlush').mockImplementationOnce(jest.fn());
//@ts-expect-error private
uartAsh.serialPort.port.emitData(Buffer.from(RECD_RSTACK_BYTES));
const startResult = (await uartAsh.start());

Expand Down

0 comments on commit cbd68df

Please sign in to comment.