Diff between 16265b991696a13de4b92357496a498497c5bb21 and 3852d13605ab970b4cf2d84d3a2f77085930609f

Changed Files

File Additions Deletions Status
android/main.c +40 -3 modified

Full Patch

diff --git a/android/main.c b/android/main.c
index b24451e..4417e3b 100644
--- a/android/main.c
+++ b/android/main.c
@@ -48,6 +48,7 @@
 
 #include "adapter.h"
 #include "hal-msg.h"
+#include "ipc.h"
 
 static GMainLoop *event_loop;
 static struct mgmt *mgmt_if = NULL;
@@ -65,9 +66,45 @@ static volatile sig_atomic_t __terminated = 0;
 static gboolean cmd_watch_cb(GIOChannel *io, GIOCondition cond,
 							gpointer user_data)
 {
-	info("HAL command socket closed, terminating");
-	g_main_loop_quit(event_loop);
+	int fd;
+	ssize_t ret;
+	char buf[BLUEZ_HAL_MTU];
+	struct hal_msg_hdr *msg = (void *) buf;
+
+	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;
+	}
+
+	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;
+	}
+
+	switch (msg->service_id) {
+	default:
+		ipc_send_error(hal_cmd_io, msg->service_id, 0x01);
+		break;
+	}
+
+	return TRUE;
 
+fail:
+	g_main_loop_quit(event_loop);
 	return FALSE;
 }
 
@@ -151,7 +188,7 @@ static gboolean cmd_connect_cb(GIOChannel *io, GIOCondition cond,
 		return FALSE;
 	}
 
-	cond = G_IO_ERR | G_IO_HUP | G_IO_NVAL;
+	cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
 
 	g_io_add_watch(io, cond, cmd_watch_cb, NULL);