Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH 2/4] android/a2dp: Fix possible NULL dereference
From: Andrei Emeltchenko @ 2013-12-02 15:46 UTC (permalink / raw)
  To: linux-bluetooth
In-Reply-To: <1385999188-1546-1-git-send-email-Andrei.Emeltchenko.news@gmail.com>

From: Andrei Emeltchenko <andrei.emeltchenko@intel.com>

Since a2dp_record may return NULL, check return value. This
silences static analysers tools.
---
 android/a2dp.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/android/a2dp.c b/android/a2dp.c
index 98c138e..324a211 100644
--- a/android/a2dp.c
+++ b/android/a2dp.c
@@ -366,9 +366,10 @@ bool bt_a2dp_register(const bdaddr_t *addr)
 	}
 
 	rec = a2dp_record();
-	if (bt_adapter_add_record(rec, SVC_HINT_CAPTURING) < 0) {
+	if (!rec || bt_adapter_add_record(rec, SVC_HINT_CAPTURING) < 0) {
 		error("Failed to register on A2DP record");
-		sdp_record_free(rec);
+		if (rec)
+			sdp_record_free(rec);
 		g_io_channel_shutdown(server, TRUE, NULL);
 		g_io_channel_unref(server);
 		server = NULL;
-- 
1.8.3.2


^ permalink raw reply related

* [PATCH 1/4] android/socket: Cleanup sockets on unregister
From: Andrei Emeltchenko @ 2013-12-02 15:46 UTC (permalink / raw)
  To: linux-bluetooth

From: Andrei Emeltchenko <andrei.emeltchenko@intel.com>

This cleans up rfsock structures closing all sockets and making general cleanup
for servers and for connections. This will be called form socket unregister.
---
 android/socket.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/android/socket.c b/android/socket.c
index 76b40c8..4502e90 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -93,8 +93,10 @@ static struct rfcomm_sock *create_rfsock(int sock, int *hal_fd)
 	return rfsock;
 }
 
-static void cleanup_rfsock(struct rfcomm_sock *rfsock)
+static void cleanup_rfsock(gpointer data)
 {
+	struct rfcomm_sock *rfsock = data;
+
 	DBG("rfsock: %p fd %d real_sock %d chan %u",
 		rfsock, rfsock->fd, rfsock->real_sock, rfsock->channel);
 
@@ -936,5 +938,8 @@ void bt_socket_unregister(void)
 {
 	DBG("");
 
+	g_list_free_full(connections, cleanup_rfsock);
+	g_list_free_full(servers, cleanup_rfsock);
+
 	ipc_unregister(HAL_SERVICE_ID_SOCK);
 }
-- 
1.8.3.2


^ permalink raw reply related

* [PATCH] android: Use G_N_ELEMENTS macro for table elements calculation
From: Szymon Janc @ 2013-12-02 15:17 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc

It is more common in codebase to use this macro instead of opencoded
(sizeof(foo)/sizeof(foo[0])).
---
 android/a2dp.c      | 2 +-
 android/bluetooth.c | 2 +-
 android/hidhost.c   | 2 +-
 android/main.c      | 2 +-
 android/pan.c       | 2 +-
 android/socket.c    | 2 +-
 6 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/android/a2dp.c b/android/a2dp.c
index 98c138e..cee4bfa 100644
--- a/android/a2dp.c
+++ b/android/a2dp.c
@@ -377,7 +377,7 @@ bool bt_a2dp_register(const bdaddr_t *addr)
 	record_id = rec->handle;
 
 	ipc_register(HAL_SERVICE_ID_A2DP, cmd_handlers,
-				sizeof(cmd_handlers)/sizeof(cmd_handlers[0]));
+						G_N_ELEMENTS(cmd_handlers));
 
 	return true;
 }
diff --git a/android/bluetooth.c b/android/bluetooth.c
index a39e7bf..6174b1f 100644
--- a/android/bluetooth.c
+++ b/android/bluetooth.c
@@ -2418,7 +2418,7 @@ void bt_bluetooth_register(void)
 	DBG("");
 
 	ipc_register(HAL_SERVICE_ID_BLUETOOTH, cmd_handlers,
-				sizeof(cmd_handlers)/sizeof(cmd_handlers[0]));
+						G_N_ELEMENTS(cmd_handlers));
 }
 
 void bt_bluetooth_unregister(void)
diff --git a/android/hidhost.c b/android/hidhost.c
index 38194d0..8bfdfed 100644
--- a/android/hidhost.c
+++ b/android/hidhost.c
@@ -1279,7 +1279,7 @@ bool bt_hid_register(const bdaddr_t *addr)
 	}
 
 	ipc_register(HAL_SERVICE_ID_HIDHOST, cmd_handlers,
-				sizeof(cmd_handlers)/sizeof(cmd_handlers[0]));
+						G_N_ELEMENTS(cmd_handlers));
 
 	return true;
 }
diff --git a/android/main.c b/android/main.c
index b9655c5..5210b4b 100644
--- a/android/main.c
+++ b/android/main.c
@@ -430,7 +430,7 @@ int main(int argc, char *argv[])
 	start_sdp_server(0, 0);
 
 	ipc_register(HAL_SERVICE_ID_CORE, cmd_handlers,
-				sizeof(cmd_handlers)/sizeof(cmd_handlers[0]));
+						G_N_ELEMENTS(cmd_handlers));
 
 	DBG("Entering main loop");
 
diff --git a/android/pan.c b/android/pan.c
index 3270aa4..fe6ee26 100644
--- a/android/pan.c
+++ b/android/pan.c
@@ -322,7 +322,7 @@ bool bt_pan_register(const bdaddr_t *addr)
 	}
 
 	ipc_register(HAL_SERVICE_ID_PAN, cmd_handlers,
-				sizeof(cmd_handlers)/sizeof(cmd_handlers[0]));
+						G_N_ELEMENTS(cmd_handlers));
 
 	return true;
 }
diff --git a/android/socket.c b/android/socket.c
index 76b40c8..c9eca44 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -929,7 +929,7 @@ void bt_socket_register(const bdaddr_t *addr)
 
 	bacpy(&adapter_addr, addr);
 	ipc_register(HAL_SERVICE_ID_SOCK, cmd_handlers,
-				sizeof(cmd_handlers)/sizeof(cmd_handlers[0]));
+						G_N_ELEMENTS(cmd_handlers));
 }
 
 void bt_socket_unregister(void)
-- 
1.8.3.2


^ permalink raw reply related

* [PATCH] android: Move sockets handling from main to IPC code
From: Szymon Janc @ 2013-12-02 14:55 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc

This moves IO handling to IPC code making it fully responsible for
creating and veryfing IPC messages exchange.
---

This should be applied on top of IPC daemon improvements.

 android/ipc.c  | 273 ++++++++++++++++++++++++++++++++++++++++++---------------
 android/ipc.h  |   4 +-
 android/main.c | 151 +------------------------------
 3 files changed, 207 insertions(+), 221 deletions(-)

diff --git a/android/ipc.c b/android/ipc.c
index 56f328b..1d369a8 100644
--- a/android/ipc.c
+++ b/android/ipc.c
@@ -32,6 +32,10 @@
 #include <signal.h>
 #include <stdbool.h>
 #include <sys/socket.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+#include <glib.h>
 
 #include "hal-msg.h"
 #include "ipc.h"
