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 <luiz.von.dentz@intel.com>
This commit is contained in:
Luiz Augusto von Dentz 2015-05-19 16:52:27 +03:00 committed by Anas Nashif
commit dd41c3375c
2 changed files with 45 additions and 0 deletions

View file

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

View file

@ -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;
}
}