diff --git a/libraries/CurieBLE/src/internal/BLECallbacks.cpp b/libraries/CurieBLE/src/internal/BLECallbacks.cpp index 5818c427..c2d90ff0 100644 --- a/libraries/CurieBLE/src/internal/BLECallbacks.cpp +++ b/libraries/CurieBLE/src/internal/BLECallbacks.cpp @@ -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); @@ -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; } @@ -242,61 +239,6 @@ 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) @@ -304,7 +246,6 @@ void bleDisconnectEventHandler(bt_conn_t *conn, 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); } @@ -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(); } + diff --git a/libraries/CurieBLE/src/internal/BLECallbacks.h b/libraries/CurieBLE/src/internal/BLECallbacks.h index 5d877ad3..fc26ea23 100644 --- a/libraries/CurieBLE/src/internal/BLECallbacks.h +++ b/libraries/CurieBLE/src/internal/BLECallbacks.h @@ -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 diff --git a/libraries/CurieBLE/src/internal/BLECharacteristicImp.cpp b/libraries/CurieBLE/src/internal/BLECharacteristicImp.cpp index 8aa94a3b..7c541585 100644 --- a/libraries/CurieBLE/src/internal/BLECharacteristicImp.cpp +++ b/libraries/CurieBLE/src/internal/BLECharacteristicImp.cpp @@ -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; @@ -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); diff --git a/libraries/CurieBLE/src/internal/BLECharacteristicImp.h b/libraries/CurieBLE/src/internal/BLECharacteristicImp.h index 320cf980..e4d01cdb 100644 --- a/libraries/CurieBLE/src/internal/BLECharacteristicImp.h +++ b/libraries/CurieBLE/src/internal/BLECharacteristicImp.h @@ -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" diff --git a/system/libarc32_arduino101/drivers/bluetooth/gatt.h b/system/libarc32_arduino101/drivers/bluetooth/gatt.h index e9eac613..5845cd13 100644 --- a/system/libarc32_arduino101/drivers/bluetooth/gatt.h +++ b/system/libarc32_arduino101/drivers/bluetooth/gatt.h @@ -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. diff --git a/system/libarc32_arduino101/framework/src/services/ble/gatt.c b/system/libarc32_arduino101/framework/src/services/ble/gatt.c index fe5ce8e0..d7906135 100644 --- a/system/libarc32_arduino101/framework/src/services/ble/gatt.c +++ b/system/libarc32_arduino101/framework/src/services/ble/gatt.c @@ -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); } } @@ -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); diff --git a/variants/arduino_101/libarc32drv_arduino101.a b/variants/arduino_101/libarc32drv_arduino101.a index db6e5b45..3110e456 100644 Binary files a/variants/arduino_101/libarc32drv_arduino101.a and b/variants/arduino_101/libarc32drv_arduino101.a differ