GATT service improvements
This set of patches enables GATT service definitions using 128-bit UUIDs. At the present, only 16 bit UUIDs are allowed in gatt-service. Furthermore, these patches fix the problem of having multiple service declarations for both 16 and 128 bit by allocating the former ones at the beginning of the database, and 128-bit services at the end. This feature is required in GATT when multiple services exist, service definitions with service declarations using 16-bit UUID should be grouped together and 128-bit UUID serviecs should be grouped together as well. BT 4.0 [Vol 3] 3.1.
These patches also replace the single database list for handlers with a double linked list in order to improve allocation operations for 128-bit service declaration at the end of the list.
Comments are welcome.
[PATCH 1/8] gatt-service: Add support for 128-bit Bluetooth UUIDs [PATCH 2/8] gatt-service: Move va_end just after processing the [PATCH 3/8] gatt-service: Provide service uuid in
[PATCH 4/8] attrib-server: Allocate 16-bits UUIDS at the begining of [PATCH 5/8] attrib-server: Set database uuids as a double linked [PATCH 6/8] glib-compat: Add g_list_free_full to deal with issues in [PATCH 7/8] attrib-server: Allocate 128-bits UUIDs using highest [PATCH 8/8] gatt-example: Fix g_assert checks when an uint16_t value
PATCH 1/8 gatt-service: Add support for 128-bit Bluetooth UUIDs
UUID services in GATT should be either 16-bit or 128-bit. Current GATT interface does not allow to provide 128-bit ones. This patch enables plugins to register services using 128-bit UUIDs.
--- attrib/gatt-service.c | 57 ++++++++++++++++++++++++++++++++++------------- attrib/gatt-service.h | 2 +- plugins/gatt-example.c | 7 ++++- time/server.c | 6 ++++- 4 files changed, 52 insertions(+), 20 deletions(-)
attrib/gatt-service.c +41 -16
@@ -116,6 +116,28 @@ static GSList *parse_opts(gatt_option opt1, va_list args)return l; } +static struct attribute *add_service_declaration (struct btd_adapter *adapter, +uint16_t handle,uint16_t scv,bt_uuid_t *uuid) +{ +bt_uuid_t bt_uuid; +uint8_t atval[16 ]; +int len; + +if (uuid->type ==BT_UUID16 ) { +att_put_u16 (uuid->value.u16, &atval[0 ]); + len =2 ; + }else if (uuid->type ==BT_UUID128 ) { +att_put_u128 (uuid->value.u128, &atval[0 ]); + len =16 ; + }else +return NULL ; + +bt_uuid16_create (&bt_uuid, scv); + +return attrib_db_add (adapter, handle, &bt_uuid,ATT_NONE , +ATT_NOT_PERMITTED , atval, len); +} +static int att_read_reqs (int authorization,int authentication,uint8_t props) { if (authorization == GATT_CHR_VALUE_READ ||
@@ -268,15 +290,21 @@ static void service_attr_del(struct btd_adapter *adapter, uint16_t start_handle, }gboolean gatt_service_add (struct btd_adapter *adapter,uint16_t uuid, -uint16_t svc_uuid,gatt_option opt1, ...) +bt_uuid_t svc_uuid,gatt_option opt1, ...) { +char uuidstr[MAX_LEN_UUID_STR ];uint16_t start_handle, h;unsigned int size; -bt_uuid_t bt_uuid; -uint8_t atval[2 ];va_list args;GSList *chrs, *l; +bt_uuid_to_string (&svc_uuid, uuidstr,MAX_LEN_UUID_STR ); + +if (svc_uuid.type !=BT_UUID16 && svc_uuid.type !=BT_UUID128 ) { +error ("Invalid service uuid: %s" , uuidstr); +return FALSE; + } +va_start (args, opt1); chrs =parse_opts (opt1, args);/* calculate how many attributes are necessary for this service */
@@ -288,31 +316,24 @@ gboolean gatt_service_add(struct btd_adapter *adapter, uint16_t uuid, start_handle =attrib_db_find_avail (adapter, size);if (start_handle ==0 ) {error ("Not enough free handles to register service" ); -g_slist_free_full (chrs, free_gatt_info); -return FALSE; + goto fail; } -DBG ("New service: handle 0x%04x, UUID 0x%04x, %d attributes" , - start_handle, svc_uuid, size); +DBG ("New service: handle 0x%04x, UUID %s, %d attributes" , + start_handle, uuidstr, size);/* service declaration */ h = start_handle; -bt_uuid16_create (&bt_uuid, uuid); -att_put_u16 (svc_uuid, &atval[0 ]); -if (attrib_db_add (adapter, h++, &bt_uuid,ATT_NONE ,ATT_NOT_PERMITTED , - atval,sizeof (atval)) ==NULL ) { -g_slist_free_full (chrs, free_gatt_info); -return FALSE; - } +if (add_service_declaration (adapter, h++, uuid, &svc_uuid) ==NULL ) + goto fail;for (l = chrs; l !=NULL ; l = l->next) {struct gatt_info *info = l->data;DBG ("New characteristic: handle 0x%04x" , h);if (!add_characteristic (adapter, &h, info)) { -g_slist_free_full (chrs, free_gatt_info);service_attr_del (adapter, start_handle, h -1 ); -return FALSE; + goto fail; } }
@@ -321,4 +342,8 @@ gboolean gatt_service_add(struct btd_adapter *adapter, uint16_t uuid,g_slist_free_full (chrs, free_gatt_info);return TRUE; + +fail: +g_slist_free_full (chrs, free_gatt_info); +return FALSE; }
attrib/gatt-service.h +1 -1
@@ -48,4 +48,4 @@ typedef enum { } attrib_event_t;gboolean gatt_service_add (struct btd_adapter *adapter,uint16_t uuid, -uint16_t svc_uuid,gatt_option opt1, ...); +bt_uuid_t svc_uuid,gatt_option opt1, ...);
plugins/gatt-example.c +5 -2
@@ -105,8 +105,11 @@ static uint8_t battery_state_read(struct attribute *a, gpointer user_data)static gboolean register_battery_service (struct btd_adapter *adapter) { - return gatt_service_add(adapter, GATT_PRIM_SVC_UUID, - BATTERY_STATE_SVC_UUID, +bt_uuid_t uuid; + +bt_uuid16_create (&uuid,BATTERY_STATE_SVC_UUID ); + + return gatt_service_add(adapter, GATT_PRIM_SVC_UUID, uuid, /* battery state characteristic */ GATT_OPT_CHR_UUID, BATTERY_STATE_UUID, GATT_OPT_CHR_PROPS, ATT_CHAR_PROPER_READ |
time/server.c +5 -1
@@ -115,9 +115,13 @@ static uint8_t local_time_info_read(struct attribute *a, gpointer user_data)static void register_current_time_service (void ) { +bt_uuid_t uuid; + +bt_uuid16_create (&uuid,CURRENT_TIME_SVC_UUID ); +/* Current Time service */ /* FIXME: Provide the adapter in next function */ - gatt_service_add(NULL, GATT_PRIM_SVC_UUID, CURRENT_TIME_SVC_UUID, + gatt_service_add(NULL, GATT_PRIM_SVC_UUID, uuid, /* CT Time characteristic */ GATT_OPT_CHR_UUID, CT_TIME_CHR_UUID, GATT_OPT_CHR_PROPS, ATT_CHAR_PROPER_READ |
Signature · 1 line
1.7.8.4
PATCH 2/8 gatt-service: Move va_end just after processing the argument list
--- attrib/gatt-service.c | 4 +++- 1 files changed, 3 insertions(+), 1 deletions(-)
attrib/gatt-service.c +3 -1
@@ -307,12 +307,14 @@ gboolean gatt_service_add(struct btd_adapter *adapter, uint16_t uuid,va_start (args, opt1); chrs =parse_opts (opt1, args); +va_end (args); +/* calculate how many attributes are necessary for this service */ for (l = chrs, size =1 ; l !=NULL ; l = l->next) {struct gatt_info *info = l->data; size += info->num_attrs; } -va_end (args); + start_handle =attrib_db_find_avail (adapter, size);if (start_handle ==0 ) {error ("Not enough free handles to register service" );
Signature · 1 line
1.7.8.4
PATCH 3/8 gatt-service: Provide service uuid in attrib_db_find_avail function
We need to provide the service uuid because of GATT server should group 16-bit uuid services together and 128-bit uuid services together, (Bluetooth 4.0, Vol 3, Part G, 3.1).
--- attrib/gatt-service.c | 2 +- plugins/gatt-example.c | 16 ++++++++++------ proximity/reporter.c | 9 ++++++--- src/attrib-server.c | 3 ++- src/attrib-server.h | 3 ++- 5 files changed, 21 insertions(+), 12 deletions(-)
attrib/gatt-service.c +1 -1
@@ -315,7 +315,7 @@ gboolean gatt_service_add(struct btd_adapter *adapter, uint16_t uuid, size += info->num_attrs; } - start_handle =attrib_db_find_avail (adapter, size); + start_handle =attrib_db_find_avail (adapter, &svc_uuid, size);if (start_handle ==0 ) {error ("Not enough free handles to register service" ); goto fail;
plugins/gatt-example.c +10 -6
@@ -132,7 +132,8 @@ static void register_termometer_service(struct gatt_example_adapter *adapter,bt_uuid_t uuid;int len; - start_handle =attrib_db_find_avail (adapter->adapter, svc_size); +bt_uuid16_create (&uuid,THERM_HUMIDITY_SVC_UUID ); + start_handle =attrib_db_find_avail (adapter->adapter, &uuid, svc_size);if (start_handle ==0 ) {error ("Not enough free handles to register service" );return ;
@@ -253,7 +254,8 @@ static void register_manuf1_service(struct gatt_example_adapter *adapter,bt_uuid_t uuid;int len; - start_handle =attrib_db_find_avail (adapter->adapter, svc_size); +bt_uuid16_create (&uuid,MANUFACTURER_SVC_UUID ); + start_handle =attrib_db_find_avail (adapter->adapter, &uuid, svc_size);if (start_handle ==0 ) {error ("Not enough free handles to register service" );return ;
@@ -316,7 +318,8 @@ static void register_manuf2_service(struct gatt_example_adapter *adapter,bt_uuid_t uuid;int len; - start_handle =attrib_db_find_avail (adapter->adapter, svc_size); +bt_uuid16_create (&uuid,MANUFACTURER_SVC_UUID ); + start_handle =attrib_db_find_avail (adapter->adapter, &uuid, svc_size);if (start_handle ==0 ) {error ("Not enough free handles to register service" );return ;
@@ -376,7 +379,8 @@ static void register_vendor_service(struct gatt_example_adapter *adapter,uint8_t atval[256 ];bt_uuid_t uuid; - start_handle =attrib_db_find_avail (adapter->adapter, svc_size); +bt_uuid16_create (&uuid,VENDOR_SPECIFIC_SVC_UUID ); + start_handle =attrib_db_find_avail (adapter->adapter, &uuid, svc_size);if (start_handle ==0 ) {error ("Not enough free handles to register service" );return ;
@@ -436,8 +440,8 @@ static void register_weight_service(struct gatt_example_adapter *adapter,int len;btoh128 (&char_weight_uuid_btorder, &char_weight_uuid); - - start_handle =attrib_db_find_avail (adapter->adapter, svc_size); +bt_uuid128_create (&uuid, char_weight_uuid_btorder); + start_handle =attrib_db_find_avail (adapter->adapter, &uuid, svc_size);if (start_handle ==0 ) {error ("Not enough free handles to register service" );return ;
proximity/reporter.c +6 -3
@@ -59,8 +59,9 @@ static void register_link_loss(void)uint8_t atval[256 ];bt_uuid_t uuid; +bt_uuid16_create (&uuid,LINK_LOSS_SVC_UUID );/* FIXME: Provide the adapter in next function */ - start_handle =attrib_db_find_avail (NULL , svc_size); + start_handle =attrib_db_find_avail (NULL , &uuid, svc_size);if (start_handle ==0 ) {error ("Not enough free handles to register service" );return ;
@@ -100,8 +101,9 @@ static void register_tx_power(void)uint8_t atval[256 ];bt_uuid_t uuid; +bt_uuid16_create (&uuid,TX_POWER_SVC_UUID );/* FIXME: Provide the adapter in next function */ - start_handle =attrib_db_find_avail (NULL , svc_size); + start_handle =attrib_db_find_avail (NULL , &uuid, svc_size);if (start_handle ==0 ) {error ("Not enough free handles to register service" );return ;
@@ -149,8 +151,9 @@ static void register_immediate_alert(void)uint8_t atval[256 ];bt_uuid_t uuid; +bt_uuid16_create (&uuid,IMMEDIATE_ALERT_SVC_UUID );/* FIXME: Provide the adapter in next function */ - start_handle =attrib_db_find_avail (NULL , svc_size); + start_handle =attrib_db_find_avail (NULL , &uuid, svc_size);if (start_handle ==0 ) {error ("Not enough free handles to register service" );return ;
src/attrib-server.c +2 -1
@@ -1270,7 +1270,8 @@ void attrib_free_sdp(uint32_t sdp_handle)remove_record_from_server (sdp_handle); } -uint16_t attrib_db_find_avail (struct btd_adapter *adapter,uint16_t nitems) +uint16_t attrib_db_find_avail (struct btd_adapter *adapter,bt_uuid_t *svc_uuid, +uint16_t nitems) {struct gatt_server *server;uint16_t handle;
src/attrib-server.h +2 -1
@@ -22,7 +22,8 @@* */ -uint16_t attrib_db_find_avail (struct btd_adapter *adapter,uint16_t nitems); +uint16_t attrib_db_find_avail (struct btd_adapter *adapter,bt_uuid_t *svc_uuid, +uint16_t nitems);struct attribute *attrib_db_add (struct btd_adapter *adapter,uint16_t handle,bt_uuid_t *uuid,int read_reqs,int write_reqs,const uint8_t *value,int len);
Signature · 1 line
1.7.8.4
PATCH 4/8 attrib-server: Allocate 16-bits UUIDS at the begining of the list
--- src/attrib-server.c | 43 ++++++++++++++++++++++++++++++++++++------- 1 files changed, 36 insertions(+), 7 deletions(-)
src/attrib-server.c +36 -7
@@ -350,7 +350,6 @@ static struct attribute *attrib_db_add_new(struct gatt_server *server, server->database =g_slist_insert_sorted (server->database, a, attribute_cmp); -return a; }
@@ -1270,30 +1269,35 @@ void attrib_free_sdp(uint32_t sdp_handle)remove_record_from_server (sdp_handle); } -uint16_t attrib_db_find_avail (struct btd_adapter *adapter,bt_uuid_t *svc_uuid, -uint16_t nitems) +static uint16_t find_uuid16_avail (struct btd_adapter *adapter,uint16_t nitems) {struct gatt_server *server;uint16_t handle;GSList *l; -g_assert (nitems >0 ); - l =g_slist_find_custom (servers, adapter, adapter_cmp);if (l ==NULL )return 0 ; server = l->data; +if (server->database ==NULL ) +return 0x0001 ; -for (l = server->database, handle =0 ; l; l = l->next) { +for (l = server->database, handle =0x0001 ; l; l = l->next) {struct attribute *a = l->data; -if (handle && (bt_uuid_cmp (&a->uuid, &prim_uuid) ==0 || +if ((bt_uuid_cmp (&a->uuid, &prim_uuid) ==0 ||bt_uuid_cmp (&a->uuid, &snd_uuid) ==0 ) && a->handle - handle >= nitems)/* Note: the range above excludes the current handle */ return handle; +if (a->len ==16 && (bt_uuid_cmp (&a->uuid, &prim_uuid) ==0 || +bt_uuid_cmp (&a->uuid, &snd_uuid) ==0 )) { +/* 128 bit UUID service definition */ +return 0 ; + } +if (a->handle ==0xffff )return 0 ;
@@ -1306,6 +1310,31 @@ uint16_t attrib_db_find_avail(struct btd_adapter *adapter, bt_uuid_t *svc_uuid,return 0 ; } +static uint16_t find_uuid128_avail (struct btd_adapter *adapter,uint16_t nitems) +{ +/* TODO: Allocate 128 uuids at the end of the list */ +return 0 ; +} + +uint16_t attrib_db_find_avail (struct btd_adapter *adapter,bt_uuid_t *svc_uuid, +uint16_t nitems) +{ +g_assert (nitems >0 ); + +if (svc_uuid->type ==BT_UUID16 ) +return find_uuid16_avail (adapter, nitems); +else if (svc_uuid->type ==BT_UUID128 ) +return find_uuid128_avail (adapter, nitems); +else { +char uuidstr[MAX_LEN_UUID_STR ]; + +bt_uuid_to_string (svc_uuid, uuidstr,MAX_LEN_UUID_STR ); +error ("Service uuid: %s is neither a 16-bit nor a 128-bit uuid" , + uuidstr); +return 0 ; + } +} +struct attribute *attrib_db_add (struct btd_adapter *adapter,uint16_t handle,bt_uuid_t *uuid,int read_reqs,int write_reqs,const uint8_t *value,int len)
Signature · 1 line
1.7.8.4
PATCH 5/8 attrib-server: Set database uuids as a double linked list
16-bit UUIDs are allocated at the begining of the database list and 128-bit ones are grouped at the end. Replacing the list type with a double linked one enables us to look for available handles in the 128-bit group efficiently.
--- src/attrib-server.c | 75 ++++++++++++++++++++++++++++----------------------- 1 files changed, 41 insertions(+), 34 deletions(-)
src/attrib-server.c +41 -34
@@ -60,7 +60,7 @@ struct gatt_server {GIOChannel *le_io;uint32_t gatt_sdp_handle;uint32_t gap_sdp_handle; -GSList *database; +GList *database;GSList *clients;uint16_t name_handle;uint16_t appearance_handle;
@@ -114,7 +114,7 @@ static void channel_free(struct gatt_channel *channel)static void gatt_server_free (struct gatt_server *server) { -g_slist_free_full (server->database, attrib_free); +g_list_free_full (server->database, attrib_free);if (server->l2cap_io !=NULL ) {g_io_channel_unref (server->l2cap_io);
@@ -254,12 +254,12 @@ static struct attribute *find_primary_range(struct gatt_server *server, {struct attribute *attrib;guint h = start; -GSList *l; +GList *l;if (end ==NULL )return NULL ; - l =g_slist_find_custom (server->database,GUINT_TO_POINTER (h), + l =g_list_find_custom (server->database,GUINT_TO_POINTER (h), handle_cmp);if (!l)return NULL ;
@@ -336,7 +336,7 @@ static struct attribute *attrib_db_add_new(struct gatt_server *server,DBG ("handle=0x%04x" , handle); -if (g_slist_find_custom (server->database,GUINT_TO_POINTER (h), +if (g_list_find_custom (server->database,GUINT_TO_POINTER (h), handle_cmp))return NULL ;
@@ -348,7 +348,7 @@ static struct attribute *attrib_db_add_new(struct gatt_server *server, a->read_reqs = read_reqs; a->write_reqs = write_reqs; - server->database =g_slist_insert_sorted (server->database, a, + server->database =g_list_insert_sorted (server->database, a, attribute_cmp);return a; }
@@ -394,7 +394,8 @@ static uint16_t read_by_group(struct gatt_channel *channel, uint16_t start,struct att_data_list *adl;struct attribute *a;struct group_elem *cur, *old =NULL ; -GSList *l, *groups, *database; +GSList *l, *groups; +GList *dl, *database;uint16_t length, last_handle, last_size =0 ;uint8_t status;int i;
@@ -415,9 +416,9 @@ static uint16_t read_by_group(struct gatt_channel *channel, uint16_t start, last_handle = end; database = channel->server->database; -for (l = database, groups =NULL , cur =NULL ; l; l = l->next) { +for (dl = database, groups =NULL , cur =NULL ; dl; dl = dl->next) { - a = l->data; + a = dl->data;if (a->handle < start)continue ;
@@ -471,7 +472,7 @@ static uint16_t read_by_group(struct gatt_channel *channel, uint16_t start,return enc_error_resp (ATT_OP_READ_BY_GROUP_REQ , start,ATT_ECODE_ATTR_NOT_FOUND , pdu, len); -if (l ==NULL ) +if (dl ==NULL ) cur->end = a->handle;else cur->end = last_handle;
@@ -506,7 +507,8 @@ static uint16_t read_by_type(struct gatt_channel *channel, uint16_t start, uint8_t *pdu, int len) {struct att_data_list *adl; -GSList *l, *types, *database; +GSList *l, *types; +GList *dl, *database;struct attribute *a;uint16_t num, length;uint8_t status;
@@ -517,9 +519,9 @@ static uint16_t read_by_type(struct gatt_channel *channel, uint16_t start, ATT_ECODE_INVALID_HANDLE, pdu, len); database = channel->server->database; -for (l = database, length =0 , types =NULL ; l; l = l->next) { +for (dl = database, length =0 , types =NULL ; dl; dl = dl->next) { - a = l->data; + a = dl->data;if (a->handle < start)continue ;
@@ -588,7 +590,8 @@ static int find_info(struct gatt_channel *channel, uint16_t start, uint16_t end, {struct attribute *a;struct att_data_list *adl; -GSList *l, *info, *database; +GSList *l, *info; +GList *dl, *database;uint8_t format, last_type =BT_UUID_UNSPEC ;uint16_t length, num;int i;
@@ -598,8 +601,8 @@ static int find_info(struct gatt_channel *channel, uint16_t start, uint16_t end, ATT_ECODE_INVALID_HANDLE, pdu, len); database = channel->server->database; -for (l = database, info =NULL , num =0 ; l; l = l->next) { - a = l->data; +for (dl = database, info =NULL , num =0 ; dl; dl = dl->next) { + a = dl->data;if (a->handle < start)continue ;
@@ -663,7 +666,8 @@ static int find_by_type(struct gatt_channel *channel, uint16_t start, {struct attribute *a;struct att_range *range; -GSList *l, *matches, *database; +GSList *matches; +GList *dl, *database;int len;if (start > end || start ==0x0000 )
@@ -672,8 +676,8 @@ static int find_by_type(struct gatt_channel *channel, uint16_t start,/* Searching first requested handle number */ database = channel->server->database; -for (l = database, matches =NULL , range =NULL ; l; l = l->next) { - a = l->data; +for (dl = database, matches =NULL , range =NULL ; dl; dl = dl->next) { + a = dl->data;if (a->handle < start)continue ;
@@ -720,11 +724,11 @@ static uint16_t read_value(struct gatt_channel *channel, uint16_t handle, {struct attribute *a;uint8_t status; -GSList *l; +GList *l;uint16_t cccval;guint h = handle; - l =g_slist_find_custom (channel->server->database, + l =g_list_find_custom (channel->server->database,GUINT_TO_POINTER (h), handle_cmp);if (!l) return enc_error_resp(ATT_OP_READ_REQ, handle,
@@ -758,11 +762,11 @@ static uint16_t read_blob(struct gatt_channel *channel, uint16_t handle, {struct attribute *a;uint8_t status; -GSList *l; +GList *l;uint16_t cccval;guint h = handle; - l =g_slist_find_custom (channel->server->database, + l =g_list_find_custom (channel->server->database,GUINT_TO_POINTER (h), handle_cmp);if (!l) return enc_error_resp(ATT_OP_READ_BLOB_REQ, handle,
@@ -802,10 +806,10 @@ static uint16_t write_value(struct gatt_channel *channel, uint16_t handle, {struct attribute *a;uint8_t status; -GSList *l; +GList *l;guint h = handle; - l =g_slist_find_custom (channel->server->database, + l =g_list_find_custom (channel->server->database,GUINT_TO_POINTER (h), handle_cmp);if (!l) return enc_error_resp(ATT_OP_WRITE_REQ, handle,
@@ -1274,6 +1278,7 @@ static uint16_t find_uuid16_avail(struct btd_adapter *adapter, uint16_t nitems)struct gatt_server *server;uint16_t handle;GSList *l; +GList *dl; l =g_slist_find_custom (servers, adapter, adapter_cmp);if (l ==NULL )
@@ -1283,8 +1288,8 @@ static uint16_t find_uuid16_avail(struct btd_adapter *adapter, uint16_t nitems)if (server->database ==NULL )return 0x0001 ; -for (l = server->database, handle =0x0001 ; l; l = l->next) { -struct attribute *a = l->data; +for (dl = server->database, handle =0x0001 ; dl; dl = dl->next) { +struct attribute *a = dl->data; if ((bt_uuid_cmp(&a->uuid, &prim_uuid) == 0 || bt_uuid_cmp(&a->uuid, &snd_uuid) == 0) &&
@@ -1356,6 +1361,7 @@ int attrib_db_update(struct btd_adapter *adapter, uint16_t handle,struct gatt_server *server;struct attribute *a;GSList *l; +GList *dl;guint h = handle; l =g_slist_find_custom (servers, adapter, adapter_cmp);
@@ -1366,12 +1372,12 @@ int attrib_db_update(struct btd_adapter *adapter, uint16_t handle,DBG ("handle=0x%04x" , handle); - l =g_slist_find_custom (server->database,GUINT_TO_POINTER (h), + dl =g_list_find_custom (server->database,GUINT_TO_POINTER (h), handle_cmp); -if (!l) +if (dl ==NULL )return -ENOENT ; - a = l->data; + a = dl->data; a->data =g_try_realloc (a->data, len);if (a->data ==NULL )
@@ -1394,6 +1400,7 @@ int attrib_db_del(struct btd_adapter *adapter, uint16_t handle)struct gatt_server *server;struct attribute *a;GSList *l; +GList *dl;guint h = handle; l =g_slist_find_custom (servers, adapter, adapter_cmp);
@@ -1404,13 +1411,13 @@ int attrib_db_del(struct btd_adapter *adapter, uint16_t handle)DBG ("handle=0x%04x" , handle); - l =g_slist_find_custom (server->database,GUINT_TO_POINTER (h), + dl =g_list_find_custom (server->database,GUINT_TO_POINTER (h), handle_cmp); -if (!l) +if (dl ==NULL )return -ENOENT ; - a = l->data; - server->database =g_slist_remove (server->database, a); + a = dl->data; + server->database =g_list_remove (server->database, a);g_free (a->data);g_free (a);
Signature · 1 line
1.7.8.4
PATCH 6/8 glib-compat: Add g_list_free_full to deal with issues in old GLib versions
--- acinclude.m4 | 3 +++ src/glib-compat.h | 8 ++++++++ 2 files changed, 11 insertions(+), 0 deletions(-)
acinclude.m4 +3 -0
@@ -111,6 +111,9 @@ AC_DEFUN([AC_PATH_GLIB], [ AC_CHECK_LIB(glib-2.0, g_slist_free_full, dummy=yes, AC_DEFINE(NEED_G_SLIST_FREE_FULL, 1, [Define to 1 if you need g_slist_free_full() function.])) + AC_CHECK_LIB(glib-2.0, g_list_free_full, dummy=yes, + AC_DEFINE(NEED_G_LIST_FREE_FULL, 1, + [Define to 1 if you need g_list_free_full() function.])) AC_SUBST(GLIB_CFLAGS) AC_SUBST(GLIB_LIBS) ])
src/glib-compat.h +8 -0
@@ -28,3 +28,11 @@ static inline void g_slist_free_full(GSList *list, GDestroyNotify free_func) g_slist_free(list); }#endif + +#ifdef NEED_G_LIST_FREE_FULL +static inline void g_list_free_full (GList *list,GDestroyNotify free_func) +{ +g_list_foreach (list, (GFunc ) free_func,NULL ); +g_list_free (list); +} +#endif
Signature · 1 line
1.7.8.4
PATCH 7/8 attrib-server: Allocate 128-bits UUIDs using highest available handlers
128-uuids services are grouped at the end of the handlers database list. This group grows up from the highest handlers toward lowers handlers until the whole range is used or the last 16 bit-uuid service is reached.
--- src/attrib-server.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 46 insertions(+), 1 deletions(-)
src/attrib-server.c +46 -1
@@ -1317,7 +1317,52 @@ static uint16_t find_uuid16_avail(struct btd_adapter *adapter, uint16_t nitems)static uint16_t find_uuid128_avail (struct btd_adapter *adapter,uint16_t nitems) { -/* TODO: Allocate 128 uuids at the end of the list */ +struct gatt_server *server; +uint16_t handle =0 , end =0xffff ; +gboolean pick = TRUE; +GList *dl; +GSList *l; + + l =g_slist_find_custom (servers, adapter, adapter_cmp); +if (l ==NULL ) +return 0 ; + + server = l->data; +if (server->database ==NULL ) +return 0xffff - nitems +1 ; + +for (dl =g_list_last (server->database); dl; dl = dl->prev) { +struct attribute *a = dl->data; +if (pick) { + handle = a->handle; + pick = FALSE; + } + +if (bt_uuid_cmp (&a->uuid, &prim_uuid) !=0 && +bt_uuid_cmp (&a->uuid, &snd_uuid) !=0 ) +continue ; + +if ((bt_uuid_cmp (&a->uuid, &prim_uuid) ==0 || +bt_uuid_cmp (&a->uuid, &snd_uuid) ==0 ) && + end - handle >= nitems) +return end - nitems +1 ; + +if (a->len ==2 && (bt_uuid_cmp (&a->uuid, &prim_uuid) ==0 || +bt_uuid_cmp (&a->uuid, &snd_uuid) ==0 )) { +/* 16 bit UUID service definition */ +return 0 ; + } + +if (a->handle ==0x0001 ) +return 0 ; + + end = a->handle -1 ; + pick = TRUE; + } + +if (end -0x0001 >= nitems) +return end - nitems +1 ; +return 0 ; }
Signature · 1 line
1.7.8.4
PATCH 8/8 gatt-example: Fix g_assert checks when an uint16_t value overflows
g_assert statements are buggy when the last handler available overflows the uint16_t range. This check is currently used to evaluate if the number of requested attributes for a sevice match with the number of attributes added in the data base.
--- plugins/gatt-example.c | 21 ++++++++++----------- 1 files changed, 10 insertions(+), 11 deletions(-)
plugins/gatt-example.c +10 -11
@@ -230,10 +230,10 @@ static void register_termometer_service(struct gatt_example_adapter *adapter,bt_uuid16_create (&uuid,GATT_CHARAC_USER_DESC_UUID ); len =strlen (desc_out_hum);strncpy ((char *) atval, desc_out_hum, len); -attrib_db_add (adapter->adapter, h++, &uuid,ATT_NONE ,ATT_NOT_PERMITTED , +attrib_db_add (adapter->adapter, h, &uuid,ATT_NONE ,ATT_NOT_PERMITTED , atval, len); -g_assert (h - start_handle == svc_size); +g_assert (h - start_handle +1 == svc_size);/* Add an SDP record for the above service */ sdp_handle = attrib_create_sdp(adapter->adapter, start_handle,
@@ -298,10 +298,10 @@ static void register_manuf1_service(struct gatt_example_adapter *adapter,bt_uuid16_create (&uuid,MANUFACTURER_SERIAL_UUID ); len =strlen (serial1);strncpy ((char *) atval, serial1, len); -attrib_db_add (adapter->adapter, h++, &uuid,ATT_NONE ,ATT_NOT_PERMITTED , +attrib_db_add (adapter->adapter, h, &uuid,ATT_NONE ,ATT_NOT_PERMITTED , atval, len); -g_assert (h - start_handle == svc_size); +g_assert (h - start_handle +1 == svc_size); range[0 ] = start_handle; range[1 ] = start_handle + svc_size -1 ;
@@ -362,10 +362,10 @@ static void register_manuf2_service(struct gatt_example_adapter *adapter,bt_uuid16_create (&uuid,MANUFACTURER_SERIAL_UUID ); len =strlen (serial2);strncpy ((char *) atval, serial2, len); -attrib_db_add (adapter->adapter, h++, &uuid,ATT_NONE ,ATT_NOT_PERMITTED , +attrib_db_add (adapter->adapter, h, &uuid,ATT_NONE ,ATT_NOT_PERMITTED , atval, len); -g_assert (h - start_handle == svc_size); +g_assert (h - start_handle +1 == svc_size); range[0 ] = start_handle; range[1 ] = start_handle + svc_size -1 ;
@@ -412,10 +412,10 @@ static void register_vendor_service(struct gatt_example_adapter *adapter, atval[3 ] =0x64 ; atval[4 ] =0x6F ; atval[5 ] =0x72 ; -attrib_db_add (adapter->adapter, h++, &uuid,ATT_NONE ,ATT_NOT_PERMITTED , +attrib_db_add (adapter->adapter, h, &uuid,ATT_NONE ,ATT_NOT_PERMITTED , atval,6 ); -g_assert (h - start_handle == svc_size); +g_assert (h - start_handle +1 == svc_size); range[0 ] = start_handle; range[1 ] = start_handle + svc_size -1 ;
@@ -499,10 +499,9 @@ static void register_weight_service(struct gatt_example_adapter *adapter,bt_uuid16_create (&uuid,GATT_CHARAC_USER_DESC_UUID ); len =strlen (desc_weight);strncpy ((char *) atval, desc_weight, len); -attrib_db_add (adapter->adapter, h++, &uuid,ATT_NONE ,ATT_NOT_PERMITTED , +attrib_db_add (adapter->adapter, h, &uuid,ATT_NONE ,ATT_NOT_PERMITTED , atval, len); - -g_assert (h - start_handle == svc_size); +g_assert (h - start_handle +1 == svc_size);/* Add an SDP record for the above service */ sdp_handle = attrib_create_sdp(adapter->adapter, start_handle,
Signature · 1 line
1.7.8.4
Re: [PATCH 1/8] gatt-service: Add support for 128-bit Bluetooth UUIDs
In reply to Santiago Carot-NemesioHi Santiago,
Quoted 15 lines · Santiago Carot-Nemesio
UUID services in GATT should be either 16-bit or 128-bit. Current GATT interface does not allow to provide 128-bit ones. This patch enables plugins to register services using 128-bit UUIDs.
---
attrib/gatt-service.c | 57 ++++++++++++++++++++++++++++++++++-------------attrib/gatt-service.h| 2 +-
plugins/gatt-example.c | 7 ++++-
time/server.c | 6 ++++-4 files changed, 52 insertions(+), 20 deletions(-)attrib/gatt-service.c +0
-0
attrib/gatt-service.c static GSList *parse_opts(gatt_option opt1, va_list args) · line 120
Quoted 5 lines
attrib/gatt-service.c +2
-0@@ -116,6 +116,28 @@ static GSList *parse_opts(gatt_option opt1, va_list args) return l; } +static struct attribute *add_service_declaration (struct btd_adapter *adapter, + uint16_t handle, uint16_t scv, bt_uuid_t *uuid)
typo: scv -> svc
Quoted 24 lines
attrib/gatt-service.c +20
-0+{ + bt_uuid_t bt_uuid; + uint8_t atval[16]; + int len; + + if (uuid->type == BT_UUID16) { + att_put_u16(uuid->value.u16, &atval[0]); + len = 2; + } else if (uuid->type == BT_UUID128) { + att_put_u128(uuid->value.u128, &atval[0]); + len = 16; + } else + return NULL; + + bt_uuid16_create(&bt_uuid, scv); + + return attrib_db_add(adapter, handle, &bt_uuid, ATT_NONE, + ATT_NOT_PERMITTED, atval, len); +} + static int att_read_reqs(int authorization, int authentication, uint8_t props) {if (authorization ==
GATT_CHR_VALUE_READ||
attrib/gatt-service.c static void service_attr_del(struct btd_adapter *adapter, uint16_t start_handle, · line 293
Quoted 5 lines
attrib/gatt-service.c +1
-1@@ -268,15 +290,21 @@ static void service_attr_del(struct btd_adapter *adapter, uint16_t start_handle, } gboolean gatt_service_add(struct btd_adapter *adapter, uint16_t uuid, - uint16_t svc_uuid, gatt_option opt1, ...) + bt_uuid_t svc_uuid, gatt_option opt1, ...)
I think it is more efficient to pass svc_uuid as pointer (this avoids a possible 128-bit copy).
Quoted 9 lines
attrib/gatt-service.c +1
-2{ + char uuidstr[MAX_LEN_UUID_STR]; uint16_t start_handle, h; unsigned int size; - bt_uuid_t bt_uuid; - uint8_t atval[2]; va_list args; GSList *chrs, *l;
attrib/gatt-service.c static void service_attr_del(struct btd_adapter *adapter, uint16_t start_handle, · line 310
Quoted 10 lines
attrib/gatt-service.c +7
-0+ bt_uuid_to_string(&svc_uuid, uuidstr, MAX_LEN_UUID_STR); + + if (svc_uuid.type != BT_UUID16 && svc_uuid.type != BT_UUID128) { + error("Invalid service uuid: %s", uuidstr); + return FALSE; + } + va_start(args, opt1); chrs = parse_opts(opt1, args); /* calculate how many attributes are necessary for this service */
Regards,
Signature · 3 lines
Anderson Lizardo Instituto Nokia de Tecnologia - INdT Manaus - Brazil
Re: [PATCH 3/8] gatt-service: Provide service uuid in attrib_db_find_avail function
In reply to Santiago Carot-NemesioHi Santiago,
Quoted 16 lines · Santiago Carot-Nemesio
We need to provide the service uuid because of GATT server should group 16-bit uuid services together and 128-bit uuid services together, (Bluetooth 4.0, Vol 3, Part G, 3.1).
---
attrib/gatt-service.c | 2 +-
plugins/gatt-example.c | 16 ++++++++++------proximity/reporter.c| 9 ++++++---
src/attrib-server.c | 3 ++-
src/attrib-server.h | 3 ++-5 files changed, 21 insertions(+), 12 deletions(-)attrib/gatt-service.c +0
-0
attrib/gatt-service.c static void register_weight_service(struct gatt_example_adapter *adapter, · line 447
Quoted 10 lines
attrib/gatt-service.c +2
-2@@ -436,8 +440,8 @@ static void register_weight_service(struct gatt_example_adapter *adapter, int len; btoh128(&char_weight_uuid_btorder, &char_weight_uuid); - - start_handle = attrib_db_find_avail(adapter->adapter, svc_size); + bt_uuid128_create(&uuid, char_weight_uuid_btorder); + start_handle = attrib_db_find_avail(adapter->adapter, &uuid, svc_size); if (start_handle == 0) { error("Not enough free handles to register service"); return;
This is wrong. You are using a *characteristic* UUID as a service UUID. This example weight service is 16-bit, but it is has one 128-bit characteristic.
BTW, 16-bit vs. 128-bit characteristic grouping is optional as per page 1897 (section 3.3.1 "Characteristic Declaration").
Regards,
Signature · 3 lines
Anderson Lizardo Instituto Nokia de Tecnologia - INdT Manaus - Brazil
Re: [PATCH 4/8] attrib-server: Allocate 16-bits UUIDS at the begining of the list
In reply to Santiago Carot-NemesioHi Santiago,
Quoted 9 lines · Santiago Carot-Nemesio
---
src/attrib-server.c | 43 ++++++++++++++++++++++++++++++++++++-------1 files changed, 36 insertions(+), 7 deletions(-)src/attrib-server.c +0
-0
src/attrib-server.c static struct attribute *attrib_db_add_new(struct gatt_server *server, · line 354
Quoted 6 lines
src/attrib-server.c +0
-1@@ -350,7 +350,6 @@ static struct attribute *attrib_db_add_new(struct gatt_server *server, server->database = g_slist_insert_sorted(server->database, a, attribute_cmp); - return a; }
The change above is bogus.
Regards,
Signature · 3 lines
Anderson Lizardo Instituto Nokia de Tecnologia - INdT Manaus - Brazil
Re: [PATCH 7/8] attrib-server: Allocate 128-bits UUIDs using highest available handlers
In reply to Santiago Carot-NemesioHi Santiago,
Quoted 26 lines · Santiago Carot-Nemesio
128-uuids services are grouped at the end of the handlers database list. This group grows up from the highest handlers toward lowers handlers until the whole range is used or the last 16 bit-uuid service is reached.
---
src/attrib-server.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++-1 files changed, 46 insertions(+), 1 deletions(-)src/attrib-server.c +10
-1@@ -1317,7 +1317,52 @@ static uint16_t find_uuid16_avail(struct btd_adapter *adapter, uint16_t nitems) static uint16_t find_uuid128_avail(struct btd_adapter *adapter, uint16_t nitems) { - /* TODO: Allocate 128 uuids at the end of the list */ + struct gatt_server *server; + uint16_t handle = 0, end = 0xffff; + gboolean pick = TRUE; + GList *dl; + GSList *l; + + l = g_slist_find_custom(servers, adapter, adapter_cmp); + if (l == NULL) + return 0; +
src/attrib-server.c static uint16_t find_uuid16_avail(struct btd_adapter *adapter, uint16_t nitems) · line 1339
Quoted 10 lines
src/attrib-server.c +10
-0+ server = l->data; + if (server->database == NULL) + return 0xffff - nitems + 1; + + for (dl = g_list_last(server->database); dl; dl = dl->prev) { + struct attribute *a = dl->data; + if (pick) { + handle = a->handle; + pick = FALSE; + }
I suspect you can get this code a lot simpler without using this "pick" variable. Unfortunately, I don't have time to look at this further right now.
src/attrib-server.c static uint16_t find_uuid16_avail(struct btd_adapter *adapter, uint16_t nitems) · line 1343
Quoted 4 lines
src/attrib-server.c +4
-0+ + if (bt_uuid_cmp(&a->uuid, &prim_uuid) != 0 && + bt_uuid_cmp(&a->uuid, &snd_uuid) != 0) + continue;
At this point you know a->uuid is either primary or secondary. So I don't think it is necessary to check again below.
src/attrib-server.c static uint16_t find_uuid16_avail(struct btd_adapter *adapter, uint16_t nitems) · line 1348
Quoted 5 lines
src/attrib-server.c +5
-0+ + if ((bt_uuid_cmp(&a->uuid, &prim_uuid) == 0 || + bt_uuid_cmp(&a->uuid, &snd_uuid) == 0) && + end - handle >= nitems) + return end - nitems + 1;
Same below. At this point, they are guaranteed to be either primary or secondary uuids. You only need to check for a->len.
Quoted 23 lines
src/attrib-server.c +17
-1+ + if (a->len == 2 && (bt_uuid_cmp(&a->uuid, &prim_uuid) == 0 || + bt_uuid_cmp(&a->uuid, &snd_uuid) == 0)) { + /* 16 bit UUID service definition */ + return 0; + } + + if (a->handle == 0x0001) + return 0; + + end = a->handle - 1; + pick = TRUE; + } + + if (end - 0x0001 >= nitems) + return end - nitems + 1; + return 0; } --1.7.8.4
Quoted 4 lines
--
To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Regards,
Signature · 3 lines
Anderson Lizardo Instituto Nokia de Tecnologia - INdT Manaus - Brazil
Re: [PATCH 7/8] attrib-server: Allocate 128-bits UUIDs using highest available handlers
Hi Anderson,
2012/1/24 Anderson Lizardo <[email protected]>:
Quoted 53 lines
Hi Santiago,
Quoted 26 lines · Santiago Carot-Nemesio
128-uuids services are grouped at the end of the handlers database list. This group grows up from the highest handlers toward lowers handlers until the whole range is used or the last 16 bit-uuid service is reached.
---
src/attrib-server.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++-1 files changed, 46 insertions(+), 1 deletions(-)src/attrib-server.c +10
-1@@ -1317,7 +1317,52 @@ static uint16_t find_uuid16_avail(struct btd_adapter *adapter, uint16_t nitems) static uint16_t find_uuid128_avail(struct btd_adapter *adapter, uint16_t nitems) { - /* TODO: Allocate 128 uuids at the end of the list */ + struct gatt_server *server; + uint16_t handle = 0, end = 0xffff; + gboolean pick = TRUE; + GList *dl; + GSList *l; + + l = g_slist_find_custom(servers, adapter, adapter_cmp); + if (l == NULL) + return 0; +src/attrib-server.c static uint16_t find_uuid16_avail(struct btd_adapter *adapter, uint16_t nitems) · line 1339
Quoted 10 lines
src/attrib-server.c +10
-0+ server = l->data; + if (server->database == NULL) + return 0xffff - nitems + 1; + + for (dl = g_list_last(server->database); dl; dl = dl->prev) { + struct attribute *a = dl->data; + if (pick) { + handle = a->handle; + pick = FALSE; + }I suspect you can get this code a lot simpler without using this "pick" variable. Unfortunately, I don't have time to look at this further right now.
src/attrib-server.c static uint16_t find_uuid16_avail(struct btd_adapter *adapter, uint16_t nitems) · line 1343
Quoted 4 lines
src/attrib-server.c +4
-0+ + if (bt_uuid_cmp(&a->uuid, &prim_uuid) != 0 && + bt_uuid_cmp(&a->uuid, &snd_uuid) != 0) + continue;At this point you know
a->uuidis either primary or secondary. So I don't think it is necessary to check again below.
Quoted 9 lines
src/attrib-server.c static uint16_t find_uuid16_avail(struct btd_adapter *adapter, uint16_t nitems) · line 1348
Quoted 5 lines
src/attrib-server.c +5
-0+ + if ((bt_uuid_cmp(&a->uuid, &prim_uuid) == 0 || + bt_uuid_cmp(&a->uuid, &snd_uuid) == 0) && + end - handle >= nitems) + return end - nitems + 1;Same below. At this point, they are guaranteed to be either primary or secondary uuids. You only need to check for
a->len.
Thanks for the feedback, I'll send a new set with the changes suggested. Regards.