From ea1534f1009fa5193df2b3e8a7dbee44eb8dc6fa Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Fri, 15 Jan 2016 08:33:13 -0300 Subject: [PATCH] Bluetooth: GATT: Add bt_gatt_attr_next function This add bt_gatt_attr_next function that can be used to iterate to next attribute which is convenient if original attribute is known and using bt_gatt_foreach_attr would require a lookup and another function to pass as callback. Change-Id: I1bd522fd4ae784e08aa375b35320191cbfc03a54 Signed-off-by: Luiz Augusto von Dentz --- include/bluetooth/gatt.h | 10 ++++++++++ net/bluetooth/gatt.c | 22 ++++++++++++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/include/bluetooth/gatt.h b/include/bluetooth/gatt.h index e44eb95e71e..f0e4c81f36b 100644 --- a/include/bluetooth/gatt.h +++ b/include/bluetooth/gatt.h @@ -280,6 +280,16 @@ typedef uint8_t (*bt_gatt_attr_func_t)(const struct bt_gatt_attr *attr, void bt_gatt_foreach_attr(uint16_t start_handle, uint16_t end_handle, bt_gatt_attr_func_t func, void *user_data); +/** @brief Iterate to the next attribute + * + * Iterate to the next attribute following a given attribute. + * + * @param attr Current Attribute. + * + * @return The next attribute or NULL if it cannot be found. + */ +struct bt_gatt_attr *bt_gatt_attr_next(const struct bt_gatt_attr *attr); + /** @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 a830ea9abf8..0ddb1d8a0f6 100644 --- a/net/bluetooth/gatt.c +++ b/net/bluetooth/gatt.c @@ -51,12 +51,8 @@ static struct bt_gatt_attr *db; static struct bt_gatt_subscribe_params *subscriptions; #endif /* CONFIG_BLUETOOTH_GATT_CLIENT */ -#if defined(CONFIG_BLUETOOTH_GATT_DYNAMIC_DB) -#define BT_GATT_ATTR_NEXT(attr) (attr->_next) -#else +#if !defined(CONFIG_BLUETOOTH_GATT_DYNAMIC_DB) static size_t attr_count; -#define BT_GATT_ATTR_NEXT(attr) \ - ((attr < db || attr > &db[attr_count - 2]) ? NULL : &attr[1]) #endif /* CONFIG_BLUETOOTH_GATT_DYNAMIC_DB */ int bt_gatt_register(struct bt_gatt_attr *attrs, size_t count) @@ -117,7 +113,7 @@ populate: #endif BT_DBG("attr %p next %p handle 0x%04x uuid %s perm 0x%02x", - attrs, BT_GATT_ATTR_NEXT(attrs), attrs->handle, + attrs, bt_gatt_attr_next(attrs), attrs->handle, bt_uuid_str(attrs->uuid), attrs->perm); } @@ -218,7 +214,7 @@ int bt_gatt_attr_read_chrc(struct bt_conn *conn, * declaration. All characteristic definitions shall have a * Characteristic Value declaration. */ - next = BT_GATT_ATTR_NEXT(attr); + next = bt_gatt_attr_next(attr); if (!next) { BT_WARN("No value for characteristic at 0x%04x", attr->handle); pdu.value_handle = 0x0000; @@ -243,7 +239,7 @@ void bt_gatt_foreach_attr(uint16_t start_handle, uint16_t end_handle, { const struct bt_gatt_attr *attr; - for (attr = db; attr; attr = BT_GATT_ATTR_NEXT(attr)) { + for (attr = db; attr; attr = bt_gatt_attr_next(attr)) { /* Check if attribute handle is within range */ if (attr->handle < start_handle || attr->handle > end_handle) { continue; @@ -255,6 +251,16 @@ void bt_gatt_foreach_attr(uint16_t start_handle, uint16_t end_handle, } } +struct bt_gatt_attr *bt_gatt_attr_next(const struct bt_gatt_attr *attr) +{ +#if defined(CONFIG_BLUETOOTH_GATT_DYNAMIC_DB) + return attr->_next; +#else + return ((attr < db || attr > &db[attr_count - 2]) ? NULL : + (struct bt_gatt_attr *) &attr[1]); +#endif /* CONFIG_BLUETOOTH_GATT_DYNAMIC_DB */ +} + int bt_gatt_attr_read_ccc(struct bt_conn *conn, const struct bt_gatt_attr *attr, void *buf, uint16_t len, uint16_t offset)