All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH BlueZ v2] emulator/b1ee: Enable to specify host and port
@ 2018-04-23  9:00 ERAMOTO Masaya
  2018-04-26 12:57 ` Luiz Augusto von Dentz
  0 siblings, 1 reply; 2+ messages in thread
From: ERAMOTO Masaya @ 2018-04-23  9:00 UTC (permalink / raw)
  To: linux-bluetooth

Removes the DEFAULT_SERVER macro because the default host seems to be
unofficial since 2017.
---
 emulator/b1ee.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 95 insertions(+), 5 deletions(-)

diff --git a/emulator/b1ee.c b/emulator/b1ee.c
index 1fe46840f..1253a3dca 100644
--- a/emulator/b1ee.c
+++ b/emulator/b1ee.c
@@ -32,6 +32,8 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
+#include <getopt.h>
+#include <ctype.h>
 
 #include <netdb.h>
 #include <arpa/inet.h>
@@ -41,7 +43,6 @@
 
 #include "src/shared/mainloop.h"
 
-#define DEFAULT_SERVER		"b1ee.com"
 #define DEFAULT_HOST_PORT	"45550"		/* 0xb1ee */
 #define DEFAULT_SNIFFER_PORT	"45551"		/* 0xb1ef */
 
@@ -49,6 +50,43 @@ static int sniffer_fd;
 static int server_fd;
 static int vhci_fd;
 
+static void usage(void)
+{
+	printf("b1ee - Bluetooth device testing tool over internet\n"
+		"Usage:\n");
+	printf("\tb1ee [options] <host>\n");
+	printf("options:\n"
+		"\t-p, --port <port>          Specify the server port\n"
+		"\t-s, --sniffer-port <port>  Specify the sniffer port\n"
+		"\t-v, --version              Show version information\n"
+		"\t-h, --help                 Show help options\n");
+}
+
+static const struct option main_options[] = {
+	{ "port",		required_argument,	NULL, 'p' },
+	{ "sniffer-port",	required_argument,	NULL, 's' },
+	{ "version",		no_argument,		NULL, 'v' },
+	{ "help",		no_argument,		NULL, 'h' },
+	{ }
+};
+
+static char *set_port(char *str)
+{
+	char *c;
+
+	if (str == NULL || str[0] == '\0')
+		return NULL;
+
+	for (c = str; *c != '\0'; c++)
+		if (isdigit(*c) == 0)
+			return NULL;
+
+	if (atol(str) > 65535)
+		return NULL;
+
+	return strdup(str);
+}
+
 static void sniffer_read_callback(int fd, uint32_t events, void *user_data)
 {
 	static uint8_t buf[4096];
@@ -182,7 +220,7 @@ static int do_connect(const char *node, const char *service)
 	hints.ai_family = PF_UNSPEC;
 	hints.ai_socktype = SOCK_STREAM;
 
-	err = getaddrinfo(DEFAULT_SERVER, DEFAULT_HOST_PORT, &hints, &res);
+	err = getaddrinfo(node, service, &hints, &res);
 	if (err) {
 		perror(gai_strerror(err));
 		exit(1);
@@ -224,11 +262,53 @@ int main(int argc, char *argv[])
 {
 	const char sniff_cmd[] = { 0x01, 0x00,
 					0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
+	char *server_port = NULL, *sniffer_port = NULL;
+	int ret = EXIT_FAILURE;
 	ssize_t written;
 	sigset_t mask;
 
-	server_fd = do_connect(DEFAULT_SERVER, DEFAULT_HOST_PORT);
-	sniffer_fd = do_connect(DEFAULT_SERVER, DEFAULT_SNIFFER_PORT);
+	for (;;) {
+		int opt;
+
+		opt = getopt_long(argc, argv, "s:p:vh", main_options, NULL);
+		if (opt < 0)
+			break;
+
+		switch (opt) {
+		case 'p':
+			server_port = set_port(optarg);
+			if (server_port == NULL)
+				goto usage;
+
+			break;
+		case 's':
+			sniffer_port = set_port(optarg);
+			if (sniffer_port == NULL)
+				goto usage;
+
+			break;
+		case 'v':
+			printf("%s\n", VERSION);
+			ret = EXIT_SUCCESS;
+			goto done;
+		case 'h':
+			ret = EXIT_SUCCESS;
+			goto usage;
+		default:
+			goto usage;
+		}
+	}
+
+	argc = argc - optind;
+	argv = argv + optind;
+	optind = 0;
+
+	if (argv[0] == NULL || argv[0][0] == '\0')
+		goto usage;
+
+	server_fd = do_connect(argv[0], server_port ? : DEFAULT_HOST_PORT);
+	sniffer_fd = do_connect(argv[0],
+				sniffer_port ? : DEFAULT_SNIFFER_PORT);
 
 	written = write(sniffer_fd, sniff_cmd, sizeof(sniff_cmd));
 	if (written < 0)
@@ -253,5 +333,15 @@ int main(int argc, char *argv[])
 	mainloop_add_fd(server_fd, EPOLLIN, server_read_callback, NULL, NULL);
 	mainloop_add_fd(vhci_fd, EPOLLIN, vhci_read_callback, NULL, NULL);
 
-	return mainloop_run();
+	ret = mainloop_run();
+
+	goto done;
+
+usage:
+	usage();
+done:
+	free(server_port);
+	free(sniffer_port);
+
+	return ret;
 }
-- 
2.14.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH BlueZ v2] emulator/b1ee: Enable to specify host and port
  2018-04-23  9:00 [PATCH BlueZ v2] emulator/b1ee: Enable to specify host and port ERAMOTO Masaya
@ 2018-04-26 12:57 ` Luiz Augusto von Dentz
  0 siblings, 0 replies; 2+ messages in thread
