Repository navigation
Expand file tree
/
Copy pathgap_windows.go
More file actions
837 lines (725 loc) · 25.4 KB
/
Copy pathgap_windows.go
File metadata and controls
837 lines (725 loc) · 25.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
package bluetooth
import (
"context"
"errors"
"fmt"
"slices"
"sync"
"sync/atomic"
"syscall"
"time"
"unsafe"
"github.com/go-ole/go-ole"
"github.com/saltosystems/winrt-go"
"github.com/saltosystems/winrt-go/windows/devices/bluetooth"
"github.com/saltosystems/winrt-go/windows/devices/bluetooth/advertisement"
"github.com/saltosystems/winrt-go/windows/devices/bluetooth/genericattributeprofile"
"github.com/saltosystems/winrt-go/windows/foundation"
"github.com/saltosystems/winrt-go/windows/foundation/collections"
"github.com/saltosystems/winrt-go/windows/storage/streams"
)
const defaultConnectionTimeout time.Duration = 10 * time.Second
// Address contains a Bluetooth MAC address.
type Address struct {
MACAddress
}
type Advertisement struct {
advertisement *advertisement.BluetoothLEAdvertisement
publisher *advertisement.BluetoothLEAdvertisementPublisher
}
// DefaultAdvertisement returns the default advertisement instance but does not
// configure it.
func (a *Adapter) DefaultAdvertisement() *Advertisement {
if a.defaultAdvertisement == nil {
a.defaultAdvertisement = &Advertisement{}
}
return a.defaultAdvertisement
}
// Configure this advertisement.
// on Windows we're only able to set "Manufacturer Data" for advertisements.
// https://learn.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.advertisement.bluetoothleadvertisementpublisher?view=winrt-22621#remarks
// following this c# source for this implementation: https://github.com/microsoft/Windows-universal-samples/blob/main/Samples/BluetoothAdvertisement/cs/Scenario2_Publisher.xaml.cs
// adding service data / localname leads to errors when starting the advertisement.
func (a *Advertisement) Configure(options AdvertisementOptions) error {
// we can only advertise manufacturer / company data on windows, so no need to continue if we have none
if len(options.ManufacturerData) == 0 {
return nil
}
if a.publisher != nil {
a.publisher.Release()
}
if a.advertisement != nil {
a.advertisement.Release()
}
pub, err := advertisement.NewBluetoothLEAdvertisementPublisher()
if err != nil {
return err
}
a.publisher = pub
ad, err := a.publisher.GetAdvertisement()
if err != nil {
return err
}
a.advertisement = ad
vec, err := ad.GetManufacturerData()
if err != nil {
return err
}
for _, optManData := range options.ManufacturerData {
writer, err := streams.NewDataWriter()
if err != nil {
return err
}
defer writer.Release()
err = writer.WriteBytes(uint32(len(optManData.Data)), optManData.Data)
if err != nil {
return err
}
buf, err := writer.DetachBuffer()
if err != nil {
return err
}
manData, err := advertisement.BluetoothLEManufacturerDataCreate(optManData.CompanyID, buf)
if err != nil {
return err
}
if err = vec.Append(unsafe.Pointer(&manData.IUnknown.RawVTable)); err != nil {
return err
}
}
return nil
}
// Start advertisement. May only be called after it has been configured.
func (a *Advertisement) Start() error {
// publisher will be present if we actually have manufacturer data to advertise.
if a.publisher != nil {
return a.publisher.Start()
}
return nil
}
// Stop advertisement. May only be called after it has been started.
func (a *Advertisement) Stop() error {
if a.publisher != nil {
return a.publisher.Stop()
}
return nil
}
// Scan starts a BLE scan. It is stopped by a call to StopScan. A common pattern
// is to cancel the scan when a particular device has been found.
func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) (err error) {
if a.watcher != nil {
// Cannot scan more than once: which one should ScanStop()
// stop?
return errScanning
}
a.watcher, err = advertisement.NewBluetoothLEAdvertisementWatcher()
if err != nil {
return
}
defer func() {
_ = a.watcher.Release()
a.watcher = nil
}()
// Set scanning mode to active so we receive scan responses
// from devices in advertising mode
err = a.watcher.SetScanningMode(advertisement.BluetoothLEScanningModeActive)
if err != nil {
return
}
// Listen for incoming BLE advertisement packets.
// We need a TypedEventHandler<TSender, TResult> to listen to events, but since this is a parameterized delegate
// its GUID depends on the classes used as sender and result, so we need to compute it:
// TypedEventHandler<BluetoothLEAdvertisementWatcher, BluetoothLEAdvertisementReceivedEventArgs>
eventReceivedGuid := winrt.ParameterizedInstanceGUID(
foundation.GUIDTypedEventHandler,
advertisement.SignatureBluetoothLEAdvertisementWatcher,
advertisement.SignatureBluetoothLEAdvertisementReceivedEventArgs,
)
var seen scanCache
handler := foundation.NewTypedEventHandler(ole.NewGUID(eventReceivedGuid), func(instance *foundation.TypedEventHandler, sender, arg unsafe.Pointer) {
args := (*advertisement.BluetoothLEAdvertisementReceivedEventArgs)(arg)
result := getScanResultFromArgs(args)
// WinRT reports a scan response as its own event, so fill in what a packet lacks from earlier ones.
// See https://learn.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.advertisement.bluetoothleadvertisementtype
if p, ok := result.AdvertisementPayload.(*advertisementFields); ok {
p.AdvertisementFields = seen.merge(result.Address, p.AdvertisementFields)
}
callback(a, result)
})
defer handler.Release()
token, err := a.watcher.AddReceived(handler)
if err != nil {
return
}
defer a.watcher.RemoveReceived(token)
// Wait for when advertisement has stopped by a call to StopScan().
// Advertisement doesn't seem to stop right away, there is an
// intermediate Stopping state.
stoppingChan := make(chan error)
// TypedEventHandler<BluetoothLEAdvertisementWatcher, BluetoothLEAdvertisementWatcherStoppedEventArgs>
eventStoppedGuid := winrt.ParameterizedInstanceGUID(
foundation.GUIDTypedEventHandler,
advertisement.SignatureBluetoothLEAdvertisementWatcher,
advertisement.SignatureBluetoothLEAdvertisementWatcherStoppedEventArgs,
)
stoppedHandler := foundation.NewTypedEventHandler(ole.NewGUID(eventStoppedGuid), func(_ *foundation.TypedEventHandler, _, arg unsafe.Pointer) {
args := (*advertisement.BluetoothLEAdvertisementWatcherStoppedEventArgs)(arg)
errCode, err := args.GetError()
if err != nil {
// Got an error while getting the error value, that shouldn't
// happen.
stoppingChan <- fmt.Errorf("failed to get stopping error value: %w", err)
} else if errCode != bluetooth.BluetoothErrorSuccess {
// Could not stop the scan? I'm not sure when this would actually
// happen.
stoppingChan <- fmt.Errorf("failed to stop scanning (error code %d)", errCode)
}
close(stoppingChan)
})
defer stoppedHandler.Release()
token, err = a.watcher.AddStopped(stoppedHandler)
if err != nil {
return
}
defer a.watcher.RemoveStopped(token)
err = a.watcher.Start()
if err != nil {
return err
}
// Wait until advertisement has stopped, and finish.
return <-stoppingChan
}
func getScanResultFromArgs(args *advertisement.BluetoothLEAdvertisementReceivedEventArgs) ScanResult {
// parse bluetooth address
addr, _ := args.GetBluetoothAddress()
adr := Address{}
for i := range adr.MAC {
adr.MAC[i] = byte(addr)
addr >>= 8
}
sigStrength, _ := args.GetRawSignalStrengthInDBm()
result := ScanResult{
RSSI: sigStrength,
Address: adr,
}
winAdv, err := args.GetAdvertisement()
if err != nil {
return result
}
defer winAdv.Release()
var manufacturerData []ManufacturerDataElement
var serviceUUIDs []UUID
// Extract manufacturer data
manDataVector, _ := winAdv.GetManufacturerData()
if manDataVector != nil {
defer manDataVector.Release()
size, _ := manDataVector.GetSize()
for i := uint32(0); i < size; i++ {
element, _ := manDataVector.GetAt(i)
manData := (*advertisement.BluetoothLEManufacturerData)(element)
companyID, _ := manData.GetCompanyId()
buffer, _ := manData.GetData()
if buffer != nil {
manufacturerData = append(manufacturerData, ManufacturerDataElement{
CompanyID: companyID,
Data: bufferToSlice(buffer),
})
buffer.Release()
}
manData.Release()
}
}
// Extract service UUIDs
serviceUuidsVector, _ := winAdv.GetServiceUuids()
if serviceUuidsVector != nil {
defer serviceUuidsVector.Release()
size, _ := serviceUuidsVector.GetSize()
for i := uint32(0); i < size; i++ {
serviceGUID, err := guidVectorAt(serviceUuidsVector, i)
if err != nil {
continue
}
serviceUUIDs = append(serviceUUIDs, GUIDToUUID(serviceGUID))
}
}
// Note: the IsRandom bit is never set.
localName, _ := winAdv.GetLocalName()
result.AdvertisementPayload = &advertisementFields{
AdvertisementFields{
LocalName: localName,
ServiceUUIDs: serviceUUIDs,
ManufacturerData: manufacturerData,
},
}
return result
}
// scanCacheSize limits how many devices a scan keeps fields for.
// When it is full, the device seen longest ago is dropped.
const scanCacheSize = 256
// scanCache keeps the advertisement fields each device has sent during a scan.
type scanCache struct {
mu sync.Mutex
tick uint64
devices map[Address]*scanCacheEntry
}
type scanCacheEntry struct {
fields AdvertisementFields
lastSeen uint64
}
// merge fills in what fields lack from the fields addr sent before, keeps the
// result, and returns a copy of it that shares no memory with the cache.
func (c *scanCache) merge(addr Address, fields AdvertisementFields) AdvertisementFields {
c.mu.Lock()
defer c.mu.Unlock()
c.tick++
e, ok := c.devices[addr]
if !ok {
if c.devices == nil {
c.devices = make(map[Address]*scanCacheEntry)
}
if len(c.devices) >= scanCacheSize {
c.dropOldest()
}
e = &scanCacheEntry{}
c.devices[addr] = e
}
e.fields = mergeAdvertisementFields(e.fields, fields)
e.lastSeen = c.tick
return cloneAdvertisementFields(e.fields)
}
func (c *scanCache) dropOldest() {
var oldest Address
var oldestSeen uint64
for addr, e := range c.devices {
if oldestSeen == 0 || e.lastSeen < oldestSeen {
oldest, oldestSeen = addr, e.lastSeen
}
}
delete(c.devices, oldest)
}
// mergeAdvertisementFields merges cur into last, with lists merged by key like BlueZ.
// See https://github.com/bluez/bluez/blob/fa6ba55020dee3c3183041c854c347b4582448be/src/device.c#L2497-L2567
func mergeAdvertisementFields(last, cur AdvertisementFields) AdvertisementFields {
if cur.LocalName == "" {
cur.LocalName = last.LocalName
}
cur.ServiceUUIDs = mergeByKey(last.ServiceUUIDs, cur.ServiceUUIDs, func(u UUID) UUID { return u })
cur.ManufacturerData = mergeByKey(last.ManufacturerData, cur.ManufacturerData, func(m ManufacturerDataElement) uint16 { return m.CompanyID })
cur.ServiceData = mergeByKey(last.ServiceData, cur.ServiceData, func(s ServiceDataElement) UUID { return s.UUID })
return cur
}
// mergeByKey returns a copy of last where each element of cur replaces the one with
// the same key in place, or is appended, so the order stays stable.
func mergeByKey[E any, K comparable](last, cur []E, key func(E) K) []E {
merged := slices.Clone(last)
for _, c := range cur {
if i := slices.IndexFunc(merged, func(m E) bool { return key(m) == key(c) }); i >= 0 {
merged[i] = c
} else {
merged = append(merged, c)
}
}
return merged
}
func cloneAdvertisementFields(f AdvertisementFields) AdvertisementFields {
f.ServiceUUIDs = slices.Clone(f.ServiceUUIDs)
f.ManufacturerData = slices.Clone(f.ManufacturerData)
for i := range f.ManufacturerData {
f.ManufacturerData[i].Data = slices.Clone(f.ManufacturerData[i].Data)
}
f.ServiceData = slices.Clone(f.ServiceData)
for i := range f.ServiceData {
f.ServiceData[i].Data = slices.Clone(f.ServiceData[i].Data)
}
return f
}
// IVector.GetAt has room for a pointer, but GetAt on an IVector<GUID> writes a 16 byte GUID.
// See https://learn.microsoft.com/en-us/uwp/api/windows.foundation.collections.ivector-1.getat
func guidVectorAt(v *collections.IVector, i uint32) (syscall.GUID, error) {
var guid syscall.GUID
hr, _, _ := syscall.SyscallN(
v.VTable().GetAt,
uintptr(unsafe.Pointer(v)),
uintptr(i),
uintptr(unsafe.Pointer(&guid)),
)
if hr != 0 {
return syscall.GUID{}, ole.NewError(hr)
}
return guid, nil
}
func GUIDToUUID(guid syscall.GUID) UUID {
return NewUUID([16]byte{
byte(guid.Data1 >> 24),
byte(guid.Data1 >> 16),
byte(guid.Data1 >> 8),
byte(guid.Data1),
byte(guid.Data2 >> 8),
byte(guid.Data2),
byte(guid.Data3 >> 8),
byte(guid.Data3),
guid.Data4[0], guid.Data4[1],
guid.Data4[2], guid.Data4[3],
guid.Data4[4], guid.Data4[5],
guid.Data4[6], guid.Data4[7],
})
}
func bufferToSlice(buffer *streams.IBuffer) []byte {
dataReader, _ := streams.DataReaderFromBuffer(buffer)
defer dataReader.Release()
bufferSize, _ := buffer.GetLength()
if bufferSize == 0 {
return nil
}
data, _ := dataReader.ReadBytes(bufferSize)
return data
}
// StopScan stops any in-progress scan. It can be called from within a Scan
// callback to stop the current scan. If no scan is in progress, an error will
// be returned.
func (a *Adapter) StopScan() error {
if a.watcher == nil {
return errNotScanning
}
return a.watcher.Stop()
}
var _ GAPDevice = Device{}
// Device is a connection to a remote peripheral.
type Device struct {
ctx context.Context
cancel context.CancelFunc
Address Address // the MAC address of the device
device *bluetooth.BluetoothLEDevice
session *genericattributeprofile.GattSession
connectionStatusListenerToken foundation.EventRegistrationToken
connectionStatusListener *foundation.TypedEventHandler
connParams *connectionParamsState
services *openServices
link *linkState
}
var errDeviceNotConnected = errors.New("bluetooth: device is not connected")
// connectionParamsState keeps the open connection parameters request.
// Windows applies the request only while the object is open:
// https://learn.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.bluetoothledevice.requestpreferredconnectionparameters
type connectionParamsState struct {
mu sync.Mutex
request *bluetooth.BluetoothLEPreferredConnectionParametersRequest
closed bool
}
// close closes the open request, which restores the system defaults. A request
// after this gets an error.
func (s *connectionParamsState) close() {
s.mu.Lock()
defer s.mu.Unlock()
s.closed = true
if s.request != nil {
discardConnectionParamsRequest(s.request)
s.request = nil
}
}
// openServices are the services DiscoverServices returned, which Disconnect closes.
type openServices struct {
mu sync.Mutex
list []*genericattributeprofile.GattDeviceService
}
func (s *openServices) add(srv *genericattributeprofile.GattDeviceService) {
s.mu.Lock()
if !slices.Contains(s.list, srv) {
s.list = append(s.list, srv)
}
s.mu.Unlock()
}
func (s *openServices) close() {
s.mu.Lock()
list := s.list
s.list = nil
s.mu.Unlock()
for _, srv := range list {
_ = srv.Close()
}
}
// linkState calls the connect handler once for each change of the link.
type linkState struct {
connected atomic.Bool
handler func(device Device, state ConnectionState)
}
func (s *linkState) report(d Device, connected bool) {
if s.connected.CompareAndSwap(!connected, connected) && s.handler != nil {
s.handler(d, ConnectionState{Connected: connected})
}
}
// Connect starts a connection attempt to the given peripheral device address.
//
// On Linux and Windows, the IsRandom part of the address is ignored.
//
// Windows requests params.Priority after the connection, and ignores an error.
// Call RequestConnectionParams to see the error.
func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, error) {
var winAddr uint64
for i := range address.MAC {
winAddr += uint64(address.MAC[i]) << (8 * i)
}
// IAsyncOperation<BluetoothLEDevice>
bleDeviceOp, err := bluetooth.BluetoothLEDeviceFromBluetoothAddressAsync(winAddr)
if err != nil {
return Device{}, err
}
defer bleDeviceOp.Release()
// We need to pass the signature of the parameter returned by the async operation:
// IAsyncOperation<BluetoothLEDevice>
if err := awaitAsyncOperation(bleDeviceOp, bluetooth.SignatureBluetoothLEDevice); err != nil {
return Device{}, fmt.Errorf("error connecting to device: %w", err)
}
res, err := bleDeviceOp.GetResults()
if err != nil {
return Device{}, err
}
// The returned BluetoothLEDevice is set to null if FromBluetoothAddressAsync can't find the device identified by bluetoothAddress
if uintptr(res) == 0x0 {
return Device{}, fmt.Errorf("device with the given address was not found")
}
bleDevice := (*bluetooth.BluetoothLEDevice)(res)
// Creating a BluetoothLEDevice object by calling this method alone doesn't (necessarily) initiate a connection.
// To initiate a connection, we need to set GattSession.MaintainConnection to true.
dID, err := bleDevice.GetBluetoothDeviceId()
if err != nil {
return Device{}, err
}
defer dID.Release()
// Windows does not support explicitly connecting to a device.
// Instead it has the concept of a GATT session that is owned
// by the calling program.
gattSessionOp, err := genericattributeprofile.GattSessionFromDeviceIdAsync(dID) // IAsyncOperation<GattSession>
if err != nil {
return Device{}, err
}
defer gattSessionOp.Release()
if err := awaitAsyncOperation(gattSessionOp, genericattributeprofile.SignatureGattSession); err != nil {
return Device{}, fmt.Errorf("error getting gatt session: %w", err)
}
gattRes, err := gattSessionOp.GetResults()
if err != nil {
return Device{}, err
}
newSession := (*genericattributeprofile.GattSession)(gattRes)
// This keeps the device connected until we set maintain_connection = False.
if err := newSession.SetMaintainConnection(true); err != nil {
return Device{}, err
}
ctx, cancel := context.WithCancel(context.Background())
device := Device{
ctx: ctx,
cancel: cancel,
Address: address,
device: bleDevice,
session: newSession,
connParams: &connectionParamsState{},
services: &openServices{},
link: &linkState{handler: a.connectHandler},
}
// https://learn.microsoft.com/es-es/uwp/api/windows.devices.bluetooth.bluetoothledevice.connectionstatuschanged?view=winrt-26100
// TypedEventHandler<BluetoothLEDevice,object>
connectionStatusChangedGUID := winrt.ParameterizedInstanceGUID(
foundation.GUIDTypedEventHandler,
bluetooth.SignatureBluetoothLEDevice,
"cinterface(IInspectable)", // object
)
up := make(chan struct{}, 1)
handler := foundation.NewTypedEventHandler(ole.NewGUID(connectionStatusChangedGUID), func(instance *foundation.TypedEventHandler, sender, arg unsafe.Pointer) {
status, err := bleDevice.GetConnectionStatus()
if err != nil {
return
}
if status == bluetooth.BluetoothConnectionStatusConnected {
select {
case up <- struct{}{}:
default:
}
}
device.link.report(device, status == bluetooth.BluetoothConnectionStatusConnected)
})
token, err := device.device.AddConnectionStatusChanged(handler)
device.connectionStatusListenerToken = token
device.connectionStatusListener = handler
if err != nil {
_ = handler.Release()
return device, err
}
// MaintainConnection only starts connecting, so wait like the other backends do.
// See https://learn.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.genericattributeprofile.gattsession.maintainconnection
timeout := defaultConnectionTimeout
if params.ConnectionTimeout != 0 {
timeout = time.Duration(int64(params.ConnectionTimeout)*625) * time.Microsecond
}
if err := waitConnected(up, device.Connected, timeout); err != nil {
_ = device.Disconnect()
return Device{}, err
}
// The link can come up before the listener is added, which misses its event.
device.link.report(device, true)
// WinRT has no priority at connection time. The connection stays if this
// fails, as on the platforms that have no priority.
if params.Priority != ConnectionPriorityUnspecified {
_ = device.RequestConnectionParams(params)
}
return device, nil
}
// waitConnected returns once connected reports true or up receives, or fails after timeout.
func waitConnected(up <-chan struct{}, connected func() (bool, error), timeout time.Duration) error {
if ok, err := connected(); err == nil && ok {
return nil
}
timer := time.NewTimer(timeout)
defer timer.Stop()
select {
case <-up:
return nil
case <-timer.C:
return errors.New("timeout on Connect")
}
}
// Disconnect from the BLE device. This method is non-blocking and does not
// wait until the connection is fully gone.
func (d Device) Disconnect() error {
defer d.device.Release()
defer d.session.Release()
if d.connectionStatusListener != nil {
defer d.connectionStatusListener.Release()
}
d.cancel()
d.services.close()
// Close the request while the device is still open.
if d.connParams != nil {
d.connParams.close()
}
err := d.session.Close()
_ = d.device.RemoveConnectionStatusChanged(d.connectionStatusListenerToken)
d.link.report(d, false)
if err != nil {
return err
}
if err := d.device.Close(); err != nil {
return err
}
return nil
}
// Connected returns whether the device is currently connected.
func (d Device) Connected() (bool, error) {
if d.device == nil {
return false, nil
}
status, err := d.device.GetConnectionStatus()
if err != nil {
return false, err
}
return status == bluetooth.BluetoothConnectionStatusConnected, nil
}
// RequestConnectionParams requests a different connection latency and timeout
// of the given device connection. Fields that are unset will be left alone.
// Whether or not the device will actually honor this, depends on the device and
// on the specific parameters.
//
// Windows uses only ConnectionParams.Priority. It ignores MinInterval,
// MaxInterval and Timeout. It needs Windows 11 build 22000 or later.
func (d Device) RequestConnectionParams(params ConnectionParams) error {
if params.Priority == ConnectionPriorityUnspecified {
return nil
}
if d.device == nil || d.connParams == nil {
return errDeviceNotConnected
}
// Disconnect waits for the lock, so the device stays open during the request.
d.connParams.mu.Lock()
defer d.connParams.mu.Unlock()
if d.connParams.closed {
return errDeviceNotConnected
}
preferred, err := preferredConnectionParameters(params.Priority)
if err != nil {
return err
}
defer preferred.Release()
request, err := d.device.RequestPreferredConnectionParameters(preferred)
if err != nil {
return err
}
// The status shows only that the system accepted the request. The agreed
// parameters come later, in the ConnectionParametersChanged event.
status, err := request.GetStatus()
if err != nil {
discardConnectionParamsRequest(request)
return err
}
if status != bluetooth.BluetoothLEPreferredConnectionParametersRequestStatusSuccess {
discardConnectionParamsRequest(request)
return fmt.Errorf("bluetooth: connection parameters request was not accepted: %s",
connectionParamsRequestStatusString(status))
}
// Keep the request and close the previous one. Windows applies a request
// only while the object is open.
if d.connParams.request != nil {
discardConnectionParamsRequest(d.connParams.request)
}
d.connParams.request = request
return nil
}
// preferredConnectionParameters returns the Windows preset for the priority.
// The caller must release the result.
func preferredConnectionParameters(priority ConnectionPriority) (*bluetooth.BluetoothLEPreferredConnectionParameters, error) {
var (
preferred *bluetooth.BluetoothLEPreferredConnectionParameters
err error
)
switch priority {
case ConnectionPriorityThroughput:
preferred, err = bluetooth.BluetoothLEPreferredConnectionParametersGetThroughputOptimized()
case ConnectionPriorityBalanced:
preferred, err = bluetooth.BluetoothLEPreferredConnectionParametersGetBalanced()
case ConnectionPriorityPowerSaving:
preferred, err = bluetooth.BluetoothLEPreferredConnectionParametersGetPowerOptimized()
default:
return nil, fmt.Errorf("bluetooth: unknown connection priority %d", priority)
}
if isClassNotRegistered(err) {
// Windows 10 does not have the activation factory for these presets.
return nil, fmt.Errorf("bluetooth: connection priorities need Windows 11 build 22000 or later: %w", err)
}
if err != nil {
return nil, err
}
return preferred, nil
}
// isClassNotRegistered reports whether err is REGDB_E_CLASSNOTREG (0x80040154).
// https://learn.microsoft.com/en-us/windows/win32/com/com-error-codes-1
func isClassNotRegistered(err error) bool {
var oleErr *ole.OleError
return errors.As(err, &oleErr) && uint32(oleErr.Code()) == 0x80040154
}
// discardConnectionParamsRequest closes a request that the device does not keep.
func discardConnectionParamsRequest(request *bluetooth.BluetoothLEPreferredConnectionParametersRequest) {
_ = request.Close()
request.Release()
}
func connectionParamsRequestStatusString(status bluetooth.BluetoothLEPreferredConnectionParametersRequestStatus) string {
switch status {
case bluetooth.BluetoothLEPreferredConnectionParametersRequestStatusSuccess:
return "success"
case bluetooth.BluetoothLEPreferredConnectionParametersRequestStatusDeviceNotAvailable:
return "device not available"
case bluetooth.BluetoothLEPreferredConnectionParametersRequestStatusAccessDenied:
return "access denied"
case bluetooth.BluetoothLEPreferredConnectionParametersRequestStatusUnspecified:
return "unspecified"
default:
return fmt.Sprintf("BluetoothLEPreferredConnectionParametersRequestStatus(%d)", status)
}
}
// SetRandomAddress sets the random address to be used for advertising.
func (a *Adapter) SetRandomAddress(mac MAC) error {
return errors.ErrUnsupported
}
// SetTxPower sets the radio transmit power for advertising, in dBm.
// Windows does not give this control, so it is not supported.
func (a *Advertisement) SetTxPower(dbm int8) error {
return errNotSupported
}