Linux bluetooth development
 help / color / mirror / Atom feed
From: Gustavo Padovan <gustavo@padovan.org>
To: linux-bluetooth@vger.kernel.org
Cc: Gustavo Padovan <gustavo.padovan@collabora.co.uk>
Subject: [PATCH 2/2] rctest: add option to save received data to file
Date: Thu, 30 Aug 2012 11:32:28 -0700	[thread overview]
Message-ID: <1346351548-5056-2-git-send-email-gustavo@padovan.org> (raw)
In-Reply-To: <1346351548-5056-1-git-send-email-gustavo@padovan.org>

From: Gustavo Padovan <gustavo.padovan@collabora.co.uk>

works only for automated test option for now
---
 test/rctest.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 52 insertions(+), 3 deletions(-)

diff --git a/test/rctest.c b/test/rctest.c
index 0c645b7..f60c8f9 100644
--- a/test/rctest.c
+++ b/test/rctest.c
@@ -78,6 +78,8 @@ static uint16_t uuid = 0x0000;
 static uint8_t channel = 10;
 
 static char *filename = NULL;
+static char *savefile = NULL;
+static int save_fd = 0;
 
 static int master = 0;
 static int auth = 0;
@@ -448,6 +450,23 @@ static void dump_mode(int sk)
 		syslog(LOG_INFO, "Received %d bytes", len);
 }
 
+static void save_mode(int sk)
+{
+	int len, ret;
+	char *b;
+
+	b = malloc(data_size);
+	if (!b)
+		syslog(LOG_ERR, "Failed to open file to save recv data");
+
+	syslog(LOG_INFO, "Receiving ...");
+	while ((len = read(sk, b, data_size)) > 0) {
+		ret = write(save_fd, b, len);
+		if (ret < 0)
+			return;
+	}
+}
+
 static void recv_mode(int sk)
 {
 	struct timeval tv_beg, tv_end, tv_diff;
@@ -603,7 +622,20 @@ static void automated_send_recv()
 	char device[18];
 
 	if (fork()) {
-		do_listen(recv_mode);
+		if (!savefile) {
+			do_listen(recv_mode);
+			return;
+		}
+
+		save_fd = open(savefile, O_CREAT | O_WRONLY,
+						S_IRUSR | S_IWUSR);
+		if (!save_fd)
+			syslog(LOG_ERR, "Failed to open file to save "
+							"recv data");
+
+		do_listen(save_mode);
+
+		close(save_fd);
 	} else {
 		ba2str(&bdaddr, device);
 
@@ -614,6 +646,15 @@ static void automated_send_recv()
 	}
 }
 
+static void sig_child_exit(int code)
+{
+	syslog(LOG_INFO, "Exit");
+	exit(0);
+
+	if (save_fd)
+		close(save_fd);
+}
+
 static void usage(void)
 {
 	printf("rctest - RFCOMM testing\n"
@@ -635,6 +676,7 @@ static void usage(void)
 		"\t[-L seconds] enabled SO_LINGER option\n"
 		"\t[-W seconds] enable deferred setup\n"
 		"\t[-B filename] use data packets from file\n"
+		"\t[-O filename] save received data to file\n"
 		"\t[-N num] number of frames to send\n"
 		"\t[-C num] send num frames before delay (default = 1)\n"
 		"\t[-D milliseconds] delay after sending num frames (default = 0)\n"
@@ -654,7 +696,7 @@ int main(int argc, char *argv[])
 	bacpy(&bdaddr, BDADDR_ANY);
 	bacpy(&auto_bdaddr, BDADDR_ANY);
 
-	while ((opt=getopt(argc,argv,"rdscuwmna:b:i:P:U:B:N:MAESL:W:C:D:Y:T")) != EOF) {
+	while ((opt=getopt(argc,argv,"rdscuwmna:b:i:P:U:B:O:N:MAESL:W:C:D:Y:T")) != EOF) {
 		switch (opt) {
 		case 'r':
 			mode = RECV;
@@ -754,6 +796,10 @@ int main(int argc, char *argv[])
 			filename = strdup(optarg);
 			break;
 
+		case 'O':
+			savefile = strdup(optarg);
+			break;
+
 		case 'N':
 			num_frames = atoi(optarg);
 			break;
@@ -791,7 +837,10 @@ int main(int argc, char *argv[])
 	}
 
 	memset(&sa, 0, sizeof(sa));
-	sa.sa_handler = SIG_IGN;
+	if (mode == AUTO)
+		sa.sa_handler = sig_child_exit;
+	else
+		sa.sa_handler = SIG_IGN;
 	sa.sa_flags   = SA_NOCLDSTOP;
 	sigaction(SIGCHLD, &sa, NULL);
 
-- 
1.7.11.2


      reply	other threads:[~2012-08-30 18:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-30 18:32 [PATCH 1/2] rctest: add automated test Gustavo Padovan
2012-08-30 18:32 ` Gustavo Padovan [this message]

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=1346351548-5056-2-git-send-email-gustavo@padovan.org \
    --to=gustavo@padovan.org \
    --cc=gustavo.padovan@collabora.co.uk \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox