Skip to content

Commit d737f9b

Browse files
authored
fix: Windows services ordering in DiscoverServices (#415)
* fix: Windows services ordering in DiscoverServices The services will now appear on the expected order * use makeService helper function
1 parent 30770f1 commit d737f9b

1 file changed

Lines changed: 31 additions & 11 deletions

File tree

gattc_windows.go

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ func (d Device) DiscoverServices(filterUUIDs []UUID) ([]DeviceService, error) {
7979
}
8080

8181
var services []DeviceService
82+
83+
if len(filterUUIDs) > 0 {
84+
// The caller wants to get a list of services in a specific
85+
// order.
86+
services = make([]DeviceService, len(filterUUIDs))
87+
}
88+
8289
for i := uint32(0); i < servicesSize; i++ {
8390
s, err := servicesVector.GetAt(i)
8491
if err != nil {
@@ -95,29 +102,26 @@ func (d Device) DiscoverServices(filterUUIDs []UUID) ([]DeviceService, error) {
95102

96103
// only include services that are included in the input filter
97104
if len(filterUUIDs) > 0 {
98-
found := false
99-
for _, uuid := range filterUUIDs {
105+
for j, uuid := range filterUUIDs {
100106
if serviceUuid.String() == uuid.String() {
101107
// One of the services we're looking for.
102-
found = true
108+
services[j] = makeService(serviceUuid, srv, d)
103109
break
104110
}
105111
}
106-
if !found {
107-
continue
108-
}
112+
} else {
113+
// The caller wants to get all services, in any order.
114+
services = append(services, makeService(serviceUuid, srv, d))
109115
}
110116

111117
go func() {
112118
<-d.ctx.Done()
113119
srv.Close()
114120
}()
121+
}
115122

116-
services = append(services, DeviceService{
117-
uuidWrapper: serviceUuid,
118-
service: srv,
119-
device: d,
120-
})
123+
if slices.Contains(services, (DeviceService{})) {
124+
return nil, errors.New("bluetooth: did not find all requested services")
121125
}
122126

123127
return services, nil
@@ -144,8 +148,24 @@ func winRTUuidToUuid(uuid syscall.GUID) UUID {
144148
// struct method of the same name.
145149
type uuidWrapper = UUID
146150

151+
// Small helper to create a DeviceService object.
152+
func makeService(serviceUuid uuidWrapper, srv *genericattributeprofile.GattDeviceService, d Device) DeviceService {
153+
svc := DeviceService{
154+
deviceService: &deviceService{
155+
uuidWrapper: serviceUuid,
156+
service: srv,
157+
device: d,
158+
},
159+
}
160+
return svc
161+
}
162+
147163
// DeviceService is a BLE service on a connected peripheral device.
148164
type DeviceService struct {
165+
*deviceService
166+
}
167+
168+
type deviceService struct {
149169
uuidWrapper
150170

151171
service *genericattributeprofile.GattDeviceService

0 commit comments

Comments
 (0)