All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/7] Move adapter config file to ini-file format
@ 2012-10-09 15:58 Frédéric Danis
  2012-10-09 15:58 ` [PATCH 1/7] adapter: Read name in storage at init Frédéric Danis
                   ` (7 more replies)
  0 siblings, 8 replies; 12+ messages in thread
From: Frédéric Danis @ 2012-10-09 15:58 UTC (permalink / raw)
  To: linux-bluetooth

Adapter saved configuration will be saved to /var/lib/bluetooth/<adapter address>/adapter
in ini-file format for BlueZ 5.
If this file does not exist, we try to convert old config file to it.

Access to variables during run-time is performed in adapter structure which is populated by loading saved configuration during initialization.

First 6 patches remove access to config file from run-time.
Last patch move to ini-file format style (load, save and convert).

Frédéric Danis (7):
  adapter: Read name in storage at init
  adapter: Read device class in storage at init
  adapter: Move pairable read to load_config()
  adapter: Read pairable timeout in storage at init
  adapter: Read discoverable timeout in storage at init
  adapter: Read mode in storage at init
  adapter: Move saved config to ini-file format

 plugins/adaptername.c |    9 +-
 src/adapter.c         |  313 +++++++++++++++++++++++++++++++++++++------------
 src/adapter.h         |    1 +
 3 files changed, 245 insertions(+), 78 deletions(-)

-- 
1.7.9.5


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

* [PATCH 1/7] adapter: Read name in storage at init
  2012-10-09 15:58 [PATCH 0/7] Move adapter config file to ini-file format Frédéric Danis
@ 2012-10-09 15:58 ` Frédéric Danis
  2012-10-09 15:58 ` [PATCH 2/7] adapter: Read device class " Frédéric Danis
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 12+ messages in thread
From: Frédéric Danis @ 2012-10-09 15:58 UTC (permalink / raw)
  To: linux-bluetooth

---
 plugins/adaptername.c |    9 ++++++---
 src/adapter.c         |   34 ++++++++++++++++++++++++++++++++++
 src/adapter.h         |    1 +
 3 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/plugins/adaptername.c b/plugins/adaptername.c
