linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Frédéric Danis" <frederic.danis@linux.intel.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH 09/13] proximity: Use new storage architecture
Date: Mon, 17 Dec 2012 16:09:50 +0100	[thread overview]
Message-ID: <1355756994-18953-9-git-send-email-frederic.danis@linux.intel.com> (raw)
In-Reply-To: <1355756994-18953-1-git-send-email-frederic.danis@linux.intel.com>

---
 profiles/proximity/monitor.c |   72 ++++++++++++++++++++----------------------
 1 file changed, 34 insertions(+), 38 deletions(-)

diff --git a/profiles/proximity/monitor.c b/profiles/proximity/monitor.c
index d9ed3ff..0be0ae4 100644
--- a/profiles/proximity/monitor.c
+++ b/profiles/proximity/monitor.c
@@ -83,51 +83,52 @@ struct monitor {
 	guint attioid;
 };
 
-static inline int create_filename(char *buf, size_t size,
-				const bdaddr_t *bdaddr, const char *name)
+static void write_proximity_config(struct btd_device *device, const char *alert,
+					const char *level)
 {
-	char addr[18];
+	char *filename;
+	GKeyFile *key_file;
+	char *data;
+	gsize length = 0;
 
-	ba2str(bdaddr, addr);
+	filename = btd_device_get_storage_path(device, "proximity");
 
-	return create_name(buf, size, STORAGEDIR, addr, name);
-}
-
-static int write_proximity_config(const bdaddr_t *sba, const bdaddr_t *dba,
-					const char *alert, const char *level)
-{
-	char filename[PATH_MAX + 1], addr[18], key[38];
-
-	create_filename(filename, PATH_MAX, sba, "proximity");
+	key_file = g_key_file_new();
+	g_key_file_load_from_file(key_file, filename, 0, NULL);
 
-	create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+	if (level)
+		g_key_file_set_string(key_file, alert, "Level", level);
+	else
+		g_key_file_remove_group(key_file, alert, NULL);
 
-	ba2str(dba, addr);
-
-	snprintf(key, sizeof(key), "%17s#%s", addr, alert);
+	data = g_key_file_to_data(key_file, &length, NULL);
+	if (length > 0) {
+		create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+		g_file_set_contents(filename, data, length, NULL);
+	}
 
-	return textfile_put(filename, key, level);
+	g_free(data);
+	g_free(filename);
+	g_key_file_free(key_file);
 }
 
-static char *read_proximity_config(const bdaddr_t *sba, const bdaddr_t *dba,
-							const char *alert)
+static char *read_proximity_config(struct btd_device *device, const char *alert)
 {
-	char filename[PATH_MAX + 1], addr[18], key[38];
-	char *str, *strnew;
+	char *filename;
+	GKeyFile *key_file;
+	char *str;
 
-	create_filename(filename, PATH_MAX, sba, "proximity");
+	filename = btd_device_get_storage_path(device, "proximity");
 
-	ba2str(dba, addr);
-	snprintf(key, sizeof(key), "%17s#%s", addr, alert);
+	key_file = g_key_file_new();
+	g_key_file_load_from_file(key_file, filename, 0, NULL);
 
-	str = textfile_caseget(filename, key);
-	if (str == NULL)
-		return NULL;
+	str = g_key_file_get_string(key_file, alert, "Level", NULL);
 
-	strnew = g_strdup(str);
-	free(str);
+	g_free(filename);
+	g_key_file_free(key_file);
 
-	return strnew;
+	return str;
 }
 
 static uint8_t str2level(const char *level)
@@ -417,7 +418,6 @@ static void property_set_link_loss_level(const GDBusPropertyTable *property,
 		DBusMessageIter *iter, GDBusPendingPropertySet id, void *data)
 {
 	struct monitor *monitor = data;
-	struct btd_device *device = monitor->device;
 	const char *level;
 
 	if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_STRING)
@@ -438,9 +438,7 @@ static void property_set_link_loss_level(const GDBusPropertyTable *property,
 	g_free(monitor->linklosslevel);
 	monitor->linklosslevel = g_strdup(level);
 
-	write_proximity_config(adapter_get_address(device_get_adapter(device)),
-					device_get_address(device),
-					"LinkLossAlertLevel", level);
+	write_proximity_config(monitor->device, "LinkLossAlertLevel", level);
 
 	if (monitor->attrib)
 		write_alert_level(monitor);
@@ -601,9 +599,7 @@ int monitor_register(struct btd_device *device,
 	struct monitor *monitor;
 	char *level;
 
-	level = read_proximity_config(
-			adapter_get_address(device_get_adapter(device)),
-			device_get_address(device), "LinkLossAlertLevel");
+	level = read_proximity_config(device, "LinkLossAlertLevel");
 
 	monitor = g_new0(struct monitor, 1);
 	monitor->device = btd_device_ref(device);
-- 
1.7.9.5


  parent reply	other threads:[~2012-12-17 15:09 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-17 15:09 [PATCH 01/13] adapter: Add btd_adapter_for_each_device() Frédéric Danis
2012-12-17 15:09 ` [PATCH 02/13] alert: Use ccc file from device storage Frédéric Danis
2012-12-17 15:09 ` [PATCH 03/13] doc: Add device appearance in settings-storage doc Frédéric Danis
2012-12-17 15:09 ` [PATCH 04/13] adapter: Convert appearances file Frédéric Danis
2012-12-17 15:09 ` [PATCH 05/13] device: Load appearance from storage Frédéric Danis
2012-12-17 15:09 ` [PATCH 06/13] adapter: Convert gatt file Frédéric Danis
2012-12-17 15:09 ` [PATCH 07/13] gatt: Use new storage architecture Frédéric Danis
2012-12-17 15:09 ` [PATCH 08/13] adapter: Convert proximity file Frédéric Danis
2012-12-17 15:09 ` Frédéric Danis [this message]
2012-12-17 15:09 ` [PATCH 10/13] adapter: Remove support of pincodes storage file Frédéric Danis
2012-12-17 15:09 ` [PATCH 11/13] adapter: Fix invalid read in conversions Frédéric Danis
2012-12-17 15:09 ` [PATCH 12/13] bluetoothd: Remove storage info from man page Frédéric Danis
2012-12-17 15:09 ` [PATCH 13/13] TODO: Mark convert storage to ini-file item as done Frédéric Danis
2012-12-17 15:54 ` [PATCH 01/13] adapter: Add btd_adapter_for_each_device() Johan Hedberg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1355756994-18953-9-git-send-email-frederic.danis@linux.intel.com \
    --to=frederic.danis@linux.intel.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).