Please ignore my all not-applied previous patches. I am resending them
with a few corrections.
Bruna Moreira (2):
Move attr_data_from_string() to utils.c
Add Write Request in interactive gatttool
Sheldon Demario (4):
Create a helper function to deal with handles on interactive gatttool
Add Characteristics Descriptor Discovery option in interactive
gatttool
Add characteristics read options in interactive gatttool
Add sec-level option to interactive gattool
attrib/gatttool.c | 23 +---
attrib/gatttool.h | 1 +
attrib/interactive.c | 348 ++++++++++++++++++++++++++++++++++++++++++++++++--
attrib/utils.c | 19 +++
4 files changed, 361 insertions(+), 30 deletions(-)
Hi Sheldon,
On Thu, Feb 24, 2011, Sheldon Demario wrote:
> ---
> attrib/interactive.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
> 1 files changed, 44 insertions(+), 0 deletions(-)
All six patches have been pushed upstream. Thanks.
Johan
---
attrib/interactive.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 72 insertions(+), 0 deletions(-)
diff --git a/attrib/interactive.c b/attrib/interactive.c
index 7b18e90..89e244a 100644
--- a/attrib/interactive.c
+++ b/attrib/interactive.c
@@ -169,6 +169,47 @@ static void char_cb(GSList *characteristics, guint8 status, gpointer user_data)
rl_forced_update_display();
}
+static void char_desc_cb(guint8 status, const guint8 *pdu, guint16 plen,
+ gpointer user_data)
+{
+ struct att_data_list *list;
+ guint8 format;
+ int i;
+
+ if (status != 0) {
+ printf("Discover all characteristic descriptors failed: "
+ "%s\n", att_ecode2str(status));
+ return;
+ }
+
+ list = dec_find_info_resp(pdu, plen, &format);
+ if (list == NULL)
+ return;
+
+ printf("\n");
+ for (i = 0; i < list->num; i++) {
+ char uuidstr[MAX_LEN_UUID_STR];
+ uint16_t handle;
+ uint8_t *value;
+ uuid_t uuid;
+
+ value = list->data[i];
+ handle = att_get_u16(value);
+
+ if (format == 0x01)
+ sdp_uuid16_create(&uuid, att_get_u16(&value[2]));
+ else
+ sdp_uuid128_create(&uuid, &value[2]);
+
+ sdp_uuid2strn(&uuid, uuidstr, MAX_LEN_UUID_STR);
+ printf("handle: 0x%04x, uuid: %s\n", handle, uuidstr);
+ }
+
+ att_data_list_free(list);
+
+ rl_forced_update_display();
+}
+
static void cmd_exit(int argcp, char **argvp)
{
rl_callback_handler_remove();
@@ -280,6 +321,35 @@ static void cmd_char(int argcp, char **argvp)
gatt_discover_char(attrib, start, end, char_cb, NULL);
}
+static void cmd_char_desc(int argcp, char **argvp)
+{
+ int start = 0x0001;
+ int end = 0xffff;
+
+ if (conn_state != STATE_CONNECTED) {
+ printf("Command failed: disconnected\n");
+ return;
+ }
+
+ if (argcp > 1) {
+ start = strtohandle(argvp[1]);
+ if (start < 0) {
+ printf("Invalid start handle: %s\n", argvp[1]);
+ return;
+ }
+ }
+
+ if (argcp > 2) {
+ end = strtohandle(argvp[2]);
+ if (end < 0) {
+ printf("Invalid end handle: %s\n", argvp[2]);
+ return;
+ }
+ }
+
+ gatt_find_info(attrib, start, end, char_desc_cb, NULL);
+}
+
static struct {
const char *cmd;
void (*func)(int argcp, char **argvp);
@@ -298,6 +368,8 @@ static struct {
"Primary Service Discovery" },
{ "characteristics", cmd_char, "[start hnd] [end hnd]",
"Characteristics Discovery" },
+ { "char-desc", cmd_char_desc, "[start hnd] [end hnd]",
+ "Characteristics Descriptor Discovery" },
{ NULL, NULL, NULL}
};
--
1.7.1
---
attrib/interactive.c | 27 +++++++++++++++++----------
1 files changed, 17 insertions(+), 10 deletions(-)
diff --git a/attrib/interactive.c b/attrib/interactive.c
index 7cc03bc..7b18e90 100644
--- a/attrib/interactive.c
+++ b/attrib/interactive.c
@@ -238,6 +238,19 @@ static void cmd_primary(int argcp, char **argvp)
gatt_discover_primary(attrib, &uuid, primary_by_uuid_cb, NULL);
}
+static int strtohandle(const char *src)
+{
+ char *e;
+ int dst;
+
+ errno = 0;
+ dst = strtoll(src, &e, 16);
+ if (errno != 0 || *e != '\0')
+ return -EINVAL;
+
+ return dst;
+}
+
static void cmd_char(int argcp, char **argvp)
{
int start = 0x0001;
@@ -249,28 +262,22 @@ static void cmd_char(int argcp, char **argvp)
}
if (argcp > 1) {
- char *e;
-
- start = strtoll(argvp[1], &e, 16);
- if (errno != 0 || *e != '\0') {
+ start = strtohandle(argvp[1]);
+ if (start < 0) {
printf("Invalid start handle: %s\n", argvp[1]);
return;
}
}
if (argcp > 2) {
- char *e;
-
- end = strtoll(argvp[2], &e, 16);
- if (errno != 0 || *e != '\0') {
+ end = strtohandle(argvp[2]);
+ if (end < 0) {
printf("Invalid end handle: %s\n", argvp[2]);
return;
}
}
gatt_discover_char(attrib, start, end, char_cb, NULL);
-
- return;
}
static struct {
--
1.7.1
From: Bruna Moreira <[email protected]>
The attr_data_from_string() function will be used in interactive and
usual gatttool so this function was moved to common file utils.c.
---
attrib/gatttool.c | 23 ++---------------------
attrib/gatttool.h | 1 +
attrib/utils.c | 19 +++++++++++++++++++
3 files changed, 22 insertions(+), 21 deletions(-)
diff --git a/attrib/gatttool.c b/attrib/gatttool.c
index 7478043..d7887c6 100644
--- a/attrib/gatttool.c
+++ b/attrib/gatttool.c
@@ -313,25 +313,6 @@ static gboolean characteristics_read(gpointer user_data)
return FALSE;
}
-static size_t attr_data_from_string(const char *str, uint8_t **data)
-{
- char tmp[3];
- size_t size, i;
-
- size = strlen(str) / 2;
- *data = g_try_malloc0(size);
- if (*data == NULL)
- return 0;
-
- tmp[2] = '\0';
- for (i = 0; i < size; i++) {
- memcpy(tmp, str + (i * 2), 2);
- (*data)[i] = (uint8_t) strtol(tmp, NULL, 16);
- }
-
- return size;
-}
-
static void mainloop_quit(gpointer user_data)
{
uint8_t *value = user_data;
@@ -356,7 +337,7 @@ static gboolean characteristics_write(gpointer user_data)
goto error;
}
- len = attr_data_from_string(opt_value, &value);
+ len = gatt_attr_data_from_string(opt_value, &value);
if (len == 0) {
g_printerr("Invalid value\n");
goto error;
@@ -408,7 +389,7 @@ static gboolean characteristics_write_req(gpointer user_data)
goto error;
}
- len = attr_data_from_string(opt_value, &value);
+ len = gatt_attr_data_from_string(opt_value, &value);
if (len == 0) {
g_printerr("Invalid value\n");
goto error;
diff --git a/attrib/gatttool.h b/attrib/gatttool.h
index 7eae18d..2fd4a46 100644
--- a/attrib/gatttool.h
+++ b/attrib/gatttool.h
@@ -25,3 +25,4 @@ int interactive(gchar *dst, gboolean le);
GIOChannel *gatt_connect(const gchar *src, const gchar *dst,
const gchar *sec_level, int psm, int mtu,
BtIOConnect connect_cb);
+size_t gatt_attr_data_from_string(const char *str, uint8_t **data);
diff --git a/attrib/utils.c b/attrib/utils.c
index 4d0000d..8d1ca74 100644
--- a/attrib/utils.c
+++ b/attrib/utils.c
@@ -104,3 +104,22 @@ GIOChannel *gatt_connect(const gchar *src, const gchar *dst,
return chan;
}
+
+size_t gatt_attr_data_from_string(const char *str, uint8_t **data)
+{
+ char tmp[3];
+ size_t size, i;
+
+ size = strlen(str) / 2;
+ *data = g_try_malloc0(size);
+ if (*data == NULL)
+ return 0;
+
+ tmp[2] = '\0';
+ for (i = 0; i < size; i++) {
+ memcpy(tmp, str + (i * 2), 2);
+ (*data)[i] = (uint8_t) strtol(tmp, NULL, 16);
+ }
+
+ return size;
+}
--
1.7.1
---
attrib/interactive.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 44 insertions(+), 0 deletions(-)
diff --git a/attrib/interactive.c b/attrib/interactive.c
index e5817ab..ff676b4 100644
--- a/attrib/interactive.c
+++ b/attrib/interactive.c
@@ -551,6 +551,48 @@ static void cmd_char_write_req(int argcp, char **argvp)
g_free(value);
}
+static void cmd_sec_level(int argcp, char **argvp)
+{
+ GError *gerr = NULL;
+ BtIOSecLevel sec_level;
+
+ if (argcp < 2) {
+ printf("sec-level: %s\n", opt_sec_level);
+ return;
+ }
+
+ if (strcasecmp(argvp[1], "medium") == 0)
+ sec_level = BT_IO_SEC_MEDIUM;
+ else if (strcasecmp(argvp[1], "high") == 0)
+ sec_level = BT_IO_SEC_HIGH;
+ else if (strcasecmp(argvp[1], "low") == 0)
+ sec_level = BT_IO_SEC_LOW;
+ else {
+ printf("Allowed values: low | medium | high\n");
+ return;
+ }
+
+ g_free(opt_sec_level);
+ opt_sec_level = strdup(argvp[1]);
+
+ if (conn_state != STATE_CONNECTED)
+ return;
+
+ if (opt_psm) {
+ printf("It must be reconnected to this change take effect\n");
+ return;
+ }
+
+ bt_io_set(iochannel, BT_IO_L2CAP, &gerr,
+ BT_IO_OPT_SEC_LEVEL, sec_level,
+ BT_IO_OPT_INVALID);
+
+ if (gerr) {
+ printf("Error: %s\n", gerr->message);
+ g_error_free(gerr);
+ }
+}
+
static struct {
const char *cmd;
void (*func)(int argcp, char **argvp);
@@ -577,6 +619,8 @@ static struct {
"Characteristics Value/Descriptor Read by UUID" },
{ "char-write-req", cmd_char_write_req, "<handle> <new value>",
"Characteristic Value Write (Write Request)" },
+ { "sec-level", cmd_sec_level, "[low | medium | high]",
+ "Set security level. Default: low" },
{ NULL, NULL, NULL}
};
--
1.7.1
From: Bruna Moreira <[email protected]>
---
attrib/interactive.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 52 insertions(+), 0 deletions(-)
diff --git a/attrib/interactive.c b/attrib/interactive.c
index c5dd2b7..e5817ab 100644
--- a/attrib/interactive.c
+++ b/attrib/interactive.c
@@ -501,6 +501,56 @@ static void cmd_read_uuid(int argcp, char **argvp)
char_read_by_uuid_cb, char_data);
}
+static void char_write_req_cb(guint8 status, const guint8 *pdu, guint16 plen,
+ gpointer user_data)
+{
+ if (status != 0) {
+ printf("Characteristic Write Request failed: "
+ "%s\n", att_ecode2str(status));
+ return;
+ }
+
+ if (!dec_write_resp(pdu, plen)) {
+ printf("Protocol error\n");
+ return;
+ }
+
+ printf("Characteristic value was written successfully\n");
+}
+
+static void cmd_char_write_req(int argcp, char **argvp)
+{
+ uint8_t *value;
+ size_t plen;
+ int handle;
+
+ if (conn_state != STATE_CONNECTED) {
+ printf("Command failed: disconnected\n");
+ return;
+ }
+
+ if (argcp < 3) {
+ printf("Usage: char-write-req <handle> <new value>\n");
+ return;
+ }
+
+ handle = strtoll(argvp[1], NULL, 16);
+ if (errno != 0 || handle <= 0) {
+ printf("A valid handle is required\n");
+ return;
+ }
+
+ plen = gatt_attr_data_from_string(argvp[2], &value);
+ if (plen == 0) {
+ g_printerr("Invalid value\n");
+ return;
+ }
+
+ gatt_write_char(attrib, handle, value, plen, char_write_req_cb, NULL);
+
+ g_free(value);
+}
+
static struct {
const char *cmd;
void (*func)(int argcp, char **argvp);
@@ -525,6 +575,8 @@ static struct {
"Characteristics Value/Descriptor Read by handle" },
{ "char-read-uuid", cmd_read_uuid, "<UUID> [start hnd] [end hnd]",
"Characteristics Value/Descriptor Read by UUID" },
+ { "char-write-req", cmd_char_write_req, "<handle> <new value>",
+ "Characteristic Value Write (Write Request)" },
{ NULL, NULL, NULL}
};
--
1.7.1
---
attrib/interactive.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 156 insertions(+), 1 deletions(-)
diff --git a/attrib/interactive.c b/attrib/interactive.c
index 89e244a..c5dd2b7 100644
--- a/attrib/interactive.c
+++ b/attrib/interactive.c
@@ -50,6 +50,13 @@ static gchar *opt_sec_level = NULL;
static int opt_psm = 0;
static int opt_mtu = 0;
+struct characteristic_data {
+ uint16_t orig_start;
+ uint16_t start;
+ uint16_t end;
+ uuid_t uuid;
+};
+
static void cmd_help(int argcp, char **argvp);
enum state {
@@ -210,6 +217,79 @@ static void char_desc_cb(guint8 status, const guint8 *pdu, guint16 plen,
rl_forced_update_display();
}
+static void char_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
+ gpointer user_data)
+{
+ uint8_t value[ATT_MAX_MTU];
+ int i, vlen;
+
+ if (status != 0) {
+ printf("Characteristic value/descriptor read failed: %s\n",
+ att_ecode2str(status));
+ return;
+ }
+
+ if (!dec_read_resp(pdu, plen, value, &vlen)) {
+ printf("Protocol error\n");
+ return;
+ }
+
+ printf("\nCharacteristic value/descriptor: ");
+ for (i = 0; i < vlen; i++)
+ printf("%02x ", value[i]);
+ printf("\n");
+
+ rl_forced_update_display();
+}
+
+static void char_read_by_uuid_cb(guint8 status, const guint8 *pdu,
+ guint16 plen, gpointer user_data)
+{
+ struct characteristic_data *char_data = user_data;
+ struct att_data_list *list;
+ int i;
+
+ if (status == ATT_ECODE_ATTR_NOT_FOUND &&
+ char_data->start != char_data->orig_start)
+ goto done;
+
+ if (status != 0) {
+ printf("Read characteristics by UUID failed: %s\n",
+ att_ecode2str(status));
+ goto done;
+ }
+
+ list = dec_read_by_type_resp(pdu, plen);
+ if (list == NULL)
+ goto done;
+
+ for (i = 0; i < list->num; i++) {
+ uint8_t *value = list->data[i];
+ int j;
+
+ char_data->start = att_get_u16(value) + 1;
+
+ printf("\nhandle: 0x%04x \t value: ", att_get_u16(value));
+ value += 2;
+ for (j = 0; j < list->len - 2; j++, value++)
+ printf("%02x ", *value);
+ printf("\n");
+ }
+
+ att_data_list_free(list);
+
+ gatt_read_char_by_uuid(attrib, char_data->start, char_data->end,
+ &char_data->uuid, char_read_by_uuid_cb,
+ char_data);
+
+ rl_forced_update_display();
+
+ return;
+
+done:
+ g_free(char_data);
+}
+
static void cmd_exit(int argcp, char **argvp)
{
rl_callback_handler_remove();
@@ -350,6 +430,77 @@ static void cmd_char_desc(int argcp, char **argvp)
gatt_find_info(attrib, start, end, char_desc_cb, NULL);
}
+static void cmd_read_hnd(int argcp, char **argvp)
+{
+ int handle;
+
+ if (conn_state != STATE_CONNECTED) {
+ printf("Command failed: disconnected\n");
+ return;
+ }
+
+ if (argcp < 2) {
+ printf("Missing argument: handle\n");
+ return;
+ }
+
+ handle = strtohandle(argvp[1]);
+ if (handle < 0) {
+ printf("Invalid handle: %s\n", argvp[1]);
+ return;
+ }
+
+ gatt_read_char(attrib, handle, char_read_cb, attrib);
+}
+
+static void cmd_read_uuid(int argcp, char **argvp)
+{
+ struct characteristic_data *char_data;
+ int start = 0x0001;
+ int end = 0xffff;
+ uuid_t uuid;
+
+ if (conn_state != STATE_CONNECTED) {
+ printf("Command failed: disconnected\n");
+ return;
+ }
+
+ if (argcp < 2) {
+ printf("Missing argument: UUID\n");
+ return;
+ }
+
+ if (bt_string2uuid(&uuid, argvp[1]) < 0) {
+ printf("Invalid UUID\n");
+ return;
+ }
+
+ if (argcp > 2) {
+ start = strtohandle(argvp[2]);
+ if (start < 0) {
+ printf("Invalid start handle: %s\n", argvp[1]);
+ return;
+ }
+ }
+
+ if (argcp > 3) {
+ end = strtohandle(argvp[3]);
+ if (end < 0) {
+ printf("Invalid end handle: %s\n", argvp[2]);
+ return;
+ }
+ }
+
+ char_data = g_new(struct characteristic_data, 1);
+ char_data->orig_start = start;
+ char_data->start = start;
+ char_data->end = end;
+ char_data->uuid = uuid;
+
+ gatt_read_char_by_uuid(attrib, start, end, &char_data->uuid,
+ char_read_by_uuid_cb, char_data);
+}
+
static struct {
const char *cmd;
void (*func)(int argcp, char **argvp);
@@ -370,6 +521,10 @@ static struct {
"Characteristics Discovery" },
{ "char-desc", cmd_char_desc, "[start hnd] [end hnd]",
"Characteristics Descriptor Discovery" },
+ { "char-read-hnd", cmd_read_hnd, "<handle>",
+ "Characteristics Value/Descriptor Read by handle" },
+ { "char-read-uuid", cmd_read_uuid, "<UUID> [start hnd] [end hnd]",
+ "Characteristics Value/Descriptor Read by UUID" },
{ NULL, NULL, NULL}
};
@@ -378,7 +533,7 @@ static void cmd_help(int argcp, char **argvp)
int i;
for (i = 0; commands[i].cmd; i++)
- printf("%-15s %-25s %s\n", commands[i].cmd,
+ printf("%-15s %-30s %s\n", commands[i].cmd,
commands[i].params, commands[i].desc);
}
--
1.7.1