Compare commits
1 Commits
release
...
safe-notif
Author | SHA1 | Date | |
---|---|---|---|
![]() |
9f0114a77a |
@ -240,11 +240,10 @@ func handleEvent() {
|
|||||||
// Find the callback and call it (if there is any).
|
// Find the callback and call it (if there is any).
|
||||||
for _, callbackInfo := range gattcNotificationCallbacks {
|
for _, callbackInfo := range gattcNotificationCallbacks {
|
||||||
if callbackInfo.valueHandle == hvxEvent.handle && callbackInfo.connectionHandle == gattcEvent.conn_handle {
|
if callbackInfo.valueHandle == hvxEvent.handle && callbackInfo.connectionHandle == gattcEvent.conn_handle {
|
||||||
// Create a Go slice from the data, to pass to the
|
|
||||||
// callback.
|
|
||||||
data := (*[255]byte)(unsafe.Pointer(&hvxEvent.data[0]))[:hvxEvent.len:hvxEvent.len]
|
|
||||||
if callbackInfo.callback != nil {
|
if callbackInfo.callback != nil {
|
||||||
callbackInfo.callback(data)
|
// copy to Go slice from the data, to pass to the callback.
|
||||||
|
copy(callbackInfo.data, (*[255]byte)(unsafe.Pointer(&hvxEvent.data[0]))[:hvxEvent.len:hvxEvent.len])
|
||||||
|
callbackInfo.callback(callbackInfo.data[:hvxEvent.len])
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
22
gattc_sd.go
22
gattc_sd.go
@ -341,6 +341,7 @@ type gattcNotificationCallback struct {
|
|||||||
connectionHandle uint16
|
connectionHandle uint16
|
||||||
valueHandle uint16 // may be 0 if the slot is empty
|
valueHandle uint16 // may be 0 if the slot is empty
|
||||||
callback func([]byte)
|
callback func([]byte)
|
||||||
|
data []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// List of notification callbacks for the current connection. Some slots may be
|
// List of notification callbacks for the current connection. Some slots may be
|
||||||
@ -348,6 +349,15 @@ type gattcNotificationCallback struct {
|
|||||||
// notification callbacks.
|
// notification callbacks.
|
||||||
var gattcNotificationCallbacks []gattcNotificationCallback
|
var gattcNotificationCallbacks []gattcNotificationCallback
|
||||||
|
|
||||||
|
func (c DeviceCharacteristic) newgattcNotificationCallback(cb func([]byte)) gattcNotificationCallback {
|
||||||
|
return gattcNotificationCallback{
|
||||||
|
connectionHandle: c.connectionHandle,
|
||||||
|
valueHandle: c.valueHandle,
|
||||||
|
data: make([]byte, 255),
|
||||||
|
callback: cb,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// EnableNotifications enables notifications in the Client Characteristic
|
// EnableNotifications enables notifications in the Client Characteristic
|
||||||
// Configuration Descriptor (CCCD). This means that most peripherals will send a
|
// Configuration Descriptor (CCCD). This means that most peripherals will send a
|
||||||
// notification with a new value every time the value of the characteristic
|
// notification with a new value every time the value of the characteristic
|
||||||
@ -376,11 +386,7 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
|
|||||||
for i, callbackInfo := range gattcNotificationCallbacks {
|
for i, callbackInfo := range gattcNotificationCallbacks {
|
||||||
// Check for empty slots.
|
// Check for empty slots.
|
||||||
if callbackInfo.valueHandle == 0 {
|
if callbackInfo.valueHandle == 0 {
|
||||||
gattcNotificationCallbacks[i] = gattcNotificationCallback{
|
gattcNotificationCallbacks[i] = c.newgattcNotificationCallback(callback)
|
||||||
connectionHandle: c.connectionHandle,
|
|
||||||
valueHandle: c.valueHandle,
|
|
||||||
callback: callback,
|
|
||||||
}
|
|
||||||
updatedCallback = true
|
updatedCallback = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -393,11 +399,7 @@ func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) err
|
|||||||
if !updatedCallback {
|
if !updatedCallback {
|
||||||
// The append call is done outside of a cricital section to avoid GC in
|
// The append call is done outside of a cricital section to avoid GC in
|
||||||
// a critical section.
|
// a critical section.
|
||||||
callbackList := append(gattcNotificationCallbacks, gattcNotificationCallback{
|
callbackList := append(gattcNotificationCallbacks, c.newgattcNotificationCallback(callback))
|
||||||
connectionHandle: c.connectionHandle,
|
|
||||||
valueHandle: c.valueHandle,
|
|
||||||
callback: callback,
|
|
||||||
})
|
|
||||||
mask := DisableInterrupts()
|
mask := DisableInterrupts()
|
||||||
gattcNotificationCallbacks = callbackList
|
gattcNotificationCallbacks = callbackList
|
||||||
RestoreInterrupts(mask)
|
RestoreInterrupts(mask)
|
||||||
|
Loading…
Reference in New Issue
Block a user