linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCHv1 0/4] Gatt attributes database skeleton
@ 2014-04-16  7:19 Marcin Kraglak
  2014-04-16  7:19 ` [PATCHv1 1/4] gatt: Add skeleton of gatt-db Marcin Kraglak
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Marcin Kraglak @ 2014-04-16  7:19 UTC (permalink / raw)
  To: linux-bluetooth

This set introduce gatt database skeleton for storing local attributes.
Now functionality for creating, destroying database and adding/removing
services is provided.
In comparision to RFC version, comments are added in header file, and type
of sevice enum was replaced with bool "primary".

Comments are welcome

Marcin Kraglak (4):
  gatt: Add skeleton of gatt-db
  gatt: Add services list to gatt_db struct
  gatt: Add method for creating services in gatt_db
  gatt: Add remove_service function to gatt-db

 android/Android.mk   |   1 +
 android/Makefile.am  |   1 +
 src/shared/gatt-db.c | 189 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/shared/gatt-db.h |  63 +++++++++++++++++
 4 files changed, 254 insertions(+)
 create mode 100644 src/shared/gatt-db.c
 create mode 100644 src/shared/gatt-db.h

-- 
1.8.5.3


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCHv1 1/4] gatt: Add skeleton of gatt-db
  2014-04-16  7:19 [PATCHv1 0/4] Gatt attributes database skeleton Marcin Kraglak
@ 2014-04-16  7:19 ` Marcin Kraglak
  2014-04-24 10:40   ` Szymon Janc
  2014-04-16  7:19 ` [PATCHv1 2/4] gatt: Add services list to gatt_db struct Marcin Kraglak
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Marcin Kraglak @ 2014-04-16  7:19 UTC (permalink / raw)
  To: linux-bluetooth

This change adds new() and destroy() fuctions for gatt_db,
which will be used for storing local attributes.
---
 android/Android.mk   |  1 +
 android/Makefile.am  |  1 +
 src/shared/gatt-db.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 src/shared/gatt-db.h | 38 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 87 insertions(+)
 create mode 100644 src/shared/gatt-db.c
 create mode 100644 src/shared/gatt-db.h

diff --git a/android/Android.mk b/android/Android.mk
index 5f4e70c..4c9cfb5 100644
--- a/android/Android.mk
+++ b/android/Android.mk
@@ -51,6 +51,7 @@ LOCAL_SRC_FILES := \
 	bluez/src/shared/queue.c \
 	bluez/src/shared/ringbuf.c \
 	bluez/src/shared/hfp.c \
+	bluez/src/shared/gatt-db.c \
 	bluez/src/shared/io-glib.c \
 	bluez/src/sdpd-database.c \
 	bluez/src/sdpd-service.c \
diff --git a/android/Makefile.am b/android/Makefile.am
index c51cce2..70e1dec 100644
--- a/android/Makefile.am
+++ b/android/Makefile.am
@@ -29,6 +29,7 @@ android_bluetoothd_SOURCES = android/main.c \
 				src/shared/mgmt.h src/shared/mgmt.c \
 				src/shared/ringbuf.h src/shared/ringbuf.c \
 				src/shared/hfp.h src/shared/hfp.c \
+				src/shared/gatt-db.h src/shared/gatt-db.c \
 				android/bluetooth.h android/bluetooth.c \
 				android/hidhost.h android/hidhost.c \
 				android/ipc-common.h \
diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
new file mode 100644
index 0000000..e56b381
--- /dev/null
+++ b/src/shared/gatt-db.c
@@ -0,0 +1,47 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2014  Intel Corporation. All rights reserved.
+ *
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#include "src/shared/util.h"
+#include "src/shared/gatt-db.h"
+
+struct gatt_db {
+	uint16_t next_handle;
+};
+
+struct gatt_db *gatt_db_new(void)
+{
+	struct gatt_db *db;
+
+	db = new0(struct gatt_db, 1);
+	if (!db)
+		return NULL;
+
+	db->next_handle = 0x0001;
+
+	return db;
+}
+
+void gatt_db_destroy(struct gatt_db *db)
+{
+	free(db);
+}
diff --git a/src/shared/gatt-db.h b/src/shared/gatt-db.h
new file mode 100644
index 0000000..b3cd7a6
--- /dev/null
+++ b/src/shared/gatt-db.h
@@ -0,0 +1,38 @@
+/*
+ *
+ *  BlueZ - Bluetooth protocol stack for Linux
+ *
+ *  Copyright (C) 2014  Intel Corporation. All rights reserved.
+ *
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU Lesser General Public
+ *  License as published by the Free Software Foundation; either
+ *  version 2.1 of the License, or (at your option) any later version.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  Lesser General Public License for more details.
+ *
+ *  You should have received a copy of the GNU Lesser General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+struct gatt_db;
+
+/*
+ * gatt_db_new - Create new database for storing attributes
+ *
+ * Returns new database. In case of error NULL will be returned.
+ */
+struct gatt_db *gatt_db_new(void);
+
+/*
+ * gatt_db_destroy - Destroy gatt database
+ *
+ * @db - gatt database to be destroyed
+ */
+void gatt_db_destroy(struct gatt_db *db);
-- 
1.8.5.3


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCHv1 2/4] gatt: Add services list to gatt_db struct
  2014-04-16  7:19 [PATCHv1 0/4] Gatt attributes database skeleton Marcin Kraglak
  2014-04-16  7:19 ` [PATCHv1 1/4] gatt: Add skeleton of gatt-db Marcin Kraglak
@ 2014-04-16  7:19 ` Marcin Kraglak
  2014-04-16  7:19 ` [PATCHv1 3/4] gatt: Add method for creating services in gatt_db Marcin Kraglak
  2014-04-16  7:19 ` [PATCHv1 4/4] gatt: Add remove_service function to gatt-db Marcin Kraglak
  3 siblings, 0 replies; 7+ messages in thread
From: Marcin Kraglak @ 2014-04-16  7:19 UTC (permalink / raw)
  To: linux-bluetooth

Gatt database will store all services in queue services. Each service
contains attributes and number of handles in this service.
---
 src/shared/gatt-db.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
index e56b381..b057c15 100644
--- a/src/shared/gatt-db.c
+++ b/src/shared/gatt-db.c
@@ -21,11 +21,23 @@
  *
  */
 
+#include <stdbool.h>
+
 #include "src/shared/util.h"
+#include "src/shared/queue.h"
 #include "src/shared/gatt-db.h"
 
 struct gatt_db {
 	uint16_t next_handle;
+	struct queue *services;
+};
+
+struct gatt_db_attribute {
+};
+
+struct gatt_db_service {
+	uint16_t num_handles;
+	struct gatt_db_attribute **attributes;
 };
 
 struct gatt_db *gatt_db_new(void)
@@ -36,12 +48,31 @@ struct gatt_db *gatt_db_new(void)
 	if (!db)
 		return NULL;
 
+	db->services = queue_new();
+	if (!db->services) {
+		free(db);
+		return NULL;
+	}
+
 	db->next_handle = 0x0001;
 
 	return db;
 }
 
+static void gatt_db_service_destroy(void *data)
+{
+	struct gatt_db_service *service = data;
+	int i;
+
+	for (i = 0; i < service->num_handles; i++)
+		free(service->attributes[i]);
+
+	free(service->attributes);
+	free(service);
+}
+
 void gatt_db_destroy(struct gatt_db *db)
 {
+	queue_destroy(db->services, gatt_db_service_destroy);
 	free(db);
 }
