Skip to content

Commit 6e5a9de

Browse files
committed
Add written and subscribed API's to BleCharacteristic, pass central into BleCharacteristic even handers, remove BleAck event for now
1 parent cd54545 commit 6e5a9de

File tree

5 files changed

+55
-19
lines changed

5 files changed

+55
-19
lines changed

libraries/CurieBle/examples/AutomationIO/AutomationIO.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ 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 digitalOutputCharWrittenEventCb(BleCharacteristic &characteristic)
159+
void digitalOutputCharWrittenEventCb(BleCentral &central, BleCharacteristic &characteristic)
160160
{
161161
for(unsigned int i = 0; i < ARRAY_SIZE(digitalOutputPins); i++) {
162162
if (&digitalOutputPins[i].characteristic == &characteristic) {
@@ -172,7 +172,7 @@ void digitalOutputCharWrittenEventCb(BleCharacteristic &characteristic)
172172
}
173173

174174
/* This function will be called when a connected remote peer sets a new value for an analog output characteristic */
175-
void analogOutputCharWrittenEventCb(BleCharacteristic &characteristic)
175+
void analogOutputCharWrittenEventCb(BleCentral &central, BleCharacteristic &characteristic)
176176
{
177177
for(unsigned int i = 0; i < ARRAY_SIZE(analogOutputPins); i++) {
178178
if (&analogOutputPins[i].characteristic == &characteristic) {

libraries/CurieBle/src/BleCharacteristic.cpp

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* by a remote client to enable/disable receipt of notifications/indications
3232
*/
3333
void
34-
_cccdEventHandler(BleDescriptor &cccd, BleDescriptorEvent event, void *arg)
34+
_cccdEventHandler(BleCentral& central, BleDescriptor &cccd, BleDescriptorEvent event, void *arg)
3535
{
3636
if (BLE_DESC_EVENT_WRITE == event) {
3737
BleStatus status;
@@ -47,11 +47,11 @@ _cccdEventHandler(BleDescriptor &cccd, BleDescriptorEvent event, void *arg)
4747

4848
if (ch->_notifyEnabled || ch->_indicateEnabled) {
4949
if (ch->_event_handlers[BleSubscribed]) {
50-
ch->_event_handlers[BleSubscribed](*ch);
50+
ch->_event_handlers[BleSubscribed](central, *ch);
5151
}
5252
} else {
5353
if (ch->_event_handlers[BleUnsubscribed]) {
54-
ch->_event_handlers[BleUnsubscribed](*ch);
54+
ch->_event_handlers[BleUnsubscribed](central, *ch);
5555
}
5656
}
5757
}
@@ -65,6 +65,7 @@ BleCharacteristic::BleCharacteristic(const char* uuid,
6565
{
6666
_initialised = false;
6767
_connected = false;
68+
_written = false;
6869
_notifyEnabled = false;
6970
_indicateEnabled = false;
7071

@@ -164,9 +165,6 @@ BleCharacteristic::_setValue(void)
164165
status = ble_client_gatts_send_notif_ind(_handles.value_handle, _data_len, _data, 0, _indicateEnabled);
165166
if (BLE_STATUS_SUCCESS != status)
166167
return status;
167-
168-
if (_indicateEnabled && _event_handlers[BleAcked])
169-
_event_handlers[BleAcked](*this);
170168
}
171169
}
172170

@@ -188,6 +186,23 @@ BleCharacteristic::setValue(const uint8_t value[], const uint16_t length)
188186
return _setValue();
189187
}
190188

189+
void
190+
BleCharacteristic::setValue(BleCentral& central, const uint8_t* value, uint16_t length)
191+
{
192+
if (length > _char_data.max_len) {
193+
length = _char_data.max_len;
194+
}
195+
196+
memcpy(_data, value, length);
197+
_data_len = length;
198+
199+
_written = true;
200+
201+
if (_event_handlers[BleUnsubscribed]) {
202+
_event_handlers[BleWritten](central, *this);
203+
}
204+
}
205+
191206
BleStatus
192207
BleCharacteristic::setValue(const String &str)
193208
{
@@ -388,6 +403,23 @@ BleCharacteristic::operator[] (int offset) const
388403
return _data[offset];
389404
}
390405

406+
407+
boolean_t
408+
BleCharacteristic::written()
409+
{
410+
boolean_t written = _written;
411+
412+
_written = false;
413+
414+
return written;
415+
}
416+
417+
boolean_t
418+
BleCharacteristic::subscribed()
419+
{
420+
return (_notifyEnabled || _indicateEnabled);
421+
}
422+
391423
void
392424
BleCharacteristic::setEventHandler(BleCharacteristicEvent event, BleCharacteristicEventHandler callback)
393425
{

libraries/CurieBle/src/BleCharacteristic.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define _BLE_CHARACTERISTIC_H_INCLUDED
2222

2323
#include "BleAttribute.h"
24+
#include "BleCentral.h"
2425
#include "BleCommon.h"
2526
#include "BleDescriptor.h"
2627

@@ -33,9 +34,8 @@ enum BleCharacteristicEvent {
3334
BleWritten = 0,
3435
BleSubscribed = 1,
3536
BleUnsubscribed = 2,
36-
BleAcked = 3,
3737

38-
BleCharacteristicEventLast = 4
38+
BleCharacteristicEventLast = 3
3939
};
4040

4141
/* Forward declaration needed for callback function prototype below */
@@ -44,7 +44,7 @@ class BleService;
4444
class BlePeripheral;
4545

4646
/** Function prototype for BLE Characteristic event callback */
47-
typedef void (*BleCharacteristicEventHandler)(BleCharacteristic &characteristic);
47+
typedef void (*BleCharacteristicEventHandler)(BleCentral &central, BleCharacteristic &characteristic);
4848

4949
enum BleProperty {
5050
// BleBroadcast = 0x01,
@@ -103,6 +103,9 @@ class BleCharacteristic : public BleAttribute {
103103
uint16_t valueLength() const;
104104
uint8_t operator[] (int offset) const;
105105

106+
boolean_t written();
107+
boolean_t subscribed();
108+
106109
/**
107110
* Provide a function to be called when events related to this Characteristic are raised
108111
*
@@ -111,6 +114,8 @@ class BleCharacteristic : public BleAttribute {
111114
*/
112115
void setEventHandler(BleCharacteristicEvent event, BleCharacteristicEventHandler callback);
113116

117+
void setValue(BleCentral& central, const uint8_t value[], uint16_t length);
118+
114119
private:
115120
/**
116121
* Add an optional User-Description descriptor
@@ -197,7 +202,7 @@ class BleCharacteristic : public BleAttribute {
197202
friend class BlePeripheral;
198203
friend class BleService;
199204
friend void blePeripheralGattsEventHandler(ble_client_gatts_event_t event, struct ble_gatts_evt_msg *event_data, void *param);
200-
friend void _cccdEventHandler(BleDescriptor &cccd, BleDescriptorEvent event, void *arg);
205+
friend void _cccdEventHandler(BleCentral &central, BleDescriptor &cccd, BleDescriptorEvent event, void *arg);
201206

202207
BleCharacteristic(const uint16_t maxLength,
203208
const uint8_t properties);
@@ -212,6 +217,7 @@ class BleCharacteristic : public BleAttribute {
212217

213218
boolean_t _initialised;
214219
boolean_t _connected;
220+
boolean_t _written;
215221
uint8_t _properties;
216222
BleCharacteristicEventHandler _event_handlers[BleCharacteristicEventLast];
217223

libraries/CurieBle/src/BleDescriptor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define _BLE_DESCRIPTOR_H_INCLUDED
2222

2323
#include "BleAttribute.h"
24+
#include "BleCentral.h"
2425
#include "BleCommon.h"
2526

2627
#include "internal/ble_client.h"
@@ -37,7 +38,7 @@ class BleCharacteristic;
3738
class BleDescriptor;
3839

3940
/** Function prototype for BLE Characteristic event callback */
40-
typedef void (*BleDescriptorEventCb)(BleDescriptor &descriptor, BleDescriptorEvent event, void *arg);
41+
typedef void (*BleDescriptorEventCb)(BleCentral& central, BleDescriptor &descriptor, BleDescriptorEvent event, void *arg);
4142

4243
/**
4344
* BLE GATT Descriptor class

libraries/CurieBle/src/BlePeripheral.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,19 @@ void
7272
blePeripheralGattsEventHandler(ble_client_gatts_event_t event, struct ble_gatts_evt_msg *event_data, void *param)
7373
{
7474
BlePeripheral *p = (BlePeripheral *)param;
75+
BleCentral c = p->central();
7576
BleCharacteristic *ch;
7677
BleDescriptor *desc;
7778

7879
switch (event) {
7980
case BLE_CLIENT_GATTS_EVENT_WRITE: {
8081
if ((ch = p->_matchCharacteristic(event_data->wr.attr_handle))) {
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);
85-
}
82+
ch->setValue(c, event_data->wr.data, event_data->wr.len);
8683
} else if ((desc = p->_matchDescriptor(event_data->wr.attr_handle))) {
8784
if (desc->_event_cb) {
8885
desc->_desc.length = event_data->wr.len > sizeof(desc->_data) ? sizeof(desc->_data) : event_data->wr.len;
8986
memcpy(desc->_data, event_data->wr.data, desc->_desc.length);
90-
desc->_event_cb(*desc, BLE_DESC_EVENT_WRITE, desc->_event_cb_arg);
87+
desc->_event_cb(c, *desc, BLE_DESC_EVENT_WRITE, desc->_event_cb_arg);
9188
}
9289
}
9390
}

0 commit comments

Comments
 (0)