@@ -44,19 +48,202 @@ struct service_handler {
 
 static struct service_handler services[HAL_SERVICE_ID_MAX + 1];
 
-static int cmd_sk = -1;
-static int notif_sk = -1;
+static GIOChannel *cmd_io = NULL;
+static GIOChannel *notif_io = NULL;
 
-void ipc_init(int command_sk, int notification_sk)
+static void ipc_handle_msg(const void *buf, ssize_t len)
 {
-	cmd_sk = command_sk;
-	notif_sk = notification_sk;
+	const struct hal_hdr *msg = buf;
+	const struct ipc_handler *handler;
+
+	if (len < (ssize_t) sizeof(*msg)) {
+		error("IPC: message too small (%zd bytes), terminating", len);
+		raise(SIGTERM);
+		return;
+	}
+
+	if (len != (ssize_t) (sizeof(*msg) + msg->len)) {
+		error("IPC: message malformed (%zd bytes), terminating", len);
+		raise(SIGTERM);
+		return;
+	}
+
+	/* if service is valid */
+	if (msg->service_id > HAL_SERVICE_ID_MAX) {
+		error("IPC: unknown service (0x%x), terminating",
+							msg->service_id);
+		raise(SIGTERM);
+		return;
+	}
+
+	/* if service is registered */
+	if (!services[msg->service_id].handler) {
+		error("IPC: unregistered service (0x%x), terminating",
+							msg->service_id);
+		raise(SIGTERM);
+		return;
+	}
+
+	/* if opcode is valid */
+	if (msg->opcode == HAL_OP_STATUS ||
+			msg->opcode > services[msg->service_id].size) {
+		error("IPC: invalid opcode 0x%x for service 0x%x, terminating",
+						msg->opcode, msg->service_id);
+		raise(SIGTERM);
+		return;
+	}
+
+	/* opcode is table offset + 1 */
+	handler = &services[msg->service_id].handler[msg->opcode - 1];
+
+	/* if payload size is valid */
+	if ((handler->var_len && handler->data_len > msg->len) ||
+			(!handler->var_len && handler->data_len != msg->len)) {
+		error("IPC: size invalid opcode 0x%x service 0x%x, terminating",
+						msg->service_id, msg->opcode);
+		raise(SIGTERM);
+		return;
+	}
+
+	handler->handler(msg->payload, msg->len);
+}
+
+static gboolean cmd_watch_cb(GIOChannel *io, GIOCondition cond,
+							gpointer user_data)
+{
+	char buf[BLUEZ_HAL_MTU];
+	ssize_t ret;
+	int fd;
+
+	if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
+		info("IPC: command socket closed, terminating");
+		goto fail;
+	}
+
+	fd = g_io_channel_unix_get_fd(io);
+
+	ret = read(fd, buf, sizeof(buf));
+	if (ret < 0) {
+		error("IPC: command read failed, terminating (%s)",
+							strerror(errno));
+		goto fail;
+	}
+
+	ipc_handle_msg(buf, ret);
+	return TRUE;
+
+fail:
+	raise(SIGTERM);
+	return FALSE;
+}
+
+static gboolean notif_watch_cb(GIOChannel *io, GIOCondition cond,
+							gpointer user_data)
+{
+	info("IPC: notification socket closed, terminating");
+	raise(SIGTERM);
+
+	return FALSE;
+}
+
+static GIOChannel *connect_hal(GIOFunc connect_cb)
+{
+	struct sockaddr_un addr;
+	GIOCondition cond;
+	GIOChannel *io;
+	int sk;
+
+	sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
+	if (sk < 0) {
+		error("IPC: failed to create socket: %d (%s)", errno,
+							strerror(errno));
+		return NULL;
+	}
+
+	io = g_io_channel_unix_new(sk);
+
+	g_io_channel_set_close_on_unref(io, TRUE);
+	g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
+
+	memset(&addr, 0, sizeof(addr));
+	addr.sun_family = AF_UNIX;
+
+	memcpy(addr.sun_path, BLUEZ_HAL_SK_PATH, sizeof(BLUEZ_HAL_SK_PATH));
+
+	if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+		error("IPC: failed to connect HAL socket: %d (%s)", errno,
+							strerror(errno));
+		g_io_channel_unref(io);
+		return NULL;
+	}
+
+	cond = G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
+
+	g_io_add_watch(io, cond, connect_cb, NULL);
+
+	return io;
+}
+
+static gboolean notif_connect_cb(GIOChannel *io, GIOCondition cond,
+							gpointer user_data)
+{
+	DBG("");
+
+	if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
+		error("IPC: notification socket connect failed, terminating");
+		raise(SIGTERM);
+		return FALSE;
+	}
+
+	cond = G_IO_ERR | G_IO_HUP | G_IO_NVAL;
+
+	g_io_add_watch(io, cond, notif_watch_cb, NULL);
+
+	cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
+
+	g_io_add_watch(cmd_io, cond, cmd_watch_cb, NULL);
+
+	info("IPC: successfully connected");
+
+	return FALSE;
+}
+
+static gboolean cmd_connect_cb(GIOChannel *io, GIOCondition cond,
+							gpointer user_data)
+{
+	DBG("");
+
+	if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
+		error("IPC: command socket connect failed, terminating");
+		raise(SIGTERM);
+		return FALSE;
+	}
+
+	notif_io = connect_hal(notif_connect_cb);
+	if (!notif_io)
+		raise(SIGTERM);
+
+	return FALSE;
+}
+
+void ipc_init(void)
+{
+	cmd_io = connect_hal(cmd_connect_cb);
 }
 
 void ipc_cleanup(void)
 {
-	cmd_sk = -1;
-	notif_sk = -1;
+	if (cmd_io) {
+		g_io_channel_shutdown(cmd_io, TRUE, NULL);
+		g_io_channel_unref(cmd_io);
+		cmd_io = NULL;
+	}
+
+	if (notif_io) {
+		g_io_channel_shutdown(notif_io, TRUE, NULL);
+		g_io_channel_unref(notif_io);
+		notif_io = NULL;
+	}
 }
 
 static void ipc_send(int sk, uint8_t service_id, uint8_t opcode, uint16_t len,
@@ -107,30 +294,35 @@ static void ipc_send(int sk, uint8_t service_id, uint8_t opcode, uint16_t len,
 void ipc_send_rsp(uint8_t service_id, uint8_t opcode, uint8_t status)
 {
 	struct hal_status s;
+	int sk;
+
+	sk = g_io_channel_unix_get_fd(cmd_io);
 
 	if (status == HAL_STATUS_SUCCESS) {
-		ipc_send(cmd_sk, service_id, opcode, 0, NULL, -1);
+		ipc_send(sk, service_id, opcode, 0, NULL, -1);
 		return;
 	}
 
 	s.code = status;
 
-	ipc_send(cmd_sk, service_id, HAL_OP_STATUS, sizeof(s), &s, -1);
+	ipc_send(sk, service_id, HAL_OP_STATUS, sizeof(s), &s, -1);
 }
 
 void ipc_send_rsp_full(uint8_t service_id, uint8_t opcode, uint16_t len,
 							void *param, int fd)
 {
-	ipc_send(cmd_sk, service_id, opcode, len, param, fd);
+	ipc_send(g_io_channel_unix_get_fd(cmd_io), service_id, opcode, len,
+								param, fd);
 }
 
 void ipc_send_notif(uint8_t service_id, uint8_t opcode,  uint16_t len,
 								void *param)
 {
-	if (notif_sk < 0)
+	if (!notif_io)
 		return;
 
-	ipc_send(notif_sk, service_id, opcode, len, param, -1);
+	ipc_send(g_io_channel_unix_get_fd(notif_io), service_id, opcode, len,
+								param, -1);
 }
 
 void ipc_register(uint8_t service, const struct ipc_handler *handlers,
@@ -145,60 +337,3 @@ void ipc_unregister(uint8_t service)
 	services[service].handler = NULL;
 	services[service].size = 0;
 }
-
-void ipc_handle_msg(const void *buf, ssize_t len)
-{
-	const struct hal_hdr *msg = buf;
-	const struct ipc_handler *handler;
-
-	if (len < (ssize_t) sizeof(*msg)) {
-		error("IPC: message too small (%zd bytes), terminating", len);
-		raise(SIGTERM);
-		return;
-	}
-
-	if (len != (ssize_t) (sizeof(*msg) + msg->len)) {
-		error("IPC: message malformed (%zd bytes), terminating", len);
-		raise(SIGTERM);
-		return;
-	}
-
-	/* if service is valid */
-	if (msg->service_id > HAL_SERVICE_ID_MAX) {
-		error("IPC: unknown service (0x%x), terminating",
-							msg->service_id);
-		raise(SIGTERM);
-		return;
-	}
-
-	/* if service is registered */
-	if (!services[msg->service_id].handler) {
-		error("IPC: unregistered service (0x%x), terminating",
-							msg->service_id);
-		raise(SIGTERM);
-		return;
-	}
-
-	/* if opcode is valid */
-	if (msg->opcode == HAL_OP_STATUS ||
-			msg->opcode > services[msg->service_id].size) {
-		error("IPC: invalid opcode 0x%x for service 0x%x, terminating",
-						msg->opcode, msg->service_id);
-		raise(SIGTERM);
-		return;
-	}
-
-	/* opcode is table offset + 1 */
-	handler = &services[msg->service_id].handler[msg->opcode - 1];
-
-	/* if payload size is valid */
-	if ((handler->var_len && handler->data_len > msg->len) ||
-			(!handler->var_len && handler->data_len != msg->len)) {
-		error("IPC: size invalid opcode 0x%x service 0x%x, terminating",
-						msg->service_id, msg->opcode);
-		raise(SIGTERM);
-		return;
-	}
-
-	handler->handler(msg->payload, msg->len);
-}
diff --git a/android/ipc.h b/android/ipc.h
index 9d0c5e1..6cd102b 100644
--- a/android/ipc.h
+++ b/android/ipc.h
@@ -26,7 +26,7 @@ struct ipc_handler {
 	bool var_len;
 	size_t data_len;
 };
-void ipc_init(int command_sk, int notification_sk);
+void ipc_init(void);
 void ipc_cleanup(void);
 
 void ipc_send_rsp(uint8_t service_id, uint8_t opcode, uint8_t status);
@@ -37,5 +37,3 @@ void ipc_send_notif(uint8_t service_id, uint8_t opcode,  uint16_t len,
 void ipc_register(uint8_t service, const struct ipc_handler *handlers,
 								uint8_t size);
 void ipc_unregister(uint8_t service);
-
-void ipc_handle_msg(const void *buf, ssize_t len);
diff --git a/android/main.c b/android/main.c
index c0f8901..b9655c5 100644
--- a/android/main.c
+++ b/android/main.c
@@ -36,8 +36,6 @@
 #include <unistd.h>
 
 #include <sys/signalfd.h>
-#include <sys/socket.h>
-#include <sys/un.h>
 
 #include <glib.h>
 
@@ -69,9 +67,6 @@ static bdaddr_t adapter_bdaddr;
 
 static GMainLoop *event_loop;
 
-static GIOChannel *hal_cmd_io = NULL;
-static GIOChannel *hal_notif_io = NULL;
-
 static bool services[HAL_SERVICE_ID_MAX + 1] = { false };
 
 static void service_register(const void *buf, uint16_t len)
@@ -209,127 +204,6 @@ static void stop_bluetooth(void)
 	g_timeout_add_seconds(SHUTDOWN_GRACE_SECONDS, quit_eventloop, NULL);
 }
 
-static gboolean cmd_watch_cb(GIOChannel *io, GIOCondition cond,
-							gpointer user_data)
-{
-	char buf[BLUEZ_HAL_MTU];
-	ssize_t ret;
-	int fd;
-
-	if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
-		info("HAL command socket closed, terminating");
-		goto fail;
-	}
-
-	fd = g_io_channel_unix_get_fd(io);
-
-	ret = read(fd, buf, sizeof(buf));
-	if (ret < 0) {
-		error("HAL command read failed, terminating (%s)",
-							strerror(errno));
-		goto fail;
-	}
-
-	ipc_handle_msg(buf, ret);
-	return TRUE;
-
-fail:
-	stop_bluetooth();
-	return FALSE;
-}
-
-static gboolean notif_watch_cb(GIOChannel *io, GIOCondition cond,
-							gpointer user_data)
-{
-	info("HAL notification socket closed, terminating");
-	stop_bluetooth();
-
-	return FALSE;
-}
-
-static GIOChannel *connect_hal(GIOFunc connect_cb)
-{
-	struct sockaddr_un addr;
-	GIOCondition cond;
-	GIOChannel *io;
-	int sk;
-
-	sk = socket(PF_LOCAL, SOCK_SEQPACKET, 0);
-	if (sk < 0) {
-		error("Failed to create socket: %d (%s)", errno,
-							strerror(errno));
-		return NULL;
-	}
-
-	io = g_io_channel_unix_new(sk);
-
-	g_io_channel_set_close_on_unref(io, TRUE);
-	g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
-
-	memset(&addr, 0, sizeof(addr));
-	addr.sun_family = AF_UNIX;
-
-	memcpy(addr.sun_path, BLUEZ_HAL_SK_PATH, sizeof(BLUEZ_HAL_SK_PATH));
-
-	if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
-		error("Failed to connect HAL socket: %d (%s)", errno,
-							strerror(errno));
-		g_io_channel_unref(io);
-		return NULL;
-	}
-
-	cond = G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
-
-	g_io_add_watch(io, cond, connect_cb, NULL);
-
-	return io;
-}
-
-static gboolean notif_connect_cb(GIOChannel *io, GIOCondition cond,
-							gpointer user_data)
-{
-	DBG("");
-
-	if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
-		stop_bluetooth();
-		return FALSE;
-	}
-
-	cond = G_IO_ERR | G_IO_HUP | G_IO_NVAL;
-
-	g_io_add_watch(io, cond, notif_watch_cb, NULL);
-
-	cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
-
-	g_io_add_watch(hal_cmd_io, cond, cmd_watch_cb, NULL);
-
-	ipc_init(g_io_channel_unix_get_fd(hal_cmd_io),
-				g_io_channel_unix_get_fd(hal_notif_io));
-
-	info("Successfully connected to HAL");
-
-	return FALSE;
-}
-
-static gboolean cmd_connect_cb(GIOChannel *io, GIOCondition cond,
-							gpointer user_data)
-{
-	DBG("");
-
-	if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
-		stop_bluetooth();
-		return FALSE;
-	}
-
-	hal_notif_io = connect_hal(notif_connect_cb);
-	if (!hal_notif_io) {
-		error("Cannot connect to HAL, terminating");
-		stop_bluetooth();
-	}
-
-	return FALSE;
-}
-
 static void adapter_ready(int err, const bdaddr_t *addr)
 {
 	if (err < 0) {
@@ -346,11 +220,7 @@ static void adapter_ready(int err, const bdaddr_t *addr)
 
 	info("Adapter initialized");
 
-	hal_cmd_io = connect_hal(cmd_connect_cb);
-	if (!hal_cmd_io) {
-		error("Cannot connect to HAL, terminating");
-		stop_bluetooth();
-	}
+	ipc_init();
 }
 
 static gboolean signal_handler(GIOChannel *channel, GIOCondition cond,
@@ -433,23 +303,6 @@ static GOptionEntry options[] = {
 	{ NULL }
 };
 
-static void cleanup_hal_connection(void)
-{
-	if (hal_cmd_io) {
-		g_io_channel_shutdown(hal_cmd_io, TRUE, NULL);
-		g_io_channel_unref(hal_cmd_io);
-		hal_cmd_io = NULL;
-	}
-
-	if (hal_notif_io) {
-		g_io_channel_shutdown(hal_notif_io, TRUE, NULL);
-		g_io_channel_unref(hal_notif_io);
-		hal_notif_io = NULL;
-	}
-
-	ipc_cleanup();
-}
-
 static void cleanup_services(void)
 {
 	int i;
@@ -592,7 +445,7 @@ int main(int argc, char *argv[])
 
 	cleanup_services();
 
-	cleanup_hal_connection();
+	ipc_cleanup();
 	stop_sdp_server();
 	bt_bluetooth_cleanup();
 	g_main_loop_unref(event_loop);
-- 
1.8.3.2


^ permalink raw reply related

* Re: [PATCH v3 0/9] android: IPC improvements - daemon part
From: Luiz Augusto von Dentz @ 2013-12-02 14:53 UTC (permalink / raw)
  To: Szymon Janc; +Cc: linux-bluetooth@vger.kernel.org
In-Reply-To: <1385986848-8023-1-git-send-email-szymon.janc@tieto.com>

Hi Szymon,

On Mon, Dec 2, 2013 at 2:20 PM, Szymon Janc <szymon.janc@tieto.com> wrote:
> v3:
>  - rebased againt latest pan changes
>
> v2:
>  - rebased against latest IPC helpers improvements
>  - more compact command handlers table format
>  - error handling path in command handlers improved according to Johan comments
>  - randmon small fixes
>  - patches not directly related to refactor removed from serie, will
>    be send after this is merged
>
> v1:
> This serie implements IPC message handling iprovments in daemon similar
> to what is already done in HAL part.
>
> Szymon Janc (9):
>   android: Add initial code for IPC message handlers
>   android/main: Use generic IPC message handling for core service
>   android/main: Use common exit path in core service functions
>   android/bluetooth: Use generic IPC msg handling for commands
>   android/bluetooth: Make property handling function return HAL status
>   android/hidhost: Use generic IPC message handling for commands
>   android/pan: Use generic IPC message handling for commands
>   android/a2dp: Use generic IPC message handling for commands
>   android/socket: Use generic IPC message handling for commands
>
>  android/a2dp.c      |  69 ++++----
>  android/a2dp.h      |   2 -
>  android/bluetooth.c | 477 ++++++++++++++++++++++++++++++++++------------------
>  android/hidhost.c   | 309 ++++++++++++++++++++--------------
>  android/hidhost.h   |   2 -
>  android/ipc.c       |  78 +++++++++
>  android/ipc.h       |  10 ++
>  android/main.c      | 123 +++++---------
>  android/pan.c       |  87 +++++-----
>  android/pan.h       |   2 -
>  android/socket.c    | 102 ++++++-----
>  11 files changed, 754 insertions(+), 507 deletions(-)
>
> --
> 1.8.3.2

Pushed, thanks.


-- 
Luiz Augusto von Dentz

^ permalink raw reply

* [PATCH v3 9/9] android/socket: Use generic IPC message handling for commands
From: Szymon Janc @ 2013-12-02 12:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1385986848-8023-1-git-send-email-szymon.janc@tieto.com>

Handlers are registered on service register and unregistered on
unregister.
---
 android/socket.c | 102 ++++++++++++++++++++++++++-----------------------------
 1 file changed, 49 insertions(+), 53 deletions(-)

diff --git a/android/socket.c b/android/socket.c
index 4f47861..76b40c8 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -650,15 +650,15 @@ static void accept_cb(GIOChannel *io, GError *err, gpointer user_data)
 		rfsock_acc->rfcomm_watch);
 }
 
-static int handle_listen(void *buf)
+static void handle_listen(const void *buf, uint16_t len)
 {
-	struct hal_cmd_sock_listen *cmd = buf;
+	const struct hal_cmd_sock_listen *cmd = buf;
 	const struct profile_info *profile;
-	struct rfcomm_sock *rfsock;
+	struct rfcomm_sock *rfsock = NULL;
 	BtIOSecLevel sec_level;
 	GIOChannel *io;
 	GError *err = NULL;
-	int hal_fd;
+	int hal_fd = -1;
 	int chan;
 
 	DBG("");
@@ -666,11 +666,10 @@ static int handle_listen(void *buf)
 	profile = get_profile_by_uuid(cmd->uuid);
 	if (!profile) {
 		if (!cmd->channel)
-			return -1;
-		else {
-			chan = cmd->channel;
-			sec_level = BT_IO_SEC_MEDIUM;
-		}
+			goto failed;
+
+		chan = cmd->channel;
+		sec_level = BT_IO_SEC_MEDIUM;
 	} else {
 		chan = profile->channel;
 		sec_level = profile->sec_level;
@@ -680,7 +679,7 @@ static int handle_listen(void *buf)
 
 	rfsock = create_rfsock(-1, &hal_fd);
 	if (!rfsock)
-		return -1;
+		goto failed;
 
 	io = bt_io_listen(accept_cb, NULL, rfsock, NULL, &err,
 				BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
@@ -690,8 +689,7 @@ static int handle_listen(void *buf)
 	if (!io) {
 		error("Failed listen: %s", err->message);
 		g_error_free(err);
-		cleanup_rfsock(rfsock);
-		return -1;
+		goto failed;
 	}
 
 	rfsock->real_sock = g_io_channel_unix_get_fd(io);
@@ -703,15 +701,27 @@ static int handle_listen(void *buf)
 
 	if (write(rfsock->fd, &chan, sizeof(chan)) != sizeof(chan)) {
 		error("Error sending RFCOMM channel");
-		cleanup_rfsock(rfsock);
-		return -1;
+		goto failed;
 	}
 
 	rfsock->service_handle = sdp_service_register(profile, cmd->name);
 
 	servers = g_list_append(servers, rfsock);
 
-	return hal_fd;
+	ipc_send_rsp_full(HAL_SERVICE_ID_SOCK, HAL_OP_SOCK_LISTEN, 0, NULL,
+									hal_fd);
+	close(hal_fd);
+	return;
+
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_SOCK, HAL_OP_SOCK_LISTEN,
+							HAL_STATUS_FAILED);
+
+	if (rfsock)
+		cleanup_rfsock(rfsock);
+
+	if (hal_fd >= 0)
+		close(hal_fd);
 }
 
 static bool sock_send_connect(struct rfcomm_sock *rfsock, bdaddr_t *bdaddr)
@@ -865,9 +875,9 @@ fail:
 	cleanup_rfsock(rfsock);
 }
 
-static int handle_connect(void *buf)
+static void handle_connect(const void *buf, uint16_t len)
 {
-	struct hal_cmd_sock_connect *cmd = buf;
+	const struct hal_cmd_sock_connect *cmd = buf;
 	struct rfcomm_sock *rfsock;
 	uuid_t uuid;
 	int hal_fd = -1;
@@ -876,7 +886,7 @@ static int handle_connect(void *buf)
 
 	rfsock = create_rfsock(-1, &hal_fd);
 	if (!rfsock)
-		return -1;
+		goto failed;
 
 	android2bdaddr(cmd->bdaddr, &rfsock->dst);
 
@@ -890,55 +900,41 @@ static int handle_connect(void *buf)
 					sdp_search_cb, rfsock, NULL) < 0) {
 		error("Failed to search SDP records");
 		cleanup_rfsock(rfsock);
-		return -1;
+		goto failed;
 	}
 
-	return hal_fd;
-}
-
-void bt_sock_handle_cmd(int sk, uint8_t opcode, void *buf, uint16_t len)
-{
-	int fd;
-
-	switch (opcode) {
-	case HAL_OP_SOCK_LISTEN:
-		fd = handle_listen(buf);
-		if (fd < 0)
-			break;
-
-		ipc_send_rsp_full(HAL_SERVICE_ID_SOCK, opcode, 0, NULL, fd);
-
-		if (close(fd) < 0)
-			error("close() fd %d failed: %s", fd, strerror(errno));
-
-		return;
-	case HAL_OP_SOCK_CONNECT:
-		fd = handle_connect(buf);
-		if (fd < 0)
-			break;
-
-		ipc_send_rsp_full(HAL_SERVICE_ID_SOCK, opcode, 0, NULL, fd);
+	ipc_send_rsp_full(HAL_SERVICE_ID_SOCK, HAL_OP_SOCK_CONNECT, 0, NULL,
+									hal_fd);
+	close(hal_fd);
+	return;
 
-		if (close(fd) < 0)
-			error("close() fd %d failed: %s", fd, strerror(errno));
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_SOCK, HAL_OP_SOCK_CONNECT,
+							HAL_STATUS_FAILED);
 
-		return;
-	default:
-		DBG("Unhandled command, opcode 0x%x", opcode);
-		break;
-	}
-
-	ipc_send_rsp(HAL_SERVICE_ID_SOCK, opcode, HAL_STATUS_FAILED);
+	if (hal_fd >= 0)
+		close(hal_fd);
 }
 
+static const struct ipc_handler cmd_handlers[] = {
+	/* HAL_OP_SOCK_LISTEN */
+	{ handle_listen, false, sizeof(struct hal_cmd_sock_listen) },
+	/* HAL_OP_SOCK_CONNECT */
+	{ handle_connect, false, sizeof(struct hal_cmd_sock_connect) },
+};
+
 void bt_socket_register(const bdaddr_t *addr)
 {
 	DBG("");
 
 	bacpy(&adapter_addr, addr);
+	ipc_register(HAL_SERVICE_ID_SOCK, cmd_handlers,
+				sizeof(cmd_handlers)/sizeof(cmd_handlers[0]));
 }
 
 void bt_socket_unregister(void)
 {
 	DBG("");
+
+	ipc_unregister(HAL_SERVICE_ID_SOCK);
 }
