darwin: properly handle 16-bit UUIDs for service and characteristics in the unique format used by macOS

Signed-off-by: Ron Evans <ron@hybridgroup.com>
This commit is contained in:
Ron Evans 2021-01-17 01:58:49 +01:00
parent a355f254da
commit b4b125480a
4 changed files with 67 additions and 29 deletions

View File

@ -15,13 +15,7 @@ import (
// Passing a nil slice of UUIDs will return a complete list of
// services.
func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) {
cbuuids := []cbgo.UUID{}
for _, u := range uuids {
uuid, _ := cbgo.ParseUUID(u.String())
cbuuids = append(cbuuids, uuid)
}
d.prph.DiscoverServices(cbuuids)
d.prph.DiscoverServices([]cbgo.UUID{})
// clear cache of services
d.services = make(map[UUID]*DeviceService)
@ -31,9 +25,24 @@ func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) {
case <-d.servicesChan:
svcs := []DeviceService{}
for _, dsvc := range d.prph.Services() {
uuid, _ := ParseUUID(dsvc.UUID().String())
dsvcuuid, _ := ParseUUID(dsvc.UUID().String())
// add if in our original list
if len(uuids) > 0 {
found := false
for _, uuid := range uuids {
if dsvcuuid.String() == uuid.String() {
// one of the services we're looking for.
found = true
break
}
}
if !found {
continue
}
}
svc := DeviceService{
uuidWrapper: uuid,
uuidWrapper: dsvcuuid,
device: d,
service: dsvc,
}
@ -75,10 +84,6 @@ func (s *DeviceService) UUID() UUID {
// characteristics.
func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error) {
cbuuids := []cbgo.UUID{}
for _, u := range uuids {
uuid, _ := cbgo.ParseUUID(u.String())
cbuuids = append(cbuuids, uuid)
}
s.device.prph.DiscoverCharacteristics(cbuuids, s.service)
@ -90,10 +95,25 @@ func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacter
case <-s.device.charsChan:
chars := []DeviceCharacteristic{}
for _, dchar := range s.service.Characteristics() {
uuid, _ := ParseUUID(dchar.UUID().String())
dcuuid, _ := ParseUUID(dchar.UUID().String())
// add if in our original list
if len(uuids) > 0 {
found := false
for _, uuid := range uuids {
if dcuuid.String() == uuid.String() {
// one of the characteristics we're looking for.
found = true
break
}
}
if !found {
continue
}
}
char := DeviceCharacteristic{
deviceCharacteristic: &deviceCharacteristic{
uuidWrapper: uuid,
uuidWrapper: dcuuid,
service: s,
characteristic: dchar,
},

14
uuid.go
View File

@ -11,20 +11,6 @@ type UUID [4]uint32
var errInvalidUUID = errors.New("bluetooth: failed to parse UUID")
// New16BitUUID returns a new 128-bit UUID based on a 16-bit UUID.
//
// Note: only use registered UUIDs. See
// https://www.bluetooth.com/specifications/gatt/services/ for a list.
func New16BitUUID(shortUUID uint16) UUID {
// https://stackoverflow.com/questions/36212020/how-can-i-convert-a-bluetooth-16-bit-service-uuid-into-a-128-bit-uuid
var uuid UUID
uuid[0] = 0x5F9B34FB
uuid[1] = 0x80000080
uuid[2] = 0x00001000
uuid[3] = uint32(shortUUID)
return uuid
}
// NewUUID returns a new UUID based on the 128-bit (or 16-byte) input.
func NewUUID(uuid [16]byte) UUID {
u := UUID{}

17
uuid16.go Normal file
View File

@ -0,0 +1,17 @@
// +build !darwin
package bluetooth
// New16BitUUID returns a new 128-bit UUID based on a 16-bit UUID.
//
// Note: only use registered UUIDs. See
// https://www.bluetooth.com/specifications/gatt/services/ for a list.
func New16BitUUID(shortUUID uint16) UUID {
// https://stackoverflow.com/questions/36212020/how-can-i-convert-a-bluetooth-16-bit-service-uuid-into-a-128-bit-uuid
var uuid UUID
uuid[0] = 0x5F9B34FB
uuid[1] = 0x80000080
uuid[2] = 0x00001000
uuid[3] = uint32(shortUUID)
return uuid
}

15
uuid16_darwin.go Normal file
View File

@ -0,0 +1,15 @@
package bluetooth
// New16BitUUID returns a new 128-bit UUID based on a 16-bit UUID.
//
// Note: only use registered UUIDs. See
// https://www.bluetooth.com/specifications/gatt/services/ for a list.
func New16BitUUID(shortUUID uint16) UUID {
// mac OS uses a unique format for UUID.
var uuid UUID
uuid[0] = 0x00000000
uuid[1] = 0x00000000
uuid[2] = 0x00000000
uuid[3] = uint32(shortUUID) << 16
return uuid
}