Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions gattc_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ func (d Device) DiscoverServices(filterUUIDs []UUID) ([]DeviceService, error) {
}

var services []DeviceService

if len(filterUUIDs) > 0 {
// The caller wants to get a list of services in a specific
// order.
services = make([]DeviceService, len(filterUUIDs))
}

for i := uint32(0); i < servicesSize; i++ {
s, err := servicesVector.GetAt(i)
if err != nil {
Expand All @@ -95,29 +102,26 @@ func (d Device) DiscoverServices(filterUUIDs []UUID) ([]DeviceService, error) {

// only include services that are included in the input filter
if len(filterUUIDs) > 0 {
found := false
for _, uuid := range filterUUIDs {
for j, uuid := range filterUUIDs {
if serviceUuid.String() == uuid.String() {
// One of the services we're looking for.
found = true
services[j] = makeService(serviceUuid, srv, d)
break
}
}
if !found {
continue
}
} else {
// The caller wants to get all services, in any order.
services = append(services, makeService(serviceUuid, srv, d))
}

go func() {
<-d.ctx.Done()
srv.Close()
}()
}

services = append(services, DeviceService{
uuidWrapper: serviceUuid,
service: srv,
device: d,
})
if slices.Contains(services, (DeviceService{})) {
return nil, errors.New("bluetooth: did not find all requested services")
}

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

// Small helper to create a DeviceService object.
func makeService(serviceUuid uuidWrapper, srv *genericattributeprofile.GattDeviceService, d Device) DeviceService {
svc := DeviceService{
deviceService: &deviceService{
uuidWrapper: serviceUuid,
service: srv,
device: d,
},
}
return svc
}

// DeviceService is a BLE service on a connected peripheral device.
type DeviceService struct {
*deviceService
}

type deviceService struct {
uuidWrapper

service *genericattributeprofile.GattDeviceService
Expand Down
Loading