Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp1469867rwl; Thu, 5 Jan 2023 14:12:01 -0800 (PST) X-Google-Smtp-Source: AMrXdXvw9daINsud8cVSflxL3eC9BNnhYLbIL8xQF1kM7+5U/MFk6Unwv4VPxCeY290Q4KeBLCvv X-Received: by 2002:a17:907:c28f:b0:7c1:eb:b2a7 with SMTP id tk15-20020a170907c28f00b007c100ebb2a7mr10556563ejc.13.1672956720832; Thu, 05 Jan 2023 14:12:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672956720; cv=none; d=google.com; s=arc-20160816; b=c7BDOlT2Lh6rlnsjBCbxoDStua4MPgX9tym8NzeqelV8uYdNZJku0cBW0M50M6ur2E +oIjQVGcoj79+ChQMtylf6Yt7+8nJXrg2xjeXVsUANtI7Cma6Al39Ta2F9S8Nn2iXes4 NHA5EFK60zKFbrGd2113zg5Cgc8z2BEUBBDrGk77yowkqTDAuEyE53dzkZ7fYzRJU419 u3tfdA3zD1/fU6pVF/DPyP/uFIQHjXLg0cuhvfIquKGWTuvG8VsC7bd3+pvgYjYMVoXf U/lKVjVsLM2orW9OoTsEVlPCMxOkmAapet/I38Y9NMWH8X2JEddZdbkbyHH12dyXD1iq nCdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=Gw6itf7AVrCO7LXSXZODgQaXufV/CbE8VRMeicmNgDc=; b=u4oM1fk57IZFXb7DzjYC0dNqfxW4ElCZxxlJyJhl/PEzeLQWc6RnbK6MhV3iW4I8sG p/vj1/vlI8IHIzsX79gpsALYipZVbvyuMMhfuUQa8/PcOKg4o/8C0Am/0Nk4D53MRDu+ khCBcGhcWD0un64Xt/iH4tH+EawxlJSLrgaFNO5eq3B+FwGbT2iAU09GbrX3v9/y26wh +e3v6gAJkd8WZXf7TF76BVEX0BHg4tIZWTRWqVUiGrIvbEMZhe25J8iE1Ab26G+YDVoj 0gUqVWIDfvH+LqdqgtDf3Px3V/ekSVqb8AxZGqPlhHmEao+bEmaPyd0oeramPT6ezR4f DWIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=TyWwwYCG; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g25-20020a170906595900b007c0a9aca484si27554418ejr.755.2023.01.05.14.11.37; Thu, 05 Jan 2023 14:12:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=TyWwwYCG; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235623AbjAEWKO (ORCPT + 99 others); Thu, 5 Jan 2023 17:10:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47104 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230402AbjAEWJw (ORCPT ); Thu, 5 Jan 2023 17:09:52 -0500 Received: from mail-pl1-x630.google.com (mail-pl1-x630.google.com [IPv6:2607:f8b0:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1432967BEA for ; Thu, 5 Jan 2023 14:09:51 -0800 (PST) Received: by mail-pl1-x630.google.com with SMTP id n4so40802577plp.1 for ; Thu, 05 Jan 2023 14:09:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=Gw6itf7AVrCO7LXSXZODgQaXufV/CbE8VRMeicmNgDc=; b=TyWwwYCGbAZpp2Qiwlr03WssIfQgQ21+P0AZPHakekrKtRUp/u/YoQ9fx9DbCWACcA IbzEQy0xekxmwEJOHXQzromGzkKPnUheWkbAFUGrpl3twIAbSeX81hgN5krj9a3IQ7EF vwwskenk7Ns0jB9NEGRHaxmx22Qed6mVMrDi02umU44o8q/TjFIJjRgo9LBHWS560FkN EEMjRXmOZ/Cj/6B4nud3fmgHBsPaEhWBL9twEMLqUiUqSLxX05q9KXjDRhq07mBB3dVV OaaVjAWEfRx4HhyLgY6rDdH1+mcdguh2DZ8ok20pPoiBY6tilXQxWN+kidRUsoAE0aLz dzVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Gw6itf7AVrCO7LXSXZODgQaXufV/CbE8VRMeicmNgDc=; b=Tn/+bMmwHfEcOc1t3Teehz4d/fjXQ6/ujUKtOrwRTg/LnvU0YqFXS75IJhxiK/DL5V C+488s5SdFKbIURnmMp9H2hIFDNNxP8i31rZNRvK3pkJn4ceGezcbL8uMkqLx5Oyocxf P1j8tN2/SFixFA/qcN3/ZtBmHeWUA5Tiysv/XfHWdJsBx35nawdnYOpn++0vFfQV5JHc 7+yBBRRML8wTry/7mTmsKMiIhlLSW/PlXrsmmX2FGC4fU4tbc4hCvehlT22isJms05EY DKjPnz6L8OvzE9UHllTxbKJecB7Huj42l1/ce1E1ojjts16hXrD9fUT5DA1D3HRmyCCT yeIA== X-Gm-Message-State: AFqh2krJMFTO6xvhf7SdQIUpNgQrZxSOo35x83Hc7lDg2hUXnX+ZlW/6 nNor4RG1SsIv591q+7SGc36RGK7syN4= X-Received: by 2002:a17:903:3311:b0:189:d696:63e2 with SMTP id jk17-20020a170903331100b00189d69663e2mr58379339plb.39.1672956589964; Thu, 05 Jan 2023 14:09:49 -0800 (PST) Received: from lvondent-mobl4.. (c-71-56-157-77.hsd1.or.comcast.net. [71.56.157.77]) by smtp.gmail.com with ESMTPSA id jg21-20020a17090326d500b001929827731esm16744861plb.201.2023.01.05.14.09.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Jan 2023 14:09:49 -0800 (PST) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ 3/4] attrib: Introduce g_attrib_attach_client Date: Thu, 5 Jan 2023 14:09:43 -0800 Message-Id: <20230105220944.2373424-3-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20230105220944.2373424-1-luiz.dentz@gmail.com> References: <20230105220944.2373424-1-luiz.dentz@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Luiz Augusto von Dentz This introduces g_attrib_attach_client which can be used to attach a bt_gatt_client instance to GAttr so it can be used to register notifications. --- Makefile.am | 6 +++--- attrib/gattrib.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ attrib/gattrib.h | 2 ++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/Makefile.am b/Makefile.am index aa3a5e053cd8..0e8cce249c7d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -565,9 +565,9 @@ unit_tests += unit/test-gattrib unit_test_gattrib_SOURCES = unit/test-gattrib.c attrib/gattrib.c \ $(btio_sources) src/log.h src/log.c -unit_test_gattrib_LDADD = lib/libbluetooth-internal.la \ - src/libshared-glib.la \ - $(GLIB_LIBS) $(DBUS_LIBS) -ldl -lrt +unit_test_gattrib_LDADD = src/libshared-glib.la \ + lib/libbluetooth-internal.la \ + $(GLIB_LIBS) $(DBUS_LIBS) -ldl -lrt unit_tests += unit/test-bap diff --git a/attrib/gattrib.c b/attrib/gattrib.c index 041b9d289c64..997af3699c51 100644 --- a/attrib/gattrib.c +++ b/attrib/gattrib.c @@ -21,17 +21,23 @@ #include #include "lib/bluetooth.h" +#include "lib/uuid.h" #include "btio/btio.h" #include "src/log.h" #include "src/shared/util.h" #include "src/shared/att.h" +#include "src/shared/gatt-helpers.h" #include "src/shared/queue.h" +#include "src/shared/gatt-db.h" +#include "src/shared/gatt-client.h" +#include "attrib/att.h" #include "attrib/gattrib.h" struct _GAttrib { int ref_count; struct bt_att *att; + struct bt_gatt_client *client; GIOChannel *io; GDestroyNotify destroy; gpointer destroy_user_data; @@ -145,6 +151,7 @@ void g_attrib_unref(GAttrib *attrib) if (attrib->destroy) attrib->destroy(attrib->destroy_user_data); + bt_gatt_client_unref(attrib->client); bt_att_unref(attrib->att); queue_destroy(attrib->callbacks, attrib_callbacks_destroy); @@ -338,6 +345,20 @@ gboolean g_attrib_cancel_all(GAttrib *attrib) return TRUE; } +static void client_notify_cb(uint16_t value_handle, const uint8_t *value, + uint16_t length, void *user_data) +{ + uint8_t *buf = newa(uint8_t, length + 2); + + put_le16(value_handle, buf); + + if (length) + memcpy(buf + 2, value, length); + + attrib_callback_notify(NULL, ATT_OP_HANDLE_NOTIFY, buf, length + 2, + user_data); +} + guint g_attrib_register(GAttrib *attrib, guint8 opcode, guint16 handle, GAttribNotifyFunc func, gpointer user_data, GDestroyNotify notify) @@ -359,6 +380,16 @@ guint g_attrib_register(GAttrib *attrib, guint8 opcode, guint16 handle, queue_push_head(attrib->callbacks, cb); } + if (opcode == ATT_OP_HANDLE_NOTIFY && attrib->client) { + unsigned int id; + + id = bt_gatt_client_register_notify(attrib->client, handle, + NULL, client_notify_cb, cb, + attrib_callbacks_remove); + if (id) + return id; + } + if (opcode == GATTRIB_ALL_REQS) opcode = BT_ATT_ALL_REQUESTS; @@ -410,6 +441,21 @@ gboolean g_attrib_set_mtu(GAttrib *attrib, int mtu) return bt_att_set_mtu(attrib->att, mtu); } +gboolean g_attrib_attach_client(GAttrib *attrib, struct bt_gatt_client *client) +{ + if (!attrib || !client) + return FALSE; + + if (attrib->client) + bt_gatt_client_unref(attrib->client); + + attrib->client = bt_gatt_client_clone(client); + if (!attrib->client) + return FALSE; + + return TRUE; +} + gboolean g_attrib_unregister(GAttrib *attrib, guint id) { if (!attrib) diff --git a/attrib/gattrib.h b/attrib/gattrib.h index c2877d757342..0111bfc3f2fa 100644 --- a/attrib/gattrib.h +++ b/attrib/gattrib.h @@ -19,6 +19,7 @@ extern "C" { #define GATTRIB_ALL_HANDLES 0x0000 struct bt_att; /* Forward declaration for compatibility */ +struct bt_gatt_client; /* Forward declaration for compatibility */ struct _GAttrib; typedef struct _GAttrib GAttrib; @@ -53,6 +54,7 @@ guint g_attrib_register(GAttrib *attrib, guint8 opcode, guint16 handle, uint8_t *g_attrib_get_buffer(GAttrib *attrib, size_t *len); gboolean g_attrib_set_mtu(GAttrib *attrib, int mtu); +gboolean g_attrib_attach_client(GAttrib *attrib, struct bt_gatt_client *client); gboolean g_attrib_unregister(GAttrib *attrib, guint id); gboolean g_attrib_unregister_all(GAttrib *attrib); -- 2.37.3