-- 
1.8.5.3


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCHv1 3/4] gatt: Add method for creating services in gatt_db
  2014-04-16  7:19 [PATCHv1 0/4] Gatt attributes database skeleton Marcin Kraglak
  2014-04-16  7:19 ` [PATCHv1 1/4] gatt: Add skeleton of gatt-db Marcin Kraglak
  2014-04-16  7:19 ` [PATCHv1 2/4] gatt: Add services list to gatt_db struct Marcin Kraglak
@ 2014-04-16  7:19 ` Marcin Kraglak
  2014-04-24 10:41   ` Szymon Janc
  2014-04-16  7:19 ` [PATCHv1 4/4] gatt: Add remove_service function to gatt-db Marcin Kraglak
  3 siblings, 1 reply; 7+ messages in thread
From: Marcin Kraglak @ 2014-04-16  7:19 UTC (permalink / raw)
  To: linux-bluetooth

This function will reserve number of handles and create service attribute.
Primary arguments service type: PRIMARY or SECONDARY.
---
 src/shared/gatt-db.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/shared/gatt-db.h | 14 ++++++++
 2 files changed, 104 insertions(+)

diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
index b057c15..0094616 100644
--- a/src/shared/gatt-db.c
+++ b/src/shared/gatt-db.c
@@ -23,16 +23,26 @@
 
 #include <stdbool.h>
 
+#include "lib/uuid.h"
 #include "src/shared/util.h"
 #include "src/shared/queue.h"
 #include "src/shared/gatt-db.h"
 
+static const bt_uuid_t primary_service_uuid  = { .type = BT_UUID16,
+					.value.u16 = GATT_PRIM_SVC_UUID };
+static const bt_uuid_t secondary_service_uuid  = { .type = BT_UUID16,
+					.value.u16 = GATT_SND_SVC_UUID };
+
 struct gatt_db {
 	uint16_t next_handle;
 	struct queue *services;
 };
 
 struct gatt_db_attribute {
+	uint16_t handle;
+	bt_uuid_t uuid;
+	uint16_t val_len;
+	uint8_t value[0];
 };
 
 struct gatt_db_service {
@@ -76,3 +86,83 @@ void gatt_db_destroy(struct gatt_db *db)
 	queue_destroy(db->services, gatt_db_service_destroy);
 	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;
+	memcpy(&attribute->value, val, len);
+	attribute->val_len = len;
+
+	return attribute;
+}
+
+static int uuid_to_le(const bt_uuid_t *uuid, uint8_t *dst)
+{
+	switch (uuid->type) {
+	case BT_UUID16:
+		put_le16(uuid->value.u16, dst);
+		break;
+	case BT_UUID32:
+		put_le32(uuid->value.u32, dst);
+		break;
+	default:
+		bswap_128(&uuid->value.u128, dst);
+		break;
+	}
+
+	return bt_uuid_len(uuid);
+}
+
+uint16_t gatt_db_new_service(struct gatt_db *db, const bt_uuid_t *uuid,
+					bool primary, uint16_t num_handles)
+{
+	struct gatt_db_service *service;
+	const bt_uuid_t *type;
+	uint8_t value[16];
+	uint16_t len;
+
+	service = new0(struct gatt_db_service, 1);
+	if (!service)
+		return 0;
+
+	service->attributes = new0(struct gatt_db_attribute *, num_handles);
+	if (!service->attributes) {
+		free(service);
+		return 0;
+	}
+
+	if (primary)
+		type = &primary_service_uuid;
+	else
+		type = &secondary_service_uuid;
+
+	len = uuid_to_le(uuid, value);
+
+	service->attributes[0] = new_attribute(type, value, len);
+	if (!service->attributes[0]) {
+		gatt_db_service_destroy(service);
+		return 0;
+	}
+
+	if (!queue_push_tail(db->services, service)) {
+		gatt_db_service_destroy(service);
+		return 0;
+	}
+
+	/* TODO now we get next handle from database. We should first look
+	 * for 'holes' between existing services first, and assign next_handle
+	 * only if enough space was not found. */
+	service->attributes[0]->handle = db->next_handle;
+	db->next_handle += num_handles;
+	service->num_handles = num_handles;
+
+	return service->attributes[0]->handle;
+}
diff --git a/src/shared/gatt-db.h b/src/shared/gatt-db.h
index b3cd7a6..1656bb0 100644
--- a/src/shared/gatt-db.h
+++ b/src/shared/gatt-db.h
@@ -36,3 +36,17 @@ struct gatt_db *gatt_db_new(void);
  * @db - gatt database to be destroyed
  */
 void gatt_db_destroy(struct gatt_db *db);
+
+/*
+ * gatt_db_new_service - Add service attribute and reserve number of handles
+ * for this service
+ *
+ * @db - gatt database to add service
+ * @uuid - service uuid
+ * @primary - if service is primary or secondary
+ * @num_handles - num of handles to reserve for service
+ *
+ * Returns handle of service. In case of error 0 is returned.
+ */
+uint16_t gatt_db_new_service(struct gatt_db *db, const bt_uuid_t *uuid,
+					bool primary, uint16_t num_handles);
-- 
1.8.5.3


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCHv1 4/4] gatt: Add remove_service function to gatt-db
  2014-04-16  7:19 [PATCHv1 0/4] Gatt attributes database skeleton Marcin Kraglak
                   ` (2 preceding siblings ...)
  2014-04-16  7:19 ` [PATCHv1 3/4] gatt: Add method for creating services in gatt_db Marcin Kraglak
@ 2014-04-16  7:19 ` Marcin Kraglak
  3 siblings, 0 replies; 7+ messages in thread
