diff --git a/wireless/bluetooth/btsak/btsak.h b/wireless/bluetooth/btsak/btsak.h index 03e20bf6e..587b32909 100644 --- a/wireless/bluetooth/btsak/btsak.h +++ b/wireless/bluetooth/btsak/btsak.h @@ -124,27 +124,27 @@ int btsak_char2nibble(char ch); * Name: btsak_str2long * * Description: - * Convert a hex string to an integer value + * Convert a numeric string to an long value * ****************************************************************************/ long btsak_str2long(FAR const char *str); /**************************************************************************** - * Name: btsak_str2luint8 + * Name: btsak_str2uint8 * * Description: - * Convert a string to an integer value + * Convert a numeric string to an uint8_t value * ****************************************************************************/ -uint8_t btsak_str2luint8(FAR const char *str); +uint8_t btsak_str2uint8(FAR const char *str); /**************************************************************************** * Name: btsak_str2uint16 * * Description: - * Convert a string to an integer value + * Convert a numeric string to an uint16_t value * ****************************************************************************/ diff --git a/wireless/bluetooth/btsak/btsak_advertise.c b/wireless/bluetooth/btsak/btsak_advertise.c index 42232748c..1e41fe074 100644 --- a/wireless/bluetooth/btsak/btsak_advertise.c +++ b/wireless/bluetooth/btsak/btsak_advertise.c @@ -139,7 +139,7 @@ static void btsak_cmd_advertisestart(FAR struct btsak_s *btsak, FAR char *cmd, strcpy((FAR char *)sd[1].data, "btsak"); memset(&btreq, 0, sizeof(struct btreq_s)); - strncpy(btreq.btr_name, btsak->ifname, HCI_DEVNAME_SIZE); + strncpy(btreq.btr_name, btsak->ifname, IFNAMSIZ); btreq.btr_advtype = BT_LE_ADV_IND; btreq.btr_advad = ad; btreq.btr_advsd = sd; @@ -179,7 +179,7 @@ static void btsak_cmd_advertisestop(FAR struct btsak_s *btsak, FAR char *cmd, /* Perform the IOCTL to stop advertising */ memset(&btreq, 0, sizeof(struct btreq_s)); - strncpy(btreq.btr_name, btsak->ifname, HCI_DEVNAME_SIZE); + strncpy(btreq.btr_name, btsak->ifname, IFNAMSIZ); sockfd = btsak_socket(btsak); if (sockfd >= 0) diff --git a/wireless/bluetooth/btsak/btsak_features.c b/wireless/bluetooth/btsak/btsak_features.c index 276722acd..de58f6a35 100644 --- a/wireless/bluetooth/btsak/btsak_features.c +++ b/wireless/bluetooth/btsak/btsak_features.c @@ -117,7 +117,7 @@ void btsak_cmd_features(FAR struct btsak_s *btsak, int argc, FAR char *argv[]) /* Perform the IOCTL to stop advertising */ memset(&btreq, 0, sizeof(struct btreq_s)); - strncpy(btreq.btr_name, btsak->ifname, HCI_DEVNAME_SIZE); + strncpy(btreq.btr_name, btsak->ifname, IFNAMSIZ); sockfd = btsak_socket(btsak); if (sockfd >= 0) diff --git a/wireless/bluetooth/btsak/btsak_gatt.c b/wireless/bluetooth/btsak/btsak_gatt.c index 33f318c36..9fa4b1455 100644 --- a/wireless/bluetooth/btsak/btsak_gatt.c +++ b/wireless/bluetooth/btsak/btsak_gatt.c @@ -53,6 +53,10 @@ #include "btsak.h" +/**************************************************************************** + * Private data + ****************************************************************************/ + /**************************************************************************** * Private functions ****************************************************************************/ @@ -90,7 +94,7 @@ static void btsak_cmd_discover_common(FAR struct btsak_s *btsak, btsak_gatt_showusage(btsak->progname, argv[0], EXIT_FAILURE); } - strncpy(btreq.btr_name, btsak->ifname, HCI_DEVNAME_SIZE); + strncpy(btreq.btr_name, btsak->ifname, IFNAMSIZ); btreq.btr_dtype = (uint8_t)type; ret = btsak_str2addr(argv[1], btreq.btr_dpeer.val); @@ -200,7 +204,7 @@ void btsak_cmd_gatt_exchange_mtu(FAR struct btsak_s *btsak, int argc, /* Perform the IOCTL to start the MTU exchange */ - strncpy(btreq.btr_name, btsak->ifname, HCI_DEVNAME_SIZE); + strncpy(btreq.btr_name, btsak->ifname, IFNAMSIZ); sockfd = btsak_socket(btsak); if (sockfd >= 0) @@ -237,7 +241,7 @@ void btsak_cmd_gatt_exchange_mtu_result(FAR struct btsak_s *btsak, int argc, /* Perform the IOCTL to recover the result of the MTU exchange */ - strncpy(btreq.btr_name, btsak->ifname, HCI_DEVNAME_SIZE); + strncpy(btreq.btr_name, btsak->ifname, IFNAMSIZ); sockfd = btsak_socket(btsak); if (sockfd >= 0) @@ -339,7 +343,7 @@ void btsak_cmd_gat_discover_get(FAR struct btsak_s *btsak, /* Perform the IOCTL to start the discovery */ - strncpy(btreq.btr_name, btsak->ifname, HCI_DEVNAME_SIZE); + strncpy(btreq.btr_name, btsak->ifname, IFNAMSIZ); btreq.btr_gnrsp = 8; btreq.btr_grsp = result; @@ -379,33 +383,214 @@ void btsak_cmd_gat_discover_get(FAR struct btsak_s *btsak, void btsak_cmd_gatt_read(FAR struct btsak_s *btsak, int argc, FAR char *argv[]) { -# warning Missing logic + struct btreq_s btreq; + int sockfd; + int ret; + + if (argc == 2 && strcmp(argv[1], "-h") == 0) + { + btsak_gatt_showusage(btsak->progname, argv[0], EXIT_SUCCESS); + } + + if (argc < 4 || argc > 5) + { + fprintf(stderr, + "ERROR: Invalid number of arguments. Found %d expected 3 or 4\n", + argc - 1); + btsak_gatt_showusage(btsak->progname, argv[0], EXIT_FAILURE); + } + + ret = btsak_str2addr(argv[1], btreq.btr_rdpeer.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_rdpeer.type); + { + fprintf(stderr, "ERROR: Bad value for address type: %s\n", argv[2]); + btsak_gatt_showusage(btsak->progname, argv[0], EXIT_FAILURE); + } + + btreq.btr_rdhandles[0] = btsak_str2uint16(argv[3]); + btreq.btr_rdnhandles = 1; + + btreq.btr_rdoffset = 0; + if (argc > 4) + { + btreq.btr_rdoffset = btsak_str2uint16(argv[4]); + } + + /* Perform the IOCTL to start the read */ + + strncpy(btreq.btr_name, btsak->ifname, IFNAMSIZ); + + sockfd = btsak_socket(btsak); + if (sockfd >= 0) + { + ret = ioctl(sockfd, SIOCBTGATTRD, (unsigned long)((uintptr_t)&btreq)); + if (ret < 0) + { + fprintf(stderr, "ERROR: ioctl(SIOCBTGATTRD) failed: %d\n", + errno); + } + else + { + printf("Read pending...\n"); + } + } + + close(sockfd); } /**************************************************************************** * Name: btsak_cmd_gatt_read_multiple * * Description: - * gatt [-h] read-multiple [-h] public|private command + * gatt [-h] read-multiple [-h] public|private [ []..] * ****************************************************************************/ void btsak_cmd_gatt_read_multiple(FAR struct btsak_s *btsak, int argc, FAR char *argv[]) { -# warning Missing logic + struct btreq_s btreq; + int sockfd; + int ret; + int i; + + if (argc == 2 && strcmp(argv[1], "-h") == 0) + { + btsak_gatt_showusage(btsak->progname, argv[0], EXIT_SUCCESS); + } + + if (argc < 4) + { + fprintf(stderr, + "ERROR: Invalid number of arguments. Found %d expected at least 3\n", + argc - 1); + btsak_gatt_showusage(btsak->progname, argv[0], EXIT_FAILURE); + } + + ret = btsak_str2addr(argv[1], btreq.btr_rdpeer.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_rdpeer.type); + { + fprintf(stderr, "ERROR: Bad value for address type: %s\n", argv[2]); + btsak_gatt_showusage(btsak->progname, argv[0], EXIT_FAILURE); + } + + btreq.btr_rdoffset = 0; + btreq.btr_rdnhandles = argc - 3; + + for (i = 0; i < btreq.btr_rdnhandles; i++) + { + btreq.btr_rdhandles[i] = btsak_str2uint16(argv[i + 3]); + } + + /* Perform the IOCTL to start the read */ + + strncpy(btreq.btr_name, btsak->ifname, IFNAMSIZ); + + sockfd = btsak_socket(btsak); + if (sockfd >= 0) + { + ret = ioctl(sockfd, SIOCBTGATTRD, (unsigned long)((uintptr_t)&btreq)); + if (ret < 0) + { + fprintf(stderr, "ERROR: ioctl(SIOCBTGATTRD) failed: %d\n", + errno); + } + else + { + printf("Read pending...\n"); + } + } + + close(sockfd); } /**************************************************************************** * Name: btsak_cmd_gatt_write * * Description: - * gatt [-h] write [-h] [-h] public|private command + * gatt [-h] write [-h] [-h] public|private [ []..] * ****************************************************************************/ void btsak_cmd_gatt_write(FAR struct btsak_s *btsak, int argc, FAR char *argv[]) { -# warning Missing logic + struct btreq_s btreq; + int sockfd; + int ret; + int i; + + if (argc == 2 && strcmp(argv[1], "-h") == 0) + { + btsak_gatt_showusage(btsak->progname, argv[0], EXIT_SUCCESS); + } + + if (argc < 5) + { + fprintf(stderr, + "ERROR: Invalid number of arguments. Found %d expected at least 4\n", + argc - 1); + btsak_gatt_showusage(btsak->progname, argv[0], EXIT_FAILURE); + } + + ret = btsak_str2addr(argv[1], btreq.btr_wrpeer.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_wrpeer.type); + { + fprintf(stderr, "ERROR: Bad value for address type: %s\n", argv[2]); + btsak_gatt_showusage(btsak->progname, argv[0], EXIT_FAILURE); + } + + btreq.btr_wrhandle = btsak_str2uint16(argv[3]); + btreq.btr_wrnbytes = argc - 3; + + if (btreq.btr_wrnbytes > HCI_GATTWR_DATA) + { + fprintf(stderr, "ERROR: Too much data. Limit is %u bytes%s\n", + HCI_GATTWR_DATA); + btsak_gatt_showusage(btsak->progname, argv[0], EXIT_FAILURE); + } + + for (i = 0; i < btreq.btr_wrnbytes; i++) + { + btreq.btr_wrdata[i] = btsak_str2uint8(argv[i + 3]); + } + + /* Perform the IOCTL to start the read */ + + strncpy(btreq.btr_name, btsak->ifname, IFNAMSIZ); + + sockfd = btsak_socket(btsak); + if (sockfd >= 0) + { + ret = ioctl(sockfd, SIOCBTGATTWR, (unsigned long)((uintptr_t)&btreq)); + if (ret < 0) + { + fprintf(stderr, "ERROR: ioctl(SIOCBTGATTWR) failed: %d\n", + errno); + } + else + { + printf("Write pending...\n"); + } + } + + close(sockfd); } diff --git a/wireless/bluetooth/btsak/btsak_info.c b/wireless/bluetooth/btsak/btsak_info.c index aded541c0..60caddb58 100644 --- a/wireless/bluetooth/btsak/btsak_info.c +++ b/wireless/bluetooth/btsak/btsak_info.c @@ -105,7 +105,7 @@ void btsak_cmd_info(FAR struct btsak_s *btsak, int argc, FAR char *argv[]) /* Perform the IOCTL to stop advertising */ memset(&btreq, 0, sizeof(struct btreq_s)); - strncpy(btreq.btr_name, btsak->ifname, HCI_DEVNAME_SIZE); + strncpy(btreq.btr_name, btsak->ifname, IFNAMSIZ); sockfd = btsak_socket(btsak); if (sockfd >= 0) diff --git a/wireless/bluetooth/btsak/btsak_main.c b/wireless/bluetooth/btsak/btsak_main.c index c8bea196a..6ed4435e7 100644 --- a/wireless/bluetooth/btsak/btsak_main.c +++ b/wireless/bluetooth/btsak/btsak_main.c @@ -155,12 +155,12 @@ static const struct btsak_command_s g_btsak_gatt_commands[] = { "read-multiple", (CODE void *)btsak_cmd_gatt_read_multiple, - "[-h] public|private " + "[-h] public|private [ []..]" }, { "write", (CODE void *)btsak_cmd_gatt_write, - "[-h] public|private " + "[-h] public|private [ []..]" } }; @@ -340,7 +340,7 @@ int btsak_char2nibble(char ch) * Name: btsak_str2long * * Description: - * Convert a hex string to an integer value + * Convert a numeric string to a long value * ****************************************************************************/ @@ -356,25 +356,38 @@ long btsak_str2long(FAR const char *str) exit(EXIT_FAILURE); } - if (value > INT_MAX || value < INT_MIN) - { - fprintf(stderr, "ERROR: Integer value out of range\n"); - return LONG_MAX; - exit(EXIT_FAILURE); - } - return value; } /**************************************************************************** - * Name: btsak_str2luint8 + * Name: btsak_str2int * * Description: - * Convert a string to an integer value + * Convert a numeric string to an integer value * ****************************************************************************/ -uint8_t btsak_str2luint8(FAR const char *str) +long btsak_str2int(FAR const char *str) +{ + long value = btsak_str2long(str); + if (value > INT_MAX || value < INT_MIN) + { + fprintf(stderr, "ERROR: Integer value out of range\n"); + exit(EXIT_FAILURE); + } + + return (int)value; +} + +/**************************************************************************** + * Name: btsak_str2uint8 + * + * Description: + * Convert a numeric string to an uint8_t value + * + ****************************************************************************/ + +uint8_t btsak_str2uint8(FAR const char *str) { long value = btsak_str2long(str); if (value < 0 || value > UINT8_MAX) @@ -390,7 +403,7 @@ uint8_t btsak_str2luint8(FAR const char *str) * Name: btsak_str2uint16 * * Description: - * Convert a string to an integer value + * Convert a numeric string to an uint16_t value * ****************************************************************************/ diff --git a/wireless/bluetooth/btsak/btsak_scan.c b/wireless/bluetooth/btsak/btsak_scan.c index 7710a8c02..c95d75744 100644 --- a/wireless/bluetooth/btsak/btsak_scan.c +++ b/wireless/bluetooth/btsak/btsak_scan.c @@ -97,7 +97,7 @@ static void btsak_cmd_scanstart(FAR struct btsak_s *btsak, FAR char *cmd, int ret; memset(&btreq, 0, sizeof(struct btreq_s)); - strncpy(btreq.btr_name, btsak->ifname, HCI_DEVNAME_SIZE); + strncpy(btreq.btr_name, btsak->ifname, IFNAMSIZ); /* Check if an option was provided */ @@ -154,7 +154,7 @@ static void btsak_cmd_scanget(FAR struct btsak_s *btsak, FAR char *cmd, /* Perform the IOCTL to get the scan results so far */ memset(&btreq, 0, sizeof(struct btreq_s)); - strncpy(btreq.btr_name, btsak->ifname, HCI_DEVNAME_SIZE); + strncpy(btreq.btr_name, btsak->ifname, IFNAMSIZ); btreq.btr_nrsp = 5; btreq.btr_rsp = result; @@ -232,7 +232,7 @@ static void btsak_cmd_scanstop(FAR struct btsak_s *btsak, FAR char *cmd, /* Perform the IOCTL to stop scanning and flush any buffered responses. */ memset(&btreq, 0, sizeof(struct btreq_s)); - strncpy(btreq.btr_name, btsak->ifname, HCI_DEVNAME_SIZE); + strncpy(btreq.btr_name, btsak->ifname, IFNAMSIZ); sockfd = btsak_socket(btsak); if (sockfd >= 0) diff --git a/wireless/bluetooth/btsak/btsak_security.c b/wireless/bluetooth/btsak/btsak_security.c index cebc2c91e..6d00543d8 100644 --- a/wireless/bluetooth/btsak/btsak_security.c +++ b/wireless/bluetooth/btsak/btsak_security.c @@ -152,7 +152,7 @@ void btsak_cmd_security(FAR struct btsak_s *btsak, int argc, FAR char *argv[]) /* The first argument must be an address of the form xx:xx:xx:xx:xx:xx */ memset(&btreq, 0, sizeof(struct btreq_s)); - strncpy(btreq.btr_name, btsak->ifname, HCI_DEVNAME_SIZE); + strncpy(btreq.btr_name, btsak->ifname, IFNAMSIZ); ret = btsak_str2addr(argv[1], btreq.btr_secaddr.val); if (ret < 0)