linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/5] adapter: Convert storage aliases
@ 2012-11-14 11:28 Frédéric Danis
  2012-11-14 11:28 ` [PATCH v2 2/5] device: Use new storage for device alias Frédéric Danis
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Frédéric Danis @ 2012-11-14 11:28 UTC (permalink / raw)
  To: linux-bluetooth

---
 src/adapter.c |   66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)

diff --git a/src/adapter.c b/src/adapter.c
index f9eb3af..7ba821e 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2533,6 +2533,69 @@ static void convert_names_entry(char *key, char *value, void *user_data)
 	store_cached_name(&local, &peer, value);
 }
 
+struct device_converter {
+	char *address;
+	void (*cb)(GKeyFile *key_file, void *value);
+};
+
+static void convert_aliases_entry(GKeyFile *key_file, void *value)
+{
+	g_key_file_set_string(key_file, "General", "Alias", value);
+}
+
+static void convert_entry(char *key, char *value, void *user_data)
+{
+	struct device_converter *converter = user_data;
+	char filename[PATH_MAX + 1];
+	GKeyFile *key_file;
+	char *data;
+	gsize length = 0;
+
+	if (key[17] == '#')
+		key[17] = '\0';
+
+	if (bachk(key) != 0)
+		return;
+
+	snprintf(filename, PATH_MAX, STORAGEDIR "/%s/%s/info",
+			converter->address, key);
+	filename[PATH_MAX] = '\0';
+	create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+
+	key_file = g_key_file_new();
+	g_key_file_load_from_file(key_file, filename, 0, NULL);
+	converter->cb(key_file, value);
+
+	data = g_key_file_to_data(key_file, &length, NULL);
+	g_file_set_contents(filename, data, length, NULL);
+	g_free(data);
+
+	g_key_file_free(key_file);
+}
+
+static void convert_file(char *file, char *address,
+				void (*cb)(GKeyFile *key_file, void *value))
+{
+	char filename[PATH_MAX + 1];
+	struct device_converter converter;
+	char *str;
+
+	snprintf(filename, PATH_MAX, STORAGEDIR "/%s/%s", address, file);
+	filename[PATH_MAX] = '\0';
+
+	str = textfile_get(filename, "converted");
+	if (str && strcmp(str, "yes") == 0) {
+		DBG("Legacy file %s already converted", filename);
+	} else {
+		converter.address = address;
+		converter.cb = cb;
+
+		textfile_foreach(filename, convert_entry, &converter);
+		textfile_put(filename, "converted", "yes");
+	}
+	free(str);
+}
+
 static void convert_device_storage(struct btd_adapter *adapter)
 {
 	char filename[PATH_MAX + 1];
@@ -2553,6 +2616,9 @@ static void convert_device_storage(struct btd_adapter *adapter)
 		textfile_put(filename, "converted", "yes");
 	}
 	free(str);
+
+	/* Convert aliases */
+	convert_file("aliases", address, convert_aliases_entry);
 }
 
 static void convert_config(struct btd_adapter *adapter, const char *filename,
-- 
1.7.9.5


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

* [PATCH v2 2/5] device: Use new storage for device alias
  2012-11-14 11:28 [PATCH v2 1/5] adapter: Convert storage aliases Frédéric Danis
@ 2012-11-14 11:28 ` Frédéric Danis
  2012-11-14 11:28 ` [PATCH v2 3/5] adapter: Convert storage trusts Frédéric Danis
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Frédéric Danis @ 2012-11-14 11:28 UTC (permalink / raw)
  To: linux-bluetooth

As set_alias() changes value in device structure and write to
storage is deferred, property set could not return failure.
---
 src/device.c |   28 +++++++++++-----------------
 1 file changed, 11 insertions(+), 17 deletions(-)

diff --git a/src/device.c b/src/device.c
index 48970d2..c508d18 100644
--- a/src/device.c
+++ b/src/device.c
@@ -222,6 +222,10 @@ static gboolean store_device_info_cb(gpointer user_data)
 
 	g_key_file_set_string(key_file, "General", "Name", device->name);
 
+	if (device->alias != NULL)
+		g_key_file_set_string(key_file, "General", "Alias",
+					device->alias);
+
 	ba2str(adapter_get_address(device->adapter), adapter_addr);
 	ba2str(&device->bdaddr, device_addr);
 	snprintf(filename, PATH_MAX, INFO_PATH, adapter_addr, device_addr);
@@ -432,28 +436,17 @@ static void set_alias(GDBusPendingPropertySet id, const char *alias,
 								void *data)
 {
 	struct btd_device *device = data;
-	struct btd_adapter *adapter = device->adapter;
-	char srcaddr[18], dstaddr[18];
-	int err;
 
 	/* No change */
 	if ((device->alias == NULL && g_str_equal(alias, "")) ||
 					g_strcmp0(device->alias, alias) == 0)
 		return g_dbus_pending_property_success(id);
 
-	ba2str(adapter_get_address(adapter), srcaddr);
-	ba2str(&device->bdaddr, dstaddr);
-
-	/* Remove alias if empty string */
-	err = write_device_alias(srcaddr, dstaddr, device->bdaddr_type,
-					g_str_equal(alias, "") ? NULL : alias);
-	if (err < 0)
-		return g_dbus_pending_property_error(id,
-				ERROR_INTERFACE ".Failed", strerror(-err));
-
 	g_free(device->alias);
 	device->alias = g_str_equal(alias, "") ? NULL : g_strdup(alias);
 
+	store_device_info(device);
+
 	g_dbus_emit_property_changed(btd_get_dbus_connection(),
 				device->path, DEVICE_INTERFACE, "Alias");
 
@@ -1746,6 +1739,10 @@ static void load_info(struct btd_device *device, const gchar *local,
 		g_free(str);
 	}
 
+	/* Load alias */
+	device->alias = g_key_file_get_string(key_file, "General", "Alias",
+						NULL);
+
 	if (store_needed)
 		store_device_info(device);
 
@@ -1759,7 +1756,7 @@ struct btd_device *device_create(struct btd_adapter *adapter,
 	struct btd_device *device;
 	const gchar *adapter_path = adapter_get_path(adapter);
 	const bdaddr_t *src;
-	char srcaddr[18], alias[MAX_NAME_LENGTH + 1];
+	char srcaddr[18];
 	uint16_t vendor, product, version;
 
 	device = g_try_malloc0(sizeof(struct btd_device));
@@ -1790,9 +1787,6 @@ struct btd_device *device_create(struct btd_adapter *adapter,
 
 	load_info(device, srcaddr, address);
 
-	if (read_device_alias(srcaddr, address, bdaddr_type, alias,
-							sizeof(alias)) == 0)
-		device->alias = g_strdup(alias);
 	device->trusted = read_trust(src, address, device->bdaddr_type);
 
 	if (read_blocked(src, &device->bdaddr, device->bdaddr_type))
-- 
1.7.9.5


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

* [PATCH v2 3/5] adapter: Convert storage trusts
  2012-11-14 11:28 [PATCH v2 1/5] adapter: Convert storage aliases Frédéric Danis
  2012-11-14 11:28 ` [PATCH v2 2/5] device: Use new storage for device alias Frédéric Danis
