*
* Connection Manager
*
- * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
+ * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
#include <config.h>
#endif
+#include <unistd.h>
+
#include "connman.h"
+#define PROFILE_SUFFIX "profile"
+#define CONFIG_SUFFIX "config"
+
static GSList *storage_list = NULL;
static gint compare_priority(gconstpointer a, gconstpointer b)
storage_list = g_slist_remove(storage_list, storage);
}
-int __connman_storage_load_device(struct connman_device *device)
+GKeyFile *__connman_storage_open(const char *ident, const char *suffix)
{
- GSList *list;
-
- DBG("device %p", device);
+ GKeyFile *keyfile;
+ gchar *pathname, *data = NULL;
+ gboolean result;
+ gsize length;
- for (list = storage_list; list; list = list->next) {
- struct connman_storage *storage = list->data;
+ DBG("ident %s suffix %s", ident, suffix);
- if (storage->device_load) {
- DBG("%s", storage->name);
+ pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
+ if (pathname == NULL)
+ return NULL;
- if (storage->device_load(device) == 0)
- return 0;
- }
- }
+ result = g_file_get_contents(pathname, &data, &length, NULL);
- return -ENOENT;
-}
+ g_free(pathname);
-int __connman_storage_save_device(struct connman_device *device)
-{
- GSList *list;
+ keyfile = g_key_file_new();
- DBG("device %p", device);
+ if (result == FALSE)
+ goto done;
- for (list = storage_list; list; list = list->next) {
- struct connman_storage *storage = list->data;
+ if (length > 0)
+ g_key_file_load_from_data(keyfile, data, length, 0, NULL);
- if (storage->device_save) {
- DBG("%s", storage->name);
+ g_free(data);
- if (storage->device_save(device) == 0)
- return 0;
- }
- }
+done:
+ DBG("keyfile %p", keyfile);
- return -ENOENT;
+ return keyfile;
}
-int __connman_storage_load_network(struct connman_network *network)
+void __connman_storage_close(const char *ident, const char *suffix,
+ GKeyFile *keyfile, gboolean save)
{
- GSList *list;
+ gchar *pathname, *data = NULL;
+ gsize length = 0;
- DBG("network %p", network);
+ DBG("ident %s suffix %s keyfile %p save %d",
+ ident, suffix, keyfile, save);
- for (list = storage_list; list; list = list->next) {
- struct connman_storage *storage = list->data;
+ if (save == FALSE) {
+ g_key_file_free(keyfile);
+ return;
+ }
- if (storage->network_load) {
- DBG("%s", storage->name);
+ pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
+ if (pathname == NULL)
+ return;
- if (storage->network_load(network) == 0)
- return 0;
- }
- }
+ data = g_key_file_to_data(keyfile, &length, NULL);
- return -ENOENT;
+ if (g_file_set_contents(pathname, data, length, NULL) == FALSE)
+ connman_error("Failed to store information");
+
+ g_free(data);
+
+ g_free(pathname);
+
+ g_key_file_free(keyfile);
}
-int __connman_storage_save_network(struct connman_network *network)
+void __connman_storage_delete(const char *ident, const char *suffix)
{
- GSList *list;
+ gchar *pathname;
- DBG("network %p", network);
-
- for (list = storage_list; list; list = list->next) {
- struct connman_storage *storage = list->data;
+ DBG("ident %s suffix %s", ident, suffix);
- if (storage->network_save) {
- DBG("%s", storage->name);
+ pathname = g_strdup_printf("%s/%s.%s", STORAGEDIR, ident, suffix);
+ if (pathname == NULL)
+ return;
- if (storage->network_save(network) == 0)
- return 0;
- }
- }
+ if (unlink(pathname) < 0)
+ connman_error("Failed to remove %s", pathname);
+}
- return -ENOENT;
+GKeyFile *__connman_storage_open_profile(const char *ident)
+{
+ return __connman_storage_open(ident, PROFILE_SUFFIX);
}
-int __connman_storage_init(void)
+void __connman_storage_close_profile(const char *ident,
+ GKeyFile *keyfile, gboolean save)
{
- DBG("");
+ __connman_storage_close(ident, PROFILE_SUFFIX, keyfile, save);
+}
- return 0;
+void __connman_storage_delete_profile(const char *ident)
+{
+ __connman_storage_delete(ident, PROFILE_SUFFIX);
}
-void __connman_storage_cleanup(void)
+GKeyFile *__connman_storage_open_config(const char *ident)
{
- DBG("");
+ return __connman_storage_open(ident, CONFIG_SUFFIX);
}
-static int do_load(GKeyFile *keyfile, struct connman_element *element)
+void __connman_storage_close_config(const char *ident,
+ GKeyFile *keyfile, gboolean save)
{
- const gchar *value;
+ __connman_storage_close(ident, CONFIG_SUFFIX, keyfile, save);
+}
- DBG("element %p name %s", element, element->name);
+void __connman_storage_delete_config(const char *ident)
+{
+ __connman_storage_delete(ident, CONFIG_SUFFIX);
+}
- value = g_key_file_get_string(keyfile, element->path,
- "Policy", NULL);
- if (value != NULL)
- element->policy = __connman_element_string2policy(value);
+int __connman_storage_init_profile(void)
+{
+ GSList *list;
- if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK)
- element->remember = g_key_file_get_boolean(keyfile,
- element->path, "Remember", NULL);
+ DBG("");
- value = g_key_file_get_string(keyfile, element->path,
- "WiFi.Security", NULL);
- if (value != NULL)
- connman_element_set_property(element,
- CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value);
+ for (list = storage_list; list; list = list->next) {
+ struct connman_storage *storage = list->data;
- value = g_key_file_get_string(keyfile, element->path,
- "WiFi.Passphrase", NULL);
- if (value != NULL)
- connman_element_set_property(element,
- CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value);
+ if (storage->profile_init) {
+ if (storage->profile_init() == 0)
+ return 0;
+ }
+ }
- return 0;
+ return -ENOENT;
}
-int __connman_element_load(struct connman_element *element)
+int __connman_storage_load_profile(struct connman_profile *profile)
{
- GKeyFile *keyfile;
- gchar *pathname, *data = NULL;
- gsize length;
-
- DBG("element %p name %s", element, element->name);
+ GSList *list;
- pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
- if (pathname == NULL)
- return -ENOMEM;
+ DBG("profile %p", profile);
- keyfile = g_key_file_new();
+ for (list = storage_list; list; list = list->next) {
+ struct connman_storage *storage = list->data;
- if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
- g_free(pathname);
- return -ENOENT;
+ if (storage->profile_load) {
+ if (storage->profile_load(profile) == 0)
+ return 0;
+ }
}
- g_free(pathname);
+ return -ENOENT;
+}
- if (g_key_file_load_from_data(keyfile, data, length,
- 0, NULL) == FALSE) {
- g_free(data);
- return -EILSEQ;
- }
+int __connman_storage_save_profile(struct connman_profile *profile)
+{
+ GSList *list;
- g_free(data);
+ DBG("profile %p", profile);
- do_load(keyfile, element);
+ for (list = storage_list; list; list = list->next) {
+ struct connman_storage *storage = list->data;
- g_key_file_free(keyfile);
+ if (storage->profile_save) {
+ if (storage->profile_save(profile) == 0)
+ return 0;
+ }
+ }
- return 0;
+ return -ENOENT;
}
-static void do_update(GKeyFile *keyfile, struct connman_element *element)
+int __connman_storage_load_service(struct connman_service *service)
{
GSList *list;
- char *value;
- const char *str;
- DBG("element %p name %s", element, element->name);
+ DBG("service %p", service);
- g_key_file_set_string(keyfile, element->path, "Name", element->name);
-
- str = __connman_element_policy2string(element->policy);
- if (str != NULL)
- g_key_file_set_string(keyfile, element->path, "Policy", str);
-
- //g_key_file_set_boolean(keyfile, element->path, "Enabled",
- // element->enabled);
+ for (list = storage_list; list; list = list->next) {
+ struct connman_storage *storage = list->data;
- if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK)
- g_key_file_set_boolean(keyfile, element->path, "Remember",
- element->remember);
+ if (storage->service_load) {
+ if (storage->service_load(service) == 0)
+ return 0;
+ }
+ }
- __connman_element_lock(element);
+ return -ENOENT;
+}
- for (list = element->properties; list; list = list->next) {
- struct connman_property *property = list->data;
+int __connman_storage_save_service(struct connman_service *service)
+{
+ GSList *list;
- if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
- continue;
+ DBG("service %p", service);
- if (property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)
- continue;
+ for (list = storage_list; list; list = list->next) {
+ struct connman_storage *storage = list->data;
- if (property->type == DBUS_TYPE_STRING)
- g_key_file_set_string(keyfile, element->path,
- property->name, property->value);
+ if (storage->service_save) {
+ if (storage->service_save(service) == 0)
+ return 0;
+ }
}
- __connman_element_unlock(element);
-
- if (connman_element_get_value(element,
- CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value) == 0)
- g_key_file_set_string(keyfile, element->path,
- "WiFi.Security", value);
-
- if (connman_element_get_value(element,
- CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value) == 0)
- g_key_file_set_string(keyfile, element->path,
- "WiFi.Passphrase", value);
+ return -ENOENT;
}
-int __connman_element_store(struct connman_element *element)
+int __connman_storage_load_device(struct connman_device *device)
{
- GKeyFile *keyfile;
- gchar *pathname, *data = NULL;
- gsize length;
-
- DBG("element %p name %s", element, element->name);
-
- if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE &&
- element->type != CONNMAN_ELEMENT_TYPE_NETWORK)
- return -EINVAL;
-
- if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_FAKE)
- return -EINVAL;
-
- pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
- if (pathname == NULL)
- return -ENOMEM;
+ GSList *list;
- keyfile = g_key_file_new();
+ DBG("device %p", device);
- if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
- goto update;
+ for (list = storage_list; list; list = list->next) {
+ struct connman_storage *storage = list->data;
- if (length > 0) {
- if (g_key_file_load_from_data(keyfile, data, length,
- 0, NULL) == FALSE)
- goto done;
+ if (storage->device_load) {
+ if (storage->device_load(device) == 0)
+ return 0;
+ }
}
- g_free(data);
+ return -ENOENT;
+}
-update:
- do_update(keyfile, element);
+int __connman_storage_save_device(struct connman_device *device)
+{
+ GSList *list;
- data = g_key_file_to_data(keyfile, &length, NULL);
+ DBG("device %p", device);
- g_file_set_contents(pathname, data, length, NULL);
+ for (list = storage_list; list; list = list->next) {
+ struct connman_storage *storage = list->data;
-done:
- g_free(data);
+ if (storage->device_save) {
+ if (storage->device_save(device) == 0)
+ return 0;
+ }
+ }
- g_key_file_free(keyfile);
+ return -ENOENT;
+}
- g_free(pathname);
+int __connman_storage_init(void)
+{
+ DBG("");
return 0;
}
+
+void __connman_storage_cleanup(void)
+{
+ DBG("");
+}