Skip to content

Commit

Permalink
Bluetooth: host: Improve gatt subscribe
Browse files Browse the repository at this point in the history
* Remove optimization that would not send a ccc write
over the air when another subscription existed. This behavior
could trick the application to wait for subscribe callback which
would never happen.

Signed-off-by: Yago Fontoura do Rosario <[email protected]>
  • Loading branch information
Yago Fontoura do Rosario committed Nov 28, 2024
1 parent 2f583a8 commit 2f212c5
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions subsys/bluetooth/host/gatt.c
Original file line number Diff line number Diff line change
Expand Up @@ -5429,7 +5429,7 @@ int bt_gatt_subscribe(struct bt_conn *conn,
{
struct gatt_sub *sub;
struct bt_gatt_subscribe_params *tmp;
bool has_subscription = false;
int err;

__ASSERT(conn, "invalid parameters\n");
__ASSERT(params && params->notify, "invalid parameters\n");
Expand Down Expand Up @@ -5465,28 +5465,17 @@ int bt_gatt_subscribe(struct bt_conn *conn,
gatt_sub_remove(conn, sub, NULL, NULL);
return -EALREADY;
}

/* Check if another subscription exists */
if (tmp->value_handle == params->value_handle &&
tmp->value >= params->value) {
has_subscription = true;
}
}

/* Skip write if already subscribed */
if (!has_subscription) {
int err;

#if defined(CONFIG_BT_GATT_AUTO_DISCOVER_CCC)
if (params->ccc_handle == BT_GATT_AUTO_DISCOVER_CCC_HANDLE) {
return gatt_ccc_discover(conn, params);
}
if (params->ccc_handle == BT_GATT_AUTO_DISCOVER_CCC_HANDLE) {
return gatt_ccc_discover(conn, params);
}
#endif
err = gatt_write_ccc(conn, params, gatt_write_ccc_rsp);
if (err) {
gatt_sub_remove(conn, sub, NULL, NULL);
return err;
}
err = gatt_write_ccc(conn, params, gatt_write_ccc_rsp);
if (err) {
gatt_sub_remove(conn, sub, NULL, NULL);
return err;
}

/*
Expand Down

0 comments on commit 2f212c5

Please sign in to comment.