apps/wireless/bluetooth/btsak: Clean-up for changes to IOCTL defintions; Add GATT read/write comments. Stell need commands to get the result of the read/write operations.

This commit is contained in:
Gregory Nutt 2018-04-21 09:45:50 -06:00
parent 99479aa8d9
commit 8a0e690483
8 changed files with 234 additions and 36 deletions

View File

@ -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
*
****************************************************************************/

View File

@ -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)

View File

@ -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)

View File

@ -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 <addr>: %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] <addr> public|private <handle> <nitems> command
* gatt [-h] read-multiple [-h] <addr> public|private <handle> [<handle> [<handle>]..]
*
****************************************************************************/
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 <addr>: %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] <addr> public|private <handle> <datum> command
* gatt [-h] write [-h] [-h] <addr> public|private <handle> <byte> [<byte> [<byte>]..]
*
****************************************************************************/
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 <addr>: %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);
}

View File

@ -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)

View File

@ -155,12 +155,12 @@ static const struct btsak_command_s g_btsak_gatt_commands[] =
{
"read-multiple",
(CODE void *)btsak_cmd_gatt_read_multiple,
"[-h] <addr> public|private <handle> <nitems>"
"[-h] <addr> public|private <handle> [<handle> [<handle>]..]"
},
{
"write",
(CODE void *)btsak_cmd_gatt_write,
"[-h] <addr> public|private <handle> <datum>"
"[-h] <addr> public|private <handle> <byte> [<byte> [<byte>]..]"
}
};
@ -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
*
****************************************************************************/

View File

@ -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)

View File

@ -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)