All of lore.kernel.org
 help / color / mirror / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
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	[thread overview]
Message-ID: <20230105220944.2373424-3-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20230105220944.2373424-1-luiz.dentz@gmail.com>

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

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 <glib.h>
 
 #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


  parent reply	other threads:[~2023-01-05 22:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-05 22:09 [PATCH BlueZ 1/4] shared/gatt-client: Use parent debug_callback if not set on clone Luiz Augusto von Dentz
2023-01-05 22:09 ` [PATCH BlueZ 2/4] shared/gatt-client: Allow registering with NULL callback Luiz Augusto von Dentz
2023-01-05 22:09 ` Luiz Augusto von Dentz [this message]
2023-01-05 22:09 ` [PATCH BlueZ 4/4] hog-lib: Fix not handling BT_ATT_OP_HANDLE_NFY_MULT Luiz Augusto von Dentz
2023-01-06  0:03 ` [BlueZ,1/4] shared/gatt-client: Use parent debug_callback if not set on clone bluez.test.bot
2023-01-06  0:11   ` Luiz Augusto von Dentz
2023-01-06 20:00 ` [PATCH BlueZ 1/4] " patchwork-bot+bluetooth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230105220944.2373424-3-luiz.dentz@gmail.com \
    --to=luiz.dentz@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.