Skip to contentSkip to conversation

android/hal-bluetooth: Use fixed size buffers for commands

PATCH In review Szymon Janc 2 participants 3 messages Feb 19, 2014 – Feb 21, 2014 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 android/hal-bluetooth: Use fixed size buffers for commands

This make code follow same conventions for all commands.

---
 android/hal-bluetooth.c | 44 +++++++++++++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 17 deletions(-)
android/hal-bluetooth.c +27 -17
index 65432a8..a01229a 100644
@@ -525,8 +525,9 @@ static int get_adapter_property(bt_property_type_t type)
 
 static int set_adapter_property(const bt_property_t *property)
 {
-	char buf[sizeof(struct hal_cmd_set_adapter_prop) + property->len];
+	char buf[BLUEZ_HAL_MTU];
 	struct hal_cmd_set_adapter_prop *cmd = (void *) buf;
+	size_t len;
 
 	DBG("prop: %s", btproperty2str(property));
 
@@ -535,8 +536,10 @@ static int set_adapter_property(const bt_property_t *property)
 
 	adapter_prop_from_hal(property, &cmd->type, &cmd->len, cmd->val);
 
+	len = sizeof(*cmd) + cmd->len;
+
 	return hal_ipc_cmd(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_SET_ADAPTER_PROP,
-				sizeof(*cmd) + cmd->len, cmd, 0, NULL, NULL);
+						len, cmd, 0, NULL, NULL);
 }
 
 static int get_remote_device_properties(bt_bdaddr_t *remote_addr)
@@ -579,8 +582,9 @@ static int get_remote_device_property(bt_bdaddr_t *remote_addr,
 static int set_remote_device_property(bt_bdaddr_t *remote_addr,
 						const bt_property_t *property)
 {
-	struct hal_cmd_set_remote_device_prop *cmd;
-	uint8_t buf[sizeof(*cmd) + property->len];
+	char buf[BLUEZ_HAL_MTU];
+	struct hal_cmd_set_remote_device_prop *cmd = (void *) buf;
+	size_t len;
 
 	DBG("bdaddr: %s prop: %s", bdaddr2str(remote_addr),
 				bt_property_type_t2str(property->type));
@@ -588,8 +592,6 @@ static int set_remote_device_property(bt_bdaddr_t *remote_addr,
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
 
-	cmd = (void *) buf;
-
 	memcpy(cmd->bdaddr, remote_addr, sizeof(cmd->bdaddr));
 
 	/* type match IPC type */
@@ -597,9 +599,11 @@ static int set_remote_device_property(bt_bdaddr_t *remote_addr,
 	cmd->len = property->len;
 	memcpy(cmd->val, property->val, property->len);
 
+	len = sizeof(*cmd) + cmd->len;
+
 	return hal_ipc_cmd(HAL_SERVICE_ID_BLUETOOTH,
 					HAL_OP_SET_REMOTE_DEVICE_PROP,
-					sizeof(buf), cmd, 0, NULL, NULL);
+					len, cmd, 0, NULL, NULL);
 }
 
 static int get_remote_service_record(bt_bdaddr_t *remote_addr, bt_uuid_t *uuid)
@@ -786,40 +790,46 @@ static int dut_mode_configure(uint8_t enable)
 					sizeof(cmd), &cmd, 0, NULL, NULL);
 }
 
-static int dut_mode_send(uint16_t opcode, uint8_t *buf, uint8_t len)
+static int dut_mode_send(uint16_t opcode, uint8_t *buf, uint8_t buf_len)
 {
-	uint8_t cmd_buf[sizeof(struct hal_cmd_dut_mode_send) + len];
+	char cmd_buf[BLUEZ_HAL_MTU];
 	struct hal_cmd_dut_mode_send *cmd = (void *) cmd_buf;
+	size_t len;
 
-	DBG("opcode %u len %u", opcode, len);
+	DBG("opcode %u len %u", opcode, buf_len);
 
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
 
 	cmd->opcode = opcode;
-	cmd->len = len;
+	cmd->len = buf_len;
 	memcpy(cmd->data, buf, cmd->len);
 
+	len = sizeof(*cmd) + cmd->len;
+
 	return hal_ipc_cmd(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_DUT_MODE_SEND,
-					sizeof(cmd_buf), cmd, 0, NULL, NULL);
+						len, cmd, 0, NULL, NULL);
 }
 
