2012-05-23 18:48:18

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ 00/15] Store address type on storage - remaining files

This patchset contains the remaining changes for the new storage format
used (bdaddr#type) on storage files (LE-only or shared files).

The files that have been changed so far are:
primaries, characteristics, appearance, ccc, names, aliases,
longtermkeys, attributes, blocked.

Unchanged files:
lastseen, lastused, eir, classes, manufacturers, features, linkkeys,
pincodes, trusts, profiles.

Maybe we should initially use the new storage format in lastseen,
lastused and trusts files as well. However, I'm not quite sure about
that. So, what do you guys think about using the new storage format in
these files ?


Claudio Takahasi (4):
storage: Store address type in "aliases" file
storage: Store address type in blocked file
storage: Rename characteristic to characteristics
storage: Rename primary file to primaries

Paulo Alcantara (11):
storage: Store address type in "characteristics"
storage: Store address type in "appearance" file
storage: Store address type in "ccc" file
storage: Store address type in "names" file
core: Fix reading stored data from "aliases" and "names" file
storage: Do not store address type twice
storage: Store address type in "longtermkeys" file
core: Fix creating device from "longtermkeys" file
storage: Store address type in "attributes" file
core: Fix creating device from "blocked" file
core: Fix deleting entries

attrib/client.c | 31 +++++++----
input/device.c | 6 ++-
src/adapter.c | 64 ++++++++++++++--------
src/attrib-server.c | 19 +++++--
src/device.c | 49 +++++++++++------
src/device.h | 1 +
src/event.c | 13 +++--
src/storage.c | 147 +++++++++++++++++++++++++++++---------------------
src/storage.h | 44 +++++++++------
9 files changed, 232 insertions(+), 142 deletions(-)

--
1.7.7.6



2012-05-25 15:46:56

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 04/18] storage: Store address type in "names" file

"names" file is shared between BR/EDR and BLE. Addressing types can be
either BR/EDR, BLE public or BLE random so the entries in the "names"
file did not contain enough information to distinguish which addressing
type it's supposed to be. Entries will now contain both address number,
and address type as a single key in every entry in the file.
---
input/device.c | 6 ++++--
src/adapter.c | 3 ++-
src/device.c | 3 ++-
src/event.c | 7 ++++++-
src/storage.c | 20 +++++++++++++-------
src/storage.h | 6 ++++--
6 files changed, 31 insertions(+), 14 deletions(-)

diff --git a/input/device.c b/input/device.c
index e2c48b9..be12544 100644
--- a/input/device.c
+++ b/input/device.c
@@ -1084,10 +1084,11 @@ static struct input_device *input_device_new(DBusConnection *conn,
struct btd_adapter *adapter = device_get_adapter(device);
struct input_device *idev;
char name[249], src_addr[18], dst_addr[18];
+ uint8_t bdaddr_type;

idev = g_new0(struct input_device, 1);
adapter_get_address(adapter, &idev->src);
- device_get_address(device, &idev->dst, NULL);
+ device_get_address(device, &idev->dst, &bdaddr_type);
idev->device = btd_device_ref(device);
idev->path = g_strdup(path);
idev->conn = dbus_connection_ref(conn);
@@ -1096,7 +1097,8 @@ static struct input_device *input_device_new(DBusConnection *conn,

ba2str(&idev->src, src_addr);
ba2str(&idev->dst, dst_addr);
- if (read_device_name(src_addr, dst_addr, name) == 0)
+
+ if (read_device_name(src_addr, dst_addr, bdaddr_type, name) == 0)
idev->name = g_strdup(name);

if (g_dbus_register_interface(conn, idev->path, INPUT_DEVICE_INTERFACE,
diff --git a/src/adapter.c b/src/adapter.c
index 1542e06..a364935 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2874,7 +2874,8 @@ void adapter_update_found_devices(struct btd_adapter *adapter,
eir_data.appearance);

if (eir_data.name != NULL && eir_data.name_complete)
- write_device_name(&adapter->bdaddr, bdaddr, eir_data.name);
+ write_device_name(&adapter->bdaddr, bdaddr, bdaddr_type,
+ eir_data.name);

dev = adapter_search_found_devices(adapter, bdaddr);
if (dev) {
diff --git a/src/device.c b/src/device.c
index a01a524..e239da6 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1075,7 +1075,8 @@ struct btd_device *device_create(DBusConnection *conn,
device->bdaddr_type = bdaddr_type;
adapter_get_address(adapter, &src);
ba2str(&src, srcaddr);
- read_device_name(srcaddr, address, device->name);
+
+ read_device_name(srcaddr, address, bdaddr_type, device->name);
if (read_device_alias(srcaddr, address, alias, sizeof(alias)) == 0)
device->alias = g_strdup(alias);
device->trusted = read_trust(&src, address, GLOBAL_TRUST);
diff --git a/src/event.c b/src/event.c
index 8ecb942..e453e13 100644
--- a/src/event.c
+++ b/src/event.c
@@ -319,6 +319,7 @@ void btd_event_remote_name(bdaddr_t *local, bdaddr_t *peer, char *name)
{
struct btd_adapter *adapter;
struct btd_device *device;
+ uint8_t bdaddr_type;
struct remote_dev_info *dev_info;

if (!g_utf8_validate(name, -1, NULL)) {
@@ -333,11 +334,15 @@ void btd_event_remote_name(bdaddr_t *local, bdaddr_t *peer, char *name)
g_strstrip(name);
}

- write_device_name(local, peer, name);
+

if (!get_adapter_and_device(local, peer, &adapter, &device, FALSE))
return;

+ bdaddr_type = device_get_addr_type(device);
+
+ write_device_name(local, peer, bdaddr_type, name);
+
dev_info = adapter_search_found_devices(adapter, peer);
if (dev_info) {
g_free(dev_info->name);
diff --git a/src/storage.c b/src/storage.c
index 3cde9c1..6fe9279 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -358,9 +358,10 @@ int read_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t *class)
return 0;
}

-int write_device_name(bdaddr_t *local, bdaddr_t *peer, char *name)
+int write_device_name(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ char *name)
{
- char filename[PATH_MAX + 1], addr[18], str[HCI_MAX_NAME_LENGTH + 1];
+ char filename[PATH_MAX + 1], key[20], str[HCI_MAX_NAME_LENGTH + 1];
int i;

memset(str, 0, sizeof(str));
@@ -374,18 +375,23 @@ int write_device_name(bdaddr_t *local, bdaddr_t *peer, char *name)

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

- ba2str(peer, addr);
- return textfile_put(filename, addr, str);
+ ba2str(peer, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);
+
+ return textfile_put(filename, key, str);
}

-int read_device_name(const char *src, const char *dst, char *name)
+int read_device_name(const char *src, const char *dst, uint8_t bdaddr_type,
+ char *name)
{
- char filename[PATH_MAX + 1], *str;
+ char filename[PATH_MAX + 1], *str, key[20];
int len;

create_name(filename, PATH_MAX, STORAGEDIR, src, "names");

- str = textfile_get(filename, dst);
+ snprintf(key, sizeof(key), "%17s#%hhu", dst, bdaddr_type);
+
+ str = textfile_get(filename, key);
if (!str)
return -ENOENT;

diff --git a/src/storage.h b/src/storage.h
index b6d24e9..7443d6f 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -42,8 +42,10 @@ int read_remote_appearance(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
uint16_t *appearance);
int write_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t class);
int read_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t *class);
-int write_device_name(bdaddr_t *local, bdaddr_t *peer, char *name);
-int read_device_name(const char *src, const char *dst, char *name);
+int write_device_name(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ char *name);
+int read_device_name(const char *src, const char *dst, uint8_t bdaddr_type,
+ char *name);
int write_remote_eir(bdaddr_t *local, bdaddr_t *peer, uint8_t *data,
uint8_t data_len);
int read_remote_eir(bdaddr_t *local, bdaddr_t *peer, uint8_t *data);
--
1.7.7.6


2012-05-25 15:46:55

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 03/18] storage: Store address type in "ccc" file

Addressing types can be either BR/EDR, BLE public or BLE random so
the entries in the "ccc" file did not contain enough information to
distinguish which addressing type it's supposed to be. Entries will
now contain both address number and address type as a single key in
every entry in the file.
---
src/attrib-server.c | 19 ++++++++++++++-----
src/device.c | 5 +++++
src/device.h | 1 +
src/storage.c | 20 ++++++++++----------
src/storage.h | 8 ++++----
5 files changed, 34 insertions(+), 19 deletions(-)

diff --git a/src/attrib-server.c b/src/attrib-server.c
index 21b1501..3291e2d 100644
--- a/src/attrib-server.c
+++ b/src/attrib-server.c
@@ -737,6 +737,7 @@ 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,
@@ -747,9 +748,11 @@ 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,
- handle, &cccval) == 0) {
+ read_device_ccc(&channel->src, &channel->dst, bdaddr_type,
+ handle, &cccval) == 0) {
uint8_t config[2];

att_put_u16(cccval, config);
@@ -775,6 +778,7 @@ 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,
@@ -789,9 +793,11 @@ 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,
- handle, &cccval) == 0) {
+ read_device_ccc(&channel->src, &channel->dst, bdaddr_type,
+ handle, &cccval) == 0) {
uint8_t config[2];

att_put_u16(cccval, config);
@@ -847,7 +853,10 @@ static uint16_t write_value(struct gatt_channel *channel, uint16_t handle,
}
} else {
uint16_t cccval = att_get_u16(value);
- write_device_ccc(&channel->src, &channel->dst, handle, cccval);
+ uint8_t bdaddr_type = device_get_addr_type(channel->device);
+
+ write_device_ccc(&channel->src, &channel->dst, bdaddr_type,
+ handle, cccval);
}

return enc_write_resp(pdu, len);
diff --git a/src/device.c b/src/device.c
index eb015ec..a01a524 100644
--- a/src/device.c
+++ b/src/device.c
@@ -2205,6 +2205,11 @@ void device_set_addr_type(struct btd_device *device, uint8_t bdaddr_type)
device->bdaddr_type = bdaddr_type;
}

+uint8_t device_get_addr_type(struct btd_device *device)
+{
+ return device->bdaddr_type;
+}
+
const gchar *device_get_path(struct btd_device *device)
{
if (!device)
diff --git a/src/device.h b/src/device.h
index 51140c7..26e17f7 100644
--- a/src/device.h
+++ b/src/device.h
@@ -62,6 +62,7 @@ struct btd_adapter *device_get_adapter(struct btd_device *device);
void device_get_address(struct btd_device *device, bdaddr_t *bdaddr,
uint8_t *bdaddr_type);
void device_set_addr_type(struct btd_device *device, uint8_t bdaddr_type);
+uint8_t device_get_addr_type(struct btd_device *device);
const gchar *device_get_path(struct btd_device *device);
struct agent *device_get_agent(struct btd_device *device);
gboolean device_is_bredr(struct btd_device *device);
diff --git a/src/storage.c b/src/storage.c
index ab9b1ed..3cde9c1 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1229,12 +1229,12 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
create_filename(filename, PATH_MAX, sba, "attributes");
delete_by_pattern(filename, key);

+ sprintf(&key[17], "#%hhu", bdaddr_type);
+
/* Deleting all CCC values of a given key */
create_filename(filename, PATH_MAX, sba, "ccc");
delete_by_pattern(filename, key);

- sprintf(&key[17], "#%hhu", bdaddr_type);
-
create_filename(filename, PATH_MAX, sba, "primary");

return textfile_del(filename, key);
@@ -1307,10 +1307,10 @@ int read_device_attributes(const bdaddr_t *sba, textfile_cb func, void *data)
return textfile_foreach(filename, func, data);
}

-int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint16_t handle,
- uint16_t *value)
+int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ uint16_t handle, uint16_t *value)
{
- char filename[PATH_MAX + 1], addr[18], key[23];
+ char filename[PATH_MAX + 1], addr[18], key[25];
char *str;
unsigned int config;
int err = 0;
@@ -1318,7 +1318,7 @@ int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint16_t handle,
create_filename(filename, PATH_MAX, local, "ccc");

ba2str(peer, addr);
- snprintf(key, sizeof(key), "%17s#%04X", addr, handle);
+ snprintf(key, sizeof(key), "%17s#%hhu#%04X", addr, bdaddr_type, handle);

str = textfile_caseget(filename, key);
if (str == NULL)
@@ -1334,18 +1334,18 @@ int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint16_t handle,
return err;
}

-int write_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint16_t handle,
- uint16_t value)
+int write_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ uint16_t handle, uint16_t value)
{
- char filename[PATH_MAX + 1], addr[18], key[23], config[5];
+ char filename[PATH_MAX + 1], addr[18], key[25], config[5];

create_filename(filename, PATH_MAX, local, "ccc");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

ba2str(peer, addr);
+ snprintf(key, sizeof(key), "%17s#%hhu#%04X", addr, bdaddr_type, handle);

- snprintf(key, sizeof(key), "%17s#%04X", addr, handle);
snprintf(config, sizeof(config), "%04X", value);

return textfile_put(filename, key, config);
diff --git a/src/storage.h b/src/storage.h
index db33b43..b6d24e9 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -91,10 +91,10 @@ char *read_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
int write_device_attribute(const bdaddr_t *sba, const bdaddr_t *dba,
uint16_t handle, const char *chars);
int read_device_attributes(const bdaddr_t *sba, textfile_cb func, void *data);
-int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint16_t handle,
- uint16_t *value);
-int write_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint16_t handle,
- uint16_t value);
+int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ uint16_t handle, uint16_t *value);
+int write_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ uint16_t handle, uint16_t value);
void delete_device_ccc(bdaddr_t *local, bdaddr_t *peer);
int write_longtermkeys(bdaddr_t *local, bdaddr_t *peer, const char *key);
gboolean has_longtermkeys(bdaddr_t *local, bdaddr_t *peer);
--
1.7.7.6


2012-05-25 15:47:00

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 08/18] storage: Do not store address type twice

"longtermkeys" file already includes address type in its keys. It isn't
necessary to store the address type in its value as well.
---
src/event.c | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/event.c b/src/event.c
index e453e13..a1fc016 100644
--- a/src/event.c
+++ b/src/event.c
@@ -385,8 +385,8 @@ static int store_longtermkey(bdaddr_t *local, bdaddr_t *peer,
newkey = g_string_new(val);
g_free(val);

- g_string_append_printf(newkey, " %d %d %d %d %d ", bdaddr_type,
- authenticated, master, enc_size, ediv);
+ g_string_append_printf(newkey, " %d %d %d %d ", authenticated, master,
+ enc_size, ediv);

str = buf2str(rand, 8);
if (str == NULL) {
--
1.7.7.6


2012-05-25 15:46:59

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 07/18] storage: Store address type in blocked file

From: Claudio Takahasi <[email protected]>

---
src/device.c | 6 +++---
src/storage.c | 21 ++++++++++++---------
src/storage.h | 5 +++--
3 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/src/device.c b/src/device.c
index a91eba7..e44ce58 100644
--- a/src/device.c
+++ b/src/device.c
@@ -547,7 +547,7 @@ int device_block(DBusConnection *conn, struct btd_device *device,

adapter_get_address(device->adapter, &src);

- err = write_blocked(&src, &device->bdaddr, TRUE);
+ err = write_blocked(&src, &device->bdaddr, device->bdaddr_type, TRUE);
if (err < 0)
error("write_blocked(): %s (%d)", strerror(-err), -err);

@@ -579,7 +579,7 @@ int device_unblock(DBusConnection *conn, struct btd_device *device,

adapter_get_address(device->adapter, &src);

- err = write_blocked(&src, &device->bdaddr, FALSE);
+ err = write_blocked(&src, &device->bdaddr, device->bdaddr_type, FALSE);
if (err < 0)
error("write_blocked(): %s (%d)", strerror(-err), -err);

@@ -1082,7 +1082,7 @@ struct btd_device *device_create(DBusConnection *conn,
device->alias = g_strdup(alias);
device->trusted = read_trust(&src, address, GLOBAL_TRUST);

- if (read_blocked(&src, &device->bdaddr))
+ if (read_blocked(&src, &device->bdaddr, device->bdaddr_type))
device_block(conn, device, FALSE);

if (read_link_key(&src, &device->bdaddr, NULL, NULL) == 0) {
diff --git a/src/storage.c b/src/storage.c
index f2eaae1..1eb916f 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1144,15 +1144,17 @@ int read_device_pairable(bdaddr_t *bdaddr, gboolean *mode)
return 0;
}

-gboolean read_blocked(const bdaddr_t *local, const bdaddr_t *remote)
+gboolean read_blocked(const bdaddr_t *local, const bdaddr_t *remote,
+ uint8_t bdaddr_type)
{
- char filename[PATH_MAX + 1], *str, addr[18];
+ char filename[PATH_MAX + 1], *str, key[20];

create_filename(filename, PATH_MAX, local, "blocked");

- ba2str(remote, addr);
+ ba2str(remote, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);

- str = textfile_caseget(filename, addr);
+ str = textfile_caseget(filename, key);
if (!str)
return FALSE;

@@ -1162,20 +1164,21 @@ gboolean read_blocked(const bdaddr_t *local, const bdaddr_t *remote)
}

int write_blocked(const bdaddr_t *local, const bdaddr_t *remote,
- gboolean blocked)
+ uint8_t bdaddr_type, gboolean blocked)
{
- char filename[PATH_MAX + 1], addr[18];
+ char filename[PATH_MAX + 1], key[20];

create_filename(filename, PATH_MAX, local, "blocked");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

- ba2str(remote, addr);
+ ba2str(remote, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);

if (blocked == FALSE)
- return textfile_casedel(filename, addr);
+ return textfile_casedel(filename, key);

- return textfile_caseput(filename, addr, "");
+ return textfile_caseput(filename, key, "");
}

int write_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
diff --git a/src/storage.h b/src/storage.h
index eda7f9b..9032801 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -78,9 +78,10 @@ int read_device_id(const gchar *src, const gchar *dst,
uint16_t *product, uint16_t *version);
int write_device_pairable(bdaddr_t *local, gboolean mode);
int read_device_pairable(bdaddr_t *local, gboolean *mode);
-gboolean read_blocked(const bdaddr_t *local, const bdaddr_t *remote);
+gboolean read_blocked(const bdaddr_t *local, const bdaddr_t *remote,
+ uint8_t bdaddr_type);
int write_blocked(const bdaddr_t *local, const bdaddr_t *remote,
- gboolean blocked);
+ uint8_t bdaddr_type, gboolean blocked);
int write_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
uint8_t bdaddr_type, const char *services);
int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
--
1.7.7.6


2012-05-25 15:47:09

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 17/18] storage: Keep backward compatibility in "names" file

The "names" file is shared by both BR/EDR and LE devices, so we must keep
backward compatibility with the old storage format.
---
src/storage.c | 10 +++++++++-
1 files changed, 9 insertions(+), 1 deletions(-)

diff --git a/src/storage.c b/src/storage.c
index cb7cde2..97817cc 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -400,9 +400,17 @@ int read_device_name(const char *src, const char *dst, uint8_t bdaddr_type,
snprintf(key, sizeof(key), "%17s#%hhu", dst, bdaddr_type);

str = textfile_get(filename, key);
- if (!str)
+ if (str != NULL)
+ goto done;
+
+ /* Try old format (address only) */
+ key[17] = '\0';
+
+ str = textfile_get(filename, key);
+ if (str == NULL)
return -ENOENT;

+done:
len = strlen(str);
if (len > HCI_MAX_NAME_LENGTH)
str[HCI_MAX_NAME_LENGTH] = '\0';
--
1.7.7.6


2012-05-25 15:47:10

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 18/18] storage: Keep backward compatibility in "aliases" file

The "aliases" file is shared by both BR/EDR and LE devices, so we must
keep backward compatibility with the old storage format.
---
src/storage.c | 10 +++++++++-
1 files changed, 9 insertions(+), 1 deletions(-)

