Skip to content

Commit bf647ec

Browse files
committed
ninafw: this PR contains several fixes and improvements for the NINAFW implementation including:
- correctly return from read requests instead of returning spurious error - move some steps previously being done during Configure() into Start() where they more correctly belonged. - use advertising display name as the correct default value for the generic access characteristic. - speed up the polling for new notifications for Centrals Signed-off-by: deadprogram <[email protected]>
1 parent 564b0ba commit bf647ec

File tree

4 files changed

+71
-44
lines changed

4 files changed

+71
-44
lines changed

adapter_ninafw.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (a *Adapter) startNotifications() {
171171
}
172172
}
173173

174-
time.Sleep(250 * time.Millisecond)
174+
time.Sleep(10 * time.Millisecond)
175175
}
176176
}()
177177

att_ninafw.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,8 @@ func (a *att) handleReadReq(handle, attrHandle uint16) error {
925925
if err := a.hci.sendAclPkt(handle, attCID, response[:pos]); err != nil {
926926
return err
927927
}
928+
929+
return nil
928930
}
929931

930932
case attributeTypeDescriptor:
@@ -945,6 +947,8 @@ func (a *att) handleReadReq(handle, attrHandle uint16) error {
945947
if err := a.hci.sendAclPkt(handle, attCID, response[:pos]); err != nil {
946948
return err
947949
}
950+
951+
return nil
948952
}
949953
}
950954

gap_ninafw.go

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -269,49 +269,69 @@ var defaultAdvertisement Advertisement
269269
// Advertisement encapsulates a single advertisement instance.
270270
type Advertisement struct {
271271
adapter *Adapter
272+
273+
localName []byte
274+
serviceUUIDs []UUID
275+
interval uint16
272276
}
273277

274278
// DefaultAdvertisement returns the default advertisement instance but does not
275279
// configure it.
276280
func (a *Adapter) DefaultAdvertisement() *Advertisement {
277281
if defaultAdvertisement.adapter == nil {
278282
defaultAdvertisement.adapter = a
279-
280-
a.AddService(
281-
&Service{
282-
UUID: ServiceUUIDGenericAccess,
283-
Characteristics: []CharacteristicConfig{
284-
{
285-
UUID: CharacteristicUUIDDeviceName,
286-
Flags: CharacteristicReadPermission,
287-
},
288-
{
289-
UUID: CharacteristicUUIDAppearance,
290-
Flags: CharacteristicReadPermission,
291-
},
292-
},
293-
})
294-
a.AddService(
295-
&Service{
296-
UUID: ServiceUUIDGenericAttribute,
297-
Characteristics: []CharacteristicConfig{
298-
{
299-
UUID: CharacteristicUUIDServiceChanged,
300-
Flags: CharacteristicIndicatePermission,
301-
},
302-
},
303-
})
304283
}
305284

306285
return &defaultAdvertisement
307286
}
308287

