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
3136ssize_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
241300void 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+
0 commit comments