diff --git a/src/storage.c b/src/storage.c
index 97817cc..3374fc0 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -73,9 +73,17 @@ int read_device_alias(const char *src, const char *dst, uint8_t bdaddr_type,
snprintf(key, sizeof(key), "%17s#%hhu", dst, bdaddr_type);

tmp = textfile_get(filename, key);
- if (!tmp)
+ if (tmp != NULL)
+ goto done;
+
+ /* Try old format (address only) */
+ key[17] = '\0';
+
+ tmp = textfile_get(filename, key);
+ if (tmp == NULL)
return -ENXIO;

+done:
err = snprintf(alias, size, "%s", tmp);

free(tmp);
--
1.7.7.6


2012-05-25 15:47:07

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 15/18] core: Fix deleting entries

The "aliases" and "longtermkeys" files now use the new storage format,
so the correct keys must be passed to delete_entry() function.
---
src/device.c | 21 ++++++++++++++-------
1 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/src/device.c b/src/device.c
index 2357684..89a78cf 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1155,23 +1155,30 @@ uint16_t btd_device_get_version(struct btd_device *device)
static void device_remove_stored(struct btd_device *device)
{
bdaddr_t src;
- char addr[18];
+ char key[20];
DBusConnection *conn = get_dbus_connection();

adapter_get_address(device->adapter, &src);
- ba2str(&device->bdaddr, addr);
+ ba2str(&device->bdaddr, key);
+
+ /* key: address only */
+ delete_entry(&src, "profiles", key);
+ delete_entry(&src, "trusts", key);

if (device_is_bonded(device)) {
- delete_entry(&src, "linkkeys", addr);
- delete_entry(&src, "aliases", addr);
- delete_entry(&src, "longtermkeys", addr);
+ delete_entry(&src, "linkkeys", key);
+
+ /* key: appending address type */
+ sprintf(&key[17], "#%hhu", device->bdaddr_type);
+
+ delete_entry(&src, "aliases", key);
+ delete_entry(&src, "longtermkeys", key);
device_set_bonded(device, FALSE);
device->paired = FALSE;
btd_adapter_remove_bonding(device->adapter, &device->bdaddr,
device->bdaddr_type);
}
- delete_entry(&src, "profiles", addr);
- delete_entry(&src, "trusts", addr);
+
delete_all_records(&src, &device->bdaddr);
delete_device_service(&src, &device->bdaddr, device->bdaddr_type);

--
1.7.7.6


2012-05-25 15:47:08

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 16/18] storage: Keep backward compatibility in "blocked" file

The "blocked" file is shared by both BR/EDR and LE devices, so we must
keep backward compatibility with the old storage format.
---
src/storage.c | 10 +++++++++-
1 files changed, 9 insertions(+), 1 deletions(-)

diff --git a/src/storage.c b/src/storage.c
index 33dcace..cb7cde2 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1155,9 +1155,17 @@ gboolean read_blocked(const bdaddr_t *local, const bdaddr_t *remote,
sprintf(&key[17], "#%hhu", bdaddr_type);

str = textfile_caseget(filename, key);
- if (!str)
+ if (str != NULL)
+ goto done;
+
+ /* Try old format (address only) */
+ key[17] = '\0';
+
+ str = textfile_caseget(filename, key);
+ if (str == NULL)
return FALSE;

+done:
free(str);

return TRUE;
--
1.7.7.6


2012-05-25 15:47:06

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 14/18] storage: Rename primary file to primaries

From: Claudio Takahasi <[email protected]>

This patch renames the "primary" file to "primaries" to be more suitable
with its content.
---
src/adapter.c | 6 +++---
src/storage.c | 6 +++---
2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index a7e9e77..1e0ed36 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -1940,7 +1940,7 @@ static GSList *string_to_primary_list(char *str)
return l;
}

-static void create_stored_device_from_primary(char *key, char *value,
+static void create_stored_device_from_primaries(char *key, char *value,
void *user_data)
{
struct btd_adapter *adapter = user_data;
@@ -1999,8 +1999,8 @@ static void load_devices(struct btd_adapter *adapter)
textfile_foreach(filename, create_stored_device_from_profiles,
adapter);

- create_name(filename, PATH_MAX, STORAGEDIR, srcaddr, "primary");
- textfile_foreach(filename, create_stored_device_from_primary,
+ create_name(filename, PATH_MAX, STORAGEDIR, srcaddr, "primaries");
+ textfile_foreach(filename, create_stored_device_from_primaries,
adapter);

create_name(filename, PATH_MAX, STORAGEDIR, srcaddr, "linkkeys");
diff --git a/src/storage.c b/src/storage.c
index 8693552..33dcace 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1186,7 +1186,7 @@ int write_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
{
char filename[PATH_MAX + 1], key[20];

- create_filename(filename, PATH_MAX, sba, "primary");
+ create_filename(filename, PATH_MAX, sba, "primaries");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

@@ -1248,7 +1248,7 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
create_filename(filename, PATH_MAX, sba, "ccc");
delete_by_pattern(filename, key);

- create_filename(filename, PATH_MAX, sba, "primary");
+ create_filename(filename, PATH_MAX, sba, "primaries");

return textfile_del(filename, key);
}
@@ -1258,7 +1258,7 @@ char *read_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
{
char filename[PATH_MAX + 1], key[20];

- create_filename(filename, PATH_MAX, sba, "primary");
+ create_filename(filename, PATH_MAX, sba, "primaries");

ba2str(dba, key);
sprintf(&key[17], "#%hhu", bdaddr_type);
--
1.7.7.6


2012-05-25 15:47:05

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 13/18] storage: Rename characteristic to characteristics

From: Claudio Takahasi <[email protected]>

This patch renames the "characteristic" file to "characteristics" to be
more suitable with its content.
---
src/storage.c | 6 +++---
1 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/storage.c b/src/storage.c
index 2d15a23..8693552 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1237,7 +1237,7 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
sprintf(&key[17], "#%hhu", bdaddr_type);

/* Deleting all characteristics of a given key */
- create_filename(filename, PATH_MAX, sba, "characteristic");
+ create_filename(filename, PATH_MAX, sba, "characteristics");
delete_by_pattern(filename, key);

/* Deleting all attributes values of a given key */
@@ -1272,7 +1272,7 @@ int write_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
{
char filename[PATH_MAX + 1], addr[18], key[25];

- create_filename(filename, PATH_MAX, sba, "characteristic");
+ create_filename(filename, PATH_MAX, sba, "characteristics");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

@@ -1287,7 +1287,7 @@ char *read_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
{
char filename[PATH_MAX + 1], addr[18], key[25];

- create_filename(filename, PATH_MAX, sba, "characteristic");
+ create_filename(filename, PATH_MAX, sba, "characteristics");

ba2str(dba, addr);
snprintf(key, sizeof(key), "%17s#%hhu#%04X", addr, bdaddr_type, handle);
--
1.7.7.6


2012-05-25 15:47:04

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 12/18] core: Fix creating device from "blocked" file

---
src/adapter.c | 9 +++++++--
1 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index c1caffd..a7e9e77 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -1888,12 +1888,17 @@ static void create_stored_device_from_blocked(char *key, char *value,
{
struct btd_adapter *adapter = user_data;
struct btd_device *device;
+ char address[18];
+ uint8_t bdaddr_type;
+
+ if (sscanf(key, "%17s#%hhu", address, &bdaddr_type) < 2)
+ return;

if (g_slist_find_custom(adapter->devices,
- key, (GCompareFunc) device_address_cmp))
+ address, (GCompareFunc) device_address_cmp))
return;

- device = device_create(connection, adapter, key, BDADDR_BREDR);
+ device = device_create(connection, adapter, address, bdaddr_type);
if (device) {
device_set_temporary(device, FALSE);
adapter->devices = g_slist_append(adapter->devices, device);
--
1.7.7.6


2012-05-25 15:47:03

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 11/18] storage: Store address type in "attributes" file

---
attrib/client.c | 6 +++++-
src/storage.c | 12 ++++--------
src/storage.h | 3 ++-
3 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/attrib/client.c b/attrib/client.c
index 8aa2800..7aefd96 100644
--- a/attrib/client.c
+++ b/attrib/client.c
@@ -635,7 +635,9 @@ static GSList *load_characteristics(struct gatt_service *gatt, uint16_t start)
static void store_attribute(struct gatt_service *gatt, uint16_t handle,
uint16_t type, uint8_t *value, gsize len)
{
+ struct btd_device *device = gatt->dev;
bdaddr_t sba, dba;
+ uint8_t bdaddr_type;
bt_uuid_t uuid;
char *str, *tmp;
guint i;
@@ -652,7 +654,9 @@ static void store_attribute(struct gatt_service *gatt, uint16_t handle,

gatt_get_address(gatt, &sba, &dba, NULL);

- write_device_attribute(&sba, &dba, handle, str);
+ bdaddr_type = device_get_addr_type(device);
+
+ write_device_attribute(&sba, &dba, bdaddr_type, handle, str);

g_free(str);
}
diff --git a/src/storage.c b/src/storage.c
index caee8f0..2d15a23 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1240,14 +1240,10 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
create_filename(filename, PATH_MAX, sba, "characteristic");
delete_by_pattern(filename, key);

- key[17] = '\0';
-
/* Deleting all attributes values of a given key */
create_filename(filename, PATH_MAX, sba, "attributes");
delete_by_pattern(filename, key);

- sprintf(&key[17], "#%hhu", bdaddr_type);
-
/* Deleting all CCC values of a given key */
create_filename(filename, PATH_MAX, sba, "ccc");
delete_by_pattern(filename, key);
@@ -1300,17 +1296,17 @@ char *read_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
}

int write_device_attribute(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t handle, const char *chars)
+ uint8_t bdaddr_type, uint16_t handle,
+ const char *chars)
{
- char filename[PATH_MAX + 1], addr[18], key[23];
+ char filename[PATH_MAX + 1], addr[18], key[25];

create_filename(filename, PATH_MAX, sba, "attributes");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

ba2str(dba, addr);
-
- snprintf(key, sizeof(key), "%17s#%04X", addr, handle);
+ snprintf(key, sizeof(key), "%17s#%hhu#%04X", addr, bdaddr_type, handle);

return textfile_put(filename, key, chars);
}
diff --git a/src/storage.h b/src/storage.h
index 67364df..3705d3d 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -94,7 +94,8 @@ int write_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
char *read_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
uint8_t bdaddr_type, uint16_t handle);
int write_device_attribute(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t handle, const char *chars);
+ uint8_t bdaddr_type, uint16_t handle,
+ const char *chars);
int read_device_attributes(const bdaddr_t *sba, textfile_cb func, void *data);
int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
uint16_t handle, uint16_t *value);
--
1.7.7.6


2012-05-25 15:47:02

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 10/18] core: Fix creating device from "longtermkeys" file

---
src/adapter.c | 26 ++++++++++++++++----------
1 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 2776204..c1caffd 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -1787,7 +1787,8 @@ static struct link_key_info *get_key_info(const char *addr, const char *value)
return info;
}

