Skip to content

Commit 3fc493d

Browse files
committed
Jira 913 BLESubscribed, BLEUnsubscribed Event Handlers are not called
1. Implement the subscribe changed event notify feature. 2. Fix unsubscribe return wrong value 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 6d37b45 commit 3fc493d

File tree

4 files changed

+121
-5
lines changed

4 files changed

+121
-5
lines changed

Diff for: libraries/CurieBLE/src/internal/BLECallbacks.cpp

+86
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,
@@ -237,6 +242,60 @@ void bleConnectEventHandler(bt_conn_t *conn,
237242
p->handleConnectEvent(conn, err);
238243
}
239244

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

241300
void bleDisconnectEventHandler(bt_conn_t *conn,
242301
uint8_t reason,
@@ -245,6 +304,7 @@ void bleDisconnectEventHandler(bt_conn_t *conn,
245304
BLEDeviceManager* p = (BLEDeviceManager*)param;
246305

247306
pr_info(LOG_MODULE_BLE, "Connect lost. Reason: %d", reason);
307+
bt_gatt_foreach_attr(0x0001, 0xffff, ble_gatt_disconnected_cb, conn);
248308

249309
p->handleDisconnectEvent(conn, reason);
250310
}
@@ -283,3 +343,29 @@ void ble_on_write_no_rsp_complete(struct bt_conn *conn, uint8_t err,
283343
BLECharacteristicImp::writeResponseReceived(conn, err, data);
284344
}
285345

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

Diff for: libraries/CurieBLE/src/internal/BLECallbacks.h

+6
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

Diff for: libraries/CurieBLE/src/internal/BLECharacteristicImp.cpp

+28-4
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,13 @@ bool BLECharacteristicImp::unsubscribe(void)
433433
// Enable CCCD to allow peripheral send Notification/Indication
434434
retval = bt_gatt_unsubscribe(conn, &_sub_params);
435435
bt_conn_unref(conn);
436-
if (0 == retval)
436+
if (0 != retval)
437437
{
438-
_subscribed = false;
438+
return false;
439439
}
440-
return _subscribed;
440+
441+
_subscribed = false;
442+
return true;
441443
}
442444

443445
bool BLECharacteristicImp::subscribe(void)
@@ -802,7 +804,7 @@ int BLECharacteristicImp::updateProfile(bt_gatt_attr_t *attr_start, int& index)
802804
start->uuid = this->getClientCharacteristicConfigUuid();
803805
start->perm = BT_GATT_PERM_READ | BT_GATT_PERM_WRITE;
804806
start->read = bt_gatt_attr_read_ccc;
805-
start->write = bt_gatt_attr_write_ccc;
807+
start->write = profile_gatt_attr_write_ccc;
806808
start->user_data = this->getCccCfg();
807809

808810
pr_info(LOG_MODULE_BLE, "cccd-%p", start);
@@ -1070,4 +1072,26 @@ uint8_t BLECharacteristicImp::discoverResponseProc(bt_conn_t *conn,
10701072
return retVal;
10711073
}
10721074

1075+
void BLECharacteristicImp::cccdValueChanged()
1076+
{
1077+
1078+
enum BLECharacteristicEvent event = BLEUnsubscribed;
1079+
if (subscribed())
1080+
{
1081+
event = BLESubscribed;
1082+
}
1083+
1084+
if (_event_handlers[event])
1085+
{
1086+
BLECharacteristic chrcTmp(this, &_ble_device);
1087+
_event_handlers[event](_ble_device, chrcTmp);
1088+
}
1089+
1090+
if (_oldevent_handlers[event])
1091+
{
1092+
BLECharacteristic chrcTmp(this, &_ble_device);
1093+
BLECentral central(_ble_device);
1094+
_oldevent_handlers[event](central, chrcTmp);
1095+
}
1096+
}
10731097

Diff for: libraries/CurieBLE/src/internal/BLECharacteristicImp.h

+1-1
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)