windows: add winrt-go dependency and remove manually generated code

This commit is contained in:
Jagoba Gascón Sánchez 2022-07-04 18:48:45 +02:00 committed by Ron Evans
parent 7ec948bf3f
commit 8f13d06111
10 changed files with 124 additions and 770 deletions

View file

@ -2,11 +2,11 @@ package bluetooth
import (
"github.com/go-ole/go-ole"
"tinygo.org/x/bluetooth/winbt"
"github.com/saltosystems/winrt-go/windows/devices/bluetooth/advertisement"
)
type Adapter struct {
watcher *winbt.IBluetoothLEAdvertisementWatcher
watcher *advertisement.BluetoothLEAdvertisementWatcher
connectHandler func(device Addresser, connected bool)
}

View file

@ -1,7 +1,13 @@
package bluetooth
import (
"tinygo.org/x/bluetooth/winbt"
"unsafe"
"github.com/go-ole/go-ole"
"github.com/saltosystems/winrt-go"
"github.com/saltosystems/winrt-go/windows/devices/bluetooth/advertisement"
"github.com/saltosystems/winrt-go/windows/foundation"
"github.com/saltosystems/winrt-go/windows/storage/streams"
)
// Address contains a Bluetooth MAC address.
@ -18,61 +24,61 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) (err error) {
return errScanning
}
a.watcher, err = winbt.NewBluetoothLEAdvertisementWatcher()
a.watcher, err = advertisement.NewBluetoothLEAdvertisementWatcher()
if err != nil {
return
}
defer a.watcher.Release()
defer func() {
_ = a.watcher.Release()
a.watcher = nil
}()
// Listen for incoming BLE advertisement packets.
err = a.watcher.AddReceivedEvent(func(watcher *winbt.IBluetoothLEAdvertisementWatcher, args *winbt.IBluetoothLEAdvertisementReceivedEventArgs) {
// parse bluetooth address
addr := args.BluetoothAddress()
adr := Address{}
for i := range adr.MAC {
adr.MAC[i] = byte(addr)
addr >>= 8
}
result := ScanResult{
RSSI: args.RawSignalStrengthInDBm(),
Address: adr,
}
var manufacturerData map[uint16][]byte
if winAdv := args.Advertisement(); winAdv != nil {
manufacturerData = winAdv.ManufacturerData()
} else {
manufacturerData = make(map[uint16][]byte)
}
// Note: the IsRandom bit is never set.
advertisement := args.Advertisement()
result.AdvertisementPayload = &advertisementFields{
AdvertisementFields{
LocalName: advertisement.LocalName(),
ManufacturerData: manufacturerData,
},
}
// 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,
)
handler := foundation.NewTypedEventHandler(ole.NewGUID(eventReceivedGuid), func(instance *foundation.TypedEventHandler, sender, arg unsafe.Pointer) {
args := (*advertisement.BluetoothLEAdvertisementReceivedEventArgs)(arg)
result := getScanResultFromArgs(args)
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 struct{})
err = a.watcher.AddStoppedEvent(func(watcher *winbt.IBluetoothLEAdvertisementWatcher, args *winbt.IBluetoothLEAdvertisementWatcherStoppedEventArgs) {
// TypedEventHandler<BluetoothLEAdvertisementWatcher, BluetoothLEAdvertisementWatcherStoppedEventArgs>
eventStoppedGuid := winrt.ParameterizedInstanceGUID(
foundation.GUIDTypedEventHandler,
advertisement.SignatureBluetoothLEAdvertisementWatcher,
advertisement.SignatureBluetoothLEAdvertisementWatcherStoppedEventArgs,
)
stoppedHandler := foundation.NewTypedEventHandler(ole.NewGUID(eventStoppedGuid), func(_ *foundation.TypedEventHandler, _, _ unsafe.Pointer) {
// Note: the args parameter has an Error property that should
// probably be checked, but I'm not sure when stopping the
// advertisement watcher could ever result in an error (except
// for bugs).
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 {
@ -81,10 +87,57 @@ func (a *Adapter) Scan(callback func(*Adapter, ScanResult)) (err error) {
// Wait until advertisement has stopped, and finish.
<-stoppingChan
a.watcher = nil
return nil
}
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,
}
var manufacturerData map[uint16][]byte = make(map[uint16][]byte)
if winAdv, err := args.GetAdvertisement(); err == nil && winAdv != nil {
vector, _ := winAdv.GetManufacturerData()
size, _ := vector.GetSize()
for i := uint32(0); i < size; i++ {
element, _ := vector.GetAt(i)
manData := (*advertisement.BluetoothLEManufacturerData)(element)
companyID, _ := manData.GetCompanyId()
buffer, _ := manData.GetData()
manufacturerData[companyID] = bufferToSlice(buffer)
}
}
// Note: the IsRandom bit is never set.
advertisement, _ := args.GetAdvertisement()
localName, _ := advertisement.GetLocalName()
result.AdvertisementPayload = &advertisementFields{
AdvertisementFields{
LocalName: localName,
ManufacturerData: manufacturerData,
},
}
return result
}
func bufferToSlice(buffer *streams.IBuffer) []byte {
dataReader, _ := streams.FromBuffer(buffer)
defer dataReader.Release()
bufferSize, _ := buffer.GetLength()
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.

5
go.mod
View file

@ -3,12 +3,13 @@ module tinygo.org/x/bluetooth
go 1.15
require (
github.com/go-ole/go-ole v1.2.4
github.com/go-ole/go-ole v1.2.6
github.com/godbus/dbus/v5 v5.0.3
github.com/muka/go-bluetooth v0.0.0-20220323170840-382ca1d29f29
github.com/saltosystems/winrt-go v0.0.0-20220826130236-ddc8202da421
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/tinygo-org/cbgo v0.0.4
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect
tinygo.org/x/drivers v0.20.0
tinygo.org/x/tinyterm v0.1.0

38
go.sum
View file

@ -1,3 +1,4 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/bgould/http v0.0.0-20190627042742-d268792bdee7/go.mod h1:BTqvVegvwifopl4KTEDth6Zezs9eR+lCWhvGKvkxJHE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@ -6,8 +7,11 @@ github.com/eclipse/paho.mqtt.golang v1.2.0/go.mod h1:H9keYFcgq3Qr5OUJm/JZI/i6U7j
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/frankban/quicktest v1.10.2/go.mod h1:K+q6oSqb0W0Ininfk863uOk1lMy69l/P6txr3mVT54s=
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
github.com/glerchundi/subcommands v0.0.0-20181212083838-923a6ccb11f8/go.mod h1:r0g3O7Y5lrWXgDfcFBRgnAKzjmPgTzwoMC2ieB345FY=
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/godbus/dbus/v5 v5.0.3 h1:ZqHaoEF7TBzh4jzPmqVhE/5A1z9of6orkAe5uHoAeME=
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
@ -23,50 +27,72 @@ github.com/muka/go-bluetooth v0.0.0-20220323170840-382ca1d29f29/go.mod h1:dMCjic
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/paypal/gatt v0.0.0-20151011220935-4ae819d591cf/go.mod h1:+AwQL2mK3Pd3S+TUwg0tYQjid0q1txyNUJuuSmz8Kdk=
github.com/pelletier/go-toml v1.6.0/go.mod h1:5N711Q9dKgbdkxHL+MEfF31hpT7l0S0s/t2kKREewys=
github.com/peterbourgon/ff/v3 v3.1.2/go.mod h1:XNJLY8EIl6MjMVjBS4F0+G0LYoAqs0DTa4rmHHukKDE=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/saltosystems/winrt-go v0.0.0-20220826130236-ddc8202da421 h1:eOgynOew0HzvLwtAsughGzqkrcuTJ6XFpT7+WNCuRNU=
github.com/saltosystems/winrt-go v0.0.0-20220826130236-ddc8202da421/go.mod h1:UvKm1lyhg+8ehk99i8g5Q7AX1LXUJgks0lRyAkG/ahQ=
github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.5 h1:s5PTfem8p8EbKQOctVV53k6jCJt3UX4IEJzwh+C324Q=
github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/suapapa/go_eddystone v1.3.1/go.mod h1:bXC11TfJOS+3g3q/Uzd7FKd5g62STQEfeEIhcKe4Qy8=
github.com/tdakkota/win32metadata v0.1.0/go.mod h1:77e6YvX0LIVW+O81fhWLnXAxxcyu/wdZdG7iwed7Fyk=
github.com/tinygo-org/cbgo v0.0.4 h1:3D76CRYbH03Rudi8sEgs/YO0x3JIMdyq8jlQtk/44fU=
github.com/tinygo-org/cbgo v0.0.4/go.mod h1:7+HgWIHd4nbAz0ESjGlJ1/v9LDU1Ox8MGzP9mah/fLk=
github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200728102440-3e129f6d46b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220624220833-87e55d714810/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY=
golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200925191224-5d1fdd8fa346/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU=
golang.org/x/tools v0.1.11/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
@ -74,8 +100,10 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8T
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
tinygo.org/x/drivers v0.14.0/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI=
tinygo.org/x/drivers v0.15.1/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI=
tinygo.org/x/drivers v0.16.0/go.mod h1:uT2svMq3EpBZpKkGO+NQHjxjGf1f42ra4OnMMwQL2aI=

View file

@ -1,318 +0,0 @@
package winbt
import (
"syscall"
"unsafe"
"github.com/go-ole/go-ole"
)
type WatcherStatus uint32
const (
WatcherStatusCreated WatcherStatus = 0
WatcherStatusStarted WatcherStatus = 1
WatcherStatusStopping WatcherStatus = 2
WatcherStatusStopped WatcherStatus = 3
WatcherStatusAborted WatcherStatus = 4
)
type IBluetoothLEAdvertisementWatcher struct {
ole.IInspectable
}
type IBluetoothLEAdvertisementWatcherVtbl struct {
ole.IInspectableVtbl
GetMinSamplingInterval uintptr // ([out] [retval] Windows.Foundation.TimeSpan* value);
GetMaxSamplingInterval uintptr // ([out] [retval] Windows.Foundation.TimeSpan* value);
GetMinOutOfRangeTimeout uintptr // ([out] [retval] Windows.Foundation.TimeSpan* value);
GetMaxOutOfRangeTimeout uintptr // ([out] [retval] Windows.Foundation.TimeSpan* value);
GetStatus uintptr // ([out] [retval] Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementWatcherStatus* value);
GetScanningMode uintptr // ([out] [retval] Windows.Devices.Bluetooth.Advertisement.BluetoothLEScanningMode* value);
SetScanningMode uintptr // ([in] Windows.Devices.Bluetooth.Advertisement.BluetoothLEScanningMode value);
GetSignalStrengthFilter uintptr // ([out] [retval] Windows.Devices.Bluetooth.BluetoothSignalStrengthFilter** value);
SetSignalStrengthFilter uintptr // ([in] Windows.Devices.Bluetooth.BluetoothSignalStrengthFilter* value);
GetAdvertisementFilter uintptr // ([out] [retval] Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementFilter** value);
SetAdvertisementFilter uintptr // ([in] Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementFilter* value);
Start uintptr // ();
Stop uintptr // ();
AddReceivedEvent uintptr // ([in] Windows.Foundation.TypedEventHandler<Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementWatcher*, Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementReceivedEventArgs*>* handler, [out] [retval] EventRegistrationToken* token);
RemoveReceivedEvent uintptr // ([in] EventRegistrationToken token);
AddStoppedEvent uintptr // ([in] Windows.Foundation.TypedEventHandler<Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementWatcher*, Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementWatcherStoppedEventArgs*>* handler, [out] [retval] EventRegistrationToken* token);
RemoveStoppedEvent uintptr // ([in] EventRegistrationToken token);
}
func (v *IBluetoothLEAdvertisementWatcher) VTable() *IBluetoothLEAdvertisementWatcherVtbl {
return (*IBluetoothLEAdvertisementWatcherVtbl)(unsafe.Pointer(v.RawVTable))
}
func NewBluetoothLEAdvertisementWatcher() (*IBluetoothLEAdvertisementWatcher, error) {
inspectable, err := ole.RoActivateInstance("Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementWatcher")
if err != nil {
return nil, err
}
watcherItf := inspectable.MustQueryInterface(ole.NewGUID("A6AC336F-F3D3-4297-8D6C-C81EA6623F40"))
return (*IBluetoothLEAdvertisementWatcher)(unsafe.Pointer(watcherItf)), nil
}
func (v *IBluetoothLEAdvertisementWatcher) AddReceivedEvent(handler func(*IBluetoothLEAdvertisementWatcher, *IBluetoothLEAdvertisementReceivedEventArgs)) (err error) {
event := NewEvent(ole.NewGUID("{90EB4ECA-D465-5EA0-A61C-033C8C5ECEF2}"), func(event *Event, argsInspectable *ole.IInspectable) {
args := (*IBluetoothLEAdvertisementReceivedEventArgs)(unsafe.Pointer(argsInspectable.MustQueryInterface(IID_IBluetoothLEAdvertisementReceivedEventArgs)))
defer args.Release()
handler(v, args)
})
hr, _, _ := syscall.Syscall(
v.VTable().AddReceivedEvent,
3,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(event)),
uintptr(unsafe.Pointer(&event.token)),
)
return makeError(hr)
}
func (v *IBluetoothLEAdvertisementWatcher) AddStoppedEvent(handler func(*IBluetoothLEAdvertisementWatcher, *IBluetoothLEAdvertisementWatcherStoppedEventArgs)) (err error) {
event := NewEvent(ole.NewGUID("{9936A4DB-DC99-55C3-9E9B-BF4854BD9EAB}"), func(event *Event, argsInspectable *ole.IInspectable) {
args := (*IBluetoothLEAdvertisementWatcherStoppedEventArgs)(unsafe.Pointer(argsInspectable.MustQueryInterface(IID_IBluetoothLEAdvertisementWatcherStoppedEventArgs)))
defer args.Release()
handler(v, args)
})
hr, _, _ := syscall.Syscall(
v.VTable().AddStoppedEvent,
3,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(event)),
uintptr(unsafe.Pointer(&event.token)),
)
return makeError(hr)
}
func (v *IBluetoothLEAdvertisementWatcher) Start() error {
hr, _, _ := syscall.Syscall(
v.VTable().Start,
1,
uintptr(unsafe.Pointer(v)),
0,
0)
return makeError(hr)
}
func (v *IBluetoothLEAdvertisementWatcher) Stop() error {
hr, _, _ := syscall.Syscall(
v.VTable().Stop,
1,
uintptr(unsafe.Pointer(v)),
0,
0)
return makeError(hr)
}
func (v *IBluetoothLEAdvertisementWatcher) Status() WatcherStatus {
var status WatcherStatus
hr, _, _ := syscall.Syscall(
v.VTable().GetStatus,
2,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(&status)),
0)
mustSucceed(hr)
return status
}
type IBluetoothLEAdvertisementReceivedEventArgs struct {
ole.IInspectable
}
type IBluetoothLEAdvertisementReceivedEventArgsVtbl struct {
ole.IInspectableVtbl
RawSignalStrengthInDBm uintptr // ([out] [retval] INT16* value);
BluetoothAddress uintptr // ([out] [retval] UINT64* value);
AdvertisementType uintptr // ([out] [retval] Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementType* value);
Timestamp uintptr // ([out] [retval] Windows.Foundation.DateTime* value);
Advertisement uintptr // ([out] [retval] Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisement** value);
}
func (v *IBluetoothLEAdvertisementReceivedEventArgs) VTable() *IBluetoothLEAdvertisementReceivedEventArgsVtbl {
return (*IBluetoothLEAdvertisementReceivedEventArgsVtbl)(unsafe.Pointer(v.RawVTable))
}
func (v *IBluetoothLEAdvertisementReceivedEventArgs) RawSignalStrengthInDBm() (rssi int16) {
hr, _, _ := syscall.Syscall(
v.VTable().RawSignalStrengthInDBm,
2,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(&rssi)),
0)
mustSucceed(hr)
return
}
func (v *IBluetoothLEAdvertisementReceivedEventArgs) BluetoothAddress() (address uint64) {
hr, _, _ := syscall.Syscall(
v.VTable().BluetoothAddress,
2,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(&address)),
0)
mustSucceed(hr)
return
}
func (v *IBluetoothLEAdvertisementReceivedEventArgs) Advertisement() (advertisement *IBluetoothLEAdvertisement) {
hr, _, _ := syscall.Syscall(
v.VTable().Advertisement,
2,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(&advertisement)),
0)
mustSucceed(hr)
return
}
type IBluetoothLEAdvertisementWatcherStoppedEventArgs struct {
ole.IInspectable
}
type IBluetoothLEManufacturerData struct {
ole.IInspectable
}
type IBluetoothLEManufacturerDataVtbl struct {
ole.IInspectableVtbl
GetCompanyId uintptr // ([out] [retval] UINT16* value);
SetCompanyId uintptr // ([in] UINT16 value);
GetData uintptr // ([out] [retval] Windows.Storage.Streams.IBuffer** value);
SetData uintptr // ([in] Windows.Storage.Streams.IBuffer* value);
}
func (v *IBluetoothLEManufacturerData) VTable() *IBluetoothLEManufacturerDataVtbl {
return (*IBluetoothLEManufacturerDataVtbl)(unsafe.Pointer(v.RawVTable))
}
func (v *IBluetoothLEManufacturerData) GetCompanyID() uint16 {
var manufacturerID uint16
hr, _, _ := syscall.SyscallN(
v.VTable().GetCompanyId,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(&manufacturerID)),
)
mustSucceed(hr)
return manufacturerID
}
func (v *IBluetoothLEManufacturerData) GetData() *IBuffer {
var buf *IBuffer
hr, _, _ := syscall.SyscallN(
v.VTable().GetData,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(&buf)),
)
mustSucceed(hr)
return buf
}
type IBluetoothLEAdvertisement struct {
ole.IInspectable
}
type IBluetoothLEAdvertisementVtbl struct {
ole.IInspectableVtbl
GetFlags uintptr // ([out] [retval] Windows.Foundation.IReference<Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementFlags>** value);
SetFlags uintptr // ([in] Windows.Foundation.IReference<Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementFlags>* value);
GetLocalName uintptr // ([out] [retval] HSTRING* value);
SetLocalName uintptr // ([in] HSTRING value);
GetServiceUuids uintptr // ([out] [retval] Windows.Foundation.Collections.IVector<GUID>** value);
GetManufacturerData uintptr // ([out] [retval] Windows.Foundation.Collections.IVector<Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData*>** value);
GetDataSections uintptr // ([out] [retval] Windows.Foundation.Collections.IVector<Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementDataSection*>** value);
GetManufacturerDataByCompanyId uintptr // ([in] UINT16 companyId, [out] [retval] Windows.Foundation.Collections.IVectorView<Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData*>** dataList);
GetSectionsByType uintptr // ([in] BYTE type, [out] [retval] Windows.Foundation.Collections.IVectorView<Windows.Devices.Bluetooth.Advertisement.BluetoothLEAdvertisementDataSection*>** sectionList);
}
func (v *IBluetoothLEAdvertisement) VTable() *IBluetoothLEAdvertisementVtbl {
return (*IBluetoothLEAdvertisementVtbl)(unsafe.Pointer(v.RawVTable))
}
func (v *IBluetoothLEAdvertisement) LocalName() string {
var hstring ole.HString
hr, _, _ := syscall.Syscall(
v.VTable().GetLocalName,
2,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(&hstring)),
0)
if hr != 0 {
// Should not happen.
panic(ole.NewError(hr))
}
name := hstring.String()
ole.DeleteHString(hstring)
return name
}
func (v *IBluetoothLEAdvertisement) ManufacturerData() map[uint16][]byte {
// ([out] [retval] Windows.Foundation.Collections.IVector<Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData*>** value);
var vector *IVector
hr, _, _ := syscall.SyscallN(
v.VTable().GetManufacturerData,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(&vector)),
)
mustSucceed(hr)
manufacturerData := make(map[uint16][]byte)
// convert manufacturer data to go bytes
for i := 0; i < vector.Size(); i++ {
mData := (*IBluetoothLEManufacturerData)(vector.At(i))
mID := mData.GetCompanyID()
mPayload := mData.GetData()
b, err := mPayload.Bytes()
if err == nil {
manufacturerData[mID] = b
}
}
return manufacturerData
}
func (v *IBluetoothLEAdvertisement) DataSections() (vector *IVector) {
hr, _, _ := syscall.Syscall(
v.VTable().GetDataSections,
2,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(&vector)),
0)
mustSucceed(hr)
return
}
type IBluetoothLEAdvertisementDataSection struct {
ole.IInspectable
}
type IBluetoothLEAdvertisementDataSectionVtbl struct {
ole.IInspectableVtbl
GetDataType uintptr // ([out] [retval] BYTE* value)
SetDataType uintptr // ([in] BYTE value)
GetData uintptr // ([out] [retval] Windows.Storage.Streams.IBuffer** value)
SetData uintptr // ([in] Windows.Storage.Streams.IBuffer* value)
}
func (v *IBluetoothLEAdvertisementDataSection) VTable() *IBluetoothLEAdvertisementDataSectionVtbl {
return (*IBluetoothLEAdvertisementDataSectionVtbl)(unsafe.Pointer(v.RawVTable))
}
func (v *IBluetoothLEAdvertisementDataSection) DataType() (value byte) {
hr, _, _ := syscall.Syscall(
v.VTable().GetDataType,
2,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(&value)),
0)
mustSucceed(hr)
return
}