From: Luiz Augusto von Dentz @ 2018-04-26 12:57 UTC (permalink / raw)
  To: ERAMOTO Masaya; +Cc: linux-bluetooth

Hi Eramoto,

On Mon, Apr 23, 2018 at 12:00 PM, ERAMOTO Masaya
<eramoto.masaya@jp.fujitsu.com> wrote:
> Removes the DEFAULT_SERVER macro because the default host seems to be
> unofficial since 2017.
> ---
>  emulator/b1ee.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
>  1 file changed, 95 insertions(+), 5 deletions(-)
>
> diff --git a/emulator/b1ee.c b/emulator/b1ee.c
> index 1fe46840f..1253a3dca 100644
> --- a/emulator/b1ee.c
> +++ b/emulator/b1ee.c
> @@ -32,6 +32,8 @@
>  #include <unistd.h>
>  #include <stdlib.h>
>  #include <string.h>
> +#include <getopt.h>
> +#include <ctype.h>
>
>  #include <netdb.h>
>  #include <arpa/inet.h>
> @@ -41,7 +43,6 @@
>
>  #include "src/shared/mainloop.h"
>
> -#define DEFAULT_SERVER         "b1ee.com"
>  #define DEFAULT_HOST_PORT      "45550"         /* 0xb1ee */
>  #define DEFAULT_SNIFFER_PORT   "45551"         /* 0xb1ef */
>
> @@ -49,6 +50,43 @@ static int sniffer_fd;
>  static int server_fd;
>  static int vhci_fd;
>
> +static void usage(void)
> +{
> +       printf("b1ee - Bluetooth device testing tool over internet\n"
> +               "Usage:\n");
> +       printf("\tb1ee [options] <host>\n");
> +       printf("options:\n"
> +               "\t-p, --port <port>          Specify the server port\n"
> +               "\t-s, --sniffer-port <port>  Specify the sniffer port\n"
> +               "\t-v, --version              Show version information\n"
> +               "\t-h, --help                 Show help options\n");
> +}
> +
> +static const struct option main_options[] = {
> +       { "port",               required_argument,      NULL, 'p' },
> +       { "sniffer-port",       required_argument,      NULL, 's' },
> +       { "version",            no_argument,            NULL, 'v' },
> +       { "help",               no_argument,            NULL, 'h' },
> +       { }
> +};
> +
> +static char *set_port(char *str)
> +{
> +       char *c;
> +
> +       if (str == NULL || str[0] == '\0')
> +               return NULL;
> +
> +       for (c = str; *c != '\0'; c++)
> +               if (isdigit(*c) == 0)
> +                       return NULL;
> +
> +       if (atol(str) > 65535)
> +               return NULL;
> +
> +       return strdup(str);
> +}
> +
>  static void sniffer_read_callback(int fd, uint32_t events, void *user_data)
>  {
>         static uint8_t buf[4096];
> @@ -182,7 +220,7 @@ static int do_connect(const char *node, const char *service)
>         hints.ai_family = PF_UNSPEC;
>         hints.ai_socktype = SOCK_STREAM;
>
> -       err = getaddrinfo(DEFAULT_SERVER, DEFAULT_HOST_PORT, &hints, &res);
> +       err = getaddrinfo(node, service, &hints, &res);
>         if (err) {
>                 perror(gai_strerror(err));
>                 exit(1);
> @@ -224,11 +262,53 @@ int main(int argc, char *argv[])
>  {
>         const char sniff_cmd[] = { 0x01, 0x00,
>                                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
> +       char *server_port = NULL, *sniffer_port = NULL;
> +       int ret = EXIT_FAILURE;
>         ssize_t written;
>         sigset_t mask;
>
> -       server_fd = do_connect(DEFAULT_SERVER, DEFAULT_HOST_PORT);
> -       sniffer_fd = do_connect(DEFAULT_SERVER, DEFAULT_SNIFFER_PORT);
> +       for (;;) {
> +               int opt;
> +
> +               opt = getopt_long(argc, argv, "s:p:vh", main_options, NULL);
> +               if (opt < 0)
> +                       break;
> +
> +               switch (opt) {
> +               case 'p':
> +                       server_port = set_port(optarg);
> +                       if (server_port == NULL)
> +                               goto usage;
> +
> +                       break;
> +               case 's':
> +                       sniffer_port = set_port(optarg);
> +                       if (sniffer_port == NULL)
> +                               goto usage;
> +
> +                       break;
> +               case 'v':
> +                       printf("%s\n", VERSION);
> +                       ret = EXIT_SUCCESS;
> +                       goto done;
> +               case 'h':
> +                       ret = EXIT_SUCCESS;
> +                       goto usage;
> +               default:
> +                       goto usage;
> +               }
> +       }
> +
> +       argc = argc - optind;
> +       argv = argv + optind;
> +       optind = 0;
> +
> +       if (argv[0] == NULL || argv[0][0] == '\0')
> +               goto usage;
> +
> +       server_fd = do_connect(argv[0], server_port ? : DEFAULT_HOST_PORT);
> +       sniffer_fd = do_connect(argv[0],
> +                               sniffer_port ? : DEFAULT_SNIFFER_PORT);
>
>         written = write(sniffer_fd, sniff_cmd, sizeof(sniff_cmd));
>         if (written < 0)
> @@ -253,5 +333,15 @@ int main(int argc, char *argv[])
>         mainloop_add_fd(server_fd, EPOLLIN, server_read_callback, NULL, NULL);
>         mainloop_add_fd(vhci_fd, EPOLLIN, vhci_read_callback, NULL, NULL);
>
> -       return mainloop_run();
> +       ret = mainloop_run();
> +
> +       goto done;
> +
> +usage:
> +       usage();
> +done:
> +       free(server_port);
> +       free(sniffer_port);
> +
> +       return ret;
>  }
> --
> 2.14.1

Applied, thanks.

-- 
Luiz Augusto von Dentz

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-04-26 12:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-23  9:00 [PATCH BlueZ v2] emulator/b1ee: Enable to specify host and port ERAMOTO Masaya
2018-04-26 12:57 ` Luiz Augusto von Dentz

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.