From 09d55bd1994bf5fefdc4d03241484315c84c31bb Mon Sep 17 00:00:00 2001 From: Heiko Rothe Date: Sun, 3 Jan 2021 13:11:22 +0100 Subject: [PATCH] fix(bluetooth): disallow parallel resets --- src/integrations/bluetooth/bluetooth.service.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/integrations/bluetooth/bluetooth.service.ts b/src/integrations/bluetooth/bluetooth.service.ts index 71db3b2..103a8fa 100644 --- a/src/integrations/bluetooth/bluetooth.service.ts +++ b/src/integrations/bluetooth/bluetooth.service.ts @@ -15,7 +15,7 @@ const SCAN_NO_PERIPHERAL_TIMEOUT = 30 * 1000; const execPromise = util.promisify(exec); -type BluetoothAdapterState = 'inquiry' | 'scan' | 'inactive'; +type BluetoothAdapterState = 'inquiry' | 'scan' | 'inactive' | 'resetting'; type ExecOutput = { stdout: string; stderr: string }; class BluetoothAdapter { @@ -244,8 +244,17 @@ export class BluetoothService { * Reset the hci (Bluetooth) device. */ protected async resetHciDevice(adapterId: number): Promise { + if (this.adapters.getState(adapterId) === 'resetting') { + throw new Error('Adapter is already resetting'); + } + try { + this.adapters.setState(adapterId, 'resetting'); await execPromise(`hciconfig hci${adapterId} reset`); + this.adapters.setState( + adapterId, + this.lowEnergyAdapterId === adapterId ? 'scan' : 'inactive' + ); } catch (e) { this.logger.error(e.message); } @@ -255,10 +264,16 @@ export class BluetoothService { * Hard reset the hci (Bluetooth) device (down and up). */ protected async hardResetHciDevice(adapterId: number): Promise { + if (this.adapters.getState(adapterId) === 'resetting') { + throw new Error('Adapter is already resetting'); + } + try { + this.adapters.setState(adapterId, 'resetting'); await execPromise(`hciconfig hci${adapterId} down`); await sleep(1200); await execPromise(`hciconfig hci${adapterId} up`); + this.adapters.setState(adapterId, 'inactive'); } catch (e) { this.logger.error(e.message); }