-- 
1.8.3.2


^ permalink raw reply related

* [PATCH v3 8/9] android/a2dp: Use generic IPC message handling for commands
From: Szymon Janc @ 2013-12-02 12:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1385986848-8023-1-git-send-email-szymon.janc@tieto.com>

Handlers are registered on service register and unregistered on
unregister.
---
 android/a2dp.c | 69 +++++++++++++++++++++++++++++-----------------------------
 android/a2dp.h |  2 --
 2 files changed, 35 insertions(+), 36 deletions(-)

diff --git a/android/a2dp.c b/android/a2dp.c
index 99aa14d..98c138e 100644
--- a/android/a2dp.c
+++ b/android/a2dp.c
@@ -164,9 +164,11 @@ static void signaling_connect_cb(GIOChannel *chan, GError *err,
 	bt_a2dp_notify_state(dev, HAL_A2DP_STATE_CONNECTED);
 }
 
-static uint8_t bt_a2dp_connect(struct hal_cmd_a2dp_connect *cmd, uint16_t len)
+static void bt_a2dp_connect(const void *buf, uint16_t len)
 {
+	const struct hal_cmd_a2dp_connect *cmd = buf;
 	struct a2dp_device *dev;
+	uint8_t status;
 	char addr[18];
 	bdaddr_t dst;
 	GSList *l;
@@ -174,14 +176,13 @@ static uint8_t bt_a2dp_connect(struct hal_cmd_a2dp_connect *cmd, uint16_t len)
 
 	DBG("");
 
-	if (len < sizeof(*cmd))
-		return HAL_STATUS_INVALID;
-
 	android2bdaddr(&cmd->bdaddr, &dst);
 
 	l = g_slist_find_custom(devices, &dst, device_cmp);
-	if (l)
-		return HAL_STATUS_FAILED;
+	if (l) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
 
 	dev = a2dp_device_new(&dst);
 	dev->io = bt_io_connect(signaling_connect_cb, dev, NULL, &err,
@@ -194,7 +195,8 @@ static uint8_t bt_a2dp_connect(struct hal_cmd_a2dp_connect *cmd, uint16_t len)
 		error("%s", err->message);
 		g_error_free(err);
 		a2dp_device_free(dev);
-		return HAL_STATUS_FAILED;
+		status = HAL_STATUS_FAILED;
+		goto failed;
 	}
 
 	ba2str(&dev->dst, addr);
@@ -202,26 +204,29 @@ static uint8_t bt_a2dp_connect(struct hal_cmd_a2dp_connect *cmd, uint16_t len)
 
 	bt_a2dp_notify_state(dev, HAL_A2DP_STATE_CONNECTING);
 
-	return HAL_STATUS_SUCCESS;
+	status = HAL_STATUS_SUCCESS;
+
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_A2DP, HAL_OP_A2DP_CONNECT, status);
 }
 
-static uint8_t bt_a2dp_disconnect(struct hal_cmd_a2dp_connect *cmd,
-								uint16_t len)
+static void bt_a2dp_disconnect(const void *buf, uint16_t len)
 {
+	const struct hal_cmd_a2dp_connect *cmd = buf;
+	uint8_t status;
 	struct a2dp_device *dev;
 	GSList *l;
 	bdaddr_t dst;
 
 	DBG("");
 
-	if (len < sizeof(*cmd))
-		return HAL_STATUS_INVALID;
-
 	android2bdaddr(&cmd->bdaddr, &dst);
 
 	l = g_slist_find_custom(devices, &dst, device_cmp);
-	if (!l)
-		return HAL_STATUS_FAILED;
+	if (!l) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
 
 	dev = l->data;
 
@@ -231,28 +236,19 @@ static uint8_t bt_a2dp_disconnect(struct hal_cmd_a2dp_connect *cmd,
 
 	bt_a2dp_notify_state(dev, HAL_A2DP_STATE_DISCONNECTING);
 
-	return HAL_STATUS_SUCCESS;
-}
-
-void bt_a2dp_handle_cmd(int sk, uint8_t opcode, void *buf, uint16_t len)
-{
-	uint8_t status = HAL_STATUS_FAILED;
-
-	switch (opcode) {
-	case HAL_OP_A2DP_CONNECT:
-		status = bt_a2dp_connect(buf, len);
-		break;
-	case HAL_OP_A2DP_DISCONNECT:
-		status = bt_a2dp_disconnect(buf, len);
-		break;
-	default:
-		DBG("Unhandled command, opcode 0x%x", opcode);
-		break;
-	}
+	status = HAL_STATUS_SUCCESS;
 
-	ipc_send_rsp(HAL_SERVICE_ID_A2DP, opcode, status);
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_A2DP, HAL_OP_A2DP_DISCONNECT, status);
 }
 
+static const struct ipc_handler cmd_handlers[] = {
+	/* HAL_OP_A2DP_CONNECT */
+	{ bt_a2dp_connect, false, sizeof(struct hal_cmd_a2dp_connect) },
+	/* HAL_OP_A2DP_DISCONNECT */
+	{ bt_a2dp_disconnect, false, sizeof(struct hal_cmd_a2dp_disconnect) },
+};
+
 static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data)
 {
 	struct a2dp_device *dev;
@@ -380,6 +376,9 @@ bool bt_a2dp_register(const bdaddr_t *addr)
 	}
 	record_id = rec->handle;
 
+	ipc_register(HAL_SERVICE_ID_A2DP, cmd_handlers,
+				sizeof(cmd_handlers)/sizeof(cmd_handlers[0]));
+
 	return true;
 }
 
@@ -397,6 +396,8 @@ void bt_a2dp_unregister(void)
 	g_slist_foreach(devices, a2dp_device_disconnected, NULL);
 	devices = NULL;
 
+
+	ipc_unregister(HAL_SERVICE_ID_A2DP);
 	bt_adapter_remove_record(record_id);
 	record_id = 0;
 
diff --git a/android/a2dp.h b/android/a2dp.h
index 2a1eb3c..7e9b2f6 100644
--- a/android/a2dp.h
+++ b/android/a2dp.h
@@ -21,7 +21,5 @@
  *
  */
 
-void bt_a2dp_handle_cmd(int sk, uint8_t opcode, void *buf, uint16_t len);
-
 bool bt_a2dp_register(const bdaddr_t *addr);
 void bt_a2dp_unregister(void);
-- 
1.8.3.2


^ permalink raw reply related

* [PATCH v3 7/9] android/pan: Use generic IPC message handling for commands
From: Szymon Janc @ 2013-12-02 12:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1385986848-8023-1-git-send-email-szymon.janc@tieto.com>

Handlers are registered on service register and unregistered on
unregister.

This also fix sending two IPC responses for get pan role command.
---
 android/pan.c | 87 +++++++++++++++++++++++++++++------------------------------
 android/pan.h |  2 --
 2 files changed, 42 insertions(+), 47 deletions(-)

diff --git a/android/pan.c b/android/pan.c
index 9e388c3..3270aa4 100644
--- a/android/pan.c
+++ b/android/pan.c
@@ -188,9 +188,11 @@ static void connect_cb(GIOChannel *chan, GError *err, gpointer data)
 	}
 }
 
-static uint8_t bt_pan_connect(struct hal_cmd_pan_connect *cmd, uint16_t len)
+static void bt_pan_connect(const void *buf, uint16_t len)
 {
+	const struct hal_cmd_pan_connect *cmd = buf;
 	struct pan_device *dev;
+	uint8_t status;
 	bdaddr_t dst;
 	char addr[18];
 	GSList *l;
@@ -198,14 +200,13 @@ static uint8_t bt_pan_connect(struct hal_cmd_pan_connect *cmd, uint16_t len)
 
 	DBG("");
 
-	if (len < sizeof(*cmd))
-		return HAL_STATUS_INVALID;
-
 	android2bdaddr(&cmd->bdaddr, &dst);
 
 	l = g_slist_find_custom(devices, &dst, device_cmp);
-	if (l)
-		return HAL_STATUS_FAILED;
+	if (l) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
 
 	dev = g_new0(struct pan_device, 1);
 	bacpy(&dev->dst, &dst);
@@ -227,32 +228,36 @@ static uint8_t bt_pan_connect(struct hal_cmd_pan_connect *cmd, uint16_t len)
 		error("%s", gerr->message);
 		g_error_free(gerr);
 		g_free(dev);
-		return HAL_STATUS_FAILED;
+		status = HAL_STATUS_FAILED;
+		goto failed;
 	}
 
 	devices = g_slist_append(devices, dev);
 	bt_pan_notify_conn_state(dev, HAL_PAN_STATE_CONNECTING);
 
-	return HAL_STATUS_SUCCESS;
+	status =  HAL_STATUS_SUCCESS;
+
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_PAN, HAL_OP_PAN_CONNECT, status);
 }
 
-static uint8_t bt_pan_disconnect(struct hal_cmd_pan_disconnect *cmd,
-								uint16_t len)
+static void bt_pan_disconnect(const void *buf, uint16_t len)
 {
+	const struct hal_cmd_pan_disconnect *cmd = buf;
 	struct pan_device *dev;
+	uint8_t status;
 	GSList *l;
 	bdaddr_t dst;
 
 	DBG("");
 
-	if (len < sizeof(*cmd))
-		return HAL_STATUS_INVALID;
-
 	android2bdaddr(&cmd->bdaddr, &dst);
 
 	l = g_slist_find_custom(devices, &dst, device_cmp);
-	if (!l)
-		return HAL_STATUS_FAILED;
+	if (!l) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
 
 	dev = l->data;
 
@@ -267,17 +272,20 @@ static uint8_t bt_pan_disconnect(struct hal_cmd_pan_disconnect *cmd,
 	bt_pan_notify_conn_state(dev, HAL_PAN_STATE_DISCONNECTED);
 	pan_device_free(dev);
 
-	return HAL_STATUS_SUCCESS;
+	status = HAL_STATUS_SUCCESS;
+
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_PAN, HAL_OP_PAN_DISCONNECT, status);
 }
 
-static uint8_t bt_pan_enable(struct hal_cmd_pan_enable *cmd, uint16_t len)
+static void bt_pan_enable(const void *buf, uint16_t len)
 {
 	DBG("Not Implemented");
 
-	return HAL_STATUS_FAILED;
+	ipc_send_rsp(HAL_SERVICE_ID_PAN, HAL_OP_PAN_ENABLE, HAL_STATUS_FAILED);
 }
 
-static uint8_t bt_pan_get_role(void *cmd, uint16_t len)
+static void bt_pan_get_role(const void *buf, uint16_t len)
 {
 	struct hal_rsp_pan_get_role rsp;
 
@@ -286,34 +294,18 @@ static uint8_t bt_pan_get_role(void *cmd, uint16_t len)
 	rsp.local_role = local_role;
 	ipc_send_rsp_full(HAL_SERVICE_ID_PAN, HAL_OP_PAN_GET_ROLE, sizeof(rsp),
 								&rsp, -1);
-
-	return HAL_STATUS_SUCCESS;
 }
 
-void bt_pan_handle_cmd(int sk, uint8_t opcode, void *buf, uint16_t len)
-{
-	uint8_t status = HAL_STATUS_FAILED;
-
-	switch (opcode) {
-	case HAL_OP_PAN_ENABLE:
-		status = bt_pan_enable(buf, len);
-		break;
-	case HAL_OP_PAN_GET_ROLE:
-		status = bt_pan_get_role(buf, len);
-		break;
-	case HAL_OP_PAN_CONNECT:
-		status = bt_pan_connect(buf, len);
-		break;
-	case HAL_OP_PAN_DISCONNECT:
-		status = bt_pan_disconnect(buf, len);
-		break;
-	default:
-		DBG("Unhandled command, opcode 0x%x", opcode);
-		break;
-	}
-
-	ipc_send_rsp(HAL_SERVICE_ID_PAN, opcode, status);
-}
+static const struct ipc_handler cmd_handlers[] = {
+	/* HAL_OP_PAN_ENABLE */
+	{ bt_pan_enable, false, sizeof(struct hal_cmd_pan_enable) },
+	/* HAL_OP_PAN_GET_ROLE */
+	{ bt_pan_get_role, false, 0 },
+	/* HAL_OP_PAN_CONNECT */
+	{ bt_pan_connect, false, sizeof(struct hal_cmd_pan_connect) },
+	/* HAL_OP_PAN_DISCONNECT */
+	{ bt_pan_disconnect, false, sizeof(struct hal_cmd_pan_disconnect) },
+};
 
 bool bt_pan_register(const bdaddr_t *addr)
 {
@@ -329,6 +321,9 @@ bool bt_pan_register(const bdaddr_t *addr)
 		return false;
 	}
 
+	ipc_register(HAL_SERVICE_ID_PAN, cmd_handlers,
+				sizeof(cmd_handlers)/sizeof(cmd_handlers[0]));
+
 	return true;
 }
 
@@ -337,4 +332,6 @@ void bt_pan_unregister(void)
 	DBG("");
 
 	bnep_cleanup();
+
+	ipc_unregister(HAL_SERVICE_ID_PAN);
 }
diff --git a/android/pan.h b/android/pan.h
index dd18f68..3178d88 100644
--- a/android/pan.h
+++ b/android/pan.h
@@ -21,7 +21,5 @@
  *
  */
 
-void bt_pan_handle_cmd(int sk, uint8_t opcode, void *buf, uint16_t len);
-
 bool bt_pan_register(const bdaddr_t *addr);
 void bt_pan_unregister(void);
-- 
1.8.3.2


^ permalink raw reply related

* [PATCH v3 6/9] android/hidhost: Use generic IPC message handling for commands
From: Szymon Janc @ 2013-12-02 12:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1385986848-8023-1-git-send-email-szymon.janc@tieto.com>

Handlers are registered on service register and unregistered on
unregister.
---
 android/hidhost.c | 309 ++++++++++++++++++++++++++++++++----------------------
 android/hidhost.h |   2 -
 2 files changed, 184 insertions(+), 127 deletions(-)

diff --git a/android/hidhost.c b/android/hidhost.c
index 44310ed..38194d0 100644
--- a/android/hidhost.c
+++ b/android/hidhost.c
@@ -720,10 +720,11 @@ fail:
 	hid_device_free(dev);
 }
 
-static uint8_t bt_hid_connect(struct hal_cmd_hidhost_connect *cmd,
-								uint16_t len)
+static void bt_hid_connect(const void *buf, uint16_t len)
 {
+	const struct hal_cmd_hidhost_connect *cmd = buf;
 	struct hid_device *dev;
+	uint8_t status;
 	char addr[18];
 	bdaddr_t dst;
 	GSList *l;
@@ -731,14 +732,13 @@ static uint8_t bt_hid_connect(struct hal_cmd_hidhost_connect *cmd,
 
 	DBG("");
 
-	if (len < sizeof(*cmd))
-		return HAL_STATUS_INVALID;
-
 	android2bdaddr(&cmd->bdaddr, &dst);
 
 	l = g_slist_find_custom(devices, &dst, device_cmp);
-	if (l)
-		return HAL_STATUS_FAILED;
+	if (l) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
 
 	dev = g_new0(struct hid_device, 1);
 	bacpy(&dev->dst, &dst);
@@ -752,32 +752,36 @@ static uint8_t bt_hid_connect(struct hal_cmd_hidhost_connect *cmd,
 					hid_sdp_search_cb, dev, NULL) < 0) {
 		error("Failed to search sdp details");
 		hid_device_free(dev);
-		return HAL_STATUS_FAILED;
+		status = HAL_STATUS_FAILED;
+		goto failed;
 	}
 
 	devices = g_slist_append(devices, dev);
 	bt_hid_notify_state(dev, HAL_HIDHOST_STATE_CONNECTING);
 
-	return HAL_STATUS_SUCCESS;
+	status = HAL_STATUS_SUCCESS;
+
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_HIDHOST, HAL_OP_HIDHOST_CONNECT, status);
 }
 
