linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Szymon Janc <szymon.janc@tieto.com>
To: Mariusz Skamra <mariusz.skamra@tieto.com>
Cc: linux-bluetooth@vger.kernel.org,
	Mariusz Skamra <mariusz.skamra@gmail.com>
Subject: Re: [PATCH 01/28] android/hidhost: Create bt_gatt_client
Date: Thu, 02 Apr 2015 16:16:17 +0200	[thread overview]
Message-ID: <52890061.YpcNOg0Lq6@leonov> (raw)
In-Reply-To: <1427906444-11769-2-git-send-email-mariusz.skamra@tieto.com>

Hi Mariusz,

On Wednesday 01 of April 2015 18:40:17 Mariusz Skamra wrote:
> 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, 65
> insertions(+), 12 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..3da1130 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)
> @@ -142,8 +152,15 @@ static void hid_device_free(void *data)
>  	if (dev->uhid)
>  		bt_uhid_unref(dev->uhid);
> 
> -	if (dev->hog)
> +	if (dev->hog) {
>  		bt_hog_unref(dev->hog);
> +		dev->hog = NULL;
> +	}

dev is freed few lines later so setting dev->hog to NULL shouldn't be needed.

> +
> +	if (dev->db) {
> +		gatt_db_unref(dev->db);
> +		dev->db = NULL;
> +	}

Same here.

> 
>  	g_free(dev->rd_data);
>  	g_free(dev);
> @@ -792,10 +809,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 +851,8 @@ 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);

Shouldn't we NULL those to avoid dangling pointers?

>  			return;
>  		}
>  		goto fail;
> @@ -816,34 +862,39 @@ 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);

This can return NULL.

> 
> +	/* 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);

-- 
BR
Szymon Janc

  reply	other threads:[~2015-04-02 14:16 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-01 16:40 [PATCH 00/28] android/hog Introduce bt_gatt_client Mariusz Skamra
2015-04-01 16:40 ` [PATCH 01/28] android/hidhost: Create bt_gatt_client Mariusz Skamra
2015-04-02 14:16   ` Szymon Janc [this message]
2015-04-01 16:40 ` [PATCH 02/28] android/hog: Introduce bt_gatt_client Mariusz Skamra
2015-04-02 14:17   ` Szymon Janc
2015-04-01 16:40 ` [PATCH 03/28] shared/gatt-client: Expose gatt_db Mariusz Skamra
2015-04-02 14:20   ` Szymon Janc
2015-04-01 16:40 ` [PATCH 04/28] android/hog: Remove tracking gatt operations Mariusz Skamra
2015-04-01 16:40 ` [PATCH 05/28] android/hog: Use bt_gatt_client to read characteristic value Mariusz Skamra
2015-04-01 16:40 ` [PATCH 06/28] android/hog: Use bt_gatt_client to register for notifications Mariusz Skamra
2015-04-01 16:40 ` [PATCH 07/28] android/hog: Use bt_gatt_client to write without response Mariusz Skamra
2015-04-01 16:40 ` [PATCH 08/28] android/hog: Replace gatt_write_char with bt_gatt_client_write_value Mariusz Skamra
2015-04-01 16:40 ` [PATCH 09/28] android/hog: Use gatt_db to search for services and characteristics in db Mariusz Skamra
2015-04-02 15:22   ` Szymon Janc
2015-04-01 16:40 ` [PATCH 10/28] android/hog: Add helper to create uhid device Mariusz Skamra
2015-04-01 16:40 ` [PATCH 11/28] lib/uuid: Add define for HoG UUID Mariusz Skamra
2015-04-01 16:40 ` [PATCH 12/28] android/hog: Replace list of reports with a queue of reports Mariusz Skamra
2015-04-01 16:40 ` [PATCH 13/28] android/hog: Replace definitions of characteristic uuids with bt_uuids Mariusz Skamra
2015-04-02 15:26   ` Szymon Janc
2015-04-01 16:40 ` [PATCH 14/28] android/hog: Replace GSList of hog instances with queue of instances Mariusz Skamra
2015-04-02 15:29   ` Szymon Janc
2015-04-01 16:40 ` [PATCH 15/28] android/dis: Remove tracking pending gatt operations Mariusz Skamra
2015-04-01 16:40 ` [PATCH 16/28] android/dis: Introduce bt_gatt_client Mariusz Skamra
2015-04-01 16:40 ` [PATCH 17/28] android/scpp: Remove tracking pending gatt operations Mariusz Skamra
2015-04-01 16:40 ` [PATCH 18/28] android/scpp: Introduce bt_gatt_client Mariusz Skamra
2015-04-02 15:34   ` Szymon Janc
2015-04-01 16:40 ` [PATCH 19/28] android/scpp: Merge refresh_discovered_cb with iwin_discovered_cb Mariusz Skamra
2015-04-01 16:40 ` [PATCH 20/28] android/bas: Remove tracking pending gatt operations Mariusz Skamra
2015-04-01 16:40 ` [PATCH 21/28] android/bas: Start using bt_gatt_client Mariusz Skamra
2015-04-02 15:37   ` Szymon Janc
2015-04-01 16:40 ` [PATCH 22/28] android/hog: Strip btio dependencies Mariusz Skamra
2015-04-02 15:39   ` Szymon Janc
2015-04-01 16:40 ` [PATCH 23/28] android/hog: Enable Input Report notifications only if uhid is created Mariusz Skamra
2015-04-01 16:40 ` [PATCH 24/28] android/bas: Enable Battery Level notifications after reconnection Mariusz Skamra
2015-04-01 16:40 ` [PATCH 25/28] android/hog: Add MIN definition Mariusz Skamra
2015-04-02 15:41   ` Szymon Janc
2015-04-01 16:40 ` [PATCH 26/28] android/hog: Remove attrib/ Mariusz Skamra
2015-04-02 15:44   ` Szymon Janc
2015-04-01 16:40 ` [PATCH 27/28] android/hog: Remove glib dependencies Mariusz Skamra
2015-04-02 15:45   ` Szymon Janc
2015-04-01 16:40 ` [PATCH 28/28] android/hog: Remove redundant code 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=52890061.YpcNOg0Lq6@leonov \
    --to=szymon.janc@tieto.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=mariusz.skamra@gmail.com \
    --cc=mariusz.skamra@tieto.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).