309288
// Configure this advertisement.
310289
func (a *Advertisement) Configure(options AdvertisementOptions) error {
311-
// uint8_t type = (_connectable) ? 0x00 : (_localName ? 0x02 : 0x03);
290+
switch {
291+
case options.LocalName != "":
292+
a.localName = []byte(options.LocalName)
293+
default:
294+
a.localName = []byte("TinyGo")
295+
}
296+
297+
a.serviceUUIDs = append([]UUID{}, options.ServiceUUIDs...)
298+
a.interval = uint16(options.Interval)
299+
300+
a.adapter.AddService(
301+
&Service{
302+
UUID: ServiceUUIDGenericAccess,
303+
Characteristics: []CharacteristicConfig{
304+
{
305+
UUID: CharacteristicUUIDDeviceName,
306+
Flags: CharacteristicReadPermission,
307+
Value: a.localName,
308+
},
309+
{
310+
UUID: CharacteristicUUIDAppearance,
311+
Flags: CharacteristicReadPermission,
312+
},
313+
},
314+
})
315+
a.adapter.AddService(
316+
&Service{
317+
UUID: ServiceUUIDGenericAttribute,
318+
Characteristics: []CharacteristicConfig{
319+
{
320+
UUID: CharacteristicUUIDServiceChanged,
321+
Flags: CharacteristicIndicatePermission,
322+
},
323+
},
324+
})
325+
326+
return nil
327+
}
328+
329+
// Start advertisement. May only be called after it has been configured.
330+
func (a *Advertisement) Start() error {
331+
// uint8_t type = (_connectable) ? 0x00 : (_localName ? 0x02 : 0x03);
312332
typ := uint8(0x00)
313333

314-
if err := a.adapter.hci.leSetAdvertisingParameters(uint16(options.Interval), uint16(options.Interval),
334+
if err := a.adapter.hci.leSetAdvertisingParameters(a.interval, a.interval,
315335
typ, 0x00, 0x00, [6]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 0x07, 0); err != nil {
316336
return err
317337
}
@@ -325,8 +345,8 @@ func (a *Advertisement) Configure(options AdvertisementOptions) error {
325345
advertisingDataLen += 3
326346

327347
// TODO: handle multiple service UUIDs
328-
if len(options.ServiceUUIDs) > 0 {
329-
uuid := options.ServiceUUIDs[0]
348+
if len(a.serviceUUIDs) > 0 {
349+
uuid := a.serviceUUIDs[0]
330350
var sz uint8
331351

332352
switch {
@@ -355,23 +375,22 @@ func (a *Advertisement) Configure(options AdvertisementOptions) error {
355375
scanResponseDataLen := uint8(0)
356376

357377
switch {
358-
case len(options.LocalName) > 29:
378+
case len(a.localName) > 29:
359379
scanResponseData[1] = 0x08
360380
scanResponseData[0] = 1 + 29
361-
copy(scanResponseData[2:], options.LocalName[:29])
381+
copy(scanResponseData[2:], a.localName[:29])
362382
scanResponseDataLen = 31
363-
case len(options.LocalName) > 0:
383+
case len(a.localName) > 0:
364384
scanResponseData[1] = 0x09
365-
scanResponseData[0] = uint8(1 + len(options.LocalName))
366-
copy(scanResponseData[2:], options.LocalName)
367-
scanResponseDataLen = uint8(2 + len(options.LocalName))
385+
scanResponseData[0] = uint8(1 + len(a.localName))
386+
copy(scanResponseData[2:], a.localName)
387+
scanResponseDataLen = uint8(2 + len(a.localName))
368388
}
369389

370-
return a.adapter.hci.leSetScanResponseData(scanResponseData[:scanResponseDataLen])
371-
}
390+
if err := a.adapter.hci.leSetScanResponseData(scanResponseData[:scanResponseDataLen]); err != nil {
391+
return err
392+
}
372393

373-
// Start advertisement. May only be called after it has been configured.
374-
func (a *Advertisement) Start() error {
375394
if err := a.adapter.hci.leSetAdvertiseEnable(true); err != nil {
376395
return err
377396
}

gatts_ninafw.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ func (a *Adapter) AddService(service *Service) error {
4242
endHandle = a.att.addLocalAttribute(attributeTypeDescriptor, charHandle, shortUUID(gattClientCharacteristicConfigUUID).UUID(), CharacteristicReadPermission|CharacteristicWritePermission, []byte{0, 0})
4343
}
4444

45-
if service.Characteristics[i].Handle != nil {
46-
service.Characteristics[i].Handle.adapter = a
47-
service.Characteristics[i].Handle.handle = valueHandle
48-
service.Characteristics[i].Handle.permissions = service.Characteristics[i].Flags
45+
if service.Characteristics[i].Handle == nil {
46+
service.Characteristics[i].Handle = &Characteristic{}
47+
}
48+
49+
service.Characteristics[i].Handle.adapter = a
50+
service.Characteristics[i].Handle.handle = valueHandle
51+
service.Characteristics[i].Handle.permissions = service.Characteristics[i].Flags
52+
if len(service.Characteristics[i].Value) > 0 {
4953
service.Characteristics[i].Handle.value = service.Characteristics[i].Value
5054
}
5155

0 commit comments

Comments
 (0)