-static uint8_t bt_hid_disconnect(struct hal_cmd_hidhost_disconnect *cmd,
-								uint16_t len)
+static void bt_hid_disconnect(const void *buf, uint16_t len)
 {
+	const struct hal_cmd_hidhost_disconnect *cmd = buf;
 	struct hid_device *dev;
+	uint8_t status;
 	GSList *l;
 	bdaddr_t dst;
 
 	DBG("");
 
-	if (len < sizeof(*cmd))
-		return HAL_STATUS_INVALID;
-
 	android2bdaddr(&cmd->bdaddr, &dst);
 
 	l = g_slist_find_custom(devices, &dst, device_cmp);
-	if (!l)
-		return HAL_STATUS_FAILED;
+	if (!l) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
 
 	dev = l->data;
 
@@ -790,33 +794,38 @@ static uint8_t bt_hid_disconnect(struct hal_cmd_hidhost_disconnect *cmd,
 
 	bt_hid_notify_state(dev, HAL_HIDHOST_STATE_DISCONNECTING);
 
-	return HAL_STATUS_SUCCESS;
+	status = HAL_STATUS_SUCCESS;
+
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_HIDHOST, HAL_OP_HIDHOST_DISCONNECT, status);
 }
 
-static uint8_t bt_hid_virtual_unplug(struct hal_cmd_hidhost_virtual_unplug *cmd,
-								uint16_t len)
+static void bt_hid_virtual_unplug(const void *buf, uint16_t len)
 {
+	const struct hal_cmd_hidhost_virtual_unplug *cmd = buf;
 	struct hid_device *dev;
 	GSList *l;
+	uint8_t status;
 	bdaddr_t dst;
 	uint8_t hdr;
 	int fd;
 
 	DBG("");
 
-	if (len < sizeof(*cmd))
-		return HAL_STATUS_INVALID;
-
 	android2bdaddr(&cmd->bdaddr, &dst);
 
 	l = g_slist_find_custom(devices, &dst, device_cmp);
-	if (!l)
-		return HAL_STATUS_FAILED;
+	if (!l) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
 
 	dev = l->data;
 
-	if (!(dev->ctrl_io))
-		return HAL_STATUS_FAILED;
+	if (!(dev->ctrl_io)) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
 
 	hdr = HID_MSG_CONTROL | HID_VIRTUAL_CABLE_UNPLUG;
 
@@ -825,7 +834,8 @@ static uint8_t bt_hid_virtual_unplug(struct hal_cmd_hidhost_virtual_unplug *cmd,
 	if (write(fd, &hdr, sizeof(hdr)) < 0) {
 		error("error writing virtual unplug command: %s (%d)",
 						strerror(errno), errno);
-		return HAL_STATUS_FAILED;
+		status = HAL_STATUS_FAILED;
+		goto failed;
 	}
 
 	/* Wait either channels to HUP */
@@ -837,10 +847,14 @@ static uint8_t bt_hid_virtual_unplug(struct hal_cmd_hidhost_virtual_unplug *cmd,
 
 	bt_hid_notify_state(dev, HAL_HIDHOST_STATE_DISCONNECTING);
 
-	return HAL_STATUS_SUCCESS;
+	status = HAL_STATUS_SUCCESS;
+
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_HIDHOST, HAL_OP_HIDHOST_VIRTUAL_UNPLUG,
+									status);
 }
 
-static uint8_t bt_hid_info(struct hal_cmd_hidhost_set_info *cmd, uint16_t len)
+static void bt_hid_info(const void *buf, uint16_t len)
 {
 	/* Data from hal_cmd_hidhost_set_info is usefull only when we create
 	 * UHID device. Once device is created all the transactions will be
@@ -848,33 +862,36 @@ static uint8_t bt_hid_info(struct hal_cmd_hidhost_set_info *cmd, uint16_t len)
 	 * once device is created with HID internals. */
 	DBG("Not supported");
 
-	return HAL_STATUS_UNSUPPORTED;
+	ipc_send_rsp(HAL_SERVICE_ID_HIDHOST, HAL_OP_HIDHOST_SET_INFO,
+							HAL_STATUS_UNSUPPORTED);
 }
 
-static uint8_t bt_hid_get_protocol(struct hal_cmd_hidhost_get_protocol *cmd,
-								uint16_t len)
+static void bt_hid_get_protocol(const void *buf, uint16_t len)
 {
+	const struct hal_cmd_hidhost_get_protocol *cmd = buf;
 	struct hid_device *dev;
 	GSList *l;
 	bdaddr_t dst;
 	int fd;
 	uint8_t hdr;
+	uint8_t status;
 
 	DBG("");
 
-	if (len < sizeof(*cmd))
-		return HAL_STATUS_INVALID;
-
 	android2bdaddr(&cmd->bdaddr, &dst);
 
 	l = g_slist_find_custom(devices, &dst, device_cmp);
-	if (!l)
-		return HAL_STATUS_FAILED;
+	if (!l) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
 
 	dev = l->data;
 
-	if (dev->boot_dev)
-		return HAL_STATUS_UNSUPPORTED;
+	if (dev->boot_dev) {
+		status = HAL_STATUS_UNSUPPORTED;
+		goto failed;
+	}
 
 	hdr = HID_MSG_GET_PROTOCOL | cmd->mode;
 	fd = g_io_channel_unix_get_fd(dev->ctrl_io);
@@ -882,37 +899,45 @@ static uint8_t bt_hid_get_protocol(struct hal_cmd_hidhost_get_protocol *cmd,
 	if (write(fd, &hdr, sizeof(hdr)) < 0) {
 		error("error writing device_get_protocol: %s (%d)",
 						strerror(errno), errno);
-		return HAL_STATUS_FAILED;
+		status = HAL_STATUS_FAILED;
+		goto failed;
 	}
 
 	dev->last_hid_msg = HID_MSG_GET_PROTOCOL;
-	return HAL_STATUS_SUCCESS;
+
+	status = HAL_STATUS_SUCCESS;
+
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_HIDHOST, HAL_OP_HIDHOST_GET_PROTOCOL,
+									status);
 }
 
-static uint8_t bt_hid_set_protocol(struct hal_cmd_hidhost_set_protocol *cmd,
-								uint16_t len)
+static void bt_hid_set_protocol(const void *buf, uint16_t len)
 {
+	const struct hal_cmd_hidhost_set_protocol *cmd = buf;
 	struct hid_device *dev;
 	GSList *l;
 	bdaddr_t dst;
 	int fd;
 	uint8_t hdr;
+	uint8_t status;
 
 	DBG("");
 
-	if (len < sizeof(*cmd))
-		return HAL_STATUS_INVALID;
-
 	android2bdaddr(&cmd->bdaddr, &dst);
 
 	l = g_slist_find_custom(devices, &dst, device_cmp);
-	if (!l)
-		return HAL_STATUS_FAILED;
+	if (!l) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
 
 	dev = l->data;
 
-	if (dev->boot_dev)
-		return HAL_STATUS_UNSUPPORTED;
+	if (dev->boot_dev) {
+		status = HAL_STATUS_UNSUPPORTED;
+		goto failed;
+	}
 
 	hdr = HID_MSG_SET_PROTOCOL | cmd->mode;
 	fd = g_io_channel_unix_get_fd(dev->ctrl_io);
@@ -920,39 +945,47 @@ static uint8_t bt_hid_set_protocol(struct hal_cmd_hidhost_set_protocol *cmd,
 	if (write(fd, &hdr, sizeof(hdr)) < 0) {
 		error("error writing device_set_protocol: %s (%d)",
 						strerror(errno), errno);
-		return HAL_STATUS_FAILED;
+		status = HAL_STATUS_FAILED;
+		goto failed;
 	}
 
 	dev->last_hid_msg = HID_MSG_SET_PROTOCOL;
-	return HAL_STATUS_SUCCESS;
+
+	status = HAL_STATUS_SUCCESS;
+
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_HIDHOST, HAL_OP_HIDHOST_SET_PROTOCOL,
+									status);
 }
 
-static uint8_t bt_hid_get_report(struct hal_cmd_hidhost_get_report *cmd,
-								uint16_t len)
+static void bt_hid_get_report(const void *buf, uint16_t len)
 {
+	const struct hal_cmd_hidhost_get_report *cmd = buf;
 	struct hid_device *dev;
 	GSList *l;
 	bdaddr_t dst;
 	int fd;
 	uint8_t *req;
 	uint8_t req_size;
+	uint8_t status;
 
 	DBG("");
 
-	if (len < sizeof(*cmd))
-		return HAL_STATUS_INVALID;
-
 	android2bdaddr(&cmd->bdaddr, &dst);
 
 	l = g_slist_find_custom(devices, &dst, device_cmp);
-	if (!l)
-		return HAL_STATUS_FAILED;
+	if (!l) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
 
 	dev = l->data;
 	req_size = (cmd->buf_size > 0) ? 4 : 2;
 	req = g_try_malloc0(req_size);
-	if (!req)
-		return HAL_STATUS_NOMEM;
+	if (!req) {
+		status = HAL_STATUS_NOMEM;
+		goto failed;
+	}
 
 	req[0] = HID_MSG_GET_REPORT | cmd->type;
 	req[1] = cmd->id;
@@ -968,44 +1001,60 @@ static uint8_t bt_hid_get_report(struct hal_cmd_hidhost_get_report *cmd,
 		error("error writing hid_get_report: %s (%d)",
 						strerror(errno), errno);
 		g_free(req);
-		return HAL_STATUS_FAILED;
+		status = HAL_STATUS_FAILED;
+		goto failed;
 	}
 
 	dev->last_hid_msg = HID_MSG_GET_REPORT;
 	g_free(req);
-	return HAL_STATUS_SUCCESS;
+
+	status = HAL_STATUS_SUCCESS;
+
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_HIDHOST, HAL_OP_HIDHOST_GET_REPORT, status);
 }
 
-static uint8_t bt_hid_set_report(struct hal_cmd_hidhost_set_report *cmd,
-								uint16_t len)
+static void bt_hid_set_report(const void *buf, uint16_t len)
 {
+	const struct hal_cmd_hidhost_set_report *cmd = buf;
 	struct hid_device *dev;
 	GSList *l;
 	bdaddr_t dst;
 	int i, fd;
 	uint8_t *req;
 	uint8_t req_size;
+	uint8_t status;
 
 	DBG("");
 
-	if (len < sizeof(*cmd))
-		return HAL_STATUS_INVALID;
+	if (len != sizeof(*cmd) + cmd->len) {
+		error("Invalid hid set report size (%u bytes), terminating",
+									len);
+		raise(SIGTERM);
+		return;
+	}
 
 	android2bdaddr(&cmd->bdaddr, &dst);
 
 	l = g_slist_find_custom(devices, &dst, device_cmp);
-	if (!l)
-		return HAL_STATUS_FAILED;
+	if (!l) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
 
 	dev = l->data;
 
-	if (!(dev->ctrl_io))
-		return HAL_STATUS_FAILED;
+	if (!(dev->ctrl_io)) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
 
 	req_size = 1 + (cmd->len / 2);
 	req = g_try_malloc0(req_size);
-	if (!req)
-		return HAL_STATUS_NOMEM;
+	if (!req) {
+		status = HAL_STATUS_NOMEM;
+		goto failed;
+	}
 
 	req[0] = HID_MSG_SET_REPORT | cmd->type;
 	/* Report data coming to HAL is in ascii format, HAL sends
@@ -1019,44 +1068,60 @@ static uint8_t bt_hid_set_report(struct hal_cmd_hidhost_set_report *cmd,
 		error("error writing hid_set_report: %s (%d)",
 						strerror(errno), errno);
 		g_free(req);
-		return HAL_STATUS_FAILED;
+		status = HAL_STATUS_FAILED;
+		goto failed;
 	}
 
 	dev->last_hid_msg = HID_MSG_SET_REPORT;
 	g_free(req);
-	return HAL_STATUS_SUCCESS;
+
+	status = HAL_STATUS_SUCCESS;
+
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_HIDHOST, HAL_OP_HIDHOST_SET_REPORT, status);
 }
 
-static uint8_t bt_hid_send_data(struct hal_cmd_hidhost_send_data *cmd,
-								uint16_t len)
+static void bt_hid_send_data(const void *buf, uint16_t len)
 {
+	const struct hal_cmd_hidhost_send_data *cmd = buf;
 	struct hid_device *dev;
 	GSList *l;
 	bdaddr_t dst;
 	int i, fd;
 	uint8_t *req;
 	uint8_t req_size;
+	uint8_t status;
 
 	DBG("");
 
-	if (len < sizeof(*cmd))
-		return HAL_STATUS_INVALID;
+	if (len != sizeof(*cmd) + cmd->len) {
+		error("Invalid hid send data size (%u bytes), terminating",
+									len);
+		raise(SIGTERM);
+		return;
+	}
 
 	android2bdaddr(&cmd->bdaddr, &dst);
 
 	l = g_slist_find_custom(devices, &dst, device_cmp);
-	if (!l)
-		return HAL_STATUS_FAILED;
+	if (!l) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
 
 	dev = l->data;
 
-	if (!(dev->intr_io))
-		return HAL_STATUS_FAILED;
+	if (!(dev->intr_io)) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
 
 	req_size = 1 + (cmd->len / 2);
 	req = g_try_malloc0(req_size);
-	if (!req)
-		return HAL_STATUS_NOMEM;
+	if (!req) {
+		status = HAL_STATUS_NOMEM;
+		goto failed;
+	}
 
 	req[0] = HID_MSG_DATA | HID_DATA_TYPE_OUTPUT;
 	/* Report data coming to HAL is in ascii format, HAL sends
@@ -1070,53 +1135,42 @@ static uint8_t bt_hid_send_data(struct hal_cmd_hidhost_send_data *cmd,
 		error("error writing data to HID device: %s (%d)",
 						strerror(errno), errno);
 		g_free(req);
-		return HAL_STATUS_FAILED;
+		status = HAL_STATUS_FAILED;
+		goto failed;
 	}
 
 	g_free(req);
-	return HAL_STATUS_SUCCESS;
-}
 
-void bt_hid_handle_cmd(int sk, uint8_t opcode, void *buf, uint16_t len)
-{
-	uint8_t status = HAL_STATUS_FAILED;
+	status = HAL_STATUS_SUCCESS;
 
-	switch (opcode) {
-	case HAL_OP_HIDHOST_CONNECT:
-		status = bt_hid_connect(buf, len);
-		break;
-	case HAL_OP_HIDHOST_DISCONNECT:
-		status = bt_hid_disconnect(buf, len);
-		break;
-	case HAL_OP_HIDHOST_VIRTUAL_UNPLUG:
-		status = bt_hid_virtual_unplug(buf, len);
-		break;
-	case HAL_OP_HIDHOST_SET_INFO:
-		status = bt_hid_info(buf, len);
-		break;
-	case HAL_OP_HIDHOST_GET_PROTOCOL:
-		status = bt_hid_get_protocol(buf, len);
-		break;
-	case HAL_OP_HIDHOST_SET_PROTOCOL:
-		status = bt_hid_set_protocol(buf, len);
-		break;
-	case HAL_OP_HIDHOST_GET_REPORT:
-		status = bt_hid_get_report(buf, len);
-		break;
-	case HAL_OP_HIDHOST_SET_REPORT:
-		status = bt_hid_set_report(buf, len);
-		break;
-	case HAL_OP_HIDHOST_SEND_DATA:
-		status = bt_hid_send_data(buf, len);
-		break;
-	default:
-		DBG("Unhandled command, opcode 0x%x", opcode);
-		break;
-	}
-
-	ipc_send_rsp(HAL_SERVICE_ID_HIDHOST, opcode, status);
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_HIDHOST, HAL_OP_HIDHOST_SEND_DATA, status);
 }
 
+static const struct ipc_handler cmd_handlers[] = {
+	/* HAL_OP_HIDHOST_CONNECT */
+	{ bt_hid_connect, false, sizeof(struct hal_cmd_hidhost_connect) },
+	/* HAL_OP_HIDHOST_DISCONNECT */
+	{ bt_hid_disconnect, false, sizeof(struct hal_cmd_hidhost_disconnect) },
+	/* HAL_OP_HIDHOST_VIRTUAL_UNPLUG */
+	{ bt_hid_virtual_unplug, false,
+				sizeof(struct hal_cmd_hidhost_virtual_unplug) },
+	/* HAL_OP_HIDHOST_SET_INFO */
+	{ bt_hid_info, true, sizeof(struct hal_cmd_hidhost_set_info) },
+	/* HAL_OP_HIDHOST_GET_PROTOCOL */
+	{ bt_hid_get_protocol, false,
+				sizeof(struct hal_cmd_hidhost_get_protocol) },
+	/* HAL_OP_HIDHOST_SET_PROTOCOL */
+	{ bt_hid_set_protocol, false,
+				sizeof(struct hal_cmd_hidhost_get_protocol) },
+	/* HAL_OP_HIDHOST_GET_REPORT */
+	{ bt_hid_get_report, false, sizeof(struct hal_cmd_hidhost_get_report) },
+	/* HAL_OP_HIDHOST_SET_REPORT */
+	{ bt_hid_set_report, true, sizeof(struct hal_cmd_hidhost_set_report) },
+	/* HAL_OP_HIDHOST_SEND_DATA */
+	{ bt_hid_send_data, true, sizeof(struct hal_cmd_hidhost_send_data)  },
+};
+
 static void connect_cb(GIOChannel *chan, GError *err, gpointer user_data)
 {
 	struct hid_device *dev;
@@ -1224,6 +1278,9 @@ bool bt_hid_register(const bdaddr_t *addr)
 		return false;
 	}
 
