2014-11-26 23:03:07

by Arman Uguray

[permalink] [raw]
Subject: [PATCH BlueZ v1 0/5] shared/gatt-db: Add support for client role.

*v1: Addressed comments by Luiz and Michael.

This patch introduces new API functions to shared/gatt-db to prepare it for
GATT client-role usage. This is the first step before rewriting
shared/gatt-client using gatt-db.

This patch set adds the following:
- Functions for extracting service, characteristic, descriptor, and include
definition information from appropriate attributes.
- foreach functions that allows iterating through all services, listing the
characteristics of a particular service, etc.
- Database clear functions to remove services from that database as needed.
- A new service insertion function for inserting a service with a pre-defined
handle into the database.
- A new queue_push_after function for shared/queue to enable the previous
point.

Comments are welcome. I have implemented a trial version of the new gatt-client
using the above additions and this is the basic set of functionality that should
enable most use cases for client role. I think this gives us enough of a
starting point that we can easily change later if we find that these could be
done better.

Arman Uguray (5):
shared/gatt-db: Add high-level functions for client
shared: Add function to insert element after entry
unit/test-queue: Add /queue/insert_after test
shared/gatt-db: Add gatt_db_insert_service function
shared/gatt-db: Add clear functions

src/shared/gatt-db.c | 416 +++++++++++++++++++++++++++++++++++++++++++++++++--
src/shared/gatt-db.h | 51 +++++++
src/shared/queue.c | 35 +++++
src/shared/queue.h | 1 +
unit/test-queue.c | 52 +++++++
5 files changed, 545 insertions(+), 10 deletions(-)

--
2.2.0.rc0.207.ga3a616c



2014-11-28 12:06:30

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: Re: [PATCH BlueZ v1 0/5] shared/gatt-db: Add support for client role.

Hi Arman,

On Thu, Nov 27, 2014 at 1:03 AM, Arman Uguray <[email protected]> wrote:
> *v1: Addressed comments by Luiz and Michael.
>
> This patch introduces new API functions to shared/gatt-db to prepare it for
> GATT client-role usage. This is the first step before rewriting
> shared/gatt-client using gatt-db.
>
> This patch set adds the following:
> - Functions for extracting service, characteristic, descriptor, and include
> definition information from appropriate attributes.
> - foreach functions that allows iterating through all services, listing the
> characteristics of a particular service, etc.
> - Database clear functions to remove services from that database as needed.
> - A new service insertion function for inserting a service with a pre-defined
> handle into the database.
> - A new queue_push_after function for shared/queue to enable the previous
> point.
>
> Comments are welcome. I have implemented a trial version of the new gatt-client
> using the above additions and this is the basic set of functionality that should
> enable most use cases for client role. I think this gives us enough of a
> starting point that we can easily change later if we find that these could be
> done better.
>
> Arman Uguray (5):
> shared/gatt-db: Add high-level functions for client
> shared: Add function to insert element after entry
> unit/test-queue: Add /queue/insert_after test
> shared/gatt-db: Add gatt_db_insert_service function
> shared/gatt-db: Add clear functions
>
> src/shared/gatt-db.c | 416 +++++++++++++++++++++++++++++++++++++++++++++++++--
> src/shared/gatt-db.h | 51 +++++++
> src/shared/queue.c | 35 +++++
> src/shared/queue.h | 1 +
> unit/test-queue.c | 52 +++++++
> 5 files changed, 545 insertions(+), 10 deletions(-)
>
> --
> 2.2.0.rc0.207.ga3a616c

Applied after replacing /insert_after/push_after, thanks.


--
Luiz Augusto von Dentz

2014-11-26 23:03:12

by Arman Uguray

[permalink] [raw]
Subject: [PATCH BlueZ v1 5/5] shared/gatt-db: Add clear functions

This patch introduces gatt_db_clear and gatt_db_clear_range functions
for removing services from the database.
---
src/shared/gatt-db.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
src/shared/gatt-db.h | 3 +++
2 files changed, 48 insertions(+)

diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
index 0c24904..2515e5a 100644
--- a/src/shared/gatt-db.c
+++ b/src/shared/gatt-db.c
@@ -305,6 +305,51 @@ bool gatt_db_remove_service(struct gatt_db *db,
return true;
}