-static int le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
+static int le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t buf_len)
 {
-	uint8_t cmd_buf[sizeof(struct hal_cmd_le_test_mode) + len];
+	char cmd_buf[BLUEZ_HAL_MTU];
 	struct hal_cmd_le_test_mode *cmd = (void *) cmd_buf;
+	size_t len;
 
-	DBG("opcode %u len %u", opcode, len);
+	DBG("opcode %u len %u", opcode, buf_len);
 
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
 
 	cmd->opcode = opcode;
-	cmd->len = len;
+	cmd->len = buf_len;
 	memcpy(cmd->data, buf, cmd->len);
 
+	len = sizeof(*cmd) + cmd->len;
+
 	return hal_ipc_cmd(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_LE_TEST_MODE,
-					sizeof(cmd_buf), cmd, 0, NULL, NULL);
+						len, cmd, 0, NULL, NULL);
 }
 
 static int config_hci_snoop_log(uint8_t enable)
Signature · 1 line
1.9.0
#1 lore raw

PATCH 2/2 android/hal-handsfree: Use fixed size buffers for commands

This make code follow same conventions for all commands and simplify code.

---
 android/hal-handsfree.c | 90 +++++++++++++++----------------------------------
 1 file changed, 27 insertions(+), 63 deletions(-)
android/hal-handsfree.c +27 -63
index 601c14f..7964c7d 100644
@@ -360,9 +360,9 @@ static bt_status_t device_status_notification(bthf_network_state_t state,
 
 static bt_status_t cops_response(const char *cops)
 {
-	struct hal_cmd_handsfree_cops_response *cmd;
-	bt_status_t status;
-	int len;
+	char buf[BLUEZ_HAL_MTU];
+	struct hal_cmd_handsfree_cops_response *cmd = (void *) buf;
+	size_t len;
 
 	DBG("");
 
@@ -372,22 +372,14 @@ static bt_status_t cops_response(const char *cops)
 	if (!cops)
 		return BT_STATUS_PARM_INVALID;
 
-	len = sizeof(*cmd) + strlen(cops);
-
-	cmd = malloc(len);
-	if (!cmd)
-		return BT_STATUS_NOMEM;
-
 	cmd->len = strlen(cops);
 	memcpy(cmd->buf, cops, cmd->len);
 
-	status = hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
-				HAL_OP_HANDSFREE_COPS_RESPONSE, len, cmd, 0,
-				NULL, NULL);
-
-	free(cmd);
+	len = sizeof(*cmd) + cmd->len;
 
-	return status;
+	return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
+						HAL_OP_HANDSFREE_COPS_RESPONSE,
+						len, cmd, 0, NULL, NULL);
 }
 
 static bt_status_t cind_response(int svc, int num_active, int num_held,
@@ -416,9 +408,9 @@ static bt_status_t cind_response(int svc, int num_active, int num_held,
 
 static bt_status_t formatted_at_response(const char *rsp)
 {
-	struct hal_cmd_handsfree_formatted_at_response *cmd;
-	bt_status_t status;
-	int len;
+	char buf[BLUEZ_HAL_MTU];
+	struct hal_cmd_handsfree_formatted_at_response *cmd = (void *) buf;
+	size_t len;
 
 	DBG("");
 
@@ -428,22 +420,14 @@ static bt_status_t formatted_at_response(const char *rsp)
 	if (!rsp)
 		return BT_STATUS_PARM_INVALID;
 
-	len = sizeof(*cmd) + strlen(rsp);
-
-	cmd = malloc(len);
-	if (!cmd)
-		return BT_STATUS_NOMEM;
-
 	cmd->len = strlen(rsp);
 	memcpy(cmd->buf, rsp, cmd->len);
 
-	status = hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
-				HAL_OP_HANDSFREE_FORMATTED_AT_RESPONSE, len,
-				cmd, 0, NULL, NULL);
-
-	free(cmd);
+	len = sizeof(*cmd) + cmd->len;
 
-	return status;
+	return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
+					HAL_OP_HANDSFREE_FORMATTED_AT_RESPONSE,
+					len, cmd, 0, NULL, NULL);
 }
 
 static bt_status_t at_response(bthf_at_response_t response, int error)
