Skip to contentSkip to conversation

android: Fix opcode parameter type from uint16_t to uint8_t

PATCH In review Ravi kumar Veeramally 3 participants 8 messages Nov 13, 2013 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 android: Fix opcode parameter type from uint16_t to uint8_t

---
 android/hal-a2dp.c      | 2 +-
 android/hal-bluetooth.c | 2 +-
 android/hal-hidhost.c   | 2 +-
 android/hal.h           | 6 +++---
 4 files changed, 6 insertions(+), 6 deletions(-)
android/hal-a2dp.c +1 -1
index f0c301e..4fe7c20 100644
@@ -49,7 +49,7 @@ static void handle_audio_state(void *buf)
 }
 
 /* will be called from notification thread context */
-void bt_notify_a2dp(uint16_t opcode, void *buf, uint16_t len)
+void bt_notify_a2dp(uint8_t opcode, void *buf, uint16_t len)
 {
 	if (!interface_ready())
 		return;
android/hal-bluetooth.c +1 -1
index 3e5d41f..1cfd994 100644
@@ -281,7 +281,7 @@ static void handle_acl_state_changed(void *buf)
 }
 
 /* will be called from notification thread context */
-void bt_notify_adapter(uint16_t opcode, void *buf, uint16_t len)
+void bt_notify_adapter(uint8_t opcode, void *buf, uint16_t len)
 {
 	if (!interface_ready())
 		return;
android/hal-hidhost.c +1 -1
index 97a1aa0..2ce17a3 100644
@@ -88,7 +88,7 @@ static void handle_virtual_unplug(void *buf)
 }
 
 /* will be called from notification thread context */
-void bt_notify_hidhost(uint16_t opcode, void *buf, uint16_t len)
+void bt_notify_hidhost(uint8_t opcode, void *buf, uint16_t len)
 {
 	if (!interface_ready())
 		return;
android/hal.h +3 -3
index 2ce7932..baa4754 100644
@@ -26,8 +26,8 @@ bthh_interface_t *bt_get_hidhost_interface(void);
 btpan_interface_t *bt_get_pan_interface(void);
 btav_interface_t *bt_get_a2dp_interface(void);
 
-void bt_notify_adapter(uint16_t opcode, void *buf, uint16_t len);
+void bt_notify_adapter(uint8_t opcode, void *buf, uint16_t len);
 void bt_thread_associate(void);
 void bt_thread_disassociate(void);
-void bt_notify_hidhost(uint16_t opcode, void *buf, uint16_t len);
-void bt_notify_a2dp(uint16_t opcode, void *buf, uint16_t len);
+void bt_notify_hidhost(uint8_t opcode, void *buf, uint16_t len);
+void bt_notify_a2dp(uint8_t opcode, void *buf, uint16_t len);
Signature · 1 line
1.8.3.2
#1 lore raw

PATCH 2/3 android/pan: Add notify method to PAN notifications

---
 android/hal-pan.c | 6 ++++++
 android/hal.h     | 1 +
 2 files changed, 7 insertions(+)
android/hal-pan.c +6 -0
index bec179f..851c5d2 100644
@@ -31,6 +31,12 @@ static bool interface_ready(void)
 	return cbs != NULL;
 }
 
+void bt_notify_pan(uint8_t opcode, void *buf, uint16_t len)
+{
+	if (!interface_ready())
+		return;
+}
+
 static bt_status_t pan_enable(int local_role)
 {
 	struct hal_cmd_pan_enable cmd;
android/hal.h +1 -0
index baa4754..72090fe 100644
@@ -31,3 +31,4 @@ void bt_thread_associate(void);
 void bt_thread_disassociate(void);
 void bt_notify_hidhost(uint8_t opcode, void *buf, uint16_t len);
 void bt_notify_a2dp(uint8_t opcode, void *buf, uint16_t len);
+void bt_notify_pan(uint8_t opcode, void *buf, uint16_t len);
Signature · 1 line
1.8.3.2
#2 lore raw

PATCH 3/3 android/pan: Handle connection and control state notifications

In reply to Ravi kumar Veeramally
---
 android/hal-pan.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
android/hal-pan.c +31 -0
index 851c5d2..2bc560e 100644
@@ -31,10 +31,41 @@ static bool interface_ready(void)
 	return cbs != NULL;
 }
 
