All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCHv2 3/4] gatt: Add method for creating services in gatt_db
  2014-04-24 11:23 [PATCHv2 0/4] Gatt database skeleton Marcin Kraglak
@ 2014-04-24 11:23 ` Marcin Kraglak
  0 siblings, 0 replies; 4+ messages in thread
From: Marcin Kraglak @ 2014-04-24 11:23 UTC (permalink / raw)
  To: linux-bluetooth

This function will reserve number of handles and create service attribute.
Both types of service can be created, PRIMARY or SECONDARy, user can select
type with primary flag.
---
 src/shared/gatt-db.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/shared/gatt-db.h |  3 ++
 2 files changed, 97 insertions(+)

diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
index b057c15..5e478d6 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,87 @@ 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;
+
+	if (num_handles < 1)
+		return 0;
+
+	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 2901afb..ee9178a 100644
--- a/src/shared/gatt-db.h
+++ b/src/shared/gatt-db.h
@@ -25,3 +25,6 @@ struct gatt_db;
 
 struct gatt_db *gatt_db_new(void);
 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);
-- 
1.8.5.3


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

* [PATCHv2 3/4] gatt: Add method for creating services in gatt_db
@ 2014-04-24 12:44 Marcin Kraglak
  2014-04-24 12:44 ` [PATCHv2 4/4] gatt: Add remove_service function to gatt-db Marcin Kraglak
  2014-04-25  7:53 ` [PATCHv2 3/4] gatt: Add method for creating services in gatt_db Szymon Janc
  0 siblings, 2 replies; 4+ messages in thread
From: Marcin Kraglak @ 2014-04-24 12:44 UTC (permalink / raw)
  To: linux-bluetooth

This function will reserve number of handles and create service attribute.
Both types of service can be created, PRIMARY or SECONDARy, user can select
type with primary flag.
---
 src/shared/gatt-db.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/shared/gatt-db.h |  3 ++
 2 files changed, 97 insertions(+)

diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
index b057c15..672c11c 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,87 @@ 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_add_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;
+
+	if (num_handles < 1)
+		return 0;
+
+	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 2901afb..7cae6b1 100644
--- a/src/shared/gatt-db.h
+++ b/src/shared/gatt-db.h
@@ -25,3 +25,6 @@ struct gatt_db;
 
 struct gatt_db *gatt_db_new(void);
 void gatt_db_destroy(struct gatt_db *db);
+
+uint16_t gatt_db_add_service(struct gatt_db *db, const bt_uuid_t *uuid,
+					bool primary, uint16_t num_handles);
-- 
1.8.3.1


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

* [PATCHv2 4/4] gatt: Add remove_service function to gatt-db
  2014-04-24 12:44 [PATCHv2 3/4] gatt: Add method for creating services in gatt_db Marcin Kraglak
@ 2014-04-24 12:44 ` Marcin Kraglak
  2014-04-25  7:53 ` [PATCHv2 3/4] gatt: Add method for creating services in gatt_db Szymon Janc
  1 sibling, 0 replies; 4+ messages in thread
From: Marcin Kraglak @ 2014-04-24 12:44 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 |  1 +
 2 files changed, 22 insertions(+)

diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
index 672c11c..a211cda 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;
@@ -170,3 +177,17 @@ uint16_t gatt_db_add_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 7cae6b1..b014b17 100644
--- a/src/shared/gatt-db.h
+++ b/src/shared/gatt-db.h
@@ -28,3 +28,4 @@ void gatt_db_destroy(struct gatt_db *db);
 
 uint16_t gatt_db_add_service(struct gatt_db *db, const bt_uuid_t *uuid,
 					bool primary, uint16_t num_handles);
+bool gatt_db_remove_service(struct gatt_db *db, uint16_t handle);
-- 
1.8.3.1


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

* Re: [PATCHv2 3/4] gatt: Add method for creating services in gatt_db
  2014-04-24 12:44 [PATCHv2 3/4] gatt: Add method for creating services in gatt_db Marcin Kraglak
  2014-04-24 12:44 ` [PATCHv2 4/4] gatt: Add remove_service function to gatt-db Marcin Kraglak
@ 2014-04-25  7:53 ` Szymon Janc
  1 sibling, 0 replies; 4+ messages in thread
From: Szymon Janc @ 2014-04-25  7:53 UTC (permalink / raw)
  To: Marcin Kraglak; +Cc: linux-bluetooth

Hi Marcin,

On Thursday 24 of April 2014 14:44:51 Marcin Kraglak wrote:
> This function will reserve number of handles and create service attribute.
> Both types of service can be created, PRIMARY or SECONDARy, user can select
> type with primary flag.
> ---
>  src/shared/gatt-db.c | 94
> ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/shared/gatt-db.h |
>  3 ++
>  2 files changed, 97 insertions(+)
> 
> diff --git a/src/shared/gatt-db.c b/src/shared/gatt-db.c
> index b057c15..672c11c 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,87 @@ 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_add_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;
> +
> +	if (num_handles < 1)
> +		return 0;
> +
> +	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 2901afb..7cae6b1 100644
> --- a/src/shared/gatt-db.h
> +++ b/src/shared/gatt-db.h
> @@ -25,3 +25,6 @@ struct gatt_db;
> 
>  struct gatt_db *gatt_db_new(void);
>  void gatt_db_destroy(struct gatt_db *db);
> +
> +uint16_t gatt_db_add_service(struct gatt_db *db, const bt_uuid_t *uuid,
> +					bool primary, uint16_t num_handles);

Both patches applied, thanks.

-- 
BR
Szymon Janc

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

end of thread, other threads:[~2014-04-25  7:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-04-24 12:44 [PATCHv2 3/4] gatt: Add method for creating services in gatt_db Marcin Kraglak
2014-04-24 12:44 ` [PATCHv2 4/4] gatt: Add remove_service function to gatt-db Marcin Kraglak
2014-04-25  7:53 ` [PATCHv2 3/4] gatt: Add method for creating services in gatt_db Szymon Janc
  -- strict thread matches above, loose matches on Subject: below --
2014-04-24 11:23 [PATCHv2 0/4] Gatt database skeleton Marcin Kraglak
2014-04-24 11:23 ` [PATCHv2 3/4] gatt: Add method for creating services in gatt_db Marcin Kraglak

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.