Skip to content

Commit 7fa3de8

Browse files
feat: addGATTCService and GATTCCharacteristic common interface (#432)
* feat: addGATTCService and GATTCCharacteristic common interface * cast nil pointers * Update gattc.go Co-authored-by: Ron Evans <ron@hybridgroup.com> --------- Co-authored-by: Ron Evans <ron@hybridgroup.com>
1 parent a668143 commit 7fa3de8

6 files changed

Lines changed: 79 additions & 1 deletion

File tree

gattc.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//go:build !baremetal || !(s110v8 || s113v7)
2+
3+
package bluetooth
4+
5+
// GATTCService is the common interface that all platform-specific
6+
// DeviceService types must implement.
7+
type GATTCService interface {
8+
// UUID returns the UUID for this DeviceService.
9+
UUID() UUID
10+
11+
// DiscoverCharacteristics discovers characteristics in this service.
12+
// Pass a list of characteristic UUIDs you are interested in to this
13+
// function. Either a list of all requested services is returned, or if
14+
// some services could not be discovered an error is returned.
15+
DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error)
16+
}
17+
18+
// GATTCCharacteristic is the common interface that all platform-specific
19+
// DeviceCharacteristic types must implement.
20+
type GATTCCharacteristic interface {
21+
// UUID returns the UUID for this DeviceCharacteristic.
22+
UUID() UUID
23+
24+
// Read reads the current characteristic value.
25+
Read(data []byte) (int, error)
26+
27+
// Write replaces the characteristic value with a new value.
28+
Write(p []byte) (n int, err error)
29+
30+
// WriteWithoutResponse replaces the characteristic value with a new
31+
// value. The call will return before all data has been written.
32+
WriteWithoutResponse(p []byte) (n int, err error)
33+
34+
// EnableNotifications enables notifications for this characteristic,
35+
// calling the provided callback with the new value when the
36+
// characteristic value is changed by the remote peripheral.
37+
EnableNotifications(callback func(buf []byte)) error
38+
39+
// GetMTU returns the MTU for this characteristic.
40+
GetMTU() (uint16, error)
41+
}

gattc_darwin.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import (
88
"github.com/tinygo-org/cbgo"
99
)
1010

11+
var errCannotSendWriteWithoutResponse = errors.New("bluetooth: cannot send write without response (buffer full)")
12+
1113
var (
12-
errCannotSendWriteWithoutResponse = errors.New("bluetooth: cannot send write without response (buffer full)")
14+
_ GATTCService = (*DeviceService)(nil)
15+
_ GATTCCharacteristic = (*DeviceCharacteristic)(nil)
1316
)
1417

1518
// DiscoverServices starts a service discovery procedure. Pass a list of service

gattc_hci.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ var (
1717
errCharacteristicNotFound = errors.New("bluetooth: characteristic not found")
1818
)
1919

20+
var (
21+
_ GATTCService = (*DeviceService)(nil)
22+
_ GATTCCharacteristic = (*DeviceCharacteristic)(nil)
23+
)
24+
2025
const (
2126
maxDefaultServicesToDiscover = 8
2227
maxDefaultCharacteristicsToDiscover = 16
@@ -236,6 +241,13 @@ func (s DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteri
236241
return characteristics, nil
237242
}
238243

244+
// Write replaces the characteristic value with a new value.
245+
//
246+
// Not yet implemented on HCI.
247+
func (c DeviceCharacteristic) Write(p []byte) (n int, err error) {
248+
return 0, errNotYetImplemented
249+
}
250+
239251
// WriteWithoutResponse replaces the characteristic value with a new value. The
240252
// call will return before all data has been written. A limited number of such
241253
// writes can be in flight at any given time. This call is also known as a

gattc_linux.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import (
1111
"github.com/godbus/dbus/v5"
1212
)
1313

14+
var (
15+
_ GATTCService = (*DeviceService)(nil)
16+
_ GATTCCharacteristic = (*DeviceCharacteristic)(nil)
17+
)
18+
1419
var (
1520
errDupNotif = errors.New("unclosed notifications")
1621
)

gattc_sd.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ const (
1919
maxDefaultCharacteristicsToDiscover = 8
2020
)
2121

22+
var (
23+
_ GATTCService = (*DeviceService)(nil)
24+
_ GATTCCharacteristic = (*DeviceCharacteristic)(nil)
25+
)
26+
2227
var (
2328
errAlreadyDiscovering = errors.New("bluetooth: already discovering a service or characteristic")
2429
errNotFound = errors.New("bluetooth: not found")
@@ -312,6 +317,13 @@ func (s DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteri
312317
return characteristics, nil
313318
}
314319

320+
// Write replaces the characteristic value with a new value.
321+
//
322+
// Not yet implemented on SoftDevice.
323+
func (c DeviceCharacteristic) Write(p []byte) (n int, err error) {
324+
return 0, errNotFound
325+
}
326+
315327
// WriteWithoutResponse replaces the characteristic value with a new value. The
316328
// call will return before all data has been written. A limited number of such
317329
// writes can be in flight at any given time. This call is also known as a

gattc_windows.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ import (
1515
"github.com/saltosystems/winrt-go/windows/storage/streams"
1616
)
1717

18+
var (
19+
_ GATTCService = (*DeviceService)(nil)
20+
_ GATTCCharacteristic = (*DeviceCharacteristic)(nil)
21+
)
22+
1823
var (
1924
errNoWrite = errors.New("bluetooth: write not supported")
2025
errNoWriteWithoutResponse = errors.New("bluetooth: write without response not supported")

0 commit comments

Comments
 (0)