+	ipc_register(HAL_SERVICE_ID_HIDHOST, cmd_handlers,
+				sizeof(cmd_handlers)/sizeof(cmd_handlers[0]));
+
 	return true;
 }
 
@@ -1253,4 +1310,6 @@ void bt_hid_unregister(void)
 		g_io_channel_unref(intr_io);
 		intr_io = NULL;
 	}
+
+	ipc_unregister(HAL_SERVICE_ID_HIDHOST);
 }
diff --git a/android/hidhost.h b/android/hidhost.h
index b5545fb..ea14446 100644
--- a/android/hidhost.h
+++ b/android/hidhost.h
@@ -21,7 +21,5 @@
  *
  */
 
-void bt_hid_handle_cmd(int sk, uint8_t opcode, void *buf, uint16_t len);
-
 bool bt_hid_register(const bdaddr_t *addr);
 void bt_hid_unregister(void);
-- 
1.8.3.2


^ permalink raw reply related

* [PATCH v3 5/9] android/bluetooth: Make property handling function return HAL status
From: Szymon Janc @ 2013-12-02 12:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1385986848-8023-1-git-send-email-szymon.janc@tieto.com>

This makes funtions follow have similar style and makes properties
dispatch function much simpler.
---
 android/bluetooth.c | 85 +++++++++++++++++++----------------------------------
 1 file changed, 30 insertions(+), 55 deletions(-)

diff --git a/android/bluetooth.c b/android/bluetooth.c
index eb8dbc5..a39e7bf 100644
--- a/android/bluetooth.c
+++ b/android/bluetooth.c
@@ -1133,7 +1133,7 @@ static void uuid16_to_uint128(uint16_t uuid, uint128_t *u128)
 	ntoh128(&uuid128.value.uuid128, u128);
 }
 
-static bool get_uuids(void)
+static uint8_t get_uuids(void)
 {
 	struct hal_ev_adapter_props_changed *ev;
 	GSList *list = adapter.uuids;
@@ -1169,7 +1169,7 @@ static bool get_uuids(void)
 	ipc_send_notif(HAL_SERVICE_ID_BLUETOOTH, HAL_EV_ADAPTER_PROPS_CHANGED,
 							sizeof(buf), ev);
 
-	return true;
+	return HAL_STATUS_SUCCESS;
 }
 
 static void remove_uuid_complete(uint8_t status, uint16_t length,
@@ -1691,7 +1691,7 @@ static bool set_discoverable(uint8_t mode, uint16_t timeout)
 	return false;
 }
 
-static void get_address(void)
+static uint8_t get_address(void)
 {
 	uint8_t buf[BASELEN_PROP_CHANGED + sizeof(bdaddr_t)];
 	struct hal_ev_adapter_props_changed *ev = (void *) buf;
@@ -1705,65 +1705,67 @@ static void get_address(void)
 
 	ipc_send_notif(HAL_SERVICE_ID_BLUETOOTH, HAL_EV_ADAPTER_PROPS_CHANGED,
 							sizeof(buf), buf);
+
+	return HAL_STATUS_SUCCESS;
 }
 
-static bool get_name(void)
+static uint8_t get_name(void)
 {
 	if (!adapter.name)
-		return false;
+		return HAL_STATUS_FAILED;
 
 	adapter_name_changed((uint8_t *) adapter.name);
 
-	return true;
+	return HAL_STATUS_SUCCESS;
 }
 
 
-static bool get_class(void)
+static uint8_t get_class(void)
 {
 	DBG("");
 
 	adapter_class_changed();
 
-	return true;
+	return HAL_STATUS_SUCCESS;
 }
 
-static bool get_type(void)
+static uint8_t get_type(void)
 {
 	DBG("Not implemented");
 
 	/* TODO: Add implementation */
 
-	return false;
+	return HAL_STATUS_FAILED;
 }
 
-static bool get_service(void)
+static uint8_t get_service(void)
 {
 	DBG("Not implemented");
 
 	/* TODO: Add implementation */
 
-	return false;
+	return HAL_STATUS_FAILED;
 }
 
-static bool get_scan_mode(void)
+static uint8_t get_scan_mode(void)
 {
 	DBG("");
 
 	scan_mode_changed();
 
-	return true;
+	return HAL_STATUS_SUCCESS;
 }
 
-static bool get_devices(void)
+static uint8_t get_devices(void)
 {
 	DBG("Not implemented");
 
 	/* TODO: Add implementation */
 
-	return false;
+	return HAL_STATUS_FAILED;
 }
 
-static bool get_discoverable_timeout(void)
+static uint8_t get_discoverable_timeout(void)
 {
 	struct hal_ev_adapter_props_changed *ev;
 	uint8_t buf[BASELEN_PROP_CHANGED + sizeof(uint32_t)];
@@ -1782,7 +1784,7 @@ static bool get_discoverable_timeout(void)
 	ipc_send_notif(HAL_SERVICE_ID_BLUETOOTH, HAL_EV_ADAPTER_PROPS_CHANGED,
 							sizeof(buf), ev);
 
-	return true;
+	return HAL_STATUS_SUCCESS;
 }
 
 static void handle_get_adapter_prop_cmd(const void *buf, uint16_t len)
@@ -1792,64 +1794,37 @@ static void handle_get_adapter_prop_cmd(const void *buf, uint16_t len)
 
 	switch (cmd->type) {
 	case HAL_PROP_ADAPTER_ADDR:
-		get_address();
+		status = get_address();
 		break;
 	case HAL_PROP_ADAPTER_NAME:
-		if (!get_name()) {
-			status = HAL_STATUS_FAILED;
-			goto failed;
-		}
+		status = get_name();
 		break;
 	case HAL_PROP_ADAPTER_UUIDS:
-		if (!get_uuids()) {
-			status = HAL_STATUS_FAILED;
-			goto failed;
-		}
+		status = get_uuids();
 		break;
 	case HAL_PROP_ADAPTER_CLASS:
-		if (!get_class()) {
-			status = HAL_STATUS_FAILED;
-			goto failed;
-		}
+		status = get_class();
 		break;
 	case HAL_PROP_ADAPTER_TYPE:
-		if (!get_type()) {
-			status = HAL_STATUS_FAILED;
-			goto failed;
-		}
+		status = get_type();
 		break;
 	case HAL_PROP_ADAPTER_SERVICE_REC:
-		if (!get_service()) {
-			status = HAL_STATUS_FAILED;
-			goto failed;
-		}
+		status = get_service();
 		break;
 	case HAL_PROP_ADAPTER_SCAN_MODE:
-		if (!get_scan_mode()) {
-			status = HAL_STATUS_FAILED;
-			goto failed;
-		}
+		status = get_scan_mode();
 		break;
 	case HAL_PROP_ADAPTER_BONDED_DEVICES:
-		if (!get_devices()) {
-			status = HAL_STATUS_FAILED;
-			goto failed;
-		}
+		status = get_devices();
 		break;
 	case HAL_PROP_ADAPTER_DISC_TIMEOUT:
-		if (!get_discoverable_timeout()) {
-			status = HAL_STATUS_FAILED;
-			goto failed;
-		}
+		status = get_discoverable_timeout();
 		break;
 	default:
 		status = HAL_STATUS_FAILED;
-		goto failed;
+		break;
 	}
 
-	status = HAL_STATUS_SUCCESS;
-
-failed:
 	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_GET_ADAPTER_PROP, status);
 }
 
-- 
1.8.3.2


^ permalink raw reply related

* [PATCH v3 4/9] android/bluetooth: Use generic IPC msg handling for commands
From: Szymon Janc @ 2013-12-02 12:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1385986848-8023-1-git-send-email-szymon.janc@tieto.com>

Handlers are registered on service register and unregistered on
unregister.
---
 android/bluetooth.c | 464 ++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 321 insertions(+), 143 deletions(-)

diff --git a/android/bluetooth.c b/android/bluetooth.c
index 8a1d444..eb8dbc5 100644
--- a/android/bluetooth.c
+++ b/android/bluetooth.c
@@ -1361,7 +1361,7 @@ static void set_adapter_name_complete(uint8_t status, uint16_t length,
 	adapter_set_name(rp->name);
 }
 
-static uint8_t set_adapter_name(uint8_t *name, uint16_t len)
+static uint8_t set_adapter_name(const uint8_t *name, uint16_t len)
 {
 	struct mgmt_cp_set_local_name cp;
 
@@ -1378,8 +1378,17 @@ static uint8_t set_adapter_name(uint8_t *name, uint16_t len)
 	return HAL_STATUS_FAILED;
 }
 
-static uint8_t set_discoverable_timeout(uint8_t *timeout)
+static uint8_t set_discoverable_timeout(const void *buf, uint16_t len)
 {
+	const uint32_t *timeout = buf;
+
+	if (len != sizeof(*timeout)) {
+		error("Invalid set disc timeout size (%u bytes), terminating",
+									len);
+		raise(SIGTERM);
+		return HAL_STATUS_FAILED;
+	}
+
 	/* Android handles discoverable timeout in Settings app.
 	 * There is no need to use kernel feature for that.
 	 * Just need to store this value here */
@@ -1776,33 +1785,72 @@ static bool get_discoverable_timeout(void)
 	return true;
 }
 
-static bool get_property(void *buf, uint16_t len)
+static void handle_get_adapter_prop_cmd(const void *buf, uint16_t len)
 {
-	struct hal_cmd_get_adapter_prop *cmd = buf;
+	const struct hal_cmd_get_adapter_prop *cmd = buf;
+	uint8_t status;
 
 	switch (cmd->type) {
 	case HAL_PROP_ADAPTER_ADDR:
 		get_address();
-		return true;
+		break;
 	case HAL_PROP_ADAPTER_NAME:
-		return get_name();
+		if (!get_name()) {
+			status = HAL_STATUS_FAILED;
+			goto failed;
+		}
+		break;
 	case HAL_PROP_ADAPTER_UUIDS:
-		return get_uuids();
+		if (!get_uuids()) {
+			status = HAL_STATUS_FAILED;
+			goto failed;
+		}
+		break;
 	case HAL_PROP_ADAPTER_CLASS:
-		return get_class();
+		if (!get_class()) {
+			status = HAL_STATUS_FAILED;
+			goto failed;
+		}
+		break;
 	case HAL_PROP_ADAPTER_TYPE:
-		return get_type();
+		if (!get_type()) {
+			status = HAL_STATUS_FAILED;
+			goto failed;
+		}
+		break;
 	case HAL_PROP_ADAPTER_SERVICE_REC:
-		return get_service();
+		if (!get_service()) {
+			status = HAL_STATUS_FAILED;
+			goto failed;
+		}
+		break;
 	case HAL_PROP_ADAPTER_SCAN_MODE:
-		return get_scan_mode();
+		if (!get_scan_mode()) {
+			status = HAL_STATUS_FAILED;
+			goto failed;
+		}
+		break;
 	case HAL_PROP_ADAPTER_BONDED_DEVICES:
-		return get_devices();
+		if (!get_devices()) {
+			status = HAL_STATUS_FAILED;
+			goto failed;
+		}
+		break;
 	case HAL_PROP_ADAPTER_DISC_TIMEOUT:
-		return get_discoverable_timeout();
+		if (!get_discoverable_timeout()) {
+			status = HAL_STATUS_FAILED;
+			goto failed;
+		}
+		break;
 	default:
-		return false;
+		status = HAL_STATUS_FAILED;
+		goto failed;
 	}
+
+	status = HAL_STATUS_SUCCESS;
+
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_GET_ADAPTER_PROP, status);
 }
 
 static void get_properties(void)
@@ -1858,11 +1906,18 @@ static bool stop_discovery(void)
 	return false;
 }
 
-static uint8_t set_scan_mode(void *buf, uint16_t len)
+static uint8_t set_scan_mode(const void *buf, uint16_t len)
 {
-	uint8_t *mode = buf;
+	const uint8_t *mode = buf;
 	bool conn, disc, cur_conn, cur_disc;
 
+	if (len != sizeof(*mode)) {
+		error("Invalid set scan mode size (%u bytes), terminating",
+								len);
+		raise(SIGTERM);
+		return HAL_STATUS_FAILED;
+	}
+
 	cur_conn = adapter.current_settings & MGMT_SETTING_CONNECTABLE;
 	cur_disc = adapter.current_settings & MGMT_SETTING_DISCOVERABLE;
 
@@ -1914,21 +1969,35 @@ done:
 	return HAL_STATUS_DONE;
 }
 
-static uint8_t set_property(void *buf, uint16_t len)
+static void handle_set_adapter_prop_cmd(const void *buf, uint16_t len)
 {
-	struct hal_cmd_set_adapter_prop *cmd = buf;
+	const struct hal_cmd_set_adapter_prop *cmd = buf;
+	uint8_t status;
+
+	if (len != sizeof(*cmd) + cmd->len) {
+		error("Invalid set adapter prop cmd (0x%x), terminating",
+								cmd->type);
+		raise(SIGTERM);
+		return;
+	}
 
 	switch (cmd->type) {
 	case HAL_PROP_ADAPTER_SCAN_MODE:
-		return set_scan_mode(cmd->val, cmd->len);
+		status = set_scan_mode(cmd->val, cmd->len);
+		break;
 	case HAL_PROP_ADAPTER_NAME:
-		return set_adapter_name(cmd->val, cmd->len);
+		status = set_adapter_name(cmd->val, cmd->len);
+		break;
 	case HAL_PROP_ADAPTER_DISC_TIMEOUT:
-		return set_discoverable_timeout(cmd->val);
+		status = set_discoverable_timeout(cmd->val, cmd->len);
+		break;
 	default:
 		DBG("Unhandled property type 0x%x", cmd->type);
-		return HAL_STATUS_FAILED;
+		status = HAL_STATUS_FAILED;
+		break;
 	}
+
+	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_SET_ADAPTER_PROP, status);
 }
 
 static void pair_device_complete(uint8_t status, uint16_t length,
@@ -1947,9 +2016,10 @@ static void pair_device_complete(uint8_t status, uint16_t length,
 							HAL_BOND_STATE_NONE);
 }
 