From: Marcin Kraglak @ 2014-04-16  7:19 UTC (permalink / raw)
  To: linux-bluetooth

It will remove service with given handle. Return true if service
was found and removed, otherwise false.
---
 src/shared/gatt-db.c | 21 +++++++++++++++++++++
 src/shared/gatt-db.h | 11 +++++++++++
 2 files changed, 32 insertions(+)

diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
index 0094616..7dc5908 100644
--- a/src/shared/gatt-db.c
+++ b/src/shared/gatt-db.c
@@ -50,6 +50,13 @@ struct gatt_db_service {
 	struct gatt_db_attribute **attributes;
 };
 
+static bool match_service_by_handle(const void *data, const void *user_data)
+{
+	const struct gatt_db_service *service = data;
+
+	return service->attributes[0]->handle == PTR_TO_INT(user_data);
+}
+
 struct gatt_db *gatt_db_new(void)
 {
 	struct gatt_db *db;
@@ -166,3 +173,17 @@ uint16_t gatt_db_new_service(struct gatt_db *db, const bt_uuid_t *uuid,
 
 	return service->attributes[0]->handle;
 }
+
+bool gatt_db_remove_service(struct gatt_db *db, uint16_t handle)
+{
+	struct gatt_db_service *service;
+
+	service = queue_remove_if(db->services, match_service_by_handle,
+							INT_TO_PTR(handle));
+	if (!service)
+		return false;
+
+	gatt_db_service_destroy(service);
+
+	return true;
+}
diff --git a/src/shared/gatt-db.h b/src/shared/gatt-db.h
index 1656bb0..a98684b 100644
--- a/src/shared/gatt-db.h
+++ b/src/shared/gatt-db.h
@@ -50,3 +50,14 @@ void gatt_db_destroy(struct gatt_db *db);
  */
 uint16_t gatt_db_new_service(struct gatt_db *db, const bt_uuid_t *uuid,
 					bool primary, uint16_t num_handles);
+
+/*
+ * gatt_db_remove_service - remove service from attribute database
+ *
+ * @db - gatt database
+ * @handle - service handle
+ *
+ * Return true if service was successfully removed. In case of error false is
+ * returned
+ */
+bool gatt_db_remove_service(struct gatt_db *db, uint16_t handle);
-- 
1.8.5.3


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCHv1 1/4] gatt: Add skeleton of gatt-db
  2014-04-16  7:19 ` [PATCHv1 1/4] gatt: Add skeleton of gatt-db Marcin Kraglak
@ 2014-04-24 10:40   ` Szymon Janc
  0 siblings, 0 replies; 7+ messages in thread
