From d566c3314bf64dce10648e2b95c11b23b32b3135 Mon Sep 17 00:00:00 2001 From: Louis Caron Date: Fri, 11 Dec 2015 09:48:57 +0100 Subject: [PATCH] Bluetooth: add CUD descriptor support In order to support more standard descriptors, the UD macro and helper function are added. Change-Id: Ic9a82658668107b40abc5c98002fe5091dc10f08 Signed-off-by: Louis Caron --- include/bluetooth/gatt.h | 41 ++++++++++++++++++++++++++++++++++------ net/bluetooth/gatt.c | 9 +++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/include/bluetooth/gatt.h b/include/bluetooth/gatt.h index 76ef0e16d3c..58c6dbe2fb6 100644 --- a/include/bluetooth/gatt.h +++ b/include/bluetooth/gatt.h @@ -191,12 +191,6 @@ struct bt_gatt_cep { uint16_t properties; }; -/** @brief Characteristic User Description Attribute Value. */ -struct bt_gatt_cud { - /** Characteristic User Description string. */ - char *string; -}; - /* Client Characteristic Configuration Values */ /** @def BT_GATT_CCC_NOTIFY @@ -523,6 +517,41 @@ int bt_gatt_attr_read_cep(struct bt_conn *conn, .user_data = _value, \ } +/** @brief Read Characteristic User Description Descriptor Attribute helper + * + * Read CUD attribute value storing the result into buffer after + * encoding it. + * NOTE: Only use this with attributes which user_data is a NULL-terminated C string. + * + * @param conn Connection object + * @param attr Attribute to read + * @param buf Buffer to store the value read + * @param len Buffer length + * @param offset Start offset + * + * @return number of bytes read in case of success or negative values in + * case of error. + */ +int bt_gatt_attr_read_cud(struct bt_conn *conn, + const struct bt_gatt_attr *attr, void *buf, + uint16_t len, uint16_t offset); + +/** @def BT_GATT_CUD + * @brief Characteristic User Format Descriptor Declaration Macro. + * + * Helper macro to declare a CUD attribute. + * + * @param _value User description NULL-terminated C string. + * @param _perm Descriptor attribute access permissions. + */ +#define BT_GATT_CUD(_value, _perm) \ +{ \ + .uuid = BT_UUID_GATT_CUD, \ + .perm = _perm, \ + .read = bt_gatt_attr_read_cud, \ + .user_data = _value, \ +} + /** @def BT_GATT_DESCRIPTOR * @brief Descriptor Declaration Macro. * diff --git a/net/bluetooth/gatt.c b/net/bluetooth/gatt.c index f04e5dbcde0..6bf0b0c1ab0 100644 --- a/net/bluetooth/gatt.c +++ b/net/bluetooth/gatt.c @@ -349,6 +349,15 @@ int bt_gatt_attr_read_cep(struct bt_conn *conn, sizeof(props)); } +int bt_gatt_attr_read_cud(struct bt_conn *conn, + const struct bt_gatt_attr *attr, void *buf, + uint16_t len, uint16_t offset) +{ + char *value = attr->user_data; + + return bt_gatt_attr_read(conn, attr, buf, len, offset, value, strlen(value)); +} + struct notify_data { const void *data; size_t len;