Skip to content

Commit 136f463

Browse files
committed
add CCCD Write callback for BLECharacteristic
1 parent a5a409d commit 136f463

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

libraries/Bluefruit52Lib/src/BLECharacteristic.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@
3838

3939
void BLECharacteristic::init(void)
4040
{
41-
_is_temp = false;
41+
_is_temp = false;
42+
4243
varclr(&_properties);
4344
_descriptor = NULL;
44-
_max_len = BLE_GATTS_VAR_ATTR_LEN_MAX;
45+
_max_len = BLE_GATTS_VAR_ATTR_LEN_MAX;
4546

4647
varclr(&_report_ref_desc);
4748

@@ -60,7 +61,8 @@ void BLECharacteristic::init(void)
6061

6162
_rd_authorize_cb = NULL;
6263
_wr_authorize_cb = NULL;
63-
_wr_cb = NULL;
64+
_wr_cb = NULL;
65+
_cccd_wr_cb = NULL;
6466
}
6567

6668
BLECharacteristic::BLECharacteristic(void)
@@ -136,6 +138,11 @@ void BLECharacteristic::setWriteCallback(write_cb_t fp)
136138
_wr_cb = fp;
137139
}
138140

141+
void BLECharacteristic::setCccdWriteCallback(write_cb_t fp)
142+
{
143+
_cccd_wr_cb = fp;
144+
}
145+
139146
void BLECharacteristic::setReadAuthorizeCallback(read_authorize_cb_t fp)
140147
{
141148
_attr_meta.rd_auth = (fp ? 1 : 0);
@@ -274,8 +281,10 @@ err_t BLECharacteristic::start(void)
274281

275282
// Currently Only register to Bluefruit when having callback support
276283
// And The Characteristic must not be temporary memory aka local variable
284+
// Or Properties is Notify and/or Indicate for saving CCCD for bonded connection
277285
if ( !_is_temp &&
278-
(_rd_authorize_cb || _wr_authorize_cb || _wr_cb || _properties.notify || _properties.indicate) )
286+
(_rd_authorize_cb || _wr_authorize_cb || _wr_cb || _cccd_wr_cb ||
287+
_properties.notify || _properties.indicate) )
279288
{
280289
(void) Bluefruit._registerCharacteristic(this);
281290
}
@@ -318,9 +327,16 @@ void BLECharacteristic::eventHandler(ble_evt_t* event)
318327
{
319328
ble_gatts_evt_write_t* request = (ble_gatts_evt_write_t*) &event->evt.gatts_evt.params.write;
320329

321-
if ( _wr_cb && (uuid == request->uuid))
330+
// Value write
331+
if ( _wr_cb && (request->handle == _handles.value_handle))
332+
{
333+
_wr_cb(*this, request);
334+
}
335+
336+
// CCCD write
337+
if ( _cccd_wr_cb && (request->handle == _handles.cccd_handle) )
322338
{
323-
_wr_cb(*this, &event->evt.gatts_evt.params.write);
339+
_cccd_wr_cb(*this, request);
324340
}
325341
}
326342
break;

libraries/Bluefruit52Lib/src/BLECharacteristic.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ class BLECharacteristic
9999
// Callback pointer
100100
read_authorize_cb_t _rd_authorize_cb;
101101
write_authorize_cb_t _wr_authorize_cb;
102+
102103
write_cb_t _wr_cb;
104+
write_cb_t _cccd_wr_cb;
103105

104106
void init(void);
105107
void eventHandler(ble_evt_t* event);
@@ -135,6 +137,8 @@ class BLECharacteristic
135137

136138
// Callback
137139
void setWriteCallback(write_cb_t fp);
140+
void setCccdWriteCallback(write_cb_t fp);
141+
138142
void setReadAuthorizeCallback(read_authorize_cb_t fp);
139143
void setWriteAuthorizeCallbak(write_authorize_cb_t fp);
140144

0 commit comments

Comments
 (0)