-static struct smp_ltk_info *get_ltk_info(const char *addr, const char *value)
+static struct smp_ltk_info *get_ltk_info(const char *addr, uint8_t bdaddr_type,
+ const char *value)
{
struct smp_ltk_info *ltk;
char *ptr;
@@ -1802,14 +1803,15 @@ static struct smp_ltk_info *get_ltk_info(const char *addr, const char *value)

str2ba(addr, &ltk->bdaddr);

+ ltk->bdaddr_type = bdaddr_type;
+
str2buf(value, ltk->val, sizeof(ltk->val));

ptr = (char *) value + 2 * sizeof(ltk->val) + 1;

- ret = sscanf(ptr, " %hhd %hhd %hhd %hhd %hd %n",
- (uint8_t *) &ltk->bdaddr_type,
- &ltk->authenticated, &ltk->master,
- &ltk->enc_size, &ltk->ediv, &i);
+ ret = sscanf(ptr, " %hhd %hhd %hhd %hd %n",
+ &ltk->authenticated, &ltk->master, &ltk->enc_size,
+ &ltk->ediv, &i);
if (ret < 2) {
g_free(ltk);
return NULL;
@@ -1851,26 +1853,30 @@ static void create_stored_device_from_ltks(char *key, char *value,
struct btd_adapter *adapter = keys->adapter;
struct btd_device *device;
struct smp_ltk_info *info;
- char srcaddr[18];
+ char address[18], srcaddr[18];
+ uint8_t bdaddr_type;
bdaddr_t src;

- info = get_ltk_info(key, value);
+ if (sscanf(key, "%17s#%hhu", address, &bdaddr_type) < 2)
+ return;
+
+ info = get_ltk_info(address, bdaddr_type, value);
if (info == NULL)
return;

keys->keys = g_slist_append(keys->keys, info);

- if (g_slist_find_custom(adapter->devices, key,
+ if (g_slist_find_custom(adapter->devices, address,
(GCompareFunc) device_address_cmp))
return;

adapter_get_address(adapter, &src);
ba2str(&src, srcaddr);

- if (g_strcmp0(srcaddr, key) == 0)
+ if (g_strcmp0(srcaddr, address) == 0)
return;

- device = device_create(connection, adapter, key, info->bdaddr_type);
+ device = device_create(connection, adapter, address, bdaddr_type);
if (device) {
device_set_temporary(device, FALSE);
adapter->devices = g_slist_append(adapter->devices, device);
--
1.7.7.6


2012-05-25 15:47:01

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 09/18] storage: Store address type in "longtermkeys" file

---
src/device.c | 3 ++-
src/event.c | 2 +-
src/storage.c | 16 ++++++++++------
src/storage.h | 5 +++--
4 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/src/device.c b/src/device.c
index e44ce58..2357684 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1090,7 +1090,8 @@ struct btd_device *device_create(DBusConnection *conn,
device_set_bonded(device, TRUE);
}

- if (device_is_le(device) && has_longtermkeys(&src, &device->bdaddr)) {
+ if (device_is_le(device) && has_longtermkeys(&src, &device->bdaddr,
+ device->bdaddr_type)) {
device_set_paired(device, TRUE);
device_set_bonded(device, TRUE);
}
diff --git a/src/event.c b/src/event.c
index a1fc016..acfcef3 100644
--- a/src/event.c
+++ b/src/event.c
@@ -397,7 +397,7 @@ static int store_longtermkey(bdaddr_t *local, bdaddr_t *peer,
newkey = g_string_append(newkey, str);
g_free(str);

- err = write_longtermkeys(local, peer, newkey->str);
+ err = write_longtermkeys(local, peer, bdaddr_type, newkey->str);

g_string_free(newkey, TRUE);

diff --git a/src/storage.c b/src/storage.c
index 1eb916f..caee8f0 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1379,9 +1379,10 @@ void delete_device_ccc(bdaddr_t *local, bdaddr_t *peer)
delete_by_pattern(filename, addr);
}

-int write_longtermkeys(bdaddr_t *local, bdaddr_t *peer, const char *key)
+int write_longtermkeys(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ const char *key)
{
- char filename[PATH_MAX + 1], addr[18];
+ char filename[PATH_MAX + 1], addr[20];

if (!key)
return -EINVAL;
@@ -1391,18 +1392,21 @@ int write_longtermkeys(bdaddr_t *local, bdaddr_t *peer, const char *key)
create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

ba2str(peer, addr);
+ sprintf(&addr[17], "#%hhu", bdaddr_type);
+
return textfile_put(filename, addr, key);
}

-gboolean has_longtermkeys(bdaddr_t *local, bdaddr_t *peer)
+gboolean has_longtermkeys(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type)
{
- char filename[PATH_MAX + 1], addr[18], *str;
+ char filename[PATH_MAX + 1], key[20], *str;

create_filename(filename, PATH_MAX, local, "longtermkeys");

- ba2str(peer, addr);
+ ba2str(peer, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);

- str = textfile_caseget(filename, addr);
+ str = textfile_caseget(filename, key);
if (str) {
free(str);
return TRUE;
diff --git a/src/storage.h b/src/storage.h
index 9032801..67364df 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -101,7 +101,8 @@ int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
int write_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
uint16_t handle, uint16_t value);
void delete_device_ccc(bdaddr_t *local, bdaddr_t *peer);
-int write_longtermkeys(bdaddr_t *local, bdaddr_t *peer, const char *key);
-gboolean has_longtermkeys(bdaddr_t *local, bdaddr_t *peer);
+int write_longtermkeys(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ const char *key);
+gboolean has_longtermkeys(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type);

#define PNP_UUID "00001200-0000-1000-8000-00805f9b34fb"
--
1.7.7.6


2012-05-25 15:46:57

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 05/18] storage: Store address type in "aliases" file

From: Claudio Takahasi <[email protected]>

---
src/device.c | 7 ++++---
src/storage.c | 16 ++++++++++++----
src/storage.h | 6 ++++--
3 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/src/device.c b/src/device.c
index e239da6..a91eba7 100644
--- a/src/device.c
+++ b/src/device.c
@@ -458,8 +458,8 @@ static DBusMessage *set_alias(DBusConnection *conn, DBusMessage *msg,
ba2str(&device->bdaddr, dstaddr);

/* Remove alias if empty string */
- err = write_device_alias(srcaddr, dstaddr,
- g_str_equal(alias, "") ? NULL : alias);
+ err = write_device_alias(srcaddr, dstaddr, device->bdaddr_type,
+ g_str_equal(alias, "") ? NULL : alias);
if (err < 0)
return btd_error_failed(msg, strerror(-err));

@@ -1077,7 +1077,8 @@ struct btd_device *device_create(DBusConnection *conn,
ba2str(&src, srcaddr);

read_device_name(srcaddr, address, bdaddr_type, device->name);
- if (read_device_alias(srcaddr, address, alias, sizeof(alias)) == 0)
+ if (read_device_alias(srcaddr, address, bdaddr_type, alias,
+ sizeof(alias)) == 0)
device->alias = g_strdup(alias);
device->trusted = read_trust(&src, address, GLOBAL_TRUST);

diff --git a/src/storage.c b/src/storage.c
index 6fe9279..f2eaae1 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -61,14 +61,18 @@ static inline int create_filename(char *buf, size_t size,
return create_name(buf, size, STORAGEDIR, addr, name);
}

-int read_device_alias(const char *src, const char *dst, char *alias, size_t size)
+int read_device_alias(const char *src, const char *dst, uint8_t bdaddr_type,
+ char *alias, size_t size)
{
char filename[PATH_MAX + 1], *tmp;
+ char key[20];
int err;

create_name(filename, PATH_MAX, STORAGEDIR, src, "aliases");

- tmp = textfile_get(filename, dst);
+ snprintf(key, sizeof(key), "%17s#%hhu", dst, bdaddr_type);
+
+ tmp = textfile_get(filename, key);
if (!tmp)
return -ENXIO;

@@ -79,15 +83,19 @@ int read_device_alias(const char *src, const char *dst, char *alias, size_t size
return err < 0 ? -EIO : 0;
}

-int write_device_alias(const char *src, const char *dst, const char *alias)
+int write_device_alias(const char *src, const char *dst, uint8_t bdaddr_type,
+ const char *alias)
{
char filename[PATH_MAX + 1];
+ char key[20];

create_name(filename, PATH_MAX, STORAGEDIR, src, "aliases");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

- return textfile_put(filename, dst, alias);
+ snprintf(key, sizeof(key), "%17s#%hhu", dst, bdaddr_type);
+
+ return textfile_put(filename, key, alias);
}

int write_discoverable_timeout(bdaddr_t *bdaddr, int timeout)
diff --git a/src/storage.h b/src/storage.h
index 7443d6f..eda7f9b 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -23,8 +23,10 @@

#include "textfile.h"

-int read_device_alias(const char *src, const char *dst, char *alias, size_t size);
-int write_device_alias(const char *src, const char *dst, const char *alias);
+int read_device_alias(const char *src, const char *dst, uint8_t bdaddr_type,
+ char *alias, size_t size);
+int write_device_alias(const char *src, const char *dst, uint8_t bdaddr_type,
+ const char *alias);
int write_discoverable_timeout(bdaddr_t *bdaddr, int timeout);
int read_discoverable_timeout(const char *src, int *timeout);
int write_pairable_timeout(bdaddr_t *bdaddr, int timeout);
--
1.7.7.6


2012-05-25 15:46:58

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 06/18] core: Fix reading stored data from "aliases" and "names" file

---
src/adapter.c | 16 ++++++++++------
1 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index a364935..2776204 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2833,16 +2833,19 @@ static gboolean pairing_is_legacy(bdaddr_t *local, bdaddr_t *peer,
return TRUE;
}

-static char *read_stored_data(bdaddr_t *local, bdaddr_t *peer, const char *file)
+static char *read_stored_data(bdaddr_t *local, bdaddr_t *peer,
+ uint8_t bdaddr_type, const char *file)
{
- char local_addr[18], peer_addr[18], filename[PATH_MAX + 1];
+ char local_addr[18], key[20], filename[PATH_MAX + 1];

ba2str(local, local_addr);
- ba2str(peer, peer_addr);

create_name(filename, PATH_MAX, STORAGEDIR, local_addr, file);

- return textfile_get(filename, peer_addr);
+ ba2str(peer, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);
+
+ return textfile_get(filename, key);
}

void adapter_update_found_devices(struct btd_adapter *adapter,
@@ -2900,7 +2903,7 @@ void adapter_update_found_devices(struct btd_adapter *adapter,

/* New device in the discovery session */

- name = read_stored_data(&adapter->bdaddr, bdaddr, "names");
+ name = read_stored_data(&adapter->bdaddr, bdaddr, bdaddr_type, "names");

if (bdaddr_type == BDADDR_BREDR) {
legacy = pairing_is_legacy(&adapter->bdaddr, bdaddr, data,
@@ -2920,7 +2923,8 @@ void adapter_update_found_devices(struct btd_adapter *adapter,
adapter_ops->confirm_name(adapter->dev_id, bdaddr, bdaddr_type,
name_known);

- alias = read_stored_data(&adapter->bdaddr, bdaddr, "aliases");
+ alias = read_stored_data(&adapter->bdaddr, bdaddr, bdaddr_type,
+ "aliases");

dev = found_device_new(bdaddr, bdaddr_type, name, alias, dev_class,
legacy, eir_data.flags);
--
1.7.7.6


2012-05-25 15:46:54

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 02/18] storage: Store address type in "appearance" file

BLE addressing types can be either public or random so the entries in
the "appearance" file did not contain enough information to distinguish
which addressing type it's supposed to be (LE public or LE random).

Appearance is an information related to BLE device only. Entries will
now contain both BLE address number and BLE address type as a single key
in every entry in the file.
---
src/adapter.c | 4 ++--
src/device.c | 6 ++++--
src/storage.c | 19 +++++++++++--------
src/storage.h | 6 ++++--
4 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index c6904a1..1542e06 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2731,7 +2731,7 @@ void adapter_emit_device_found(struct btd_adapter *adapter,
dev->legacy = FALSE;

if (read_remote_appearance(&adapter->bdaddr, &dev->bdaddr,
- &app) == 0)
+ dev->bdaddr_type, &app) == 0)
icon = gap_appearance_to_icon(app);
else
icon = NULL;
@@ -2870,7 +2870,7 @@ void adapter_update_found_devices(struct btd_adapter *adapter,
write_remote_class(&adapter->bdaddr, bdaddr, dev_class);

if (eir_data.appearance != 0)
- write_remote_appearance(&adapter->bdaddr, bdaddr,
+ write_remote_appearance(&adapter->bdaddr, bdaddr, bdaddr_type,
eir_data.appearance);

if (eir_data.name != NULL && eir_data.name_complete)
diff --git a/src/device.c b/src/device.c
index 7cdd025..eb015ec 100644
--- a/src/device.c
+++ b/src/device.c
@@ -373,7 +373,8 @@ static DBusMessage *get_properties(DBusConnection *conn,
icon = class_to_icon(class);

dict_append_entry(&dict, "Class", DBUS_TYPE_UINT32, &class);
- } else if (read_remote_appearance(&src, &device->bdaddr, &app) == 0)
+ } else if (read_remote_appearance(&src, &device->bdaddr,
+ device->bdaddr_type, &app) == 0)
/* Appearance */
icon = gap_appearance_to_icon(app);

@@ -1851,7 +1852,8 @@ static void appearance_cb(guint8 status, const guint8 *pdu, guint16 plen,
app = att_get_u16(atval);

adapter_get_address(adapter, &src);
- write_remote_appearance(&src, &device->bdaddr, app);
+ write_remote_appearance(&src, &device->bdaddr, device->bdaddr_type,
+ app);

done:
att_data_list_free(list);
diff --git a/src/storage.c b/src/storage.c
index b783be0..ab9b1ed 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -281,16 +281,17 @@ int read_local_class(bdaddr_t *bdaddr, uint8_t *class)
return 0;
}

-int read_remote_appearance(bdaddr_t *local, bdaddr_t *peer,
+int read_remote_appearance(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
uint16_t *appearance)
{
- char filename[PATH_MAX + 1], addr[18], *str;
+ char filename[PATH_MAX + 1], key[20], *str;

create_filename(filename, PATH_MAX, local, "appearance");

- ba2str(peer, addr);
+ ba2str(peer, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);

- str = textfile_get(filename, addr);
+ str = textfile_get(filename, key);
if (!str)
return -ENOENT;

@@ -305,18 +306,20 @@ int read_remote_appearance(bdaddr_t *local, bdaddr_t *peer,
}

int write_remote_appearance(bdaddr_t *local, bdaddr_t *peer,
- uint16_t appearance)
+ uint8_t bdaddr_type, uint16_t appearance)
{
- char filename[PATH_MAX + 1], addr[18], str[7];
+ char filename[PATH_MAX + 1], key[20], str[7];

create_filename(filename, PATH_MAX, local, "appearance");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

- ba2str(peer, addr);
+ ba2str(peer, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);
+
sprintf(str, "0x%4.4x", appearance);

- return textfile_put(filename, addr, str);
+ return textfile_put(filename, key, str);
}

int write_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t class)
diff --git a/src/storage.h b/src/storage.h
index c3be64a..db33b43 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -36,8 +36,10 @@ int write_local_name(bdaddr_t *bdaddr, const char *name);
int read_local_name(bdaddr_t *bdaddr, char *name);
int write_local_class(bdaddr_t *bdaddr, uint8_t *class);
int read_local_class(bdaddr_t *bdaddr, uint8_t *class);
-int write_remote_appearance(bdaddr_t *local, bdaddr_t *peer, uint16_t appearance);
-int read_remote_appearance(bdaddr_t *local, bdaddr_t *peer, uint16_t *appearance);
+int write_remote_appearance(bdaddr_t *local, bdaddr_t *peer,
+ uint8_t bdaddr_type, uint16_t appearance);
+int read_remote_appearance(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ uint16_t *appearance);
int write_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t class);
int read_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t *class);
int write_device_name(bdaddr_t *local, bdaddr_t *peer, char *name);
--
1.7.7.6


2012-05-25 15:46:53

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 01/18] storage: Store address type in "characteristics"

GATT can use BR/EDR or BLE as transport. Addressing types can be either
BR/EDR, BLE public or BLE random so the entries in the "characteristics"
file did not contain enough information to distinguish which addressing
type it's supposed to be. Entries will now contain both address number
and address type as a single key in every entry in the file.
---
attrib/client.c | 25 +++++++++++++++----------
src/storage.c | 19 +++++++++++--------
src/storage.h | 5 +++--
3 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/attrib/client.c b/attrib/client.c
index 8fbc1cc..8aa2800 100644
--- a/attrib/client.c
+++ b/attrib/client.c
@@ -155,15 +155,15 @@ static void remove_attio(struct gatt_service *gatt)
}
}

-static void gatt_get_address(struct gatt_service *gatt,
- bdaddr_t *sba, bdaddr_t *dba)
+static void gatt_get_address(struct gatt_service *gatt, bdaddr_t *sba,
+ bdaddr_t *dba, uint8_t *bdaddr_type)
{
struct btd_device *device = gatt->dev;
struct btd_adapter *adapter;

adapter = device_get_adapter(device);
adapter_get_address(adapter, sba);
- device_get_address(device, dba, NULL);
+ device_get_address(device, dba, bdaddr_type);
}

static int characteristic_handle_cmp(gconstpointer a, gconstpointer b)
@@ -548,13 +548,15 @@ static char *characteristic_list_to_string(GSList *chars)
}

static void store_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t start, GSList *chars)
+ uint8_t bdaddr_type, uint16_t start,
+ GSList *chars)
{
char *characteristics;

characteristics = characteristic_list_to_string(chars);

- write_device_characteristics(sba, dba, start, characteristics);
+ write_device_characteristics(sba, dba, bdaddr_type, start,
+ characteristics);

g_free(characteristics);
}
@@ -614,11 +616,12 @@ static GSList *load_characteristics(struct gatt_service *gatt, uint16_t start)
{
GSList *chrs_list;
bdaddr_t sba, dba;
+ uint8_t bdaddr_type;
char *str;

- gatt_get_address(gatt, &sba, &dba);
+ gatt_get_address(gatt, &sba, &dba, &bdaddr_type);

- str = read_device_characteristics(&sba, &dba, start);
+ str = read_device_characteristics(&sba, &dba, bdaddr_type, start);
if (str == NULL)
return NULL;

@@ -647,7 +650,7 @@ static void store_attribute(struct gatt_service *gatt, uint16_t handle,
for (i = 0, tmp = str + MAX_LEN_UUID_STR; i < len; i++, tmp += 2)
sprintf(tmp, "%02X", value[i]);

- gatt_get_address(gatt, &sba, &dba);
+ gatt_get_address(gatt, &sba, &dba, NULL);

write_device_attribute(&sba, &dba, handle, str);

@@ -886,6 +889,7 @@ static void char_discovered_cb(GSList *characteristics, guint8 status,
uint16_t *previous_end = NULL;
GSList *l;
bdaddr_t sba, dba;
+ uint8_t bdaddr_type;

if (status != 0) {
const char *str = att_ecode2str(status);
@@ -924,8 +928,9 @@ static void char_discovered_cb(GSList *characteristics, guint8 status,
if (previous_end)
*previous_end = prim->range.end;

- gatt_get_address(gatt, &sba, &dba);
- store_characteristics(&sba, &dba, prim->range.start, gatt->chars);
+ gatt_get_address(gatt, &sba, &dba, &bdaddr_type);
+ store_characteristics(&sba, &dba, bdaddr_type, prim->range.start,
+ gatt->chars);

g_slist_foreach(gatt->chars, update_all_chars, gatt);

diff --git a/src/storage.c b/src/storage.c
index a91ee2d..b783be0 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1212,12 +1212,16 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
char filename[PATH_MAX + 1], key[20];

memset(key, 0, sizeof(key));
+
ba2str(dba, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);

/* Deleting all characteristics of a given key */
create_filename(filename, PATH_MAX, sba, "characteristic");
delete_by_pattern(filename, key);

+ key[17] = '\0';
+
/* Deleting all attributes values of a given key */
create_filename(filename, PATH_MAX, sba, "attributes");
delete_by_pattern(filename, key);
@@ -1247,31 +1251,30 @@ char *read_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
}

int write_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t handle, const char *chars)
+ uint8_t bdaddr_type, uint16_t handle,
+ const char *chars)
{
- char filename[PATH_MAX + 1], addr[18], key[23];
+ char filename[PATH_MAX + 1], addr[18], key[25];

create_filename(filename, PATH_MAX, sba, "characteristic");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

ba2str(dba, addr);
-
- snprintf(key, sizeof(key), "%17s#%04X", addr, handle);
+ snprintf(key, sizeof(key), "%17s#%hhu#%04X", addr, bdaddr_type, handle);

return textfile_put(filename, key, chars);
}

char *read_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t handle)
+ uint8_t bdaddr_type, uint16_t handle)
{
- char filename[PATH_MAX + 1], addr[18], key[23];
+ char filename[PATH_MAX + 1], addr[18], key[25];

create_filename(filename, PATH_MAX, sba, "characteristic");

ba2str(dba, addr);
-
- snprintf(key, sizeof(key), "%17s#%04X", addr, handle);
+ snprintf(key, sizeof(key), "%17s#%hhu#%04X", addr, bdaddr_type, handle);

return textfile_caseget(filename, key);
}
diff --git a/src/storage.h b/src/storage.h
index d00976f..c3be64a 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -82,9 +82,10 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
char *read_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
uint8_t bdaddr_type);
int write_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t handle, const char *chars);
+ uint8_t bdaddr_type, uint16_t handle,
+ const char *chars);
char *read_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t handle);
+ uint8_t bdaddr_type, uint16_t handle);
int write_device_attribute(const bdaddr_t *sba, const bdaddr_t *dba,
uint16_t handle, const char *chars);
int read_device_attributes(const bdaddr_t *sba, textfile_cb func, void *data);
--
1.7.7.6


2012-05-25 15:46:52

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v3 00/18] Store address type on storage - remaining files

This patchset contains the remaining changes for the new storage format
used (bdaddr#type) on storage files (LE-only or shared files).

The files that have been changed so far are:
primaries, characteristics, appearance, ccc, names, aliases,
longtermkeys, attributes, blocked.

Unchanged files:
lastseen, lastused, eir, classes, manufacturers, features, linkkeys,
pincodes, trusts, profiles.


Note: The v3 of this series adds two other patchs (17/18 and 18/18) which
keep backward compatibility with the old storage format in both
names and aliases files since they're shared by both BR/EDR and LE
devices.


Claudio Takahasi (4):
storage: Store address type in "aliases" file
storage: Store address type in blocked file
storage: Rename characteristic to characteristics
storage: Rename primary file to primaries

Paulo Alcantara (14):
storage: Store address type in "characteristics"
storage: Store address type in "appearance" file
storage: Store address type in "ccc" file
storage: Store address type in "names" file
core: Fix reading stored data from "aliases" and "names" file
storage: Do not store address type twice
storage: Store address type in "longtermkeys" file
core: Fix creating device from "longtermkeys" file
storage: Store address type in "attributes" file
core: Fix creating device from "blocked" file
core: Fix deleting entries
storage: Keep backward compatibility in "blocked" file
storage: Keep backward compatibility in "names" file
storage: Keep backward compatibility in "aliases" file

attrib/client.c | 31 ++++++---
input/device.c | 6 +-
src/adapter.c | 64 ++++++++++++-------
src/attrib-server.c | 19 ++++--
src/device.c | 51 ++++++++++-----
src/device.h | 1 +
src/event.c | 13 +++-
src/storage.c | 177 ++++++++++++++++++++++++++++++++-------------------
src/storage.h | 44 ++++++++-----
9 files changed, 261 insertions(+), 145 deletions(-)

--
1.7.7.6


2012-05-25 07:39:22

by Johan Hedberg

[permalink] [raw]
Subject: Re: [PATCH BlueZ 00/15] Store address type on storage - remaining files

Hi Paulo,

On Thu, May 24, 2012, Paulo Alcantara wrote:
> > On Wed, May 23, 2012, Paulo Alcantara wrote:
> > > This patchset contains the remaining changes for the new storage format
> > > used (bdaddr#type) on storage files (LE-only or shared files).
> > >
> > > The files that have been changed so far are:
> > > primaries, characteristics, appearance, ccc, names, aliases,
> > > longtermkeys, attributes, blocked.
> >
> > Any files that are used for BR/EDR would have to keep their backwards
> > compatibility. Otherwise someone who upgrades from 4.99 to 4.100 will
> > suddenly find that the device they had blocked is not blocked anymore
> > and none of their devices have any names. We can't break something like
> > that between 4.x versions. Are these patches guaranteeing this backwards
> > compatibility? Looking at the patches it doesn't look like they give
> > this guarantee.
>
> Ok, you're right. The only file that we should keep backward compatibility on
> this series is the blocked file, since it's shared by both BR/EDR and LE devices.

The aliases and names files are also shared with BR/EDR. It seems your
patches do not take this into account.

Johan

2012-05-24 18:46:13

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v2 16/16] storage: Keep backward compatibility

The "blocked" file is shared by both BR/EDR and LE devices, so we must
keep backward compatibility with the old storage format.
---
src/storage.c | 10 +++++++++-
1 files changed, 9 insertions(+), 1 deletions(-)

diff --git a/src/storage.c b/src/storage.c
index 33dcace..cb7cde2 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1155,9 +1155,17 @@ gboolean read_blocked(const bdaddr_t *local, const bdaddr_t *remote,
sprintf(&key[17], "#%hhu", bdaddr_type);

str = textfile_caseget(filename, key);
- if (!str)
+ if (str != NULL)
+ goto done;
+
+ /* Try old format (address only) */
+ key[17] = '\0';
+
+ str = textfile_caseget(filename, key);
+ if (str == NULL)
return FALSE;

+done:
free(str);

return TRUE;
--
1.7.7.6


2012-05-24 18:46:12

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v2 15/16] core: Fix deleting entries

The "aliases" and "longtermkeys" files now use the new storage format,
so the correct keys must be passed to delete_entry() function.
---
src/device.c | 21 ++++++++++++++-------
1 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/src/device.c b/src/device.c
index 2357684..89a78cf 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1155,23 +1155,30 @@ uint16_t btd_device_get_version(struct btd_device *device)
static void device_remove_stored(struct btd_device *device)
{
bdaddr_t src;
- char addr[18];
+ char key[20];
DBusConnection *conn = get_dbus_connection();

adapter_get_address(device->adapter, &src);
- ba2str(&device->bdaddr, addr);
+ ba2str(&device->bdaddr, key);
+
+ /* key: address only */
+ delete_entry(&src, "profiles", key);
+ delete_entry(&src, "trusts", key);

if (device_is_bonded(device)) {
- delete_entry(&src, "linkkeys", addr);
- delete_entry(&src, "aliases", addr);
- delete_entry(&src, "longtermkeys", addr);
+ delete_entry(&src, "linkkeys", key);
+
+ /* key: appending address type */
+ sprintf(&key[17], "#%hhu", device->bdaddr_type);
+
+ delete_entry(&src, "aliases", key);
+ delete_entry(&src, "longtermkeys", key);
device_set_bonded(device, FALSE);
device->paired = FALSE;
btd_adapter_remove_bonding(device->adapter, &device->bdaddr,
device->bdaddr_type);
}
- delete_entry(&src, "profiles", addr);
- delete_entry(&src, "trusts", addr);
+
delete_all_records(&src, &device->bdaddr);
delete_device_service(&src, &device->bdaddr, device->bdaddr_type);

--
1.7.7.6


2012-05-24 18:46:11

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v2 14/16] storage: Rename primary file to primaries

From: Claudio Takahasi <[email protected]>

This patch renames the "primary" file to "primaries" to be more suitable
with its content.
---
src/adapter.c | 6 +++---
src/storage.c | 6 +++---
2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index a7e9e77..1e0ed36 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -1940,7 +1940,7 @@ static GSList *string_to_primary_list(char *str)
return l;
}