+static void handle_conn_state(void *buf)
+{
+	struct hal_ev_pan_conn_state *ev = buf;
+
+	if (cbs->connection_state_cb)
+		cbs->connection_state_cb(ev->state, ev->status,
+					(bt_bdaddr_t *) ev->bdaddr,
+					ev->local_role, ev->remote_role);
+}
+
+static void handle_ctrl_state(void *buf)
+{
+	struct hal_ev_pan_ctrl_state *ev = buf;
+
+	if (cbs->control_state_cb)
+		cbs->control_state_cb(ev->state, ev->status,
+					ev->local_role, (char *)ev->name);
+}
+
 void bt_notify_pan(uint8_t opcode, void *buf, uint16_t len)
 {
 	if (!interface_ready())
 		return;
+
+	switch (opcode) {
+	case HAL_EV_PAN_CONN_STATE:
+		handle_conn_state(buf);
+		break;
+	case HAL_EV_PAN_CTRL_STATE:
+		handle_ctrl_state(buf);
+		break;
+	default:
+		DBG("Unhandled callback opcode=0x%x", opcode);
+		break;
+	}
 }
 
 static bt_status_t pan_enable(int local_role)
Signature · 1 line
1.8.3.2
#3 lore raw

Re: [PATCH_v3 3/3] android/pan: Handle connection and control state notifications

Hi Ravi,

Quoted 3 lines · Ravi kumar Veeramally
---
 android/hal-pan.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

I've applied the first two patches, but for this one I've got a few questions:

Quoted 34 lines
android/hal-pan.c +22 -0
index 851c5d2..2bc560e 100644
@@ -31,10 +31,41 @@ static bool interface_ready(void)
 	return cbs != NULL;
 }
 
+static void handle_conn_state(void *buf)
+{
+	struct hal_ev_pan_conn_state *ev = buf;
+
+	if (cbs->connection_state_cb)
+		cbs->connection_state_cb(ev->state, ev->status,
+					(bt_bdaddr_t *) ev->bdaddr,
+					ev->local_role, ev->remote_role);
+}
+
+static void handle_ctrl_state(void *buf)
+{
+	struct hal_ev_pan_ctrl_state *ev = buf;
+
+	if (cbs->control_state_cb)
+		cbs->control_state_cb(ev->state, ev->status,
+					ev->local_role, (char *)ev->name);
+}
+
 void bt_notify_pan(uint8_t opcode, void *buf, uint16_t len)
 {
 	if (!interface_ready())
 		return;
+
+	switch (opcode) {
+	case HAL_EV_PAN_CONN_STATE:

android/hal-pan.c static bool interface_ready(void) · line 69

Quoted 10 lines
android/hal-pan.c +9 -0
+		handle_conn_state(buf);
+		break;
+	case HAL_EV_PAN_CTRL_STATE:
+		handle_ctrl_state(buf);
+		break;
+	default:
+		DBG("Unhandled callback opcode=0x%x", opcode);
+		break;
+	}
 }

What I don't like about this is that you're not pushing the data length to the handler functions. If you did that the handler functions could:

	if (len < sizeof(*ev))
		return;

Instead of return we could also just abort - what's the general policy on the HAL side regarding invalid data from the daemon? How does this relate to the work Szymon is doing to add proper checks for the IPC data? Is that only for the daemon side?

Are we missing similar checks in other HALs too?

Johan

#4 lore raw

Re: [PATCH_v3 3/3] android/pan: Handle connection and control state notifications

Hi Johan,

Quoted 59 lines · Johan Hedberg

Hi Ravi,

Quoted 3 lines · Ravi kumar Veeramally
---
  android/hal-pan.c | 31 +++++++++++++++++++++++++++++++
  1 file changed, 31 insertions(+)

I've applied the first two patches, but for this one I've got a few questions:

Quoted 34 lines
android/hal-pan.c +22 -0
index 851c5d2..2bc560e 100644
@@ -31,10 +31,41 @@ static bool interface_ready(void)
  	return cbs != NULL;
  }
 
