Skip to content

Commit

Permalink
Jira 913 BLESubscribed, BLEUnsubscribed Event Handlers are not called
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sgbihu committed Apr 25, 2017
1 parent 6d37b45 commit e087206
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 16 deletions.
20 changes: 16 additions & 4 deletions libraries/CurieBLE/src/internal/BLECallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
#include "BLEDeviceManager.h"
#include "BLEProfileManager.h"

#include "BLECallbacks.h"

#include <atomic.h>
#include "../src/services/ble/conn_internal.h"

// GATT Server Only
ssize_t profile_read_process(bt_conn_t *conn,
const bt_gatt_attr_t *attr,
Expand Down Expand Up @@ -123,8 +128,6 @@ uint8_t profile_notify_process (bt_conn_t *conn,
bt_gatt_subscribe_params_t *params,
const void *data, uint16_t length)
{
//BLEPeripheralHelper* peripheral = BLECentralRole::instance()->peripheral(conn);// Find peripheral by bt_conn
//BLEAttribute* notifyatt = peripheral->attribute(params); // Find attribute by params
BLECharacteristicImp* chrc = NULL;
BLEDevice bleDevice(bt_conn_get_dst(conn));
chrc = BLEProfileManager::instance()->characteristic(bleDevice, params->value_handle);
Expand All @@ -146,7 +149,6 @@ uint8_t profile_discover_process(bt_conn_t *conn,
uint8_t ret = BT_GATT_ITER_STOP;
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
ret = BLEProfileManager::instance()->discoverResponseProc(conn, attr, params);
pr_debug(LOG_MODULE_BLE, "%s-%d", __FUNCTION__, __LINE__);
return ret;
}

Expand Down Expand Up @@ -237,7 +239,6 @@ void bleConnectEventHandler(bt_conn_t *conn,
p->handleConnectEvent(conn, err);
}


void bleDisconnectEventHandler(bt_conn_t *conn,
uint8_t reason,
void *param)
Expand Down Expand Up @@ -283,3 +284,14 @@ void ble_on_write_no_rsp_complete(struct bt_conn *conn, uint8_t err,
BLECharacteristicImp::writeResponseReceived(conn, err, data);
}

void prfile_cccd_cfg_changed(void *user_data, uint16_t value)
{
if (NULL == user_data)
return;
pr_debug(LOG_MODULE_BLE, "%s-%d: ccc userdata %p", __FUNCTION__, __LINE__, user_data);

BLECharacteristicImp *blecharacteritic = (BLECharacteristicImp *)user_data;
blecharacteritic->cccdValueChanged();
}


2 changes: 2 additions & 0 deletions libraries/CurieBLE/src/internal/BLECallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,7 @@ uint8_t profile_characteristic_read_rsp_process(bt_conn_t *conn,
const void *data,
uint16_t length);

void prfile_cccd_cfg_changed(void *user_data, uint16_t value);

#endif

38 changes: 35 additions & 3 deletions libraries/CurieBLE/src/internal/BLECharacteristicImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ BLECharacteristicImp::BLECharacteristicImp(const bt_uuid_t* uuid,

_ccc_value.cfg = &_ccc_cfg;
_ccc_value.cfg_len = 1;
_ccc_value.user_data = (void *)this;
_ccc_value.cfg_changed = prfile_cccd_cfg_changed;
_ccc_value.value = 0;

if (BLERead & properties)
{
_gatt_chrc.properties |= BT_GATT_CHRC_READ;
Expand Down Expand Up @@ -139,6 +143,10 @@ BLECharacteristicImp::BLECharacteristicImp(BLECharacteristic& characteristic,

_ccc_value.cfg = &_ccc_cfg;
_ccc_value.cfg_len = 1;
_ccc_value.user_data = (void *)this;
_ccc_value.cfg_changed = prfile_cccd_cfg_changed;
_ccc_value.value = 0;

if (BLERead & properties)
{
_gatt_chrc.properties |= BT_GATT_CHRC_READ;
Expand Down Expand Up @@ -433,11 +441,13 @@ bool BLECharacteristicImp::unsubscribe(void)
// Enable CCCD to allow peripheral send Notification/Indication
retval = bt_gatt_unsubscribe(conn, &_sub_params);
bt_conn_unref(conn);
if (0 == retval)
if (0 != retval)
{
_subscribed = false;
return false;
}
return _subscribed;

_subscribed = false;
return true;
}

bool BLECharacteristicImp::subscribe(void)
Expand Down Expand Up @@ -1070,4 +1080,26 @@ uint8_t BLECharacteristicImp::discoverResponseProc(bt_conn_t *conn,
return retVal;
}

void BLECharacteristicImp::cccdValueChanged()
{

enum BLECharacteristicEvent event = BLEUnsubscribed;
if (subscribed())
{
event = BLESubscribed;
}

if (_event_handlers[event])
{
BLECharacteristic chrcTmp(this, &_ble_device);
_event_handlers[event](_ble_device, chrcTmp);
}

if (_oldevent_handlers[event])
{
BLECharacteristic chrcTmp(this, &_ble_device);
BLECentral central(_ble_device);
_oldevent_handlers[event](central, chrcTmp);
}
}

7 changes: 1 addition & 6 deletions libraries/CurieBLE/src/internal/BLECharacteristicImp.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
#ifndef _BLE_CHARACTERISTICIMP_H_INCLUDED
#define _BLE_CHARACTERISTICIMP_H_INCLUDED

//#include "BLECommon.h"

//#include "BLEDevice.h"
//#include "BLEDescriptor.h"

#include "CurieBLE.h"
#include "BLEDescriptorImp.h"

Expand Down Expand Up @@ -172,7 +167,7 @@ class BLECharacteristicImp: public BLEAttribute{
static void writeResponseReceived(struct bt_conn *conn,
uint8_t err,
const void *data);

void cccdValueChanged();
int descriptorCount() const;
uint8_t discoverResponseProc(bt_conn_t *conn,
const bt_gatt_attr_t *attr,
Expand Down
3 changes: 2 additions & 1 deletion system/libarc32_arduino101/drivers/bluetooth/gatt.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,8 @@ struct _bt_gatt_ccc {
struct bt_gatt_ccc_cfg *cfg;
size_t cfg_len;
uint16_t value;
void (*cfg_changed)(uint16_t value);
void *user_data;
void (*cfg_changed)(void *user_data, uint16_t value);
};

/** @brief Read Client Characteristic Configuration Attribute helper.
Expand Down
4 changes: 2 additions & 2 deletions system/libarc32_arduino101/framework/src/services/ble/gatt.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ static void gatt_ccc_changed(struct _bt_gatt_ccc *ccc)
if (value != ccc->value) {
ccc->value = value;
if (ccc->cfg_changed)
ccc->cfg_changed(value);
ccc->cfg_changed(ccc->user_data, value);
}
}

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

if (ccc->cfg_changed) {
ccc->cfg_changed(ccc->value);
ccc->cfg_changed(ccc->user_data, ccc->value);
}

BT_DBG("ccc %p reseted", ccc);
Expand Down
Binary file modified variants/arduino_101/libarc32drv_arduino101.a
Binary file not shown.

0 comments on commit e087206

Please sign in to comment.