Skip to content

Commit 99ab222

Browse files
committed
BleCharacteristic event handler API's
1 parent d4efbb2 commit 99ab222

File tree

4 files changed

+57
-41
lines changed

4 files changed

+57
-41
lines changed

libraries/CurieBle/examples/AutomationIO/AutomationIO.ino

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -156,34 +156,34 @@ void blePeripheralDisconnectedEventCb(BleCentral &bleCentral)
156156
}
157157

158158
/* This function will be called when a connected remote peer sets a new value for a digital output characteristic */
159-
void digitalOutputCharEventCb(BleUnsignedCharCharacteristic &characteristic, BleCharacteristicEvent event, void *arg)
159+
void digitalOutputCharWrittenEventCb(BleCharacteristic &characteristic)
160160
{
161-
unsigned pin = (unsigned)arg;
162-
unsigned char val;
163-
164-
if (BLE_CHAR_EVENT_WRITE == event) {
165-
/* The remote client has updated the value for this pin, get the current value */
166-
val = characteristic.value();
167-
/* Update the state of the pin to reflect the new value */
168-
digitalWrite(pin, VAL_TO_DIGITAL_PIN_STATE(pin, val));
169-
} else
170-
LOG_SERIAL.println("Got UNKNOWN characteristic event");
161+
for(unsigned int i = 0; i < ARRAY_SIZE(digitalOutputPins); i++) {
162+
if (&digitalOutputPins[i].characteristic == &characteristic) {
163+
unsigned pin = digitalOutputPins[i].pin;
164+
/* The remote client has updated the value for this pin, get the current value */
165+
unsigned char val = digitalOutputPins[i].characteristic.value();
166+
167+
/* Update the state of the pin to reflect the new value */
168+
digitalWrite(pin, VAL_TO_DIGITAL_PIN_STATE(pin, val));
169+
break;
170+
}
171+
}
171172
}
172173