index f58fb0f..46dbbe8 100644
--- a/plugins/adaptername.c
+++ b/plugins/adaptername.c
@@ -198,7 +198,8 @@ static void set_pretty_name(struct btd_adapter *adapter,
 static int adaptername_probe(struct btd_adapter *adapter)
 {
 	int current_id;
-	char name[MAX_NAME_LENGTH + 1];
+	char *name;
+	char str[MAX_NAME_LENGTH + 1];
 	char *pretty_hostname;
 
 	pretty_hostname = read_pretty_host_name();
@@ -211,8 +212,10 @@ static int adaptername_probe(struct btd_adapter *adapter)
 	adapter_set_allow_name_changes(adapter, TRUE);
 	current_id = adapter_get_dev_id(adapter);
 
-	if (read_local_name(adapter_get_address(adapter), name) < 0)
-		expand_name(name, MAX_NAME_LENGTH, main_opts.name, current_id);
+	if (adapter_get_config_name(adapter, &name) < 0) {
+		expand_name(str, MAX_NAME_LENGTH, main_opts.name, current_id);
+		name = str;
+	}
 
 	DBG("Setting name '%s' for device 'hci%d'", name, current_id);
 	adapter_set_name(adapter, name);
diff --git a/src/adapter.c b/src/adapter.c
index 6b28651..d5379c1 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -113,6 +113,10 @@ struct service_auth {
 	struct agent *agent;		/* NULL for queued auths */
 };
 
+struct btd_adapter_config {
+	char *name;
+};
+
 struct btd_adapter {
 	uint16_t dev_id;
 	gboolean up;
@@ -153,6 +157,8 @@ struct btd_adapter {
 	gboolean pairable;		/* pairable state */
 	gboolean initialized;
 
+	struct btd_adapter_config config;
+
 	gboolean off_requested;		/* DEVDOWN ioctl was called */
 
 	gint ref;
@@ -778,11 +784,24 @@ int adapter_set_name(struct btd_adapter *adapter, const char *name)
 		adapter->name = g_strdup(maxname);
 	}
 
+	g_free(adapter->config.name);
+	adapter->config.name = g_strdup(maxname);
+
 	write_local_name(&adapter->bdaddr, maxname);
 
 	return 0;
 }
 
+int adapter_get_config_name(struct btd_adapter *adapter, char **name)
+{
+	if (adapter->config.name == NULL)
+		return -EINVAL;
+
+	*name = adapter->config.name;
+
+	return 0;
+}
+
 static void set_name(struct btd_adapter *adapter, const char *name,
 						GDBusPendingPropertySet id)
 {
@@ -2661,6 +2680,18 @@ void btd_adapter_unref(struct btd_adapter *adapter)
 	g_free(path);
 }
 
+static void load_config(struct btd_adapter *adapter)
+{
+	char name[MAX_NAME_LENGTH + 1];
+
+	/* Get name */
+	if (read_local_name(&adapter->bdaddr, name) < 0)
+		adapter->config.name = NULL;
+	else
+		adapter->config.name = g_strdup(name);
+
+}
+
 gboolean adapter_init(struct btd_adapter *adapter, gboolean up)
 {
 	adapter->up = up;
@@ -2680,6 +2711,7 @@ gboolean adapter_init(struct btd_adapter *adapter, gboolean up)
 	if (main_opts.gatt_enabled)
 		btd_adapter_gatt_server_start(adapter);
 
+	load_config(adapter);
 	load_drivers(adapter);
 	btd_profile_foreach(probe_profile, adapter);
 	clear_blocked(adapter);
@@ -2748,6 +2780,8 @@ void adapter_remove(struct btd_adapter *adapter)
 	/* Return adapter to down state if it was not up on init */
 	if (!adapter->already_up && adapter->up)
 		mgmt_set_powered(adapter->dev_id, FALSE);
+
+	g_free(adapter->config.name);
 }
 
 uint16_t adapter_get_dev_id(struct btd_adapter *adapter)
diff --git a/src/adapter.h b/src/adapter.h
index 72a9988..3e00f4f 100644
--- a/src/adapter.h
+++ b/src/adapter.h
@@ -133,6 +133,7 @@ void adapter_update_found_devices(struct btd_adapter *adapter,
 void adapter_emit_device_found(struct btd_adapter *adapter,
 						struct remote_dev_info *dev);
 void adapter_mode_changed(struct btd_adapter *adapter, uint8_t scan_mode);
+int adapter_get_config_name(struct btd_adapter *adapter, char **name);
 int adapter_set_name(struct btd_adapter *adapter, const char *name);
 void adapter_name_changed(struct btd_adapter *adapter, const char *name);
 void adapter_service_insert(struct btd_adapter *adapter, void *rec);
-- 
1.7.9.5


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

* [PATCH 2/7] adapter: Read device class in storage at init
  2012-10-09 15:58 [PATCH 0/7] Move adapter config file to ini-file format Frédéric Danis
  2012-10-09 15:58 ` [PATCH 1/7] adapter: Read name in storage at init Frédéric Danis
@ 2012-10-09 15:58 ` Frédéric Danis
  2012-10-09 15:58 ` [PATCH 3/7] adapter: Move pairable read to load_config() Frédéric Danis
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 12+ messages in thread
From: Frédéric Danis @ 2012-10-09 15:58 UTC (permalink / raw)
  To: linux-bluetooth

---
 src/adapter.c |   21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index d5379c1..f66d2ab 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -115,6 +115,7 @@ struct service_auth {
 
 struct btd_adapter_config {
 	char *name;
+	uint8_t class[3];
 };
 
 struct btd_adapter {
@@ -727,6 +728,10 @@ void btd_adapter_class_changed(struct btd_adapter *adapter, uint8_t *new_class)
 	if (class == adapter->dev_class)
 		return;
 
+	adapter->config.class[0] = new_class[0];
+	adapter->config.class[1] = new_class[1];
+	adapter->config.class[2] = new_class[2];
+
 	write_local_class(&adapter->bdaddr, new_class);
 
 	adapter->dev_class = class;
@@ -2351,15 +2356,8 @@ void btd_adapter_get_mode(struct btd_adapter *adapter, uint8_t *mode,
 void btd_adapter_read_class(struct btd_adapter *adapter, uint8_t *major,
 								uint8_t *minor)
 {
-	uint8_t cls[3];
-
-	if (read_local_class(&adapter->bdaddr, cls) < 0) {
-		uint32_t class = htobl(main_opts.class);
-		memcpy(cls, &class, 3);
-	}
-
-	*major = cls[1];
-	*minor = cls[0];
+	*major = adapter->config.class[1];
+	*minor = adapter->config.class[0];
 }
 
 uint32_t btd_adapter_get_class(struct btd_adapter *adapter)
@@ -2690,6 +2688,11 @@ static void load_config(struct btd_adapter *adapter)
 	else
 		adapter->config.name = g_strdup(name);
 
+	/* Get class */
+	if (read_local_class(&adapter->bdaddr, adapter->config.class) < 0) {
+		uint32_t class = htobl(main_opts.class);
+		memcpy(adapter->config.class, &class, 3);
+	}
 }
 
 gboolean adapter_init(struct btd_adapter *adapter, gboolean up)
-- 
1.7.9.5


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

* [PATCH 3/7] adapter: Move pairable read to load_config()
  2012-10-09 15:58 [PATCH 0/7] Move adapter config file to ini-file format Frédéric Danis
  2012-10-09 15:58 ` [PATCH 1/7] adapter: Read name in storage at init Frédéric Danis
  2012-10-09 15:58 ` [PATCH 2/7] adapter: Read device class " Frédéric Danis
@ 2012-10-09 15:58 ` Frédéric Danis
  2012-10-09 15:58 ` [PATCH 4/7] adapter: Read pairable timeout in storage at init Frédéric Danis
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 12+ messages in thread
From: Frédéric Danis @ 2012-10-09 15:58 UTC (permalink / raw)
  To: linux-bluetooth

---
 src/adapter.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index f66d2ab..3dc91df 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2693,6 +2693,10 @@ static void load_config(struct btd_adapter *adapter)
 		uint32_t class = htobl(main_opts.class);
 		memcpy(adapter->config.class, &class, 3);
 	}
+
+	/* Get pairable mode */
+	if (read_device_pairable(&adapter->bdaddr, &adapter->pairable) < 0)
+		adapter->pairable = TRUE;
 }
 
 gboolean adapter_init(struct btd_adapter *adapter, gboolean up)
@@ -2720,10 +2724,6 @@ gboolean adapter_init(struct btd_adapter *adapter, gboolean up)
 	clear_blocked(adapter);
 	load_devices(adapter);
 
-	/* Set pairable mode */
-	if (read_device_pairable(&adapter->bdaddr, &adapter->pairable) < 0)
-		adapter->pairable = TRUE;
-
 	/* retrieve the active connections: address the scenario where
 	 * the are active connections before the daemon've started */
 	load_connections(adapter);
-- 
1.7.9.5


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

* [PATCH 4/7] adapter: Read pairable timeout in storage at init
  2012-10-09 15:58 [PATCH 0/7] Move adapter config file to ini-file format Frédéric Danis
                   ` (2 preceding siblings ...)
  2012-10-09 15:58 ` [PATCH 3/7] adapter: Move pairable read to load_config() Frédéric Danis
@ 2012-10-09 15:58 ` Frédéric Danis
  2012-10-09 15:58 ` [PATCH 5/7] adapter: Read discoverable " Frédéric Danis
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 12+ messages in thread
From: Frédéric Danis @ 2012-10-09 15:58 UTC (permalink / raw)
  To: linux-bluetooth

---
 src/adapter.c |   21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 3dc91df..ed9f848 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2276,16 +2276,6 @@ static int get_discoverable_timeout(const char *src)
 	return main_opts.discovto;
 }
 
-static int get_pairable_timeout(const char *src)
-{
-	int timeout;
-
-	if (read_pairable_timeout(src, &timeout) == 0)
-		return timeout;
-
-	return main_opts.pairto;
-}
-
 static void set_auto_connect(gpointer data, gpointer user_data)
 {
 	struct btd_device *device = data;
@@ -2429,7 +2419,6 @@ void btd_adapter_start(struct btd_adapter *adapter)
 	adapter->off_requested = FALSE;
 	adapter->up = TRUE;
 	adapter->discov_timeout = get_discoverable_timeout(address);
-	adapter->pairable_timeout = get_pairable_timeout(address);
 	adapter->off_timer = 0;
 
 	if (adapter->scan_mode & SCAN_INQUIRY)
@@ -2681,6 +2670,10 @@ void btd_adapter_unref(struct btd_adapter *adapter)
 static void load_config(struct btd_adapter *adapter)
 {
 	char name[MAX_NAME_LENGTH + 1];
+	char address[18];
+	int timeout;
+
+	ba2str(&adapter->bdaddr, address);
 
 	/* Get name */
 	if (read_local_name(&adapter->bdaddr, name) < 0)
@@ -2697,6 +2690,12 @@ static void load_config(struct btd_adapter *adapter)
 	/* Get pairable mode */
 	if (read_device_pairable(&adapter->bdaddr, &adapter->pairable) < 0)
 		adapter->pairable = TRUE;
+
+	/* Get pairable timeout */
+	if (read_pairable_timeout(address, &timeout) < 0)
+		adapter->pairable_timeout = main_opts.pairto;
+	else
+		adapter->pairable_timeout = timeout;
 }
 
 gboolean adapter_init(struct btd_adapter *adapter, gboolean up)
-- 
1.7.9.5


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

* [PATCH 5/7] adapter: Read discoverable timeout in storage at init
  2012-10-09 15:58 [PATCH 0/7] Move adapter config file to ini-file format Frédéric Danis
                   ` (3 preceding siblings ...)
  2012-10-09 15:58 ` [PATCH 4/7] adapter: Read pairable timeout in storage at init Frédéric Danis
@ 2012-10-09 15:58 ` Frédéric Danis
  2012-10-09 15:58 ` [PATCH 6/7] adapter: Read mode " Frédéric Danis
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 12+ messages in thread
From: Frédéric Danis @ 2012-10-09 15:58 UTC (permalink / raw)
  To: linux-bluetooth

---
 src/adapter.c |   19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index ed9f848..03ed0f0 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -2266,16 +2266,6 @@ static void load_connections(struct btd_adapter *adapter)
 	g_slist_free_full(conns, g_free);
 }
 
-static int get_discoverable_timeout(const char *src)
-{
-	int timeout;
-
-	if (read_discoverable_timeout(src, &timeout) == 0)
-		return timeout;
-
-	return main_opts.discovto;
-}
-
 static void set_auto_connect(gpointer data, gpointer user_data)
 {
 	struct btd_device *device = data;
@@ -2337,7 +2327,7 @@ void btd_adapter_get_mode(struct btd_adapter *adapter, uint8_t *mode,
 		*on_mode = get_mode(&adapter->bdaddr, "on");
 
 	if (discoverable_timeout)
-		*discoverable_timeout = get_discoverable_timeout(address);
+		*discoverable_timeout = adapter->discov_timeout;
 
 	if (pairable)
 		*pairable = adapter->pairable;
@@ -2418,7 +2408,6 @@ void btd_adapter_start(struct btd_adapter *adapter)
 	adapter->dev_class = 0;
 	adapter->off_requested = FALSE;
 	adapter->up = TRUE;
-	adapter->discov_timeout = get_discoverable_timeout(address);
 	adapter->off_timer = 0;
 
 	if (adapter->scan_mode & SCAN_INQUIRY)
@@ -2696,6 +2685,12 @@ static void load_config(struct btd_adapter *adapter)
 		adapter->pairable_timeout = main_opts.pairto;
 	else
 		adapter->pairable_timeout = timeout;
+
+	/* Get discoverable timeout */
+	if (read_discoverable_timeout(address, &timeout) < 0)
+		adapter->discov_timeout = main_opts.discovto;
+	else
+		adapter->discov_timeout = timeout;
 }
 
 gboolean adapter_init(struct btd_adapter *adapter, gboolean up)
-- 
1.7.9.5


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

* [PATCH 6/7] adapter: Read mode in storage at init
  2012-10-09 15:58 [PATCH 0/7] Move adapter config file to ini-file format Frédéric Danis
                   ` (4 preceding siblings ...)
  2012-10-09 15:58 ` [PATCH 5/7] adapter: Read discoverable " Frédéric Danis
@ 2012-10-09 15:58 ` Frédéric Danis
  2012-10-09 15:58 ` [PATCH 7/7] adapter: Move saved config to ini-file format Frédéric Danis
  2012-10-09 16:08 ` [PATCH 0/7] Move adapter config file " Marcel Holtmann
  7 siblings, 0 replies; 12+ messages in thread
From: Frédéric Danis @ 2012-10-09 15:58 UTC (permalink / raw)
  To: linux-bluetooth

---
 src/adapter.c |   61 ++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 34 insertions(+), 27 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 03ed0f0..8582a0e 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -116,6 +116,8 @@ struct service_auth {
 struct btd_adapter_config {
 	char *name;
 	uint8_t class[3];
+	uint8_t mode;
+	uint8_t on_mode;
 };
 
 struct btd_adapter {
@@ -208,7 +210,7 @@ static const char *mode2str(uint8_t mode)
 	}
 }
 
-static uint8_t get_mode(const bdaddr_t *bdaddr, const char *mode)
+static uint8_t get_mode(const char *mode)
 {
 	if (strcasecmp("off", mode) == 0)
 		return MODE_OFF;
@@ -216,15 +218,7 @@ static uint8_t get_mode(const bdaddr_t *bdaddr, const char *mode)
 		return MODE_CONNECTABLE;
 	else if (strcasecmp("discoverable", mode) == 0)
 		return MODE_DISCOVERABLE;
-	else if (strcasecmp("on", mode) == 0) {
-		char onmode[14], srcaddr[18];
-
-		ba2str(bdaddr, srcaddr);
-		if (read_on_mode(srcaddr, onmode, sizeof(onmode)) < 0)
-			return MODE_CONNECTABLE;
-
-		return get_mode(bdaddr, onmode);
-	} else
+	else
 		return MODE_UNKNOWN;
 }
 
@@ -329,6 +323,10 @@ static int set_mode(struct btd_adapter *adapter, uint8_t new_mode)
 		return err;
 
 done:
+	adapter->config.mode = new_mode;
+	if (new_mode != MODE_OFF)
+		adapter->config.on_mode = new_mode;
+
 	modestr = mode2str(new_mode);
 	write_device_mode(&adapter->bdaddr, modestr);
 
@@ -388,7 +386,7 @@ static void set_powered(struct btd_adapter *adapter, gboolean powered,
 	int err;
 
 	if (powered) {
-		mode = get_mode(&adapter->bdaddr, "on");
+		mode = adapter->config.on_mode;
 		return set_discoverable(adapter, mode == MODE_DISCOVERABLE,
 									id);
 	}
@@ -1401,7 +1399,7 @@ static DBusMessage *request_session(DBusConnection *conn,
 	if (!adapter->mode_sessions)
 		adapter->global_mode = adapter->mode;
 
-	new_mode = get_mode(&adapter->bdaddr, "on");
+	new_mode = adapter->config.on_mode;
 
 	req = find_session(adapter->mode_sessions, sender);
 	if (req) {
@@ -2310,21 +2308,15 @@ void btd_adapter_get_mode(struct btd_adapter *adapter, uint8_t *mode,
 						uint16_t *discoverable_timeout,
 						gboolean *pairable)
 {
-	char str[14], address[18];
+	char address[18];
 
 	ba2str(&adapter->bdaddr, address);
 
-	if (mode) {
-		if (main_opts.remember_powered == FALSE)
-			*mode = main_opts.mode;
-		else if (read_device_mode(address, str, sizeof(str)) == 0)
-			*mode = get_mode(&adapter->bdaddr, str);
-		else
-			*mode = main_opts.mode;
-	}
+	if (mode)
+		*mode = adapter->config.mode;
 
 	if (on_mode)
-		*on_mode = get_mode(&adapter->bdaddr, "on");
+		*on_mode = adapter->config.on_mode;
 
 	if (discoverable_timeout)
 		*discoverable_timeout = adapter->discov_timeout;
@@ -2486,6 +2478,10 @@ static void set_mode_complete(struct btd_adapter *adapter)
 	const char *modestr;
 	int err;
 
+	adapter->config.mode = adapter->mode;
+	if (adapter->mode != MODE_OFF)
+		adapter->config.on_mode = adapter->mode;
+
 	modestr = mode2str(adapter->mode);
 	write_device_mode(&adapter->bdaddr, modestr);
 
@@ -2660,6 +2656,7 @@ static void load_config(struct btd_adapter *adapter)
 {
 	char name[MAX_NAME_LENGTH + 1];
 	char address[18];
+	char mode[14];
 	int timeout;
 
 	ba2str(&adapter->bdaddr, address);
@@ -2691,6 +2688,20 @@ static void load_config(struct btd_adapter *adapter)
 		adapter->discov_timeout = main_opts.discovto;
 	else
 		adapter->discov_timeout = timeout;
+
+	/* Get mode */
+	if (main_opts.remember_powered == FALSE)
+		adapter->config.mode = main_opts.mode;
+	else if (read_device_mode(address, mode, sizeof(mode)) == 0)
+		adapter->config.mode = get_mode(mode);
+	else
+		adapter->config.mode = main_opts.mode;
+
+	/* Get on mode */
+	if (read_on_mode(address, mode, sizeof(mode)) == 0)
+		adapter->config.on_mode = get_mode(mode);
+	else
+		adapter->config.on_mode = MODE_CONNECTABLE;
 }
 
 gboolean adapter_init(struct btd_adapter *adapter, gboolean up)
@@ -3625,17 +3636,13 @@ gboolean adapter_powering_down(struct btd_adapter *adapter)
 
 int btd_adapter_restore_powered(struct btd_adapter *adapter)
 {
-	char mode[14], address[18];
-
 	if (!main_opts.remember_powered)
 		return -EINVAL;
 
 	if (adapter->up)
 		return 0;
 
-	ba2str(&adapter->bdaddr, address);
-	if (read_device_mode(address, mode, sizeof(mode)) == 0 &&
-						g_str_equal(mode, "off"))
+	if (adapter->config.mode == MODE_OFF)
 		return 0;
 
 	return mgmt_set_powered(adapter->dev_id, TRUE);
-- 
1.7.9.5


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

* [PATCH 7/7] adapter: Move saved config to ini-file format
  2012-10-09 15:58 [PATCH 0/7] Move adapter config file to ini-file format Frédéric Danis
                   ` (5 preceding siblings ...)
  2012-10-09 15:58 ` [PATCH 6/7] adapter: Read mode " Frédéric Danis
@ 2012-10-09 15:58 ` Frédéric Danis
  2012-10-09 17:04   ` Anderson Lizardo
  2012-10-09 16:08 ` [PATCH 0/7] Move adapter config file " Marcel Holtmann
  7 siblings, 1 reply; 12+ messages in thread
From: Frédéric Danis @ 2012-10-09 15:58 UTC (permalink / raw)
  To: linux-bluetooth

Read and write config file in ini-file format.
If the file can not be loaded, try to convert legacy configuration.
---
 src/adapter.c |  189 +++++++++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 157 insertions(+), 32 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index 8582a0e..63c4c84 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -33,6 +33,7 @@
 #include <stdlib.h>
 #include <stdbool.h>
 #include <sys/ioctl.h>
+#include <sys/file.h>
 
 #include <bluetooth/bluetooth.h>
 #include <bluetooth/uuid.h>
@@ -222,6 +223,54 @@ static uint8_t get_mode(const char *mode)
 		return MODE_UNKNOWN;
 }
 
+static void write_config(struct btd_adapter *adapter)
+{
+	GKeyFile *key_file;
+	char filename[PATH_MAX + 1];
+	char address[18];
+	char *str;
+	gsize length = 0;
+
+	key_file = g_key_file_new();
+
+	g_key_file_set_string(key_file, "General", "Name",
+				adapter->config.name);
+
+	str = g_strdup_printf("0x%2.2x%2.2x%2.2x", adapter->config.class[2],
+				adapter->config.class[1],
+				adapter->config.class[0]);
+	g_key_file_set_string(key_file, "General", "Class", str);
+	g_free(str);
+
+	g_key_file_set_boolean(key_file, "General", "Pairable",
+				adapter->pairable);
+
+	if (adapter->pairable_timeout != main_opts.pairto)
+		g_key_file_set_integer(key_file, "General", "PairableTimeout",
+					adapter->pairable_timeout);
+
+	if (adapter->discov_timeout != main_opts.discovto)
+		g_key_file_set_integer(key_file, "General",
+					"DiscoverableTimeout",
+					adapter->discov_timeout);
+
+	g_key_file_set_string(key_file, "General", "Mode",
+				mode2str(adapter->config.mode));
+	g_key_file_set_string(key_file, "General", "OnMode",
+				mode2str(adapter->config.on_mode));
+
+	ba2str(&adapter->bdaddr, address);
+	snprintf(filename, PATH_MAX, STORAGEDIR "/%s/adapter", address);
+
+	create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+
+	str = g_key_file_to_data(key_file, &length, NULL);
+	g_file_set_contents(filename, str, length, NULL);
+	g_free(str);
+
+	g_key_file_free(key_file);
+}
+
 static struct session_req *session_ref(struct session_req *req)
 {
 	req->refcount++;
@@ -291,7 +340,6 @@ static struct session_req *find_session_by_msg(GSList *list, const DBusMessage *
 static int set_mode(struct btd_adapter *adapter, uint8_t new_mode)
 {
 	int err;
-	const char *modestr;
 
 	if (adapter->pending_mode != NULL)
 		return -EALREADY;
@@ -327,10 +375,9 @@ done:
 	if (new_mode != MODE_OFF)
 		adapter->config.on_mode = new_mode;
 
-	modestr = mode2str(new_mode);
-	write_device_mode(&adapter->bdaddr, modestr);
+	write_config(adapter);
 
-	DBG("%s", modestr);
+	DBG("%s", mode2str(new_mode));
 
 	return 0;
 }
@@ -473,7 +520,7 @@ void btd_adapter_pairable_changed(struct btd_adapter *adapter,
 {
 	adapter->pairable = pairable;
 
-	write_device_pairable(&adapter->bdaddr, pairable);
+	write_config(adapter);
 
 	g_dbus_emit_property_changed(btd_get_dbus_connection(), adapter->path,
 					ADAPTER_INTERFACE, "Pairable");
@@ -690,7 +737,7 @@ static void set_discoverable_timeout(struct btd_adapter *adapter,
 
 	adapter->discov_timeout = timeout;
 
-	write_discoverable_timeout(&adapter->bdaddr, timeout);
+	write_config(adapter);
 
 	g_dbus_emit_property_changed(conn, adapter->path, ADAPTER_INTERFACE,
 						"DiscoverableTimeout");
@@ -710,7 +757,7 @@ static void set_pairable_timeout(struct btd_adapter *adapter,
 
 	adapter->pairable_timeout = timeout;
 
-	write_pairable_timeout(&adapter->bdaddr, timeout);
+	write_config(adapter);
 
 	g_dbus_emit_property_changed(conn, adapter->path, ADAPTER_INTERFACE,
 							"PairableTimeout");
@@ -730,7 +777,7 @@ void btd_adapter_class_changed(struct btd_adapter *adapter, uint8_t *new_class)
 	adapter->config.class[1] = new_class[1];
 	adapter->config.class[2] = new_class[2];
 
-	write_local_class(&adapter->bdaddr, new_class);
+	write_config(adapter);
 
 	adapter->dev_class = class;
 
@@ -790,7 +837,7 @@ int adapter_set_name(struct btd_adapter *adapter, const char *name)
 	g_free(adapter->config.name);
 	adapter->config.name = g_strdup(maxname);
 
-	write_local_name(&adapter->bdaddr, maxname);
+	write_config(adapter);
 
 	return 0;
 }
@@ -2475,17 +2522,15 @@ static void set_mode_complete(struct btd_adapter *adapter)
 {
 	DBusConnection *conn = btd_get_dbus_connection();
 	struct session_req *pending;
-	const char *modestr;
 	int err;
 
 	adapter->config.mode = adapter->mode;
 	if (adapter->mode != MODE_OFF)
 		adapter->config.on_mode = adapter->mode;
 
-	modestr = mode2str(adapter->mode);
-	write_device_mode(&adapter->bdaddr, modestr);
+	write_config(adapter);
 
-	DBG("%s", modestr);
+	DBG("%s", mode2str(adapter->mode));
 
 	if (adapter->mode == MODE_OFF) {
 		g_slist_free_full(adapter->mode_sessions, session_free);
@@ -2652,56 +2697,136 @@ void btd_adapter_unref(struct btd_adapter *adapter)
 	g_free(path);
 }
 
-static void load_config(struct btd_adapter *adapter)
+static void convert_config(struct btd_adapter *adapter, const char *filename,
+				GKeyFile *key_file)
 {
-	char name[MAX_NAME_LENGTH + 1];
 	char address[18];
-	char mode[14];
+	char str[MAX_NAME_LENGTH + 1];
+	uint8_t class[3];
+	gboolean flag;
 	int timeout;
+	char *data;
+	gsize length = 0;
 
 	ba2str(&adapter->bdaddr, address);
 
+	if (read_local_name(&adapter->bdaddr, str) == 0)
+		g_key_file_set_string(key_file, "General", "Name", str);
+
+	if (read_local_class(&adapter->bdaddr, class) == 0) {
+		sprintf(str, "0x%2.2x%2.2x%2.2x", class[2], class[1], class[0]);
+		g_key_file_set_string(key_file, "General", "Class", str);
+	}
+
+	if (read_device_pairable(&adapter->bdaddr, &flag) == 0)
+		g_key_file_set_boolean(key_file, "General", "Pairable", flag);
+
+	if (read_pairable_timeout(address, &timeout) == 0)
+		g_key_file_set_integer(key_file, "General",
+						"PairableTimeout", timeout);
+
+	if (read_pairable_timeout(address, &timeout) == 0)
+		g_key_file_set_integer(key_file, "General",
+						"DiscoverableTimeout", timeout);
+
+	if (read_device_mode(address, str, sizeof(str)) == 0)
+		g_key_file_set_string(key_file, "General", "Mode", str);
+
+	if (read_on_mode(address, str, sizeof(str)) == 0)
+		g_key_file_set_string(key_file, "General", "OnMode", str);
+
+	create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+
+	data = g_key_file_to_data(key_file, &length, NULL);
+	g_file_set_contents(filename, data, length, NULL);
+	g_free(data);
+}
+
+static void load_config(struct btd_adapter *adapter)
+{
+	GKeyFile *key_file;
+	char filename[PATH_MAX + 1];
+	char address[18];
+	char *str;
+	GError *gerr = NULL;
+
+	ba2str(&adapter->bdaddr, address);
+
+	key_file = g_key_file_new();
+
+	snprintf(filename, PATH_MAX, STORAGEDIR "/%s/adapter", address);
+
+	if (!g_key_file_load_from_file(key_file, filename, 0, NULL))
+		convert_config(adapter, filename, key_file);
+
 	/* Get name */
-	if (read_local_name(&adapter->bdaddr, name) < 0)
-		adapter->config.name = NULL;
-	else
-		adapter->config.name = g_strdup(name);
+	adapter->config.name = g_key_file_get_string(key_file, "General",
+								"Name", NULL);
 
 	/* Get class */
-	if (read_local_class(&adapter->bdaddr, adapter->config.class) < 0) {
+	str = g_key_file_get_string(key_file, "General", "Class", NULL);
+	if (str) {
+		char tmp[3];
+		int i;
+		uint8_t *class = adapter->config.class;
+
+		memset(tmp, 0, sizeof(tmp));
+		for (i = 0; i < 3; i++) {
+			memcpy(tmp, str + (i * 2) + 2, 2);
+			class[2 - i] = (uint8_t) strtol(tmp, NULL, 16);
+		}
+	} else {
 		uint32_t class = htobl(main_opts.class);
 		memcpy(adapter->config.class, &class, 3);
 	}
+	g_free(str);
 
 	/* Get pairable mode */
-	if (read_device_pairable(&adapter->bdaddr, &adapter->pairable) < 0)
+	adapter->pairable = g_key_file_get_boolean(key_file, "General",
+							"Pairable", &gerr);
+	if (gerr) {
 		adapter->pairable = TRUE;
+		g_error_free(gerr);
+		gerr = NULL;
+	}
 
 	/* Get pairable timeout */
-	if (read_pairable_timeout(address, &timeout) < 0)
+	adapter->pairable_timeout = g_key_file_get_integer(key_file, "General",
+						"PairableTimeout", &gerr);
+	if (gerr) {
 		adapter->pairable_timeout = main_opts.pairto;
-	else
-		adapter->pairable_timeout = timeout;
+		g_error_free(gerr);
+		gerr = NULL;
+	}
 
 	/* Get discoverable timeout */
-	if (read_discoverable_timeout(address, &timeout) < 0)
+	adapter->discov_timeout = g_key_file_get_integer(key_file, "General",
+						"DiscoverableTimeout", &gerr);
+	if (gerr) {
 		adapter->discov_timeout = main_opts.discovto;
-	else
-		adapter->discov_timeout = timeout;
+		g_error_free(gerr);
+		gerr = NULL;
+	}
 
 	/* Get mode */
+	str = g_key_file_get_string(key_file, "General", "Mode", NULL);
 	if (main_opts.remember_powered == FALSE)
 		adapter->config.mode = main_opts.mode;
-	else if (read_device_mode(address, mode, sizeof(mode)) == 0)
-		adapter->config.mode = get_mode(mode);
+	else if (str)
+		adapter->config.mode = get_mode(str);
 	else
 		adapter->config.mode = main_opts.mode;
+	g_free(str);
 
 	/* Get on mode */
-	if (read_on_mode(address, mode, sizeof(mode)) == 0)
-		adapter->config.on_mode = get_mode(mode);
+	str = g_key_file_get_string(key_file, "General", "OnMode", NULL);
+	if (str)
+		adapter->config.on_mode = get_mode(str);
 	else
 		adapter->config.on_mode = MODE_CONNECTABLE;
+	g_free(str);
+
+	g_key_file_free(key_file);
 }
 
 gboolean adapter_init(struct btd_adapter *adapter, gboolean up)
-- 
1.7.9.5


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

* Re: [PATCH 0/7] Move adapter config file to ini-file format
  2012-10-09 15:58 [PATCH 0/7] Move adapter config file to ini-file format Frédéric Danis
                   ` (6 preceding siblings ...)
  2012-10-09 15:58 ` [PATCH 7/7] adapter: Move saved config to ini-file format Frédéric Danis
@ 2012-10-09 16:08 ` Marcel Holtmann
  2012-10-09 16:20   ` Frederic Danis
  7 siblings, 1 reply; 12+ messages in thread
From: Marcel Holtmann @ 2012-10-09 16:08 UTC (permalink / raw)
  To: Frédéric Danis; +Cc: linux-bluetooth

Hi Fred,

> Adapter saved configuration will be saved to /var/lib/bluetooth/<adapter address>/adapter
> in ini-file format for BlueZ 5.
> If this file does not exist, we try to convert old config file to it.

since we are already using /config as file name, I propose we convert
this to /settings. I do not like the file name /adapter.

We need to make sure that once the new file exists, that we do not keep
converting over and over again.

I would also propose to add an extra entry to the old file that marks it
as converted. Then we can at some point in the future, start cleaning
out the leftovers.

Regards

Marcel



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

* Re: [PATCH 0/7] Move adapter config file to ini-file format
  2012-10-09 16:08 ` [PATCH 0/7] Move adapter config file " Marcel Holtmann
@ 2012-10-09 16:20   ` Frederic Danis
  2012-10-09 16:21     ` Marcel Holtmann
  0 siblings, 1 reply; 12+ messages in thread
From: Frederic Danis @ 2012-10-09 16:20 UTC (permalink / raw)
  To: Marcel Holtmann; +Cc: linux-bluetooth

Hello Marcel,

On 09/10/2012 18:08, Marcel Holtmann wrote:
> Hi Fred,
>
>> Adapter saved configuration will be saved to /var/lib/bluetooth/<adapter address>/adapter
>> in ini-file format for BlueZ 5.
>> If this file does not exist, we try to convert old config file to it.
>
> since we are already using /config as file name, I propose we convert
> this to /settings. I do not like the file name /adapter.

OK, I will change it.

> We need to make sure that once the new file exists, that we do not keep
> converting over and over again.

/config file is converted only if loading of new /settings file failed.

> I would also propose to add an extra entry to the old file that marks it
> as converted. Then we can at some point in the future, start cleaning
> out the leftovers.

OK, I will add "converted yes" at end of /config file after conversion.

Btw, should not we remove /config file after conversion ?

Regards

Fred

-- 
Frederic Danis                            Open Source Technology Center
frederic.danis@intel.com                              Intel Corporation


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

* Re: [PATCH 0/7] Move adapter config file to ini-file format
  2012-10-09 16:20   ` Frederic Danis
@ 2012-10-09 16:21     ` Marcel Holtmann
  0 siblings, 0 replies; 12+ messages in thread
From: Marcel Holtmann @ 2012-10-09 16:21 UTC (permalink / raw)
  To: Frederic Danis; +Cc: linux-bluetooth

Hi Fred,

> >> Adapter saved configuration will be saved to /var/lib/bluetooth/<adapter address>/adapter
> >> in ini-file format for BlueZ 5.
> >> If this file does not exist, we try to convert old config file to it.
> >
> > since we are already using /config as file name, I propose we convert
> > this to /settings. I do not like the file name /adapter.
> 
> OK, I will change it.
> 
> > We need to make sure that once the new file exists, that we do not keep
> > converting over and over again.
> 
> /config file is converted only if loading of new /settings file failed.

good. And then check if it got marked as converted yes and then just do
not convert twice.

> > I would also propose to add an extra entry to the old file that marks it
> > as converted. Then we can at some point in the future, start cleaning
> > out the leftovers.
> 
> OK, I will add "converted yes" at end of /config file after conversion.
> 
> Btw, should not we remove /config file after conversion ?

No. In case you go for and back between 4.x and 5.x we need to keep this
around for a bit. We should put a notice in the TODO file for some 5.x
version in lets say 6-8 month to start removing leftover config files.

Regards

Marcel



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

* Re: [PATCH 7/7] adapter: Move saved config to ini-file format
  2012-10-09 15:58 ` [PATCH 7/7] adapter: Move saved config to ini-file format Frédéric Danis
@ 2012-10-09 17:04   ` Anderson Lizardo
  0 siblings, 0 replies; 12+ messages in thread
From: Anderson Lizardo @ 2012-10-09 17:04 UTC (permalink / raw)
  To: Frédéric Danis; +Cc: linux-bluetooth

Hi Frédéric,

On Tue, Oct 9, 2012 at 11:58 AM, Frédéric Danis
<frederic.danis@linux.intel.com> wrote:
> +       if (read_pairable_timeout(address, &timeout) == 0)
> +               g_key_file_set_integer(key_file, "General",
> +                                               "PairableTimeout", timeout);
> +
> +       if (read_pairable_timeout(address, &timeout) == 0)
> +               g_key_file_set_integer(key_file, "General",
> +                                               "DiscoverableTimeout", timeout);
> +

This second if() should use read_discoverable_timeout().

Regards,
-- 
Anderson Lizardo
Instituto Nokia de Tecnologia - INdT
Manaus - Brazil

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

end of thread, other threads:[~2012-10-09 17:04 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-09 15:58 [PATCH 0/7] Move adapter config file to ini-file format Frédéric Danis
2012-10-09 15:58 ` [PATCH 1/7] adapter: Read name in storage at init Frédéric Danis
2012-10-09 15:58 ` [PATCH 2/7] adapter: Read device class " Frédéric Danis
2012-10-09 15:58 ` [PATCH 3/7] adapter: Move pairable read to load_config() Frédéric Danis
2012-10-09 15:58 ` [PATCH 4/7] adapter: Read pairable timeout in storage at init Frédéric Danis
2012-10-09 15:58 ` [PATCH 5/7] adapter: Read discoverable " Frédéric Danis
2012-10-09 15:58 ` [PATCH 6/7] adapter: Read mode " Frédéric Danis
2012-10-09 15:58 ` [PATCH 7/7] adapter: Move saved config to ini-file format Frédéric Danis
2012-10-09 17:04   ` Anderson Lizardo
2012-10-09 16:08 ` [PATCH 0/7] Move adapter config file " Marcel Holtmann
2012-10-09 16:20   ` Frederic Danis
2012-10-09 16:21     ` Marcel Holtmann

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