Remove useless driver debugging details
[platform/upstream/connman.git] / src / storage.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "connman.h"
27
28 static GSList *storage_list = NULL;
29
30 static gint compare_priority(gconstpointer a, gconstpointer b)
31 {
32         const struct connman_storage *storage1 = a;
33         const struct connman_storage *storage2 = b;
34
35         return storage2->priority - storage1->priority;
36 }
37
38 /**
39  * connman_storage_register:
40  * @storage: storage module
41  *
42  * Register a new storage module
43  *
44  * Returns: %0 on success
45  */
46 int connman_storage_register(struct connman_storage *storage)
47 {
48         DBG("storage %p name %s", storage, storage->name);
49
50         storage_list = g_slist_insert_sorted(storage_list, storage,
51                                                         compare_priority);
52
53         return 0;
54 }
55
56 /**
57  * connman_storage_unregister:
58  * @storage: storage module
59  *
60  * Remove a previously registered storage module
61  */
62 void connman_storage_unregister(struct connman_storage *storage)
63 {
64         DBG("storage %p name %s", storage, storage->name);
65
66         storage_list = g_slist_remove(storage_list, storage);
67 }
68
69 int __connman_storage_load_device(struct connman_device *device)
70 {
71         GSList *list;
72
73         DBG("device %p", device);
74
75         for (list = storage_list; list; list = list->next) {
76                 struct connman_storage *storage = list->data;
77
78                 if (storage->device_load) {
79                         if (storage->device_load(device) == 0)
80                                 return 0;
81                 }
82         }
83
84         return -ENOENT;
85 }
86
87 int __connman_storage_save_device(struct connman_device *device)
88 {
89         GSList *list;
90
91         DBG("device %p", device);
92
93         for (list = storage_list; list; list = list->next) {
94                 struct connman_storage *storage = list->data;
95
96                 if (storage->device_save) {
97                         if (storage->device_save(device) == 0)
98                                 return 0;
99                 }
100         }
101
102         return -ENOENT;
103 }
104
105 int __connman_storage_load_network(struct connman_network *network)
106 {
107         GSList *list;
108
109         DBG("network %p", network);
110
111         for (list = storage_list; list; list = list->next) {
112                 struct connman_storage *storage = list->data;
113
114                 if (storage->network_load) {
115                         if (storage->network_load(network) == 0)
116                                 return 0;
117                 }
118         }
119
120         return -ENOENT;
121 }
122
123 int __connman_storage_save_network(struct connman_network *network)
124 {
125         GSList *list;
126
127         DBG("network %p", network);
128
129         for (list = storage_list; list; list = list->next) {
130                 struct connman_storage *storage = list->data;
131
132                 if (storage->network_save) {
133                         if (storage->network_save(network) == 0)
134                                 return 0;
135                 }
136         }
137
138         return -ENOENT;
139 }
140
141 int __connman_storage_init(void)
142 {
143         DBG("");
144
145         return 0;
146 }
147
148 void __connman_storage_cleanup(void)
149 {
150         DBG("");
151 }
152
153 static int do_load(GKeyFile *keyfile, struct connman_element *element)
154 {
155         const gchar *value;
156
157         DBG("element %p name %s", element, element->name);
158
159         value = g_key_file_get_string(keyfile, element->path,
160                                                 "Policy", NULL);
161         if (value != NULL)
162                 element->policy = __connman_element_string2policy(value);
163
164         if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK)
165                 element->remember = g_key_file_get_boolean(keyfile,
166                                         element->path, "Remember", NULL);
167
168         value = g_key_file_get_string(keyfile, element->path,
169                                                 "WiFi.Security", NULL);
170         if (value != NULL)
171                 connman_element_set_property(element,
172                                 CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value);
173
174         value = g_key_file_get_string(keyfile, element->path,
175                                                 "WiFi.Passphrase", NULL);
176         if (value != NULL)
177                 connman_element_set_property(element,
178                                 CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value);
179
180         return 0;
181 }
182
183 int __connman_element_load(struct connman_element *element)
184 {
185         GKeyFile *keyfile;
186         gchar *pathname, *data = NULL;
187         gsize length;
188
189         DBG("element %p name %s", element, element->name);
190
191         pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
192         if (pathname == NULL)
193                 return -ENOMEM;
194
195         keyfile = g_key_file_new();
196
197         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
198                 g_free(pathname);
199                 return -ENOENT;
200         }
201
202         g_free(pathname);
203
204         if (g_key_file_load_from_data(keyfile, data, length,
205                                                         0, NULL) == FALSE) {
206                 g_free(data);
207                 return -EILSEQ;
208         }
209
210         g_free(data);
211
212         do_load(keyfile, element);
213
214         g_key_file_free(keyfile);
215
216         return 0;
217 }
218
219 static void do_update(GKeyFile *keyfile, struct connman_element *element)
220 {
221         GSList *list;
222         char *value;
223         const char *str;
224
225         DBG("element %p name %s", element, element->name);
226
227         g_key_file_set_string(keyfile, element->path, "Name", element->name);
228
229         str = __connman_element_policy2string(element->policy);
230         if (str != NULL)
231                 g_key_file_set_string(keyfile, element->path, "Policy", str);
232
233         //g_key_file_set_boolean(keyfile, element->path, "Enabled",
234         //                                              element->enabled);
235
236         if (element->type == CONNMAN_ELEMENT_TYPE_NETWORK)
237                 g_key_file_set_boolean(keyfile, element->path, "Remember",
238                                                         element->remember);
239
240         __connman_element_lock(element);
241
242         for (list = element->properties; list; list = list->next) {
243                 struct connman_property *property = list->data;
244
245                 if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
246                         continue;
247
248                 if (property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)
249                         continue;
250
251                 if (property->type == DBUS_TYPE_STRING)
252                         g_key_file_set_string(keyfile, element->path,
253                                         property->name, property->value);
254         }
255
256         __connman_element_unlock(element);
257
258         if (connman_element_get_value(element,
259                         CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value) == 0)
260                 g_key_file_set_string(keyfile, element->path,
261                                                 "WiFi.Security", value);
262
263         if (connman_element_get_value(element,
264                         CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value) == 0)
265                 g_key_file_set_string(keyfile, element->path,
266                                                 "WiFi.Passphrase", value);
267 }
268
269 int __connman_element_store(struct connman_element *element)
270 {
271         GKeyFile *keyfile;
272         gchar *pathname, *data = NULL;
273         gsize length;
274
275         DBG("element %p name %s", element, element->name);
276
277         if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE &&
278                                 element->type != CONNMAN_ELEMENT_TYPE_NETWORK)
279                 return -EINVAL;
280
281         if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_FAKE)
282                 return -EINVAL;
283
284         pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
285         if (pathname == NULL)
286                 return -ENOMEM;
287
288         keyfile = g_key_file_new();
289
290         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
291                 goto update;
292
293         if (length > 0) {
294                 if (g_key_file_load_from_data(keyfile, data, length,
295                                                         0, NULL) == FALSE)
296                         goto done;
297         }
298
299         g_free(data);
300
301 update:
302         do_update(keyfile, element);
303
304         data = g_key_file_to_data(keyfile, &length, NULL);
305
306         g_file_set_contents(pathname, data, length, NULL);
307
308 done:
309         g_free(data);
310
311         g_key_file_free(keyfile);
312
313         g_free(pathname);
314
315         return 0;
316 }