Skip to content

Commit

Permalink
fix(bluetooth): hard reset adapter on timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
mKeRix committed Jan 10, 2021
1 parent d54c53d commit 112441b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/integrations/bluetooth/bluetooth.service.spec.ts
Expand Up @@ -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 () => {
Expand Down
17 changes: 15 additions & 2 deletions src/integrations/bluetooth/bluetooth.service.ts
Expand Up @@ -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();
Expand Down Expand Up @@ -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<void> {
try {
Expand All @@ -243,6 +243,19 @@ export class BluetoothService {
}
}

/**
* Hard reset the hci (Bluetooth) device (down and up).
*/
protected async hardResetHciDevice(adapterId: number): Promise<void> {
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.
*
Expand Down

0 comments on commit 112441b

Please sign in to comment.