From: Szymon Janc @ 2014-04-24 10:40 UTC (permalink / raw)
  To: Marcin Kraglak; +Cc: linux-bluetooth

Hi Marcin,

On Wednesday 16 of April 2014 09:19:04 Marcin Kraglak wrote:
> This change adds new() and destroy() fuctions for gatt_db,
> which will be used for storing local attributes.
> ---
>  android/Android.mk   |  1 +
>  android/Makefile.am  |  1 +
>  src/shared/gatt-db.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
>  src/shared/gatt-db.h | 38 ++++++++++++++++++++++++++++++++++++++
>  4 files changed, 87 insertions(+)
>  create mode 100644 src/shared/gatt-db.c
>  create mode 100644 src/shared/gatt-db.h
> 
> diff --git a/android/Android.mk b/android/Android.mk
> index 5f4e70c..4c9cfb5 100644
> --- a/android/Android.mk
> +++ b/android/Android.mk
> @@ -51,6 +51,7 @@ LOCAL_SRC_FILES := \
>  	bluez/src/shared/queue.c \
>  	bluez/src/shared/ringbuf.c \
>  	bluez/src/shared/hfp.c \
> +	bluez/src/shared/gatt-db.c \
>  	bluez/src/shared/io-glib.c \
>  	bluez/src/sdpd-database.c \
>  	bluez/src/sdpd-service.c \
> diff --git a/android/Makefile.am b/android/Makefile.am
> index c51cce2..70e1dec 100644
> --- a/android/Makefile.am
> +++ b/android/Makefile.am
> @@ -29,6 +29,7 @@ android_bluetoothd_SOURCES = android/main.c \
>  				src/shared/mgmt.h src/shared/mgmt.c \
>  				src/shared/ringbuf.h src/shared/ringbuf.c \
>  				src/shared/hfp.h src/shared/hfp.c \
> +				src/shared/gatt-db.h src/shared/gatt-db.c \
>  				android/bluetooth.h android/bluetooth.c \
>  				android/hidhost.h android/hidhost.c \
>  				android/ipc-common.h \
> diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
> new file mode 100644
> index 0000000..e56b381
> --- /dev/null
> +++ b/src/shared/gatt-db.c
> @@ -0,0 +1,47 @@
> +/*
> + *
> + *  BlueZ - Bluetooth protocol stack for Linux
> + *
> + *  Copyright (C) 2014  Intel Corporation. All rights reserved.
> + *
> + *
> + *  This library is free software; you can redistribute it and/or
> + *  modify it under the terms of the GNU Lesser General Public
> + *  License as published by the Free Software Foundation; either
> + *  version 2.1 of the License, or (at your option) any later version.
> + *
> + *  This library is distributed in the hope that it will be useful,
> + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
> + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + *  Lesser General Public License for more details.
> + *
> + *  You should have received a copy of the GNU Lesser General Public
> + *  License along with this library; if not, write to the Free Software
> + *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 
> USA + *
> + */
> +
> +#include "src/shared/util.h"
> +#include "src/shared/gatt-db.h"
> +
> +struct gatt_db {
> +	uint16_t next_handle;
> +};
> +
> +struct gatt_db *gatt_db_new(void)
> +{
> +	struct gatt_db *db;
> +
> +	db = new0(struct gatt_db, 1);
> +	if (!db)
> +		return NULL;
> +
> +	db->next_handle = 0x0001;
> +
> +	return db;
> +}
> +
> +void gatt_db_destroy(struct gatt_db *db)
> +{
> +	free(db);
> +}
> diff --git a/src/shared/gatt-db.h b/src/shared/gatt-db.h
> new file mode 100644
> index 0000000..b3cd7a6
> --- /dev/null
> +++ b/src/shared/gatt-db.h
> @@ -0,0 +1,38 @@
> +/*
> + *
> + *  BlueZ - Bluetooth protocol stack for Linux
> + *
> + *  Copyright (C) 2014  Intel Corporation. All rights reserved.
> + *
> + *
> + *  This library is free software; you can redistribute it and/or
> + *  modify it under the terms of the GNU Lesser General Public
> + *  License as published by the Free Software Foundation; either
> + *  version 2.1 of the License, or (at your option) any later version.
> + *
> + *  This library is distributed in the hope that it will be useful,
> + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
> + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + *  Lesser General Public License for more details.
> + *
> + *  You should have received a copy of the GNU Lesser General Public
> + *  License along with this library; if not, write to the Free Software
> + *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 
> USA + *
> + */
> +
> +struct gatt_db;
> +
> +/*
> + * gatt_db_new - Create new database for storing attributes
> + *
> + * Returns new database. In case of error NULL will be returned.
> + */
> +struct gatt_db *gatt_db_new(void);
> +
> +/*
> + * gatt_db_destroy - Destroy gatt database
> + *
> + * @db - gatt database to be destroyed
> + */
> +void gatt_db_destroy(struct gatt_db *db);