View file

@ -1,137 +0,0 @@
package winbt
import (
"syscall"
"unsafe"
"github.com/go-ole/go-ole"
)
// IBuffer Represents a referenced array of bytes used by
// byte stream read and write interfaces. Buffer is the class
// implementation of this interface.
type IBuffer struct {
ole.IInspectable
}
type IBufferVtbl struct {
ole.IInspectableVtbl
// These methods have been obtained from windows.storage.streams.h in the WinRT API.
// read methods
GetCapacity uintptr // ([out] [retval] UINT32* value)
GetLength uintptr // ([out] [retval] UINT32* value)
// write methods
SetLength uintptr // ([in] UINT32 value);
}
func (v *IBuffer) VTable() *IBufferVtbl {
return (*IBufferVtbl)(unsafe.Pointer(v.RawVTable))
}
func (v *IBuffer) Length() int {
var n int
hr, _, _ := syscall.SyscallN(
v.VTable().GetLength,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(&n)),
)
mustSucceed(hr)
return n
}
func (v *IBuffer) Bytes() ([]byte, error) {
// Get DataReaderStatics: we need to pass the class name, and the iid of the interface
// GUID: https://github.com/tpn/winsdk-10/blob/9b69fd26ac0c7d0b83d378dba01080e93349c2ed/Include/10.0.14393.0/winrt/windows.storage.streams.idl#L311
inspectable, err := ole.RoGetActivationFactory("Windows.Storage.Streams.DataReader", ole.NewGUID("11FCBFC8-F93A-471B-B121-F379E349313C"))
if err != nil {
return nil, err
}
drStatics := (*IDataReaderStatics)(unsafe.Pointer(inspectable))
// Call FromBuffer to create new DataReader
var dr *IDataReader
hr, _, _ := syscall.SyscallN(
drStatics.VTable().FromBuffer,
0, // this is a static func, so there's no this
uintptr(unsafe.Pointer(v)), // in buffer
uintptr(unsafe.Pointer(&dr)), // out DataReader
)
err = makeError(hr)
if err != nil {
return nil, err
}
data := make([]byte, v.Length())
err = dr.Bytes(data)
return data, err
}
type IDataReaderStatics struct {
ole.IInspectable
}
type IDataReaderStaticsVtbl struct {
ole.IInspectableVtbl
FromBuffer uintptr // ([in] Windows.Storage.Streams.IBuffer* buffer, [out] [retval] Windows.Storage.Streams.DataReader** dataReader);
}
func (v *IDataReaderStatics) VTable() *IDataReaderStaticsVtbl {
return (*IDataReaderStaticsVtbl)(unsafe.Pointer(v.RawVTable))
}
type IDataReader struct {
ole.IInspectable
}
type IDataReaderVtbl struct {
ole.IInspectableVtbl
GetUnconsumedBufferLength uintptr // ([out] [retval] UINT32* value);
GetUnicodeEncoding uintptr // ([out] [retval] Windows.Storage.Streams.UnicodeEncoding* value);
PutUnicodeEncoding uintptr // ([in] Windows.Storage.Streams.UnicodeEncoding value);
GetByteOrder uintptr // ([out] [retval] Windows.Storage.Streams.ByteOrder* value);
PutByteOrder uintptr // ([in] Windows.Storage.Streams.ByteOrder value);
GetInputStreamOptions uintptr // ([out] [retval] Windows.Storage.Streams.InputStreamOptions* value);
PutInputStreamOptions uintptr // ([in] Windows.Storage.Streams.InputStreamOptions value);
ReadByte uintptr // ([out] [retval] BYTE* value);
ReadBytes uintptr // ([in] UINT32 __valueSize, [out] [size_is(__valueSize)] BYTE* value);
ReadBuffer uintptr // ([in] UINT32 length, [out] [retval] Windows.Storage.Streams.IBuffer** buffer);
ReadBoolean uintptr // ([out] [retval] boolean* value);
ReadGuid uintptr // ([out] [retval] GUID* value);
ReadInt16 uintptr // ([out] [retval] INT16* value);
ReadInt32 uintptr // ([out] [retval] INT32* value);
ReadInt64 uintptr // ([out] [retval] INT64* value);
ReadUInt16 uintptr // ([out] [retval] UINT16* value);
ReadUInt32 uintptr // ([out] [retval] UINT32* value);
ReadUInt64 uintptr // ([out] [retval] UINT64* value);
ReadSingle uintptr // ([out] [retval] FLOAT* value);
ReadDouble uintptr // ([out] [retval] DOUBLE* value);
ReadString uintptr // ([in] UINT32 codeUnitCount, [out] [retval] HSTRING* value);
ReadDateTime uintptr // ([out] [retval] Windows.Foundation.DateTime* value);
ReadTimeSpan uintptr // ([out] [retval] Windows.Foundation.TimeSpan* value);
LoadAsync uintptr // ([in] UINT32 count, [out] [retval] Windows.Storage.Streams.DataReaderLoadOperation** operation);
DetachBuffer uintptr // ([out] [retval] Windows.Storage.Streams.IBuffer** buffer);
DetachStream uintptr // ([out] [retval] Windows.Storage.Streams.IInputStream** stream);*/
}
func (v *IDataReader) VTable() *IDataReaderVtbl {
return (*IDataReaderVtbl)(unsafe.Pointer(v.RawVTable))
}
// Bytes fills the incoming array with the data from the buffer
func (v *IDataReader) Bytes(b []byte) error {
// ([in] UINT32 __valueSize, [out] [size_is(__valueSize)] BYTE* value);
size := len(b)
hr, _, _ := syscall.SyscallN(
v.VTable().ReadBytes,
uintptr(unsafe.Pointer(v)),
uintptr(size),
uintptr(unsafe.Pointer(&b[0])),
)
return makeError(hr)
}

