All of lore.kernel.org
 help / color / mirror / Atom feed
From: Mariusz Skamra <mariusz.skamra@tieto.com>
To: linux-bluetooth@vger.kernel.org
Cc: Mariusz Skamra <mariusz.skamra@gmail.com>
Subject: [PATCHv2 01/27] android/hidhost: Create bt_gatt_client
Date: Fri,  3 Apr 2015 15:43:30 +0200	[thread overview]
Message-ID: <1428068636-13073-2-git-send-email-mariusz.skamra@tieto.com> (raw)
In-Reply-To: <1428068636-13073-1-git-send-email-mariusz.skamra@tieto.com>

From: Mariusz Skamra <mariusz.skamra@gmail.com>

This patch introduces bt_gatt_client to be used by HoG profile.
As long as the android/gatt doesn't use bt_gatt_client, initialization
can be performed here.
---
 android/Android.mk |  2 ++
 android/hidhost.c  | 75 ++++++++++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 66 insertions(+), 11 deletions(-)

diff --git a/android/Android.mk b/android/Android.mk
index f218805..b9dd1c8 100644
--- a/android/Android.mk
+++ b/android/Android.mk
@@ -72,6 +72,8 @@ LOCAL_SRC_FILES := \
 	bluez/src/shared/crypto.c \
 	bluez/src/shared/uhid.c \
 	bluez/src/shared/att.c \
+	bluez/src/shared/gatt-helpers.c \
+	bluez/src/shared/gatt-client.c \
 	bluez/src/sdpd-database.c \
 	bluez/src/sdpd-service.c \
 	bluez/src/sdpd-request.c \
diff --git a/android/hidhost.c b/android/hidhost.c
index 729b884..26a8013 100644
--- a/android/hidhost.c
+++ b/android/hidhost.c
@@ -36,15 +36,22 @@
 
 #include "btio/btio.h"
 #include "lib/bluetooth.h"
+#include "lib/uuid.h"
 #include "lib/sdp.h"
 #include "lib/sdp_lib.h"
 #include "src/shared/mgmt.h"
+#include "src/shared/queue.h"
 #include "src/shared/util.h"
 #include "src/shared/uhid.h"
+#include "src/shared/att.h"
+#include "src/shared/gatt-db.h"
+#include "src/shared/gatt-client.h"
 #include "src/sdp-client.h"
 #include "src/uuid-helper.h"
 #include "src/log.h"
 
+#include "attrib/gattrib.h"
+
 #include "hal-msg.h"
 #include "ipc-common.h"
 #include "ipc.h"
@@ -113,6 +120,9 @@ struct hid_device {
 	uint8_t		last_hid_msg;
 	struct bt_hog	*hog;
 	int		sec_level;
+	GAttrib		*attrib;
+	struct gatt_db	*db;
+	struct bt_gatt_client *client;
 };
 
 static int device_cmp(gconstpointer s, gconstpointer user_data)
@@ -145,6 +155,9 @@ static void hid_device_free(void *data)
 	if (dev->hog)
 		bt_hog_unref(dev->hog);
 
+	if (dev->db)
+		gatt_db_unref(dev->db);
+
 	g_free(dev->rd_data);
 	g_free(dev);
 }
@@ -792,10 +805,37 @@ fail:
 	hid_device_remove(dev);
 }
 
+static void client_ready_cb(bool success, uint8_t att_ecode, void *user_data)
+{
+	struct hid_device *dev = user_data;
+
+	if (!success) {
+		error("HoG: client not ready");
+		goto fail;
+	}
+
+	if (!bt_hog_attach(dev->hog, dev->attrib)) {
+		error("HoG: unable to attach");
+		goto fail;
+	}
+
+	bt_hid_notify_state(dev, HAL_HIDHOST_STATE_CONNECTED);
+
+	if (!bt_gatt_add_autoconnect(hog_app, &dev->dst))
+		error("HoG: Could not add to autoconnect list");
+
+	return;
+fail:
+	bt_hid_notify_state(dev, HAL_HIDHOST_STATE_DISCONNECTED);
+	hid_device_remove(dev);
+}
+
 static void hog_conn_cb(const bdaddr_t *addr, int err, void *attrib)
 {
 	GSList *l;
 	struct hid_device *dev;
+	struct bt_att *att;
+	uint16_t mtu;
 
 	l = g_slist_find_custom(devices, addr, device_cmp);
 	dev = l ? l->data : NULL;
@@ -807,6 +847,10 @@ static void hog_conn_cb(const bdaddr_t *addr, int err, void *attrib)
 			bt_hid_notify_state(dev,
 						HAL_HIDHOST_STATE_DISCONNECTED);
 			bt_hog_detach(dev->hog);
+			bt_gatt_client_unref(dev->client);
+			g_attrib_unref(dev->attrib);
+			dev->attrib = NULL;
+			dev->client = NULL;
 			return;
 		}
 		goto fail;
