[PATCH 5/7] android/gatt: Reduce callback data struct scope

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

 



This saves us from redundant data (de)allocation if characteristics are
already cached.
---
 android/gatt.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)

diff --git a/android/gatt.c b/android/gatt.c
index e97677d..fe8ba74 100644
--- a/android/gatt.c
+++ b/android/gatt.c
@@ -1054,7 +1054,6 @@ static bool find_service(int32_t conn_id, struct element_id *service_id,
 static void handle_client_get_characteristic(const void *buf, uint16_t len)
 {
 	const struct hal_cmd_gatt_client_get_characteristic *cmd = buf;
-	struct discover_char_data *cb_data;
 	struct characteristic *ch;
 	struct element_id match_id;
 	struct gatt_device *dev;
@@ -1076,28 +1075,33 @@ static void handle_client_get_characteristic(const void *buf, uint16_t len)
 		goto done;
 	}
 
-	cb_data = new0(struct discover_char_data, 1);
-	if (!cb_data) {
-		error("gatt: Cannot allocate call data");
-		status = HAL_STATUS_FAILED;
-		goto done;
-	}
-
-	cb_data->service = srvc;
-	cb_data->conn_id = dev->conn_id;
-
 	/* Discover all characteristics for services if not cached yet */
 	if (queue_isempty(srvc->chars)) {
-		gatt_discover_char(dev->attrib, srvc->primary.range.start,
+		struct discover_char_data *cb_data =
+					new0(struct discover_char_data, 1);
+
+		if (!cb_data) {
+			error("gatt: Cannot allocate cb data");
+			status = HAL_STATUS_FAILED;
+			goto done;
+		}
+
+		cb_data->service = srvc;
+		cb_data->conn_id = dev->conn_id;
+
+		if (!gatt_discover_char(dev->attrib, srvc->primary.range.start,
 					srvc->primary.range.end, NULL,
-					discover_char_cb, cb_data);
+					discover_char_cb, cb_data)) {
+			free(cb_data);
+
+			status = HAL_STATUS_FAILED;
+			goto done;
+		}
 
 		status = HAL_STATUS_SUCCESS;
 		goto done;
 	}
 
-	free(cb_data);
-
 	if (cmd->number)
 		ch = queue_find(srvc->chars, match_char_by_higher_inst_id,
 					INT_TO_PTR(cmd->gatt_id[0].inst_id));
-- 
1.9.0

--
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