Skip to content

Commit f22f309

Browse files
committed
Jira 913 BLESubscribed, BLEUnsubscribed Event Handlers are not called
1. Implement the subscribe changed event notify feature. Changed files: BLECallbacks.cpp - Add subscribe changed handler and disconnect process BLECallbacks.h - Callback declaration BLECharacteristicImp.cpp - Add the event changed handler BLECharacteristicImp.h - Handler declaration
1 parent ac2fe83 commit f22f309

File tree

4 files changed

+116
-2
lines changed

4 files changed

+116
-2
lines changed

libraries/CurieBLE/src/internal/BLECallbacks.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
#include "BLEDeviceManager.h"
2828
#include "BLEProfileManager.h"
2929

30+
#include "BLECallbacks.h"
31+
32+
#include <atomic.h>
33+
#include "../src/services/ble/conn_internal.h"
34+
3035
// GATT Server Only
3136
ssize_t profile_read_process(bt_conn_t *conn,
3237
const bt_gatt_attr_t *attr,
@@ -233,6 +238,60 @@ void bleConnectEventHandler(bt_conn_t *conn,
233238
p->handleConnectEvent(conn, err);
234239
}
235240

241+
static uint8_t ble_gatt_disconnected_cb(const struct bt_gatt_attr *attr, void *user_data)
242+
{
243+
struct bt_conn *conn = (struct bt_conn *)user_data;
244+
struct _bt_gatt_ccc *ccc;
245+
size_t i;
246+
247+
/* Check attribute user_data must be of type struct _bt_gatt_ccc */
248+
if (attr->write != profile_gatt_attr_write_ccc) {
249+
return BT_GATT_ITER_CONTINUE;
250+
}
251+
252+
ccc = (struct _bt_gatt_ccc *)attr->user_data;
253+
/* If already disabled skip */
254+
if (!ccc->value) {
255+
return BT_GATT_ITER_CONTINUE;
256+
}
257+
258+
for (i = 0; i < ccc->cfg_len; i++)
259+
{
260+
/* Ignore configurations with disabled value */
261+
if (!ccc->cfg[i].value)
262+
{
263+
continue;
264+
}
265+
266+
if (bt_addr_le_cmp(&conn->le.dst, &ccc->cfg[i].peer))
267+
{
268+
struct bt_conn *tmp;
269+
270+
/* Skip if there is another peer connected */
271+
tmp = bt_conn_lookup_addr_le(&ccc->cfg[i].peer);
272+
if (tmp) {
273+
if (tmp->state == BT_CONN_CONNECTED) {
274+
bt_conn_unref(tmp);
275+
return BT_GATT_ITER_CONTINUE;
276+
}
277+
278+
bt_conn_unref(tmp);
279+
}
280+
}
281+
}
282+
283+
/* Reset value while disconnected */
284+
memset(&ccc->value, 0, sizeof(ccc->value));
285+
286+
if (ccc->cfg_changed) {
287+
ccc->cfg_changed(ccc->value);
288+
}
289+
290+
pr_debug(LOG_MODULE_BLE, "ccc %p reseted", ccc);
291+
292+
return BT_GATT_ITER_CONTINUE;
293+
}
294+
236295

237296
void bleDisconnectEventHandler(bt_conn_t *conn,
238297
uint8_t reason,
@@ -241,6 +300,7 @@ void bleDisconnectEventHandler(bt_conn_t *conn,
241300
BLEDeviceManager* p = (BLEDeviceManager*)param;
242301

243302
pr_info(LOG_MODULE_BLE, "Connect lost. Reason: %d", reason);
303+
bt_gatt_foreach_attr(0x0001, 0xffff, ble_gatt_disconnected_cb, conn);
244304

245305
p->handleDisconnectEvent(conn, reason);
246306
}
@@ -279,3 +339,29 @@ void ble_on_write_no_rsp_complete(struct bt_conn *conn, uint8_t err,
279339
BLECharacteristicImp::writeResponseReceived(conn, err, data);
280340
}
281341

342+
ssize_t profile_gatt_attr_write_ccc(struct bt_conn *conn,
343+
const struct bt_gatt_attr *attr,
344+
const void *buf,
345+
uint16_t len,
346+
uint16_t offset)
347+
{
348+
struct _bt_gatt_ccc *ccc = (struct _bt_gatt_ccc *)attr->user_data;
349+
const uint16_t *data = (const uint16_t *)buf;
350+
bool cccdChanged = (ccc->value != *data);
351+
ssize_t retValue = bt_gatt_attr_write_ccc(conn, attr, buf, len, offset);
352+
if (cccdChanged)
353+
{
354+
// Find characteristic and do notification
355+
const struct bt_gatt_attr *attrChrc = attr - 1;
356+
BLEAttribute *bleattr = (BLEAttribute *)attrChrc->user_data;
357+
BLEAttributeType type = bleattr->type();
358+
pr_debug(LOG_MODULE_BLE, "The Attribute type:%d", type);
359+
if (BLETypeCharacteristic == type)
360+
{
361+
BLECharacteristicImp *blecharacteritic = (BLECharacteristicImp*)bleattr;
362+
blecharacteritic->cccdValueChanged();
363+
}
364+
}
365+
return retValue;
366+
}
367+

libraries/CurieBLE/src/internal/BLECallbacks.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,11 @@ uint8_t profile_characteristic_read_rsp_process(bt_conn_t *conn,
8989
const void *data,
9090
uint16_t length);
9191

92+
ssize_t profile_gatt_attr_write_ccc(struct bt_conn *conn,
93+
const struct bt_gatt_attr *attr,
94+
const void *buf,
95+
uint16_t len,
96+
uint16_t offset);
97+
9298
#endif
9399

libraries/CurieBLE/src/internal/BLECharacteristicImp.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ int BLECharacteristicImp::updateProfile(bt_gatt_attr_t *attr_start, int& index)
856856
start->uuid = this->getClientCharacteristicConfigUuid();
857857
start->perm = BT_GATT_PERM_READ | BT_GATT_PERM_WRITE;
858858
start->read = bt_gatt_attr_read_ccc;
859-
start->write = bt_gatt_attr_write_ccc;
859+
start->write = profile_gatt_attr_write_ccc;
860860
start->user_data = this->getCccCfg();
861861

862862
pr_info(LOG_MODULE_BLE, "cccd-%p", start);
@@ -1124,4 +1124,26 @@ uint8_t BLECharacteristicImp::discoverResponseProc(bt_conn_t *conn,
11241124
return retVal;
11251125
}
11261126

1127+
void BLECharacteristicImp::cccdValueChanged()
1128+
{
1129+
1130+
enum BLECharacteristicEvent event = BLEUnsubscribed;
1131+
if (subscribed())
1132+
{
1133+
event = BLESubscribed;
1134+
}
1135+
1136+
if (_event_handlers[event])
1137+
{
1138+
BLECharacteristic chrcTmp(this, &_ble_device);
1139+
_event_handlers[event](_ble_device, chrcTmp);
1140+
}
1141+
1142+
if (_oldevent_handlers[event])
1143+
{
1144+
BLECharacteristic chrcTmp(this, &_ble_device);
1145+
BLECentral central(_ble_device);
1146+
_oldevent_handlers[event](central, chrcTmp);
1147+
}
1148+
}
11271149

libraries/CurieBLE/src/internal/BLECharacteristicImp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class BLECharacteristicImp: public BLEAttribute{
172172
static void writeResponseReceived(struct bt_conn *conn,
173173
uint8_t err,
174174
const void *data);
175-
175+
void cccdValueChanged();
176176
int descriptorCount() const;
177177
uint8_t discoverResponseProc(bt_conn_t *conn,
178178
const bt_gatt_attr_t *attr,

0 commit comments

Comments
 (0)