View file

@ -1,56 +0,0 @@
// This file implements the C side of WinRT events.
// Unfortunately, this cannot be done entirely in pure go, for two reasons:
// * An Event object must be shared with the C world (WinRT) after
// syscall.Syscall returns. This is not allowed in Go, to keep the option
// open to switch to a moving GC in the future. For this, it is needed to
// allocate the Event object on the C heap (using malloc).
// * Building a virtual function table is very difficult (if not impossible)
// in pure Go. It might be possible using reflect, but due the the previous
// issue I haven't investigated that.
#include <stdint.h>
// Note: these functions have a different signature but because they are only
// used as function pointers (and never called) and because they use C name
// mangling, the signature doesn't really matter.
void winbt_Event_Invoke(void);
void winbt_Event_QueryInterface(void);
// This is the contract the functions below should adhere to:
// https://docs.microsoft.com/en-us/windows/win32/api/unknwn/nn-unknwn-iunknown
static uint64_t winbt_Event_AddRef(void) {
// This is safe, see winbt_Event_Release.
return 2;
}
static uint64_t winbt_Event_Release(void) {
// Pretend there is one reference left.
// The docs say:
// > This value is intended to be used only for test purposes.
// Also see:
// https://docs.microsoft.com/en-us/archive/msdn-magazine/2013/august/windows-with-c-the-windows-runtime-application-model
return 1;
}
// The Vtable structure for WinRT event interfaces.
typedef struct {
void *QueryInterface;
void *AddRef;
void *Release;
void *Invoke;
} EventVtbl_t;
// The Vtable itself. It can be kept constant.
static const EventVtbl_t winbt_EventVtbl = {
(void*)winbt_Event_QueryInterface,
(void*)winbt_Event_AddRef,
(void*)winbt_Event_Release,
(void*)winbt_Event_Invoke,
};
// A small helper function to get the Vtable.
const EventVtbl_t * winbt_getEventVtbl(void) {
return &winbt_EventVtbl;
}

