gatts: implement write event for characteristics

This commit is contained in:
Ayke van Laethem 2019-11-09 13:55:34 +01:00
parent 1d44126ac9
commit 6e4cf6f8e0
No known key found for this signature in database
GPG key ID: E97FF5335DFDFDED
3 changed files with 48 additions and 4 deletions

View file

@ -57,8 +57,9 @@ func init() {
// Adapter is a dummy adapter: it represents the connection to the (only) // Adapter is a dummy adapter: it represents the connection to the (only)
// SoftDevice on the chip. // SoftDevice on the chip.
type Adapter struct { type Adapter struct {
isDefault bool isDefault bool
handler func(Event) handler func(Event)
charWriteHandlers []charWriteHandler
} }
// DefaultAdapter is an adapter to the default Bluetooth stack on a given // DefaultAdapter is an adapter to the default Bluetooth stack on a given
@ -123,6 +124,18 @@ func handleEvent() {
case C.BLE_GAP_EVT_DISCONNECTED: case C.BLE_GAP_EVT_DISCONNECTED:
handler(&DisconnectEvent{GAPEvent: gapEvent}) handler(&DisconnectEvent{GAPEvent: gapEvent})
} }
case id >= C.BLE_GATTS_EVT_BASE && id <= C.BLE_GATTS_EVT_LAST:
gattsEvent := eventBuf.evt.unionfield_gatts_evt()
switch id {
case C.BLE_GATTS_EVT_WRITE:
writeEvent := gattsEvent.params.unionfield_write()
len := writeEvent.len - writeEvent.offset
data := (*[255]byte)(unsafe.Pointer(&writeEvent.data[0]))[:len:len]
handler := DefaultAdapter.getCharWriteHandler(writeEvent.handle)
if handler != nil {
handler.callback(Connection(gattsEvent.conn_handle), int(writeEvent.offset), data)
}
}
} }
} }

View file

@ -22,8 +22,9 @@ type Characteristic struct {
type CharacteristicConfig struct { type CharacteristicConfig struct {
Handle *Characteristic Handle *Characteristic
UUID UUID
Value []byte Value []byte
Flags CharacteristicPermissions Flags CharacteristicPermissions
WriteEvent func(client Connection, offset int, value []byte)
} }
// Handle returns the numeric handle for this characteristic. This is used // Handle returns the numeric handle for this characteristic. This is used

View file

@ -55,6 +55,36 @@ func (a *Adapter) AddService(service *Service) error {
char.Handle.handle = handles.value_handle char.Handle.handle = handles.value_handle
char.Handle.permissions = char.Flags char.Handle.permissions = char.Flags
} }
if char.Flags.Write() && char.WriteEvent != nil {
a.charWriteHandlers = append(a.charWriteHandlers, charWriteHandler{
handle: handles.value_handle,
callback: char.WriteEvent,
})
}
} }
return makeError(errCode) return makeError(errCode)
} }
// charWriteHandler contains a handler->callback mapping for characteristic
// writes.
type charWriteHandler struct {
handle uint16
callback func(connection Connection, offset int, value []byte)
}
// getCharWriteHandler returns a characteristic write handler if one matches the
// handle, or nil otherwise.
func (a *Adapter) getCharWriteHandler(handle uint16) *charWriteHandler {
// Look through all handlers for a match.
// There is probably a way to do this more efficiently (with a hashmap for
// example) but the number of event handlers is likely low and improving
// this does not need an API change.
for i := range a.charWriteHandlers {
h := &a.charWriteHandlers[i]
if h.handle == handle {
// Handler found.
return h
}
}
return nil // not found
}