summaryrefslogtreecommitdiff
path: root/tools/btgatt-client.c
diff options
context:
space:
mode:
authorArman Uguray <armansito@chromium.org>2014-09-03 12:46:07 -0700
committerMarcel Holtmann <marcel@holtmann.org>2014-09-05 05:39:17 +0200
commita34cd13eded5e17b93673cb1891fddaf2a19aa31 (patch)
tree53ad6edcdc3e011bb8c07160a2f23ba19eef7ed8 /tools/btgatt-client.c
parent5f782cbd73d638eea0e9dc19cf7e7760676e482e (diff)
downloadbluez-a34cd13eded5e17b93673cb1891fddaf2a19aa31.tar.gz
tools/btgatt-client: Added the "read-value" command.
Added the "read-value" command which sends a simple read request to the specified handle.
Diffstat (limited to 'tools/btgatt-client.c')
-rw-r--r--tools/btgatt-client.c94
1 files changed, 82 insertions, 12 deletions
diff --git a/tools/btgatt-client.c b/tools/btgatt-client.c
index 0f13d35bf..e0c48be5f 100644
--- a/tools/btgatt-client.c
+++ b/tools/btgatt-client.c
@@ -274,9 +274,27 @@ static void services_usage(void)
"\tservices\n\tservices -u 0x180d\n\tservices -a 0x0009\n");
}
+static bool parse_args(char *str, int expected_argc, char **argv, int *argc)
+{
+ char **ap;
+
+ for (ap = argv; (*ap = strsep(&str, " \t")) != NULL;) {
+ if (**ap == '\0')
+ continue;
+
+ (*argc)++;
+ ap++;
+
+ if (*argc > expected_argc)
+ return false;
+ }
+
+ return true;
+}
+
static void cmd_services(struct client *cli, char *cmd_str)
{
- char **ap, *argv[3];
+ char *argv[3];
int argc = 0;
if (!bt_gatt_client_is_ready(cli->gatt)) {
@@ -284,17 +302,9 @@ static void cmd_services(struct client *cli, char *cmd_str)
return;
}
- for (ap = argv; (*ap = strsep(&cmd_str, " \t")) != NULL;) {
- if (**ap == '\0')
- continue;
-
- argc++;
- ap++;
-
- if (argc > 2) {
- services_usage();
- return;
- }
+ if (!parse_args(cmd_str, 2, argv, &argc)) {
+ services_usage();
+ return;
}
if (!argc) {
@@ -333,6 +343,64 @@ static void cmd_services(struct client *cli, char *cmd_str)
services_usage();
}
+static void read_value_usage(void)
+{
+ printf("Usage: read-value <value_handle>\n");
+}
+
+static void read_cb(bool success, uint8_t att_ecode, const uint8_t *value,
+ uint16_t length, void *user_data)
+{
+ int i;
+
+ if (!success) {
+ PRLOG("\nRead request failed: 0x%02x\n", att_ecode);
+ return;
+ }
+
+ printf("\nRead value");
+
+ if (length == 0) {
+ PRLOG(": 0 bytes\n");
+ return;
+ }
+
+ printf(" (%u bytes): ", length);
+
+ for (i = 0; i < length; i++)
+ printf("%02x ", value[i]);
+
+ PRLOG("\n");
+}
+
+static void cmd_read_value(struct client *cli, char *cmd_str)
+{
+ char *argv[2];
+ int argc = 0;
+ uint16_t handle;
+ char *endptr = NULL;
+
+ if (!bt_gatt_client_is_ready(cli->gatt)) {
+ printf("GATT client not initialized\n");
+ return;
+ }
+
+ if (!parse_args(cmd_str, 1, argv, &argc) || argc != 1) {
+ read_value_usage();
+ return;
+ }
+
+ handle = strtol(argv[0], &endptr, 16);
+ if (!endptr || *endptr != '\0' || !handle) {
+ printf("Invalid value handle: %s\n", argv[0]);
+ return;
+ }
+
+ if (!bt_gatt_client_read_value(cli->gatt, handle, read_cb,
+ NULL, NULL))
+ printf("Failed to initiate read value procedure\n");
+}
+
static void cmd_help(struct client *cli, char *cmd_str);
typedef void (*command_func_t)(struct client *cli, char *cmd_str);
@@ -344,6 +412,8 @@ static struct {
} command[] = {
{ "help", cmd_help, "\tDisplay help message" },
{ "services", cmd_services, "\tShow discovered services" },
+ { "read-value", cmd_read_value,
+ "\tRead a characteristic or descriptor Value" },
{ }
};