View file

@ -1,82 +0,0 @@
package winbt
// This file implements a COM object that can be used for various event
// callbacks. In C++, it would use an ITypedEventHandler instantiation.
import (
"unsafe"
"github.com/go-ole/go-ole"
)
// const void * winbt_getEventVtbl(void);
import "C"
// Event implements event handler interfaces (ITypedEventHandler). This is
// therefore a valid COM object, derived from IUnknown.
type Event struct {
ole.IUnknown
IID *ole.GUID
Callback func(*Event, *ole.IInspectable)
token uintptr
}
// NewEvent creates a new COM object for event callbacks. The IID must be the
// IID for a particular event, which is unfortunately not included in the *.idl
// files but has to be found in the relevant C++/WinRT header files.
func NewEvent(iid *ole.GUID, callback func(*Event, *ole.IInspectable)) *Event {
// Another way to find the IID is to print the requested IID in the
// QueryInterface method below. The first queried IID is likely the one
// that is the event interface IID.
// The event must be allocated on the C heap, because it is not allowed to
// retain a pointer on the Go heap after a non-Go call returns (e.g.
// syscall.Syscall).
event := (*Event)(C.malloc(C.size_t(unsafe.Sizeof(Event{}))))
event.RawVTable = (*interface{})(C.winbt_getEventVtbl())
event.IID = iid
event.Callback = callback
return event
}
// The two functions below implement IUnknown and an interface I couldn't
// really get the definition of (possibly ITypedEventHandler, although
// ITypedEventHandler is really a C++ template). The closest thing I could find
// to documentation are the C++/WinRT headers and this mention by Kenny Kerr
// (the original developer of C++/WinRT):
// https://docs.microsoft.com/en-us/archive/msdn-magazine/2013/august/windows-with-c-the-windows-runtime-application-model
// > [...] Fortunately, all of these interfaces are generated by the MIDL
// > compiler, which takes care to specialize each one, and its on these
// > specializations that it attaches the GUID representing the interface
// > identifier. As complicated as the previous typedef might appear, it
// > defines a COM interface that derives directly from IUnknown and provides
// > a single method called Invoke.
//export winbt_Event_QueryInterface
func winbt_Event_QueryInterface(eventPtr, iidPtr unsafe.Pointer, ppvObject *unsafe.Pointer) uintptr {
// This function must adhere to the QueryInterface defined here:
// https://docs.microsoft.com/en-us/windows/win32/api/unknwn/nn-unknwn-iunknown
iid := (*ole.GUID)(iidPtr)
event := (*Event)(eventPtr)
if ole.IsEqualGUID(iid, event.IID) {
// This is us.
*ppvObject = eventPtr
return ole.S_OK
}
if ole.IsEqualGUID(iid, ole.IID_IUnknown) {
// This is our parent. There are some limitations as to what we can
// return here, but returning the *Event pointer is fine.
*ppvObject = eventPtr
return ole.S_OK
}
return 0x80004002 // E_NOTINTERFACE
}
//export winbt_Event_Invoke
func winbt_Event_Invoke(eventPtr, senderPtr, argsPtr unsafe.Pointer) uintptr {
// See the quote above.
event := (*Event)(eventPtr)
argsInspectable := (*ole.IInspectable)(argsPtr)
event.Callback(event, argsInspectable)
return ole.S_OK
}

