All of lore.kernel.org
 help / color / mirror / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: ERAMOTO Masaya <eramoto.masaya@jp.fujitsu.com>
Cc: "linux-bluetooth@vger.kernel.org" <linux-bluetooth@vger.kernel.org>
Subject: Re: [PATCH BlueZ v2] emulator/b1ee: Enable to specify host and port
Date: Thu, 26 Apr 2018 15:57:47 +0300	[thread overview]
Message-ID: <CABBYNZK-JX=T-0p0325mT2v5MRX4V4fPStkKfd3nObL_wF7PCA@mail.gmail.com> (raw)
In-Reply-To: <d2e6c913-7438-930c-1005-ffe352b555e7@jp.fujitsu.com>

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

      reply	other threads:[~2018-04-26 12:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 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='CABBYNZK-JX=T-0p0325mT2v5MRX4V4fPStkKfd3nObL_wF7PCA@mail.gmail.com' \
    --to=luiz.dentz@gmail.com \
    --cc=eramoto.masaya@jp.fujitsu.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.