From fe6d3cf2933661fc618ed341e927edd9b255b21d Mon Sep 17 00:00:00 2001 From: Anupam Roy Date: Thu, 14 Jun 2018 12:19:59 +0530 Subject: [PATCH] emulator: sync with upstream MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This patch comes from below commits of upstream ===================================================================== commit 98a77944e6d3d5706647bfa7235c175b32173436 Author: ERAMOTO Masaya Date: Mon Apr 23 18:00:37 2018 +0900 emulator/b1ee: Enable to specify host and port Removes the DEFAULT_SERVER macro because the default host seems to be unofficial since 2017. commit b54111d2a432cdbd4926cce8be5b119b298e8477 Author: Łukasz Rymanowski Date: Fri Feb 9 18:26:25 2018 +0100 emulator: Add initial LE states to btdev and API to set new one ===================================================================== Change-Id: I9f94927c0b6f6dcf712ca137548691dfda53783a Signed-off-by: Anupam Roy --- emulator/b1ee.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++--- emulator/btdev.c | 18 ++++++++++ emulator/btdev.h | 3 +- emulator/hciemu.c | 8 +++++ emulator/hciemu.h | 3 ++ 5 files changed, 126 insertions(+), 6 deletions(-) diff --git a/emulator/b1ee.c b/emulator/b1ee.c index 1fe4684..1253a3d 100755 --- a/emulator/b1ee.c +++ b/emulator/b1ee.c @@ -32,6 +32,8 @@ #include #include #include +#include +#include #include #include @@ -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] \n"); + printf("options:\n" + "\t-p, --port Specify the server port\n" + "\t-s, --sniffer-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; } diff --git a/emulator/btdev.c b/emulator/btdev.c index a9b225a..69d84a5 100755 --- a/emulator/btdev.c +++ b/emulator/btdev.c @@ -570,6 +570,17 @@ static void set_le_features(struct btdev *btdev) btdev->le_features[0] |= 0x08; /* Slave-initiated Features Exchange */ } +static void set_le_states(struct btdev *btdev) +{ + /* Set all 41 bits as per Bluetooth 5.0 specification */ + btdev->le_states[0] = 0xff; + btdev->le_states[1] = 0xff; + btdev->le_states[2] = 0xff; + btdev->le_states[3] = 0xff; + btdev->le_states[4] = 0xff; + btdev->le_states[5] = 0x03; +} + static void set_amp_features(struct btdev *btdev) { } @@ -603,6 +614,7 @@ struct btdev *btdev_create(enum btdev_type type, uint16_t id) btdev->version = 0x09; set_bredrle_features(btdev); set_bredrle_commands(btdev); + set_le_states(btdev); break; case BTDEV_TYPE_BREDR: btdev->version = 0x05; @@ -613,6 +625,7 @@ struct btdev *btdev_create(enum btdev_type type, uint16_t id) btdev->version = 0x09; set_le_features(btdev); set_le_commands(btdev); + set_le_states(btdev); break; case BTDEV_TYPE_AMP: btdev->version = 0x01; @@ -685,6 +698,11 @@ uint8_t btdev_get_le_scan_enable(struct btdev *btdev) return btdev->le_scan_enable; } +void btdev_set_le_states(struct btdev *btdev, const uint8_t *le_states) +{ + memcpy(btdev->le_states, le_states, sizeof(btdev->le_states)); +} + static bool use_ssp(struct btdev *btdev1, struct btdev *btdev2) { if (btdev1->auth_enable || btdev2->auth_enable) diff --git a/emulator/btdev.h b/emulator/btdev.h index 40c7219..ba06a10 100755 --- a/emulator/btdev.h +++ b/emulator/btdev.h @@ -84,12 +84,13 @@ uint8_t btdev_get_scan_enable(struct btdev *btdev); uint8_t btdev_get_le_scan_enable(struct btdev *btdev); +void btdev_set_le_states(struct btdev *btdev, const uint8_t *le_states); + void btdev_set_command_handler(struct btdev *btdev, btdev_command_func handler, void *user_data); void btdev_set_send_handler(struct btdev *btdev, btdev_send_func handler, void *user_data); - void btdev_receive_h4(struct btdev *btdev, const void *data, uint16_t len); int btdev_add_hook(struct btdev *btdev, enum btdev_hook_type type, diff --git a/emulator/hciemu.c b/emulator/hciemu.c index 7debb8f..1787a6c 100755 --- a/emulator/hciemu.c +++ b/emulator/hciemu.c @@ -444,6 +444,14 @@ uint8_t hciemu_get_master_le_scan_enable(struct hciemu *hciemu) return btdev_get_le_scan_enable(hciemu->master_dev); } +void hciemu_set_master_le_states(struct hciemu *hciemu, const uint8_t *le_states) +{ + if (!hciemu || !hciemu->master_dev) + return; + + btdev_set_le_states(hciemu->master_dev, le_states); +} + bool hciemu_add_master_post_command_hook(struct hciemu *hciemu, hciemu_command_func_t function, void *user_data) { diff --git a/emulator/hciemu.h b/emulator/hciemu.h index 783f99c..5c0c4c3 100755 --- a/emulator/hciemu.h +++ b/emulator/hciemu.h @@ -57,6 +57,9 @@ uint8_t hciemu_get_master_scan_enable(struct hciemu *hciemu); uint8_t hciemu_get_master_le_scan_enable(struct hciemu *hciemu); +void hciemu_set_master_le_states(struct hciemu *hciemu, + const uint8_t *le_states); + typedef void (*hciemu_command_func_t)(uint16_t opcode, const void *data, uint8_t len, void *user_data); -- 2.7.4