Diff between 6e31be7dbe997c21c2a056e161370a92a4378206 and 764e7ca0d27a536f850355a641cd2b1681148b1b

Changed Files

File Additions Deletions Status
android/gatt.c +23 -19 modified

Full Patch

diff --git a/android/gatt.c b/android/gatt.c
index b02245e..5f68945 100644
--- a/android/gatt.c
+++ b/android/gatt.c
@@ -935,6 +935,28 @@ static struct gatt_device *find_device_by_conn_id(int32_t conn_id)
 	return queue_find(conn_list, match_dev_by_conn_id, INT_TO_PTR(conn_id));
 }
 
+static struct gatt_device *create_device(bdaddr_t *addr)
+{
+	struct gatt_device *dev;
+
+	dev = new0(struct gatt_device, 1);
+	if (!dev)
+		return NULL;
+
+	bacpy(&dev->bdaddr, addr);
+
+	dev->clients = queue_new();
+	dev->services = queue_new();
+
+	if (!dev->clients || !dev->services) {
+		error("gatt: Failed to allocate memory for client");
+		destroy_device(dev);
+		return NULL;
+	}
+
+	return dev;
+}
+
 static void handle_client_connect(const void *buf, uint16_t len)
 {
 	const struct hal_cmd_gatt_client_connect *cmd = buf;
@@ -989,30 +1011,12 @@ static void handle_client_connect(const void *buf, uint16_t len)
 	/* Lets create new gatt device and put it on conn_wait_queue.
 	  * Once it is connected we move it to conn_list
 	  */
-	dev = new0(struct gatt_device, 1);
+	dev = create_device(&addr);
 	if (!dev) {
 		status = HAL_STATUS_FAILED;
 		goto reply;
 	}
 
-	memcpy(&dev->bdaddr, &addr, sizeof(bdaddr_t));
-
-	/* Create queue to keep list of clients for given device*/
-	dev->clients = queue_new();
-	if (!dev->clients) {
-		error("gatt: Cannot create client queue");
-		status = HAL_STATUS_FAILED;
-		goto reply;
-	}
-
-	dev->services = queue_new();
-	if (!dev->services) {
-		error("gatt: Cannot create services queue");
-		queue_destroy(dev->clients, NULL);
-		status = HAL_STATUS_FAILED;
-		goto reply;
-	}
-
 	/* Update client list of device */
 	if (!queue_push_tail(dev->clients, INT_TO_PTR(cmd->client_if))) {
 		error("gatt: Cannot push client on the client queue!?");