@@ -816,34 +860,43 @@ static void hog_conn_cb(const bdaddr_t *addr, int err, void *attrib)
 		dev = hid_device_new(addr);
 
 	if (!dev->hog) {
-		/* TODO: Get device details and primary */
+		/* TODO: Get device details */
 		dev->hog = bt_hog_new_default("bluez-input-device", dev->vendor,
 					dev->product, dev->version, NULL);
 		if (!dev->hog) {
 			error("HoG: unable to create session");
 			goto fail;
 		}
-	}
 
-	if (!bt_hog_attach(dev->hog, attrib)) {
-		error("HoG: unable to attach");
-		goto fail;
+		dev->db = gatt_db_new();
+		if (!dev->db) {
+			error("HoG: unable to create gatt_db");
+			goto fail;
+		}
 	}
 
-	if (!bt_gatt_set_security(addr, BT_IO_SEC_MEDIUM)) {
+	att = g_attrib_get_att(attrib);
+	dev->attrib = g_attrib_ref(attrib);
+	mtu = bt_att_get_mtu(att);
+
+	/* Enable encryption before gatt_client starts discovering services */
+	if (!bt_att_set_sec_level(att, BT_SECURITY_MEDIUM)) {
 		error("Failed to set security level");
 		goto fail;
 	}
 
 	DBG("");
 
-	bt_hid_notify_state(dev, HAL_HIDHOST_STATE_CONNECTED);
-
-	if (!bt_gatt_add_autoconnect(hog_app, &dev->dst))
-		error("hidhost: Could not add to autoconnect list");
+	dev->client = bt_gatt_client_new(dev->db, att, mtu);
+	if (!dev->client) {
+		error("Failed to create gatt_client");
+		goto fail;
+	}
 
+	/* Wait until client is initialized */
+	bt_gatt_client_set_ready_handler(dev->client, client_ready_cb,
+								dev, NULL);
 	return;
-
 fail:
 	bt_hid_notify_state(dev, HAL_HIDHOST_STATE_DISCONNECTED);
 	hid_device_remove(dev);
-- 
1.9.1


  reply	other threads:[~2015-04-03 13:43 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-03 13:43 [PATCHv2 00/27] HoG: Replace gattrib with gatt_client Mariusz Skamra
2015-04-03 13:43 ` Mariusz Skamra [this message]
2015-04-03 16:33   ` [PATCHv2 01/27] android/hidhost: Create bt_gatt_client Michael Janssen
2015-04-03 13:43 ` [PATCHv2 02/27] android/hog: Introduce bt_gatt_client Mariusz Skamra
2015-04-07  7:44   ` Luiz Augusto von Dentz
2015-04-03 13:43 ` [PATCHv2 03/27] shared/gatt-client: Expose gatt_db Mariusz Skamra
2015-04-07  8:01   ` Luiz Augusto von Dentz
2015-04-03 13:43 ` [PATCHv2 04/27] android/hog: Remove tracking gatt operations Mariusz Skamra
2015-04-07  7:50   ` Luiz Augusto von Dentz
2015-04-08  8:47     ` Skamra Mariusz
2015-04-08 10:11       ` Luiz Augusto von Dentz
2015-04-03 13:43 ` [PATCHv2 05/27] android/hog: Use bt_gatt_client to read characteristic value Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 06/27] android/hog: Use bt_gatt_client to register for notifications Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 07/27] android/hog: Use bt_gatt_client to write without response Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 08/27] android/hog: Replace gatt_write_char with bt_gatt_client_write_value Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 09/27] android/hog: Use gatt_db to search for services and characteristics in db Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 10/27] android/hog: Add helper to create uhid device Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 11/27] lib/uuid: Add define for HoG UUID Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 12/27] android/hog: Replace list of reports with a queue of reports Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 13/27] android/hog: Replace GSList of hog instances with queue of instances Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 14/27] android/dis: Remove tracking pending gatt operations Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 15/27] android/dis: Introduce bt_gatt_client Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 16/27] android/scpp: Remove tracking pending gatt operations Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 17/27] android/scpp: Introduce bt_gatt_client Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 18/27] android/scpp: Merge refresh_discovered_cb with iwin_discovered_cb Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 19/27] android/bas: Remove tracking pending gatt operations Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 20/27] android/bas: Start using bt_gatt_client Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 21/27] android/hog: Strip btio dependencies Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 22/27] android/hog: Enable Input Report notifications only if uhid is created Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 23/27] android/bas: Enable Battery Level notifications after reconnection Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 24/27] android/hog: Clean the code from attrib dependencies Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 25/27] android/hog: Remove glib dependencies Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 26/27] android/hog: Remove redundant code Mariusz Skamra
2015-04-03 13:43 ` [PATCHv2 27/27] android/hog: Replace definitions of characteristic uuids with bt_uuids Mariusz Skamra

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=1428068636-13073-2-git-send-email-mariusz.skamra@tieto.com \
    --to=mariusz.skamra@tieto.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=mariusz.skamra@gmail.com \
    /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.