From dd41c3375c3da15b77bbdd7049e2f16ffbd1883f Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Tue, 19 May 2015 16:52:27 +0300 Subject: [PATCH] Bluetooth: Add bt_gatt_foreach_attr bt_gatt_foreach_attr iterates over attributes via callback, it will be used to implement discovery functionality. Change-Id: Ice66f4b3d0d12b3616986931d5da0fe0f202e533 Signed-off-by: Luiz Augusto von Dentz --- include/bluetooth/gatt.h | 28 ++++++++++++++++++++++++++++ net/bluetooth/gatt.c | 17 +++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/include/bluetooth/gatt.h b/include/bluetooth/gatt.h index 3afd32fe6c7..c6af15319e3 100644 --- a/include/bluetooth/gatt.h +++ b/include/bluetooth/gatt.h @@ -110,6 +110,34 @@ struct bt_gatt_ccc { */ void bt_gatt_register(const struct bt_gatt_attr *attrs, size_t count); +enum { + BT_GATT_ITER_STOP = 0, + BT_GATT_ITER_CONTINUE, +}; + +/** @brief Attribute iterator callback + * + * @param attr attribute found + * @param user_data data given + * + * @return BT_GATT_ITER_CONTINUE if should continue to the next attribute + * or BT_GATT_ITER_STOP to stop. + */ +typedef uint8_t (*bt_gatt_attr_func_t)(const struct bt_gatt_attr *attr, + void *user_data); + +/** @brief Attribute iterator + * + * Iterate attributes in the given range. + * + * @param start_handle uint16_t start handle + * @param end_handle uint16_t end handle + * @param func bt_gatt_attr_func_t callback function + * @param user_data void * data to pass to the callback + */ +void bt_gatt_foreach_attr(uint16_t start_handle, uint16_t end_handle, + bt_gatt_attr_func_t func, void *user_data); + /** @brief Generic Read Attribute value helper * * Read attribute value storing the result into buffer. diff --git a/net/bluetooth/gatt.c b/net/bluetooth/gatt.c index 944822d2e01..b4268986d16 100644 --- a/net/bluetooth/gatt.c +++ b/net/bluetooth/gatt.c @@ -148,3 +148,20 @@ int bt_gatt_attr_read_chrc(const struct bt_gatt_attr *attr, void *buf, return bt_gatt_attr_read(attr, buf, len, offset, &pdu, value_len); } + +void bt_gatt_foreach_attr(uint16_t start_handle, uint16_t end_handle, + bt_gatt_attr_func_t func, void *user_data) +{ + size_t i; + + for (i = 0; i < attr_count; i++) { + const struct bt_gatt_attr *attr = &db[i]; + + /* Check if attribute handle is within range */ + if (attr->handle < start_handle || attr->handle > end_handle) + continue; + + if (func(attr, user_data) == BT_GATT_ITER_STOP) + break; + } +}