View file

@ -1,73 +0,0 @@
package winbt
import (
"syscall"
"unsafe"
"github.com/go-ole/go-ole"
)
type IVector struct {
ole.IInspectable
}
type IVectorVtbl struct {
ole.IInspectableVtbl
// These methods have been obtained from windows.foundation.collections.h
// in the WinRT API.
// read methods
GetAt uintptr // (_In_opt_ unsigned index, _Out_ T_abi *item)
GetSize uintptr // (_Out_ unsigned *size)
GetView uintptr // (_Outptr_result_maybenull_ IVectorView<T_logical> **view)
IndexOf uintptr // (_In_opt_ T_abi value, _Out_ unsigned *index, _Out_ boolean *found)
// write methods
SetAt uintptr // (_In_ unsigned index, _In_opt_ T_abi item)
InsertAt uintptr // (_In_ unsigned index, _In_opt_ T_abi item)
RemoveAt uintptr // (_In_ unsigned index)
Append uintptr // (_In_opt_ T_abi item)
RemoveAtEnd uintptr // ()
Clear uintptr // ()
// bulk transfer methods
GetMany uintptr // (_In_ unsigned startIndex, _In_ unsigned capacity, _Out_writes_to_(capacity,*actual) T_abi *value, _Out_ unsigned *actual)
ReplaceAll uintptr // (_In_ unsigned count, _In_reads_(count) T_abi *value)
}
func (v *IVector) VTable() *IVectorVtbl {
return (*IVectorVtbl)(unsafe.Pointer(v.RawVTable))
}
func (v *IVector) At(index int) (element unsafe.Pointer) {
// The caller will need to cast the element to the correct type (for
// example, *IBluetoothLEAdvertisementDataSection).
hr, _, _ := syscall.Syscall(
v.VTable().GetAt,
3,
uintptr(unsafe.Pointer(v)),
uintptr(index),
uintptr(unsafe.Pointer(&element)),
)
mustSucceed(hr)
return
}
func (v *IVector) Size() int {
// Note that because the size is defined as `unsigned`, and `unsigned`
// means 32-bit in Windows (even 64-bit windows), the size is always a
// uint32.
// Casting to int because that is the common data type for sizes in Go. It
// should practically always fit on 32-bit Windows and definitely always
// fit on 64-bit Windows (with a 64-bit Go int).
var size uint32
hr, _, _ := syscall.Syscall(
v.VTable().GetSize,
2,
uintptr(unsafe.Pointer(v)),
uintptr(unsafe.Pointer(&size)),
0)
mustSucceed(hr)
return int(size)
}

