From 44b01509f7e0048e39f11a01e31188e41813d087 Mon Sep 17 00:00:00 2001 From: Scrin Date: Thu, 3 Mar 2022 17:16:36 +0200 Subject: [PATCH] Make the HCI device index configurable --- config.sample.yml | 3 +++ config/config.go | 3 ++- gateway/gateway.go | 12 ++++++++---- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/config.sample.yml b/config.sample.yml index af2da5f..b26f949 100644 --- a/config.sample.yml +++ b/config.sample.yml @@ -4,6 +4,9 @@ gw_mac: 00:00:00:00:00:00 # Whether to include all advertisements (true) or just those from RuuviTags (false) all_advertisements: false +# HCI device index for the bluetooth adapter to use. 0 by default, which should correspond to the hci0 device +hci_index: 0 + # Publish the data to MQTT in Ruuvi Gateway format mqtt: # Flag to enable or disable publishing the data to a MQTT server diff --git a/config/config.go b/config/config.go index 80a56b0..6e7a06e 100644 --- a/config/config.go +++ b/config/config.go @@ -38,6 +38,7 @@ type Logging struct { type Config struct { GwMac string `yaml:"gw_mac"` AllAdvertisements bool `yaml:"all_advertisements"` + HciIndex int `yaml:"hci_index"` MQTT *MQTT `yaml:"mqtt,omitempty"` HTTP *HTTP `yaml:"http,omitempty"` Logging Logging `yaml:"logging"` @@ -46,7 +47,7 @@ type Config struct { func ReadConfig(configFile string, strict bool) (Config, error) { if _, err := os.Stat(configFile); errors.Is(err, os.ErrNotExist) { - return Config{}, errors.New(fmt.Sprintf("No config found! Tried to open \"%s\"", configFile)) + return Config{}, fmt.Errorf("no config found! Tried to open \"%s\"", configFile) } f, err := os.Open(configFile) diff --git a/gateway/gateway.go b/gateway/gateway.go index dd6618d..b269c89 100644 --- a/gateway/gateway.go +++ b/gateway/gateway.go @@ -32,11 +32,15 @@ func Run(config config.Config) { log.Fatal("Neither MQTT nor HTTP is configured, check the config") } - device, err := dev.NewDevice("default", ble.OptScanParams(cmd.LESetScanParameters{ - LEScanType: 0, // passive scan - })) + device, err := dev.NewDevice("default", + ble.OptDeviceID(config.HciIndex), + ble.OptScanParams(cmd.LESetScanParameters{ + LEScanType: 0, // passive scan + })) if err != nil { - log.WithError(err).Fatal("Can't setup default bluetooth device") + log.WithError(err).WithFields(log.Fields{ + "hci_index": config.HciIndex, + }).Fatal("Can't setup bluetooth device") } ble.SetDefaultDevice(device) advHandler := func(adv ble.Advertisement) {