device : Add EndGroupHandle key in attributes storage
PATCH v2 1/5 device : Add EndGroupHandle key in attributes storage
End group handle should also be converted/saved for each group in device's attributes file.
--- doc/settings-storage.txt | 8 +++++--- src/adapter.c | 8 +++++--- src/device.c | 10 ++++++++++ 3 files changed, 20 insertions(+), 6 deletions(-)
doc/settings-storage.txt +5 -3
@@ -94,10 +94,12 @@ Attributes are stored using their handle as group name (decimal format). Each group contains: - UUID String 128-bit UUID of the attribute + UUID String 128-bit UUID of the attribute - Value String Value of the attribute as hexadecimal encoded - string + Value String Value of the attribute as hexadecimal encoded + string + + EndGroupHandle Integer End group handle in decimal format Sample: [1]
src/adapter.c +5 -3
@@ -2419,7 +2419,8 @@ static gboolean record_has_uuid(const sdp_record_t *rec, }static void store_attribute_uuid (GKeyFile *key_file,uint16_t start, -char *att_uuid,uuid_t uuid) +uint16_t end,char *att_uuid, +uuid_t uuid) {char handle[6 ], uuid_str[33 ];int i;
@@ -2443,6 +2444,7 @@ static void store_attribute_uuid(GKeyFile *key_file, uint16_t start,sprintf (handle,"%hu" , start);g_key_file_set_string (key_file, handle,"UUID" , att_uuid);g_key_file_set_string (key_file, handle,"Value" , uuid_str); +g_key_file_set_integer (key_file, handle,"EndGroupHandle" , end); }static void store_sdp_record (char *local,char *peer,int handle,char *value)
@@ -2535,7 +2537,7 @@ static void convert_sdp_entry(char *key, char *value, void *user_data) key_file =g_key_file_new ();g_key_file_load_from_file (key_file, filename,0 ,NULL ); -store_attribute_uuid (key_file, start, prim_uuid, uuid); +store_attribute_uuid (key_file, start, end, prim_uuid, uuid); data =g_key_file_to_data (key_file, &length,NULL );if (length >0 ) {
@@ -2597,7 +2599,7 @@ static void convert_primaries_entry(char *key, char *value, void *user_data)bt_string2uuid (&uuid, uuid_str);sdp_uuid128_to_uuid (&uuid); -store_attribute_uuid (key_file, start, prim_uuid, uuid); +store_attribute_uuid (key_file, start, end, prim_uuid, uuid); }g_strfreev (services);
src/device.c +10 -0
@@ -1907,6 +1907,7 @@ static void load_att_info(struct btd_device *device, const gchar *local,for (handle = groups; *handle; handle++) {gboolean uuid_ok; +gint end; str =g_key_file_get_string (key_file, *handle,"UUID" ,NULL );if (!str)
@@ -1922,8 +1923,16 @@ static void load_att_info(struct btd_device *device, const gchar *local,if (!str)continue ; + end =g_key_file_get_integer (key_file, *handle, +"EndGroupHandle" ,NULL ); +if (end ==0 ) { +g_free (str); +continue ; + } + prim = g_new0(struct gatt_primary, 1); prim->range.start =atoi (*handle); + prim->range.end = end;switch (strlen (str)) {case 4 :
@@ -2524,6 +2533,7 @@ static void store_primaries_from_sdp_record(GKeyFile *key_file,g_key_file_set_string (key_file, handle,"UUID" , prim_uuid);g_key_file_set_string (key_file, handle,"Value" , uuid_str); +g_key_file_set_integer (key_file, handle,"EndGroupHandle" , end); done:g_free (prim_uuid);
Signature · 1 line
1.7.9.5
PATCH v2 2/5 device: Store services in device's attributes file
Remove no more used write_device_primaries() from storage.[ch].
--- src/device.c | 73 +++++++++++++++++++++++++++++++++++++++++---------------- src/storage.c | 15 ------------ src/storage.h | 2 -- 3 files changed, 53 insertions(+), 37 deletions(-)
src/device.c +53 -20
@@ -2872,37 +2872,70 @@ static void init_browse(struct browse_req *req, gboolean reverse) l->data); } -static char *primary_list_to_string (GSList *primary_list) +static void store_services (struct btd_device *device) { -GString *services; +struct btd_adapter *adapter = device->adapter; +char filename[PATH_MAX +1 ]; +char src_addr[18 ], dst_addr[18 ]; +uuid_t uuid; +char *prim_uuid; +GKeyFile *key_file;GSList *l; +char *data; +gsize length =0 ; - services =g_string_new (NULL ); +sdp_uuid16_create (&uuid,GATT_PRIM_SVC_UUID ); + prim_uuid =bt_uuid2string (&uuid); -for (l = primary_list; l; l = l->next) { -struct gatt_primary *primary = l->data; -char service[64 ]; +ba2str (adapter_get_address (adapter), src_addr); +ba2str (&device->bdaddr, dst_addr); -memset (service,0 ,sizeof (service)); +snprintf (filename,PATH_MAX ,STORAGEDIR "/%s/%s/attributes" , src_addr, + dst_addr); + filename[PATH_MAX ] ='\0' ; -snprintf (service,sizeof (service),"%04X#%04X#%s " , - primary->range.start, primary->range.end, primary->uuid); + key_file =g_key_file_new (); - services =g_string_append (services, service); - } +for (l = device->primaries; l; l = l->next) { +struct gatt_primary *primary = l->data; +char handle[6 ], uuid_str[33 ]; +int i; -return g_string_free (services, FALSE); -} +sprintf (handle,"%hu" , primary->range.start); -static void store_services (struct btd_device *device) -{ -struct btd_adapter *adapter = device->adapter; -char *str =primary_list_to_string (device->primaries); +bt_string2uuid (&uuid, primary->uuid); +sdp_uuid128_to_uuid (&uuid); -write_device_primaries (adapter_get_address (adapter), &device->bdaddr, - device->bdaddr_type, str); +switch (uuid.type) { +case SDP_UUID16 : +sprintf (uuid_str,"%4.4X" , uuid.value.uuid16); +break ; +case SDP_UUID32 : +sprintf (uuid_str,"%8.8X" , uuid.value.uuid32); +break ; +case SDP_UUID128 : +for (i =0 ; i <16 ; i++) +sprintf (uuid_str + (i *2 ),"%2.2X" , + uuid.value.uuid128.data[i]); +break ; +default : + uuid_str[0 ] ='\0' ; + } -g_free (str); +g_key_file_set_string (key_file, handle,"UUID" , prim_uuid); +g_key_file_set_string (key_file, handle,"Value" , uuid_str); +g_key_file_set_integer (key_file, handle,"EndGroupHandle" , + primary->range.end); + } + + data =g_key_file_to_data (key_file, &length,NULL ); +if (length >0 ) { +create_file (filename,S_IRUSR |S_IWUSR |S_IRGRP |S_IROTH ); +g_file_set_contents (filename, data, length,NULL ); + } + +g_free (data); +g_key_file_free (key_file); }static void attio_connected (gpointer data,gpointer user_data)
src/storage.c +0 -15
@@ -273,21 +273,6 @@ int read_device_pairable(const bdaddr_t *bdaddr, gboolean *mode)return 0 ; } -int write_device_primaries (const bdaddr_t *sba,const bdaddr_t *dba, -uint8_t bdaddr_type,const char *services) -{ -char filename[PATH_MAX +1 ], key[20 ]; - -create_filename (filename,PATH_MAX , sba,"primaries" ); - -create_file (filename,S_IRUSR |S_IWUSR |S_IRGRP |S_IROTH ); - -ba2str (dba, key); -sprintf (&key[17 ],"#%hhu" , bdaddr_type); - -return textfile_put (filename, key, services); -} -static void filter_keys (char *key,char *value,void *data) {struct match *match = data;
src/storage.h +0 -2
@@ -35,8 +35,6 @@ ssize_t read_pin_code(const bdaddr_t *local, const bdaddr_t *peer, char *pin);sdp_record_t *record_from_string (const gchar *str);sdp_record_t *find_record_in_list (sdp_list_t *recs,const char *uuid);int read_device_pairable (const bdaddr_t *local,gboolean *mode); -int write_device_primaries (const bdaddr_t *sba,const bdaddr_t *dba, -uint8_t bdaddr_type,const char *services);int read_device_ccc (const bdaddr_t *local,const bdaddr_t *peer,uint8_t bdaddr_type,uint16_t handle,uint16_t *value); int write_device_ccc(const bdaddr_t *local, const bdaddr_t *peer,
Signature · 1 line
1.7.9.5
PATCH v2 3/5 adapter: Convert ccc file
In reply to Frédéric DanisUpdate settings-storage.txt documentation to add ccc file.
--- doc/settings-storage.txt | 17 +++++++++++++ src/adapter.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+)
doc/settings-storage.txt +17 -0
@@ -31,6 +31,8 @@ contains: contains: - an info file - an attributes file containing attributes of remote LE services + - a ccc file containing persistent Client Characteristic Configuration + (CCC) descriptor information for GATT characteristics So the directory structure is: /var/lib/bluetooth/<adapter address>/
@@ -43,6 +45,7 @@ So the directory structure is: ./<remote device address>/ ./info ./attributes + ./ccc ./<remote device address>/ ./info ./attributes
@@ -114,6 +117,20 @@ Sample: UUID=00002a00-0000-1000-8000-00805f9b34fb Value=4578616D706C6520446576696365 +CCC file format +====================== + +The ccc file stores the current CCC descriptor values for GATT characteristics +which have notification/indication enabled by the remote device. + +Information is stored using CCC attribute handle as group name (in decimal +format). + +Each group contains: + + Value String CCC descriptor value encoded in + hexadecimal + Cache directory file format ============================
src/adapter.c +63 -0
75 lines not on this page · raw message
Signature · 1 line
1.7.9.5
PATCH v2 4/5 device: Add btd_device_get_storage_path()
In reply to Frédéric Danis--- src/device.c | 15 +++++++++++++++ src/device.h | 2 ++ 2 files changed, 17 insertions(+)
src/device.c +15 -0
@@ -2049,6 +2049,21 @@ struct btd_device *device_create(struct btd_adapter *adapter,return btd_device_ref (device); } +char *btd_device_get_storage_path (struct btd_device *device, +const char *filename) +{ +char srcaddr[18 ], dstaddr[18 ]; + +ba2str (adapter_get_address (device->adapter), srcaddr); +ba2str (&device->bdaddr, dstaddr); + +if (!filename) +return g_strdup_printf (STORAGEDIR "/%s/%s" , srcaddr, dstaddr); + +return g_strdup_printf (STORAGEDIR "/%s/%s/%s" , srcaddr, dstaddr, + filename); +} +void device_set_name (struct btd_device *device,const char *name) {if (strncmp (name, device->name,MAX_NAME_LENGTH ) ==0 )
src/device.h +2 -0
@@ -30,6 +30,8 @@ struct btd_device *device_create(struct btd_adapter *adapter, const char *address, uint8_t bdaddr_type);struct btd_device *device_create_from_storage (struct btd_adapter *adapter,const char *address,GKeyFile *key_file); +char *btd_device_get_storage_path (struct btd_device *device, +const char *filename);void device_set_name (struct btd_device *device,const char *name);void device_get_name (struct btd_device *device,char *name,size_t len);
Signature · 1 line
1.7.9.5
PATCH v2 5/5 attrib-server: Read/write CCC info from new storage
In reply to Frédéric DanisRemove no more used storage functions.
--- src/attrib-server.c | 79 +++++++++++++++++++++++++++++++++++++--------- src/storage.c | 87 --------------------------------------------------- src/storage.h | 6 ---- 3 files changed, 64 insertions(+), 108 deletions(-)
src/attrib-server.c +64 -15
@@ -32,6 +32,7 @@#include <string.h> #include <unistd.h> #include <glib.h> +#include <sys/file.h> #include <bluetooth/bluetooth.h> #include <bluetooth/uuid.h>
@@ -746,6 +747,36 @@ static uint16_t find_by_type(struct gatt_channel *channel, uint16_t start,return len; } +static int read_device_ccc (struct btd_device *device,uint16_t handle, +uint16_t *value) +{ +char *filename; +GKeyFile *key_file; +char group[6 ]; +char *str; +unsigned int config; +int err =0 ; + + filename =btd_device_get_storage_path (device,"ccc" ); + + key_file =g_key_file_new (); +g_key_file_load_from_file (key_file, filename,0 ,NULL ); + +sprintf (group,"%hu" , handle); + + str =g_key_file_get_string (key_file, group,"Value" ,NULL ); +if (!str ||sscanf (str,"%04X" , &config) !=1 ) + err = -ENOENT ; +else + *value = config; + +g_free (str); +g_free (filename); +g_key_file_free (key_file); + +return err; +} +static uint16_t read_value (struct gatt_channel *channel,uint16_t handle,uint8_t *pdu,size_t len) {
@@ -753,7 +784,6 @@ static uint16_t read_value(struct gatt_channel *channel, uint16_t handle,uint8_t status;GList *l;uint16_t cccval; -uint8_t bdaddr_type;guint h = handle; l = g_list_find_custom(channel->server->database,
@@ -764,11 +794,8 @@ static uint16_t read_value(struct gatt_channel *channel, uint16_t handle, a = l->data; - bdaddr_type =device_get_addr_type (channel->device); -if (bt_uuid_cmp (&ccc_uuid, &a->uuid) ==0 && -read_device_ccc (&channel->src, &channel->dst, bdaddr_type, - handle, &cccval) ==0 ) { +read_device_ccc (channel->device, handle, &cccval) ==0 ) {uint8_t config[2 ];att_put_u16 (cccval, config);
@@ -794,7 +821,6 @@ static uint16_t read_blob(struct gatt_channel *channel, uint16_t handle,uint8_t status;GList *l;uint16_t cccval; -uint8_t bdaddr_type;guint h = handle; l = g_list_find_custom(channel->server->database,
@@ -809,11 +835,8 @@ static uint16_t read_blob(struct gatt_channel *channel, uint16_t handle,return enc_error_resp (ATT_OP_READ_BLOB_REQ , handle,ATT_ECODE_INVALID_OFFSET , pdu, len); - bdaddr_type =device_get_addr_type (channel->device); -if (bt_uuid_cmp (&ccc_uuid, &a->uuid) ==0 && -read_device_ccc (&channel->src, &channel->dst, bdaddr_type, - handle, &cccval) ==0 ) { +read_device_ccc (channel->device, handle, &cccval) ==0 ) {uint8_t config[2 ];att_put_u16 (cccval, config);
@@ -869,10 +892,31 @@ static uint16_t write_value(struct gatt_channel *channel, uint16_t handle, } }else {uint16_t cccval =att_get_u16 (value); -uint8_t bdaddr_type =device_get_addr_type (channel->device); +char *filename; +GKeyFile *key_file; +char group[6 ], value[5 ]; +char *data; +gsize length =0 ; + + filename =btd_device_get_storage_path (channel->device,"ccc" ); + + key_file =g_key_file_new (); +g_key_file_load_from_file (key_file, filename,0 ,NULL ); + +sprintf (group,"%hu" , handle); +sprintf (value,"%hhX" , cccval); +g_key_file_set_string (key_file, group,"Value" , value); + + data =g_key_file_to_data (key_file, &length,NULL ); +if (length >0 ) { +create_file (filename, +S_IRUSR |S_IWUSR |S_IRGRP |S_IROTH ); +g_file_set_contents (filename, data, length,NULL ); + } -write_device_ccc (&channel->src, &channel->dst, bdaddr_type, - handle, cccval); +g_free (data); +g_free (filename); +g_key_file_free (key_file); }return enc_write_resp (pdu, len);
@@ -1089,8 +1133,13 @@ guint attrib_channel_attach(GAttrib *attrib)ba2str (&channel->dst, addr); device =adapter_find_device (server->adapter, addr); -if (device ==NULL ||device_is_bonded (device) == FALSE) -delete_device_ccc (&channel->src, &channel->dst); +if (device ==NULL ||device_is_bonded (device) == FALSE) { +char *filename; + + filename =btd_device_get_storage_path (channel->device,"ccc" ); +unlink (filename); +g_free (filename); + }if (cid !=ATT_CID ) { channel->le = FALSE;
src/storage.c +0 -87
90 lines not on this page · raw message
src/storage.h +0 -6
@@ -35,9 +35,3 @@ ssize_t read_pin_code(const bdaddr_t *local, const bdaddr_t *peer, char *pin);sdp_record_t *record_from_string (const gchar *str);sdp_record_t *find_record_in_list (sdp_list_t *recs,const char *uuid);int read_device_pairable (const bdaddr_t *local,gboolean *mode); -int read_device_ccc (const bdaddr_t *local,const bdaddr_t *peer, -uint8_t bdaddr_type,uint16_t handle,uint16_t *value); -int write_device_ccc (const bdaddr_t *local,const bdaddr_t *peer, -uint8_t bdaddr_type,uint16_t handle, -uint16_t value); -void delete_device_ccc (const bdaddr_t *local,const bdaddr_t *peer);
Signature · 1 line
1.7.9.5
Hi Frédéric,
Quoted 7 lines · Frédéric Danis
End group handle should also be converted/saved for each group in device's attributes file.
--- doc/settings-storage.txt | 8 +++++--- src/adapter.c | 8 +++++--- src/device.c | 10 ++++++++++ 3 files changed, 20 insertions(+), 6 deletions(-)
All patches in this set have been applied. Thanks.
Johan