Skip to content

Commit a341e8f

Browse files
TirelessDevdeadprogram
authored andcommitted
macos: fixed reentrant service discovery
1 parent 74e8f86 commit a341e8f

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

gap_darwin.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ type Device struct {
9393
servicesChan chan error
9494
charsChan chan error
9595

96-
services map[UUID]*DeviceService
97-
characteristics map[UUID]*DeviceCharacteristic
96+
services map[UUID]*DeviceService
9897
}
9998

10099
// Connect starts a connection attempt to the given peripheral device address.
@@ -193,13 +192,17 @@ func (pd *peripheralDelegate) DidDiscoverCharacteristics(prph cbgo.Peripheral, s
193192
// or receives a value for a read request.
194193
func (pd *peripheralDelegate) DidUpdateValueForCharacteristic(prph cbgo.Peripheral, chr cbgo.Characteristic, err error) {
195194
uuid, _ := ParseUUID(chr.UUID().String())
196-
if char, ok := pd.d.characteristics[uuid]; ok {
197-
if err == nil && char.callback != nil {
198-
go char.callback(chr.Value())
199-
}
195+
svcuuid, _ := ParseUUID(chr.Service().UUID().String())
196+
197+
if svc, ok := pd.d.services[svcuuid]; ok {
198+
if char, ok := svc.characteristics[uuid]; ok {
199+
if err == nil && char.callback != nil {
200+
go char.callback(chr.Value())
201+
}
200202

201-
if char.readChan != nil {
202-
char.readChan <- err
203+
if char.readChan != nil {
204+
char.readChan <- err
205+
}
203206
}
204207
}
205208
}

gattc_darwin.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) {
4242
}
4343

4444
svc := DeviceService{
45-
uuidWrapper: dsvcuuid,
46-
device: d,
47-
service: dsvc,
45+
deviceService: &deviceService{
46+
uuidWrapper: dsvcuuid,
47+
device: d,
48+
service: dsvc,
49+
},
4850
}
4951
svcs = append(svcs, svc)
5052
d.services[svc.uuidWrapper] = &svc
@@ -61,11 +63,16 @@ type uuidWrapper = UUID
6163

6264
// DeviceService is a BLE service on a connected peripheral device.
6365
type DeviceService struct {
66+
*deviceService // embdedded as pointer to enable returning by []value in DiscoverServices
67+
}
68+
69+
type deviceService struct {
6470
uuidWrapper
6571

6672
device *Device
6773

68-
service cbgo.Service
74+
service cbgo.Service
75+
characteristics map[UUID]*DeviceCharacteristic
6976
}
7077

7178
// UUID returns the UUID for this DeviceService.
@@ -88,7 +95,7 @@ func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacter
8895
s.device.prph.DiscoverCharacteristics(cbuuids, s.service)
8996

9097
// clear cache of characteristics
91-
s.device.characteristics = make(map[UUID]*DeviceCharacteristic)
98+
s.characteristics = make(map[UUID]*DeviceCharacteristic)
9299

93100
// wait on channel for characteristic discovery
94101
select {
@@ -143,7 +150,7 @@ func (s *DeviceService) makeCharacteristic(uuid UUID, dchar cbgo.Characteristic)
143150
characteristic: dchar,
144151
},
145152
}
146-
s.device.characteristics[char.uuidWrapper] = &char
153+
s.characteristics[char.uuidWrapper] = &char
147154
return char
148155
}
149156

0 commit comments

Comments
 (0)