-static void create_stored_device_from_primary(char *key, char *value,
+static void create_stored_device_from_primaries(char *key, char *value,
void *user_data)
{
struct btd_adapter *adapter = user_data;
@@ -1999,8 +1999,8 @@ static void load_devices(struct btd_adapter *adapter)
textfile_foreach(filename, create_stored_device_from_profiles,
adapter);

- create_name(filename, PATH_MAX, STORAGEDIR, srcaddr, "primary");
- textfile_foreach(filename, create_stored_device_from_primary,
+ create_name(filename, PATH_MAX, STORAGEDIR, srcaddr, "primaries");
+ textfile_foreach(filename, create_stored_device_from_primaries,
adapter);

create_name(filename, PATH_MAX, STORAGEDIR, srcaddr, "linkkeys");
diff --git a/src/storage.c b/src/storage.c
index 8693552..33dcace 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1186,7 +1186,7 @@ int write_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
{
char filename[PATH_MAX + 1], key[20];

- create_filename(filename, PATH_MAX, sba, "primary");
+ create_filename(filename, PATH_MAX, sba, "primaries");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

@@ -1248,7 +1248,7 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
create_filename(filename, PATH_MAX, sba, "ccc");
delete_by_pattern(filename, key);

- create_filename(filename, PATH_MAX, sba, "primary");
+ create_filename(filename, PATH_MAX, sba, "primaries");

return textfile_del(filename, key);
}
@@ -1258,7 +1258,7 @@ char *read_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
{
char filename[PATH_MAX + 1], key[20];

- create_filename(filename, PATH_MAX, sba, "primary");
+ create_filename(filename, PATH_MAX, sba, "primaries");

ba2str(dba, key);
sprintf(&key[17], "#%hhu", bdaddr_type);
--
1.7.7.6


2012-05-24 18:46:10

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v2 13/16] storage: Rename characteristic to characteristics

From: Claudio Takahasi <[email protected]>

This patch renames the "characteristic" file to "characteristics" to be
more suitable with its content.
---
src/storage.c | 6 +++---
1 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/storage.c b/src/storage.c
index 2d15a23..8693552 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1237,7 +1237,7 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
sprintf(&key[17], "#%hhu", bdaddr_type);

/* Deleting all characteristics of a given key */
- create_filename(filename, PATH_MAX, sba, "characteristic");
+ create_filename(filename, PATH_MAX, sba, "characteristics");
delete_by_pattern(filename, key);

/* Deleting all attributes values of a given key */
@@ -1272,7 +1272,7 @@ int write_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
{
char filename[PATH_MAX + 1], addr[18], key[25];

- create_filename(filename, PATH_MAX, sba, "characteristic");
+ create_filename(filename, PATH_MAX, sba, "characteristics");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

@@ -1287,7 +1287,7 @@ char *read_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
{
char filename[PATH_MAX + 1], addr[18], key[25];

- create_filename(filename, PATH_MAX, sba, "characteristic");
+ create_filename(filename, PATH_MAX, sba, "characteristics");

ba2str(dba, addr);
snprintf(key, sizeof(key), "%17s#%hhu#%04X", addr, bdaddr_type, handle);
--
1.7.7.6


2012-05-24 18:46:09

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v2 12/16] core: Fix creating device from "blocked" file

---
src/adapter.c | 9 +++++++--
1 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index c1caffd..a7e9e77 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -1888,12 +1888,17 @@ static void create_stored_device_from_blocked(char *key, char *value,
{
struct btd_adapter *adapter = user_data;
struct btd_device *device;
+ char address[18];
+ uint8_t bdaddr_type;
+
+ if (sscanf(key, "%17s#%hhu", address, &bdaddr_type) < 2)
+ return;

if (g_slist_find_custom(adapter->devices,
- key, (GCompareFunc) device_address_cmp))
+ address, (GCompareFunc) device_address_cmp))
return;

- device = device_create(connection, adapter, key, BDADDR_BREDR);
+ device = device_create(connection, adapter, address, bdaddr_type);
if (device) {
device_set_temporary(device, FALSE);
adapter->devices = g_slist_append(adapter->devices, device);
--
1.7.7.6


2012-05-24 18:46:08

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v2 11/16] storage: Store address type in "attributes" file

---
attrib/client.c | 6 +++++-
src/storage.c | 12 ++++--------
src/storage.h | 3 ++-
3 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/attrib/client.c b/attrib/client.c
index 8aa2800..7aefd96 100644
--- a/attrib/client.c
+++ b/attrib/client.c
@@ -635,7 +635,9 @@ static GSList *load_characteristics(struct gatt_service *gatt, uint16_t start)
static void store_attribute(struct gatt_service *gatt, uint16_t handle,
uint16_t type, uint8_t *value, gsize len)
{
+ struct btd_device *device = gatt->dev;
bdaddr_t sba, dba;
+ uint8_t bdaddr_type;
bt_uuid_t uuid;
char *str, *tmp;
guint i;
@@ -652,7 +654,9 @@ static void store_attribute(struct gatt_service *gatt, uint16_t handle,

gatt_get_address(gatt, &sba, &dba, NULL);

- write_device_attribute(&sba, &dba, handle, str);
+ bdaddr_type = device_get_addr_type(device);
+
+ write_device_attribute(&sba, &dba, bdaddr_type, handle, str);

g_free(str);
}
diff --git a/src/storage.c b/src/storage.c
index caee8f0..2d15a23 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1240,14 +1240,10 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
create_filename(filename, PATH_MAX, sba, "characteristic");
delete_by_pattern(filename, key);

- key[17] = '\0';
-
/* Deleting all attributes values of a given key */
create_filename(filename, PATH_MAX, sba, "attributes");
delete_by_pattern(filename, key);

- sprintf(&key[17], "#%hhu", bdaddr_type);
-
/* Deleting all CCC values of a given key */
create_filename(filename, PATH_MAX, sba, "ccc");
delete_by_pattern(filename, key);
@@ -1300,17 +1296,17 @@ char *read_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
}

int write_device_attribute(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t handle, const char *chars)
+ uint8_t bdaddr_type, uint16_t handle,
+ const char *chars)
{
- char filename[PATH_MAX + 1], addr[18], key[23];
+ char filename[PATH_MAX + 1], addr[18], key[25];

create_filename(filename, PATH_MAX, sba, "attributes");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

ba2str(dba, addr);
-
- snprintf(key, sizeof(key), "%17s#%04X", addr, handle);
+ snprintf(key, sizeof(key), "%17s#%hhu#%04X", addr, bdaddr_type, handle);

return textfile_put(filename, key, chars);
}
diff --git a/src/storage.h b/src/storage.h
index 67364df..3705d3d 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -94,7 +94,8 @@ int write_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
char *read_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
uint8_t bdaddr_type, uint16_t handle);
int write_device_attribute(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t handle, const char *chars);
+ uint8_t bdaddr_type, uint16_t handle,
+ const char *chars);
int read_device_attributes(const bdaddr_t *sba, textfile_cb func, void *data);
int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
uint16_t handle, uint16_t *value);
--
1.7.7.6


2012-05-24 18:46:07

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v2 10/16] core: Fix creating device from "longtermkeys" file

---
src/adapter.c | 26 ++++++++++++++++----------
1 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 2776204..c1caffd 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -1787,7 +1787,8 @@ static struct link_key_info *get_key_info(const char *addr, const char *value)
return info;
}

