5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 static GSList *storage_list = NULL;
30 static gint compare_priority(gconstpointer a, gconstpointer b)
32 const struct connman_storage *storage1 = a;
33 const struct connman_storage *storage2 = b;
35 return storage2->priority - storage1->priority;
39 * connman_storage_register:
40 * @storage: storage module
42 * Register a new storage module
44 * Returns: %0 on success
46 int connman_storage_register(struct connman_storage *storage)
48 DBG("storage %p name %s", storage, storage->name);
50 storage_list = g_slist_insert_sorted(storage_list, storage,
57 * connman_storage_unregister:
58 * @storage: storage module
60 * Remove a previously registered storage module
62 void connman_storage_unregister(struct connman_storage *storage)
64 DBG("storage %p name %s", storage, storage->name);
66 storage_list = g_slist_remove(storage_list, storage);
69 int __connman_storage_load_device(struct connman_device *device)
73 DBG("device %p", device);
75 for (list = storage_list; list; list = list->next) {
76 struct connman_storage *storage = list->data;
78 if (storage->device_load) {
79 DBG("%s", storage->name);
81 if (storage->device_load(device) == 0)
89 int __connman_storage_save_device(struct connman_device *device)
93 DBG("device %p", device);
95 for (list = storage_list; list; list = list->next) {
96 struct connman_storage *storage = list->data;
98 if (storage->device_save) {
99 DBG("%s", storage->name);
101 if (storage->device_save(device) == 0)
109 int __connman_storage_load_network(struct connman_network *network)
113 DBG("network %p", network);
115 for (list = storage_list; list; list = list->next) {
116 struct connman_storage *storage = list->data;
118 if (storage->network_load) {
119 DBG("%s", storage->name);
121 if (storage->network_load(network) == 0)
129 int __connman_storage_save_network(struct connman_network *network)
133 DBG("network %p", network);
135 for (list = storage_list; list; list = list->next) {
136 struct connman_storage *storage = list->data;
138 if (storage->network_save) {
139 DBG("%s", storage->name);
141 if (storage->network_save(network) == 0)
149 int __connman_storage_init(void)
156 void __connman_storage_cleanup(void)
161 static int do_load(GKeyFile *keyfile, struct connman_element *element)
165 DBG("element %p name %s", element, element->name);
167 value = g_key_file_get_string(keyfile, element->path,
170 element->policy = __connman_element_string2policy(value);
172 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK)
173 element->remember = g_key_file_get_boolean(keyfile,
174 element->path, "Remember", NULL);
176 value = g_key_file_get_string(keyfile, element->path,
177 "WiFi.Security", NULL);
179 connman_element_set_property(element,
180 CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value);
182 value = g_key_file_get_string(keyfile, element->path,
183 "WiFi.Passphrase", NULL);
185 connman_element_set_property(element,
186 CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value);
191 int __connman_element_load(struct connman_element *element)
194 gchar *pathname, *data = NULL;
197 DBG("element %p name %s", element, element->name);
199 pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
200 if (pathname == NULL)
203 keyfile = g_key_file_new();
205 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
212 if (g_key_file_load_from_data(keyfile, data, length,
220 do_load(keyfile, element);
222 g_key_file_free(keyfile);
227 static void do_update(GKeyFile *keyfile, struct connman_element *element)
233 DBG("element %p name %s", element, element->name);
235 g_key_file_set_string(keyfile, element->path, "Name", element->name);
237 str = __connman_element_policy2string(element->policy);
239 g_key_file_set_string(keyfile, element->path, "Policy", str);
241 //g_key_file_set_boolean(keyfile, element->path, "Enabled",
242 // element->enabled);
244 if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK)
245 g_key_file_set_boolean(keyfile, element->path, "Remember",
248 __connman_element_lock(element);
250 for (list = element->properties; list; list = list->next) {
251 struct connman_property *property = list->data;
253 if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
256 if (property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)
259 if (property->type == DBUS_TYPE_STRING)
260 g_key_file_set_string(keyfile, element->path,
261 property->name, property->value);
264 __connman_element_unlock(element);
266 if (connman_element_get_value(element,
267 CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value) == 0)
268 g_key_file_set_string(keyfile, element->path,
269 "WiFi.Security", value);
271 if (connman_element_get_value(element,
272 CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value) == 0)
273 g_key_file_set_string(keyfile, element->path,
274 "WiFi.Passphrase", value);
277 int __connman_element_store(struct connman_element *element)
280 gchar *pathname, *data = NULL;
283 DBG("element %p name %s", element, element->name);
285 if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE &&
286 element->type != CONNMAN_ELEMENT_TYPE_NETWORK)
289 if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_FAKE)
292 pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
293 if (pathname == NULL)
296 keyfile = g_key_file_new();
298 if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
302 if (g_key_file_load_from_data(keyfile, data, length,
310 do_update(keyfile, element);
312 data = g_key_file_to_data(keyfile, &length, NULL);
314 g_file_set_contents(pathname, data, length, NULL);
319 g_key_file_free(keyfile);