I'd avoid adding doxygen style comments. Those functions are self-explanatory.

-- 
BR
Szymon Janc

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCHv1 3/4] gatt: Add method for creating services in gatt_db
  2014-04-16  7:19 ` [PATCHv1 3/4] gatt: Add method for creating services in gatt_db Marcin Kraglak
@ 2014-04-24 10:41   ` Szymon Janc
  0 siblings, 0 replies; 7+ messages in thread
From: Szymon Janc @ 2014-04-24 10:41 UTC (permalink / raw)
  To: Marcin Kraglak; +Cc: linux-bluetooth

Hi Marcin,

On Wednesday 16 of April 2014 09:19:06 Marcin Kraglak wrote:
> This function will reserve number of handles and create service attribute.
> Primary arguments service type: PRIMARY or SECONDARY.

Info about service type being enum is no longer valid, right?

> ---
>  src/shared/gatt-db.c | 90
> ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/shared/gatt-db.h |
> 14 ++++++++
>  2 files changed, 104 insertions(+)
> 
> diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
> index b057c15..0094616 100644
> --- a/src/shared/gatt-db.c
> +++ b/src/shared/gatt-db.c
> @@ -23,16 +23,26 @@
> 
>  #include <stdbool.h>
> 
> +#include "lib/uuid.h"
>  #include "src/shared/util.h"
>  #include "src/shared/queue.h"
>  #include "src/shared/gatt-db.h"
> 
> +static const bt_uuid_t primary_service_uuid  = { .type = BT_UUID16,

Double space before =.

> +					.value.u16 = GATT_PRIM_SVC_UUID };
> +static const bt_uuid_t secondary_service_uuid  = { .type = BT_UUID16,
> +					.value.u16 = GATT_SND_SVC_UUID };

Same here.