+bool gatt_db_clear(struct gatt_db *db)
+{
+ if (!db)
+ return false;
+
+ queue_remove_all(db->services, NULL, NULL, gatt_db_service_destroy);
+
+ db->next_handle = 0;
+
+ return true;
+}
+
+struct clear_range {
+ uint16_t start, end;
+};
+
+static bool match_range(const void *a, const void *b)
+{
+ const struct gatt_db_service *service = a;
+ const struct clear_range *range = b;
+ uint16_t svc_start, svc_end;
+
+ svc_start = service->attributes[0]->handle;
+ svc_end = service->attributes[0]->handle + service->num_handles - 1;
+
+ return svc_start <= range->end && svc_end >= range->start;
+}
+
+bool gatt_db_clear_range(struct gatt_db *db, uint16_t start_handle,
+ uint16_t end_handle)
+{
+ struct clear_range range;
+
+ if (!db || start_handle > end_handle)
+ return false;
+
+ range.start = start_handle;
+ range.end = end_handle;
+
+ queue_remove_all(db->services, match_range, &range,
+ gatt_db_service_destroy);
+
+ return true;
+}
+
struct insert_loc_data {
struct gatt_db_service *cur;
uint16_t start, end;
diff --git a/src/shared/gatt-db.h b/src/shared/gatt-db.h
index c9e3b59..ea9f2dc 100644
--- a/src/shared/gatt-db.h
+++ b/src/shared/gatt-db.h
@@ -34,6 +34,9 @@ struct gatt_db_attribute *gatt_db_add_service(struct gatt_db *db,

bool gatt_db_remove_service(struct gatt_db *db,
struct gatt_db_attribute *attrib);
+bool gatt_db_clear(struct gatt_db *db);
+bool gatt_db_clear_range(struct gatt_db *db, uint16_t start_handle,
+ uint16_t end_handle);

struct gatt_db_attribute *gatt_db_insert_service(struct gatt_db *db,
uint16_t handle,
--
2.2.0.rc0.207.ga3a616c


2014-11-26 23:03:10

by Arman Uguray

[permalink] [raw]
Subject: [PATCH BlueZ v1 3/5] unit/test-queue: Add /queue/insert_after test

This tests the queue_insert_after function.
---
unit/test-queue.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 52 insertions(+)

diff --git a/unit/test-queue.c b/unit/test-queue.c
index 9fe15ba..95d14b7 100644
--- a/unit/test-queue.c
+++ b/unit/test-queue.c
@@ -118,6 +118,57 @@ static void test_destroy_remove(void)
queue_destroy(static_queue, destroy_remove);
}

+static void test_push_after(void)
+{
+ struct queue *queue;
+ unsigned int len, i;
+
+ queue = queue_new();
+ g_assert(queue != NULL);
+
+ /*
+ * Pre-populate queue. Initial elements are:
+ * [ NULL, 2, 5 ]
+ */
+ g_assert(queue_push_tail(queue, NULL));
+ g_assert(queue_push_tail(queue, UINT_TO_PTR(2)));
+ g_assert(queue_push_tail(queue, UINT_TO_PTR(5)));
+ g_assert(queue_length(queue) == 3);
+
+ /* Invalid insertion */
+ g_assert(!queue_push_after(queue, UINT_TO_PTR(6), UINT_TO_PTR(1)));
+
+ /* Valid insertions */
+ g_assert(queue_push_after(queue, NULL, UINT_TO_PTR(1)));
+ g_assert(queue_push_after(queue, UINT_TO_PTR(2), UINT_TO_PTR(3)));
+ g_assert(queue_push_after(queue, UINT_TO_PTR(3), UINT_TO_PTR(4)));
+ g_assert(queue_push_after(queue, UINT_TO_PTR(5), UINT_TO_PTR(6)));
+
+ g_assert(queue_peek_head(queue) == NULL);
+ g_assert(queue_peek_tail(queue) == UINT_TO_PTR(6));
+
+ /*
+ * Queue should contain 7 elements:
+ * [ NULL, 1, 2, 3, 4, 5, 6 ]
+ */
+ len = queue_length(queue);
+ g_assert(len == 7);
+
+ for (i = 0; i < 7; i++)
+ g_assert(queue_pop_head(queue) == UINT_TO_PTR(i));
+
+ /* Test with identical elements */
+ g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
+ g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
+ g_assert(queue_push_head(queue, UINT_TO_PTR(1)));
+ g_assert(queue_push_after(queue, UINT_TO_PTR(1), UINT_TO_PTR(0)));
+
+ g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
+ g_assert(queue_pop_head(queue) == UINT_TO_PTR(0));
+ g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
+ g_assert(queue_pop_head(queue) == UINT_TO_PTR(1));
+}
+
int main(int argc, char *argv[])
{
g_test_init(&argc, &argv, NULL);
@@ -126,6 +177,7 @@ int main(int argc, char *argv[])
g_test_add_func("/queue/foreach_destroy", test_foreach_destroy);
g_test_add_func("/queue/foreach_remove_all", test_foreach_remove_all);
g_test_add_func("/queue/destroy_remove", test_destroy_remove);
+ g_test_add_func("/queue/push_after", test_push_after);

return g_test_run();
}
--
2.2.0.rc0.207.ga3a616c


2014-11-26 23:03:11

by Arman Uguray

[permalink] [raw]
Subject: [PATCH BlueZ v1 4/5] shared/gatt-db: Add gatt_db_insert_service function

This patch adds the gatt_db_insert_service function which inserts a
service with a pre-defined handle into the database if the desired
handle range is available.

This is primarily meant to be used by gatt-client, since the handles
in a client database need to perfectly match those returned from a
remote server. This also allows client to handle "Service Changed"
events in which a newly found service can be inserted at the correct
location within the database.
---
src/shared/gatt-db.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++-----
src/shared/gatt-db.h | 6 +++
2 files changed, 118 insertions(+), 10 deletions(-)

diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
index fe05595..0c24904 100644
--- a/src/shared/gatt-db.c
+++ b/src/shared/gatt-db.c
@@ -30,6 +30,10 @@
#include "src/shared/timeout.h"
#include "src/shared/gatt-db.h"

+#ifndef MAX
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+#endif
+
#define MAX_CHAR_DECL_VALUE_LEN 19
#define MAX_INCLUDED_VALUE_LEN 6
#define ATTRIBUTE_TIMEOUT 1000
@@ -216,27 +220,26 @@ static bool le_to_uuid(const uint8_t *src, size_t len, bt_uuid_t *uuid)
return true;
}

-struct gatt_db_attribute *gatt_db_add_service(struct gatt_db *db,
- const bt_uuid_t *uuid,
- bool primary,
- uint16_t num_handles)
+static struct gatt_db_service *gatt_db_service_create(const bt_uuid_t *uuid,
+ bool primary,
+ uint16_t num_handles)
{
struct gatt_db_service *service;
const bt_uuid_t *type;
uint8_t value[16];
uint16_t len;

- if (num_handles < 1 || (num_handles + db->next_handle) > UINT16_MAX)
- return 0;
+ if (num_handles < 1)
+ return NULL;

service = new0(struct gatt_db_service, 1);
if (!service)
- return 0;
+ return NULL;

service->attributes = new0(struct gatt_db_attribute *, num_handles);
if (!service->attributes) {
free(service);
- return 0;
+ return NULL;
}

if (primary)
@@ -249,12 +252,29 @@ struct gatt_db_attribute *gatt_db_add_service(struct gatt_db *db,
service->attributes[0] = new_attribute(service, type, value, len);
if (!service->attributes[0]) {
gatt_db_service_destroy(service);
- return 0;
+ return NULL;
}

+ return service;
+}
+
+struct gatt_db_attribute *gatt_db_add_service(struct gatt_db *db,
+ const bt_uuid_t *uuid,
+ bool primary,
+ uint16_t num_handles)
+{
+ struct gatt_db_service *service;
+
+ if (!db || (num_handles + db->next_handle) > UINT16_MAX)
+ return NULL;
+
+ service = gatt_db_service_create(uuid, primary, num_handles);
+ if (!service)
+ return NULL;
+
if (!queue_push_tail(db->services, service)) {
gatt_db_service_destroy(service);
- return 0;
+ return NULL;
}

/* TODO now we get next handle from database. We should first look
@@ -285,6 +305,88 @@ bool gatt_db_remove_service(struct gatt_db *db,
return true;
}

+struct insert_loc_data {
+ struct gatt_db_service *cur;
+ uint16_t start, end;
+ bool fail;
+ bool done;
+};
+
+static void search_for_insert_loc(void *data, void *user_data)
+{
+ struct insert_loc_data *loc_data = user_data;
+ struct gatt_db_service *service = data;
+ uint16_t cur_start, cur_end;
+
+ if (loc_data->done)
+ return;
+
+ cur_start = service->attributes[0]->handle;
+ cur_end = service->attributes[0]->handle + service->num_handles - 1;
+
+ /* Abort if the requested range overlaps with an existing service. */
+ if ((loc_data->start >= cur_start && loc_data->start <= cur_end) ||
+ (loc_data->end >= cur_start && loc_data->end <= cur_end)) {
+ loc_data->fail = true;
+ loc_data->done = true;
+ return;
+ }
+
+ /* Check if this is where the service should be inserted. */
+ if (loc_data->end < cur_start) {
+ loc_data->done = true;
+ return;
+ }
+
+ loc_data->cur = service;
+}
+
+struct gatt_db_attribute *gatt_db_insert_service(struct gatt_db *db,
+ uint16_t handle,
+ const bt_uuid_t *uuid,
+ bool primary,
+ uint16_t num_handles)
+{
+ struct insert_loc_data data;
+ struct gatt_db_service *service;
+
+ if (!db || num_handles < 1 || handle + num_handles > UINT16_MAX)
+ return NULL;
+
+ memset(&data, 0, sizeof(data));
+
+ data.start = handle;
+ data.end = handle + num_handles - 1;
+
+ queue_foreach(db->services, search_for_insert_loc, &data);
+
+ if (data.fail)
+ return NULL;
+
+ service = gatt_db_service_create(uuid, primary, num_handles);
+ if (!service)
+ return NULL;
+
+ if (data.cur) {
+ if (!queue_push_after(db->services, data.cur, service))
+ goto fail;
+ } else if (!queue_push_head(db->services, service)) {
+ goto fail;
+ }
+
+ service->attributes[0]->handle = handle;
+ service->num_handles = num_handles;
+
+ /* Fast-forward next_handle if the new service was added to the end */
+ db->next_handle = MAX(handle + num_handles + 1, db->next_handle);
+
+ return service->attributes[0];
+
+fail:
+ gatt_db_service_destroy(service);
+ return NULL;
+}
+
static uint16_t get_attribute_index(struct gatt_db_service *service,
int end_offset)
{
diff --git a/src/shared/gatt-db.h b/src/shared/gatt-db.h
index c943ad2..c9e3b59 100644
--- a/src/shared/gatt-db.h
+++ b/src/shared/gatt-db.h
@@ -35,6 +35,12 @@ struct gatt_db_attribute *gatt_db_add_service(struct gatt_db *db,
bool gatt_db_remove_service(struct gatt_db *db,
struct gatt_db_attribute *attrib);

+struct gatt_db_attribute *gatt_db_insert_service(struct gatt_db *db,
+ uint16_t handle,
+ const bt_uuid_t *uuid,
+ bool primary,
+ uint16_t num_handles);
+
typedef void (*gatt_db_read_t) (struct gatt_db_attribute *attrib,
unsigned int id, uint16_t offset,
uint8_t opcode, bdaddr_t *bdaddr,
--
2.2.0.rc0.207.ga3a616c


2014-11-26 23:03:09

by Arman Uguray

[permalink] [raw]
Subject: [PATCH BlueZ v1 2/5] shared: Add function to insert element after entry

This patch adds queue_insert_after, which inserts the given "data"
after the first occurence of element "entry" in a queue.
---
src/shared/queue.c | 35 +++++++++++++++++++++++++++++++++++
src/shared/queue.h | 1 +
2 files changed, 36 insertions(+)

diff --git a/src/shared/queue.c b/src/shared/queue.c
index 5329a80..9efb665 100644
--- a/src/shared/queue.c
+++ b/src/shared/queue.c
@@ -134,6 +134,41 @@ bool queue_push_head(struct queue *queue, void *data)
return true;
}

+bool queue_push_after(struct queue *queue, void *entry, void *data)
+{
+ struct queue_entry *qentry, *tmp, *new_entry;
+
+ qentry = NULL;
+
+ if (!queue)
+ return false;
+
+ for (tmp = queue->head; tmp; tmp = tmp->next) {
+ if (tmp->data == entry) {
+ qentry = tmp;
+ break;
+ }
+ }
+
+ if (!qentry)
+ return false;
+
+ new_entry = new0(struct queue_entry, 1);
+ if (!new_entry)
+ return false;
+
+ new_entry->data = data;
+ new_entry->next = qentry->next;
+
+ if (!qentry->next)
+ queue->tail = new_entry;
+
+ qentry->next = new_entry;
+ queue->entries++;
+
+ return true;
+}
+
void *queue_pop_head(struct queue *queue)
{
struct queue_entry *entry;
diff --git a/src/shared/queue.h b/src/shared/queue.h
index 709590b..602b0ce 100644
--- a/src/shared/queue.h
+++ b/src/shared/queue.h
@@ -32,6 +32,7 @@ void queue_destroy(struct queue *queue, queue_destroy_func_t destroy);

bool queue_push_tail(struct queue *queue, void *data);
bool queue_push_head(struct queue *queue, void *data);
+bool queue_push_after(struct queue *queue, void *entry, void *data);
void *queue_pop_head(struct queue *queue);
void *queue_peek_head(struct queue *queue);
void *queue_peek_tail(struct queue *queue);
--
2.2.0.rc0.207.ga3a616c


2014-11-26 23:03:08

by Arman Uguray

[permalink] [raw]
Subject: [PATCH BlueZ v1 1/5] shared/gatt-db: Add high-level functions for client

This patch introduces foreach functions to gatt-db for enumerating
service, characteristic, and decriptors stored in the database as
gatt_db_attribute pointers. This patch also adds functions for
extracting service, characteristic, and include declaration data out of
matching attributes.

This is in preparation for using gatt-db as the attribute cache in
shared/gatt-client.
---
src/shared/gatt-db.c | 249 +++++++++++++++++++++++++++++++++++++++++++++++++++
src/shared/gatt-db.h | 42 +++++++++
2 files changed, 291 insertions(+)

diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
index ab08c69..fe05595 100644
--- a/src/shared/gatt-db.c
+++ b/src/shared/gatt-db.c
@@ -193,6 +193,29 @@ static int uuid_to_le(const bt_uuid_t *uuid, uint8_t *dst)
return bt_uuid_len(&uuid128);
}

+static bool le_to_uuid(const uint8_t *src, size_t len, bt_uuid_t *uuid)
+{
+ uint128_t u128;
+
+ if (len == 2) {
+ bt_uuid16_create(uuid, get_le16(src));
+ return true;
+ }
+
+ if (len == 4) {
+ bt_uuid32_create(uuid, get_le32(src));
+ return true;
+ }
+
+ if (len != 16)
+ return false;
+
+ bswap_128(src, &u128);
+ bt_uuid128_create(uuid, u128);
+
+ return true;
+}
+
struct gatt_db_attribute *gatt_db_add_service(struct gatt_db *db,
const bt_uuid_t *uuid,
bool primary,
@@ -665,6 +688,125 @@ void gatt_db_find_information(struct gatt_db *db, uint16_t start_handle,
queue_foreach(db->services, find_information, &data);
}

+void gatt_db_foreach_service(struct gatt_db *db, gatt_db_foreach_t func,
+ void *user_data)
+{
+ gatt_db_foreach_service_in_range(db, func, user_data, 0x0001, 0xffff);
+}
+
+struct foreach_data {
+ gatt_db_foreach_t func;
+ void *user_data;
+ uint16_t start, end;
+};
+
+static void foreach_service_in_range(void *data, void *user_data)
+{
+ struct gatt_db_service *service = data;
+ struct foreach_data *foreach_data = user_data;
+ uint16_t svc_start;
+
+ svc_start = get_handle_at_index(service, 0);
+
+ if (svc_start > foreach_data->end || svc_start < foreach_data->start)
+ return;
+
+ foreach_data->func(service->attributes[0], foreach_data->user_data);
+}
+
+void gatt_db_foreach_service_in_range(struct gatt_db *db,
+ gatt_db_foreach_t func,
+ void *user_data,
+ uint16_t start_handle,
+ uint16_t end_handle)
+{
+ struct foreach_data data;
+
+ if (!db || !func || start_handle > end_handle)
+ return;
+
+ data.func = func;
+ data.user_data = user_data;
+ data.start = start_handle;
+ data.end = end_handle;
+
+ queue_foreach(db->services, foreach_service_in_range, &data);
+}
+
+void gatt_db_service_foreach(struct gatt_db_attribute *attrib,
+ const bt_uuid_t *uuid,
+ gatt_db_foreach_t func,
+ void *user_data)
+{
+ struct gatt_db_service *service;
+ struct gatt_db_attribute *attr;
+ uint16_t i;
+
+ if (!attrib || !func)
+ return;
+
+ service = attrib->service;
+
+ for (i = 0; i < service->num_handles; i++) {
+ attr = service->attributes[i];
+ if (!attr)
+ continue;
+
+ if (uuid && bt_uuid_cmp(uuid, &attr->uuid))
+ continue;
+
+ func(attr, user_data);
+ }
+}
+
+void gatt_db_service_foreach_char(struct gatt_db_attribute *attrib,
+ gatt_db_foreach_t func,
+ void *user_data)
+{
+ gatt_db_service_foreach(attrib, &characteristic_uuid, func, user_data);
+}
+
+void gatt_db_service_foreach_desc(struct gatt_db_attribute *attrib,
+ gatt_db_foreach_t func,
+ void *user_data)
+{
+ struct gatt_db_service *service;
+ struct gatt_db_attribute *attr;
+ uint16_t i;
+
+ if (!attrib || !func)
+ return;
+
+ /* Return if this attribute is not a characteristic declaration */
+ if (bt_uuid_cmp(&characteristic_uuid, &attrib->uuid))
+ return;
+
+ service = attrib->service;
+
+ /* Start from the attribute following the value handle */
+ i = attrib->handle - service->attributes[0]->handle + 2;
+ for (; i < service->num_handles; i++) {
+ attr = service->attributes[i];
+ if (!attr)
+ continue;
+
+ /* Return if we reached the end of this characteristic */
+ if (!bt_uuid_cmp(&characteristic_uuid, &attr->uuid) ||
+ !bt_uuid_cmp(&included_service_uuid, &attr->uuid))
+ return;
+
+ func(attr, user_data);
+ }
+}
+
+void gatt_db_service_foreach_incl(struct gatt_db_attribute *attrib,
+ gatt_db_foreach_t func,
+ void *user_data)
+{
+ gatt_db_service_foreach(attrib, &included_service_uuid, func,
+ user_data);
+}
+
static bool find_service_for_handle(const void *data, const void *user_data)
{
const struct gatt_db_service *service = data;
@@ -769,6 +911,113 @@ bool gatt_db_attribute_get_service_handles(struct gatt_db_attribute *attrib,
return true;
}

+bool gatt_db_attribute_get_service_data(struct gatt_db_attribute *attrib,
+ uint16_t *start_handle,
+ uint16_t *end_handle,
+ bool *primary,
+ bt_uuid_t *uuid)
+{
+ struct gatt_db_service *service;
+ struct gatt_db_attribute *decl;
+
+ if (!attrib)
+ return false;
+
+ service = attrib->service;
+ decl = service->attributes[0];
+
+ if (start_handle)
+ *start_handle = decl->handle;
+
+ if (end_handle)
+ *end_handle = decl->handle + service->num_handles - 1;
+
+ if (primary)
+ *primary = bt_uuid_cmp(&decl->uuid, &secondary_service_uuid);
+
+ if (!uuid)
+ return true;
+
+ /*
+ * The service declaration attribute value is the 16 or 128 bit service
+ * UUID.
+ */
+ return le_to_uuid(decl->value, decl->value_len, uuid);
+}
+
+bool gatt_db_attribute_get_char_data(struct gatt_db_attribute *attrib,
+ uint16_t *handle,
+ uint16_t *value_handle,
+ uint8_t *properties,
+ bt_uuid_t *uuid)
+{
+ if (!attrib)
+ return false;
+
+ if (bt_uuid_cmp(&characteristic_uuid, &attrib->uuid))
+ return false;
+
+ /*
+ * Characteristic declaration value:
+ * 1 octet: Characteristic properties
+ * 2 octets: Characteristic value handle
+ * 2 or 16 octets: characteristic UUID
+ */
+ if (!attrib->value || (attrib->value_len != 5 &&
+ attrib->value_len != 19))
+ return false;
+
+ if (handle)
+ *handle = attrib->handle;
+
+ if (properties)
+ *properties = attrib->value[0];
+
+ if (value_handle)
+ *value_handle = get_le16(attrib->value + 1);
+
+ if (!uuid)
+ return true;
+
+ return le_to_uuid(attrib->value + 3, attrib->value_len - 3, uuid);
+}
+
+bool gatt_db_attribute_get_incl_data(struct gatt_db_attribute *attrib,
+ uint16_t *handle,
+ uint16_t *start_handle,
+ uint16_t *end_handle)
+{
+ if (!attrib)
+ return false;
+
+ if (bt_uuid_cmp(&included_service_uuid, &attrib->uuid))
+ return false;
+
+ /*
+ * Include definition value:
+ * 2 octets: start handle of included service
+ * 2 octets: end handle of included service
+ * optional 2 octets: 16-bit Bluetooth UUID
+ */
+ if (!attrib->value || attrib->value_len < 4 || attrib->value_len > 6)
+ return false;
+
+ /*
+ * We only return the handles since the UUID can be easily obtained
+ * from the corresponding attribute.
+ */
+ if (handle)
+ *handle = attrib->handle;
+
+ if (start_handle)
+ *start_handle = get_le16(attrib->value);
+
+ if (end_handle)
+ *end_handle = get_le16(attrib->value + 2);
+
+ return true;
+}
+
bool gatt_db_attribute_get_permissions(struct gatt_db_attribute *attrib,
uint32_t *permissions)
{
diff --git a/src/shared/gatt-db.h b/src/shared/gatt-db.h
index 9c71814..c943ad2 100644
--- a/src/shared/gatt-db.h
+++ b/src/shared/gatt-db.h
@@ -89,6 +89,31 @@ void gatt_db_find_information(struct gatt_db *db, uint16_t start_handle,
struct queue *queue);


+typedef void (*gatt_db_foreach_t)(struct gatt_db_attribute *attrib,
+ void *user_data);
+void gatt_db_foreach_service(struct gatt_db *db, gatt_db_foreach_t func,
+ void *user_data);
+void gatt_db_foreach_service_in_range(struct gatt_db *db,
+ gatt_db_foreach_t func,
+ void *user_data,
+ uint16_t start_handle,
+ uint16_t end_handle);
+
+void gatt_db_service_foreach(struct gatt_db_attribute *attrib,
+ const bt_uuid_t *uuid,
+ gatt_db_foreach_t func,
+ void *user_data);
+void gatt_db_service_foreach_char(struct gatt_db_attribute *attrib,
+ gatt_db_foreach_t func,
+ void *user_data);
+void gatt_db_service_foreach_desc(struct gatt_db_attribute *attrib,
+ gatt_db_foreach_t func,
+ void *user_data);
+void gatt_db_service_foreach_incl(struct gatt_db_attribute *attrib,
+ gatt_db_foreach_t func,
+ void *user_data);
+
+
struct gatt_db_attribute *gatt_db_get_attribute(struct gatt_db *db,
uint16_t handle);

@@ -103,6 +128,23 @@ bool gatt_db_attribute_get_service_handles(struct gatt_db_attribute *attrib,
uint16_t *start_handle,
uint16_t *end_handle);

+bool gatt_db_attribute_get_service_data(struct gatt_db_attribute *attrib,
+ uint16_t *start_handle,
+ uint16_t *end_handle,
+ bool *primary,
+ bt_uuid_t *uuid);
+
+bool gatt_db_attribute_get_char_data(struct gatt_db_attribute *attrib,
+ uint16_t *handle,
+ uint16_t *value_handle,
+ uint8_t *properties,
+ bt_uuid_t *uuid);
+
+bool gatt_db_attribute_get_incl_data(struct gatt_db_attribute *attrib,
+ uint16_t *handle,
+ uint16_t *start_handle,
+ uint16_t *end_handle);
+
bool gatt_db_attribute_get_permissions(struct gatt_db_attribute *attrib,
uint32_t *permissions);

--
2.2.0.rc0.207.ga3a616c