+static void handle_conn_state(void *buf)
+{
+	struct hal_ev_pan_conn_state *ev = buf;
+
+	if (cbs->connection_state_cb)
+		cbs->connection_state_cb(ev->state, ev->status,
+					(bt_bdaddr_t *) ev->bdaddr,
+					ev->local_role, ev->remote_role);
+}
+
+static void handle_ctrl_state(void *buf)
+{
+	struct hal_ev_pan_ctrl_state *ev = buf;
+
+	if (cbs->control_state_cb)
+		cbs->control_state_cb(ev->state, ev->status,
+					ev->local_role, (char *)ev->name);
+}
+
  void bt_notify_pan(uint8_t opcode, void *buf, uint16_t len)
  {
  	if (!interface_ready())
  		return;
+
+	switch (opcode) {
+	case HAL_EV_PAN_CONN_STATE:

android/hal-pan.c static bool interface_ready(void) · line 69

Quoted 10 lines
android/hal-pan.c +9 -0
+		handle_conn_state(buf);
+		break;
+	case HAL_EV_PAN_CTRL_STATE:
+		handle_ctrl_state(buf);
+		break;
+	default:
+		DBG("Unhandled callback opcode=0x%x", opcode);
+		break;
+	}
  }

What I don't like about this is that you're not pushing the data length to the handler functions. If you did that the handler functions could:

	if (len < sizeof(*ev))
		return;
Quoted 6 lines

Instead of return we could also just abort - what's the general policy on the HAL side regarding invalid data from the daemon? How does this relate to the work Szymon is doing to add proper checks for the IPC data? Is that only for the daemon side?

Are we missing similar checks in other HALs too?

Yes, we are not doing similar checks in other HALs
(hal-bluetooth/a2dp/hid/) too.
Very few places in hal-bluetooth length is passing but validation is
not done.
I will fix them all and send you the patch.

Thanks,
Ravi.

#5 lore raw

Re: [PATCH_v3 3/3] android/pan: Handle connection and control state notifications

Hi,

Quoted 24 lines
Quoted 24 lines

android/hal-pan.c static bool interface_ready(void) · line 69

Quoted 13 lines
android/hal-pan.c +12 -0
+
+	switch (opcode) {
+	case HAL_EV_PAN_CONN_STATE:
+		handle_conn_state(buf);
+		break;
+	case HAL_EV_PAN_CTRL_STATE:
+		handle_ctrl_state(buf);
+		break;
+	default:
+		DBG("Unhandled callback opcode=0x%x", opcode);
+		break;
+	}
  }

What I don't like about this is that you're not pushing the data length to the handler functions. If you did that the handler functions could:

	if (len < sizeof(*ev))
		return;

Instead of return we could also just abort - what's the general policy on the HAL side regarding invalid data from the daemon? How does this relate to the work Szymon is doing to add proper checks for the IPC data? Is that only for the daemon side?

Quoted 7 lines
Quoted 1 line

Are we missing similar checks in other HALs too?

Yes, we are not doing similar checks in other HALs
(hal-bluetooth/a2dp/hid/) too.
Very few places in hal-bluetooth length is passing but validation is
not done.
I will fix them all and send you the patch.

As mentioned in my RFC, messages have very similar format and can be checked in single place using some common macros. I'm now working on addressing Johan comments in that RFC. As suggested I'm going to use tables of handlers instead switch-case and this should also allow to refactor current code to avoid switch-cases we currently have. Services will simply register own tables of handlers for service they implement. Idea is that check will be done in common place and then handlers will have form of void handle_foo(struct foo *ev) with guarantee that passed command/event is memory size valid.

Plan is to make this generic code use both by hal and daemon.

So, I'm not sure if it makes much sense to fix passing buf+len to all handlers we have now.

If you have other ideas for this please comment.

Signature · 2 lines
BR
Szymon Janc
#6 lore raw

Re: [PATCH_v3 3/3] android/pan: Handle connection and control state notifications

Quoted 41 lines · Szymon Janc

Hi,

