timeserver: Add functions to store/restore timeservers via the clock API
[platform/upstream/connman.git] / src / clock.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 <sys/time.h>
27
28 #include <gdbus.h>
29
30 #include "connman.h"
31
32 enum time_updates {
33         TIME_UPDATES_UNKNOWN = 0,
34         TIME_UPDATES_MANUAL  = 1,
35         TIME_UPDATES_AUTO    = 2,
36 };
37
38 enum timezone_updates {
39         TIMEZONE_UPDATES_UNKNOWN = 0,
40         TIMEZONE_UPDATES_MANUAL  = 1,
41         TIMEZONE_UPDATES_AUTO    = 2,
42 };
43
44 static enum time_updates time_updates_config = TIME_UPDATES_AUTO;
45 static enum timezone_updates timezone_updates_config = TIMEZONE_UPDATES_AUTO;
46
47 static char *timezone_config = NULL;
48
49 static const char *time_updates2string(enum time_updates value)
50 {
51         switch (value) {
52         case TIME_UPDATES_UNKNOWN:
53                 break;
54         case TIME_UPDATES_MANUAL:
55                 return "manual";
56         case TIME_UPDATES_AUTO:
57                 return "auto";
58         }
59
60         return NULL;
61 }
62
63 static enum time_updates string2time_updates(const char *value)
64 {
65         if (g_strcmp0(value, "manual") == 0)
66                 return TIME_UPDATES_MANUAL;
67         else if (g_strcmp0(value, "auto") == 0)
68                 return TIME_UPDATES_AUTO;
69
70         return TIME_UPDATES_UNKNOWN;
71 }
72
73 static const char *timezone_updates2string(enum timezone_updates value)
74 {
75         switch (value) {
76         case TIMEZONE_UPDATES_UNKNOWN:
77                 break;
78         case TIMEZONE_UPDATES_MANUAL:
79                 return "manual";
80         case TIMEZONE_UPDATES_AUTO:
81                 return "auto";
82         }
83
84         return NULL;
85 }
86
87 static enum timezone_updates string2timezone_updates(const char *value)
88 {
89         if (g_strcmp0(value, "manual") == 0)
90                 return TIMEZONE_UPDATES_MANUAL;
91         else if (g_strcmp0(value, "auto") == 0)
92                 return TIMEZONE_UPDATES_AUTO;
93
94         return TIMEZONE_UPDATES_UNKNOWN;
95 }
96
97 static void append_timeservers(DBusMessageIter *iter, void *user_data)
98 {
99         int i;
100         char **timeservers = __connman_timeserver_system_get();
101
102         if (timeservers == NULL)
103                 return;
104
105         for (i = 0; timeservers[i] != NULL; i++) {
106                 dbus_message_iter_append_basic(iter,
107                                 DBUS_TYPE_STRING, &timeservers[i]);
108         }
109
110         g_strfreev(timeservers);
111 }
112
113 static DBusMessage *get_properties(DBusConnection *conn,
114                                         DBusMessage *msg, void *data)
115 {
116         DBusMessage *reply;
117         DBusMessageIter array, dict;
118         struct timeval tv;
119         const char *str;
120
121         DBG("conn %p", conn);
122
123         reply = dbus_message_new_method_return(msg);
124         if (reply == NULL)
125                 return NULL;
126
127         dbus_message_iter_init_append(reply, &array);
128
129         connman_dbus_dict_open(&array, &dict);
130
131         if (gettimeofday(&tv, NULL) == 0) {
132                 dbus_uint64_t val = tv.tv_sec;
133
134                 connman_dbus_dict_append_basic(&dict, "Time",
135                                                 DBUS_TYPE_UINT64, &val);
136         }
137
138         str = time_updates2string(time_updates_config);
139         if (str != NULL)
140                 connman_dbus_dict_append_basic(&dict, "TimeUpdates",
141                                                 DBUS_TYPE_STRING, &str);
142
143         if (timezone_config != NULL)
144                 connman_dbus_dict_append_basic(&dict, "Timezone",
145                                         DBUS_TYPE_STRING, &timezone_config);
146
147         str = timezone_updates2string(timezone_updates_config);
148         if (str != NULL)
149                 connman_dbus_dict_append_basic(&dict, "TimezoneUpdates",
150                                                 DBUS_TYPE_STRING, &str);
151
152         connman_dbus_dict_append_array(&dict, "Timeservers",
153                                 DBUS_TYPE_STRING, append_timeservers, NULL);
154
155         connman_dbus_dict_close(&array, &dict);
156
157         return reply;
158 }
159
160 static DBusMessage *set_property(DBusConnection *conn,
161                                         DBusMessage *msg, void *data)
162 {
163         DBusMessageIter iter, value;
164         const char *name;
165         int type;
166
167         DBG("conn %p", conn);
168
169         if (dbus_message_iter_init(msg, &iter) == FALSE)
170                 return __connman_error_invalid_arguments(msg);
171
172         dbus_message_iter_get_basic(&iter, &name);
173         dbus_message_iter_next(&iter);
174         dbus_message_iter_recurse(&iter, &value);
175
176         type = dbus_message_iter_get_arg_type(&value);
177
178         if (g_str_equal(name, "Time") == TRUE) {
179                 struct timeval tv;
180                 dbus_uint64_t newval;
181
182                 if (type != DBUS_TYPE_UINT64)
183                         return __connman_error_invalid_arguments(msg);
184
185                 if (time_updates_config != TIME_UPDATES_MANUAL)
186                         return __connman_error_permission_denied(msg);
187
188                 dbus_message_iter_get_basic(&value, &newval);
189
190                 tv.tv_sec = newval;
191                 tv.tv_usec = 0;
192
193                 if (settimeofday(&tv, NULL) < 0)
194                         return __connman_error_invalid_arguments(msg);
195
196                 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
197                                 CONNMAN_CLOCK_INTERFACE, "Time",
198                                 DBUS_TYPE_UINT64, &newval);
199         } else if (g_str_equal(name, "TimeUpdates") == TRUE) {
200                 const char *strval;
201                 enum time_updates newval;
202
203                 if (type != DBUS_TYPE_STRING)
204                         return __connman_error_invalid_arguments(msg);
205
206                 dbus_message_iter_get_basic(&value, &strval);
207                 newval = string2time_updates(strval);
208
209                 if (newval == TIME_UPDATES_UNKNOWN)
210                         return __connman_error_invalid_arguments(msg);
211
212                 if (newval == time_updates_config)
213                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
214
215                 time_updates_config = newval;
216
217                 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
218                                 CONNMAN_CLOCK_INTERFACE, "TimeUpdates",
219                                 DBUS_TYPE_STRING, &strval);
220         } else if (g_str_equal(name, "Timezone") == TRUE) {
221                 const char *strval;
222
223                 if (type != DBUS_TYPE_STRING)
224                         return __connman_error_invalid_arguments(msg);
225
226                 if (timezone_updates_config != TIMEZONE_UPDATES_MANUAL)
227                         return __connman_error_permission_denied(msg);
228
229                 dbus_message_iter_get_basic(&value, &strval);
230
231                 if (__connman_timezone_change(strval) < 0)
232                         return __connman_error_invalid_arguments(msg);
233         } else if (g_str_equal(name, "TimezoneUpdates") == TRUE) {
234                 const char *strval;
235                 enum timezone_updates newval;
236
237                 if (type != DBUS_TYPE_STRING)
238                         return __connman_error_invalid_arguments(msg);
239
240                 dbus_message_iter_get_basic(&value, &strval);
241                 newval = string2timezone_updates(strval);
242
243                 if (newval == TIMEZONE_UPDATES_UNKNOWN)
244                         return __connman_error_invalid_arguments(msg);
245
246                 if (newval == timezone_updates_config)
247                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
248
249                 timezone_updates_config = newval;
250
251                 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
252                                 CONNMAN_CLOCK_INTERFACE, "TimezoneUpdates",
253                                 DBUS_TYPE_STRING, &strval);
254         } else if (g_str_equal(name, "Timeservers") == TRUE) {
255                 DBusMessageIter entry;
256
257                 if (type != DBUS_TYPE_ARRAY)
258                         return __connman_error_invalid_arguments(msg);
259
260                 dbus_message_iter_recurse(&value, &entry);
261
262                 if (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_INVALID)
263                         __connman_timeserver_system_append(NULL);
264
265                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
266                         const char *val;
267
268                         dbus_message_iter_get_basic(&entry, &val);
269                         __connman_timeserver_system_append(val);
270                         dbus_message_iter_next(&entry);
271                 }
272
273                 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
274                                 CONNMAN_CLOCK_INTERFACE, "Timeservers",
275                                 DBUS_TYPE_STRING, append_timeservers, NULL);
276         } else
277                 return __connman_error_invalid_property(msg);
278
279         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
280 }
281
282 static GDBusMethodTable clock_methods[] = {
283         { "GetProperties", "",   "a{sv}", get_properties },
284         { "SetProperty",   "sv", "",      set_property   },
285         { },
286 };
287
288 static GDBusSignalTable clock_signals[] = {
289         { "PropertyChanged", "sv" },
290         { },
291 };
292
293 static DBusConnection *connection = NULL;
294
295 void __connman_clock_update_timezone(void)
296 {
297         DBG("");
298
299         g_free(timezone_config);
300         timezone_config = __connman_timezone_lookup();
301
302         if (timezone_config == NULL)
303                 return;
304
305         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
306                                 CONNMAN_CLOCK_INTERFACE, "Timezone",
307                                 DBUS_TYPE_STRING, &timezone_config);
308 }
309
310 int __connman_clock_init(void)
311 {
312         DBG("");
313
314         connection = connman_dbus_get_connection();
315         if (connection == NULL)
316                 return -1;
317
318         __connman_timezone_init();
319
320         timezone_config = __connman_timezone_lookup();
321
322         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
323                                                 CONNMAN_CLOCK_INTERFACE,
324                                                 clock_methods, clock_signals,
325                                                 NULL, NULL, NULL);
326
327         return 0;
328 }
329
330 void __connman_clock_cleanup(void)
331 {
332         DBG("");
333
334         if (connection == NULL)
335                 return;
336
337         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
338                                                 CONNMAN_CLOCK_INTERFACE);
339
340         dbus_connection_unref(connection);
341
342         __connman_timezone_cleanup();
343
344         g_free(timezone_config);
345 }