Skip to content
Open
Show file tree
Hide file tree
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
38 changes: 25 additions & 13 deletions gap_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,27 +233,36 @@ func getScanResultFromArgs(args *advertisement.BluetoothLEAdvertisementReceivedE

var manufacturerData []ManufacturerDataElement
var serviceUUIDs []UUID
if winAdv, err := args.GetAdvertisement(); err == nil && winAdv != nil {
// Extract manufacturer data
vector, _ := winAdv.GetManufacturerData()
size, _ := vector.GetSize()

// Extract manufacturer data
manDataVector, _ := winAdv.GetManufacturerData()
if manDataVector != nil {
defer manDataVector.Release()
size, _ := manDataVector.GetSize()
for i := uint32(0); i < size; i++ {
element, _ := vector.GetAt(i)
element, _ := manDataVector.GetAt(i)
manData := (*advertisement.BluetoothLEManufacturerData)(element)

companyID, _ := manData.GetCompanyId()
buffer, _ := manData.GetData()
manufacturerData = append(manufacturerData, ManufacturerDataElement{
CompanyID: companyID,
Data: bufferToSlice(buffer),
})
if buffer != nil {
manufacturerData = append(manufacturerData, ManufacturerDataElement{
CompanyID: companyID,
Data: bufferToSlice(buffer),
})
buffer.Release()
}
manData.Release()
}
}

// Extract service UUIDs
vector, _ = winAdv.GetServiceUuids()
size, _ = vector.GetSize()
// Extract service UUIDs
serviceUuidsVector, _ := winAdv.GetServiceUuids()
if serviceUuidsVector != nil {
defer serviceUuidsVector.Release()
size, _ := serviceUuidsVector.GetSize()
for i := uint32(0); i < size; i++ {
element, _ := vector.GetAt(i)
element, _ := serviceUuidsVector.GetAt(i)
// element is not a pointer, but a GUID struct. But we cannot convert
// unsafe.Pointer to a non-pointer type, so instead we are doing this:
serviceGUID := (*syscall.GUID)(unsafe.Pointer(&element))
Expand Down Expand Up @@ -342,6 +351,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
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>
Expand All @@ -367,6 +377,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
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
Expand All @@ -375,6 +386,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
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)
Expand Down
17 changes: 17 additions & 0 deletions gattc_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (d Device) DiscoverServices(filterUUIDs []UUID) ([]DeviceService, error) {
if err != nil {
return nil, err
}
defer getServicesOperation.Release()

if err := awaitAsyncOperation(getServicesOperation, genericattributeprofile.SignatureGattDeviceServicesResult); err != nil {
return nil, err
Expand All @@ -64,6 +65,7 @@ func (d Device) DiscoverServices(filterUUIDs []UUID) ([]DeviceService, error) {
}

servicesResult := (*genericattributeprofile.GattDeviceServicesResult)(res)
defer servicesResult.Release()

status, err := servicesResult.GetStatus()
if err != nil {
Expand All @@ -77,6 +79,7 @@ func (d Device) DiscoverServices(filterUUIDs []UUID) ([]DeviceService, error) {
if err != nil {
return nil, err
}
defer servicesVector.Release()

// Convert services vector to array
servicesSize, err := servicesVector.GetSize()
Expand Down Expand Up @@ -197,6 +200,7 @@ func (s DeviceService) DiscoverCharacteristics(filterUUIDs []UUID) ([]DeviceChar
if err != nil {
return nil, err
}
defer getCharacteristicsOp.Release()

// IAsyncOperation<GattCharacteristicsResult>
if err := awaitAsyncOperation(getCharacteristicsOp, genericattributeprofile.SignatureGattCharacteristicsResult); err != nil {
Expand All @@ -209,12 +213,14 @@ func (s DeviceService) DiscoverCharacteristics(filterUUIDs []UUID) ([]DeviceChar
}

gattCharResult := (*genericattributeprofile.GattCharacteristicsResult)(res)
defer gattCharResult.Release()

// IVectorView<GattCharacteristic>
charVector, err := gattCharResult.GetCharacteristics()
if err != nil {
return nil, err
}
defer charVector.Release()

// Convert characteristics vector to array
characteristicsSize, err := charVector.GetSize()
Expand Down Expand Up @@ -361,9 +367,14 @@ func (c DeviceCharacteristic) write(p []byte, mode genericattributeprofile.GattW
if err != nil {
return 0, err
}
defer value.Release()

// IAsyncOperation<GattCommunicationStatus>
asyncOp, err := c.characteristic.WriteValueWithOptionAsync(value, mode)
if err != nil {
return 0, err
}
defer asyncOp.Release()

if err := awaitAsyncOperation(asyncOp, genericattributeprofile.SignatureGattCommunicationStatus); err != nil {
return 0, err
Expand Down Expand Up @@ -395,6 +406,7 @@ func (c DeviceCharacteristic) Read(data []byte) (int, error) {
if err != nil {
return 0, err
}
defer readOp.Release()

// IAsyncOperation<GattReadResult>
if err := awaitAsyncOperation(readOp, genericattributeprofile.SignatureGattReadResult); err != nil {
Expand All @@ -407,16 +419,19 @@ func (c DeviceCharacteristic) Read(data []byte) (int, error) {
}

result := (*genericattributeprofile.GattReadResult)(res)
defer result.Release()

buffer, err := result.GetValue()
if err != nil {
return 0, err
}
defer buffer.Release()

datareader, err := streams.DataReaderFromBuffer(buffer)
if err != nil {
return 0, err
}
defer datareader.Release()

bufferlen, err := buffer.GetLength()
if err != nil {
Expand Down Expand Up @@ -496,6 +511,7 @@ func (c DeviceCharacteristic) EnableNotificationsWithMode(mode NotificationMode,
if err != nil {
return
}
defer buf.Release()

reader, err := streams.DataReaderFromBuffer(buf)
if err != nil {
Expand Down Expand Up @@ -527,6 +543,7 @@ func (c DeviceCharacteristic) EnableNotificationsWithMode(mode NotificationMode,
if err != nil {
return err
}
defer writeOp.Release()

// IAsyncOperation<GattCommunicationStatus>
if err := awaitAsyncOperation(writeOp, genericattributeprofile.SignatureGattCommunicationStatus); err != nil {
Expand Down
Loading