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