-static bool create_bond(void *buf, uint16_t len)
+static void handle_create_bond_cmd(const void *buf, uint16_t len)
 {
-	struct hal_cmd_create_bond *cmd = buf;
+	const struct hal_cmd_create_bond *cmd = buf;
+	uint8_t status;
 	struct mgmt_cp_pair_device cp;
 
 	cp.io_cap = DEFAULT_IO_CAPABILITY;
@@ -1957,25 +2027,36 @@ static bool create_bond(void *buf, uint16_t len)
 	android2bdaddr(cmd->bdaddr, &cp.addr.bdaddr);
 
 	if (mgmt_send(mgmt_if, MGMT_OP_PAIR_DEVICE, adapter.index, sizeof(cp),
-				&cp, pair_device_complete, NULL, NULL) == 0)
-		return false;
+				&cp, pair_device_complete, NULL, NULL) == 0) {
+		status = HAL_STATUS_FAILED;
+		goto fail;
+	}
+
+	status = HAL_STATUS_SUCCESS;
 
 	set_device_bond_state(&cp.addr.bdaddr, HAL_STATUS_SUCCESS,
 						HAL_BOND_STATE_BONDING);
 
-	return true;
+fail:
+	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_CREATE_BOND, status);
 }
 
-static bool cancel_bond(void *buf, uint16_t len)
+static void handle_cancel_bond_cmd(const void *buf, uint16_t len)
 {
-	struct hal_cmd_cancel_bond *cmd = buf;
+	const struct hal_cmd_cancel_bond *cmd = buf;
 	struct mgmt_addr_info cp;
+	uint8_t status;
 
 	cp.type = BDADDR_BREDR;
 	android2bdaddr(cmd->bdaddr, &cp.bdaddr);
 
-	return mgmt_reply(mgmt_if, MGMT_OP_CANCEL_PAIR_DEVICE, adapter.index,
-					sizeof(cp), &cp, NULL, NULL, NULL) > 0;
+	if (mgmt_reply(mgmt_if, MGMT_OP_CANCEL_PAIR_DEVICE, adapter.index,
+					sizeof(cp), &cp, NULL, NULL, NULL) > 0)
+		status = HAL_STATUS_SUCCESS;
+	else
+		status = HAL_STATUS_FAILED;
+
+	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_CANCEL_BOND, status);
 }
 
 static void unpair_device_complete(uint8_t status, uint16_t length,
@@ -1992,23 +2073,30 @@ static void unpair_device_complete(uint8_t status, uint16_t length,
 							HAL_BOND_STATE_NONE);
 }
 
-static bool remove_bond(void *buf, uint16_t len)
+static void handle_remove_bond_cmd(const void *buf, uint16_t len)
 {
-	struct hal_cmd_remove_bond *cmd = buf;
+	const struct hal_cmd_remove_bond *cmd = buf;
 	struct mgmt_cp_unpair_device cp;
+	uint8_t status;
 
 	cp.disconnect = 1;
 	cp.addr.type = BDADDR_BREDR;
 	android2bdaddr(cmd->bdaddr, &cp.addr.bdaddr);
 
-	return mgmt_send(mgmt_if, MGMT_OP_UNPAIR_DEVICE, adapter.index,
+	if (mgmt_send(mgmt_if, MGMT_OP_UNPAIR_DEVICE, adapter.index,
 				sizeof(cp), &cp, unpair_device_complete,
-				NULL, NULL) > 0;
+				NULL, NULL) > 0)
+		status = HAL_STATUS_SUCCESS;
+	else
+		status = HAL_STATUS_FAILED;
+
+	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_REMOVE_BOND, status);
 }
 
-static uint8_t pin_reply(void *buf, uint16_t len)
+static void handle_pin_reply_cmd(const void *buf, uint16_t len)
 {
-	struct hal_cmd_pin_reply *cmd = buf;
+	const struct hal_cmd_pin_reply *cmd = buf;
+	uint8_t status;
 	bdaddr_t bdaddr;
 	char addr[18];
 
@@ -2017,8 +2105,10 @@ static uint8_t pin_reply(void *buf, uint16_t len)
 
 	DBG("%s accept %u pin_len %u", addr, cmd->accept, cmd->pin_len);
 
-	if (!cmd->accept && cmd->pin_len)
-		return HAL_STATUS_INVALID;
+	if (!cmd->accept && cmd->pin_len) {
+		status = HAL_STATUS_INVALID;
+		goto failed;
+	}
 
 	if (cmd->accept) {
 		struct mgmt_cp_pin_code_reply rp;
@@ -2031,8 +2121,10 @@ static uint8_t pin_reply(void *buf, uint16_t len)
 		memcpy(rp.pin_code, cmd->pin_code, rp.pin_len);
 
 		if (mgmt_reply(mgmt_if, MGMT_OP_PIN_CODE_REPLY, adapter.index,
-				sizeof(rp), &rp, NULL, NULL, NULL) == 0)
-			return HAL_STATUS_FAILED;
+				sizeof(rp), &rp, NULL, NULL, NULL) == 0) {
+			status = HAL_STATUS_FAILED;
+			goto failed;
+		}
 	} else {
 		struct mgmt_cp_pin_code_neg_reply rp;
 
@@ -2041,11 +2133,15 @@ static uint8_t pin_reply(void *buf, uint16_t len)
 
 		if (mgmt_reply(mgmt_if, MGMT_OP_PIN_CODE_NEG_REPLY,
 						adapter.index, sizeof(rp), &rp,
-						NULL, NULL, NULL) == 0)
-			return HAL_STATUS_FAILED;
+						NULL, NULL, NULL) == 0) {
+			status = HAL_STATUS_FAILED;
+			goto failed;
+		}
 	}
 
-	return HAL_STATUS_SUCCESS;
+	status = HAL_STATUS_SUCCESS;
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_PIN_REPLY, status);
 }
 
 static uint8_t user_confirm_reply(const bdaddr_t *bdaddr, bool accept)
@@ -2102,11 +2198,11 @@ static uint8_t user_passkey_reply(const bdaddr_t *bdaddr, bool accept,
 	return HAL_STATUS_SUCCESS;
 }
 
-static uint8_t ssp_reply(void *buf, uint16_t len)
+static void handle_ssp_reply_cmd(const void *buf, uint16_t len)
 {
-	struct hal_cmd_ssp_reply *cmd = buf;
-	uint8_t status;
+	const struct hal_cmd_ssp_reply *cmd = buf;
 	bdaddr_t bdaddr;
+	uint8_t status;
 	char addr[18];
 
 	/* TODO should parameters sanity be verified here? */
@@ -2133,144 +2229,226 @@ static uint8_t ssp_reply(void *buf, uint16_t len)
 		break;
 	}
 
-	return status;
+	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_SSP_REPLY, status);
 }
 
-static uint8_t get_remote_services(void *buf, uint16_t len)
+static void handle_get_remote_services_cmd(const void *buf, uint16_t len)
 {
-	struct hal_cmd_get_remote_services *cmd = buf;
+	const struct hal_cmd_get_remote_services *cmd = buf;
+	uint8_t status;
 	bdaddr_t addr;
 
 	android2bdaddr(&cmd->bdaddr, &addr);
 
-	return browse_remote_sdp(&addr);
+	status = browse_remote_sdp(&addr);
+
+	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_GET_REMOTE_SERVICES,
+									status);
 }
 
-void bt_bluetooth_handle_cmd(int sk, uint8_t opcode, void *buf, uint16_t len)
+static void handle_enable_cmd(const void *buf, uint16_t len)
 {
-	uint8_t status = HAL_STATUS_FAILED;
+	uint8_t status;
 
-	switch (opcode) {
-	case HAL_OP_ENABLE:
-		/* Framework expects all properties to be emitted while
-		 * enabling adapter */
-		get_properties();
+	/* Framework expects all properties to be emitted while
+	 * enabling adapter */
+	get_properties();
 
-		if (adapter.current_settings & MGMT_SETTING_POWERED) {
-			status = HAL_STATUS_DONE;
-			goto error;
-		}
+	if (adapter.current_settings & MGMT_SETTING_POWERED) {
+		status = HAL_STATUS_DONE;
+		goto failed;
+	}
 
-		if (!set_mode(MGMT_OP_SET_POWERED, 0x01))
-			goto error;
+	if (!set_mode(MGMT_OP_SET_POWERED, 0x01)) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
 
-		break;
-	case HAL_OP_DISABLE:
-		if (!(adapter.current_settings & MGMT_SETTING_POWERED)) {
-			status = HAL_STATUS_DONE;
-			goto error;
-		}
+	status = HAL_STATUS_SUCCESS;
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_ENABLE, status);
+}
 
-		if (!set_mode(MGMT_OP_SET_POWERED, 0x00))
-			goto error;
+static void handle_disable_cmd(const void *buf, uint16_t len)
+{
+	uint8_t status;
 
-		break;
-	case HAL_OP_GET_ADAPTER_PROPS:
-		get_properties();
+	if (!(adapter.current_settings & MGMT_SETTING_POWERED)) {
+		status = HAL_STATUS_DONE;
+		goto failed;
+	}
 
-		break;
-	case HAL_OP_GET_ADAPTER_PROP:
-		if (!get_property(buf, len))
-			goto error;
+	if (!set_mode(MGMT_OP_SET_POWERED, 0x00)) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
 
-		break;
-	case HAL_OP_SET_ADAPTER_PROP:
-		status = set_property(buf, len);
-		if (status != HAL_STATUS_SUCCESS && status != HAL_STATUS_DONE)
-			goto error;
+	status = HAL_STATUS_SUCCESS;
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_DISABLE, status);
+}
 
-		break;
-	case HAL_OP_CREATE_BOND:
-		if (!create_bond(buf, len))
-			goto error;
+static void handle_get_adapter_props_cmd(const void *buf, uint16_t len)
+{
+	get_properties();
 
-		break;
-	case HAL_OP_CANCEL_BOND:
-		if (!cancel_bond(buf, len))
-			goto error;
+	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_GET_ADAPTER_PROPS,
+							HAL_STATUS_SUCCESS);
+}
 
-		break;
-	case HAL_OP_REMOVE_BOND:
-		if (!remove_bond(buf, len))
-			goto error;
+static void handle_get_remote_device_props_cmd(const void *buf, uint16_t len)
+{
+	/* TODO */
 
-		break;
-	case HAL_OP_PIN_REPLY:
-		status = pin_reply(buf, len);
-		if (status != HAL_STATUS_SUCCESS)
-			goto error;
+	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_GET_REMOTE_DEVICE_PROPS,
+							HAL_STATUS_FAILED);
+}
 
-		break;
-	case HAL_OP_SSP_REPLY:
-		status = ssp_reply(buf, len);
-		if (status != HAL_STATUS_SUCCESS)
-			goto error;
-		break;
-	case HAL_OP_START_DISCOVERY:
-		if (adapter.discovering) {
-			status = HAL_STATUS_DONE;
-			goto error;
-		}
+static void handle_get_remote_device_prop_cmd(const void *buf, uint16_t len)
+{
+	/* TODO */
 
-		if (!(adapter.current_settings & MGMT_SETTING_POWERED)) {
-			status = HAL_STATUS_NOT_READY;
-			goto error;
-		}
+	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_GET_REMOTE_DEVICE_PROP,
+							HAL_STATUS_FAILED);
+}
+
+static void handle_set_remote_device_prop_cmd(const void *buf, uint16_t len)
+{
+	const struct hal_cmd_set_remote_device_prop *cmd = buf;
+	uint8_t status;
 
-		if (!start_discovery())
-			goto error;
+	if (len != sizeof(*cmd) + cmd->len) {
+		error("Invalid set remote device prop cmd (0x%x), terminating",
+								cmd->type);
+		raise(SIGTERM);
+		return;
+	}
 
+	/* TODO */
+
+	switch (cmd->type) {
+	default:
+		DBG("Unhandled property type 0x%x", cmd->type);
+		status = HAL_STATUS_FAILED;
 		break;
-	case HAL_OP_CANCEL_DISCOVERY:
-		if (!adapter.discovering) {
-			status = HAL_STATUS_DONE;
-			goto error;
-		}
+	}
 
-		if (!(adapter.current_settings & MGMT_SETTING_POWERED)) {
-			status = HAL_STATUS_NOT_READY;
-			goto error;
-		}
+	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_SET_REMOTE_DEVICE_PROP,
+									status);
+}
 
-		if (!stop_discovery())
-			goto error;
+static void handle_get_remote_service_rec_cmd(const void *buf, uint16_t len)
+{
+	/* TODO */
 
-		break;
-	case HAL_OP_GET_REMOTE_SERVICES:
-		status = get_remote_services(buf, len);
-		if (status != HAL_STATUS_SUCCESS)
-			goto error;
-		break;
-	default:
-		DBG("Unhandled command, opcode 0x%x", opcode);
-		goto error;
+	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_GET_REMOTE_SERVICE_REC,
+							HAL_STATUS_FAILED);
+}
+
+static void handle_start_discovery_cmd(const void *buf, uint16_t len)
+{
+	uint8_t status;
+
+	if (adapter.discovering) {
+		status = HAL_STATUS_DONE;
+		goto failed;
 	}
 
-	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, opcode, HAL_STATUS_SUCCESS);
-	return;
+	if (!(adapter.current_settings & MGMT_SETTING_POWERED)) {
+		status = HAL_STATUS_NOT_READY;
+		goto failed;
+	}
 
-error:
-	error("Error handling command 0x%02x status %u", opcode, status);
+	if (!start_discovery()) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
 
-	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, opcode, status);
+	status = HAL_STATUS_SUCCESS;
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_START_DISCOVERY, status);
 }
 
+static void handle_cancel_discovery_cmd(const void *buf, uint16_t len)
+{
+	uint8_t status;
+
+	if (!adapter.discovering) {
+		status = HAL_STATUS_DONE;
+		goto failed;
+	}
+
+	if (!(adapter.current_settings & MGMT_SETTING_POWERED)) {
+		status = HAL_STATUS_NOT_READY;
+		goto failed;
+	}
+
+	if (!stop_discovery()) {
+		status = HAL_STATUS_FAILED;
+		goto failed;
+	}
+
+	status = HAL_STATUS_SUCCESS;
+
+failed:
+	ipc_send_rsp(HAL_SERVICE_ID_BLUETOOTH, HAL_OP_CANCEL_DISCOVERY, status);
+}
+
+static const struct ipc_handler cmd_handlers[] = {
+	/* HAL_OP_ENABLE */
+	{ handle_enable_cmd, false, 0 },
+	/* HAL_OP_DISABLE */
+	{ handle_disable_cmd, false, 0 },
+	/* HAL_OP_GET_ADAPTER_PROPS */
+	{ handle_get_adapter_props_cmd, false, 0 },
+	/* HAL_OP_GET_ADAPTER_PROP */
+	{ handle_get_adapter_prop_cmd, false,
+				sizeof(struct hal_cmd_get_adapter_prop) },
+	/* HAL_OP_SET_ADAPTER_PROP */
+	{ handle_set_adapter_prop_cmd, true,
+				sizeof(struct hal_cmd_set_adapter_prop) },
+	/* HAL_OP_GET_REMOTE_DEVICE_PROPS */
+	{ handle_get_remote_device_props_cmd, false,
+			sizeof(struct hal_cmd_get_remote_device_props) },
+	/* HAL_OP_GET_REMOTE_DEVICE_PROP */
+	{ handle_get_remote_device_prop_cmd, false,
+				sizeof(struct hal_cmd_get_remote_device_prop) },
+	/* HAL_OP_SET_REMOTE_DEVICE_PROP */
+	{ handle_set_remote_device_prop_cmd, true,
+				sizeof(struct hal_cmd_set_remote_device_prop) },
+	/* HAL_OP_GET_REMOTE_SERVICE_REC */
+	{ handle_get_remote_service_rec_cmd, false,
+				sizeof(struct hal_cmd_get_remote_service_rec) },
+	/* HAL_OP_GET_REMOTE_SERVICES */
+	{ handle_get_remote_services_cmd, false,
+				sizeof(struct hal_cmd_get_remote_services) },
+	/* HAL_OP_START_DISCOVERY */
+	{ handle_start_discovery_cmd, false, 0 },
+	/* HAL_OP_CANCEL_DISCOVERY */
+	{ handle_cancel_discovery_cmd, false, 0 },
+	/* HAL_OP_CREATE_BOND */
+	{ handle_create_bond_cmd, false, sizeof(struct hal_cmd_create_bond) },
+	/* HAL_OP_REMOVE_BOND */
+	{ handle_remove_bond_cmd, false, sizeof(struct hal_cmd_remove_bond) },
+	/* HAL_OP_CANCEL_BOND */
+	{handle_cancel_bond_cmd, false, sizeof(struct hal_cmd_cancel_bond) },
+	/* HAL_OP_PIN_REPLY */
+	{ handle_pin_reply_cmd, false, sizeof(struct hal_cmd_pin_reply) },
+	/* HAL_OP_SSP_REPLY */
+	{ handle_ssp_reply_cmd, false, sizeof(struct hal_cmd_ssp_reply) },
+};
+
 void bt_bluetooth_register(void)
 {
 	DBG("");
+
+	ipc_register(HAL_SERVICE_ID_BLUETOOTH, cmd_handlers,
+				sizeof(cmd_handlers)/sizeof(cmd_handlers[0]));
 }
 
 void bt_bluetooth_unregister(void)
 {
 	DBG("");
+
+	ipc_unregister(HAL_SERVICE_ID_CORE);
 }
