Skip to content

Commit

Permalink
feat: make hci device to be used configurable
Browse files Browse the repository at this point in the history
Operation of BLE and BT Classic is now considerably easier, as separate
Bluetooth devices can now be set via the config.
  • Loading branch information
mKeRix committed Feb 28, 2020
1 parent 29c4406 commit d8f9d59
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 20 deletions.
11 changes: 6 additions & 5 deletions docs/integrations/bluetooth-classic.md
Expand Up @@ -4,7 +4,7 @@

::: warning

This integration cannot be used together with [Bluetooth Low Energy](./bluetooth-low-energy).
Using this together with [Bluetooth Low Energy](./bluetooth-low-energy) requires multiple Bluetooth adapters.

:::

Expand Down Expand Up @@ -47,10 +47,11 @@ You could use this to reduce the resources used by room-assistant when you are c

## Settings

| Name | Type | Default | Description |
| ----------- | ------ | ------- | ------------------------------------------------------------ |
| `addresses` | Array | | List of Bluetooth MAC addresses that should be tracked. You can usually find them in the device settings. |
| `minRssi` | Number | | Limits the RSSI at which a device is still reported if configured. Remember, the RSSI is the inverse of the sensor attribute distance, so for a cutoff at 10 you would configure -10. |
| Name | Type | Default | Description |
| ------------- | ------ | ------- | ------------------------------------------------------------ |
| `addresses` | Array | | List of Bluetooth MAC addresses that should be tracked. You can usually find them in the device settings. |
| `minRssi` | Number | | Limits the RSSI at which a device is still reported if configured. Remember, the RSSI is the inverse of the sensor attribute distance, so for a cutoff at 10 you would configure -10. |
| `hciDeviceId` | Number | 0 | ID of the Bluetooth device to use for the inquiries, e.g. `0` to use `hci0`. |

