Skip to content

Commit a668143

Browse files
authored
feat: add BLEAdapter common interface (#431)
* feat: add BLEAdapter common interface * add errNotSupported * cast nil instance * change gap s117 errors to errNotSupported
1 parent 468b61f commit a668143

13 files changed

Lines changed: 89 additions & 5 deletions

adapter.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
package bluetooth
22

3-
// SetConnectHandler sets a handler function to be called whenever the adaptor connects
3+
// BLEAdapter is the shared interface that all platform-specific Adapter types must implement.
4+
type BLEAdapter interface {
5+
Connect(address Address, params ConnectionParams) (Device, error)
6+
Enable() error
7+
Scan(callback func(*Adapter, ScanResult)) (err error)
8+
SetConnectHandler(c func(device Device, connected bool))
9+
StopScan() error
10+
}
11+
12+
// SetConnectHandler sets a handler function to be called whenever the adapter connects
413
// or disconnects. You must call this before you call adapter.Connect() for centrals
514
// or advertisement.Start() for peripherals in order for it to work.
615
func (a *Adapter) SetConnectHandler(c func(device Device, connected bool)) {

adapter_cyw43439.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212

1313
const maxConnections = 1
1414

15+
var _ BLEAdapter = (*Adapter)(nil)
16+
1517
// Adapter represents a SPI connection to the HCI controller on an attached CYW4349 module.
1618
type Adapter struct {
1719
hciAdapter

adapter_darwin.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/tinygo-org/cbgo"
99
)
1010

11+
var _ BLEAdapter = (*Adapter)(nil)
12+
1113
// Adapter is a connection to BLE devices.
1214
type Adapter struct {
1315
cmd *centralManagerDelegate

adapter_hci_uart.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88

99
const maxConnections = 1
1010

11+
var _ BLEAdapter = (*Adapter)(nil)
12+
1113
// Adapter represents a "plain" UART connection to the HCI controller.
1214
type Adapter struct {
1315
hciAdapter

adapter_linux.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414

1515
const defaultAdapter = "hci0"
1616

17+
var _ BLEAdapter = (*Adapter)(nil)
18+
1719
type Adapter struct {
1820
id string
1921
scanCancelChan chan struct{}

adapter_ninafw.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99

1010
const maxConnections = 1
1111

12+
var _ BLEAdapter = (*Adapter)(nil)
13+
1214
// Adapter represents the HCI connection to the NINA fw using the hardware UART.
1315
type Adapter struct {
1416
hciAdapter

adapter_nrf51.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,24 @@ func makeMACAddress(addr C.ble_gap_addr_t) MACAddress {
127127
isRandom: addr.addr_type != 0,
128128
}
129129
}
130+
131+
// Connect starts a connection attempt to the given peripheral device address.
132+
//
133+
// Not yet implemented on the nrf51.
134+
func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, error) {
135+
return Device{}, errNotYetImplmented
136+
}
137+
138+
// Scan starts a BLE scan. It is stopped by a call to StopScan.
139+
//
140+
// Not yet implemented on the nrf51.
141+
func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) (err error) {
142+
return errNotYetImplmented
143+
}
144+
145+
// StopScan stops any in-progress scan.
146+
//
147+
// Not yet implemented on the nrf51.
148+
func (a *Adapter) StopScan() error {
149+
return errNotYetImplmented
150+
}

adapter_s113v7.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,24 @@ package bluetooth
1111
nrf_nvic_state_t nrf_nvic_state = {0};
1212
*/
1313
import "C"
14+
15+
// Connect starts a connection attempt to the given peripheral device address.
16+
//
17+
// s113v7 is a peripheral-only device, so this is not supported.
18+
func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, error) {
19+
return Device{}, errNotSupported
20+
}
21+
22+
// Scan starts a BLE scan. It is stopped by a call to StopScan.
23+
//
24+
// s113v7 is a peripheral-only device, so this is not supported.
25+
func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) (err error) {
26+
return errNotSupported
27+
}
28+
29+
// StopScan stops any in-progress scan.
30+
//
31+
// s113v7 is a peripheral-only device, so this is not supported.
32+
func (a *Adapter) StopScan() error {
33+
return errNotSupported
34+
}

adapter_sd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ func init() {
4141
secModeOpen.set_bitfield_lv(1)
4242
}
4343

44+
var _ BLEAdapter = (*Adapter)(nil)
45+
4446
// Adapter is a dummy adapter: it represents the connection to the (only)
4547
// SoftDevice on the chip.
4648
type Adapter struct {

adapter_windows.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"github.com/saltosystems/winrt-go/windows/foundation"
1111
)
1212

13+
var _ BLEAdapter = (*Adapter)(nil)
14+
1315
type Adapter struct {
1416
watcher *advertisement.BluetoothLEAdvertisementWatcher
1517

0 commit comments

Comments
 (0)