-- 
1.8.3.2


^ permalink raw reply related

* [PATCH v3 3/9] android/main: Use common exit path in core service functions
From: Szymon Janc @ 2013-12-02 12:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1385986848-8023-1-git-send-email-szymon.janc@tieto.com>

This makes functions exit path simpler.
---
 android/main.c | 40 +++++++++++++++++++++++++---------------
 1 file changed, 25 insertions(+), 15 deletions(-)

diff --git a/android/main.c b/android/main.c
index 0816ec7..c0f8901 100644
--- a/android/main.c
+++ b/android/main.c
@@ -77,9 +77,12 @@ static bool services[HAL_SERVICE_ID_MAX + 1] = { false };
 static void service_register(const void *buf, uint16_t len)
 {
 	const struct hal_cmd_register_module *m = buf;
+	uint8_t status;
 
-	if (m->service_id > HAL_SERVICE_ID_MAX || services[m->service_id])
+	if (m->service_id > HAL_SERVICE_ID_MAX || services[m->service_id]) {
+		status = HAL_STATUS_FAILED;
 		goto failed;
+	}
 
 	switch (m->service_id) {
 	case HAL_SERVICE_ID_BLUETOOTH:
@@ -91,43 +94,51 @@ static void service_register(const void *buf, uint16_t len)
 
 		break;
 	case HAL_SERVICE_ID_HIDHOST:
-		if (!bt_hid_register(&adapter_bdaddr))
+		if (!bt_hid_register(&adapter_bdaddr)) {
+			status = HAL_STATUS_FAILED;
 			goto failed;
+		}
 
 		break;
 	case HAL_SERVICE_ID_A2DP:
-		if (!bt_a2dp_register(&adapter_bdaddr))
+		if (!bt_a2dp_register(&adapter_bdaddr)) {
+			status = HAL_STATUS_FAILED;
 			goto failed;
+		}
 
 		break;
 	case HAL_SERVICE_ID_PAN:
-		if (!bt_pan_register(&adapter_bdaddr))
+		if (!bt_pan_register(&adapter_bdaddr)) {
+			status = HAL_STATUS_FAILED;
 			goto failed;
+		}
 
 		break;
 	default:
 		DBG("service %u not supported", m->service_id);
+		status = HAL_STATUS_FAILED;
 		goto failed;
 	}
 
 	services[m->service_id] = true;
 
-	ipc_send_rsp(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
-							HAL_STATUS_SUCCESS);
+	status = HAL_STATUS_SUCCESS;
 
 	info("Service ID=%u registered", m->service_id);
-	return;
+
 failed:
-	ipc_send_rsp(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
-							HAL_STATUS_FAILED);
+	ipc_send_rsp(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE, status);
 }
 
 static void service_unregister(const void *buf, uint16_t len)
 {
 	const struct hal_cmd_unregister_module *m = buf;
+	uint8_t status;
 
-	if (m->service_id > HAL_SERVICE_ID_MAX || !services[m->service_id])
+	if (m->service_id > HAL_SERVICE_ID_MAX || !services[m->service_id]) {
+		status = HAL_STATUS_FAILED;
 		goto failed;
+	}
 
 	switch (m->service_id) {
 	case HAL_SERVICE_ID_BLUETOOTH:
@@ -149,19 +160,18 @@ static void service_unregister(const void *buf, uint16_t len)
 		/* This would indicate bug in HAL, as unregister should not be
 		 * called in init failed */
 		DBG("service %u not supported", m->service_id);
+		status = HAL_STATUS_FAILED;
 		goto failed;
 	}
 
 	services[m->service_id] = false;
 
-	ipc_send_rsp(HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE,
-							HAL_STATUS_SUCCESS);
+	status = HAL_STATUS_SUCCESS;
 
 	info("Service ID=%u unregistered", m->service_id);
-	return;
+
 failed:
-	ipc_send_rsp(HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE,
-							HAL_STATUS_FAILED);
+	ipc_send_rsp(HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE, status);
 }
 
 static const struct ipc_handler cmd_handlers[] = {
-- 
1.8.3.2


^ permalink raw reply related

* [PATCH v3 2/9] android/main: Use generic IPC message handling for core service
From: Szymon Janc @ 2013-12-02 12:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1385986848-8023-1-git-send-email-szymon.janc@tieto.com>

Handlers are registered on daemon start and unregistered on shutdown.
---
 android/main.c | 83 +++++++++++-----------------------------------------------
 1 file changed, 16 insertions(+), 67 deletions(-)

diff --git a/android/main.c b/android/main.c
index eedca58..0816ec7 100644
--- a/android/main.c
+++ b/android/main.c
@@ -74,9 +74,9 @@ static GIOChannel *hal_notif_io = NULL;
 
 static bool services[HAL_SERVICE_ID_MAX + 1] = { false };
 
-static void service_register(void *buf, uint16_t len)
+static void service_register(const void *buf, uint16_t len)
 {
-	struct hal_cmd_register_module *m = buf;
+	const struct hal_cmd_register_module *m = buf;
 
 	if (m->service_id > HAL_SERVICE_ID_MAX || services[m->service_id])
 		goto failed;
@@ -122,9 +122,9 @@ failed:
 							HAL_STATUS_FAILED);
 }
 
-static void service_unregister(void *buf, uint16_t len)
+static void service_unregister(const void *buf, uint16_t len)
 {
-	struct hal_cmd_unregister_module *m = buf;
+	const struct hal_cmd_unregister_module *m = buf;
 
 	if (m->service_id > HAL_SERVICE_ID_MAX || !services[m->service_id])
 		goto failed;
@@ -164,20 +164,12 @@ failed:
 							HAL_STATUS_FAILED);
 }
 