::: details Example Config
```yaml
Expand Down
23 changes: 12 additions & 11 deletions docs/integrations/bluetooth-low-energy.md
Expand Up @@ -4,7 +4,7 @@

::: warning

This integration cannot be used together with [Bluetooth Classic](./bluetooth-classic).
Using this together with [Bluetooth Classic](./bluetooth-classic) requires multiple Bluetooth adapters.

:::

Expand Down Expand Up @@ -43,17 +43,18 @@ If you are unsure what ID your device has you can start room-assistant with the

## Settings

| Name | Type | Default | Description |
| ---------------- | ------------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `whitelist` | Array | | A list of BLE tag IDs that should be tracked. Will in most cases either be the MAC address or the iBeacon UUID in kebap-case. |
| `whitelistRegex` | Boolean | `false` | Whether the whitelist should be evaluated as a list of [regular expressions](https://en.wikipedia.org/wiki/Regular_expression) or not. |
| `processIBeacon` | Boolean | `true` | Whether additional data from iBeacon devices should be taken into account or not. Affects tag IDs and distance estimation. |
| `onlyIBeacon` | Boolean | `false` | Whether only iBeacons should be considered when scanning for devices ot not. |
| Name | Type | Default | Description |
| ---------------- | ------------------------------- | -------- | ------------------------------------------------------------ |
| `whitelist` | Array | | A list of BLE tag IDs that should be tracked. Will in most cases either be the MAC address or the iBeacon UUID in kebap-case. |
| `whitelistRegex` | Boolean | `false` | Whether the whitelist should be evaluated as a list of [regular expressions](https://en.wikipedia.org/wiki/Regular_expression) or not. |
| `processIBeacon` | Boolean | `true` | Whether additional data from iBeacon devices should be taken into account or not. Affects tag IDs and distance estimation. |
| `onlyIBeacon` | Boolean | `false` | Whether only iBeacons should be considered when scanning for devices ot not. |
| `timeout` | Number | `5` | The time after which a recorded distance is considered outdated. This value should be higher than the advertisement frequency of your peripheral. |
| `maxDistance` | Number | | Limits the distance at which a received BLE advertisement is still reported if configured. Value is in meters. |
| `majorMask` | Number | `0xffff` | Filter out bits of the major ID to make dynamic tag IDs with encoded information consistent for filtering. |
| `minorMask` | Number | `0xffff` | Filter out bits of the minor ID to make dynamic tag IDs with encoded information consistent for filtering. |
| `tagOverrides` | [Tag Overrides](#tag-overrides) | | Allows you to override some properties of the tracked devices. |
| `maxDistance` | Number | | Limits the distance at which a received BLE advertisement is still reported if configured. Value is in meters. |
| `majorMask` | Number | `0xffff` | Filter out bits of the major ID to make dynamic tag IDs with encoded information consistent for filtering. |
| `minorMask` | Number | `0xffff` | Filter out bits of the minor ID to make dynamic tag IDs with encoded information consistent for filtering. |
| `tagOverrides` | [Tag Overrides](#tag-overrides) | | Allows you to override some properties of the tracked devices. |
| `hciDeviceId` | Number | 0 | ID of the Bluetooth device to use for the inquiries, e.g. `0` to use `hci0`. |

### Tag Overrides

Expand Down
@@ -1,4 +1,5 @@
export class BluetoothClassicConfig {
addresses: string[] = [];
minRssi?: number;
hciDeviceId = 0;
}
12 changes: 8 additions & 4 deletions src/integrations/bluetooth-classic/bluetooth-classic.service.ts
Expand Up @@ -183,7 +183,7 @@ export class BluetoothClassicService extends KalmanFilterable(Object, 1.4, 1)
this.logger.debug(`Querying for RSSI of ${address} using hcitool`);
try {
const output = await execPromise(
`hcitool cc "${address}" && hcitool rssi "${address}"`,
`hcitool -i hci${this.config.hciDeviceId} cc "${address}" && hcitool -i hci${this.config.hciDeviceId} rssi "${address}"`,
{
timeout: 5.5 * 1000,
killSignal: 'SIGKILL'
Expand All @@ -194,7 +194,9 @@ export class BluetoothClassicService extends KalmanFilterable(Object, 1.4, 1)
return matches?.length > 0 ? parseInt(matches[0], 10) : undefined;
} catch (e) {
if (e.signal === 'SIGKILL') {
this.logger.warn(`Query of ${address} took too long, resetting hci0`);
this.logger.warn(
`Query of ${address} took too long, resetting hci${this.config.hciDeviceId}`
);
this.resetHciDevice();
} else {
this.logger.debug(e.message);
Expand Down Expand Up @@ -223,7 +225,9 @@ export class BluetoothClassicService extends KalmanFilterable(Object, 1.4, 1)
*/
async inquireDeviceInfo(address: string): Promise<Device> {
try {
const output = await execPromise(`hcitool info "${address}"`);
const output = await execPromise(
`hcitool -i hci${this.config.hciDeviceId} info "${address}"`
);

const nameMatches = /Device Name: (.+)/g.exec(output.stdout);
const manufacturerMatches = /OUI Company: (.+) \(.+\)/g.exec(
Expand Down Expand Up @@ -271,7 +275,7 @@ export class BluetoothClassicService extends KalmanFilterable(Object, 1.4, 1)
*/
protected async resetHciDevice(): Promise<void> {
try {
await execPromise('hciconfig hci0 reset');
await execPromise(`hciconfig hci${this.config.hciDeviceId} reset`);
} catch (e) {
this.logger.error(e.message);
}
Expand Down
@@ -1,4 +1,5 @@
export class BluetoothLowEnergyConfig {
hciDeviceId = 0;
whitelist: string[] = [];
whitelistRegex = false;
processIBeacon = true;
Expand Down
@@ -1,3 +1,4 @@
import './env';
import { DynamicModule, Module } from '@nestjs/common';
import { BluetoothLowEnergyService } from './bluetooth-low-energy.service';
import { EntitiesModule } from '../../entities/entities.module';
Expand Down
3 changes: 3 additions & 0 deletions src/integrations/bluetooth-low-energy/env.ts
@@ -0,0 +1,3 @@
import c from 'config';

process.env.NOBLE_HCI_DEVICE_ID = c.get('bluetoothLowEnergy.hciDeviceId');

0 comments on commit d8f9d59

Please sign in to comment.