@@ -470,23 +454,15 @@ static bt_status_t clcc_response(int index, bthf_call_direction_t dir,
 					const char *number,
 					bthf_call_addrtype_t type)
 {
-	struct hal_cmd_handsfree_clcc_response *cmd;
-	bt_status_t status;
-	int len;
+	char buf[BLUEZ_HAL_MTU];
+	struct hal_cmd_handsfree_clcc_response *cmd = (void *) buf;
+	size_t len;
 
 	DBG("");
 
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
 
-	len = sizeof(*cmd);
-	if (number)
-		len += strlen(number);
-
-	cmd = malloc(len);
-	if (!cmd)
-		return BT_STATUS_NOMEM;
-
 	cmd->index = index;
 	cmd->dir = dir;
 	cmd->state = state;
@@ -501,13 +477,11 @@ static bt_status_t clcc_response(int index, bthf_call_direction_t dir,
 		cmd->number_len = 0;
 	}
 
-	status = hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
-					HAL_OP_HANDSFREE_CLCC_RESPONSE, len,
-					cmd, 0, NULL, NULL);
-
-	free(cmd);
+	len = sizeof(*cmd) + cmd->number_len;
 
-	return status;
+	return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
+						HAL_OP_HANDSFREE_CLCC_RESPONSE,
+						len, cmd, 0, NULL, NULL);
 }
 
 static bt_status_t phone_state_change(int num_active, int num_held,
@@ -515,23 +489,15 @@ static bt_status_t phone_state_change(int num_active, int num_held,
 					const char *number,
 					bthf_call_addrtype_t type)
 {
-	struct hal_cmd_handsfree_phone_state_change *cmd;
-	bt_status_t status;
-	int len;
+	char buf[BLUEZ_HAL_MTU];
+	struct hal_cmd_handsfree_phone_state_change *cmd = (void *) buf;
+	size_t len;
 
 	DBG("");
 
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
 
-	len = sizeof(*cmd);
-	if (number)
-		len += strlen(number);
-
-	cmd = malloc(len);
-	if (!cmd)
-		return BT_STATUS_NOMEM;
-
 	cmd->num_active = num_active;
 	cmd->num_held = num_held;
 	cmd->state = state;
@@ -544,13 +510,11 @@ static bt_status_t phone_state_change(int num_active, int num_held,
 		cmd->number_len = 0;
 	}
 
-	status = hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
+	len = sizeof(*cmd) + cmd->number_len;
+
+	return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
 					HAL_OP_HANDSFREE_PHONE_STATE_CHANGE,
 					len, cmd, 0, NULL, NULL);
-
-	free(cmd);
-
-	return status;
 }
 
 static void cleanup(void)
Signature · 1 line
1.9.0
#2 lore raw
In reply to Szymon Janc
Quoted 114 lines · On Wednesday 19 of February 2014 21:49:37 Szymon Janc wrote:

This make code follow same conventions for all commands.

---
 android/hal-bluetooth.c | 44 +++++++++++++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 17 deletions(-)
android/hal-bluetooth.c +24 -16
index 65432a8..a01229a 100644
@@ -525,8 +525,9 @@ static int get_adapter_property(bt_property_type_t type)
 
 static int set_adapter_property(const bt_property_t *property)
 {
-	char buf[sizeof(struct hal_cmd_set_adapter_prop) + property->len];
+	char buf[BLUEZ_HAL_MTU];
 	struct hal_cmd_set_adapter_prop *cmd = (void *) buf;
+	size_t len;
 
 	DBG("prop: %s", btproperty2str(property));
 
@@ -535,8 +536,10 @@ static int set_adapter_property(const bt_property_t *property)
 
 	adapter_prop_from_hal(property, &cmd->type, &cmd->len, cmd->val);
 
+	len = sizeof(*cmd) + cmd->len;
+
 	return hal_ipc_cmd(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_SET_ADAPTER_PROP,
-				sizeof(*cmd) + cmd->len, cmd, 0, NULL, NULL);
+						len, cmd, 0, NULL, NULL);
 }
 
 static int get_remote_device_properties(bt_bdaddr_t *remote_addr)
