From: "Frédéric Danis" <frederic.danis@linux.intel.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH v8 2/5] device: Retrieve name from storage
Date: Fri, 26 Oct 2012 11:25:02 +0200 [thread overview]
Message-ID: <1351243505-20702-2-git-send-email-frederic.danis@linux.intel.com> (raw)
In-Reply-To: <1351243505-20702-1-git-send-email-frederic.danis@linux.intel.com>
Try to retrieve name from device info file.
If that fails fall back to the cache and save it to device info file.
When device name is updated, save it.
---
src/device.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 98 insertions(+), 1 deletion(-)
diff --git a/src/device.c b/src/device.c
index bc7f8dd..d27fd31 100644
--- a/src/device.c
+++ b/src/device.c
@@ -76,6 +76,9 @@
#define DISCONNECT_TIMER 2
#define DISCOVERY_TIMER 2
+#define INFO_PATH STORAGEDIR "/%s/%s/info"
+#define CACHE_PATH STORAGEDIR "/%s/cache/%s"
+
struct btd_disconnect_data {
guint id;
disconnect_watch watch;
@@ -202,6 +205,33 @@ static uint16_t uuid_list[] = {
0
};
+static void store_device_info(struct btd_device *device)
+{
+ GKeyFile *key_file;
+ char filename[PATH_MAX + 1];
+ char adapter_addr[18];
+ char device_addr[18];
+ char *str;
+ gsize length = 0;
+
+ key_file = g_key_file_new();
+
+ g_key_file_set_string(key_file, "General", "Name", device->name);
+
+ ba2str(adapter_get_address(device->adapter), adapter_addr);
+ ba2str(&device->bdaddr, device_addr);
+ snprintf(filename, PATH_MAX, INFO_PATH, adapter_addr, device_addr);
+ filename[PATH_MAX] = '\0';
+
+ 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 void browse_request_free(struct browse_req *req)
{
if (req->listener_id)
@@ -1564,6 +1594,70 @@ static void device_set_version(struct btd_device *device, uint16_t value)
DEVICE_INTERFACE, "Version");
}
+static char *load_cached_name(struct btd_device *device, const char *local,
+ const gchar *peer)
+{
+ char filename[PATH_MAX + 1];
+ GKeyFile *key_file;
+ char *str = NULL;
+ int len;
+
+ snprintf(filename, PATH_MAX, CACHE_PATH, local, peer);
+ filename[PATH_MAX] = '\0';
+
+ key_file = g_key_file_new();
+
+ if (!g_key_file_load_from_file(key_file, filename, 0, NULL))
+ goto failed;
+
+ str = g_key_file_get_string(key_file, "General", "Name", NULL);
+ if (str) {
+ len = strlen(str);
+ if (len > HCI_MAX_NAME_LENGTH)
+ str[HCI_MAX_NAME_LENGTH] = '\0';
+ }
+
+failed:
+ g_key_file_free(key_file);
+
+ return str;
+}
+
+static void load_info(struct btd_device *device, const gchar *local,
+ const gchar *peer)
+{
+ char filename[PATH_MAX + 1];
+ GKeyFile *key_file;
+ char *str;
+ gboolean store_needed = FALSE;
+
+ snprintf(filename, PATH_MAX, INFO_PATH, local, peer);
+ filename[PATH_MAX] = '\0';
+
+ key_file = g_key_file_new();
+ g_key_file_load_from_file(key_file, filename, 0, NULL);
+
+ /* Load device name from storage info file, if that fails fall back to
+ * the cache.
+ */
+ str = g_key_file_get_string(key_file, "General", "Name", NULL);
+ if (str == NULL) {
+ str = load_cached_name(device, local, peer);
+ if (str)
+ store_needed = TRUE;
+ }
+
+ if (str) {
+ strcpy(device->name, str);
+ g_free(str);
+ }
+
+ if (store_needed)
+ store_device_info(device);
+
+ g_key_file_free(key_file);
+}
+
struct btd_device *device_create(struct btd_adapter *adapter,
const gchar *address, uint8_t bdaddr_type)
{
@@ -1600,7 +1694,8 @@ struct btd_device *device_create(struct btd_adapter *adapter,
src = adapter_get_address(adapter);
ba2str(src, srcaddr);
- read_device_name(srcaddr, address, bdaddr_type, device->name);
+ load_info(device, srcaddr, address);
+
if (read_device_alias(srcaddr, address, bdaddr_type, alias,
sizeof(alias)) == 0)
device->alias = g_strdup(alias);
@@ -1640,6 +1735,8 @@ void device_set_name(struct btd_device *device, const char *name)
strncpy(device->name, name, MAX_NAME_LENGTH);
+ store_device_info(device);
+
g_dbus_emit_property_changed(btd_get_dbus_connection(), device->path,
DEVICE_INTERFACE, "Name");
--
1.7.9.5
next prev parent reply other threads:[~2012-10-26 9:25 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-10-26 9:25 [PATCH v8 1/5] doc: Update settings-storage.txt Frédéric Danis
2012-10-26 9:25 ` Frédéric Danis [this message]
2012-10-26 9:25 ` [PATCH v8 3/5] dbusoob: Set device name in device object Frédéric Danis
2012-10-26 9:25 ` [PATCH v8 4/5] input: Retrieve device name from " Frédéric Danis
2012-10-26 9:25 ` [PATCH v8 5/5] hcitool: Retrieve names from cache directory Frédéric Danis
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=1351243505-20702-2-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 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.