Return-Path: From: Claudio Takahasi To: linux-bluetooth@vger.kernel.org Cc: claudio.takahasi@openbossa.org, Andre Guedes Subject: [PATCH BlueZ v1 02/15] gatt: Add helper for creating GATT characteristics Date: Wed, 12 Mar 2014 13:26:58 -0300 Message-Id: <1394641631-1534-3-git-send-email-claudio.takahasi@openbossa.org> In-Reply-To: <1394641631-1534-1-git-send-email-claudio.takahasi@openbossa.org> References: <1394477445-8987-1-git-send-email-claudio.takahasi@openbossa.org> <1394641631-1534-1-git-send-email-claudio.takahasi@openbossa.org> Sender: linux-bluetooth-owner@vger.kernel.org List-ID: From: Andre Guedes This patch adds btd_gatt_add_char() helper. It creates and adds the Characteristic declaration, and Characteristic value attributes to the local attribute database. --- src/gatt.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/gatt.h | 12 +++++++++ 2 files changed, 98 insertions(+) diff --git a/src/gatt.c b/src/gatt.c index eed4fae..669c716 100644 --- a/src/gatt.c +++ b/src/gatt.c @@ -40,6 +40,9 @@ static const bt_uuid_t primary_uuid = { .type = BT_UUID16, .value.u16 = GATT_PRIM_SVC_UUID }; +static const bt_uuid_t chr_uuid = { .type = BT_UUID16, + .value.u16 = GATT_CHARAC_UUID }; + struct btd_attribute { uint16_t handle; bt_uuid_t type; @@ -50,6 +53,11 @@ struct btd_attribute { static GList *local_attribute_db; static uint16_t next_handle = 0x0001; +static inline void put_le16(uint16_t val, void *dst) +{ + put_unaligned(cpu_to_le16(val), (uint16_t *) dst); +} + static inline void put_uuid(const bt_uuid_t *src, void *dst) { if (src->type == BT_UUID16) @@ -120,6 +128,84 @@ struct btd_attribute *btd_gatt_add_service(const bt_uuid_t *uuid) return attr; } +struct btd_attribute *btd_gatt_add_char(const bt_uuid_t *uuid, + uint8_t properties) +{ + struct btd_attribute *char_decl, *char_value = NULL; + + /* Attribute value length */ + uint16_t len = 1 + 2 + bt_uuid_len(uuid); + uint8_t value[len]; + + /* + * Characteristic DECLARATION + * + * TYPE ATTRIBUTE VALUE + * +-------+---------------------------------+ + * |0x2803 | 0xXX 0xYYYY 0xZZZZ... | + * | (1) | (2) (3) (4) | + * +------+----------------------------------+ + * (1) - 2 octets: Characteristic declaration UUID + * (2) - 1 octet : Properties + * (3) - 2 octets: Handle of the characteristic Value + * (4) - 2 or 16 octets: Characteristic UUID + */ + + value[0] = properties; + + /* + * Since we don't know yet the characteristic value attribute + * handle, we skip and set it later. + */ + + put_uuid(uuid, &value[3]); + + char_decl = new_const_attribute(&chr_uuid, value, len); + if (local_database_add(next_handle, char_decl) < 0) + goto fail; + + next_handle = next_handle + 1; + + /* + * Characteristic VALUE + * + * TYPE ATTRIBUTE VALUE + * +----------+---------------------------------+ + * |0xZZZZ... | 0x... | + * | (1) | (2) | + * +----------+---------------------------------+ + * (1) - 2 or 16 octets: Characteristic UUID + * (2) - N octets: Value is read dynamically from the service + * implementation (external entity). + */ + + char_value = new0(struct btd_attribute, 1); + char_value->type = *uuid; + + /* TODO: Read & Write callbacks */ + + if (local_database_add(next_handle, char_value) < 0) + goto fail; + + next_handle = next_handle + 1; + + /* + * Update characteristic value handle in characteristic declaration + * attribute. For local attributes, we can assume that the handle + * representing the characteristic value will get the next available + * handle. However, for remote attribute this assumption is not valid. + */ + put_le16(char_value->handle, &char_decl->value[1]); + + return char_value; + +fail: + free(char_decl); + free(char_value); + + return NULL; +} + void gatt_init(void) { DBG("Starting GATT server"); diff --git a/src/gatt.h b/src/gatt.h index 8dd1312..d7acc5b 100644 --- a/src/gatt.h +++ b/src/gatt.h @@ -34,3 +34,15 @@ void gatt_cleanup(void); * NULL is returned. */ struct btd_attribute *btd_gatt_add_service(const bt_uuid_t *uuid); + +/* + * btd_gatt_add_char - Add a characteristic (declaration and value attributes) + * to local attribute database. + * @uuid: Characteristic UUID (16-bits or 128-bits). + * @properties: Characteristic properties. See Core SPEC 4.1 page 2183. + * + * Returns a reference to characteristic value attribute. In case of error, + * NULL is returned. + */ +struct btd_attribute *btd_gatt_add_char(const bt_uuid_t *uuid, + uint8_t properties); -- 1.8.3.1