@@ -579,8 +582,9 @@ static int get_remote_device_property(bt_bdaddr_t *remote_addr,
 static int set_remote_device_property(bt_bdaddr_t *remote_addr,
 						const bt_property_t *property)
 {
-	struct hal_cmd_set_remote_device_prop *cmd;
-	uint8_t buf[sizeof(*cmd) + property->len];
+	char buf[BLUEZ_HAL_MTU];
+	struct hal_cmd_set_remote_device_prop *cmd = (void *) buf;
+	size_t len;
 
 	DBG("bdaddr: %s prop: %s", bdaddr2str(remote_addr),
 				bt_property_type_t2str(property->type));
@@ -588,8 +592,6 @@ static int set_remote_device_property(bt_bdaddr_t *remote_addr,
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
 
-	cmd = (void *) buf;
-
 	memcpy(cmd->bdaddr, remote_addr, sizeof(cmd->bdaddr));
 
 	/* type match IPC type */
@@ -597,9 +599,11 @@ static int set_remote_device_property(bt_bdaddr_t *remote_addr,
 	cmd->len = property->len;
 	memcpy(cmd->val, property->val, property->len);
 
+	len = sizeof(*cmd) + cmd->len;
+
 	return hal_ipc_cmd(HAL_SERVICE_ID_BLUETOOTH,
 					HAL_OP_SET_REMOTE_DEVICE_PROP,
-					sizeof(buf), cmd, 0, NULL, NULL);
+					len, cmd, 0, NULL, NULL);
 }
 
 static int get_remote_service_record(bt_bdaddr_t *remote_addr, bt_uuid_t *uuid)
@@ -786,40 +790,46 @@ static int dut_mode_configure(uint8_t enable)
 					sizeof(cmd), &cmd, 0, NULL, NULL);
 }
 
-static int dut_mode_send(uint16_t opcode, uint8_t *buf, uint8_t len)
+static int dut_mode_send(uint16_t opcode, uint8_t *buf, uint8_t buf_len)
 {
-	uint8_t cmd_buf[sizeof(struct hal_cmd_dut_mode_send) + len];
+	char cmd_buf[BLUEZ_HAL_MTU];
 	struct hal_cmd_dut_mode_send *cmd = (void *) cmd_buf;
+	size_t len;
 
-	DBG("opcode %u len %u", opcode, len);
+	DBG("opcode %u len %u", opcode, buf_len);
 
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
 
 	cmd->opcode = opcode;
-	cmd->len = len;
+	cmd->len = buf_len;
 	memcpy(cmd->data, buf, cmd->len);
 
+	len = sizeof(*cmd) + cmd->len;
+
 	return hal_ipc_cmd(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_DUT_MODE_SEND,
-					sizeof(cmd_buf), cmd, 0, NULL, NULL);
+						len, cmd, 0, NULL, NULL);
 }
 
-static int le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t len)
+static int le_test_mode(uint16_t opcode, uint8_t *buf, uint8_t buf_len)
 {
-	uint8_t cmd_buf[sizeof(struct hal_cmd_le_test_mode) + len];
+	char cmd_buf[BLUEZ_HAL_MTU];
 	struct hal_cmd_le_test_mode *cmd = (void *) cmd_buf;
+	size_t len;
 
-	DBG("opcode %u len %u", opcode, len);
+	DBG("opcode %u len %u", opcode, buf_len);
 
 	if (!interface_ready())
 		return BT_STATUS_NOT_READY;
 
 	cmd->opcode = opcode;
-	cmd->len = len;
+	cmd->len = buf_len;

android/hal-bluetooth.c static int dut_mode_configure(uint8_t enable) · line 835

Quoted 10 lines
android/hal-bluetooth.c +3 -1
 	memcpy(cmd->data, buf, cmd->len);
 
+	len = sizeof(*cmd) + cmd->len;
+
 	return hal_ipc_cmd(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_LE_TEST_MODE,
-					sizeof(cmd_buf), cmd, 0, NULL, NULL);
+						len, cmd, 0, NULL, NULL);
 }
 
 static int config_hci_snoop_log(uint8_t enable)

Both patches are now upstream.

Signature · 2 lines
Best regards, 
Szymon Janc