[PATCH 1/3] android/health: Check if device and channel already exists or not

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On channel connect request, check if device is already exists or not.
Also check if channel is already created for remote device or not.
---
 android/health.c | 113 ++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 78 insertions(+), 35 deletions(-)

diff --git a/android/health.c b/android/health.c
index 0462e99..3d852f8 100644
--- a/android/health.c
+++ b/android/health.c
@@ -213,6 +213,22 @@ static void send_channel_state_notify(struct health_channel *channel,
 					sizeof(ev), &ev, fd);
 }
 
+static bool dev_by_addr(const void *data, const void *user_data)
+{
+	const struct health_device *dev = data;
+	const bdaddr_t *addr = user_data;
+
+	return !bacmp(&dev->dst, addr);
+}
+
+static bool channel_by_mdep_id(const void *data, const void *user_data)
+{
+	const struct health_channel *channel = data;
+	uint16_t mdep_id = PTR_TO_INT(user_data);
+
+	return channel->mdep_id == mdep_id;
+}
+
 static bool mdep_by_mdep_role(const void *data, const void *user_data)
 {
 	const struct mdep_cfg *mdep = data;
@@ -1094,8 +1110,20 @@ static int connect_mcl(struct health_channel *channel)
 
 static struct health_device *create_device(uint16_t app_id, const uint8_t *addr)
 {
+	struct health_app *app;
 	struct health_device *dev;
+	bdaddr_t bdaddr;
+
+	app = queue_find(apps, app_by_app_id, INT_TO_PTR(app_id));
+	if (!app)
+		return NULL;
+
+	android2bdaddr(addr, &bdaddr);
+	dev = queue_find(apps, dev_by_addr, &bdaddr);
+	if (dev)
+		return dev;
 
+	/* create device and push it to devices queue */
 	dev = new0(struct health_device, 1);
 	if (!dev)
 		return NULL;
@@ -1103,11 +1131,25 @@ static struct health_device *create_device(uint16_t app_id, const uint8_t *addr)
 	android2bdaddr(addr, &dev->dst);
 	dev->app_id = app_id;
 
+	if (!app->devices) {
+		app->devices = queue_new();
+		if (!app->devices) {
+			free_health_device(dev);
+			return NULL;
+		}
+	}
+
+	if (!queue_push_tail(app->devices, dev)) {
+		free_health_device(dev);
+		return NULL;
+	}
+
 	return dev;
 }
 
 static struct health_channel *create_channel(uint16_t app_id,
-						uint8_t mdep_index)
+						uint8_t mdep_index,
+						struct health_device *dev)
 {
 	struct health_app *app;
 	struct mdep_cfg *mdep;
@@ -1115,22 +1157,45 @@ static struct health_channel *create_channel(uint16_t app_id,
 	uint8_t index;
 	static unsigned int channel_id = 1;
 
+	if (!dev)
+		return NULL;
+
+	index = mdep_index + 1;
+	channel = queue_find(dev->channels, channel_by_mdep_id,
+							INT_TO_PTR(index));
+	if (channel)
+		return channel;
+
 	app = queue_find(apps, app_by_app_id, INT_TO_PTR(app_id));
 	if (!app)
 		return NULL;
 
-	index = mdep_index + 1;
 	mdep = queue_find(app->mdeps, mdep_by_mdep_id, INT_TO_PTR(index));
 	if (!mdep)
 		return NULL;
 
+	/* create channel and push it to device */
 	channel = new0(struct health_channel, 1);
 	if (!channel)
 		return NULL;
 
-	channel->mdep_id = mdep_index;
+	channel->mdep_id = mdep->id;
 	channel->type = mdep->channel_type;
 	channel->id = channel_id++;
+	channel->dev = dev;
+
+	if (!dev->channels) {
+		dev->channels = queue_new();
+		if (!dev->channels) {
+			free_health_channel(channel);
+			return NULL;
+		}
+	}
+
+	if (!queue_push_tail(dev->channels, channel)) {
+		free_health_channel(channel);
+		return NULL;
+	}
 
 	return channel;
 }
@@ -1139,50 +1204,28 @@ static void bt_health_connect_channel(const void *buf, uint16_t len)
 {
 	const struct hal_cmd_health_connect_channel *cmd = buf;
 	struct hal_rsp_health_connect_channel rsp;
-	struct health_app *app;
 	struct health_device *dev = NULL;
 	struct health_channel *channel = NULL;
 
 	DBG("");
 
-	app = queue_find(apps, app_by_app_id, INT_TO_PTR(cmd->app_id));
-	if (!app)
-		goto fail;
-
 	dev = create_device(cmd->app_id, cmd->bdaddr);
-	if (!dev)
-		goto fail;
-
-	channel = create_channel(cmd->app_id, cmd->mdep_index);
-	if (!channel)
-		goto fail;
-
-	channel->dev = dev;
-
-	if (!app->devices) {
-		app->devices = queue_new();
-		if (!app->devices)
-			goto fail;
-	}
-
-	if (!queue_push_tail(app->devices, dev))
+	if (!dev) {
+		error("error creating device");
 		goto fail;
-
-	if (!dev->channels) {
-		dev->channels = queue_new();
-		if (!dev->channels)
-			goto fail;
 	}
 
-	if (!queue_push_tail(dev->channels, channel)) {
-		queue_remove(app->devices, dev);
+	channel = create_channel(cmd->app_id, cmd->mdep_index, dev);
+	if (!channel) {
+		error("error creating channel");
 		goto fail;
 	}
 
-	if (connect_mcl(channel) < 0) {
-		error("error retrieving HDP SDP record");
-		queue_remove(app->devices, dev);
-		goto fail;
+	if (!dev->mcl) {
+		if (connect_mcl(channel) < 0) {
+			error("error retrieving remote SDP record");
+			goto fail;
+		}
 	}
 
 	rsp.channel_id = channel->id;
-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Bluez Devel]     [Linux Wireless Networking]     [Linux Wireless Personal Area Networking]     [Linux ATH6KL]     [Linux USB Devel]     [Linux Media Drivers]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux