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 <luiz.von.dentz@intel.com>
This commit is contained in:
Luiz Augusto von Dentz 2016-01-15 08:33:13 -03:00 committed by Anas Nashif
parent d2d857bac0
commit ea1534f100
2 changed files with 24 additions and 8 deletions

View File

@ -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.

View File

@ -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)