-static struct smp_ltk_info *get_ltk_info(const char *addr, const char *value)
+static struct smp_ltk_info *get_ltk_info(const char *addr, uint8_t bdaddr_type,
+ const char *value)
{
struct smp_ltk_info *ltk;
char *ptr;
@@ -1802,14 +1803,15 @@ static struct smp_ltk_info *get_ltk_info(const char *addr, const char *value)

str2ba(addr, &ltk->bdaddr);

+ ltk->bdaddr_type = bdaddr_type;
+
str2buf(value, ltk->val, sizeof(ltk->val));

ptr = (char *) value + 2 * sizeof(ltk->val) + 1;

- ret = sscanf(ptr, " %hhd %hhd %hhd %hhd %hd %n",
- (uint8_t *) &ltk->bdaddr_type,
- &ltk->authenticated, &ltk->master,
- &ltk->enc_size, &ltk->ediv, &i);
+ ret = sscanf(ptr, " %hhd %hhd %hhd %hd %n",
+ &ltk->authenticated, &ltk->master, &ltk->enc_size,
+ &ltk->ediv, &i);
if (ret < 2) {
g_free(ltk);
return NULL;
@@ -1851,26 +1853,30 @@ static void create_stored_device_from_ltks(char *key, char *value,
struct btd_adapter *adapter = keys->adapter;
struct btd_device *device;
struct smp_ltk_info *info;
- char srcaddr[18];
+ char address[18], srcaddr[18];
+ uint8_t bdaddr_type;
bdaddr_t src;

- info = get_ltk_info(key, value);
+ if (sscanf(key, "%17s#%hhu", address, &bdaddr_type) < 2)
+ return;
+
+ info = get_ltk_info(address, bdaddr_type, value);
if (info == NULL)
return;

keys->keys = g_slist_append(keys->keys, info);

- if (g_slist_find_custom(adapter->devices, key,
+ if (g_slist_find_custom(adapter->devices, address,
(GCompareFunc) device_address_cmp))
return;

adapter_get_address(adapter, &src);
ba2str(&src, srcaddr);

- if (g_strcmp0(srcaddr, key) == 0)
+ if (g_strcmp0(srcaddr, address) == 0)
return;

- device = device_create(connection, adapter, key, info->bdaddr_type);
+ device = device_create(connection, adapter, address, bdaddr_type);
if (device) {
device_set_temporary(device, FALSE);
adapter->devices = g_slist_append(adapter->devices, device);
--
1.7.7.6


2012-05-24 18:46:06

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v2 09/16] storage: Store address type in "longtermkeys" file

---
src/device.c | 3 ++-
src/event.c | 2 +-
src/storage.c | 16 ++++++++++------
src/storage.h | 5 +++--
4 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/src/device.c b/src/device.c
index e44ce58..2357684 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1090,7 +1090,8 @@ struct btd_device *device_create(DBusConnection *conn,
device_set_bonded(device, TRUE);
}

- if (device_is_le(device) && has_longtermkeys(&src, &device->bdaddr)) {
+ if (device_is_le(device) && has_longtermkeys(&src, &device->bdaddr,
+ device->bdaddr_type)) {
device_set_paired(device, TRUE);
device_set_bonded(device, TRUE);
}
diff --git a/src/event.c b/src/event.c
index a1fc016..acfcef3 100644
--- a/src/event.c
+++ b/src/event.c
@@ -397,7 +397,7 @@ static int store_longtermkey(bdaddr_t *local, bdaddr_t *peer,
newkey = g_string_append(newkey, str);
g_free(str);

- err = write_longtermkeys(local, peer, newkey->str);
+ err = write_longtermkeys(local, peer, bdaddr_type, newkey->str);

g_string_free(newkey, TRUE);

diff --git a/src/storage.c b/src/storage.c
index 1eb916f..caee8f0 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1379,9 +1379,10 @@ void delete_device_ccc(bdaddr_t *local, bdaddr_t *peer)
delete_by_pattern(filename, addr);
}

-int write_longtermkeys(bdaddr_t *local, bdaddr_t *peer, const char *key)
+int write_longtermkeys(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ const char *key)
{
- char filename[PATH_MAX + 1], addr[18];
+ char filename[PATH_MAX + 1], addr[20];

if (!key)
return -EINVAL;
@@ -1391,18 +1392,21 @@ int write_longtermkeys(bdaddr_t *local, bdaddr_t *peer, const char *key)
create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

ba2str(peer, addr);
+ sprintf(&addr[17], "#%hhu", bdaddr_type);
+
return textfile_put(filename, addr, key);
}

-gboolean has_longtermkeys(bdaddr_t *local, bdaddr_t *peer)
+gboolean has_longtermkeys(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type)
{
- char filename[PATH_MAX + 1], addr[18], *str;
+ char filename[PATH_MAX + 1], key[20], *str;

create_filename(filename, PATH_MAX, local, "longtermkeys");

- ba2str(peer, addr);
+ ba2str(peer, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);

- str = textfile_caseget(filename, addr);
+ str = textfile_caseget(filename, key);
if (str) {
free(str);
return TRUE;
diff --git a/src/storage.h b/src/storage.h
index 9032801..67364df 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -101,7 +101,8 @@ int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
int write_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
uint16_t handle, uint16_t value);
void delete_device_ccc(bdaddr_t *local, bdaddr_t *peer);
-int write_longtermkeys(bdaddr_t *local, bdaddr_t *peer, const char *key);
-gboolean has_longtermkeys(bdaddr_t *local, bdaddr_t *peer);
+int write_longtermkeys(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ const char *key);
+gboolean has_longtermkeys(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type);

#define PNP_UUID "00001200-0000-1000-8000-00805f9b34fb"
--
1.7.7.6


2012-05-24 18:46:05

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v2 08/16] storage: Do not store address type twice

"longtermkeys" file already includes address type in its keys. It isn't
necessary to store the address type in its value as well.
---
src/event.c | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/event.c b/src/event.c
index e453e13..a1fc016 100644
--- a/src/event.c
+++ b/src/event.c
@@ -385,8 +385,8 @@ static int store_longtermkey(bdaddr_t *local, bdaddr_t *peer,
newkey = g_string_new(val);
g_free(val);

- g_string_append_printf(newkey, " %d %d %d %d %d ", bdaddr_type,
- authenticated, master, enc_size, ediv);
+ g_string_append_printf(newkey, " %d %d %d %d ", authenticated, master,
+ enc_size, ediv);

str = buf2str(rand, 8);
if (str == NULL) {
--
1.7.7.6


2012-05-24 18:46:04

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v2 07/16] storage: Store address type in blocked file

From: Claudio Takahasi <[email protected]>

---
src/device.c | 6 +++---
src/storage.c | 21 ++++++++++++---------
src/storage.h | 5 +++--
3 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/src/device.c b/src/device.c
index a91eba7..e44ce58 100644
--- a/src/device.c
+++ b/src/device.c
@@ -547,7 +547,7 @@ int device_block(DBusConnection *conn, struct btd_device *device,

adapter_get_address(device->adapter, &src);

- err = write_blocked(&src, &device->bdaddr, TRUE);
+ err = write_blocked(&src, &device->bdaddr, device->bdaddr_type, TRUE);
if (err < 0)
error("write_blocked(): %s (%d)", strerror(-err), -err);

@@ -579,7 +579,7 @@ int device_unblock(DBusConnection *conn, struct btd_device *device,

adapter_get_address(device->adapter, &src);

- err = write_blocked(&src, &device->bdaddr, FALSE);
+ err = write_blocked(&src, &device->bdaddr, device->bdaddr_type, FALSE);
if (err < 0)
error("write_blocked(): %s (%d)", strerror(-err), -err);

@@ -1082,7 +1082,7 @@ struct btd_device *device_create(DBusConnection *conn,
device->alias = g_strdup(alias);
device->trusted = read_trust(&src, address, GLOBAL_TRUST);

- if (read_blocked(&src, &device->bdaddr))
+ if (read_blocked(&src, &device->bdaddr, device->bdaddr_type))
device_block(conn, device, FALSE);

if (read_link_key(&src, &device->bdaddr, NULL, NULL) == 0) {
diff --git a/src/storage.c b/src/storage.c
index f2eaae1..1eb916f 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1144,15 +1144,17 @@ int read_device_pairable(bdaddr_t *bdaddr, gboolean *mode)
return 0;
}

-gboolean read_blocked(const bdaddr_t *local, const bdaddr_t *remote)
+gboolean read_blocked(const bdaddr_t *local, const bdaddr_t *remote,
+ uint8_t bdaddr_type)
{
- char filename[PATH_MAX + 1], *str, addr[18];
+ char filename[PATH_MAX + 1], *str, key[20];

create_filename(filename, PATH_MAX, local, "blocked");

- ba2str(remote, addr);
+ ba2str(remote, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);

- str = textfile_caseget(filename, addr);
+ str = textfile_caseget(filename, key);
if (!str)
return FALSE;

@@ -1162,20 +1164,21 @@ gboolean read_blocked(const bdaddr_t *local, const bdaddr_t *remote)
}

int write_blocked(const bdaddr_t *local, const bdaddr_t *remote,
- gboolean blocked)
+ uint8_t bdaddr_type, gboolean blocked)
{
- char filename[PATH_MAX + 1], addr[18];
+ char filename[PATH_MAX + 1], key[20];

create_filename(filename, PATH_MAX, local, "blocked");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

- ba2str(remote, addr);
+ ba2str(remote, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);

if (blocked == FALSE)
- return textfile_casedel(filename, addr);
+ return textfile_casedel(filename, key);

- return textfile_caseput(filename, addr, "");
+ return textfile_caseput(filename, key, "");
}

int write_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
diff --git a/src/storage.h b/src/storage.h
index eda7f9b..9032801 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -78,9 +78,10 @@ int read_device_id(const gchar *src, const gchar *dst,
uint16_t *product, uint16_t *version);
int write_device_pairable(bdaddr_t *local, gboolean mode);
int read_device_pairable(bdaddr_t *local, gboolean *mode);
-gboolean read_blocked(const bdaddr_t *local, const bdaddr_t *remote);
+gboolean read_blocked(const bdaddr_t *local, const bdaddr_t *remote,
+ uint8_t bdaddr_type);
int write_blocked(const bdaddr_t *local, const bdaddr_t *remote,
- gboolean blocked);
+ uint8_t bdaddr_type, gboolean blocked);
int write_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
uint8_t bdaddr_type, const char *services);
int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
--
1.7.7.6


2012-05-24 18:46:03

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v2 06/16] core: Fix reading stored data from "aliases" and "names" file

---
src/adapter.c | 16 ++++++++++------
1 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index a364935..2776204 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2833,16 +2833,19 @@ static gboolean pairing_is_legacy(bdaddr_t *local, bdaddr_t *peer,
return TRUE;
}

-static char *read_stored_data(bdaddr_t *local, bdaddr_t *peer, const char *file)
+static char *read_stored_data(bdaddr_t *local, bdaddr_t *peer,
+ uint8_t bdaddr_type, const char *file)
{
- char local_addr[18], peer_addr[18], filename[PATH_MAX + 1];
+ char local_addr[18], key[20], filename[PATH_MAX + 1];

ba2str(local, local_addr);
- ba2str(peer, peer_addr);

create_name(filename, PATH_MAX, STORAGEDIR, local_addr, file);

- return textfile_get(filename, peer_addr);
+ ba2str(peer, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);
+
+ return textfile_get(filename, key);
}

void adapter_update_found_devices(struct btd_adapter *adapter,
@@ -2900,7 +2903,7 @@ void adapter_update_found_devices(struct btd_adapter *adapter,

/* New device in the discovery session */

- name = read_stored_data(&adapter->bdaddr, bdaddr, "names");
+ name = read_stored_data(&adapter->bdaddr, bdaddr, bdaddr_type, "names");

if (bdaddr_type == BDADDR_BREDR) {
legacy = pairing_is_legacy(&adapter->bdaddr, bdaddr, data,
@@ -2920,7 +2923,8 @@ void adapter_update_found_devices(struct btd_adapter *adapter,
adapter_ops->confirm_name(adapter->dev_id, bdaddr, bdaddr_type,
name_known);

- alias = read_stored_data(&adapter->bdaddr, bdaddr, "aliases");
+ alias = read_stored_data(&adapter->bdaddr, bdaddr, bdaddr_type,
+ "aliases");

dev = found_device_new(bdaddr, bdaddr_type, name, alias, dev_class,
legacy, eir_data.flags);
--
1.7.7.6


2012-05-24 18:46:02

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v2 05/16] storage: Store address type in "aliases" file

From: Claudio Takahasi <[email protected]>

---
src/device.c | 7 ++++---
src/storage.c | 16 ++++++++++++----
src/storage.h | 6 ++++--
3 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/src/device.c b/src/device.c
index e239da6..a91eba7 100644
--- a/src/device.c
+++ b/src/device.c
@@ -458,8 +458,8 @@ static DBusMessage *set_alias(DBusConnection *conn, DBusMessage *msg,
ba2str(&device->bdaddr, dstaddr);

/* Remove alias if empty string */
- err = write_device_alias(srcaddr, dstaddr,
- g_str_equal(alias, "") ? NULL : alias);
+ err = write_device_alias(srcaddr, dstaddr, device->bdaddr_type,
+ g_str_equal(alias, "") ? NULL : alias);
if (err < 0)
return btd_error_failed(msg, strerror(-err));

@@ -1077,7 +1077,8 @@ struct btd_device *device_create(DBusConnection *conn,
ba2str(&src, srcaddr);

read_device_name(srcaddr, address, bdaddr_type, device->name);
- if (read_device_alias(srcaddr, address, alias, sizeof(alias)) == 0)
+ if (read_device_alias(srcaddr, address, bdaddr_type, alias,
+ sizeof(alias)) == 0)
device->alias = g_strdup(alias);
device->trusted = read_trust(&src, address, GLOBAL_TRUST);

diff --git a/src/storage.c b/src/storage.c
index 6fe9279..f2eaae1 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -61,14 +61,18 @@ static inline int create_filename(char *buf, size_t size,
return create_name(buf, size, STORAGEDIR, addr, name);
}

-int read_device_alias(const char *src, const char *dst, char *alias, size_t size)
+int read_device_alias(const char *src, const char *dst, uint8_t bdaddr_type,
+ char *alias, size_t size)
{
char filename[PATH_MAX + 1], *tmp;
+ char key[20];
int err;

create_name(filename, PATH_MAX, STORAGEDIR, src, "aliases");

- tmp = textfile_get(filename, dst);
+ snprintf(key, sizeof(key), "%17s#%hhu", dst, bdaddr_type);
+
+ tmp = textfile_get(filename, key);
if (!tmp)
return -ENXIO;

@@ -79,15 +83,19 @@ int read_device_alias(const char *src, const char *dst, char *alias, size_t size
return err < 0 ? -EIO : 0;
}

-int write_device_alias(const char *src, const char *dst, const char *alias)
+int write_device_alias(const char *src, const char *dst, uint8_t bdaddr_type,
+ const char *alias)
{
char filename[PATH_MAX + 1];
+ char key[20];

create_name(filename, PATH_MAX, STORAGEDIR, src, "aliases");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

- return textfile_put(filename, dst, alias);
+ snprintf(key, sizeof(key), "%17s#%hhu", dst, bdaddr_type);
+
+ return textfile_put(filename, key, alias);
}

int write_discoverable_timeout(bdaddr_t *bdaddr, int timeout)
diff --git a/src/storage.h b/src/storage.h
index 7443d6f..eda7f9b 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -23,8 +23,10 @@

#include "textfile.h"

-int read_device_alias(const char *src, const char *dst, char *alias, size_t size);
-int write_device_alias(const char *src, const char *dst, const char *alias);
+int read_device_alias(const char *src, const char *dst, uint8_t bdaddr_type,
+ char *alias, size_t size);
+int write_device_alias(const char *src, const char *dst, uint8_t bdaddr_type,
+ const char *alias);
int write_discoverable_timeout(bdaddr_t *bdaddr, int timeout);
int read_discoverable_timeout(const char *src, int *timeout);
int write_pairable_timeout(bdaddr_t *bdaddr, int timeout);
--
1.7.7.6


2012-05-24 18:46:01

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v2 04/16] storage: Store address type in "names" file

"names" file is shared between BR/EDR and BLE. Addressing types can be
either BR/EDR, BLE public or BLE random so the entries in the "names"
file did not contain enough information to distinguish which addressing
type it's supposed to be. Entries will now contain both address number,
and address type as a single key in every entry in the file.
---
input/device.c | 6 ++++--
src/adapter.c | 3 ++-
src/device.c | 3 ++-
src/event.c | 7 ++++++-
src/storage.c | 20 +++++++++++++-------
src/storage.h | 6 ++++--
6 files changed, 31 insertions(+), 14 deletions(-)

diff --git a/input/device.c b/input/device.c
index e2c48b9..be12544 100644
--- a/input/device.c
+++ b/input/device.c
@@ -1084,10 +1084,11 @@ static struct input_device *input_device_new(DBusConnection *conn,
struct btd_adapter *adapter = device_get_adapter(device);
struct input_device *idev;
char name[249], src_addr[18], dst_addr[18];
+ uint8_t bdaddr_type;

idev = g_new0(struct input_device, 1);
adapter_get_address(adapter, &idev->src);
- device_get_address(device, &idev->dst, NULL);
+ device_get_address(device, &idev->dst, &bdaddr_type);
idev->device = btd_device_ref(device);
idev->path = g_strdup(path);
idev->conn = dbus_connection_ref(conn);
@@ -1096,7 +1097,8 @@ static struct input_device *input_device_new(DBusConnection *conn,

ba2str(&idev->src, src_addr);
ba2str(&idev->dst, dst_addr);
- if (read_device_name(src_addr, dst_addr, name) == 0)
+
+ if (read_device_name(src_addr, dst_addr, bdaddr_type, name) == 0)
idev->name = g_strdup(name);

if (g_dbus_register_interface(conn, idev->path, INPUT_DEVICE_INTERFACE,
diff --git a/src/adapter.c b/src/adapter.c
index 1542e06..a364935 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2874,7 +2874,8 @@ void adapter_update_found_devices(struct btd_adapter *adapter,
eir_data.appearance);

if (eir_data.name != NULL && eir_data.name_complete)
- write_device_name(&adapter->bdaddr, bdaddr, eir_data.name);
+ write_device_name(&adapter->bdaddr, bdaddr, bdaddr_type,
+ eir_data.name);

dev = adapter_search_found_devices(adapter, bdaddr);
if (dev) {
diff --git a/src/device.c b/src/device.c
index a01a524..e239da6 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1075,7 +1075,8 @@ struct btd_device *device_create(DBusConnection *conn,
device->bdaddr_type = bdaddr_type;
adapter_get_address(adapter, &src);
ba2str(&src, srcaddr);
- read_device_name(srcaddr, address, device->name);
+
+ read_device_name(srcaddr, address, bdaddr_type, device->name);
if (read_device_alias(srcaddr, address, alias, sizeof(alias)) == 0)
device->alias = g_strdup(alias);
device->trusted = read_trust(&src, address, GLOBAL_TRUST);
diff --git a/src/event.c b/src/event.c
index 8ecb942..e453e13 100644
--- a/src/event.c
+++ b/src/event.c
@@ -319,6 +319,7 @@ void btd_event_remote_name(bdaddr_t *local, bdaddr_t *peer, char *name)
{
struct btd_adapter *adapter;
struct btd_device *device;
+ uint8_t bdaddr_type;
struct remote_dev_info *dev_info;

if (!g_utf8_validate(name, -1, NULL)) {
@@ -333,11 +334,15 @@ void btd_event_remote_name(bdaddr_t *local, bdaddr_t *peer, char *name)
g_strstrip(name);
}

- write_device_name(local, peer, name);
+

if (!get_adapter_and_device(local, peer, &adapter, &device, FALSE))
return;

+ bdaddr_type = device_get_addr_type(device);
+
+ write_device_name(local, peer, bdaddr_type, name);
+
dev_info = adapter_search_found_devices(adapter, peer);
if (dev_info) {
g_free(dev_info->name);
diff --git a/src/storage.c b/src/storage.c
index 3cde9c1..6fe9279 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -358,9 +358,10 @@ int read_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t *class)
return 0;
}

-int write_device_name(bdaddr_t *local, bdaddr_t *peer, char *name)
+int write_device_name(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ char *name)
{
- char filename[PATH_MAX + 1], addr[18], str[HCI_MAX_NAME_LENGTH + 1];
+ char filename[PATH_MAX + 1], key[20], str[HCI_MAX_NAME_LENGTH + 1];
int i;

memset(str, 0, sizeof(str));
@@ -374,18 +375,23 @@ int write_device_name(bdaddr_t *local, bdaddr_t *peer, char *name)

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

- ba2str(peer, addr);
- return textfile_put(filename, addr, str);
+ ba2str(peer, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);
+
+ return textfile_put(filename, key, str);
}

-int read_device_name(const char *src, const char *dst, char *name)
+int read_device_name(const char *src, const char *dst, uint8_t bdaddr_type,
+ char *name)
{
- char filename[PATH_MAX + 1], *str;
+ char filename[PATH_MAX + 1], *str, key[20];
int len;

create_name(filename, PATH_MAX, STORAGEDIR, src, "names");

- str = textfile_get(filename, dst);
+ snprintf(key, sizeof(key), "%17s#%hhu", dst, bdaddr_type);
+
+ str = textfile_get(filename, key);
if (!str)
return -ENOENT;

diff --git a/src/storage.h b/src/storage.h
index b6d24e9..7443d6f 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -42,8 +42,10 @@ int read_remote_appearance(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
uint16_t *appearance);
int write_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t class);
int read_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t *class);
-int write_device_name(bdaddr_t *local, bdaddr_t *peer, char *name);
-int read_device_name(const char *src, const char *dst, char *name);
+int write_device_name(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ char *name);
+int read_device_name(const char *src, const char *dst, uint8_t bdaddr_type,
+ char *name);
int write_remote_eir(bdaddr_t *local, bdaddr_t *peer, uint8_t *data,
uint8_t data_len);
int read_remote_eir(bdaddr_t *local, bdaddr_t *peer, uint8_t *data);
--
1.7.7.6


2012-05-24 18:46:00

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v2 03/16] storage: Store address type in "ccc" file

Addressing types can be either BR/EDR, BLE public or BLE random so
the entries in the "ccc" file did not contain enough information to
distinguish which addressing type it's supposed to be. Entries will
now contain both address number and address type as a single key in
every entry in the file.
---
src/attrib-server.c | 19 ++++++++++++++-----
src/device.c | 5 +++++
src/device.h | 1 +
src/storage.c | 20 ++++++++++----------
src/storage.h | 8 ++++----
5 files changed, 34 insertions(+), 19 deletions(-)

diff --git a/src/attrib-server.c b/src/attrib-server.c
index 21b1501..3291e2d 100644
--- a/src/attrib-server.c
+++ b/src/attrib-server.c
@@ -737,6 +737,7 @@ 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,
@@ -747,9 +748,11 @@ 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,
- handle, &cccval) == 0) {
+ read_device_ccc(&channel->src, &channel->dst, bdaddr_type,
+ handle, &cccval) == 0) {
uint8_t config[2];

att_put_u16(cccval, config);
@@ -775,6 +778,7 @@ 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,
@@ -789,9 +793,11 @@ 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,
- handle, &cccval) == 0) {
+ read_device_ccc(&channel->src, &channel->dst, bdaddr_type,
+ handle, &cccval) == 0) {
uint8_t config[2];

att_put_u16(cccval, config);
@@ -847,7 +853,10 @@ static uint16_t write_value(struct gatt_channel *channel, uint16_t handle,
}
} else {
uint16_t cccval = att_get_u16(value);
- write_device_ccc(&channel->src, &channel->dst, handle, cccval);
+ uint8_t bdaddr_type = device_get_addr_type(channel->device);
+
+ write_device_ccc(&channel->src, &channel->dst, bdaddr_type,
+ handle, cccval);
}

return enc_write_resp(pdu, len);
diff --git a/src/device.c b/src/device.c
index eb015ec..a01a524 100644
--- a/src/device.c
+++ b/src/device.c
@@ -2205,6 +2205,11 @@ void device_set_addr_type(struct btd_device *device, uint8_t bdaddr_type)
device->bdaddr_type = bdaddr_type;
}

+uint8_t device_get_addr_type(struct btd_device *device)
+{
+ return device->bdaddr_type;
+}
+
const gchar *device_get_path(struct btd_device *device)
{
if (!device)
diff --git a/src/device.h b/src/device.h
index 51140c7..26e17f7 100644
--- a/src/device.h
+++ b/src/device.h
@@ -62,6 +62,7 @@ struct btd_adapter *device_get_adapter(struct btd_device *device);
void device_get_address(struct btd_device *device, bdaddr_t *bdaddr,
uint8_t *bdaddr_type);
void device_set_addr_type(struct btd_device *device, uint8_t bdaddr_type);
+uint8_t device_get_addr_type(struct btd_device *device);
const gchar *device_get_path(struct btd_device *device);
struct agent *device_get_agent(struct btd_device *device);
gboolean device_is_bredr(struct btd_device *device);
diff --git a/src/storage.c b/src/storage.c
index ab9b1ed..3cde9c1 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1229,12 +1229,12 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
create_filename(filename, PATH_MAX, sba, "attributes");
delete_by_pattern(filename, key);

+ sprintf(&key[17], "#%hhu", bdaddr_type);
+
/* Deleting all CCC values of a given key */
create_filename(filename, PATH_MAX, sba, "ccc");
delete_by_pattern(filename, key);

- sprintf(&key[17], "#%hhu", bdaddr_type);
-
create_filename(filename, PATH_MAX, sba, "primary");

return textfile_del(filename, key);
@@ -1307,10 +1307,10 @@ int read_device_attributes(const bdaddr_t *sba, textfile_cb func, void *data)
return textfile_foreach(filename, func, data);
}

-int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint16_t handle,
- uint16_t *value)
+int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ uint16_t handle, uint16_t *value)
{
- char filename[PATH_MAX + 1], addr[18], key[23];
+ char filename[PATH_MAX + 1], addr[18], key[25];
char *str;
unsigned int config;
int err = 0;
@@ -1318,7 +1318,7 @@ int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint16_t handle,
create_filename(filename, PATH_MAX, local, "ccc");

ba2str(peer, addr);
- snprintf(key, sizeof(key), "%17s#%04X", addr, handle);
+ snprintf(key, sizeof(key), "%17s#%hhu#%04X", addr, bdaddr_type, handle);

str = textfile_caseget(filename, key);
if (str == NULL)
@@ -1334,18 +1334,18 @@ int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint16_t handle,
return err;
}

-int write_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint16_t handle,
- uint16_t value)
+int write_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ uint16_t handle, uint16_t value)
{
- char filename[PATH_MAX + 1], addr[18], key[23], config[5];
+ char filename[PATH_MAX + 1], addr[18], key[25], config[5];

create_filename(filename, PATH_MAX, local, "ccc");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

ba2str(peer, addr);
+ snprintf(key, sizeof(key), "%17s#%hhu#%04X", addr, bdaddr_type, handle);

- snprintf(key, sizeof(key), "%17s#%04X", addr, handle);
snprintf(config, sizeof(config), "%04X", value);

return textfile_put(filename, key, config);
diff --git a/src/storage.h b/src/storage.h
index db33b43..b6d24e9 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -91,10 +91,10 @@ char *read_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
int write_device_attribute(const bdaddr_t *sba, const bdaddr_t *dba,
uint16_t handle, const char *chars);
int read_device_attributes(const bdaddr_t *sba, textfile_cb func, void *data);
-int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint16_t handle,
- uint16_t *value);
-int write_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint16_t handle,
- uint16_t value);
+int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ uint16_t handle, uint16_t *value);
+int write_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ uint16_t handle, uint16_t value);
void delete_device_ccc(bdaddr_t *local, bdaddr_t *peer);
int write_longtermkeys(bdaddr_t *local, bdaddr_t *peer, const char *key);
gboolean has_longtermkeys(bdaddr_t *local, bdaddr_t *peer);
--
1.7.7.6


2012-05-24 18:45:59

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v2 02/16] storage: Store address type in "appearance" file

BLE addressing types can be either public or random so the entries in
the "appearance" file did not contain enough information to distinguish
which addressing type it's supposed to be (LE public or LE random).

Appearance is an information related to BLE device only. Entries will
now contain both BLE address number and BLE address type as a single key
in every entry in the file.
---
src/adapter.c | 4 ++--
src/device.c | 6 ++++--
src/storage.c | 19 +++++++++++--------
src/storage.h | 6 ++++--
4 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index c6904a1..1542e06 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2731,7 +2731,7 @@ void adapter_emit_device_found(struct btd_adapter *adapter,
dev->legacy = FALSE;

if (read_remote_appearance(&adapter->bdaddr, &dev->bdaddr,
- &app) == 0)
+ dev->bdaddr_type, &app) == 0)
icon = gap_appearance_to_icon(app);
else
icon = NULL;
@@ -2870,7 +2870,7 @@ void adapter_update_found_devices(struct btd_adapter *adapter,
write_remote_class(&adapter->bdaddr, bdaddr, dev_class);

if (eir_data.appearance != 0)
- write_remote_appearance(&adapter->bdaddr, bdaddr,
+ write_remote_appearance(&adapter->bdaddr, bdaddr, bdaddr_type,
eir_data.appearance);

if (eir_data.name != NULL && eir_data.name_complete)
diff --git a/src/device.c b/src/device.c
index 7cdd025..eb015ec 100644
--- a/src/device.c
+++ b/src/device.c
@@ -373,7 +373,8 @@ static DBusMessage *get_properties(DBusConnection *conn,
icon = class_to_icon(class);

dict_append_entry(&dict, "Class", DBUS_TYPE_UINT32, &class);
- } else if (read_remote_appearance(&src, &device->bdaddr, &app) == 0)
+ } else if (read_remote_appearance(&src, &device->bdaddr,
+ device->bdaddr_type, &app) == 0)
/* Appearance */
icon = gap_appearance_to_icon(app);

@@ -1851,7 +1852,8 @@ static void appearance_cb(guint8 status, const guint8 *pdu, guint16 plen,
app = att_get_u16(atval);

adapter_get_address(adapter, &src);
- write_remote_appearance(&src, &device->bdaddr, app);
+ write_remote_appearance(&src, &device->bdaddr, device->bdaddr_type,
+ app);

done:
att_data_list_free(list);
diff --git a/src/storage.c b/src/storage.c
index b783be0..ab9b1ed 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -281,16 +281,17 @@ int read_local_class(bdaddr_t *bdaddr, uint8_t *class)
return 0;
}

-int read_remote_appearance(bdaddr_t *local, bdaddr_t *peer,
+int read_remote_appearance(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
uint16_t *appearance)
{
- char filename[PATH_MAX + 1], addr[18], *str;
+ char filename[PATH_MAX + 1], key[20], *str;

create_filename(filename, PATH_MAX, local, "appearance");

- ba2str(peer, addr);
+ ba2str(peer, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);

- str = textfile_get(filename, addr);
+ str = textfile_get(filename, key);
if (!str)
return -ENOENT;

@@ -305,18 +306,20 @@ int read_remote_appearance(bdaddr_t *local, bdaddr_t *peer,
}

int write_remote_appearance(bdaddr_t *local, bdaddr_t *peer,
- uint16_t appearance)
+ uint8_t bdaddr_type, uint16_t appearance)
{
- char filename[PATH_MAX + 1], addr[18], str[7];
+ char filename[PATH_MAX + 1], key[20], str[7];

create_filename(filename, PATH_MAX, local, "appearance");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

- ba2str(peer, addr);
+ ba2str(peer, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);
+
sprintf(str, "0x%4.4x", appearance);

- return textfile_put(filename, addr, str);
+ return textfile_put(filename, key, str);
}

int write_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t class)
diff --git a/src/storage.h b/src/storage.h
index c3be64a..db33b43 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -36,8 +36,10 @@ int write_local_name(bdaddr_t *bdaddr, const char *name);
int read_local_name(bdaddr_t *bdaddr, char *name);
int write_local_class(bdaddr_t *bdaddr, uint8_t *class);
int read_local_class(bdaddr_t *bdaddr, uint8_t *class);
-int write_remote_appearance(bdaddr_t *local, bdaddr_t *peer, uint16_t appearance);
-int read_remote_appearance(bdaddr_t *local, bdaddr_t *peer, uint16_t *appearance);
+int write_remote_appearance(bdaddr_t *local, bdaddr_t *peer,
+ uint8_t bdaddr_type, uint16_t appearance);
+int read_remote_appearance(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ uint16_t *appearance);
int write_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t class);
int read_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t *class);
int write_device_name(bdaddr_t *local, bdaddr_t *peer, char *name);
--
1.7.7.6


2012-05-24 18:45:58

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v2 01/16] storage: Store address type in "characteristics"

GATT can use BR/EDR or BLE as transport. Addressing types can be either
BR/EDR, BLE public or BLE random so the entries in the "characteristics"
file did not contain enough information to distinguish which addressing
type it's supposed to be. Entries will now contain both address number
and address type as a single key in every entry in the file.
---
attrib/client.c | 25 +++++++++++++++----------
src/storage.c | 19 +++++++++++--------
src/storage.h | 5 +++--
3 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/attrib/client.c b/attrib/client.c
index 8fbc1cc..8aa2800 100644
--- a/attrib/client.c
+++ b/attrib/client.c
@@ -155,15 +155,15 @@ static void remove_attio(struct gatt_service *gatt)
}
}