View file

@ -1,62 +0,0 @@
// Package winbt provides a thin layer over the WinRT Bluetooth interfaces. It
// is not designed to be used directly by applications: the bluetooth package
// will wrap the API exposed here in a nice platform-independent way.
//
// You can find the original *.idl and *.h files in a directory like this,
// after installing the Windows SDK:
//
// C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\winrt
//
// Some helpful articles to understand WinRT at a low level:
// https://blog.xojo.com/2019/07/02/accessing-windows-runtime-winrt/
// https://docs.microsoft.com/en-us/archive/msdn-magazine/2013/august/windows-with-c-the-windows-runtime-application-model
// https://blog.magnusmontin.net/2017/12/30/minimal-uwp-wrl-xaml-app/
// https://yizhang82.dev/what-is-winrt
// https://www.slideshare.net/goldshtn/deep-dive-into-winrt
//
package winbt // import "tinygo.org/x/bluetooth/winbt"
import (
"github.com/go-ole/go-ole"
)
var (
IID_IBluetoothLEAdvertisementReceivedEventArgs = ole.NewGUID("27987DDF-E596-41BE-8D43-9E6731D4A913")
IID_IBluetoothLEAdvertisementWatcherStoppedEventArgs = ole.NewGUID("DD40F84D-E7B9-43E3-9C04-0685D085FD8C")
)
// printGUIDs prints the GUIDs this IInspectable implements. It is primarily
// intended for debugging.
func printGUIDs(inspectable *ole.IInspectable) {
guids, err := inspectable.GetIids()
if err != nil {
println("could not get GUIDs for IInspectable:", err.Error())
return
}
for _, guid := range guids {
println("guid:", guid.String())
}
}
// makeError makes a *ole.OleError if hr is non-nil. If it is nil, it will
// return nil.
// This is an utility function to easily convert an HRESULT into a Go error
// value.
func makeError(hr uintptr) error {
if hr != 0 {
return ole.NewError(hr)
}
return nil
}
// mustSucceed can be called to check the return value of getters, which should
// always succeed. If hr is non-zero, it will panic with an error message.
func mustSucceed(hr uintptr) {
if hr != 0 {
// Status is a getter, so should never return an error unless
// an invalid `v` is passed in (for example, `v` is nil) - in
// which case, there is definitely a bug and we should fail
// early.
panic("winbt: unexpected error: " + ole.NewError(hr).String())
}
}