From 6c14b97d703b74150d48ffdd47396d65b35dd0da Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Thu, 19 Apr 2018 13:02:20 -0600 Subject: [PATCH] apps/wireless/bluetooth/btsak: Implements GATT discover commands. --- wireless/bluetooth/btsak/btsak.h | 4 +- wireless/bluetooth/btsak/btsak_gatt.c | 116 ++++++++++++++++++++++++-- wireless/bluetooth/btsak/btsak_main.c | 12 +-- 3 files changed, 119 insertions(+), 13 deletions(-) diff --git a/wireless/bluetooth/btsak/btsak.h b/wireless/bluetooth/btsak/btsak.h index e3ee4a8a1..0f8519ce0 100644 --- a/wireless/bluetooth/btsak/btsak.h +++ b/wireless/bluetooth/btsak/btsak.h @@ -141,14 +141,14 @@ long btsak_str2long(FAR const char *str); uint8_t btsak_str2luint8(FAR const char *str); /**************************************************************************** - * Name: btsak_str2luint16 + * Name: btsak_str2uint16 * * Description: * Convert a string to an integer value * ****************************************************************************/ -uint16_t btsak_str2luint16(FAR const char *str); +uint16_t btsak_str2uint16(FAR const char *str); /**************************************************************************** * Name: btsak_str2bool diff --git a/wireless/bluetooth/btsak/btsak_gatt.c b/wireless/bluetooth/btsak/btsak_gatt.c index 324b8a11f..b62e49e1c 100644 --- a/wireless/bluetooth/btsak/btsak_gatt.c +++ b/wireless/bluetooth/btsak/btsak_gatt.c @@ -42,12 +42,117 @@ * Included Files ****************************************************************************/ +#include #include +#include +#include #include +#include +#include #include "btsak.h" +/**************************************************************************** + * Private functions + ****************************************************************************/ + +/**************************************************************************** + * Name: btsak_cmd_discover_common + * + * Description: + * gatt [-h] [-h] [] + * + ****************************************************************************/ + +static void btsak_cmd_discover_common(FAR struct btsak_s *btsak, + int argc, FAR char *argv[], + enum bt_gatt_discover_e type) +{ + struct btreq_s btreq; + unsigned int argndx; + int sockfd; + int ret; + + /* Check for help command */ + + if (argc == 2 && strcmp(argv[1], "-h") == 0) + { + btsak_gatt_showusage(btsak->progname, argv[0], EXIT_SUCCESS); + } + + argndx = (type == GATT_DISCOVER) ? 4 : 3; + if (argc < argndx) + { + fprintf(stderr, + "ERROR: Invalid number of arguments. Found %d expected at least %u\n", + argc, argndx); + btsak_gatt_showusage(btsak->progname, argv[0], EXIT_FAILURE); + } + + strncpy(btreq.btr_name, btsak->ifname, HCI_DEVNAME_SIZE); + btreq.btr_dtype = (uint8_t)type; + + ret = btsak_str2addr(argv[1], btreq.btr_dpeer.val); + if (ret < 0) + { + fprintf(stderr, "ERROR: Bad value for : %s\n", argv[1]); + btsak_gatt_showusage(btsak->progname, argv[0], EXIT_FAILURE); + } + + ret = btsak_str2addrtype(argv[2], &btreq.btr_dpeer.type); + { + fprintf(stderr, "ERROR: Bad value for : %s\n", argv[2]); + btsak_gatt_showusage(btsak->progname, argv[0], EXIT_FAILURE); + } + + if (type == GATT_DISCOVER) + { + btreq.btr_duuid16 = btsak_str2uint16(argv[3]); + } + else + { + btreq.btr_duuid16 = 0; + } + + btreq.btr_dstart = 0x0001; + btreq.btr_dend = 0xffff; + + if (argc > argndx) + { + btreq.btr_dstart = btsak_str2uint16(argv[argndx]); + argndx++; + + if (argc > argndx) + { + btreq.btr_dend = btsak_str2uint16(argv[argndx]); + argndx++; + } + + if (btreq.btr_dstart > btreq.btr_dend) + { + fprintf(stderr, "ERROR: Invalid handle range: %u-%u\n", + btreq.btr_dstart, btreq.btr_dend); + btsak_gatt_showusage(btsak->progname, argv[0], EXIT_FAILURE); + } + } + + /* Perform the IOCTL to start the discovery */ + + sockfd = btsak_socket(btsak); + if (sockfd >= 0) + { + ret = ioctl(sockfd, SIOCBTDISCOVER, (unsigned long)((uintptr_t)&btreq)); + if (ret < 0) + { + fprintf(stderr, "ERROR: ioctl(SIOCBTDISCOVER) failed: %d\n", + errno); + } + } + + close(sockfd); +} + /**************************************************************************** * Public functions ****************************************************************************/ @@ -70,13 +175,13 @@ void btsak_cmd_gatt_exchange_mtu(FAR struct btsak_s *btsak, int argc, * Name: btsak_cmd_discover * * Description: - * gatt [-h] discover [-h] command + * gatt [-h] discover [-h] command * ****************************************************************************/ void btsak_cmd_discover(FAR struct btsak_s *btsak, int argc, FAR char *argv[]) { -# warning Missing logic + btsak_cmd_discover_common(btsak, argc, argv, GATT_DISCOVER); } /**************************************************************************** @@ -90,7 +195,7 @@ void btsak_cmd_discover(FAR struct btsak_s *btsak, int argc, FAR char *argv[]) void btsak_cmd_gatt_discover_characteristic(FAR struct btsak_s *btsak, int argc, FAR char *argv[]) { -# warning Missing logic + btsak_cmd_discover_common(btsak, argc, argv, GATT_DISCOVER_CHAR); } /**************************************************************************** @@ -101,9 +206,10 @@ void btsak_cmd_gatt_discover_characteristic(FAR struct btsak_s *btsak, * ****************************************************************************/ -void btsak_cmd_gat_discover_descriptor(FAR struct btsak_s *btsak, int argc, FAR char *argv[]) +void btsak_cmd_gat_discover_descriptor(FAR struct btsak_s *btsak, + int argc, FAR char *argv[]) { -# warning Missing logic + btsak_cmd_discover_common(btsak, argc, argv, GATT_DISCOVER_DESC); } /**************************************************************************** diff --git a/wireless/bluetooth/btsak/btsak_main.c b/wireless/bluetooth/btsak/btsak_main.c index d2f726e13..d68e60413 100644 --- a/wireless/bluetooth/btsak/btsak_main.c +++ b/wireless/bluetooth/btsak/btsak_main.c @@ -125,17 +125,17 @@ static const struct btsak_command_s g_btsak_gatt_commands[] = { "discover", (CODE void *)btsak_cmd_discover, - "[-h] " + "[-h] [ []]" }, { "characteristic", (CODE void *)btsak_cmd_gatt_discover_characteristic, - "[-h] " + "[-h] [ []]" }, { "descriptor", (CODE void *)btsak_cmd_gat_discover_descriptor, - "[-h] " + "[-h] [ []]" }, { "read", @@ -340,7 +340,7 @@ long btsak_str2long(FAR const char *str) long value; value = strtol(str, &endptr, 0); - if (*endptr != '\0') + if (endptr == NULL || *endptr != '\0') { fprintf(stderr, "ERROR: Garbage after numeric argument\n"); exit(EXIT_FAILURE); @@ -377,14 +377,14 @@ uint8_t btsak_str2luint8(FAR const char *str) } /**************************************************************************** - * Name: btsak_str2luint16 + * Name: btsak_str2uint16 * * Description: * Convert a string to an integer value * ****************************************************************************/ -uint16_t btsak_str2luint16(FAR const char *str) +uint16_t btsak_str2uint16(FAR const char *str) { long value = btsak_str2long(str); if (value < 0 || value > UINT16_MAX)