Quoted 24 lines
Quoted 24 lines

android/hal-pan.c static bool interface_ready(void) · line 69

Quoted 13 lines
android/hal-pan.c +12 -0
+
+	switch (opcode) {
+	case HAL_EV_PAN_CONN_STATE:
+		handle_conn_state(buf);
+		break;
+	case HAL_EV_PAN_CTRL_STATE:
+		handle_ctrl_state(buf);
+		break;
+	default:
+		DBG("Unhandled callback opcode=0x%x", opcode);
+		break;
+	}
   }

What I don't like about this is that you're not pushing the data length to the handler functions. If you did that the handler functions could:

	if (len < sizeof(*ev))
		return;

Instead of return we could also just abort - what's the general policy on the HAL side regarding invalid data from the daemon? How does this relate to the work Szymon is doing to add proper checks for the IPC data? Is that only for the daemon side?

Quoted 6 lines
Quoted 1 line

Are we missing similar checks in other HALs too?

Yes, we are not doing similar checks in other HALs
(hal-bluetooth/a2dp/hid/) too.
Very few places in hal-bluetooth length is passing but validation is
not done.
I will fix them all and send you the patch.

As mentioned in my RFC, messages have very similar format and can be checked in single place using some common macros. I'm now working on addressing Johan comments in that RFC. As suggested I'm going to use tables of handlers instead switch-case and this should also allow to refactor current code to avoid switch-cases we currently have. Services will simply register own tables of handlers for service they implement. Idea is that check will be done in common place and then handlers will have form of void handle_foo(struct foo *ev) with guarantee that passed command/event is memory size valid.

Quoted 7 lines

Plan is to make this generic code use both by hal and daemon.

So, I'm not sure if it makes much sense to fix passing buf+len to all handlers we have now.

If you have other ideas for this please comment.

Ok, good to know.
Johan: Can you now apply my left over patch?

Thanks,
Ravi.

#7 lore raw

Re: [PATCH_v3 3/3] android/pan: Handle connection and control state notifications

Hi Ravi,

Quoted 44 lines · Ravi Kumar Veeramally
Quoted 43 lines · Szymon Janc

Hi,

Quoted 24 lines
Quoted 24 lines

android/hal-pan.c static bool interface_ready(void) · line 69

Quoted 13 lines
android/hal-pan.c +12 -0
+
+	switch (opcode) {
+	case HAL_EV_PAN_CONN_STATE:
+		handle_conn_state(buf);
+		break;
+	case HAL_EV_PAN_CTRL_STATE:
+		handle_ctrl_state(buf);
+		break;
+	default:
+		DBG("Unhandled callback opcode=0x%x", opcode);
+		break;
+	}
  }

What I don't like about this is that you're not pushing the data length to the handler functions. If you did that the handler functions could:

	if (len < sizeof(*ev))
		return;

Instead of return we could also just abort - what's the general policy on the HAL side regarding invalid data from the daemon? How does this relate to the work Szymon is doing to add proper checks for the IPC data? Is that only for the daemon side?

Quoted 6 lines
Quoted 1 line

Are we missing similar checks in other HALs too?

Yes, we are not doing similar checks in other HALs
(hal-bluetooth/a2dp/hid/) too.
Very few places in hal-bluetooth length is passing but validation is
not done.
I will fix them all and send you the patch.

As mentioned in my RFC, messages have very similar format and can be checked in single place using some common macros. I'm now working on addressing Johan comments in that RFC. As suggested I'm going to use tables of handlers instead switch-case and this should also allow to refactor current code to avoid switch-cases we currently have. Services will simply register own tables of handlers for service they implement. Idea is that check will be done in common place and then handlers will have form of void handle_foo(struct foo *ev) with guarantee that passed command/event is memory size valid.

Plan is to make this generic code use both by hal and daemon.

Quoted 7 lines
Quoted 5 lines

So, I'm not sure if it makes much sense to fix passing buf+len to all handlers we have now.

If you have other ideas for this please comment.

Ok, good to know.
Johan: Can you now apply my left over patch?

Yes, it's already done, however I'd really like to see the patches to fix all this asap.

Johan