173174
/* This function will be called when a connected remote peer sets a new value for an analog output characteristic */
174-
void analogOutputCharEventCb(BleUnsignedShortCharacteristic &characteristic, BleCharacteristicEvent event, void *arg)
175+
void analogOutputCharWrittenEventCb(BleCharacteristic &characteristic)
175176
{
176-
unsigned pin = (unsigned)arg;
177-
unsigned short val;
178-
179-
if (BLE_CHAR_EVENT_WRITE == event) {
180-
/* The remote client has updated the value for this pin, get the current value */
181-
val = characteristic.value();
182-
/* Update the state of the pin to reflect the new value */
183-
analogWrite(pin, val);
177+
for(unsigned int i = 0; i < ARRAY_SIZE(analogOutputPins); i++) {
178+
if (&analogOutputPins[i].characteristic == &characteristic) {
179+
unsigned pin = analogOutputPins[i].pin;
180+
/* The remote client has updated the value for this pin, get the current value */
181+
unsigned short val = analogOutputPins[i].characteristic.value();
182+
/* Update the state of the pin to reflect the new value */
183+
analogWrite(pin, val);
184+
break;
185+
}
184186
}
185-
else
186-
LOG_SERIAL.println("Got UNKNOWN characteristic event");
187187
}
188188

189189
void setup() {
@@ -230,7 +230,7 @@ void setup() {
230230
/* Add the characteristic for this pin */
231231
CHECK_STATUS(blePeripheral.addAttribute(pin->characteristic));
232232
/* Add a callback to be triggered if the remote device updates the value for this pin */
233-
pin->characteristic.setEventCallback((void (*)(BleCharacteristic&, BleCharacteristicEvent, void*))digitalOutputCharEventCb, (void*)pin->pin);
233+
pin->characteristic.setEventHandler(BleWritten, digitalOutputCharWrittenEventCb);
234234
/* Add a number_of_digitals descriptor for this characteristic */
235235
CHECK_STATUS(blePeripheral.addAttribute(pin->userDescription));
236236
CHECK_STATUS(blePeripheral.addAttribute(pin->presentationFormat));
@@ -260,7 +260,7 @@ void setup() {
260260
CHECK_STATUS(blePeripheral.addAttribute(pin->userDescription));
261261
CHECK_STATUS(blePeripheral.addAttribute(pin->presentationFormat));
262262
/* Add a callback to be triggered if the remote device updates the value for this pin */
263-
pin->characteristic.setEventCallback((void (*)(BleCharacteristic&, BleCharacteristicEvent, void*))analogOutputCharEventCb, (void*)pin->pin);
263+
pin->characteristic.setEventHandler(BleWritten, analogOutputCharWrittenEventCb);
264264
}
265265

266266
/* Now activate the BLE device. It will start continuously transmitting BLE

libraries/CurieBle/src/BleCharacteristic.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ _cccdEventHandler(BleDescriptor &cccd, BleDescriptorEvent event, void *arg)
4444

4545
ch->_notifyEnabled = (cccdVal & BLE_CCCD_NOTIFY_EN_MASK) ? true : false;
4646
ch->_indicateEnabled = (cccdVal & BLE_CCCD_INDICATE_EN_MASK) ? true : false;
47+
48+
if (ch->_notifyEnabled || ch->_indicateEnabled) {
49+
if (ch->_event_handlers[BleSubscribed]) {
50+
ch->_event_handlers[BleSubscribed](*ch);
51+
}
52+
} else {
53+
if (ch->_event_handlers[BleUnsubscribed]) {
54+
ch->_event_handlers[BleUnsubscribed](*ch);
55+
}
56+
}
4757
}
4858
}
4959

@@ -83,6 +93,8 @@ BleCharacteristic::BleCharacteristic(const char* uuid,
8393
_char_data.init_len = _data_len;
8494
_char_data.max_len = maxLength > BLE_MAX_ATTR_DATA_LEN ? BLE_MAX_ATTR_DATA_LEN : maxLength;
8595
_char_data.p_value = _data;
96+
97+
memset(_event_handlers, 0, sizeof(_event_handlers));
8698
}
8799

88100
BleCharacteristic::BleCharacteristic(const char* uuid,
@@ -153,8 +165,8 @@ BleCharacteristic::_setValue(void)
153165
if (BLE_STATUS_SUCCESS != status)
154166
return status;
155167

156-
if (_indicateEnabled && _event_cb)
157-
_event_cb(*this, BLE_CHAR_EVENT_INDICATION_ACK, _event_cb_arg);
168+
if (_indicateEnabled && _event_handlers[BleAcked])
169+
_event_handlers[BleAcked](*this);
158170
}
159171
}
160172

@@ -370,18 +382,19 @@ BleCharacteristic::valueLength() const
370382
return _data_len;
371383
}
372384

373-
uint8_t BleCharacteristic::operator[] (int offset) const
385+
uint8_t
386+
BleCharacteristic::operator[] (int offset) const
374387
{
375388
return _data[offset];
376389
}
377390

378391
void
379-
BleCharacteristic::setEventCallback(BleCharacteristicEventCb callback,
380-
void *arg)
392+
BleCharacteristic::setEventHandler(BleCharacteristicEvent event, BleCharacteristicEventHandler callback)
381393
{
382394
noInterrupts();
383-
_event_cb = callback; /* callback disabled if NULL */
384-
_event_cb_arg = arg;
395+
if (event < sizeof(_event_handlers)) {
396+
_event_handlers[event] = callback;
397+
}
385398
interrupts();
386399
}
387400

libraries/CurieBle/src/BleCharacteristic.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@
3030
* BLE Characteristic Events
3131
*/
3232
enum BleCharacteristicEvent {
33-
BLE_CHAR_EVENT_WRITE = 0,
34-
BLE_CHAR_EVENT_INDICATION_ACK,
33+
BleWritten = 0,
34+
BleSubscribed = 1,
35+
BleUnsubscribed = 2,
36+
BleAcked = 3,
37+
38+
BleCharacteristicEventLast = 4
3539
};
3640

3741
/* Forward declaration needed for callback function prototype below */
@@ -40,7 +44,7 @@ class BleService;
4044
class BlePeripheral;
4145

4246
/** Function prototype for BLE Characteristic event callback */
43-
typedef void (*BleCharacteristicEventCb)(BleCharacteristic &characteristic, BleCharacteristicEvent event, void *arg);
47+
typedef void (*BleCharacteristicEventHandler)(BleCharacteristic &characteristic);
4448

4549
enum BleProperty {
4650
// BleBroadcast = 0x01,
@@ -105,7 +109,7 @@ class BleCharacteristic : public BleAttribute {
105109
* @param callback Pointer to callback function to invoke when an event occurs.
106110
* @param arg [Optional] Opaque argument which will be passed in the callback.
107111
*/
108-
void setEventCallback(BleCharacteristicEventCb callback, void *arg = NULL);
112+
void setEventHandler(BleCharacteristicEvent event, BleCharacteristicEventHandler callback);
109113

110114
private:
111115
/**
@@ -209,8 +213,7 @@ class BleCharacteristic : public BleAttribute {
209213
boolean_t _initialised;
210214
boolean_t _connected;
211215
uint8_t _properties;
212-
BleCharacteristicEventCb _event_cb;
213-
void *_event_cb_arg;
216+
BleCharacteristicEventHandler _event_handlers[BleCharacteristicEventLast];
214217

215218
uint16_t _svc_handle;
216219
struct ble_gatts_characteristic _char_data;

libraries/CurieBle/src/BlePeripheral.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ blePeripheralGattsEventHandler(ble_client_gatts_event_t event, struct ble_gatts_
7878
switch (event) {
7979
case BLE_CLIENT_GATTS_EVENT_WRITE: {
8080
if ((ch = p->_matchCharacteristic(event_data->wr.attr_handle))) {
81-
if (ch->_event_cb) {
82-
ch->_data_len = event_data->wr.len > ch->_char_data.max_len ? ch->_char_data.max_len : event_data->wr.len;
83-
memcpy(ch->_data, event_data->wr.data, ch->_data_len);
84-
ch->_event_cb(*ch, BLE_CHAR_EVENT_WRITE, ch->_event_cb_arg);
81+
ch->_data_len = event_data->wr.len > ch->_char_data.max_len ? ch->_char_data.max_len : event_data->wr.len;
82+
memcpy(ch->_data, event_data->wr.data, ch->_data_len);
83+
if (ch->_event_handlers[BleWritten]) {
84+
ch->_event_handlers[BleWritten](*ch);
8585
}
8686
} else if ((desc = p->_matchDescriptor(event_data->wr.attr_handle))) {
8787
if (desc->_event_cb) {

0 commit comments

Comments
 (0)