Skip to content

Commit e087206

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 BLECallbacks.h - Callback declaration BLECharacteristicImp.cpp - Add the event changed handler BLECharacteristicImp.h - Handler declaration gatt.c - Modify the callback parameter gatt.h - Modify the structure Fix
1 parent 6d37b45 commit e087206

File tree

7 files changed

+58
-16
lines changed

7 files changed

+58
-16
lines changed

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

+16-4
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,
@@ -123,8 +128,6 @@ uint8_t profile_notify_process (bt_conn_t *conn,
123128
bt_gatt_subscribe_params_t *params,
124129
const void *data, uint16_t length)
125130
{
126-
//BLEPeripheralHelper* peripheral = BLECentralRole::instance()->peripheral(conn);// Find peripheral by bt_conn
127-
//BLEAttribute* notifyatt = peripheral->attribute(params); // Find attribute by params
128131
BLECharacteristicImp* chrc = NULL;
129132
BLEDevice bleDevice(bt_conn_get_dst(conn));
130133
chrc = BLEProfileManager::instance()->characteristic(bleDevice, params->value_handle);
@@ -146,7 +149,6 @@ uint8_t profile_discover_process(bt_conn_t *conn,
146149
uint8_t ret = BT_GATT_ITER_STOP;
147150
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
148151
ret = BLEProfileManager::instance()->discoverResponseProc(conn, attr, params);
149-
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
150152
return ret;
151153
}
152154

@@ -237,7 +239,6 @@ void bleConnectEventHandler(bt_conn_t *conn,
237239
p->handleConnectEvent(conn, err);
238240
}
239241

240-
241242
void bleDisconnectEventHandler(bt_conn_t *conn,
242243
uint8_t reason,
243244
void *param)
@@ -283,3 +284,14 @@ void ble_on_write_no_rsp_complete(struct bt_conn *conn, uint8_t err,
283284
BLECharacteristicImp::writeResponseReceived(conn, err, data);
284285
}
285286

287+
void prfile_cccd_cfg_changed(void *user_data, uint16_t value)
288+
{
289+
if (NULL == user_data)
290+
return;
291+
pr_debug(LOG_MODULE_BLE, "%s-%d: ccc userdata %p", __FUNCTION__, __LINE__, user_data);
292+
293+
BLECharacteristicImp *blecharacteritic = (BLECharacteristicImp *)user_data;
294+
blecharacteritic->cccdValueChanged();
295+
}
296+
297+

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

+2
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,7 @@ uint8_t profile_characteristic_read_rsp_process(bt_conn_t *conn,
8989
const void *data,
9090
uint16_t length);
9191

92+
void prfile_cccd_cfg_changed(void *user_data, uint16_t value);
93+
9294
#endif
9395

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

+35-3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ BLECharacteristicImp::BLECharacteristicImp(const bt_uuid_t* uuid,
7171

7272
_ccc_value.cfg = &_ccc_cfg;
7373
_ccc_value.cfg_len = 1;
74+
_ccc_value.user_data = (void *)this;
75+
_ccc_value.cfg_changed = prfile_cccd_cfg_changed;
76+
_ccc_value.value = 0;
77+
7478
if (BLERead & properties)
7579
{
7680
_gatt_chrc.properties |= BT_GATT_CHRC_READ;
@@ -139,6 +143,10 @@ BLECharacteristicImp::BLECharacteristicImp(BLECharacteristic& characteristic,
139143

140144
_ccc_value.cfg = &_ccc_cfg;
141145
_ccc_value.cfg_len = 1;
146+
_ccc_value.user_data = (void *)this;
147+
_ccc_value.cfg_changed = prfile_cccd_cfg_changed;
148+
_ccc_value.value = 0;
149+
142150
if (BLERead & properties)
143151
{
144152
_gatt_chrc.properties |= BT_GATT_CHRC_READ;
@@ -433,11 +441,13 @@ bool BLECharacteristicImp::unsubscribe(void)
433441
// Enable CCCD to allow peripheral send Notification/Indication
434442
retval = bt_gatt_unsubscribe(conn, &_sub_params);
435443
bt_conn_unref(conn);
436-
if (0 == retval)
444+
if (0 != retval)
437445
{
438-
_subscribed = false;
446+
return false;
439447
}
440-
return _subscribed;
448+
449+
_subscribed = false;
450+
return true;
441451
}
442452

443453
bool BLECharacteristicImp::subscribe(void)
@@ -1070,4 +1080,26 @@ uint8_t BLECharacteristicImp::discoverResponseProc(bt_conn_t *conn,
10701080
return retVal;
10711081
}
10721082

1083+
void BLECharacteristicImp::cccdValueChanged()
1084+
{
1085+
1086+
enum BLECharacteristicEvent event = BLEUnsubscribed;
1087+
if (subscribed())
1088+
{
1089+
event = BLESubscribed;
1090+
}
1091+
1092+
if (_event_handlers[event])
1093+
{
1094+
BLECharacteristic chrcTmp(this, &_ble_device);
1095+
_event_handlers[event](_ble_device, chrcTmp);
1096+
}
1097+
1098+
if (_oldevent_handlers[event])
1099+
{
1100+
BLECharacteristic chrcTmp(this, &_ble_device);
1101+
BLECentral central(_ble_device);
1102+
_oldevent_handlers[event](central, chrcTmp);
1103+
}
1104+
}
10731105

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@
2020
#ifndef _BLE_CHARACTERISTICIMP_H_INCLUDED
2121
#define _BLE_CHARACTERISTICIMP_H_INCLUDED
2222

23-
//#include "BLECommon.h"
24-
25-
//#include "BLEDevice.h"
26-
//#include "BLEDescriptor.h"
27-
2823
#include "CurieBLE.h"
2924
#include "BLEDescriptorImp.h"
3025

@@ -172,7 +167,7 @@ class BLECharacteristicImp: public BLEAttribute{
172167
static void writeResponseReceived(struct bt_conn *conn,
173168
uint8_t err,
174169
const void *data);
175-
170+
void cccdValueChanged();
176171
int descriptorCount() const;
177172
uint8_t discoverResponseProc(bt_conn_t *conn,
178173
const bt_gatt_attr_t *attr,

Diff for: system/libarc32_arduino101/drivers/bluetooth/gatt.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,8 @@ struct _bt_gatt_ccc {
513513
struct bt_gatt_ccc_cfg *cfg;
514514
size_t cfg_len;
515515
uint16_t value;
516-
void (*cfg_changed)(uint16_t value);
516+
void *user_data;
517+
void (*cfg_changed)(void *user_data, uint16_t value);
517518
};
518519

519520
/** @brief Read Client Characteristic Configuration Attribute helper.

Diff for: system/libarc32_arduino101/framework/src/services/ble/gatt.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ static void gatt_ccc_changed(struct _bt_gatt_ccc *ccc)
434434
if (value != ccc->value) {
435435
ccc->value = value;
436436
if (ccc->cfg_changed)
437-
ccc->cfg_changed(value);
437+
ccc->cfg_changed(ccc->user_data, value);
438438
}
439439
}
440440

@@ -811,7 +811,7 @@ static uint8_t disconnected_cb(const struct bt_gatt_attr *attr, void *user_data)
811811
memset(&ccc->value, 0, sizeof(ccc->value));
812812

813813
if (ccc->cfg_changed) {
814-
ccc->cfg_changed(ccc->value);
814+
ccc->cfg_changed(ccc->user_data, ccc->value);
815815
}
816816

817817
BT_DBG("ccc %p reseted", ccc);

Diff for: variants/arduino_101/libarc32drv_arduino101.a

599 KB
Binary file not shown.

0 commit comments

Comments
 (0)