750aa43ef8c32d45d82eeae38157c28aa55c92eb
[framework/connectivity/connman.git] / src / iface-storage.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007  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 <errno.h>
27 #include <arpa/inet.h>
28
29 #include <glib.h>
30
31 #include "connman.h"
32
33 #define GROUP_CONFIG  "Config"
34
35 int __connman_iface_load(struct connman_iface *iface)
36 {
37         GKeyFile *keyfile;
38         gchar *pathname, *str;
39
40         DBG("iface %p", iface);
41
42         if (iface->identifier == NULL)
43                 return -EIO;
44
45         pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
46                                                         iface->identifier);
47         if (pathname == NULL)
48                 return -ENOMEM;
49
50         keyfile = g_key_file_new();
51
52         if (g_key_file_load_from_file(keyfile, pathname, 0, NULL) == FALSE)
53                 goto done;
54
55         if (g_key_file_has_group(keyfile, GROUP_CONFIG) == FALSE)
56                 goto done;
57
58         str = g_key_file_get_string(keyfile, GROUP_CONFIG, "Policy", NULL);
59         if (str != NULL) {
60                 iface->policy = __connman_iface_string2policy(str);
61                 g_free(str);
62         }
63
64         str = g_key_file_get_string(keyfile, GROUP_CONFIG, "Network", NULL);
65         if (str != NULL) {
66                 g_free(iface->network.essid);
67                 iface->network.essid = str;
68                 if (iface->driver->set_network)
69                         iface->driver->set_network(iface, str);
70
71                 str = g_key_file_get_string(keyfile, iface->network.essid,
72                                                                 "PSK", NULL);
73                 if (str != NULL) {
74                         g_free(iface->network.psk);
75                         iface->network.psk = str;
76                         if (iface->driver->set_passphrase)
77                                 iface->driver->set_passphrase(iface, str);
78                 }
79         }
80
81 done:
82         g_key_file_free(keyfile);
83
84         g_free(pathname);
85
86         return 0;
87 }
88
89 static void do_update(GKeyFile *keyfile, struct connman_iface *iface)
90 {
91         const char *str;
92
93         DBG("iface %p", iface);
94
95         str = __connman_iface_policy2string(iface->policy);
96         g_key_file_set_string(keyfile, GROUP_CONFIG, "Policy", str);
97
98         if (iface->network.essid != NULL) {
99                 g_key_file_set_string(keyfile, GROUP_CONFIG,
100                                         "Network", iface->network.essid);
101         } else
102                 g_key_file_remove_key(keyfile, GROUP_CONFIG, "Network", NULL);
103
104         if (iface->network.essid != NULL)
105                 g_key_file_set_string(keyfile, iface->network.essid,
106                                                 "PSK", iface->network.psk);
107 }
108
109 int __connman_iface_store(struct connman_iface *iface)
110 {
111         GKeyFile *keyfile;
112         gchar *pathname, *data = NULL;
113         gsize length;
114
115         DBG("iface %p", iface);
116
117         if (iface->identifier == NULL)
118                 return -EIO;
119
120         pathname = g_strdup_printf("%s/%s.conf", STORAGEDIR,
121                                                         iface->identifier);
122         if (pathname == NULL)
123                 return -ENOMEM;
124
125         keyfile = g_key_file_new();
126
127         if (g_file_get_contents(pathname, &data, &length, NULL) == FALSE)
128                 goto update;
129
130         if (length > 0) {
131                 if (g_key_file_load_from_data(keyfile, data, length,
132                                 G_KEY_FILE_KEEP_COMMENTS, NULL) == FALSE)
133                         goto done;
134         }
135
136         g_free(data);
137
138 update:
139         do_update(keyfile, iface);
140
141         data = g_key_file_to_data(keyfile, &length, NULL);
142
143         g_file_set_contents(pathname, data, length, NULL);
144
145 done:
146         g_free(data);
147
148         g_key_file_free(keyfile);
149
150         g_free(pathname);
151
152         return 0;
153 }