@ 2012-11-14 11:28 ` Frédéric Danis
  2012-11-14 11:28 ` [PATCH v2 4/5] device: Use new storage for device trust Frédéric Danis
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Frédéric Danis @ 2012-11-14 11:28 UTC (permalink / raw)
  To: linux-bluetooth

All entry in trusts file are set to [all] (if a device is not trusted
it does not have entry in this file).
So, we do not need to check entry value and set device (entry key) as
trusted.
---
 src/adapter.c |    8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/adapter.c b/src/adapter.c
index 7ba821e..bdea378 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2543,6 +2543,11 @@ static void convert_aliases_entry(GKeyFile *key_file, void *value)
 	g_key_file_set_string(key_file, "General", "Alias", value);
 }
 
+static void convert_trusts_entry(GKeyFile *key_file, void *value)
+{
+	g_key_file_set_boolean(key_file, "General", "Trusted", TRUE);
+}
+
 static void convert_entry(char *key, char *value, void *user_data)
 {
 	struct device_converter *converter = user_data;
@@ -2619,6 +2624,9 @@ static void convert_device_storage(struct btd_adapter *adapter)
 
 	/* Convert aliases */
 	convert_file("aliases", address, convert_aliases_entry);
+
+	/* Convert trusts */
+	convert_file("trusts", address, convert_trusts_entry);
 }
 
 static void convert_config(struct btd_adapter *adapter, const char *filename,
-- 
1.7.9.5


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

* [PATCH v2 4/5] device: Use new storage for device trust
  2012-11-14 11:28 [PATCH v2 1/5] adapter: Convert storage aliases Frédéric Danis
  2012-11-14 11:28 ` [PATCH v2 2/5] device: Use new storage for device alias Frédéric Danis
  2012-11-14 11:28 ` [PATCH v2 3/5] adapter: Convert storage trusts Frédéric Danis
@ 2012-11-14 11:28 ` Frédéric Danis
  2012-11-14 11:28 ` [PATCH v2 5/5] adapter: Remove storage path #defines Frédéric Danis
  2012-11-14 11:54 ` [PATCH v2 1/5] adapter: Convert storage aliases Johan Hedberg
  4 siblings, 0 replies; 6+ messages in thread
From: Frédéric Danis @ 2012-11-14 11:28 UTC (permalink / raw)
  To: linux-bluetooth

As set_trust() changes value in device structure and write to
storage is deferred, property set could not return failure.
---
 src/device.c |   22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/src/device.c b/src/device.c
index c508d18..792fe3d 100644
--- a/src/device.c
+++ b/src/device.c
@@ -226,6 +226,9 @@ static gboolean store_device_info_cb(gpointer user_data)
 		g_key_file_set_string(key_file, "General", "Alias",
 					device->alias);
 
+	g_key_file_set_boolean(key_file, "General", "Trusted",
+				device->trusted);
+
 	ba2str(adapter_get_address(device->adapter), adapter_addr);
 	ba2str(&device->bdaddr, device_addr);
 	snprintf(filename, PATH_MAX, INFO_PATH, adapter_addr, device_addr);
@@ -713,23 +716,14 @@ static gboolean dev_property_get_trusted(const GDBusPropertyTable *property,
 static void set_trust(GDBusPendingPropertySet id, gboolean value, void *data)
 {
 	struct btd_device *device = data;
-	struct btd_adapter *adapter = device->adapter;
-	char srcaddr[18], dstaddr[18];
-	int err;
 
 	if (device->trusted == value)
 		return g_dbus_pending_property_success(id);
 
-	ba2str(adapter_get_address(adapter), srcaddr);
-	ba2str(&device->bdaddr, dstaddr);
-
-	err = write_trust(srcaddr, dstaddr, device->bdaddr_type, value);
-	if (err < 0)
-		return g_dbus_pending_property_error(id,
-				ERROR_INTERFACE ".Failed", strerror(-err));
-
 	device->trusted = value;
 
+	store_device_info(device);
+
 	g_dbus_emit_property_changed(btd_get_dbus_connection(),
 				device->path, DEVICE_INTERFACE, "Trusted");
 
@@ -1743,6 +1737,10 @@ static void load_info(struct btd_device *device, const gchar *local,
 	device->alias = g_key_file_get_string(key_file, "General", "Alias",
 						NULL);
 
+	/* Load trust */
+	device->trusted = g_key_file_get_boolean(key_file, "General",
+							"Trusted", NULL);
+
 	if (store_needed)
 		store_device_info(device);
 
@@ -1787,8 +1785,6 @@ struct btd_device *device_create(struct btd_adapter *adapter,
 
 	load_info(device, srcaddr, address);
 
-	device->trusted = read_trust(src, address, device->bdaddr_type);
-
 	if (read_blocked(src, &device->bdaddr, device->bdaddr_type))
 		device_block(device, FALSE);
 
-- 
1.7.9.5


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

* [PATCH v2 5/5] adapter: Remove storage path #defines
  2012-11-14 11:28 [PATCH v2 1/5] adapter: Convert storage aliases Frédéric Danis
                   ` (2 preceding siblings ...)
  2012-11-14 11:28 ` [PATCH v2 4/5] device: Use new storage for device trust Frédéric Danis
@ 2012-11-14 11:28 ` Frédéric Danis
  2012-11-14 11:54 ` [PATCH v2 1/5] adapter: Convert storage aliases Johan Hedberg
  4 siblings, 0 replies; 6+ messages in thread
From: Frédéric Danis @ 2012-11-14 11:28 UTC (permalink / raw)
  To: linux-bluetooth

SETTINGS_PATH and CACHE_PATH will be static for the
entire 5.x series
---
 src/adapter.c |    9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index bdea378..3f09295 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -84,9 +84,6 @@
 #define REMOVE_TEMP_TIMEOUT (3 * 60)
 #define PENDING_FOUND_MAX 5
 
-#define SETTINGS_PATH STORAGEDIR "/%s/settings"
-#define CACHE_PATH STORAGEDIR "/%s/cache/%s"
-
 static GSList *adapter_drivers = NULL;
 
 enum session_req_type {
@@ -244,7 +241,7 @@ static void store_adapter_info(struct btd_adapter *adapter)
 					adapter->discov_timeout);
 
 	ba2str(&adapter->bdaddr, address);
-	snprintf(filename, PATH_MAX, SETTINGS_PATH, address);
+	snprintf(filename, PATH_MAX, STORAGEDIR "/%s/settings", address);
 	filename[PATH_MAX] = '\0';
 
 	create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
@@ -267,7 +264,7 @@ static void store_cached_name(const bdaddr_t *local, const bdaddr_t *peer,
 
 	ba2str(local, s_addr);
 	ba2str(peer, d_addr);
-	snprintf(filename, PATH_MAX, CACHE_PATH, s_addr, d_addr);
+	snprintf(filename, PATH_MAX, STORAGEDIR "/%s/cache/%s", s_addr, d_addr);
 	filename[PATH_MAX] = '\0';
 	create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
 
@@ -2703,7 +2700,7 @@ static void load_config(struct btd_adapter *adapter)
 
 	key_file = g_key_file_new();
 
-	snprintf(filename, PATH_MAX, SETTINGS_PATH, address);
+	snprintf(filename, PATH_MAX, STORAGEDIR "/%s/settings", address);
 	filename[PATH_MAX] = '\0';
 
 	if (!g_key_file_load_from_file(key_file, filename, 0, NULL))
-- 
1.7.9.5


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

* Re: [PATCH v2 1/5] adapter: Convert storage aliases
  2012-11-14 11:28 [PATCH v2 1/5] adapter: Convert storage aliases Frédéric Danis
                   ` (3 preceding siblings ...)
  2012-11-14 11:28 ` [PATCH v2 5/5] adapter: Remove storage path #defines Frédéric Danis
@ 2012-11-14 11:54 ` Johan Hedberg
  4 siblings, 0 replies; 6+ messages in thread
From: Johan Hedberg @ 2012-11-14 11:54 UTC (permalink / raw)
  To: Frédéric Danis; +Cc: linux-bluetooth

Hi Frederic,

On Wed, Nov 14, 2012, Frédéric Danis wrote:
> ---
>  src/adapter.c |   66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 66 insertions(+)

All patches in this set have been applied. Thanks.

Johan

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

end of thread, other threads:[~2012-11-14 11:54 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-14 11:28 [PATCH v2 1/5] adapter: Convert storage aliases Frédéric Danis
2012-11-14 11:28 ` [PATCH v2 2/5] device: Use new storage for device alias Frédéric Danis
2012-11-14 11:28 ` [PATCH v2 3/5] adapter: Convert storage trusts Frédéric Danis
2012-11-14 11:28 ` [PATCH v2 4/5] device: Use new storage for device trust Frédéric Danis
2012-11-14 11:28 ` [PATCH v2 5/5] adapter: Remove storage path #defines Frédéric Danis
2012-11-14 11:54 ` [PATCH v2 1/5] adapter: Convert storage aliases Johan Hedberg

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