> +
>  struct gatt_db {
>  	uint16_t next_handle;
>  	struct queue *services;
>  };
> 
>  struct gatt_db_attribute {
> +	uint16_t handle;
> +	bt_uuid_t uuid;
> +	uint16_t val_len;
> +	uint8_t value[0];
>  };
> 
>  struct gatt_db_service {
> @@ -76,3 +86,83 @@ void gatt_db_destroy(struct gatt_db *db)
>  	queue_destroy(db->services, gatt_db_service_destroy);
>  	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;
> +	memcpy(&attribute->value, val, len);
> +	attribute->val_len = len;
> +
> +	return attribute;
> +}
> +
> +static int uuid_to_le(const bt_uuid_t *uuid, uint8_t *dst)
> +{
> +	switch (uuid->type) {
> +	case BT_UUID16:
> +		put_le16(uuid->value.u16, dst);
> +		break;
> +	case BT_UUID32:
> +		put_le32(uuid->value.u32, dst);
> +		break;
> +	default:
> +		bswap_128(&uuid->value.u128, dst);
> +		break;
> +	}
> +
> +	return bt_uuid_len(uuid);
> +}
> +
> +uint16_t gatt_db_new_service(struct gatt_db *db, const bt_uuid_t *uuid,
> +					bool primary, uint16_t num_handles)
> +{
> +	struct gatt_db_service *service;
> +	const bt_uuid_t *type;
> +	uint8_t value[16];
> +	uint16_t len;
> +
> +	service = new0(struct gatt_db_service, 1);
> +	if (!service)
> +		return 0;
> +
> +	service->attributes = new0(struct gatt_db_attribute *, num_handles);

This can lead to nasty bug if 0 is passed - note that malloc(0) may return 
valid pointer. Maybe function should fail if 0 is passed? Or ignore
parameter if not >1 ?

> +	if (!service->attributes) {
> +		free(service);
> +		return 0;
> +	}
> +
> +	if (primary)
> +		type = &primary_service_uuid;
> +	else
> +		type = &secondary_service_uuid;
> +
> +	len = uuid_to_le(uuid, value);
> +
> +	service->attributes[0] = new_attribute(type, value, len);
> +	if (!service->attributes[0]) {
> +		gatt_db_service_destroy(service);
> +		return 0;
> +	}
> +
> +	if (!queue_push_tail(db->services, service)) {
> +		gatt_db_service_destroy(service);
> +		return 0;
> +	}
> +
> +	/* TODO now we get next handle from database. We should first look
> +	 * for 'holes' between existing services first, and assign next_handle
> +	 * only if enough space was not found. */
> +	service->attributes[0]->handle = db->next_handle;
> +	db->next_handle += num_handles;
> +	service->num_handles = num_handles;
> +
> +	return service->attributes[0]->handle;
> +}
> diff --git a/src/shared/gatt-db.h b/src/shared/gatt-db.h
> index b3cd7a6..1656bb0 100644
> --- a/src/shared/gatt-db.h
> +++ b/src/shared/gatt-db.h
> @@ -36,3 +36,17 @@ struct gatt_db *gatt_db_new(void);
>   * @db - gatt database to be destroyed
>   */
>  void gatt_db_destroy(struct gatt_db *db);
> +
> +/*
> + * gatt_db_new_service - Add service attribute and reserve number of
> handles + * for this service
> + *
> + * @db - gatt database to add service
> + * @uuid - service uuid
> + * @primary - if service is primary or secondary
> + * @num_handles - num of handles to reserve for service
> + *
> + * Returns handle of service. In case of error 0 is returned.
> + */
> +uint16_t gatt_db_new_service(struct gatt_db *db, const bt_uuid_t *uuid,
> +					bool primary, uint16_t num_handles);

Avoid such doxygen style comments in headers, those tends to diverse when 
implementation is modified. If you want to put comments about num_handles
do it in c file where you allocate memory.

-- 
BR
Szymon Janc

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-04-24 10:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-04-16  7:19 [PATCHv1 0/4] Gatt attributes database skeleton Marcin Kraglak
2014-04-16  7:19 ` [PATCHv1 1/4] gatt: Add skeleton of gatt-db Marcin Kraglak
2014-04-24 10:40   ` Szymon Janc
2014-04-16  7:19 ` [PATCHv1 2/4] gatt: Add services list to gatt_db struct Marcin Kraglak
2014-04-16  7:19 ` [PATCHv1 3/4] gatt: Add method for creating services in gatt_db Marcin Kraglak
2014-04-24 10:41   ` Szymon Janc
2014-04-16  7:19 ` [PATCHv1 4/4] gatt: Add remove_service function to gatt-db Marcin Kraglak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).