Skip to content

Commit

Permalink
all: change DefaultAdapter function to global
Browse files Browse the repository at this point in the history
All initialization can be done in the Enable call. This makes the API a
bit simpler and a bit more consistent between Nordic chips and other BLE
interfaces.
  • Loading branch information
aykevl committed Jun 1, 2020
1 parent d9d4255 commit 0474d7b
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 53 deletions.
32 changes: 13 additions & 19 deletions adapter_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,21 @@ type Adapter struct {
cancelScan func()
}

// DefaultAdapter returns the default adapter on the current system. On Linux,
// it will return the first adapter available.
func DefaultAdapter() (*Adapter, error) {
adapter, err := api.GetDefaultAdapter()
if err != nil {
return nil, err
}
adapterID, err := adapter.GetAdapterID()
if err != nil {
return nil, err
}
return &Adapter{
adapter: adapter,
id: adapterID,
}, nil
}
// DefaultAdapter is the default adapter on the system. On Linux, it is the
// first adapter available.
//
// Make sure to call Enable() before using it to initialize the adapter.
var DefaultAdapter = &Adapter{}

// Enable configures the BLE stack. It must be called before any
// Bluetooth-related calls (unless otherwise indicated).
//
// The Linux implementation is a no-op.
func (a *Adapter) Enable() error {
func (a *Adapter) Enable() (err error) {
if a.id == "" {
a.adapter, err = api.GetDefaultAdapter()
if err != nil {
return
}
a.id, err = a.adapter.GetAdapterID()
}
return nil
}
6 changes: 3 additions & 3 deletions adapter_nrf51.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ func handleEvent() {
gapEvent := eventBuf.evt.unionfield_gap_evt()
switch id {
case C.BLE_GAP_EVT_CONNECTED:
handler := defaultAdapter.handler
handler := DefaultAdapter.handler
if handler != nil {
handler(&ConnectEvent{GAPEvent: GAPEvent{Connection(gapEvent.conn_handle)}})
}
case C.BLE_GAP_EVT_DISCONNECTED:
handler := defaultAdapter.handler
handler := DefaultAdapter.handler
if handler != nil {
handler(&DisconnectEvent{GAPEvent: GAPEvent{Connection(gapEvent.conn_handle)}})
}
Expand All @@ -76,7 +76,7 @@ func handleEvent() {
writeEvent := gattsEvent.params.unionfield_write()
len := writeEvent.len - writeEvent.offset
data := (*[255]byte)(unsafe.Pointer(&writeEvent.data[0]))[:len:len]
handler := defaultAdapter.getCharWriteHandler(writeEvent.handle)
handler := DefaultAdapter.getCharWriteHandler(writeEvent.handle)
if handler != nil {
handler.callback(Connection(gattsEvent.conn_handle), int(writeEvent.offset), data)
}
Expand Down
6 changes: 3 additions & 3 deletions adapter_nrf528xx.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func handleEvent() {
gapEvent := eventBuf.evt.unionfield_gap_evt()
switch id {
case C.BLE_GAP_EVT_CONNECTED:
handler := defaultAdapter.handler
handler := DefaultAdapter.handler
if handler != nil {
handler(&ConnectEvent{GAPEvent: GAPEvent{Connection(gapEvent.conn_handle)}})
}
case C.BLE_GAP_EVT_DISCONNECTED:
handler := defaultAdapter.handler
handler := DefaultAdapter.handler
if handler != nil {
handler(&DisconnectEvent{GAPEvent: GAPEvent{Connection(gapEvent.conn_handle)}})
}
Expand Down Expand Up @@ -99,7 +99,7 @@ func handleEvent() {
writeEvent := gattsEvent.params.unionfield_write()
len := writeEvent.len - writeEvent.offset
data := (*[255]byte)(unsafe.Pointer(&writeEvent.data[0]))[:len:len]
handler := defaultAdapter.getCharWriteHandler(writeEvent.handle)
handler := DefaultAdapter.getCharWriteHandler(writeEvent.handle)
if handler != nil {
handler.callback(Connection(gattsEvent.conn_handle), int(writeEvent.offset), data)
}
Expand Down
14 changes: 5 additions & 9 deletions adapter_sd.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,11 @@ type Adapter struct {
charWriteHandlers []charWriteHandler
}

// defaultAdapter is an adapter to the default Bluetooth stack on a given
// target.
var defaultAdapter = Adapter{isDefault: true}

// DefaultAdapter returns the default adapter on the current system. On Nordic
// chips, it will return the SoftDevice interface.
func DefaultAdapter() (*Adapter, error) {
return &defaultAdapter, nil
}
// DefaultAdapter is the default adapter on the current system. On Nordic chips,
// it will return the SoftDevice interface.
//
// Make sure to call Enable() before using it to initialize the adapter.
var DefaultAdapter = Adapter{isDefault: true}

// Enable configures the BLE stack. It must be called before any
// Bluetooth-related calls (unless otherwise indicated).
Expand Down
10 changes: 4 additions & 6 deletions adapter_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ type Adapter struct {
watcher *winbt.IBluetoothLEAdvertisementWatcher
}

var defaultAdapter Adapter

// DefaultAdapter returns the default adapter on the current system.
func DefaultAdapter() (*Adapter, error) {
return &defaultAdapter, nil
}
// DefaultAdapter is the default adapter on the system.
//
// Make sure to call Enable() before using it to initialize the adapter.
var DefaultAdapter = Adapter{}

// Enable configures the BLE stack. It must be called before any
// Bluetooth-related calls (unless otherwise indicated).
Expand Down
6 changes: 3 additions & 3 deletions examples/advertisement/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
// flags + local name
var advPayload = []byte("\x02\x01\x06" + "\x07\x09TinyGo")

var adapter = bluetooth.DefaultAdapter

func main() {
adapter, err := bluetooth.DefaultAdapter()
must("get default adapter", err)
must("enable SD", adapter.Enable())
must("enable BLE stack", adapter.Enable())
adv := adapter.NewAdvertisement()
options := &bluetooth.AdvertiseOptions{
Interval: bluetooth.NewAdvertiseInterval(100),
Expand Down
6 changes: 3 additions & 3 deletions examples/heartrate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
// flags + local name
var advPayload = []byte("\x02\x01\x06" + "\x07\x09TinyGo")

var adapter = bluetooth.DefaultAdapter

// TODO: use atomics to access this value.
var heartRate uint8 = 75 // 75bpm

func main() {
println("starting")
adapter, err := bluetooth.DefaultAdapter()
must("get default adapter", err)
adapter.SetEventHandler(handleBluetoothEvents)
must("enable SD", adapter.Enable())
must("enable BLE stack", adapter.Enable())
adv := adapter.NewAdvertisement()
options := &bluetooth.AdvertiseOptions{
Interval: bluetooth.NewAdvertiseInterval(100),
Expand Down
6 changes: 3 additions & 3 deletions examples/ledcolor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
// flags + local name
var advPayload = []byte("\x02\x01\x06" + "\x0b\x09LED colors")

var adapter = bluetooth.DefaultAdapter

// TODO: use atomics to access this value.
var ledColor = [3]byte{0xff, 0x00, 0x00} // start out with red
var leds = [3]machine.Pin{machine.LED_RED, machine.LED_GREEN, machine.LED_BLUE}
Expand All @@ -22,10 +24,8 @@ var (

func main() {
println("starting")
adapter, err := bluetooth.DefaultAdapter()
must("get default adapter", err)
adapter.SetEventHandler(handleBluetoothEvents)
must("enable SD", adapter.Enable())
must("enable BLE stack", adapter.Enable())
adv := adapter.NewAdvertisement()
options := &bluetooth.AdvertiseOptions{
Interval: bluetooth.NewAdvertiseInterval(100),
Expand Down
8 changes: 4 additions & 4 deletions examples/scanner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import (
"github.com/tinygo-org/bluetooth"
)

var adapter = bluetooth.DefaultAdapter

func main() {
// Enable BLE interface.
adapter, err := bluetooth.DefaultAdapter()
must("get default adapter", err)
must("enable adapter", adapter.Enable())
must("enable BLE stack", adapter.Enable())

// Start scanning.
println("scanning...")
err = adapter.Scan(func(adapter *bluetooth.Adapter, device bluetooth.ScanResult) {
err := adapter.Scan(func(adapter *bluetooth.Adapter, device bluetooth.ScanResult) {
println("found device:", device.Address.String(), device.RSSI, device.LocalName())
})
must("start scan", err)
Expand Down

0 comments on commit 0474d7b

Please sign in to comment.