Add infrastructure for global settings storage
[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_global(void)
70 {
71         GSList *list;
72
73         DBG("");
74
75         for (list = storage_list; list; list = list->next) {
76                 struct connman_storage *storage = list->data;
77
78                 if (storage->global_load) {
79                         if (storage->global_load() == 0)
80                                 return 0;
81                 }
82         }
83
84         return -ENOENT;
85 }
86
87 int __connman_storage_save_global(void)
88 {
89         GSList *list;
90
91         DBG("");
92
93         for (list = storage_list; list; list = list->next) {
94                 struct connman_storage *storage = list->data;
95
96                 if (storage->global_save) {
97                         if (storage->global_save() == 0)
98                                 return 0;
99                 }
100         }
101
102         return -ENOENT;
103 }
104
105 int __connman_storage_init_device(void)
106 {
107         GSList *list;
108
109         DBG("");
110
111         for (list = storage_list; list; list = list->next) {
112                 struct connman_storage *storage = list->data;
113
114                 if (storage->device_init) {
115                         if (storage->device_init() == 0)
116                                 return 0;
117                 }
118         }
119
120         return -ENOENT;
121 }
122
123 int __connman_storage_load_device(struct connman_device *device)
124 {
125         GSList *list;
126
127         DBG("device %p", device);
128
129         for (list = storage_list; list; list = list->next) {
130                 struct connman_storage *storage = list->data;
131
132                 if (storage->device_load) {
133                         if (storage->device_load(device) == 0)
134                                 return 0;
135                 }
136         }
137
138         return -ENOENT;
139 }
140
141 int __connman_storage_save_device(struct connman_device *device)
142 {
143         GSList *list;
144
145         DBG("device %p", device);
146
147         for (list = storage_list; list; list = list->next) {
148                 struct connman_storage *storage = list->data;
149
150                 if (storage->device_save) {
151                         if (storage->device_save(device) == 0)
152                                 return 0;
153                 }
154         }
155
156         return -ENOENT;
157 }
158
159 int __connman_storage_init_network(struct connman_device *device)
160 {
161         GSList *list;
162
163         DBG("device %p", device);
164
165         for (list = storage_list; list; list = list->next) {
166                 struct connman_storage *storage = list->data;
167
168                 if (storage->network_init) {
169                         if (storage->network_init(device) == 0)
170                                 return 0;
171                 }
172         }
173
174         return -ENOENT;
175 }
176
177 int __connman_storage_load_network(struct connman_network *network)
178 {
179         GSList *list;
180
181         DBG("network %p", network);
182
183         for (list = storage_list; list; list = list->next) {
184                 struct connman_storage *storage = list->data;
185
186                 if (storage->network_load) {
187                         if (storage->network_load(network) == 0)
188                                 return 0;
189                 }
190         }
191
192         return -ENOENT;
193 }
194
195 int __connman_storage_save_network(struct connman_network *network)
196 {
197         GSList *list;
198
199         DBG("network %p", network);
200
201         for (list = storage_list; list; list = list->next) {
202                 struct connman_storage *storage = list->data;
203
204                 if (storage->network_save) {
205                         if (storage->network_save(network) == 0)
206                                 return 0;
207                 }
208         }
209
210         return -ENOENT;
211 }
212
213 int __connman_storage_init_service(void)
214 {
215         DBG("");
216
217         return -ENOENT;
218 }
219
220 int __connman_storage_load_service(struct connman_service *service)
221 {
222         GSList *list;
223
224         DBG("service %p", service);
225
226         for (list = storage_list; list; list = list->next) {
227                 struct connman_storage *storage = list->data;
228
229                 if (storage->service_load) {
230                         if (storage->service_load(service) == 0)
231                                 return 0;
232                 }
233         }
234
235         return -ENOENT;
236 }
237
238 int __connman_storage_save_service(struct connman_service *service)
239 {
240         GSList *list;
241
242         DBG("service %p", service);
243
244         for (list = storage_list; list; list = list->next) {
245                 struct connman_storage *storage = list->data;
246
247                 if (storage->service_save) {
248                         if (storage->service_save(service) == 0)
249                                 return 0;
250                 }
251         }
252
253         return -ENOENT;
254 }
255
256 int __connman_storage_init(void)
257 {
258         DBG("");
259
260         return 0;
261 }
262
263 void __connman_storage_cleanup(void)
264 {
265         DBG("");
266 }