All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jakub Tyszkowski <jakub.tyszkowski@tieto.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCHv5 03/11] android/ipc-tester: Add IPC initialization
Date: Mon, 20 Jan 2014 10:36:02 +0100	[thread overview]
Message-ID: <1390210570-28260-4-git-send-email-jakub.tyszkowski@tieto.com> (raw)
In-Reply-To: <1390210570-28260-1-git-send-email-jakub.tyszkowski@tieto.com>

This patch adds IPC mechanism initialization.
The deamon is being started and IPC socket connection is established.
---
 android/ipc-tester.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 125 insertions(+)

diff --git a/android/ipc-tester.c b/android/ipc-tester.c
index c8d1f0b..ff17ced 100644
--- a/android/ipc-tester.c
+++ b/android/ipc-tester.c
@@ -23,6 +23,8 @@
 
 #include <stdlib.h>
 #include <unistd.h>
+#include <errno.h>
+#include <poll.h>
 
 #include <sys/socket.h>
 #include <sys/types.h>
@@ -38,6 +40,8 @@
 #include "src/shared/mgmt.h"
 #include "src/shared/hciemu.h"
 
+#include "hal-msg.h"
+#include <cutils/properties.h>
 
 #define WAIT_FOR_SIGNAL_TIME 2 /* in seconds */
 #define EMULATOR_SIGNAL "emulator_started"
@@ -50,8 +54,14 @@ struct test_data {
 	pid_t bluetoothd_pid;
 };
 
+#define CONNECT_TIMEOUT (5 * 1000)
+#define SERVICE_NAME "bluetoothd"
+
 static char exec_dir[PATH_MAX + 1];
 
+static int cmd_sk = -1;
+static int notif_sk = -1;
+
 static void read_info_callback(uint8_t status, uint16_t length,
 					const void *param, void *user_data)
 {
@@ -248,6 +258,112 @@ failed:
 	close(fd);
 }
 
+static int accept_connection(int sk)
+{
+	int err;
+	struct pollfd pfd;
+	int new_sk;
+
+	memset(&pfd, 0 , sizeof(pfd));
+	pfd.fd = sk;
+	pfd.events = POLLIN;
+
+	err = poll(&pfd, 1, CONNECT_TIMEOUT);
+	if (err < 0) {
+		err = errno;
+		tester_warn("Failed to poll: %d (%s)", err, strerror(err));
+		return -errno;
+	}
+
+	if (err == 0) {
+		tester_warn("bluetoothd connect timeout");
+		return -errno;
+	}
+
+	new_sk = accept(sk, NULL, NULL);
+	if (new_sk < 0) {
+		err = errno;
+		tester_warn("Failed to accept socket: %d (%s)",
+							err, strerror(err));
+		return -errno;
+	}
+
+	return new_sk;
+}
+
+static bool init_ipc(void)
+{
+	struct sockaddr_un addr;
+
+	int sk;
+	int err;
+
+	sk = socket(AF_LOCAL, SOCK_SEQPACKET, 0);
+	if (sk < 0) {
+		err = errno;
+		tester_warn("Failed to create socket: %d (%s)", err,
+							strerror(err));
+		return false;
+	}
+
+	memset(&addr, 0, sizeof(addr));
+	addr.sun_family = AF_UNIX;
+
+	memcpy(addr.sun_path, BLUEZ_HAL_SK_PATH, sizeof(BLUEZ_HAL_SK_PATH));
+
+	if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+		err = errno;
+		tester_warn("Failed to bind socket: %d (%s)", err,
+								strerror(err));
+		close(sk);
+		return false;
+	}
+
+	if (listen(sk, 2) < 0) {
+		err = errno;
+		tester_warn("Failed to listen on socket: %d (%s)", err,
+								strerror(err));
+		close(sk);
+		return false;
+	}
+
+	/* Start Android Bluetooth daemon service */
+	if (property_set("ctl.start", SERVICE_NAME) < 0) {
+		tester_warn("Failed to start service %s", SERVICE_NAME);
+		close(sk);
+		return false;
+	}
+
+	cmd_sk = accept_connection(sk);
+	if (cmd_sk < 0) {
+		close(sk);
+		return false;
+	}
+
+	notif_sk = accept_connection(sk);
+	if (notif_sk < 0) {
+		close(sk);
+		close(cmd_sk);
+		cmd_sk = -1;
+		return false;
+	}
+
+	tester_print("bluetoothd connected");
+
+	close(sk);
+
+	return true;
+}
+
+static void cleanup_ipc(void)
+{
+	if (cmd_sk < 0)
+		return;
+
+	close(cmd_sk);
+	cmd_sk = -1;
+}
+
 static void setup(const void *data)
 {
 	struct test_data *test_data = tester_get_data();
@@ -285,6 +401,12 @@ static void setup(const void *data)
 		goto failed;
 	}
 
+	if (!init_ipc()) {
+		tester_warn("Cannot initialize IPC mechanism!");
+		goto failed;
+	}
+	/* TODO: register modules */
+
 	return;
 
 failed:
@@ -292,10 +414,13 @@ failed:
 	test_post_teardown(data);
 }
 
+
 static void teardown(const void *data)
 {
 	struct test_data *test_data = tester_get_data();
 
+	cleanup_ipc();
+
 	if (test_data->bluetoothd_pid)
 		waitpid(test_data->bluetoothd_pid, NULL, 0);
 
-- 
1.8.5.2


  parent reply	other threads:[~2014-01-20  9:36 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-20  9:35 [PATCHv5 00/11] IPC negative tester Jakub Tyszkowski
2014-01-20  9:36 ` [PATCHv5 01/11] android/ipc-tester: Skeleton for ipc " Jakub Tyszkowski
2014-01-20  9:36 ` [PATCHv5 02/11] android/ipc-tester: Run daemon in separate process Jakub Tyszkowski
2014-01-20  9:36 ` Jakub Tyszkowski [this message]
2014-01-20  9:36 ` [PATCHv5 04/11] android/ipc-tester: Add daemon shutdown handler Jakub Tyszkowski
2014-01-20  9:36 ` [PATCHv5 05/11] android/ipc-tester: Add sending test data with ipc Jakub Tyszkowski
2014-01-20  9:36 ` [PATCHv5 06/11] android/ipc-tester: Register services Jakub Tyszkowski
2014-01-20  9:36 ` [PATCHv5 07/11] android/ipc-tester: Add basic test cases for IPC's daemon site Jakub Tyszkowski
2014-01-20  9:36 ` [PATCHv5 08/11] android/ipc-tester: Add more cases for malformed data Jakub Tyszkowski
2014-01-20  9:36 ` [PATCHv5 09/11] android/ipc-tester: Add cases for service opcode boundaries Jakub Tyszkowski
2014-01-20  9:36 ` [PATCHv5 10/11] android/ipc-tester: Add cases for Core message data size Jakub Tyszkowski
2014-01-20  9:36 ` [PATCHv5 11/11] android/ipc-tester: Add cases for BT " Jakub Tyszkowski
2014-01-20 22:26 ` [PATCHv5 00/11] IPC negative tester Szymon Janc

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1390210570-28260-4-git-send-email-jakub.tyszkowski@tieto.com \
    --to=jakub.tyszkowski@tieto.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.