-static void handle_service_core(uint8_t opcode, void *buf, uint16_t len)
-{
-	switch (opcode) {
-	case HAL_OP_REGISTER_MODULE:
-		service_register(buf, len);
-		break;
-	case HAL_OP_UNREGISTER_MODULE:
-		service_unregister(buf, len);
-		break;
-	default:
-		ipc_send_rsp(HAL_SERVICE_ID_CORE, opcode, HAL_STATUS_FAILED);
-		break;
-	}
-}
+static const struct ipc_handler cmd_handlers[] = {
+	/* HAL_OP_REGISTER_MODULE */
+	{ service_register, false, sizeof(struct hal_cmd_register_module) },
+	/* HAL_OP_UNREGISTER_MODULE */
+	{ service_unregister, false, sizeof(struct hal_cmd_unregister_module) },
+};
 
 static void bluetooth_stopped(void)
 {
@@ -211,7 +203,6 @@ static gboolean cmd_watch_cb(GIOChannel *io, GIOCondition cond,
 							gpointer user_data)
 {
 	char buf[BLUEZ_HAL_MTU];
-	struct hal_hdr *msg = (void *) buf;
 	ssize_t ret;
 	int fd;
 
@@ -229,51 +220,7 @@ static gboolean cmd_watch_cb(GIOChannel *io, GIOCondition cond,
 		goto fail;
 	}
 
-	if (ret < (ssize_t) sizeof(*msg)) {
-		error("HAL command too small, terminating (%zd)", ret);
-		goto fail;
-	}
-
-	if (ret != (ssize_t) (sizeof(*msg) + msg->len)) {
-		error("Malformed HAL command (%zd bytes), terminating", ret);
-		goto fail;
-	}
-
-	DBG("service_id %u opcode %u len %u", msg->service_id, msg->opcode,
-								msg->len);
-
-	if (msg->service_id > HAL_SERVICE_ID_MAX ||
-						!services[msg->service_id]) {
-		error("HAL command for unregistered service %u, terminating",
-							msg->service_id);
-		goto fail;
-	}
-
-	switch (msg->service_id) {
-	case HAL_SERVICE_ID_CORE:
-		handle_service_core(msg->opcode, msg->payload, msg->len);
-		break;
-	case HAL_SERVICE_ID_BLUETOOTH:
-		bt_bluetooth_handle_cmd(fd, msg->opcode, msg->payload,
-								msg->len);
-		break;
-	case HAL_SERVICE_ID_HIDHOST:
-		bt_hid_handle_cmd(fd, msg->opcode, msg->payload, msg->len);
-		break;
-	case HAL_SERVICE_ID_SOCK:
-		bt_sock_handle_cmd(fd, msg->opcode, msg->payload, msg->len);
-		break;
-	case HAL_SERVICE_ID_A2DP:
-		bt_a2dp_handle_cmd(fd, msg->opcode, msg->payload, msg->len);
-		break;
-	case HAL_SERVICE_ID_PAN:
-		bt_pan_handle_cmd(fd, msg->opcode, msg->payload, msg->len);
-		break;
-	default:
-		ipc_send_rsp(msg->service_id, msg->opcode, HAL_STATUS_FAILED);
-		break;
-	}
-
+	ipc_handle_msg(buf, ret);
 	return TRUE;
 
 fail:
@@ -568,9 +515,6 @@ int main(int argc, char *argv[])
 	GError *err = NULL;
 	guint signal;
 
-	/* Core Service (ID=0) should always be considered registered */
-	services[0] = true;
-
 	context = g_option_context_new(NULL);
 	g_option_context_add_main_entries(context, options, NULL);
 
@@ -622,6 +566,9 @@ int main(int argc, char *argv[])
 	/* Use params: mtu = 0, flags = 0 */
 	start_sdp_server(0, 0);
 
+	ipc_register(HAL_SERVICE_ID_CORE, cmd_handlers,
+				sizeof(cmd_handlers)/sizeof(cmd_handlers[0]));
+
 	DBG("Entering main loop");
 
 	event_loop = g_main_loop_new(NULL, FALSE);
@@ -640,6 +587,8 @@ int main(int argc, char *argv[])
 	bt_bluetooth_cleanup();
 	g_main_loop_unref(event_loop);
 
+	ipc_unregister(HAL_SERVICE_ID_CORE);
+
 	info("Exit");
 
 	__btd_log_cleanup();
-- 
1.8.3.2


^ permalink raw reply related

* [PATCH v3 1/9] android: Add initial code for IPC message handlers
From: Szymon Janc @ 2013-12-02 12:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1385986848-8023-1-git-send-email-szymon.janc@tieto.com>

This will allow to register and unregister handlers for IPC messages
Basic sanity check will be done in common code. Commands with variable
length will be verified against minimum size only.
---
 android/ipc.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 android/ipc.h | 10 ++++++++
 2 files changed, 88 insertions(+)

diff --git a/android/ipc.c b/android/ipc.c
index 64b0db5..56f328b 100644
--- a/android/ipc.c
+++ b/android/ipc.c
@@ -30,12 +30,20 @@
 #include <stdint.h>
 #include <string.h>
 #include <signal.h>
+#include <stdbool.h>
 #include <sys/socket.h>
 
 #include "hal-msg.h"
 #include "ipc.h"
 #include "log.h"
 
+struct service_handler {
+	const struct ipc_handler *handler;
+	uint8_t size;
+};
+
+static struct service_handler services[HAL_SERVICE_ID_MAX + 1];
+
 static int cmd_sk = -1;
 static int notif_sk = -1;
 
@@ -124,3 +132,73 @@ void ipc_send_notif(uint8_t service_id, uint8_t opcode,  uint16_t len,
 
 	ipc_send(notif_sk, service_id, opcode, len, param, -1);
 }
+
+void ipc_register(uint8_t service, const struct ipc_handler *handlers,
+								uint8_t size)
+{
+	services[service].handler = handlers;
+	services[service].size = size;
+}
+
+void ipc_unregister(uint8_t service)
+{
+	services[service].handler = NULL;
+	services[service].size = 0;
+}
+
+void ipc_handle_msg(const void *buf, ssize_t len)
+{
+	const struct hal_hdr *msg = buf;
+	const struct ipc_handler *handler;
+
+	if (len < (ssize_t) sizeof(*msg)) {
+		error("IPC: message too small (%zd bytes), terminating", len);
+		raise(SIGTERM);
+		return;
+	}
+
+	if (len != (ssize_t) (sizeof(*msg) + msg->len)) {
+		error("IPC: message malformed (%zd bytes), terminating", len);
+		raise(SIGTERM);
+		return;
+	}
+
+	/* if service is valid */
+	if (msg->service_id > HAL_SERVICE_ID_MAX) {
+		error("IPC: unknown service (0x%x), terminating",
+							msg->service_id);
+		raise(SIGTERM);
+		return;
+	}
+
+	/* if service is registered */
+	if (!services[msg->service_id].handler) {
+		error("IPC: unregistered service (0x%x), terminating",
+							msg->service_id);
+		raise(SIGTERM);
+		return;
+	}
+
+	/* if opcode is valid */
+	if (msg->opcode == HAL_OP_STATUS ||
+			msg->opcode > services[msg->service_id].size) {
+		error("IPC: invalid opcode 0x%x for service 0x%x, terminating",
+						msg->opcode, msg->service_id);
+		raise(SIGTERM);
+		return;
+	}
+
+	/* opcode is table offset + 1 */
+	handler = &services[msg->service_id].handler[msg->opcode - 1];
+
+	/* if payload size is valid */
+	if ((handler->var_len && handler->data_len > msg->len) ||
+			(!handler->var_len && handler->data_len != msg->len)) {
+		error("IPC: size invalid opcode 0x%x service 0x%x, terminating",
+						msg->service_id, msg->opcode);
+		raise(SIGTERM);
+		return;
+	}
+
+	handler->handler(msg->payload, msg->len);
+}
diff --git a/android/ipc.h b/android/ipc.h
index f66c9e0..9d0c5e1 100644
--- a/android/ipc.h
+++ b/android/ipc.h
@@ -21,6 +21,11 @@
  *
  */
 
+struct ipc_handler {
+	void (*handler) (const void *buf, uint16_t len);
+	bool var_len;
+	size_t data_len;
+};
 void ipc_init(int command_sk, int notification_sk);
 void ipc_cleanup(void);
 
@@ -29,3 +34,8 @@ void ipc_send_rsp_full(uint8_t service_id, uint8_t opcode, uint16_t len,
 							void *param, int fd);
 void ipc_send_notif(uint8_t service_id, uint8_t opcode,  uint16_t len,
 								void *param);
+void ipc_register(uint8_t service, const struct ipc_handler *handlers,
+								uint8_t size);
+void ipc_unregister(uint8_t service);
+
+void ipc_handle_msg(const void *buf, ssize_t len);
-- 
1.8.3.2


^ permalink raw reply related

* [PATCH v3 0/9] android: IPC improvements - daemon part
From: Szymon Janc @ 2013-12-02 12:20 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc

v3:
 - rebased againt latest pan changes

v2:
 - rebased against latest IPC helpers improvements
 - more compact command handlers table format
 - error handling path in command handlers improved according to Johan comments
 - randmon small fixes
 - patches not directly related to refactor removed from serie, will
   be send after this is merged

v1:
This serie implements IPC message handling iprovments in daemon similar
to what is already done in HAL part.

Szymon Janc (9):
  android: Add initial code for IPC message handlers
  android/main: Use generic IPC message handling for core service
  android/main: Use common exit path in core service functions
  android/bluetooth: Use generic IPC msg handling for commands
  android/bluetooth: Make property handling function return HAL status
  android/hidhost: Use generic IPC message handling for commands
  android/pan: Use generic IPC message handling for commands
  android/a2dp: Use generic IPC message handling for commands
  android/socket: Use generic IPC message handling for commands

 android/a2dp.c      |  69 ++++----
 android/a2dp.h      |   2 -
 android/bluetooth.c | 477 ++++++++++++++++++++++++++++++++++------------------
 android/hidhost.c   | 309 ++++++++++++++++++++--------------
 android/hidhost.h   |   2 -
 android/ipc.c       |  78 +++++++++
 android/ipc.h       |  10 ++
 android/main.c      | 123 +++++---------
 android/pan.c       |  87 +++++-----
 android/pan.h       |   2 -
 android/socket.c    | 102 ++++++-----
 11 files changed, 754 insertions(+), 507 deletions(-)

-- 
1.8.3.2


^ permalink raw reply

* Re: [PATCH 0/2] Bluetooth: A couple of SMP fixes
From: Marcel Holtmann @ 2013-12-02 11:23 UTC (permalink / raw)
  To: Johan Hedberg; +Cc: linux-bluetooth@vger.kernel.org development
In-Reply-To: <1385974144-8836-1-git-send-email-johan.hedberg@gmail.com>

Hi Johan,

> Here are a couple of small tweaks to the SMP code that I've found while
> browsing through and testing it (with the user space smp-tester).
> 
> Johan
> 
> Johan Hedberg (2):
>  Bluetooth: Remove useless smp_rand function
>  Bluetooth: Remove dead code from SMP encryption function
> 
> net/bluetooth/smp.c | 24 +++---------------------
> 1 file changed, 3 insertions(+), 21 deletions(-)

both patches have been applied to bluetooth-next.

Regards

Marcel


^ permalink raw reply

* [PATCH 2/2] Bluetooth: Remove dead code from SMP encryption function
From: johan.hedberg @ 2013-12-02  8:49 UTC (permalink / raw)
  To: linux-bluetooth
In-Reply-To: <1385974144-8836-1-git-send-email-johan.hedberg@gmail.com>

From: Johan Hedberg <johan.hedberg@intel.com>

The AES cipher is used in ECB mode by SMP and therefore doesn't use an
IV (crypto_blkcipher_ivsize returns 0) so the code trying to set the IV
was never getting called. Simply remove this code to avoid anyone from
thinking it actually makes some difference.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
---
 net/bluetooth/smp.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)

diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
index 3bcb765b6a92..e61e74a1aabb 100644
--- a/net/bluetooth/smp.c
+++ b/net/bluetooth/smp.c
@@ -53,8 +53,7 @@ static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
 {
 	struct blkcipher_desc desc;
 	struct scatterlist sg;
-	int err, iv_len;
-	unsigned char iv[128];
+	int err;
 
 	if (tfm == NULL) {
 		BT_ERR("tfm %p", tfm);
@@ -72,12 +71,6 @@ static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
 
 	sg_init_one(&sg, r, 16);
 
-	iv_len = crypto_blkcipher_ivsize(tfm);
-	if (iv_len) {
-		memset(&iv, 0xff, iv_len);
-		crypto_blkcipher_set_iv(tfm, iv, iv_len);
-	}
-
 	err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
 	if (err)
 		BT_ERR("Encrypt data error %d", err);
-- 
1.8.4.2


^ permalink raw reply related

* [PATCH 1/2] Bluetooth: Remove useless smp_rand function
From: johan.hedberg @ 2013-12-02  8:49 UTC (permalink / raw)
  To: linux-bluetooth
In-Reply-To: <1385974144-8836-1-git-send-email-johan.hedberg@gmail.com>

From: Johan Hedberg <johan.hedberg@intel.com>

This function was always just making a single get_random_bytes() call
and always returning the value 0. It's simpler to just call
get_random_bytes() directly where needed.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
---
 net/bluetooth/smp.c | 15 ++-------------
 1 file changed, 2 insertions(+), 13 deletions(-)

diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
index f99352d1aa43..3bcb765b6a92 100644
--- a/net/bluetooth/smp.c
+++ b/net/bluetooth/smp.c
@@ -143,13 +143,6 @@ static int smp_s1(struct crypto_blkcipher *tfm, u8 k[16], u8 r1[16],
 	return err;
 }
 
-static int smp_rand(u8 *buf)
-{
-	get_random_bytes(buf, 16);
-
-	return 0;
-}
-
 static struct sk_buff *smp_build_cmd(struct l2cap_conn *conn, u8 code,
 				     u16 dlen, void *data)
 {
@@ -606,9 +599,7 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
 	if (check_enc_key_size(conn, key_size))
 		return SMP_ENC_KEY_SIZE;
 
-	ret = smp_rand(smp->prnd);
-	if (ret)
-		return SMP_UNSPECIFIED;
+	get_random_bytes(smp->prnd, sizeof(smp->prnd));
 
 	smp->prsp[0] = SMP_CMD_PAIRING_RSP;
 	memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
@@ -644,9 +635,7 @@ static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
 	if (check_enc_key_size(conn, key_size))
 		return SMP_ENC_KEY_SIZE;
 
-	ret = smp_rand(smp->prnd);
-	if (ret)
-		return SMP_UNSPECIFIED;
+	get_random_bytes(smp->prnd, sizeof(smp->prnd));
 
 	smp->prsp[0] = SMP_CMD_PAIRING_RSP;
 	memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
-- 
1.8.4.2


^ permalink raw reply related

* [PATCH 0/2] Bluetooth: A couple of SMP fixes
From: johan.hedberg @ 2013-12-02  8:49 UTC (permalink / raw)
  To: linux-bluetooth

From: Johan Hedberg <johan.hedberg@intel.com>

Hi,

Here are a couple of small tweaks to the SMP code that I've found while
browsing through and testing it (with the user space smp-tester).

Johan

Johan Hedberg (2):
  Bluetooth: Remove useless smp_rand function
  Bluetooth: Remove dead code from SMP encryption function

 net/bluetooth/smp.c | 24 +++---------------------
 1 file changed, 3 insertions(+), 21 deletions(-)

-- 
1.8.4.2


^ permalink raw reply

* Re: have to re-pair mouse every few hours
From: Brian J. Murrell @ 2013-12-01 22:44 UTC (permalink / raw)
  To: Bastien Nocera; +Cc: linux-bluetooth
In-Reply-To: <1385937507.5405.5.camel@nuvo>

[-- Attachment #1: Type: text/plain, Size: 629 bytes --]

On Sun, 2013-12-01 at 23:38 +0100, Bastien Nocera wrote: 
> 
> You could try with a LiveCD.

I don't think a LiveCD would be a useful enough work environment for me
to use long enough to see the problem occur.  It has happened within
minutes once or twice but typically it can take several hours.  This
afternoon has actually been quite good a quite a few hours.

I don't know that there is enough useful stuff for me to do (without a
whole ton of configuring self preferences) in a livecd environment for
me to keep using it for hours.  Probably upgrading to F20 is the most
productive way to see.

Cheers,
b.


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

^ permalink raw reply

* Re: have to re-pair mouse every few hours
From: Bastien Nocera @ 2013-12-01 22:38 UTC (permalink / raw)
  To: Brian J. Murrell; +Cc: linux-bluetooth
In-Reply-To: <1385937375.12122.118.camel@pc.interlinx.bc.ca>

On Sun, 2013-12-01 at 17:36 -0500, Brian J. Murrell wrote:
> On Sun, 2013-12-01 at 23:28 +0100, Bastien Nocera wrote: 
> > Hey Brian,
> 
> Hi Bastien,
> 
> > Upstream developers don't work on BlueZ 4.x anymore. Any chance for you
> > to test using Fedora 20?
> 
> Hrm.  Scheduling an upgrade, complete with back-out path here is bit of
> work.  I will see what I can do.  But for F19 I guess I am just stuck
> with this frustrating behavior?  :-(

You could try with a LiveCD.

> > It uses BlueZ 5.x which has a lot of fixes and
> > architectural changes compared to 4.x.
> 
> Interesting to know.
> 
> Cheers,
> b.
> 



^ permalink raw reply

* Re: have to re-pair mouse every few hours
From: Brian J. Murrell @ 2013-12-01 22:36 UTC (permalink / raw)
  To: Bastien Nocera; +Cc: linux-bluetooth
In-Reply-To: <1385936884.5405.3.camel@nuvo>

[-- Attachment #1: Type: text/plain, Size: 512 bytes --]

On Sun, 2013-12-01 at 23:28 +0100, Bastien Nocera wrote: 
> Hey Brian,

Hi Bastien,

> Upstream developers don't work on BlueZ 4.x anymore. Any chance for you
> to test using Fedora 20?

Hrm.  Scheduling an upgrade, complete with back-out path here is bit of
work.  I will see what I can do.  But for F19 I guess I am just stuck
with this frustrating behavior?  :-(

> It uses BlueZ 5.x which has a lot of fixes and
> architectural changes compared to 4.x.

Interesting to know.

Cheers,
b.


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 490 bytes --]

^ permalink raw reply

* Re: have to re-pair mouse every few hours
From: Bastien Nocera @ 2013-12-01 22:28 UTC (permalink / raw)
  To: Brian J. Murrell; +Cc: linux-bluetooth
In-Reply-To: <1385843856.12122.5.camel@pc.interlinx.bc.ca>

Hey Brian,

On Sat, 2013-11-30 at 15:37 -0500, Brian J. Murrell wrote:
> Hi,
> 
> I'm using a Microsoft Sculpt Touch Mouse on a Fedora 19 machine
> (kernel-3.11.9-200.fc19.x86_64 and bluez-4.101-9.fc19.x86_64) with a:
> 
> Bus 002 Device 019: ID 0a5c:2148 Broadcom Corp. BCM92046DG-CL1ROM Bluetooth 2.1 Adapter
> 
> bluetooth adapter.  This exact same configuration worked for months just
> fine with a Logitech bluetooth mouse.  But sadly that mouse was crap
> (second one to fail within the warranty period of one of them) so
> replaced it with this MS one.
> 
> The problem with this MS mouse is that it just goes AWOL and needs to be
> delete and re-paired with the machine every few hours.  Typically it's
> after I have gotten up from the computer and have come back to it.  But
> I have also had it just happen while using it.
> 
> When this happens, the messages log reports messages such as:
> 
> Nov 30 12:42:03 pc kernel: [2500032.028982] Bluetooth: Unexpected continuation frame (len 0)
> Nov 30 12:42:03 pc kernel: [2500032.115027] Bluetooth: Unexpected continuation frame (len 0)
> 
> Any ideas what the problem might be here?

Upstream developers don't work on BlueZ 4.x anymore. Any chance for you
to test using Fedora 20? It uses BlueZ 5.x which has a lot of fixes and
architectural changes compared to 4.x.

Cheers


^ permalink raw reply

* Add empty udev rule to disable hid2hci by default
From: Alexander Holler @ 2013-12-01 17:26 UTC (permalink / raw)
  To: linux-bluetooth

Hello.

Almost every distribution gets it wrong and enables hid2hci by default 
(besides Fedora where I already intervened twice).

This is a real problem, because it disables Bluetooth keyboards and/or 
mice which aren't paired with bluez, thus many Live-CDs and default 
installs aren't usable when only a Bluetooth keyboard is connect.

An easy solution to disable that behaviour would be to install an empty 
rule in /etc/udev/rules.d named the same as the one in 
/lib/udev/rules.d. It could just contain a comment like

# Delete this file in order to activate hid2hci.
#
# You might need to pair your Bluetooth keyboard and/or mouse
# in order to still use it when hid2hci is enabled.

This (empty) rule would then be used by udev instead of the one in 
/lib/udev/rules.d and thus would be an easy to use configuration switch.

I would appreciate it, if the default bluez install would install such 
an empty rule too, if configure was called with --enable-hid2hci.

I think otherwise that problem will never go away. It's really 
unbelievable how many distributions got this wrong and thus how many 
Live-CDs and default installations are unusable when only a Bluetooth 
keyboard is used with a hid-aware Bluetooth dongle.

Regards,

Alexander Holler

^ permalink raw reply

* [PATCH 4/4] sixaxis: Add support for setting LEDs when connected over USB
From: Szymon Janc @ 2013-12-01 13:41 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Szymon Janc
In-Reply-To: <1385905316-21800-1-git-send-email-szymon.janc@gmail.com>

This allows to setup LEDs when device is connected over USB, not
Bluetooth. This coverts two scenarios:
- user plugged PS3 controller and pressed PS3 button before unplugging,
  in that case LEDs are set
- user plugged already BT connected PS3 controller to USB, this results
  in new /dev/input/jsX device being create but controller is still
  transmitting over BT and old jsX device exists. In that case don't
  set LEDs as they are already set.

This is not directly related to Bluetooth itself but change is really
small and provides much better and consistent user experience.
---
 plugins/sixaxis.c | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/plugins/sixaxis.c b/plugins/sixaxis.c
index b8fe287..45fa170 100644
--- a/plugins/sixaxis.c
+++ b/plugins/sixaxis.c
@@ -181,7 +181,7 @@ static gboolean setup_leds(GIOChannel *channel, GIOCondition cond,
 	return FALSE;
 }
 
-static void setup_device(int fd, int index, struct btd_adapter *adapter)
+static bool setup_device(int fd, int index, struct btd_adapter *adapter)
 {
 	char device_addr[18], master_addr[18], adapter_addr[18];
 	bdaddr_t device_bdaddr, master_bdaddr;
@@ -189,22 +189,23 @@ static void setup_device(int fd, int index, struct btd_adapter *adapter)
 	struct btd_device *device;
 
 	if (get_device_bdaddr(fd, &device_bdaddr) < 0)
-		return;
+		return false;
 
 	if (get_master_bdaddr(fd, &master_bdaddr) < 0)
-		return;
+		return false;
 
 	/* This can happen if controller was plugged while already connected
-	 * eg. to charge up battery */
+	 * eg. to charge up battery.
+	 * Don't set LEDs in that case, hence return false */
 	device = btd_adapter_find_device(adapter, &device_bdaddr);
 	if (device && btd_device_is_connected(device))
-		return;
+		return false;
 
 	adapter_bdaddr = btd_adapter_get_address(adapter);
 
 	if (bacmp(adapter_bdaddr, &master_bdaddr)) {
 		if (set_master_bdaddr(fd, adapter_bdaddr) < 0)
-			return;
+			return false;
 	}
 
 	ba2str(&device_bdaddr, device_addr);
@@ -218,7 +219,7 @@ static void setup_device(int fd, int index, struct btd_adapter *adapter)
 	if (g_slist_find_custom(btd_device_get_uuids(device), HID_UUID,
 						(GCompareFunc)strcasecmp)) {
 		DBG("device %s already known, skipping", device_addr);
-		return;
+		return true;
 	}
 
 	info("sixaxis: setting up new device");
@@ -228,6 +229,8 @@ static void setup_device(int fd, int index, struct btd_adapter *adapter)
 				devices[index].pid, devices[index].version);
 	btd_device_set_temporary(device, FALSE);
 	btd_device_set_trusted(device, TRUE);
+
+	return true;
 }
 
 static int get_js_number(struct udev_device *udevice)
@@ -346,8 +349,10 @@ static void device_added(struct udev_device *udevice)
 
 	switch (bus) {
 	case BUS_USB:
-		setup_device(fd, index, adapter);
-		break;
+		if (!setup_device(fd, index, adapter))
+			break;
+
+		/* fall through */
 	case BUS_BLUETOOTH:
 		/* wait for events before setting leds */
 		g_io_add_watch(io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
-- 
1.8.5


^ permalink raw reply related


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox