Skip to content

Commit

Permalink
Fix Notification can't send out
Browse files Browse the repository at this point in the history
1. Change the callback and data structure.
2. Removed some unused code
  • Loading branch information
sgbihu committed Apr 25, 2017
1 parent 3fc493d commit d7e38bb
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 96 deletions.
90 changes: 8 additions & 82 deletions libraries/CurieBLE/src/internal/BLECallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,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 @@ -151,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 @@ -242,69 +239,13 @@ void bleConnectEventHandler(bt_conn_t *conn,
p->handleConnectEvent(conn, err);
}

static uint8_t ble_gatt_disconnected_cb(const struct bt_gatt_attr *attr, void *user_data)
{
struct bt_conn *conn = (struct bt_conn *)user_data;
struct _bt_gatt_ccc *ccc;
size_t i;

/* Check attribute user_data must be of type struct _bt_gatt_ccc */
if (attr->write != profile_gatt_attr_write_ccc) {
return BT_GATT_ITER_CONTINUE;
}

ccc = (struct _bt_gatt_ccc *)attr->user_data;
/* If already disabled skip */
if (!ccc->value) {
return BT_GATT_ITER_CONTINUE;
}

for (i = 0; i < ccc->cfg_len; i++)
{
/* Ignore configurations with disabled value */
if (!ccc->cfg[i].value)
{
continue;
}

if (bt_addr_le_cmp(&conn->le.dst, &ccc->cfg[i].peer))
{
struct bt_conn *tmp;

/* Skip if there is another peer connected */
tmp = bt_conn_lookup_addr_le(&ccc->cfg[i].peer);
if (tmp) {
if (tmp->state == BT_CONN_CONNECTED) {
bt_conn_unref(tmp);
return BT_GATT_ITER_CONTINUE;
}

bt_conn_unref(tmp);
}
}
}

/* Reset value while disconnected */
memset(&ccc->value, 0, sizeof(ccc->value));

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

pr_debug(LOG_MODULE_BLE, "ccc %p reseted", ccc);

return BT_GATT_ITER_CONTINUE;
}


void bleDisconnectEventHandler(bt_conn_t *conn,
uint8_t reason,
void *param)
{
BLEDeviceManager* p = (BLEDeviceManager*)param;

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

p->handleDisconnectEvent(conn, reason);
}
Expand Down Expand Up @@ -343,29 +284,14 @@ void ble_on_write_no_rsp_complete(struct bt_conn *conn, uint8_t err,
BLECharacteristicImp::writeResponseReceived(conn, err, data);
}

ssize_t profile_gatt_attr_write_ccc(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
const void *buf,
uint16_t len,
uint16_t offset)
void prfile_cccd_cfg_changed(void *user_data, uint16_t value)
{
struct _bt_gatt_ccc *ccc = (struct _bt_gatt_ccc *)attr->user_data;
const uint16_t *data = (const uint16_t *)buf;
bool cccdChanged = (ccc->value != *data);
ssize_t retValue = bt_gatt_attr_write_ccc(conn, attr, buf, len, offset);
if (cccdChanged)
{
// Find characteristic and do notification
const struct bt_gatt_attr *attrChrc = attr - 1;
BLEAttribute *bleattr = (BLEAttribute *)attrChrc->user_data;
BLEAttributeType type = bleattr->type();
pr_debug(LOG_MODULE_BLE, "The Attribute type:%d", type);
if (BLETypeCharacteristic == type)
{
BLECharacteristicImp *blecharacteritic = (BLECharacteristicImp*)bleattr;
blecharacteritic->cccdValueChanged();
}
}
return retValue;
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();
}


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

ssize_t profile_gatt_attr_write_ccc(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
const void *buf,
uint16_t len,
uint16_t offset);
void prfile_cccd_cfg_changed(void *user_data, uint16_t value);

#endif

6 changes: 5 additions & 1 deletion 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 @@ -804,7 +808,7 @@ int BLECharacteristicImp::updateProfile(bt_gatt_attr_t *attr_start, int& index)
start->uuid = this->getClientCharacteristicConfigUuid();
start->perm = BT_GATT_PERM_READ | BT_GATT_PERM_WRITE;
start->read = bt_gatt_attr_read_ccc;
start->write = profile_gatt_attr_write_ccc;
start->write = bt_gatt_attr_write_ccc;
start->user_data = this->getCccCfg();

pr_info(LOG_MODULE_BLE, "cccd-%p", start);
Expand Down
5 changes: 0 additions & 5 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
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 d7e38bb

Please sign in to comment.