Load policy configuration from storage
[platform/upstream/connman.git] / src / storage.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  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 int __connman_storage_init(void)
29 {
30         DBG("");
31
32         return 0;
33 }
34
35 void __connman_storage_cleanup(void)
36 {
37         DBG("");
38 }
39
40 static int do_load(GKeyFile *keyfile, struct connman_element *element)
41 {
42         const gchar *value;
43
44         DBG("element %p name %s", element, element->name);
45
46         value = g_key_file_get_string(keyfile, element->path,
47                                                 "Policy", NULL);
48         if (value != NULL)
49                 element->policy = __connman_element_string2policy(value);
50
51         value = g_key_file_get_string(keyfile, element->path,
52                                                 "WiFi.Security", NULL);
53         if (value != NULL)
54                 connman_element_set_property(element,
55                                 CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value);
56
57         value = g_key_file_get_string(keyfile, element->path,
58                                                 "WiFi.Passphrase", NULL);
59         if (value != NULL)
60                 connman_element_set_property(element,
61                                 CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value);
62
63         return 0;
64 }
65
66 int __connman_element_load(struct connman_element *element)
67 {
68         GKeyFile *keyfile;
69         gchar *pathname, *data = NULL;
70         gsize length;
71
72         DBG("element %p name %s", element, element->name);
73
74         pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
75         if (pathname == NULL)
76                 return -ENOMEM;
77
78         keyfile = g_key_file_new();
79
80         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE) {
81                 g_free(pathname);
82                 return -ENOENT;
83         }
84
85         g_free(pathname);
86
87         if (g_key_file_load_from_data(keyfile, data, length,
88                                                         0, NULL) == FALSE) {
89                 g_free(data);
90                 return -EILSEQ;
91         }
92
93         g_free(data);
94
95         do_load(keyfile, element);
96
97         g_key_file_free(keyfile);
98
99         return 0;
100 }
101
102 static void do_update(GKeyFile *keyfile, struct connman_element *element)
103 {
104         GSList *list;
105         char *value;
106         const char *str;
107
108         DBG("element %p name %s", element, element->name);
109
110         g_key_file_set_string(keyfile, element->path, "Name", element->name);
111
112         str = __connman_element_policy2string(element->policy);
113         if (str != NULL)
114                 g_key_file_set_string(keyfile, element->path, "Policy", str);
115
116         g_key_file_set_boolean(keyfile, element->path, "Enabled",
117                                                         element->enabled);
118
119         __connman_element_lock(element);
120
121         for (list = element->properties; list; list = list->next) {
122                 struct connman_property *property = list->data;
123
124                 if (property->flags & CONNMAN_PROPERTY_FLAG_STATIC)
125                         continue;
126
127                 if (property->flags & CONNMAN_PROPERTY_FLAG_REFERENCE)
128                         continue;
129
130                 if (property->type == DBUS_TYPE_STRING)
131                         g_key_file_set_string(keyfile, element->path,
132                                         property->name, property->value);
133         }
134
135         __connman_element_unlock(element);
136
137         if (connman_element_get_value(element,
138                         CONNMAN_PROPERTY_ID_WIFI_SECURITY, &value) == 0)
139                 g_key_file_set_string(keyfile, element->path,
140                                                 "WiFi.Security", value);
141
142         if (connman_element_get_value(element,
143                         CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &value) == 0)
144                 g_key_file_set_string(keyfile, element->path,
145                                                 "WiFi.Passphrase", value);
146 }
147
148 int __connman_element_store(struct connman_element *element)
149 {
150         GKeyFile *keyfile;
151         gchar *pathname, *data = NULL;
152         gsize length;
153
154         DBG("element %p name %s", element, element->name);
155
156         if (element->type != CONNMAN_ELEMENT_TYPE_DEVICE &&
157                                 element->type != CONNMAN_ELEMENT_TYPE_NETWORK)
158                 return -EINVAL;
159
160         if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_FAKE ||
161                         element->subtype == CONNMAN_ELEMENT_SUBTYPE_NETWORK)
162                 return -EINVAL;
163
164         pathname = g_strdup_printf("%s/elements.conf", STORAGEDIR);
165         if (pathname == NULL)
166                 return -ENOMEM;
167
168         keyfile = g_key_file_new();
169
170         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
171                 goto update;
172
173         if (length > 0) {
174                 if (g_key_file_load_from_data(keyfile, data, length,
175                                 G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
176                         goto done;
177         }
178
179         g_free(data);
180
181 update:
182         do_update(keyfile, element);
183
184         data = g_key_file_to_data(keyfile, &length, NULL);
185
186         g_file_set_contents(pathname, data, length, NULL);
187
188 done:
189         g_free(data);
190
191         g_key_file_free(keyfile);
192
193         g_free(pathname);
194
195         return 0;
196 }