-static void gatt_get_address(struct gatt_service *gatt,
- bdaddr_t *sba, bdaddr_t *dba)
+static void gatt_get_address(struct gatt_service *gatt, bdaddr_t *sba,
+ bdaddr_t *dba, uint8_t *bdaddr_type)
{
struct btd_device *device = gatt->dev;
struct btd_adapter *adapter;

adapter = device_get_adapter(device);
adapter_get_address(adapter, sba);
- device_get_address(device, dba, NULL);
+ device_get_address(device, dba, bdaddr_type);
}

static int characteristic_handle_cmp(gconstpointer a, gconstpointer b)
@@ -548,13 +548,15 @@ static char *characteristic_list_to_string(GSList *chars)
}

static void store_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t start, GSList *chars)
+ uint8_t bdaddr_type, uint16_t start,
+ GSList *chars)
{
char *characteristics;

characteristics = characteristic_list_to_string(chars);

- write_device_characteristics(sba, dba, start, characteristics);
+ write_device_characteristics(sba, dba, bdaddr_type, start,
+ characteristics);

g_free(characteristics);
}
@@ -614,11 +616,12 @@ static GSList *load_characteristics(struct gatt_service *gatt, uint16_t start)
{
GSList *chrs_list;
bdaddr_t sba, dba;
+ uint8_t bdaddr_type;
char *str;

- gatt_get_address(gatt, &sba, &dba);
+ gatt_get_address(gatt, &sba, &dba, &bdaddr_type);

- str = read_device_characteristics(&sba, &dba, start);
+ str = read_device_characteristics(&sba, &dba, bdaddr_type, start);
if (str == NULL)
return NULL;

@@ -647,7 +650,7 @@ static void store_attribute(struct gatt_service *gatt, uint16_t handle,
for (i = 0, tmp = str + MAX_LEN_UUID_STR; i < len; i++, tmp += 2)
sprintf(tmp, "%02X", value[i]);

- gatt_get_address(gatt, &sba, &dba);
+ gatt_get_address(gatt, &sba, &dba, NULL);

write_device_attribute(&sba, &dba, handle, str);

@@ -886,6 +889,7 @@ static void char_discovered_cb(GSList *characteristics, guint8 status,
uint16_t *previous_end = NULL;
GSList *l;
bdaddr_t sba, dba;
+ uint8_t bdaddr_type;

if (status != 0) {
const char *str = att_ecode2str(status);
@@ -924,8 +928,9 @@ static void char_discovered_cb(GSList *characteristics, guint8 status,
if (previous_end)
*previous_end = prim->range.end;

- gatt_get_address(gatt, &sba, &dba);
- store_characteristics(&sba, &dba, prim->range.start, gatt->chars);
+ gatt_get_address(gatt, &sba, &dba, &bdaddr_type);
+ store_characteristics(&sba, &dba, bdaddr_type, prim->range.start,
+ gatt->chars);

g_slist_foreach(gatt->chars, update_all_chars, gatt);

diff --git a/src/storage.c b/src/storage.c
index a91ee2d..b783be0 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1212,12 +1212,16 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
char filename[PATH_MAX + 1], key[20];

memset(key, 0, sizeof(key));
+
ba2str(dba, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);

/* Deleting all characteristics of a given key */
create_filename(filename, PATH_MAX, sba, "characteristic");
delete_by_pattern(filename, key);

+ key[17] = '\0';
+
/* Deleting all attributes values of a given key */
create_filename(filename, PATH_MAX, sba, "attributes");
delete_by_pattern(filename, key);
@@ -1247,31 +1251,30 @@ char *read_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
}

int write_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t handle, const char *chars)
+ uint8_t bdaddr_type, uint16_t handle,
+ const char *chars)
{
- char filename[PATH_MAX + 1], addr[18], key[23];
+ char filename[PATH_MAX + 1], addr[18], key[25];

create_filename(filename, PATH_MAX, sba, "characteristic");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

ba2str(dba, addr);
-
- snprintf(key, sizeof(key), "%17s#%04X", addr, handle);
+ snprintf(key, sizeof(key), "%17s#%hhu#%04X", addr, bdaddr_type, handle);

return textfile_put(filename, key, chars);
}

char *read_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t handle)
+ uint8_t bdaddr_type, uint16_t handle)
{
- char filename[PATH_MAX + 1], addr[18], key[23];
+ char filename[PATH_MAX + 1], addr[18], key[25];

create_filename(filename, PATH_MAX, sba, "characteristic");

ba2str(dba, addr);
-
- snprintf(key, sizeof(key), "%17s#%04X", addr, handle);
+ snprintf(key, sizeof(key), "%17s#%hhu#%04X", addr, bdaddr_type, handle);

return textfile_caseget(filename, key);
}
diff --git a/src/storage.h b/src/storage.h
index d00976f..c3be64a 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -82,9 +82,10 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
char *read_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
uint8_t bdaddr_type);
int write_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t handle, const char *chars);
+ uint8_t bdaddr_type, uint16_t handle,
+ const char *chars);
char *read_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t handle);
+ uint8_t bdaddr_type, uint16_t handle);
int write_device_attribute(const bdaddr_t *sba, const bdaddr_t *dba,
uint16_t handle, const char *chars);
int read_device_attributes(const bdaddr_t *sba, textfile_cb func, void *data);
--
1.7.7.6


2012-05-24 18:45:57

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ v2 00/16] Store address type on storage - remaining files

This patchset contains the remaining changes for the new storage format
used (bdaddr#type) on storage files (LE-only or shared files).

The files that have been changed so far are:
primaries, characteristics, appearance, ccc, names, aliases,
longtermkeys, attributes, blocked.

Unchanged files:
lastseen, lastused, eir, classes, manufacturers, features, linkkeys,
pincodes, trusts, profiles.


Note: The v2 of this series adds another patch (16/16) which keeps
backward compatibility with the old storage format in blocked
file since it's shared by both BR/EDR and LE devices, and the
patch 15/16 has been modified in order to add some trivial
comments in the changed code just to clarify what it's really
doing.


Claudio Takahasi (4):
storage: Store address type in "aliases" file
storage: Store address type in blocked file
storage: Rename characteristic to characteristics
storage: Rename primary file to primaries

Paulo Alcantara (12):
storage: Store address type in "characteristics"
storage: Store address type in "appearance" file
storage: Store address type in "ccc" file
storage: Store address type in "names" file
core: Fix reading stored data from "aliases" and "names" file
storage: Do not store address type twice
storage: Store address type in "longtermkeys" file
core: Fix creating device from "longtermkeys" file
storage: Store address type in "attributes" file
core: Fix creating device from "blocked" file
core: Fix deleting entries
storage: Keep backward compatibility

attrib/client.c | 31 +++++++----
input/device.c | 6 +-
src/adapter.c | 64 +++++++++++++--------
src/attrib-server.c | 19 +++++--
src/device.c | 51 +++++++++++------
src/device.h | 1 +
src/event.c | 13 +++-
src/storage.c | 157 ++++++++++++++++++++++++++++++--------------------
src/storage.h | 44 +++++++++------
9 files changed, 243 insertions(+), 143 deletions(-)

--
1.7.7.6


2012-05-24 17:57:07

by Paulo Alcantara

[permalink] [raw]
Subject: Re: [PATCH BlueZ 00/15] Store address type on storage - remaining files

Hi Johan,

From: Johan Hedberg <[email protected]>
Date: Thu, 24 May 2012 10:59:23 +0300

> Hi Paulo,
>
> On Wed, May 23, 2012, Paulo Alcantara wrote:
> > This patchset contains the remaining changes for the new storage format
> > used (bdaddr#type) on storage files (LE-only or shared files).
> >
> > The files that have been changed so far are:
> > primaries, characteristics, appearance, ccc, names, aliases,
> > longtermkeys, attributes, blocked.
>
> Any files that are used for BR/EDR would have to keep their backwards
> compatibility. Otherwise someone who upgrades from 4.99 to 4.100 will
> suddenly find that the device they had blocked is not blocked anymore
> and none of their devices have any names. We can't break something like
> that between 4.x versions. Are these patches guaranteeing this backwards
> compatibility? Looking at the patches it doesn't look like they give
> this guarantee.

Ok, you're right. The only file that we should keep backward compatibility on
this series is the blocked file, since it's shared by both BR/EDR and LE devices.

I've just made a patch that ensures that we're keeping backward compatibility
with this file (16/16).

> I could have gone ahead and just applied the patches that are strictly
> LE-only and gone ahead with the 4.100 release, but since you've mixed a
> BR/EDR change and an LE change in 15/15 I can't do that. I think the way
> to proceed is to just do this storage change for LE-only files and then
> with 5.0 convert the others.

I think you missed anything when looking at this patch. Note that the key
variable initially contains only the bdaddr (but not the #type appended), so we
can call delete_entry() for the BR/EDR-only files just before the sprintf() call
(if the condition on if-statement gets true) which appends the "#type" in the
key variable, thus avoiding to set the NULL byte at index 17 of the key variable
for deleting the entries of the BR/EDR-only files.


Paulo

2012-05-24 07:59:23

by Johan Hedberg

[permalink] [raw]
Subject: Re: [PATCH BlueZ 00/15] Store address type on storage - remaining files

Hi Paulo,

On Wed, May 23, 2012, Paulo Alcantara wrote:
> This patchset contains the remaining changes for the new storage format
> used (bdaddr#type) on storage files (LE-only or shared files).
>
> The files that have been changed so far are:
> primaries, characteristics, appearance, ccc, names, aliases,
> longtermkeys, attributes, blocked.

Any files that are used for BR/EDR would have to keep their backwards
compatibility. Otherwise someone who upgrades from 4.99 to 4.100 will
suddenly find that the device they had blocked is not blocked anymore
and none of their devices have any names. We can't break something like
that between 4.x versions. Are these patches guaranteeing this backwards
compatibility? Looking at the patches it doesn't look like they give
this guarantee.

I thought I made it clear that we can break something like this only for
LE (since 4.100 is the first version to be considered officially
supporting LE) but not for BR/EDR.

I could have gone ahead and just applied the patches that are strictly
LE-only and gone ahead with the 4.100 release, but since you've mixed a
BR/EDR change and an LE change in 15/15 I can't do that. I think the way
to proceed is to just do this storage change for LE-only files and then
with 5.0 convert the others.

One thing left for debate is do we still in 5.0 want to avoid a messed
up system where someone has storage files from 4.x times and therefore
gets incomplete device objects (created out of linkkeys files but most
other data is missing, like names, etc). Probably we should ensure that
either devices created out of such storage have all info or then the
devices aren't created at all (meaning we may need to change the key
format even for BR/EDR-only files like linkkeys).

Johan

2012-05-23 18:48:33

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ 15/15] core: Fix deleting entries

The "aliases" and "longtermkeys" files now use the new storage format,
so the correct keys must be passed to delete_entry() function.
---
src/device.c | 19 ++++++++++++-------
1 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/src/device.c b/src/device.c
index 2357684..0d24134 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1155,23 +1155,28 @@ uint16_t btd_device_get_version(struct btd_device *device)
static void device_remove_stored(struct btd_device *device)
{
bdaddr_t src;
- char addr[18];
+ char key[20];
DBusConnection *conn = get_dbus_connection();

adapter_get_address(device->adapter, &src);
- ba2str(&device->bdaddr, addr);
+ ba2str(&device->bdaddr, key);
+
+ delete_entry(&src, "profiles", key);
+ delete_entry(&src, "trusts", key);

if (device_is_bonded(device)) {
- delete_entry(&src, "linkkeys", addr);
- delete_entry(&src, "aliases", addr);
- delete_entry(&src, "longtermkeys", addr);
+ delete_entry(&src, "linkkeys", key);
+
+ sprintf(&key[17], "#%hhu", device->bdaddr_type);
+
+ delete_entry(&src, "aliases", key);
+ delete_entry(&src, "longtermkeys", key);
device_set_bonded(device, FALSE);
device->paired = FALSE;
btd_adapter_remove_bonding(device->adapter, &device->bdaddr,
device->bdaddr_type);
}
- delete_entry(&src, "profiles", addr);
- delete_entry(&src, "trusts", addr);
+
delete_all_records(&src, &device->bdaddr);
delete_device_service(&src, &device->bdaddr, device->bdaddr_type);

--
1.7.7.6


2012-05-23 18:48:32

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ 14/15] storage: Rename primary file to primaries

From: Claudio Takahasi <[email protected]>

This patch renames the "primary" file to "primaries" to be more suitable
with its content.
---
src/adapter.c | 6 +++---
src/storage.c | 6 +++---
2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 585c9ed..39ae3df 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -1945,7 +1945,7 @@ static GSList *string_to_primary_list(char *str)
return l;
}

-static void create_stored_device_from_primary(char *key, char *value,
+static void create_stored_device_from_primaries(char *key, char *value,
void *user_data)
{
struct btd_adapter *adapter = user_data;
@@ -2004,8 +2004,8 @@ static void load_devices(struct btd_adapter *adapter)
textfile_foreach(filename, create_stored_device_from_profiles,
adapter);

- create_name(filename, PATH_MAX, STORAGEDIR, srcaddr, "primary");
- textfile_foreach(filename, create_stored_device_from_primary,
+ create_name(filename, PATH_MAX, STORAGEDIR, srcaddr, "primaries");
+ textfile_foreach(filename, create_stored_device_from_primaries,
adapter);

create_name(filename, PATH_MAX, STORAGEDIR, srcaddr, "linkkeys");
diff --git a/src/storage.c b/src/storage.c
index bc13cee..ae27856 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1186,7 +1186,7 @@ int write_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
{
char filename[PATH_MAX + 1], key[20];

- create_filename(filename, PATH_MAX, sba, "primary");
+ create_filename(filename, PATH_MAX, sba, "primaries");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

@@ -1248,7 +1248,7 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
create_filename(filename, PATH_MAX, sba, "ccc");
delete_by_pattern(filename, key);

- create_filename(filename, PATH_MAX, sba, "primary");
+ create_filename(filename, PATH_MAX, sba, "primaries");

return textfile_del(filename, key);
}
@@ -1258,7 +1258,7 @@ char *read_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
{
char filename[PATH_MAX + 1], key[20];

- create_filename(filename, PATH_MAX, sba, "primary");
+ create_filename(filename, PATH_MAX, sba, "primaries");

ba2str(dba, key);
sprintf(&key[17], "#%hhu", bdaddr_type);
--
1.7.7.6


2012-05-23 18:48:31

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ 13/15] storage: Rename characteristic to characteristics

From: Claudio Takahasi <[email protected]>

This patch renames the "characteristic" file to "characteristics" to be
more suitable with its content.
---
src/storage.c | 6 +++---
1 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/storage.c b/src/storage.c
index 1f8852f..bc13cee 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1237,7 +1237,7 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
sprintf(&key[17], "#%hhu", bdaddr_type);

/* Deleting all characteristics of a given key */
- create_filename(filename, PATH_MAX, sba, "characteristic");
+ create_filename(filename, PATH_MAX, sba, "characteristics");
delete_by_pattern(filename, key);

/* Deleting all attributes values of a given key */
@@ -1272,7 +1272,7 @@ int write_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
{
char filename[PATH_MAX + 1], addr[18], key[25];

- create_filename(filename, PATH_MAX, sba, "characteristic");
+ create_filename(filename, PATH_MAX, sba, "characteristics");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

@@ -1287,7 +1287,7 @@ char *read_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
{
char filename[PATH_MAX + 1], addr[18], key[25];

- create_filename(filename, PATH_MAX, sba, "characteristic");
+ create_filename(filename, PATH_MAX, sba, "characteristics");

ba2str(dba, addr);
snprintf(key, sizeof(key), "%17s#%hhu#%04X", addr, bdaddr_type, handle);
--
1.7.7.6


2012-05-23 18:48:30

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ 12/15] core: Fix creating device from "blocked" file

---
src/adapter.c | 9 +++++++--
1 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 11abcad..585c9ed 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -1893,12 +1893,17 @@ static void create_stored_device_from_blocked(char *key, char *value,
{
struct btd_adapter *adapter = user_data;
struct btd_device *device;
+ char address[18];
+ uint8_t bdaddr_type;
+
+ if (sscanf(key, "%17s#%hhu", address, &bdaddr_type) < 2)
+ return;

if (g_slist_find_custom(adapter->devices,
- key, (GCompareFunc) device_address_cmp))
+ address, (GCompareFunc) device_address_cmp))
return;

- device = device_create(connection, adapter, key, BDADDR_BREDR);
+ device = device_create(connection, adapter, address, bdaddr_type);
if (device) {
device_set_temporary(device, FALSE);
adapter->devices = g_slist_append(adapter->devices, device);
--
1.7.7.6


2012-05-23 18:48:29

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ 11/15] storage: Store address type in "attributes" file

---
attrib/client.c | 6 +++++-
src/storage.c | 12 ++++--------
src/storage.h | 3 ++-
3 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/attrib/client.c b/attrib/client.c
index 8aa2800..7aefd96 100644
--- a/attrib/client.c
+++ b/attrib/client.c
@@ -635,7 +635,9 @@ static GSList *load_characteristics(struct gatt_service *gatt, uint16_t start)
static void store_attribute(struct gatt_service *gatt, uint16_t handle,
uint16_t type, uint8_t *value, gsize len)
{
+ struct btd_device *device = gatt->dev;
bdaddr_t sba, dba;
+ uint8_t bdaddr_type;
bt_uuid_t uuid;
char *str, *tmp;
guint i;
@@ -652,7 +654,9 @@ static void store_attribute(struct gatt_service *gatt, uint16_t handle,

gatt_get_address(gatt, &sba, &dba, NULL);

- write_device_attribute(&sba, &dba, handle, str);
+ bdaddr_type = device_get_addr_type(device);
+
+ write_device_attribute(&sba, &dba, bdaddr_type, handle, str);

g_free(str);
}
diff --git a/src/storage.c b/src/storage.c
index 72dbf68..1f8852f 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1240,14 +1240,10 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
create_filename(filename, PATH_MAX, sba, "characteristic");
delete_by_pattern(filename, key);

- key[17] = '\0';
-
/* Deleting all attributes values of a given key */
create_filename(filename, PATH_MAX, sba, "attributes");
delete_by_pattern(filename, key);

- sprintf(&key[17], "#%hhu", bdaddr_type);
-
/* Deleting all CCC values of a given key */
create_filename(filename, PATH_MAX, sba, "ccc");
delete_by_pattern(filename, key);
@@ -1300,17 +1296,17 @@ char *read_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
}

int write_device_attribute(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t handle, const char *chars)
+ uint8_t bdaddr_type, uint16_t handle,
+ const char *chars)
{
- char filename[PATH_MAX + 1], addr[18], key[23];
+ char filename[PATH_MAX + 1], addr[18], key[25];

create_filename(filename, PATH_MAX, sba, "attributes");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

ba2str(dba, addr);
-
- snprintf(key, sizeof(key), "%17s#%04X", addr, handle);
+ snprintf(key, sizeof(key), "%17s#%hhu#%04X", addr, bdaddr_type, handle);

return textfile_put(filename, key, chars);
}
diff --git a/src/storage.h b/src/storage.h
index 67364df..3705d3d 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -94,7 +94,8 @@ int write_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
char *read_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
uint8_t bdaddr_type, uint16_t handle);
int write_device_attribute(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t handle, const char *chars);
+ uint8_t bdaddr_type, uint16_t handle,
+ const char *chars);
int read_device_attributes(const bdaddr_t *sba, textfile_cb func, void *data);
int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
uint16_t handle, uint16_t *value);
--
1.7.7.6


2012-05-23 18:48:28

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ 10/15] core: Fix creating device from "longtermkeys" file

