diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
index db1fdff..8675dcc 100644
--- a/src/shared/gatt-db.c
+++ b/src/shared/gatt-db.c
uint16_t handle;
bt_uuid_t uuid;
uint8_t permissions;
+ uint16_t value_len;
+ uint8_t *value;
+
gatt_db_read_t read_func;
gatt_db_write_t write_func;
void *user_data;
- uint16_t val_len;
- uint8_t value[0];
};
struct gatt_db_service {
return service->attributes[0]->handle == PTR_TO_INT(user_data);
}
+static struct gatt_db_attribute *new_attribute(const bt_uuid_t *type,
+ const uint8_t *val,
+ uint16_t len)
+{
+ struct gatt_db_attribute *attribute;
+
+ attribute = new0(struct gatt_db_attribute, 1);
+ if (!attribute)
+ return NULL;
+
+ attribute->uuid = *type;
+ attribute->value_len = len;
+ if (len) {
+ attribute->value = malloc0(len);
+ if (!attribute->value) {
+ free(attribute);
+ return NULL;
+ }
+
+ memcpy(attribute->value, val, len);
+ }
+
+ return attribute;
+}
+
+static void attribute_destroy(void *data)
+{
+ struct gatt_db_attribute *attribute = data;
+
+ free(attribute->value);
+ free(attribute);
+}
+
struct gatt_db *gatt_db_new(void)
{
struct gatt_db *db;
int i;
for (i = 0; i < service->num_handles; i++)
- free(service->attributes[i]);
+ attribute_destroy(service->attributes[i]);
free(service->attributes);
free(service);
free(db);
}
-static struct gatt_db_attribute *new_attribute(const bt_uuid_t *type,
- const uint8_t *val,
- uint16_t len)
-{
- struct gatt_db_attribute *attribute;
-
- attribute = malloc0(sizeof(struct gatt_db_attribute) + len);
- if (!attribute)
- return NULL;
-
- attribute->uuid = *type;
- attribute->val_len = len;
- if (len)
- memcpy(&attribute->value, val, len);
-
- return attribute;
-}
-
static int uuid_to_le(const bt_uuid_t *uuid, uint8_t *dst)
{
bt_uuid_t uuid128;
/* The Service UUID shall only be present when the UUID is a 16-bit
* Bluetooth UUID. Vol 2. Part G. 3.2
*/
- if (included_service->attributes[0]->val_len == sizeof(uint16_t)) {
+ if (included_service->attributes[0]->value_len == sizeof(uint16_t)) {
memcpy(&value[len], included_service->attributes[0]->value,
- included_service->attributes[0]->val_len);
- len += included_service->attributes[0]->val_len;
+ included_service->attributes[0]->value_len);
+ len += included_service->attributes[0]->value_len;
}
index = get_attribute_index(service, 0);