From 112441bc884f3f0b25d28927c67a22734129e7fb Mon Sep 17 00:00:00 2001 From: Heiko Rothe Date: Fri, 18 Dec 2020 12:34:51 +0100 Subject: [PATCH] fix(bluetooth): hard reset adapter on timeouts --- .../bluetooth/bluetooth.service.spec.ts | 6 ++++-- src/integrations/bluetooth/bluetooth.service.ts | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/integrations/bluetooth/bluetooth.service.spec.ts b/src/integrations/bluetooth/bluetooth.service.spec.ts index 9660c27..f4a6a7c 100644 --- a/src/integrations/bluetooth/bluetooth.service.spec.ts +++ b/src/integrations/bluetooth/bluetooth.service.spec.ts @@ -94,12 +94,14 @@ describe('BluetoothService', () => { expect(mockExec).toHaveBeenCalledWith('hciconfig hci1 reset'); }); - it('should reset the HCI device if the command execution times out', async () => { + it('should hard reset the HCI device if the command execution times out', async () => { mockExec.mockRejectedValue({ message: 'timed out' }); const result = await service.inquireClassicRssi(1, '08:05:90:ed:3b:60'); expect(result).toBeUndefined(); - expect(mockExec).toHaveBeenCalledWith('hciconfig hci1 reset'); + expect(mockExec).toHaveBeenCalledWith( + 'hciconfig hci1 down && hciconfig hci1 up' + ); }); it('should stop scanning on an adapter while performing an inquiry', async () => { diff --git a/src/integrations/bluetooth/bluetooth.service.ts b/src/integrations/bluetooth/bluetooth.service.ts index 0b1d819..8059805 100644 --- a/src/integrations/bluetooth/bluetooth.service.ts +++ b/src/integrations/bluetooth/bluetooth.service.ts @@ -180,7 +180,7 @@ export class BluetoothService { `Bluetooth adapter ${adapterId} seems stuck, resetting` ); this.healthIndicator.reportError(); - await this.resetHciDevice(adapterId); + await this.hardResetHciDevice(adapterId); } else { this.logger.error(`Inquiring RSSI via BT Classic failed: ${e.message}`); this.healthIndicator.reportError(); @@ -233,7 +233,7 @@ export class BluetoothService { } /** - * Reset the hci (Bluetooth) device used for inquiries. + * Reset the hci (Bluetooth) device. */ protected async resetHciDevice(adapterId: number): Promise { try { @@ -243,6 +243,19 @@ export class BluetoothService { } } + /** + * Hard reset the hci (Bluetooth) device (down and up). + */ + protected async hardResetHciDevice(adapterId: number): Promise { + try { + await execPromise( + `hciconfig hci${adapterId} down && hciconfig hci${adapterId} up` + ); + } catch (e) { + this.logger.error(e.message); + } + } + /** * Locks an adapter for an active inquiry. *