diff options
author | Luiz Augusto von Dentz <luiz.von.dentz@intel.com> | 2022-12-05 14:26:17 -0800 |
---|---|---|
committer | Luiz Augusto von Dentz <luiz.von.dentz@intel.com> | 2022-12-06 13:01:28 -0800 |
commit | 9a550d43b84ed808ff2a678b0117a8e14355d80c (patch) | |
tree | a97071b0e02dab190cfbf4cd71de549b72c565a8 /client/gatt.c | |
parent | 35947e26771c89d965aec5ad059267bb8c15298e (diff) | |
download | bluez-9a550d43b84ed808ff2a678b0117a8e14355d80c.tar.gz |
client: Allow gatt.select-attribute to work with local attributes
This allows gatt.select-attribute local to select from the registered
attributes:
[bluetooth]# gatt.select-attribute local /org/bluez/app/service0/chrc0
[/org/bluez/app/service0/chrc0]# gatt.write 0x01
[CHG] Attribute /org/bluez/app/service0/chrc0 (%UUID) written
[/org/bluez/app/service0/chrc0]# gatt.read
01 .
[/org/bluez/app/service0/chrc0]# gatt.select-attribute local
/org/bluez/app/service0/chrc1
[/org/bluez/app/service0/chrc1]# gatt.write 0x01
[CHG] Attribute /org/bluez/app/service0/chrc1 (%UUID) written
[/org/bluez/app/service0/chrc1]# gatt.read
01 .
[/org/bluez/app/service0/chrc1]#
Diffstat (limited to 'client/gatt.c')
-rw-r--r-- | client/gatt.c | 298 |
1 files changed, 235 insertions, 63 deletions
diff --git a/client/gatt.c b/client/gatt.c index 8f2920269..f03fc1526 100644 --- a/client/gatt.c +++ b/client/gatt.c @@ -554,6 +554,241 @@ GDBusProxy *gatt_select_attribute(GDBusProxy *parent, const char *arg) return select_attribute_by_uuid(NULL, arg); } +static char *find_local_attribute(const char *arg, + struct service **service, + struct chrc **chrc, struct desc **desc) +{ + GList *l; + + for (l = local_services; l; l = g_list_next(l)) { + struct service *s = l->data; + GList *cl; + + if (!strcmp(arg, s->path)) { + if (service) + *service = s; + return s->path; + } + + if (!strcmp(arg, s->uuid)) { + if (service) + *service = s; + return s->path; + } + + for (cl = s->chrcs; cl; cl = g_list_next(cl)) { + struct chrc *c = cl->data; + GList *dl; + + if (!strcmp(arg, c->path)) { + if (chrc) + *chrc = c; + return c->path; + } + + if (!strcmp(arg, c->uuid)) { + if (chrc) + *chrc = c; + return c->path; + } + + for (dl = c->descs; dl; dl = g_list_next(dl)) { + struct desc *d = dl->data; + + if (!strcmp(arg, d->path)) { + if (desc) + *desc = d; + return d->path; + } + + if (!strcmp(arg, d->uuid)) { + if (desc) + *desc = d; + return d->path; + } + } + } + } + + return NULL; +} + +char *gatt_select_local_attribute(const char *arg) +{ + return find_local_attribute(arg, NULL, NULL, NULL); +} + +static int parse_offset(const char *arg) +{ + char *endptr = NULL; + unsigned long offset; + + offset = strtoul(arg, &endptr, 0); + if (!endptr || *endptr != '\0' || offset > UINT16_MAX) { + bt_shell_printf("Invalid offload: %s", arg); + return -EINVAL; + } + + return offset; +} + +void gatt_read_local_attribute(char *data, int argc, char *argv[]) +{ + int offset = 0; + struct service *s = NULL; + struct chrc *c = NULL; + struct desc *d = NULL; + + if (!find_local_attribute(data, &s, &c, &d)) { + bt_shell_printf("Unable to find local attribute %s\n", data); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + if (argc > 1) { + offset = parse_offset(argv[1]); + if (offset < 0) + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + if (s) { + bt_shell_printf("UUID %s", s->uuid); + return bt_shell_noninteractive_quit(EXIT_SUCCESS); + } + + if (c) { + if ((size_t)offset >= c->value_len) { + bt_shell_printf("Invalid offset: %d >= %zd", offset, + c->value_len); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + bt_shell_hexdump(&c->value[offset], c->value_len - offset); + return bt_shell_noninteractive_quit(EXIT_SUCCESS); + } + + if (d) { + if ((size_t)offset >= d->value_len) { + bt_shell_printf("Invalid offset: %d >= %zd", offset, + d->value_len); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + bt_shell_hexdump(&d->value[offset], d->value_len - offset); + return bt_shell_noninteractive_quit(EXIT_SUCCESS); + } +} + +static uint8_t *str2bytearray(char *arg, size_t *val_len) +{ + uint8_t value[MAX_ATTR_VAL_LEN]; + char *entry; + unsigned int i; + + for (i = 0; (entry = strsep(&arg, " \t")) != NULL; i++) { + long val; + char *endptr = NULL; + + if (*entry == '\0') + continue; + + if (i >= G_N_ELEMENTS(value)) { + bt_shell_printf("Too much data\n"); + return NULL; + } + + val = strtol(entry, &endptr, 0); + if (!endptr || *endptr != '\0' || val > UINT8_MAX) { + bt_shell_printf("Invalid value at index %d\n", i); + return NULL; + } + + value[i] = val; + } + + *val_len = i; + + return util_memdup(value, i); +} + +static int write_value(size_t *dst_len, uint8_t **dst_value, uint8_t *src_val, + size_t src_len, uint16_t offset, uint16_t max_len) +{ + if ((offset + src_len) > max_len) + return -EOVERFLOW; + + if ((offset + src_len) != *dst_len) { + *dst_len = offset + src_len; + *dst_value = g_realloc(*dst_value, *dst_len); + } + + if (src_val && src_len) + memcpy(*dst_value + offset, src_val, src_len); + + return 0; +} + +void gatt_write_local_attribute(char *data, int argc, char *argv[]) +{ + int offset = 0; + struct service *s = NULL; + struct chrc *c = NULL; + struct desc *d = NULL; + uint8_t *value; + size_t value_len; + + if (!find_local_attribute(data, &s, &c, &d)) { + bt_shell_printf("Unable to find local attribute %s\n", data); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + value = str2bytearray(argv[1], &value_len); + if (!value) + return bt_shell_noninteractive_quit(EXIT_FAILURE); + + if (argc > 2) { + offset = parse_offset(argv[2]); + if (offset < 0) + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + if (s) { + bt_shell_printf("Unable to overwrite local service %s\n", data); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + if (c) { + if (write_value(&c->value_len, &c->value, + value, value_len, + offset, c->max_val_len)) { + bt_shell_printf("Unable to write local attribute %s\n", + data); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + bt_shell_printf("[" COLORED_CHG "] Attribute %s (%s) written\n", + c->path, bt_uuidstr_to_str(c->uuid)); + + g_dbus_emit_property_changed(c->service->conn, c->path, + CHRC_INTERFACE, "Value"); + } + + if (d) { + if (write_value(&d->value_len, &d->value, + value, value_len, + offset, d->max_val_len)) { + bt_shell_printf("Unable to write local attribute %s\n", + data); + return bt_shell_noninteractive_quit(EXIT_FAILURE); + } + + bt_shell_printf("[" COLORED_CHG "] Attribute %s (%s) written\n", + d->path, bt_uuidstr_to_str(d->uuid)); + + g_dbus_emit_property_changed(d->chrc->service->conn, d->path, + DESC_INTERFACE, "Value"); + } + + free(value); +} + static char *attribute_generator(const char *text, int state, GList *source) { static int index; @@ -650,20 +885,6 @@ static void read_attribute(GDBusProxy *proxy, uint16_t offset) bt_shell_printf("Attempting to read %s\n", g_dbus_proxy_get_path(proxy)); } -static int parse_offset(const char *arg) -{ - char *endptr = NULL; - unsigned long offset; - - offset = strtoul(arg, &endptr, 0); - if (!endptr || *endptr != '\0' || offset > UINT16_MAX) { - bt_shell_printf("Invalid offload: %s", arg); - return -EINVAL; - } - - return offset; -} - void gatt_read_attribute(GDBusProxy *proxy, int argc, char *argv[]) { const char *iface; @@ -782,38 +1003,6 @@ static void write_attribute(GDBusProxy *proxy, g_dbus_proxy_get_path(proxy)); } -static uint8_t *str2bytearray(char *arg, size_t *val_len) -{ - uint8_t value[MAX_ATTR_VAL_LEN]; - char *entry; - unsigned int i; - - for (i = 0; (entry = strsep(&arg, " \t")) != NULL; i++) { - long int val; - char *endptr = NULL; - - if (*entry == '\0') - continue; - - if (i >= G_N_ELEMENTS(value)) { - bt_shell_printf("Too much data\n"); - return NULL; - } - - val = strtol(entry, &endptr, 0); - if (!endptr || *endptr != '\0' || val > UINT8_MAX) { - bt_shell_printf("Invalid value at index %d\n", i); - return NULL; - } - - value[i] = val; - } - - *val_len = i; - - return util_memdup(value, i); -} - void gatt_write_attribute(GDBusProxy *proxy, int argc, char *argv[]) { const char *iface; @@ -2132,23 +2321,6 @@ static int parse_value_arg(DBusMessageIter *iter, uint8_t **value, int *len) return 0; } -static int write_value(size_t *dst_len, uint8_t **dst_value, uint8_t *src_val, - size_t src_len, uint16_t offset, uint16_t max_len) -{ - if ((offset + src_len) > max_len) - return -EOVERFLOW; - - if ((offset + src_len) != *dst_len) { - *dst_len = offset + src_len; - *dst_value = g_realloc(*dst_value, *dst_len); - } - - if (src_val && src_len) - memcpy(*dst_value + offset, src_val, src_len); - - return 0; -} - static void authorize_write_response(const char *input, void *user_data) { struct authorize_attribute_data *aad = user_data; |