Skip to content

Commit d0178d9

Browse files
jagobagascondeadprogram
authored andcommitted
mtu-{darwin,linux,windows,sd}: add get mtu function
1 parent c85b6cc commit d0178d9

File tree

6 files changed

+34
-5
lines changed

6 files changed

+34
-5
lines changed

gattc_darwin.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
173173
return nil
174174
}
175175

176+
// GetMTU returns the MTU for the characteristic.
177+
func (c DeviceCharacteristic) GetMTU() (uint16, error) {
178+
return uint16(c.service.device.prph.MaximumWriteValueLength(false)), nil
179+
}
180+
176181
// Read reads the current characteristic value.
177182
func (c *deviceCharacteristic) Read(data []byte) (n int, err error) {
178183
c.readChan = make(chan error)

gattc_linux.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
238238
return c.characteristic.StartNotify()
239239
}
240240

241+
// GetMTU returns the MTU for the characteristic.
242+
func (c DeviceCharacteristic) GetMTU() (uint16, error) {
243+
mtu, err := c.characteristic.GetProperty("MTU")
244+
if err != nil {
245+
return uint16(0), err
246+
}
247+
return mtu.Value().(uint16), nil
248+
}
249+
241250
// Read reads the current characteristic value.
242251
func (c *DeviceCharacteristic) Read(data []byte) (int, error) {
243252
options := make(map[string]interface{})

gattc_sd.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,8 @@ func (c *DeviceCharacteristic) Read(data []byte) (n int, err error) {
451451

452452
return
453453
}
454+
455+
// GetMTU returns the MTU for the characteristic.
456+
func (c DeviceCharacteristic) GetMTU() (uint16, error) {
457+
return uint16(C.BLE_GATT_ATT_MTU_DEFAULT), nil
458+
}

gattc_windows.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func (d *Device) DiscoverServices(filterUUIDs []UUID) ([]DeviceService, error) {
100100
services = append(services, DeviceService{
101101
uuidWrapper: serviceUuid,
102102
service: srv,
103+
device: d,
103104
})
104105
}
105106

@@ -132,6 +133,7 @@ type DeviceService struct {
132133
uuidWrapper
133134

134135
service *genericattributeprofile.GattDeviceService
136+
device *Device
135137
}
136138

137139
// UUID returns the UUID for this DeviceService.
@@ -180,12 +182,12 @@ func (s *DeviceService) DiscoverCharacteristics(filterUUIDs []UUID) ([]DeviceCha
180182

181183
var characteristics []DeviceCharacteristic
182184
for i := uint32(0); i < characteristicsSize; i++ {
183-
s, err := charVector.GetAt(i)
185+
c, err := charVector.GetAt(i)
184186
if err != nil {
185187
return nil, err
186188
}
187189

188-
characteristic := (*genericattributeprofile.GattCharacteristic)(s)
190+
characteristic := (*genericattributeprofile.GattCharacteristic)(c)
189191
guid, err := characteristic.GetUuid()
190192
if err != nil {
191193
return nil, err
@@ -215,6 +217,7 @@ func (s *DeviceService) DiscoverCharacteristics(filterUUIDs []UUID) ([]DeviceCha
215217

216218
characteristics = append(characteristics, DeviceCharacteristic{
217219
uuidWrapper: characteristicUUID,
220+
service: s,
218221
characteristic: characteristic,
219222
properties: properties,
220223
})
@@ -230,6 +233,8 @@ type DeviceCharacteristic struct {
230233

231234
characteristic *genericattributeprofile.GattCharacteristic
232235
properties genericattributeprofile.GattCharacteristicProperties
236+
237+
service *DeviceService
233238
}
234239

235240
// UUID returns the UUID for this DeviceCharacteristic.
@@ -241,6 +246,11 @@ func (c *DeviceCharacteristic) Properties() uint32 {
241246
return uint32(c.properties)
242247
}
243248

249+
// GetMTU returns the MTU for the characteristic.
250+
func (c *DeviceCharacteristic) GetMTU() (uint16, error) {
251+
return c.service.device.session.GetMaxPduSize()
252+
}
253+
244254
// Write replaces the characteristic value with a new value. The
245255
// call will return after all data has been written.
246256
func (c DeviceCharacteristic) Write(p []byte) (n int, err error) {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/go-ole/go-ole v1.2.6
77
github.com/godbus/dbus/v5 v5.0.3
88
github.com/muka/go-bluetooth v0.0.0-20220830075246-0746e3a1ea53
9-
github.com/saltosystems/winrt-go v0.0.0-20220826130236-ddc8202da421
9+
github.com/saltosystems/winrt-go v0.0.0-20220913104103-712830fcd2ad
1010
github.com/sirupsen/logrus v1.9.0 // indirect
1111
github.com/tinygo-org/cbgo v0.0.4
1212
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
3434
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3535
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3636
github.com/sago35/go-bdf v0.0.0-20200313142241-6c17821c91c4/go.mod h1:rOebXGuMLsXhZAC6mF/TjxONsm45498ZyzVhel++6KM=
37-
github.com/saltosystems/winrt-go v0.0.0-20220826130236-ddc8202da421 h1:eOgynOew0HzvLwtAsughGzqkrcuTJ6XFpT7+WNCuRNU=
38-
github.com/saltosystems/winrt-go v0.0.0-20220826130236-ddc8202da421/go.mod h1:UvKm1lyhg+8ehk99i8g5Q7AX1LXUJgks0lRyAkG/ahQ=
37+
github.com/saltosystems/winrt-go v0.0.0-20220913104103-712830fcd2ad h1:sE3kYG0WAV8eML/eK3uoKgsD85qH8yvlKuPGWOgAecg=
38+
github.com/saltosystems/winrt-go v0.0.0-20220913104103-712830fcd2ad/go.mod h1:UvKm1lyhg+8ehk99i8g5Q7AX1LXUJgks0lRyAkG/ahQ=
3939
github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo=
4040
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
4141
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=

0 commit comments

Comments
 (0)