diff --git a/include/bluetooth/gatt.h b/include/bluetooth/gatt.h index 173f3c87548..f3759d13461 100644 --- a/include/bluetooth/gatt.h +++ b/include/bluetooth/gatt.h @@ -1030,7 +1030,8 @@ int bt_gatt_subscribe(struct bt_conn *conn, * * This procedure unsubscribe to value notification using the Client * Characteristic Configuration handle. Notification callback with NULL data - * will not be called if subscription was removed by this call. + * will be called if subscription was removed by this call, until then the + * parameters cannot be reused. * * @param conn Connection object. * @param params Subscribe parameters. diff --git a/subsys/bluetooth/host/gatt.c b/subsys/bluetooth/host/gatt.c index d3d5ba4daba..3f91965eab9 100644 --- a/subsys/bluetooth/host/gatt.c +++ b/subsys/bluetooth/host/gatt.c @@ -1665,6 +1665,9 @@ static void gatt_write_ccc_rsp(struct bt_conn *conn, u8_t err, prev = node; } + } else if (!params->value) { + /* Notify with NULL data to complete unsubscribe */ + params->notify(conn, params, NULL, 0); } } @@ -1725,7 +1728,7 @@ int bt_gatt_subscribe(struct bt_conn *conn, int err; err = gatt_write_ccc(conn, params->ccc_handle, params->value, - gatt_write_ccc_rsp, NULL); + gatt_write_ccc_rsp, params); if (err) { return err; } @@ -1777,10 +1780,15 @@ int bt_gatt_unsubscribe(struct bt_conn *conn, } if (has_subscription) { + /* Notify with NULL data to complete unsubscribe */ + params->notify(conn, params, NULL, 0); return 0; } - return gatt_write_ccc(conn, params->ccc_handle, 0x0000, NULL, NULL); + params->value = 0x0000; + + return gatt_write_ccc(conn, params->ccc_handle, params->value, + gatt_write_ccc_rsp, params); } void bt_gatt_cancel(struct bt_conn *conn, void *params) @@ -1802,7 +1810,7 @@ static void add_subscriptions(struct bt_conn *conn) * it properly. */ gatt_write_ccc(conn, params->ccc_handle, params->value, - NULL, NULL); + NULL, params); } } diff --git a/subsys/bluetooth/shell/bt.c b/subsys/bluetooth/shell/bt.c index 55657424692..b6e9fabd4b1 100644 --- a/subsys/bluetooth/shell/bt.c +++ b/subsys/bluetooth/shell/bt.c @@ -1433,9 +1433,6 @@ static int cmd_gatt_unsubscribe(int argc, char *argv[]) printk("Unsubscribe success\n"); } - /* Clear subscribe_params to reuse it */ - memset(&subscribe_params, 0, sizeof(subscribe_params)); - return 0; } #endif /* CONFIG_BLUETOOTH_GATT_CLIENT */