From: "Frédéric DALLEAU" <frederic.dalleau@palmsource.com>
To: BlueZ development <bluez-devel@lists.sourceforge.net>
Subject: Re: [Bluez-devel] rfcomm utility patch to loop and startup executable when connection received
Date: Thu, 10 Aug 2006 15:36:18 +0200 [thread overview]
Message-ID: <44DB3652.9030603@palmsource.com> (raw)
In-Reply-To: <1155160247.4492.3.camel@aeonflux.holtmann.net>
[-- Attachment #1: Type: text/plain, Size: 1418 bytes --]
Hi Marcel,
>>I came accross the need to startup an executable when rfcomm receives a
>>connection. I also added an option called watch that allows rfcomm not
>>to exit when the rfcomm connection is closed. It simply loops and calls
>>listen.
>>
>>listen <dev> [channel [cmd]] Listen
>>watch <dev> [channel [cmd]] Watch
>>
>>rfcomm --raw watch 0 1 "cat {}"
>>rfcomm --raw listen 0 1 "cat {}"
>>
>>Not specifying the cmd parameter will simply wait for [control-c] as before.
>>
>>the {} will be replaced by the address of the rfcomm device created. In
>>the example the command run will be 'cat /dev/rfcomm0'.
>>
>>
The syntax changed a little :
rfcomm --raw watch 0 1 cat {}
rfcomm --raw listen 0 1 cat {}
Note that "" must no longer be added arround the command line. The channel must be present as it is the number of arguments that is used to determine the start of the cmdline.
>
>the feature looks useful, but the patch is not acceptable. You need to
>fix the coding style.
>
Attached patch has been modified around what I've found to be coding
style. It's ways cleaner than the previous one. However, if it does not
suit you, please let me know with the maximum details.
> What is the difference between listen and watch?
>
>
Watch will not quit when client disconnect. Instead, it will start
listening again.
Thanks
Frédéric
[-- Attachment #2: patch_rfcomm --]
[-- Type: text/plain, Size: 2939 bytes --]
Index: main.c
===================================================================
RCS file: /cvsroot/bluez/utils/rfcomm/main.c,v
retrieving revision 1.19
diff -u -r1.19 main.c
--- main.c 6 Jul 2006 09:31:03 -0000 1.19
+++ main.c 10 Aug 2006 13:07:51 -0000
@@ -39,6 +39,7 @@
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
+#include <sys/wait.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
@@ -252,6 +253,52 @@
return 0;
}
+static void run_cmdline(struct pollfd *p, char *devname, int argc, char ** argv)
+{
+ int i = 0;
+ pid_t pid, child;
+ int status = 0;
+ char** cmdargv = malloc((argc+1)*sizeof(char*));
+
+ if(!cmdargv)
+ return;
+
+ for(i=0; i<argc; i++) {
+ cmdargv[i] = (strcmp(argv[i], "{}")==0)?devname:argv[i];
+ }
+ cmdargv[i] = NULL;
+
+ pid = fork();
+
+ switch(pid) {
+ case 0:
+ i = execvp(cmdargv[0], cmdargv);
+ fprintf(stderr, "Couldn't execute command %s (errno=%d:%s)\n", cmdargv[0], errno, strerror(errno));
+ break;
+ case -1:
+ fprintf(stderr, "Couldn't fork to execute command %s\n", cmdargv[0]);
+ break;
+ default:
+ while (1) {
+ child = waitpid(-1, &status, WNOHANG);
+ if (child == pid || (child < 0 && errno != EAGAIN))
+ break;
+
+ p->revents = 0;
+ if (poll(p, 1, 200) || __io_canceled) {
+ kill(pid, SIGTERM);
+ waitpid(pid, &status, 0);
+ break;
+ }
+
+ usleep(10*1000);
+ }
+ break;
+ }
+
+ free(cmdargv);
+}
+
static void cmd_connect(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv)
{
struct sockaddr_rc laddr, raddr;
@@ -499,10 +546,14 @@
p.fd = fd;
p.events = POLLERR | POLLHUP;
- while (!__io_canceled) {
- p.revents = 0;
- if (poll(&p, 1, 100))
- break;
+ if(argc <= 2) {
+ while (!__io_canceled) {
+ p.revents = 0;
+ if (poll(&p, 1, 100))
+ break;
+ }
+ } else {
+ run_cmdline(&p, devname, argc-2, argv+2);
}
printf("Disconnected\n");
@@ -510,6 +561,20 @@
close(fd);
}
+static void cmd_watch(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv)
+{
+ struct sigaction sa;
+
+ sa.sa_handler = sig_term;
+ sigaction(SIGTERM, &sa, NULL);
+ sigaction(SIGINT, &sa, NULL);
+
+ while(!__io_canceled) {
+ cmd_listen(ctl, dev, bdaddr, argc, argv);
+ usleep(10*1000);
+ }
+}
+
static void cmd_create(int ctl, int dev, bdaddr_t *bdaddr, int argc, char **argv)
{
if (strcmp(argv[0], "all") == 0)
@@ -552,7 +617,8 @@
{ "release", "unbind", cmd_release, "<dev>", "Release device" },
{ "show", "info", cmd_show, "<dev>", "Show device" },
{ "connect", "conn", cmd_connect, "<dev> <bdaddr> [channel]", "Connect device" },
- { "listen", "server", cmd_listen, "<dev> [channel]", "Listen" },
+ { "listen", "server", cmd_listen, "<dev> [channel [cmd]]", "Listen" },
+ { "watch", "watch", cmd_watch, "<dev> [channel [cmd]]", "Watch" },
{ NULL, NULL, NULL, 0, 0 }
};
[-- Attachment #3: Type: text/plain, Size: 373 bytes --]
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
[-- Attachment #4: Type: text/plain, Size: 164 bytes --]
_______________________________________________
Bluez-devel mailing list
Bluez-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bluez-devel
next prev parent reply other threads:[~2006-08-10 13:36 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-08-09 10:19 [Bluez-devel] rfcomm utility patch to loop and startup executable when connection received Frédéric DALLEAU
2006-08-09 21:50 ` Marcel Holtmann
2006-08-10 13:36 ` Frédéric DALLEAU [this message]
2006-08-10 15:17 ` [Bluez-devel] rfcomm utility patch to loop and startup executable when connection received [REPOST] Frédéric DALLEAU
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=44DB3652.9030603@palmsource.com \
--to=frederic.dalleau@palmsource.com \
--cc=bluez-devel@lists.sourceforge.net \
/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.