---
src/adapter.c | 26 ++++++++++++++++----------
1 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 0a168b4..11abcad 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -1792,7 +1792,8 @@ static struct link_key_info *get_key_info(const char *addr, const char *value)
return info;
}

-static struct smp_ltk_info *get_ltk_info(const char *addr, const char *value)
+static struct smp_ltk_info *get_ltk_info(const char *addr, uint8_t bdaddr_type,
+ const char *value)
{
struct smp_ltk_info *ltk;
char *ptr;
@@ -1807,14 +1808,15 @@ static struct smp_ltk_info *get_ltk_info(const char *addr, const char *value)

str2ba(addr, &ltk->bdaddr);

+ ltk->bdaddr_type = bdaddr_type;
+
str2buf(value, ltk->val, sizeof(ltk->val));

ptr = (char *) value + 2 * sizeof(ltk->val) + 1;

- ret = sscanf(ptr, " %hhd %hhd %hhd %hhd %hd %n",
- (uint8_t *) &ltk->bdaddr_type,
- &ltk->authenticated, &ltk->master,
- &ltk->enc_size, &ltk->ediv, &i);
+ ret = sscanf(ptr, " %hhd %hhd %hhd %hd %n",
+ &ltk->authenticated, &ltk->master, &ltk->enc_size,
+ &ltk->ediv, &i);
if (ret < 2) {
g_free(ltk);
return NULL;
@@ -1856,26 +1858,30 @@ static void create_stored_device_from_ltks(char *key, char *value,
struct btd_adapter *adapter = keys->adapter;
struct btd_device *device;
struct smp_ltk_info *info;
- char srcaddr[18];
+ char address[18], srcaddr[18];
+ uint8_t bdaddr_type;
bdaddr_t src;

- info = get_ltk_info(key, value);
+ if (sscanf(key, "%17s#%hhu", address, &bdaddr_type) < 2)
+ return;
+
+ info = get_ltk_info(address, bdaddr_type, value);
if (info == NULL)
return;

keys->keys = g_slist_append(keys->keys, info);

- if (g_slist_find_custom(adapter->devices, key,
+ if (g_slist_find_custom(adapter->devices, address,
(GCompareFunc) device_address_cmp))
return;

adapter_get_address(adapter, &src);
ba2str(&src, srcaddr);

- if (g_strcmp0(srcaddr, key) == 0)
+ if (g_strcmp0(srcaddr, address) == 0)
return;

- device = device_create(connection, adapter, key, info->bdaddr_type);
+ device = device_create(connection, adapter, address, bdaddr_type);
if (device) {
device_set_temporary(device, FALSE);
adapter->devices = g_slist_append(adapter->devices, device);
--
1.7.7.6


2012-05-23 18:48:27

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ 09/15] storage: Store address type in "longtermkeys" file

---
src/device.c | 3 ++-
src/event.c | 2 +-
src/storage.c | 16 ++++++++++------
src/storage.h | 5 +++--
4 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/src/device.c b/src/device.c
index e44ce58..2357684 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1090,7 +1090,8 @@ struct btd_device *device_create(DBusConnection *conn,
device_set_bonded(device, TRUE);
}

- if (device_is_le(device) && has_longtermkeys(&src, &device->bdaddr)) {
+ if (device_is_le(device) && has_longtermkeys(&src, &device->bdaddr,
+ device->bdaddr_type)) {
device_set_paired(device, TRUE);
device_set_bonded(device, TRUE);
}
diff --git a/src/event.c b/src/event.c
index a1fc016..acfcef3 100644
--- a/src/event.c
+++ b/src/event.c
@@ -397,7 +397,7 @@ static int store_longtermkey(bdaddr_t *local, bdaddr_t *peer,
newkey = g_string_append(newkey, str);
g_free(str);

- err = write_longtermkeys(local, peer, newkey->str);
+ err = write_longtermkeys(local, peer, bdaddr_type, newkey->str);

g_string_free(newkey, TRUE);

diff --git a/src/storage.c b/src/storage.c
index fc75e50..72dbf68 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1379,9 +1379,10 @@ void delete_device_ccc(bdaddr_t *local, bdaddr_t *peer)
delete_by_pattern(filename, addr);
}

-int write_longtermkeys(bdaddr_t *local, bdaddr_t *peer, const char *key)
+int write_longtermkeys(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ const char *key)
{
- char filename[PATH_MAX + 1], addr[18];
+ char filename[PATH_MAX + 1], addr[20];

if (!key)
return -EINVAL;
@@ -1391,18 +1392,21 @@ int write_longtermkeys(bdaddr_t *local, bdaddr_t *peer, const char *key)
create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

ba2str(peer, addr);
+ sprintf(&addr[17], "#%hhu", bdaddr_type);
+
return textfile_put(filename, addr, key);
}

-gboolean has_longtermkeys(bdaddr_t *local, bdaddr_t *peer)
+gboolean has_longtermkeys(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type)
{
- char filename[PATH_MAX + 1], addr[18], *str;
+ char filename[PATH_MAX + 1], key[20], *str;

create_filename(filename, PATH_MAX, local, "longtermkeys");

- ba2str(peer, addr);
+ ba2str(peer, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);

- str = textfile_caseget(filename, addr);
+ str = textfile_caseget(filename, key);
if (str) {
free(str);
return TRUE;
diff --git a/src/storage.h b/src/storage.h
index 9032801..67364df 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -101,7 +101,8 @@ int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
int write_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
uint16_t handle, uint16_t value);
void delete_device_ccc(bdaddr_t *local, bdaddr_t *peer);
-int write_longtermkeys(bdaddr_t *local, bdaddr_t *peer, const char *key);
-gboolean has_longtermkeys(bdaddr_t *local, bdaddr_t *peer);
+int write_longtermkeys(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ const char *key);
+gboolean has_longtermkeys(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type);

#define PNP_UUID "00001200-0000-1000-8000-00805f9b34fb"
--
1.7.7.6


2012-05-23 18:48:26

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ 08/15] storage: Do not store address type twice

"longtermkeys" file already includes address type in its keys. It isn't
necessary to store the address type in its value as well.
---
src/event.c | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/event.c b/src/event.c
index e453e13..a1fc016 100644
--- a/src/event.c
+++ b/src/event.c
@@ -385,8 +385,8 @@ static int store_longtermkey(bdaddr_t *local, bdaddr_t *peer,
newkey = g_string_new(val);
g_free(val);

- g_string_append_printf(newkey, " %d %d %d %d %d ", bdaddr_type,
- authenticated, master, enc_size, ediv);
+ g_string_append_printf(newkey, " %d %d %d %d ", authenticated, master,
+ enc_size, ediv);

str = buf2str(rand, 8);
if (str == NULL) {
--
1.7.7.6


2012-05-23 18:48:25

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ 07/15] storage: Store address type in blocked file

From: Claudio Takahasi <[email protected]>

---
src/device.c | 6 +++---
src/storage.c | 21 ++++++++++++---------
src/storage.h | 5 +++--
3 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/src/device.c b/src/device.c
index a91eba7..e44ce58 100644
--- a/src/device.c
+++ b/src/device.c
@@ -547,7 +547,7 @@ int device_block(DBusConnection *conn, struct btd_device *device,

adapter_get_address(device->adapter, &src);

- err = write_blocked(&src, &device->bdaddr, TRUE);
+ err = write_blocked(&src, &device->bdaddr, device->bdaddr_type, TRUE);
if (err < 0)
error("write_blocked(): %s (%d)", strerror(-err), -err);

@@ -579,7 +579,7 @@ int device_unblock(DBusConnection *conn, struct btd_device *device,

adapter_get_address(device->adapter, &src);

- err = write_blocked(&src, &device->bdaddr, FALSE);
+ err = write_blocked(&src, &device->bdaddr, device->bdaddr_type, FALSE);
if (err < 0)
error("write_blocked(): %s (%d)", strerror(-err), -err);

@@ -1082,7 +1082,7 @@ struct btd_device *device_create(DBusConnection *conn,
device->alias = g_strdup(alias);
device->trusted = read_trust(&src, address, GLOBAL_TRUST);

- if (read_blocked(&src, &device->bdaddr))
+ if (read_blocked(&src, &device->bdaddr, device->bdaddr_type))
device_block(conn, device, FALSE);

if (read_link_key(&src, &device->bdaddr, NULL, NULL) == 0) {
diff --git a/src/storage.c b/src/storage.c
index a77d015..fc75e50 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1144,15 +1144,17 @@ int read_device_pairable(bdaddr_t *bdaddr, gboolean *mode)
return 0;
}

-gboolean read_blocked(const bdaddr_t *local, const bdaddr_t *remote)
+gboolean read_blocked(const bdaddr_t *local, const bdaddr_t *remote,
+ uint8_t bdaddr_type)
{
- char filename[PATH_MAX + 1], *str, addr[18];
+ char filename[PATH_MAX + 1], *str, key[20];

create_filename(filename, PATH_MAX, local, "blocked");

- ba2str(remote, addr);
+ ba2str(remote, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);

- str = textfile_caseget(filename, addr);
+ str = textfile_caseget(filename, key);
if (!str)
return FALSE;

@@ -1162,20 +1164,21 @@ gboolean read_blocked(const bdaddr_t *local, const bdaddr_t *remote)
}

int write_blocked(const bdaddr_t *local, const bdaddr_t *remote,
- gboolean blocked)
+ uint8_t bdaddr_type, gboolean blocked)
{
- char filename[PATH_MAX + 1], addr[18];
+ char filename[PATH_MAX + 1], key[20];

create_filename(filename, PATH_MAX, local, "blocked");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

- ba2str(remote, addr);
+ ba2str(remote, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);

if (blocked == FALSE)
- return textfile_casedel(filename, addr);
+ return textfile_casedel(filename, key);

- return textfile_caseput(filename, addr, "");
+ return textfile_caseput(filename, key, "");
}

int write_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
diff --git a/src/storage.h b/src/storage.h
index eda7f9b..9032801 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -78,9 +78,10 @@ int read_device_id(const gchar *src, const gchar *dst,
uint16_t *product, uint16_t *version);
int write_device_pairable(bdaddr_t *local, gboolean mode);
int read_device_pairable(bdaddr_t *local, gboolean *mode);
-gboolean read_blocked(const bdaddr_t *local, const bdaddr_t *remote);
+gboolean read_blocked(const bdaddr_t *local, const bdaddr_t *remote,
+ uint8_t bdaddr_type);
int write_blocked(const bdaddr_t *local, const bdaddr_t *remote,
- gboolean blocked);
+ uint8_t bdaddr_type, gboolean blocked);
int write_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
uint8_t bdaddr_type, const char *services);
int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
--
1.7.7.6


2012-05-23 18:48:24

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ 06/15] core: Fix reading stored data from "aliases" and "names" file

---
src/adapter.c | 16 ++++++++++------
1 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index c06cd4d..0a168b4 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2838,16 +2838,19 @@ static gboolean pairing_is_legacy(bdaddr_t *local, bdaddr_t *peer,
return TRUE;
}

-static char *read_stored_data(bdaddr_t *local, bdaddr_t *peer, const char *file)
+static char *read_stored_data(bdaddr_t *local, bdaddr_t *peer,
+ uint8_t bdaddr_type, const char *file)
{
- char local_addr[18], peer_addr[18], filename[PATH_MAX + 1];
+ char local_addr[18], key[20], filename[PATH_MAX + 1];

ba2str(local, local_addr);
- ba2str(peer, peer_addr);

create_name(filename, PATH_MAX, STORAGEDIR, local_addr, file);

- return textfile_get(filename, peer_addr);
+ ba2str(peer, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);
+
+ return textfile_get(filename, key);
}

void adapter_update_found_devices(struct btd_adapter *adapter,
@@ -2905,7 +2908,7 @@ void adapter_update_found_devices(struct btd_adapter *adapter,

/* New device in the discovery session */

- name = read_stored_data(&adapter->bdaddr, bdaddr, "names");
+ name = read_stored_data(&adapter->bdaddr, bdaddr, bdaddr_type, "names");

if (bdaddr_type == BDADDR_BREDR) {
legacy = pairing_is_legacy(&adapter->bdaddr, bdaddr, data,
@@ -2925,7 +2928,8 @@ void adapter_update_found_devices(struct btd_adapter *adapter,
adapter_ops->confirm_name(adapter->dev_id, bdaddr, bdaddr_type,
name_known);

- alias = read_stored_data(&adapter->bdaddr, bdaddr, "aliases");
+ alias = read_stored_data(&adapter->bdaddr, bdaddr, bdaddr_type,
+ "aliases");

dev = found_device_new(bdaddr, bdaddr_type, name, alias, dev_class,
legacy, eir_data.flags);
--
1.7.7.6


2012-05-23 18:48:23

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ 05/15] storage: Store address type in "aliases" file

From: Claudio Takahasi <[email protected]>

---
src/device.c | 7 ++++---
src/storage.c | 16 ++++++++++++----
src/storage.h | 6 ++++--
3 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/src/device.c b/src/device.c
index e239da6..a91eba7 100644
--- a/src/device.c
+++ b/src/device.c
@@ -458,8 +458,8 @@ static DBusMessage *set_alias(DBusConnection *conn, DBusMessage *msg,
ba2str(&device->bdaddr, dstaddr);

/* Remove alias if empty string */
- err = write_device_alias(srcaddr, dstaddr,
- g_str_equal(alias, "") ? NULL : alias);
+ err = write_device_alias(srcaddr, dstaddr, device->bdaddr_type,
+ g_str_equal(alias, "") ? NULL : alias);
if (err < 0)
return btd_error_failed(msg, strerror(-err));

@@ -1077,7 +1077,8 @@ struct btd_device *device_create(DBusConnection *conn,
ba2str(&src, srcaddr);

read_device_name(srcaddr, address, bdaddr_type, device->name);
- if (read_device_alias(srcaddr, address, alias, sizeof(alias)) == 0)
+ if (read_device_alias(srcaddr, address, bdaddr_type, alias,
+ sizeof(alias)) == 0)
device->alias = g_strdup(alias);
device->trusted = read_trust(&src, address, GLOBAL_TRUST);

diff --git a/src/storage.c b/src/storage.c
index cb275a3..a77d015 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -61,14 +61,18 @@ static inline int create_filename(char *buf, size_t size,
return create_name(buf, size, STORAGEDIR, addr, name);
}

-int read_device_alias(const char *src, const char *dst, char *alias, size_t size)
+int read_device_alias(const char *src, const char *dst, uint8_t bdaddr_type,
+ char *alias, size_t size)
{
char filename[PATH_MAX + 1], *tmp;
+ char key[20];
int err;

create_name(filename, PATH_MAX, STORAGEDIR, src, "aliases");

- tmp = textfile_get(filename, dst);
+ snprintf(key, sizeof(key), "%17s#%hhu", dst, bdaddr_type);
+
+ tmp = textfile_get(filename, key);
if (!tmp)
return -ENXIO;

@@ -79,15 +83,19 @@ int read_device_alias(const char *src, const char *dst, char *alias, size_t size
return err < 0 ? -EIO : 0;
}

-int write_device_alias(const char *src, const char *dst, const char *alias)
+int write_device_alias(const char *src, const char *dst, uint8_t bdaddr_type,
+ const char *alias)
{
char filename[PATH_MAX + 1];
+ char key[20];

create_name(filename, PATH_MAX, STORAGEDIR, src, "aliases");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

- return textfile_put(filename, dst, alias);
+ snprintf(key, sizeof(key), "%17s#%hhu", dst, bdaddr_type);
+
+ return textfile_put(filename, key, alias);
}

int write_discoverable_timeout(bdaddr_t *bdaddr, int timeout)
diff --git a/src/storage.h b/src/storage.h
index 7443d6f..eda7f9b 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -23,8 +23,10 @@

#include "textfile.h"

-int read_device_alias(const char *src, const char *dst, char *alias, size_t size);
-int write_device_alias(const char *src, const char *dst, const char *alias);
+int read_device_alias(const char *src, const char *dst, uint8_t bdaddr_type,
+ char *alias, size_t size);
+int write_device_alias(const char *src, const char *dst, uint8_t bdaddr_type,
+ const char *alias);
int write_discoverable_timeout(bdaddr_t *bdaddr, int timeout);
int read_discoverable_timeout(const char *src, int *timeout);
int write_pairable_timeout(bdaddr_t *bdaddr, int timeout);
--
1.7.7.6


2012-05-23 18:48:22

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ 04/15] storage: Store address type in "names" file

"names" file is shared between BR/EDR and BLE. Addressing types can be
either BR/EDR, BLE public or BLE random so the entries in the "names"
file did not contain enough information to distinguish which addressing
type it's supposed to be. Entries will now contain both address number,
and address type as a single key in every entry in the file.
---
input/device.c | 6 ++++--
src/adapter.c | 3 ++-
src/device.c | 3 ++-
src/event.c | 7 ++++++-
src/storage.c | 20 +++++++++++++-------
src/storage.h | 6 ++++--
6 files changed, 31 insertions(+), 14 deletions(-)

diff --git a/input/device.c b/input/device.c
index 77e77b3..44ee100 100644
--- a/input/device.c
+++ b/input/device.c
@@ -1084,10 +1084,11 @@ static struct input_device *input_device_new(DBusConnection *conn,
struct btd_adapter *adapter = device_get_adapter(device);
struct input_device *idev;
char name[249], src_addr[18], dst_addr[18];
+ uint8_t bdaddr_type;

idev = g_new0(struct input_device, 1);
adapter_get_address(adapter, &idev->src);
- device_get_address(device, &idev->dst, NULL);
+ device_get_address(device, &idev->dst, &bdaddr_type);
idev->device = btd_device_ref(device);
idev->path = g_strdup(path);
idev->conn = dbus_connection_ref(conn);
@@ -1096,7 +1097,8 @@ static struct input_device *input_device_new(DBusConnection *conn,

ba2str(&idev->src, src_addr);
ba2str(&idev->dst, dst_addr);
- if (read_device_name(src_addr, dst_addr, name) == 0)
+
+ if (read_device_name(src_addr, dst_addr, bdaddr_type, name) == 0)
idev->name = g_strdup(name);

if (g_dbus_register_interface(conn, idev->path, INPUT_DEVICE_INTERFACE,
diff --git a/src/adapter.c b/src/adapter.c
index 3200ba8..c06cd4d 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2879,7 +2879,8 @@ void adapter_update_found_devices(struct btd_adapter *adapter,
eir_data.appearance);

if (eir_data.name != NULL && eir_data.name_complete)
- write_device_name(&adapter->bdaddr, bdaddr, eir_data.name);
+ write_device_name(&adapter->bdaddr, bdaddr, bdaddr_type,
+ eir_data.name);

dev = adapter_search_found_devices(adapter, bdaddr);
if (dev) {
diff --git a/src/device.c b/src/device.c
index a01a524..e239da6 100644
--- a/src/device.c
+++ b/src/device.c
@@ -1075,7 +1075,8 @@ struct btd_device *device_create(DBusConnection *conn,
device->bdaddr_type = bdaddr_type;
adapter_get_address(adapter, &src);
ba2str(&src, srcaddr);
- read_device_name(srcaddr, address, device->name);
+
+ read_device_name(srcaddr, address, bdaddr_type, device->name);
if (read_device_alias(srcaddr, address, alias, sizeof(alias)) == 0)
device->alias = g_strdup(alias);
device->trusted = read_trust(&src, address, GLOBAL_TRUST);
diff --git a/src/event.c b/src/event.c
index 8ecb942..e453e13 100644
--- a/src/event.c
+++ b/src/event.c
@@ -319,6 +319,7 @@ void btd_event_remote_name(bdaddr_t *local, bdaddr_t *peer, char *name)
{
struct btd_adapter *adapter;
struct btd_device *device;
+ uint8_t bdaddr_type;
struct remote_dev_info *dev_info;

if (!g_utf8_validate(name, -1, NULL)) {
@@ -333,11 +334,15 @@ void btd_event_remote_name(bdaddr_t *local, bdaddr_t *peer, char *name)
g_strstrip(name);
}

- write_device_name(local, peer, name);
+

if (!get_adapter_and_device(local, peer, &adapter, &device, FALSE))
return;

+ bdaddr_type = device_get_addr_type(device);
+
+ write_device_name(local, peer, bdaddr_type, name);
+
dev_info = adapter_search_found_devices(adapter, peer);
if (dev_info) {
g_free(dev_info->name);
diff --git a/src/storage.c b/src/storage.c
index ed02f96..cb275a3 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -358,9 +358,10 @@ int read_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t *class)
return 0;
}

-int write_device_name(bdaddr_t *local, bdaddr_t *peer, char *name)
+int write_device_name(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ char *name)
{
- char filename[PATH_MAX + 1], addr[18], str[HCI_MAX_NAME_LENGTH + 1];
+ char filename[PATH_MAX + 1], key[20], str[HCI_MAX_NAME_LENGTH + 1];
int i;

memset(str, 0, sizeof(str));
@@ -374,18 +375,23 @@ int write_device_name(bdaddr_t *local, bdaddr_t *peer, char *name)

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

- ba2str(peer, addr);
- return textfile_put(filename, addr, str);
+ ba2str(peer, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);
+
+ return textfile_put(filename, key, str);
}

-int read_device_name(const char *src, const char *dst, char *name)
+int read_device_name(const char *src, const char *dst, uint8_t bdaddr_type,
+ char *name)
{
- char filename[PATH_MAX + 1], *str;
+ char filename[PATH_MAX + 1], *str, key[20];
int len;

create_name(filename, PATH_MAX, STORAGEDIR, src, "names");

- str = textfile_get(filename, dst);
+ snprintf(key, sizeof(key), "%17s#%hhu", dst, bdaddr_type);
+
+ str = textfile_get(filename, key);
if (!str)
return -ENOENT;

diff --git a/src/storage.h b/src/storage.h
index b6d24e9..7443d6f 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -42,8 +42,10 @@ int read_remote_appearance(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
uint16_t *appearance);
int write_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t class);
int read_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t *class);
-int write_device_name(bdaddr_t *local, bdaddr_t *peer, char *name);
-int read_device_name(const char *src, const char *dst, char *name);
+int write_device_name(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ char *name);
+int read_device_name(const char *src, const char *dst, uint8_t bdaddr_type,
+ char *name);
int write_remote_eir(bdaddr_t *local, bdaddr_t *peer, uint8_t *data,
uint8_t data_len);
int read_remote_eir(bdaddr_t *local, bdaddr_t *peer, uint8_t *data);
--
1.7.7.6


2012-05-23 18:48:21

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ 03/15] storage: Store address type in "ccc" file

Addressing types can be either BR/EDR, BLE public or BLE random so
the entries in the "ccc" file did not contain enough information to
distinguish which addressing type it's supposed to be. Entries will
now contain both address number and address type as a single key in
every entry in the file.
---
src/attrib-server.c | 19 ++++++++++++++-----
src/device.c | 5 +++++
src/device.h | 1 +
src/storage.c | 20 ++++++++++----------
src/storage.h | 8 ++++----
5 files changed, 34 insertions(+), 19 deletions(-)

diff --git a/src/attrib-server.c b/src/attrib-server.c
index 21b1501..3291e2d 100644
--- a/src/attrib-server.c
+++ b/src/attrib-server.c
@@ -737,6 +737,7 @@ 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,
@@ -747,9 +748,11 @@ 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,
- handle, &cccval) == 0) {
+ read_device_ccc(&channel->src, &channel->dst, bdaddr_type,
+ handle, &cccval) == 0) {
uint8_t config[2];

att_put_u16(cccval, config);
@@ -775,6 +778,7 @@ 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,
@@ -789,9 +793,11 @@ 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,
- handle, &cccval) == 0) {
+ read_device_ccc(&channel->src, &channel->dst, bdaddr_type,
+ handle, &cccval) == 0) {
uint8_t config[2];

att_put_u16(cccval, config);
@@ -847,7 +853,10 @@ static uint16_t write_value(struct gatt_channel *channel, uint16_t handle,
}
} else {
uint16_t cccval = att_get_u16(value);
- write_device_ccc(&channel->src, &channel->dst, handle, cccval);
+ uint8_t bdaddr_type = device_get_addr_type(channel->device);
+
+ write_device_ccc(&channel->src, &channel->dst, bdaddr_type,
+ handle, cccval);
}

return enc_write_resp(pdu, len);
diff --git a/src/device.c b/src/device.c
index eb015ec..a01a524 100644
--- a/src/device.c
+++ b/src/device.c
@@ -2205,6 +2205,11 @@ void device_set_addr_type(struct btd_device *device, uint8_t bdaddr_type)
device->bdaddr_type = bdaddr_type;
}

+uint8_t device_get_addr_type(struct btd_device *device)
+{
+ return device->bdaddr_type;
+}
+
const gchar *device_get_path(struct btd_device *device)
{
if (!device)
diff --git a/src/device.h b/src/device.h
index 51140c7..26e17f7 100644
--- a/src/device.h
+++ b/src/device.h
@@ -62,6 +62,7 @@ struct btd_adapter *device_get_adapter(struct btd_device *device);
void device_get_address(struct btd_device *device, bdaddr_t *bdaddr,
uint8_t *bdaddr_type);
void device_set_addr_type(struct btd_device *device, uint8_t bdaddr_type);
+uint8_t device_get_addr_type(struct btd_device *device);
const gchar *device_get_path(struct btd_device *device);
struct agent *device_get_agent(struct btd_device *device);
gboolean device_is_bredr(struct btd_device *device);
diff --git a/src/storage.c b/src/storage.c
index bf3c5bb..ed02f96 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1229,12 +1229,12 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
create_filename(filename, PATH_MAX, sba, "attributes");
delete_by_pattern(filename, key);

+ sprintf(&key[17], "#%hhu", bdaddr_type);
+
/* Deleting all CCC values of a given key */
create_filename(filename, PATH_MAX, sba, "ccc");
delete_by_pattern(filename, key);

- sprintf(&key[17], "#%hhu", bdaddr_type);
-
create_filename(filename, PATH_MAX, sba, "primary");

return textfile_del(filename, key);
@@ -1307,10 +1307,10 @@ int read_device_attributes(const bdaddr_t *sba, textfile_cb func, void *data)
return textfile_foreach(filename, func, data);
}

-int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint16_t handle,
- uint16_t *value)
+int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ uint16_t handle, uint16_t *value)
{
- char filename[PATH_MAX + 1], addr[18], key[23];
+ char filename[PATH_MAX + 1], addr[18], key[25];
char *str;
unsigned int config;
int err = 0;
@@ -1318,7 +1318,7 @@ int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint16_t handle,
create_filename(filename, PATH_MAX, local, "ccc");

ba2str(peer, addr);
- snprintf(key, sizeof(key), "%17s#%04X", addr, handle);
+ snprintf(key, sizeof(key), "%17s#%hhu#%04X", addr, bdaddr_type, handle);

str = textfile_caseget(filename, key);
if (str == NULL)
@@ -1334,18 +1334,18 @@ int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint16_t handle,
return err;
}

-int write_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint16_t handle,
- uint16_t value)
+int write_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ uint16_t handle, uint16_t value)
{
- char filename[PATH_MAX + 1], addr[18], key[23], config[5];
+ char filename[PATH_MAX + 1], addr[18], key[25], config[5];

create_filename(filename, PATH_MAX, local, "ccc");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

ba2str(peer, addr);
+ snprintf(key, sizeof(key), "%17s#%hhu#%04X", addr, bdaddr_type, handle);

- snprintf(key, sizeof(key), "%17s#%04X", addr, handle);
snprintf(config, sizeof(config), "%04X", value);

return textfile_put(filename, key, config);
diff --git a/src/storage.h b/src/storage.h
index db33b43..b6d24e9 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -91,10 +91,10 @@ char *read_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
int write_device_attribute(const bdaddr_t *sba, const bdaddr_t *dba,
uint16_t handle, const char *chars);
int read_device_attributes(const bdaddr_t *sba, textfile_cb func, void *data);
-int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint16_t handle,
- uint16_t *value);
-int write_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint16_t handle,
- uint16_t value);
+int read_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ uint16_t handle, uint16_t *value);
+int write_device_ccc(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ uint16_t handle, uint16_t value);
void delete_device_ccc(bdaddr_t *local, bdaddr_t *peer);
int write_longtermkeys(bdaddr_t *local, bdaddr_t *peer, const char *key);
gboolean has_longtermkeys(bdaddr_t *local, bdaddr_t *peer);
--
1.7.7.6


2012-05-23 18:48:20

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ 02/15] storage: Store address type in "appearance" file

BLE addressing types can be either public or random so the entries in
the "appearance" file did not contain enough information to distinguish
which addressing type it's supposed to be (LE public or LE random).

Appearance is an information related to BLE device only. Entries will
now contain both BLE address number and BLE address type as a single key
in every entry in the file.
---
src/adapter.c | 4 ++--
src/device.c | 6 ++++--
src/storage.c | 19 +++++++++++--------
src/storage.h | 6 ++++--
4 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 18dd5b6..3200ba8 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2736,7 +2736,7 @@ void adapter_emit_device_found(struct btd_adapter *adapter,
dev->legacy = FALSE;

if (read_remote_appearance(&adapter->bdaddr, &dev->bdaddr,
- &app) == 0)
+ dev->bdaddr_type, &app) == 0)
icon = gap_appearance_to_icon(app);
else
icon = NULL;
@@ -2875,7 +2875,7 @@ void adapter_update_found_devices(struct btd_adapter *adapter,
write_remote_class(&adapter->bdaddr, bdaddr, dev_class);

if (eir_data.appearance != 0)
- write_remote_appearance(&adapter->bdaddr, bdaddr,
+ write_remote_appearance(&adapter->bdaddr, bdaddr, bdaddr_type,
eir_data.appearance);

if (eir_data.name != NULL && eir_data.name_complete)
diff --git a/src/device.c b/src/device.c
index 7cdd025..eb015ec 100644
--- a/src/device.c
+++ b/src/device.c
@@ -373,7 +373,8 @@ static DBusMessage *get_properties(DBusConnection *conn,
icon = class_to_icon(class);

dict_append_entry(&dict, "Class", DBUS_TYPE_UINT32, &class);
- } else if (read_remote_appearance(&src, &device->bdaddr, &app) == 0)
+ } else if (read_remote_appearance(&src, &device->bdaddr,
+ device->bdaddr_type, &app) == 0)
/* Appearance */
icon = gap_appearance_to_icon(app);

@@ -1851,7 +1852,8 @@ static void appearance_cb(guint8 status, const guint8 *pdu, guint16 plen,
app = att_get_u16(atval);

adapter_get_address(adapter, &src);
- write_remote_appearance(&src, &device->bdaddr, app);
+ write_remote_appearance(&src, &device->bdaddr, device->bdaddr_type,
+ app);

done:
att_data_list_free(list);
diff --git a/src/storage.c b/src/storage.c
index 5c0ef5c..bf3c5bb 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -281,16 +281,17 @@ int read_local_class(bdaddr_t *bdaddr, uint8_t *class)
return 0;
}

-int read_remote_appearance(bdaddr_t *local, bdaddr_t *peer,
+int read_remote_appearance(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
uint16_t *appearance)
{
- char filename[PATH_MAX + 1], addr[18], *str;
+ char filename[PATH_MAX + 1], key[20], *str;

create_filename(filename, PATH_MAX, local, "appearance");

- ba2str(peer, addr);
+ ba2str(peer, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);

- str = textfile_get(filename, addr);
+ str = textfile_get(filename, key);
if (!str)
return -ENOENT;

@@ -305,18 +306,20 @@ int read_remote_appearance(bdaddr_t *local, bdaddr_t *peer,
}

int write_remote_appearance(bdaddr_t *local, bdaddr_t *peer,
- uint16_t appearance)
+ uint8_t bdaddr_type, uint16_t appearance)
{
- char filename[PATH_MAX + 1], addr[18], str[7];
+ char filename[PATH_MAX + 1], key[20], str[7];

create_filename(filename, PATH_MAX, local, "appearance");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

- ba2str(peer, addr);
+ ba2str(peer, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);
+
sprintf(str, "0x%4.4x", appearance);

- return textfile_put(filename, addr, str);
+ return textfile_put(filename, key, str);
}

int write_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t class)
diff --git a/src/storage.h b/src/storage.h
index c3be64a..db33b43 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -36,8 +36,10 @@ int write_local_name(bdaddr_t *bdaddr, const char *name);
int read_local_name(bdaddr_t *bdaddr, char *name);
int write_local_class(bdaddr_t *bdaddr, uint8_t *class);
int read_local_class(bdaddr_t *bdaddr, uint8_t *class);
-int write_remote_appearance(bdaddr_t *local, bdaddr_t *peer, uint16_t appearance);
-int read_remote_appearance(bdaddr_t *local, bdaddr_t *peer, uint16_t *appearance);
+int write_remote_appearance(bdaddr_t *local, bdaddr_t *peer,
+ uint8_t bdaddr_type, uint16_t appearance);
+int read_remote_appearance(bdaddr_t *local, bdaddr_t *peer, uint8_t bdaddr_type,
+ uint16_t *appearance);
int write_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t class);
int read_remote_class(bdaddr_t *local, bdaddr_t *peer, uint32_t *class);
int write_device_name(bdaddr_t *local, bdaddr_t *peer, char *name);
--
1.7.7.6


2012-05-23 18:48:19

by Paulo Alcantara

[permalink] [raw]
Subject: [PATCH BlueZ 01/15] storage: Store address type in "characteristics"

GATT can use BR/EDR or BLE as transport. Addressing types can be either
BR/EDR, BLE public or BLE random so the entries in the "characteristics"
file did not contain enough information to distinguish which addressing
type it's supposed to be. Entries will now contain both address number
and address type as a single key in every entry in the file.
---
attrib/client.c | 25 +++++++++++++++----------
src/storage.c | 19 +++++++++++--------
src/storage.h | 5 +++--
3 files changed, 29 insertions(+), 20 deletions(-)

diff --git a/attrib/client.c b/attrib/client.c
index 8fbc1cc..8aa2800 100644
--- a/attrib/client.c
+++ b/attrib/client.c
@@ -155,15 +155,15 @@ static void remove_attio(struct gatt_service *gatt)
}
}

-static void gatt_get_address(struct gatt_service *gatt,
- bdaddr_t *sba, bdaddr_t *dba)
+static void gatt_get_address(struct gatt_service *gatt, bdaddr_t *sba,
+ bdaddr_t *dba, uint8_t *bdaddr_type)
{
struct btd_device *device = gatt->dev;
struct btd_adapter *adapter;

adapter = device_get_adapter(device);
adapter_get_address(adapter, sba);
- device_get_address(device, dba, NULL);
+ device_get_address(device, dba, bdaddr_type);
}

static int characteristic_handle_cmp(gconstpointer a, gconstpointer b)
@@ -548,13 +548,15 @@ static char *characteristic_list_to_string(GSList *chars)
}

static void store_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t start, GSList *chars)
+ uint8_t bdaddr_type, uint16_t start,
+ GSList *chars)
{
char *characteristics;

characteristics = characteristic_list_to_string(chars);

- write_device_characteristics(sba, dba, start, characteristics);
+ write_device_characteristics(sba, dba, bdaddr_type, start,
+ characteristics);

g_free(characteristics);
}
@@ -614,11 +616,12 @@ static GSList *load_characteristics(struct gatt_service *gatt, uint16_t start)
{
GSList *chrs_list;
bdaddr_t sba, dba;
+ uint8_t bdaddr_type;
char *str;

- gatt_get_address(gatt, &sba, &dba);
+ gatt_get_address(gatt, &sba, &dba, &bdaddr_type);

- str = read_device_characteristics(&sba, &dba, start);
+ str = read_device_characteristics(&sba, &dba, bdaddr_type, start);
if (str == NULL)
return NULL;

@@ -647,7 +650,7 @@ static void store_attribute(struct gatt_service *gatt, uint16_t handle,
for (i = 0, tmp = str + MAX_LEN_UUID_STR; i < len; i++, tmp += 2)
sprintf(tmp, "%02X", value[i]);

- gatt_get_address(gatt, &sba, &dba);
+ gatt_get_address(gatt, &sba, &dba, NULL);

write_device_attribute(&sba, &dba, handle, str);

@@ -886,6 +889,7 @@ static void char_discovered_cb(GSList *characteristics, guint8 status,
uint16_t *previous_end = NULL;
GSList *l;
bdaddr_t sba, dba;
+ uint8_t bdaddr_type;

if (status != 0) {
const char *str = att_ecode2str(status);
@@ -924,8 +928,9 @@ static void char_discovered_cb(GSList *characteristics, guint8 status,
if (previous_end)
*previous_end = prim->range.end;

- gatt_get_address(gatt, &sba, &dba);
- store_characteristics(&sba, &dba, prim->range.start, gatt->chars);
+ gatt_get_address(gatt, &sba, &dba, &bdaddr_type);
+ store_characteristics(&sba, &dba, bdaddr_type, prim->range.start,
+ gatt->chars);

g_slist_foreach(gatt->chars, update_all_chars, gatt);

diff --git a/src/storage.c b/src/storage.c
index 973d545..5c0ef5c 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1212,12 +1212,16 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
char filename[PATH_MAX + 1], key[20];

memset(key, 0, sizeof(key));
+
ba2str(dba, key);
+ sprintf(&key[17], "#%hhu", bdaddr_type);

/* Deleting all characteristics of a given key */
create_filename(filename, PATH_MAX, sba, "characteristic");
delete_by_pattern(filename, key);

+ key[17] = '\0';
+
/* Deleting all attributes values of a given key */
create_filename(filename, PATH_MAX, sba, "attributes");
delete_by_pattern(filename, key);
@@ -1247,31 +1251,30 @@ char *read_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
}

int write_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t handle, const char *chars)
+ uint8_t bdaddr_type, uint16_t handle,
+ const char *chars)
{
- char filename[PATH_MAX + 1], addr[18], key[23];
+ char filename[PATH_MAX + 1], addr[18], key[25];

create_filename(filename, PATH_MAX, sba, "characteristic");

create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

ba2str(dba, addr);
-
- snprintf(key, sizeof(key), "%17s#%04X", addr, handle);
+ snprintf(key, sizeof(key), "%17s#%hhu#%04X", addr, bdaddr_type, handle);

return textfile_put(filename, key, chars);
}

char *read_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t handle)
+ uint8_t bdaddr_type, uint16_t handle)
{
- char filename[PATH_MAX + 1], addr[18], key[23];
+ char filename[PATH_MAX + 1], addr[18], key[25];

create_filename(filename, PATH_MAX, sba, "characteristic");

ba2str(dba, addr);
-
- snprintf(key, sizeof(key), "%17s#%04X", addr, handle);
+ snprintf(key, sizeof(key), "%17s#%hhu#%04X", addr, bdaddr_type, handle);

return textfile_caseget(filename, key);
}
diff --git a/src/storage.h b/src/storage.h
index d00976f..c3be64a 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -82,9 +82,10 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba,
char *read_device_services(const bdaddr_t *sba, const bdaddr_t *dba,
uint8_t bdaddr_type);
int write_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t handle, const char *chars);
+ uint8_t bdaddr_type, uint16_t handle,
+ const char *chars);
char *read_device_characteristics(const bdaddr_t *sba, const bdaddr_t *dba,
- uint16_t handle);
+ uint8_t bdaddr_type, uint16_t handle);
int write_device_attribute(const bdaddr_t *sba, const bdaddr_t *dba,
uint16_t handle, const char *chars);
int read_device_attributes(const bdaddr_t *sba, textfile_cb func, void *data);
--
1.7.7.6