From patchwork Fri Jan 21 20:54:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 534595 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E4D2FC433F5 for ; Fri, 21 Jan 2022 20:54:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231499AbiAUUy6 (ORCPT ); Fri, 21 Jan 2022 15:54:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229488AbiAUUy5 (ORCPT ); Fri, 21 Jan 2022 15:54:57 -0500 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00A75C06173B for ; Fri, 21 Jan 2022 12:54:56 -0800 (PST) Received: by mail-pl1-x62e.google.com with SMTP id c3so9710904pls.5 for ; Fri, 21 Jan 2022 12:54:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=eBlCYKmp+TeOJ8JAROGHcnVF8Gb8mmBUiMsm/SyOeFw=; b=pvwziCX4VgNiS08uF1wNf3B9Qp/m0jV+mPcDhxDYS/VqS4tenQYhZhfdLLAt0YPoz9 nx8K9MlfBvR2UCypEg9rMBByYIIuzEg7DR4IMGtkK62P7z0b1Z5IQ7eT0B4lBxMEGLiC rGSudpTJvmGpwVMTIptv2KZna5scLcw9Ch/U2Ly1y1om3kcCe+mIlzqvEOJWdVVVshsj YGTyeLQb9v76N4kyRGSJxAN3StKxccKL7o14Y0pv+4Zsu9MOYE/bcp9Hj/IYHjc4Gmeu DrNim92NswNedqlMPN6BReRDMETudR8Ps0cY71YgCJKbtZZ+pa1u5p1hsGYSWq8nllE7 b0pQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=eBlCYKmp+TeOJ8JAROGHcnVF8Gb8mmBUiMsm/SyOeFw=; b=fFFpuids2fkWkUXZgHBAtqVUiVGylRvgwPt52MR9CbQ6qoaJZakeRTllRP/xAJ+Wj9 SeON7ZbvyBSYf7B3v/+Iutavg1UYurC33MKYIy+UF0ARoTP1lxczm9N+ffo7ka+HwaTW RyVyeSSDgcGa1aCdywLu0BQpB7kqCCEP/YowM1luTCrWlt1BA8ncfKpEatiV7FIdqAEb DKXruU9vlLIUT1h00uwBee3JaDg4K9IPAIC24NUT6PsjRzLASkcl0L9mqUbhj349OTwG WjN+YeWT3UQ/Lq/bbXM9Yl6D5WHmF9WR8x79ACW89NtmC9Heyc3jd4fxnDtmpcsnhgbW zEhA== X-Gm-Message-State: AOAM531r9QbCRV1kWzzzr/d4WHaMKiDMF0SEsEIurc7o6vFigEnZwOaj LONjKhrkI4899domlHMt7jVMQrjv+WvmJA== X-Google-Smtp-Source: ABdhPJxHcIfPXN0lVXlKCD+caJXQGTUb+74MEs3n75QWYtml032t5HxZx5XwOVnYNSWWX43mO1rJfw== X-Received: by 2002:a17:902:6949:b0:14a:752c:58f2 with SMTP id k9-20020a170902694900b0014a752c58f2mr5680888plt.159.1642798496047; Fri, 21 Jan 2022 12:54:56 -0800 (PST) Received: from lvondent-mobl4.intel.com (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id j20sm7441279pfj.218.2022.01.21.12.54.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Jan 2022 12:54:55 -0800 (PST) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ 1/4] shared/gatt-db: Introduce gatt_db_service_add_ccc Date: Fri, 21 Jan 2022 12:54:51 -0800 Message-Id: <20220121205454.1413365-1-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz This adds gatt_db_service_add_ccc so the likes of plugins can use it with the defaults callbacks which is useful for tracking their states. --- src/shared/gatt-db.c | 53 ++++++++++++++++++++++++++++++++++++++++++++ src/shared/gatt-db.h | 7 ++++++ 2 files changed, 60 insertions(+) diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c index 3a02289ce..12ff5badb 100644 --- a/src/shared/gatt-db.c +++ b/src/shared/gatt-db.c @@ -44,6 +44,12 @@ static const bt_uuid_t included_service_uuid = { .type = BT_UUID16, static const bt_uuid_t ext_desc_uuid = { .type = BT_UUID16, .value.u16 = GATT_CHARAC_EXT_PROPER_UUID }; +struct gatt_db_ccc { + gatt_db_read_t read_func; + gatt_db_write_t write_func; + void *user_data; +}; + struct gatt_db { int ref_count; struct bt_crypto *crypto; @@ -57,6 +63,8 @@ struct gatt_db { gatt_db_authorize_cb_t authorize; void *authorize_data; + + struct gatt_db_ccc *ccc; }; struct notify { @@ -444,6 +452,7 @@ static void gatt_db_destroy(struct gatt_db *db) timeout_remove(db->hash_id); queue_destroy(db->services, gatt_db_service_destroy); + free(db->ccc); free(db); } @@ -1038,6 +1047,50 @@ gatt_db_service_add_descriptor(struct gatt_db_attribute *attrib, user_data); } +struct gatt_db_attribute * +gatt_db_service_add_ccc(struct gatt_db_attribute *attrib, uint32_t permissions) +{ + struct gatt_db *db; + struct gatt_db_attribute *ccc; + bt_uuid_t uuid; + + if (!attrib) + return NULL; + + db = attrib->service->db; + + if (!db->ccc) + return NULL; + + bt_uuid16_create(&uuid, GATT_CLIENT_CHARAC_CFG_UUID); + + ccc = service_insert_descriptor(attrib->service, 0, &uuid, + permissions, + db->ccc->read_func, + db->ccc->write_func, + db->ccc->user_data); + if (!ccc) + return ccc; + + gatt_db_attribute_set_fixed_length(ccc, 2); + + return ccc; +} + +void gatt_db_ccc_register(struct gatt_db *db, gatt_db_read_t read_func, + gatt_db_write_t write_func, void *user_data) +{ + if (!db) + return; + + if (!db->ccc) + db->ccc = new0(struct gatt_db_ccc, 1); + + db->ccc->read_func = read_func; + db->ccc->write_func = write_func; + db->ccc->user_data = user_data; +} + static struct gatt_db_attribute * service_insert_included(struct gatt_db_service *service, uint16_t handle, struct gatt_db_attribute *include) diff --git a/src/shared/gatt-db.h b/src/shared/gatt-db.h index 321a2aba6..3de22403c 100644 --- a/src/shared/gatt-db.h +++ b/src/shared/gatt-db.h @@ -92,6 +92,7 @@ gatt_db_service_add_descriptor(struct gatt_db_attribute *attrib, gatt_db_read_t read_func, gatt_db_write_t write_func, void *user_data); + struct gatt_db_attribute * gatt_db_service_insert_descriptor(struct gatt_db_attribute *attrib, uint16_t handle, @@ -101,6 +102,9 @@ gatt_db_service_insert_descriptor(struct gatt_db_attribute *attrib, gatt_db_write_t write_func, void *user_data); +struct gatt_db_attribute * +gatt_db_service_add_ccc(struct gatt_db_attribute *attrib, uint32_t permissions); + struct gatt_db_attribute * gatt_db_insert_included(struct gatt_db *db, uint16_t handle, struct gatt_db_attribute *include); @@ -192,6 +196,9 @@ unsigned int gatt_db_register(struct gatt_db *db, gatt_db_destroy_func_t destroy); bool gatt_db_unregister(struct gatt_db *db, unsigned int id); +void gatt_db_ccc_register(struct gatt_db *db, gatt_db_read_t read_func, + gatt_db_write_t write_func, void *user_data); + typedef uint8_t (*gatt_db_authorize_cb_t)(struct gatt_db_attribute *attrib, uint8_t opcode, struct bt_att *att, void *user_data); From patchwork Fri Jan 21 20:54:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 533884 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A9D3C433EF for ; Fri, 21 Jan 2022 20:54:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230333AbiAUUy6 (ORCPT ); Fri, 21 Jan 2022 15:54:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230426AbiAUUy6 (ORCPT ); Fri, 21 Jan 2022 15:54:58 -0500 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4B2BC06173B for ; Fri, 21 Jan 2022 12:54:57 -0800 (PST) Received: by mail-pl1-x632.google.com with SMTP id c3so9710930pls.5 for ; Fri, 21 Jan 2022 12:54:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=7+4JIuezm2ASlIUJy4Hq9LioKL5A046o9d+k5kWIPuc=; b=FU4qvtfRSJ7zlGQvHJfS5YEcpkWg6KQHtZwnX7xMUDA2kfo9yPtaVUCHWt4LVMu245 j+pkZXm0xdfJDAGuxZDUSDX3eMqY7gOvJCMCgbxOcL4dkI3NE4RyEj4o6jLMWn3rp0A9 JHwa7AhlE7p1r/CHvIB3J37rg7aQylWXpQbGwxP00DwIr5PGQ64NSuTaoKL4zsTMpRKq F9ID+KO2w+4GVgEv3/V9YUffcE4EozxWc9fXZo8s9bwIe3qUMau4RqqULozaRlBPtK0s VABpjGNz1JoDnyaSn0fbCcGp5TkJBhAFuqCjNj2Hk5JgA+gWCC6mtND+ulsDxLsFykmJ dFUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7+4JIuezm2ASlIUJy4Hq9LioKL5A046o9d+k5kWIPuc=; b=WF7lbEM2569CyNgOBvLnfBMH1LvIH4Kt8irfBAnSbTg+CaxyFVjCTUhlnqA377V9fP SxvNQKb/MU80C8JmDgONu1hRbjOABAcZY7oQIV2WantMr+dJmt06Tc5ClD/2WwNB44Nb Ul+8I5p/DjXjoq0kKitGouaubKjIAAThYSF4SmZhJK7KLYvGjVrfA8Ao4jI20EP+WrtN ISggOErnTShJz5RofokwXe8crSgeWOm7S5OYlTF8dZCnxBP8eIGvlXGBqC2riPrVf1ni yQPjXCRLscESr8Bgtcz18A7DokhPYZucJ9OQK/mGoYE9z1q5UlZMWieWApVBQbDOF1uw XqSw== X-Gm-Message-State: AOAM533MS3ob5FpvY/RmkGlRfQcVsXQSyeCpmbQIYHgvdNDdcK5I0ake //D2vgG11BmhC7ubU3c30IGnHe9SBa4W/A== X-Google-Smtp-Source: ABdhPJzJZ8lhr8PlmY2mO+w/6Vs/VGYzCoNjY9jhVIHnNpRYYqbbO2m2Fnbj27GZ8uJStMNhsovsQA== X-Received: by 2002:a17:902:7c09:b0:148:e02f:176b with SMTP id x9-20020a1709027c0900b00148e02f176bmr5273432pll.130.1642798496877; Fri, 21 Jan 2022 12:54:56 -0800 (PST) Received: from lvondent-mobl4.intel.com (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id j20sm7441279pfj.218.2022.01.21.12.54.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Jan 2022 12:54:56 -0800 (PST) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ 2/4] gatt: Make use of gatt_db_service_add_ccc Date: Fri, 21 Jan 2022 12:54:52 -0800 Message-Id: <20220121205454.1413365-2-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220121205454.1413365-1-luiz.dentz@gmail.com> References: <20220121205454.1413365-1-luiz.dentz@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz This uses gatt_db_service_add_ccc and gatt_db_ccc_register so any ccc registered with the use of the former gets proper state tracking. --- src/gatt-database.c | 62 ++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/src/gatt-database.c b/src/gatt-database.c index dbe9415a3..25641da8a 100644 --- a/src/gatt-database.c +++ b/src/gatt-database.c @@ -1018,13 +1018,6 @@ static void gatt_ccc_write_cb(struct gatt_db_attribute *attrib, goto done; } - ccc_cb = queue_find(database->ccc_callbacks, ccc_cb_match_handle, - UINT_TO_PTR(gatt_db_attribute_get_handle(attrib))); - if (!ccc_cb) { - ecode = BT_ATT_ERROR_UNLIKELY; - goto done; - } - if (len == 1) val = *value; else @@ -1034,7 +1027,9 @@ static void gatt_ccc_write_cb(struct gatt_db_attribute *attrib, if (val == ccc->value) goto done; - if (ccc_cb->callback) { + ccc_cb = queue_find(database->ccc_callbacks, ccc_cb_match_handle, + UINT_TO_PTR(gatt_db_attribute_get_handle(attrib))); + if (ccc_cb) { struct pending_op *op; op = pending_ccc_new(att, attrib, val, @@ -1056,6 +1051,22 @@ done: gatt_db_attribute_write_result(attrib, id, ecode); } +static void ccc_add_cb(struct btd_gatt_database *database, + struct gatt_db_attribute *ccc, + btd_gatt_database_ccc_write_t callback, + void *user_data, btd_gatt_database_destroy_t destroy) +{ + struct ccc_cb_data *ccc_cb; + + ccc_cb = new0(struct ccc_cb_data, 1); + ccc_cb->handle = gatt_db_attribute_get_handle(ccc); + ccc_cb->callback = callback; + ccc_cb->destroy = destroy; + ccc_cb->user_data = user_data; + + queue_push_tail(database->ccc_callbacks, ccc_cb); +} + static struct gatt_db_attribute * service_add_ccc(struct gatt_db_attribute *service, struct btd_gatt_database *database, @@ -1064,34 +1075,14 @@ service_add_ccc(struct gatt_db_attribute *service, btd_gatt_database_destroy_t destroy) { struct gatt_db_attribute *ccc; - struct ccc_cb_data *ccc_cb; - bt_uuid_t uuid; - - ccc_cb = new0(struct ccc_cb_data, 1); - - /* - * Provide a way for the permissions on a characteristic to dictate - * the permissions on the CCC - */ - perm |= BT_ATT_PERM_READ | BT_ATT_PERM_WRITE; - - bt_uuid16_create(&uuid, GATT_CLIENT_CHARAC_CFG_UUID); - ccc = gatt_db_service_add_descriptor(service, &uuid, perm, - gatt_ccc_read_cb, gatt_ccc_write_cb, database); - if (!ccc) { - error("Failed to create CCC entry in database"); - free(ccc_cb); - return NULL; - } - - gatt_db_attribute_set_fixed_length(ccc, 2); - ccc_cb->handle = gatt_db_attribute_get_handle(ccc); - ccc_cb->callback = write_callback; - ccc_cb->destroy = destroy; - ccc_cb->user_data = user_data; + ccc = gatt_db_service_add_ccc(service, perm); + if (!ccc) + return ccc; - queue_push_tail(database->ccc_callbacks, ccc_cb); + /* Only add ccc_cb if callback is set */ + if (write_callback) + ccc_add_cb(database, ccc, write_callback, user_data, destroy); return ccc; } @@ -1310,6 +1301,9 @@ static void populate_devinfo_service(struct btd_gatt_database *database) static void register_core_services(struct btd_gatt_database *database) { + gatt_db_ccc_register(database->db, gatt_ccc_read_cb, gatt_ccc_write_cb, + database); + populate_gap_service(database); populate_gatt_service(database); populate_devinfo_service(database); From patchwork Fri Jan 21 20:54:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 533883 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B6C2EC433FE for ; Fri, 21 Jan 2022 20:55:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231695AbiAUUy7 (ORCPT ); Fri, 21 Jan 2022 15:54:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230426AbiAUUy6 (ORCPT ); Fri, 21 Jan 2022 15:54:58 -0500 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81C15C06173B for ; Fri, 21 Jan 2022 12:54:58 -0800 (PST) Received: by mail-pf1-x431.google.com with SMTP id n32so826764pfv.11 for ; Fri, 21 Jan 2022 12:54:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=UQrBgBcqNANOl0cOdNEioTlznopGluw+p1CpMsZGEfk=; b=nyA+PD+ES7vdjxmQuBx4n3uQbw5gtgBXH478xuNMDO2oeND9CICLxx/zRvV2dKb6tw +Gl1rqjlmyMZWZ80EE17aFBvHNS2kLC8G0jWJugfz84ds4LV1t7JyIoB3ENi3KFTTo1O hsGY+ke72q3lmgD5noTa/lHoUQnQkq+9B3FvDgi9POGvIUFUOMymawQopLiwJkaane0i eP02PRKdXdHNTHsRTsXmsJpR4GWgsyUymw+Q/6scfX6e9rqIa0LpeloVXRh49lIocniE sdQcuAhstLWU4BJuko9QCfeBoqvLugJh4UXOvueNB5eWB6D9eHZoKg9AxRZWPPRc4hp2 H74w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UQrBgBcqNANOl0cOdNEioTlznopGluw+p1CpMsZGEfk=; b=nLGiATXYIkLxLcHBgP7wLH1gEcLkjDUye4nQHHo9YtgFPfmZQ219VucUDS2yAlRINi qj/7A2nqiYcKU8kN+OyLpzUAAnUaMjWLKMFpDdcSgtxqcqfs+YQfrJgFa2yoBRpYZ727 GcaafcI52181kBAXFLyvHP14OmnuR6rFLA1dHaZOuRQm9GJBpyPl7HZQsrMXFUp/nHFJ YQf1poUfa8DTth0OxTi5MmWvFF4zMLuCKmoXwB4BUDFef/2XdcuMtnr1mcuRUe3kmCM7 XqCSwO/u79ZnERfTnBeB8kL6o4lRDAwJZ9RzAJkJh8fG8zZ0N/6oHqXjk74LEBxAYPXf we+w== X-Gm-Message-State: AOAM530pkWeYgO0ZUbtWmEENFpjfFufsUfWFjy/nfBK2vM/ESkUVq+8/ RfJHoyruo+gSAvsFC8lrRPi2NCnHYX/dzw== X-Google-Smtp-Source: ABdhPJyMNz1r8nEois63hkqXwQu41L7RVheT6Vl9u3240aPgp5X9erCIRUa+1La7mg3EKlm1Mw5aHQ== X-Received: by 2002:aa7:888e:0:b0:4c4:3ad:9eee with SMTP id z14-20020aa7888e000000b004c403ad9eeemr5061995pfe.13.1642798497657; Fri, 21 Jan 2022 12:54:57 -0800 (PST) Received: from lvondent-mobl4.intel.com (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id j20sm7441279pfj.218.2022.01.21.12.54.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Jan 2022 12:54:57 -0800 (PST) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ 3/4] shared/gatt-db: Introduce gatt_db_attribute_notify Date: Fri, 21 Jan 2022 12:54:53 -0800 Message-Id: <20220121205454.1413365-3-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220121205454.1413365-1-luiz.dentz@gmail.com> References: <20220121205454.1413365-1-luiz.dentz@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz This introduces gatt_db_attribute_notify which can be used to trigger a notification using the callback set by gatt_db_ccc_register. --- src/gatt-database.c | 2 +- src/shared/gatt-db.c | 46 ++++++++++++++++++++++++++++++++++++++++---- src/shared/gatt-db.h | 13 ++++++++++++- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/gatt-database.c b/src/gatt-database.c index 25641da8a..dc75762f3 100644 --- a/src/gatt-database.c +++ b/src/gatt-database.c @@ -1302,7 +1302,7 @@ static void populate_devinfo_service(struct btd_gatt_database *database) static void register_core_services(struct btd_gatt_database *database) { gatt_db_ccc_register(database->db, gatt_ccc_read_cb, gatt_ccc_write_cb, - database); + NULL, database); populate_gap_service(database); populate_gatt_service(database); diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c index 12ff5badb..59d19eea3 100644 --- a/src/shared/gatt-db.c +++ b/src/shared/gatt-db.c @@ -43,10 +43,13 @@ static const bt_uuid_t included_service_uuid = { .type = BT_UUID16, .value.u16 = GATT_INCLUDE_UUID }; static const bt_uuid_t ext_desc_uuid = { .type = BT_UUID16, .value.u16 = GATT_CHARAC_EXT_PROPER_UUID }; +static const bt_uuid_t ccc_uuid = { .type = BT_UUID16, + .value.u16 = GATT_CLIENT_CHARAC_CFG_UUID }; struct gatt_db_ccc { gatt_db_read_t read_func; gatt_db_write_t write_func; + gatt_db_notify_t notify_func; void *user_data; }; @@ -109,6 +112,7 @@ struct gatt_db_attribute { gatt_db_read_t read_func; gatt_db_write_t write_func; + gatt_db_notify_t notify_func; void *user_data; unsigned int read_id; @@ -1047,12 +1051,20 @@ gatt_db_service_add_descriptor(struct gatt_db_attribute *attrib, user_data); } +static void find_ccc_value(struct gatt_db_attribute *attrib, void *user_data) +{ + uint16_t *handle = user_data; + + gatt_db_attribute_get_char_data(attrib, NULL, handle, NULL, NULL, NULL); +} + struct gatt_db_attribute * gatt_db_service_add_ccc(struct gatt_db_attribute *attrib, uint32_t permissions) { struct gatt_db *db; struct gatt_db_attribute *ccc; - bt_uuid_t uuid; + struct gatt_db_attribute *value; + uint16_t handle = 0; if (!attrib) return NULL; @@ -1062,9 +1074,17 @@ gatt_db_service_add_ccc(struct gatt_db_attribute *attrib, uint32_t permissions) if (!db->ccc) return NULL; - bt_uuid16_create(&uuid, GATT_CLIENT_CHARAC_CFG_UUID); + /* Locate value handle */ + gatt_db_service_foreach_char(attrib, find_ccc_value, &handle); + + if (!handle) + return NULL; + + value = gatt_db_get_attribute(db, handle); + if (!value || value->notify_func) + return NULL; - ccc = service_insert_descriptor(attrib->service, 0, &uuid, + ccc = service_insert_descriptor(attrib->service, 0, &ccc_uuid, permissions, db->ccc->read_func, db->ccc->write_func, @@ -1073,12 +1093,16 @@ gatt_db_service_add_ccc(struct gatt_db_attribute *attrib, uint32_t permissions) return ccc; gatt_db_attribute_set_fixed_length(ccc, 2); + ccc->notify_func = db->ccc->notify_func; + value->notify_func = db->ccc->notify_func; return ccc; } void gatt_db_ccc_register(struct gatt_db *db, gatt_db_read_t read_func, - gatt_db_write_t write_func, void *user_data) + gatt_db_write_t write_func, + gatt_db_notify_t notify_func, + void *user_data) { if (!db) return; @@ -1088,6 +1112,7 @@ void gatt_db_ccc_register(struct gatt_db *db, gatt_db_read_t read_func, db->ccc->read_func = read_func; db->ccc->write_func = write_func; + db->ccc->notify_func = notify_func; db->ccc->user_data = user_data; } @@ -2103,6 +2128,19 @@ bool gatt_db_attribute_write_result(struct gatt_db_attribute *attrib, return true; } +bool gatt_db_attribute_notify(struct gatt_db_attribute *attrib, + struct gatt_db_attribute *ccc, + const uint8_t *value, size_t len, + struct bt_att *att) +{ + if (!attrib || !ccc || !attrib->notify_func) + return false; + + attrib->notify_func(attrib, ccc, value, len, att, ccc->user_data); + + return true; +} + bool gatt_db_attribute_reset(struct gatt_db_attribute *attrib) { if (!attrib) diff --git a/src/shared/gatt-db.h b/src/shared/gatt-db.h index 3de22403c..acd8f6a81 100644 --- a/src/shared/gatt-db.h +++ b/src/shared/gatt-db.h @@ -47,6 +47,10 @@ typedef void (*gatt_db_write_t) (struct gatt_db_attribute *attrib, const uint8_t *value, size_t len, uint8_t opcode, struct bt_att *att, void *user_data); +typedef void (*gatt_db_notify_t) (struct gatt_db_attribute *attrib, + struct gatt_db_attribute *ccc, + const uint8_t *value, size_t len, + struct bt_att *att, void *user_data); struct gatt_db_attribute * gatt_db_service_add_characteristic(struct gatt_db_attribute *attrib, @@ -197,7 +201,9 @@ unsigned int gatt_db_register(struct gatt_db *db, bool gatt_db_unregister(struct gatt_db *db, unsigned int id); void gatt_db_ccc_register(struct gatt_db *db, gatt_db_read_t read_func, - gatt_db_write_t write_func, void *user_data); + gatt_db_write_t write_func, + gatt_db_notify_t notify_func, + void *user_data); typedef uint8_t (*gatt_db_authorize_cb_t)(struct gatt_db_attribute *attrib, uint8_t opcode, struct bt_att *att, @@ -275,6 +281,11 @@ bool gatt_db_attribute_write(struct gatt_db_attribute *attrib, uint16_t offset, bool gatt_db_attribute_write_result(struct gatt_db_attribute *attrib, unsigned int id, int err); +bool gatt_db_attribute_notify(struct gatt_db_attribute *attrib, + struct gatt_db_attribute *ccc, + const uint8_t *value, size_t len, + struct bt_att *att); + bool gatt_db_attribute_reset(struct gatt_db_attribute *attrib); void *gatt_db_attribute_get_user_data(struct gatt_db_attribute *attrib); From patchwork Fri Jan 21 20:54:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Augusto von Dentz X-Patchwork-Id: 534594 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C45DC4332F for ; Fri, 21 Jan 2022 20:55:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231837AbiAUUzA (ORCPT ); Fri, 21 Jan 2022 15:55:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231322AbiAUUy7 (ORCPT ); Fri, 21 Jan 2022 15:54:59 -0500 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1EA3C06173D for ; Fri, 21 Jan 2022 12:54:58 -0800 (PST) Received: by mail-pj1-x1035.google.com with SMTP id n16-20020a17090a091000b001b46196d572so10060601pjn.5 for ; Fri, 21 Jan 2022 12:54:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=T157zpPrAdmyj6XH3cNbxAkpr5gHylRa4bo497MB4js=; b=UepqD61U4Bkan4OsbJGvZc6mGoSi77NvVSwPgfKJbK2KXIUvPwh1kc80n318b3acH+ aqIy1w/C1z0syuMBg9bzgy5b7D4afK9wkC+5PgHLlAhFuFbXlSIJQKEj/M++pf53hmSS rbayCD1i790B3t+2D88Ut7DQCOoV/Z9wv4dljogRNxTG+9PBHFkoi/ittUQLFZQr/vyH IuAWgLF+offo6Dq2fAavIJhBqtivDizFYqdFVfEwzfFw8NSGSyiaUnAm6AYil4EEmDCd ffXDHfuCxwTQZjCxAPdXKgL07uxG1foiV7u/1Fx4pnHuA0huZsxvltgnRo9JBiSoG2qV eMmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=T157zpPrAdmyj6XH3cNbxAkpr5gHylRa4bo497MB4js=; b=s/HT5ZDCoi6pj9dZlzdJCqYekXoXqQRgQJ5hNaX49oQn6/MXa/+JwWSwTcWAgrRsui mjlQFvS1qTsNHd172C8TKlwyGyiYyJxKlBgjua1eiCV2UXHWaTyM1CM2ljrySojFT49Y ax0Aw7u/1h1qT9hdy7SfCjDvqPyUuaC8IEXED18CWpfQtcSB/E8xaPMID7tJmJT976vz 4braVxeqYJwavTI8UbBoizeLqjyIBT19HQr5BwIEEVy/Ag9a2tgeUun8Ehc5NzF53Z0D Dwt1kq52O/j/pAxIpcyG8NgAmKbRVJYc0Nck4UxKiM3BchO9Uuj/KOCqaBiiBUFxbJqU 0lQw== X-Gm-Message-State: AOAM5318zxswtvuzLi6BK2u19mpTti8hfu47WDcJw9xVg9HwplbQEdJ+ ECcK5XeBcmrkDE6MthcbpVoCjzeHvLPi6Q== X-Google-Smtp-Source: ABdhPJzRuK+pJGWY34S2nh5uUEOFtVJbx4MSp7udzjlzzMyt6pb+2THe8hdSJTmzJF0293K567PfsQ== X-Received: by 2002:a17:902:ed85:b0:14a:2471:e11d with SMTP id e5-20020a170902ed8500b0014a2471e11dmr5135351plj.77.1642798498228; Fri, 21 Jan 2022 12:54:58 -0800 (PST) Received: from lvondent-mobl4.intel.com (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id j20sm7441279pfj.218.2022.01.21.12.54.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 21 Jan 2022 12:54:57 -0800 (PST) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ 4/4] gatt: Make use of gatt_db_attribute_notify Date: Fri, 21 Jan 2022 12:54:54 -0800 Message-Id: <20220121205454.1413365-4-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220121205454.1413365-1-luiz.dentz@gmail.com> References: <20220121205454.1413365-1-luiz.dentz@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz This makes use of gatt_db_attribute_notify to send indications of Service Changed. --- src/gatt-database.c | 73 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 14 deletions(-) diff --git a/src/gatt-database.c b/src/gatt-database.c index dc75762f3..d3518ebfc 100644 --- a/src/gatt-database.c +++ b/src/gatt-database.c @@ -378,6 +378,18 @@ static bool get_dst_info(struct bt_att *att, bdaddr_t *dst, uint8_t *dst_type) return true; } +static struct device_state * +find_device_state_by_att(struct btd_gatt_database *database, struct bt_att *att) +{ + bdaddr_t bdaddr; + uint8_t bdaddr_type; + + if (!get_dst_info(att, &bdaddr, &bdaddr_type)) + return NULL; + + return find_device_state(database, &bdaddr, bdaddr_type); +} + static struct device_state *get_device_state(struct btd_gatt_database *database, struct bt_att *att) { @@ -1299,16 +1311,6 @@ static void populate_devinfo_service(struct btd_gatt_database *database) database_add_record(database, service); } -static void register_core_services(struct btd_gatt_database *database) -{ - gatt_db_ccc_register(database->db, gatt_ccc_read_cb, gatt_ccc_write_cb, - NULL, database); - - populate_gap_service(database); - populate_gatt_service(database); - populate_devinfo_service(database); -} - static void conf_cb(void *user_data) { GDBusProxy *proxy = user_data; @@ -1432,6 +1434,49 @@ remove: } } +static void gatt_notify_cb(struct gatt_db_attribute *attrib, + struct gatt_db_attribute *ccc, + const uint8_t *value, size_t len, + struct bt_att *att, void *user_data) +{ + struct btd_gatt_database *database = user_data; + struct notify notify; + + memset(¬ify, 0, sizeof(notify)); + + notify.database = database; + notify.handle = gatt_db_attribute_get_handle(attrib); + notify.ccc_handle = gatt_db_attribute_get_handle(ccc); + notify.value = (void *) value; + notify.len = len; + + if (attrib == database->svc_chngd) + notify.conf = service_changed_conf; + + /* If a specific att is provided notify only to that device */ + if (att) { + struct device_state *state; + + state = find_device_state_by_att(database, att); + if (!state) + return; + + send_notification_to_device(state, ¬ify); + } else + queue_foreach(database->device_states, + send_notification_to_device, ¬ify); +} + +static void register_core_services(struct btd_gatt_database *database) +{ + gatt_db_ccc_register(database->db, gatt_ccc_read_cb, gatt_ccc_write_cb, + gatt_notify_cb, database); + + populate_gap_service(database); + populate_gatt_service(database); + populate_devinfo_service(database); +} + static void send_notification_to_devices(struct btd_gatt_database *database, uint16_t handle, uint8_t *value, uint16_t len, uint16_t ccc_handle, @@ -1478,8 +1523,8 @@ static void send_service_changed(struct btd_gatt_database *database, put_le16(start, value); put_le16(end, value + 2); - send_notification_to_devices(database, handle, value, sizeof(value), - ccc_handle, service_changed_conf, NULL); + gatt_db_attribute_notify(database->svc_chngd, database->svc_chngd_ccc, + value, sizeof(value), NULL); } static void gatt_db_service_added(struct gatt_db_attribute *attrib, @@ -3917,6 +3962,6 @@ void btd_gatt_database_restore_svc_chng_ccc(struct btd_gatt_database *database) put_le16(0x0001, value); put_le16(0xffff, value + 2); - send_notification_to_devices(database, handle, value, sizeof(value), - ccc_handle, service_changed_conf, NULL); + gatt_db_attribute_notify(database->svc_chngd, database->svc_chngd_ccc, + value, sizeof(value), NULL); }