0fde2c341aca79afc00296c6b233bb12eeeaa0bc
[platform/upstream/connman.git] / src / clock.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  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 timezone_updates {
33         TIMEZONE_UPDATES_UNKNOWN = 0,
34         TIMEZONE_UPDATES_MANUAL  = 1,
35         TIMEZONE_UPDATES_AUTO    = 2,
36 };
37
38 static enum time_updates time_updates_config = TIME_UPDATES_AUTO;
39 static enum timezone_updates timezone_updates_config = TIMEZONE_UPDATES_AUTO;
40
41 static char *timezone_config = NULL;
42
43 static const char *time_updates2string(enum time_updates value)
44 {
45         switch (value) {
46         case TIME_UPDATES_UNKNOWN:
47                 break;
48         case TIME_UPDATES_MANUAL:
49                 return "manual";
50         case TIME_UPDATES_AUTO:
51                 return "auto";
52         }
53
54         return NULL;
55 }
56
57 static enum time_updates string2time_updates(const char *value)
58 {
59         if (g_strcmp0(value, "manual") == 0)
60                 return TIME_UPDATES_MANUAL;
61         else if (g_strcmp0(value, "auto") == 0)
62                 return TIME_UPDATES_AUTO;
63
64         return TIME_UPDATES_UNKNOWN;
65 }
66
67 static const char *timezone_updates2string(enum timezone_updates value)
68 {
69         switch (value) {
70         case TIMEZONE_UPDATES_UNKNOWN:
71                 break;
72         case TIMEZONE_UPDATES_MANUAL:
73                 return "manual";
74         case TIMEZONE_UPDATES_AUTO:
75                 return "auto";
76         }
77
78         return NULL;
79 }
80
81 static enum timezone_updates string2timezone_updates(const char *value)
82 {
83         if (g_strcmp0(value, "manual") == 0)
84                 return TIMEZONE_UPDATES_MANUAL;
85         else if (g_strcmp0(value, "auto") == 0)
86                 return TIMEZONE_UPDATES_AUTO;
87
88         return TIMEZONE_UPDATES_UNKNOWN;
89 }
90
91 static void clock_properties_load(void)
92 {
93         GKeyFile *keyfile;
94         char *str;
95         enum time_updates time_value;
96         enum timezone_updates timezone_value;
97
98         keyfile = __connman_storage_load_global();
99         if (!keyfile)
100                 return;
101
102         str = g_key_file_get_string(keyfile, "global", "TimeUpdates", NULL);
103
104         time_value = string2time_updates(str);
105         if (time_value != TIME_UPDATES_UNKNOWN)
106                 time_updates_config = time_value;
107
108         g_free(str);
109
110         str = g_key_file_get_string(keyfile, "global", "TimezoneUpdates",
111                         NULL);
112
113         timezone_value = string2timezone_updates(str);
114         if (timezone_value != TIMEZONE_UPDATES_UNKNOWN)
115                 timezone_updates_config = timezone_value;
116
117         g_free(str);
118
119         g_key_file_free(keyfile);
120 }
121
122 static void clock_properties_save(void)
123 {
124         GKeyFile *keyfile;
125         const char *str;
126
127         keyfile = __connman_storage_load_global();
128         if (!keyfile)
129                 keyfile = g_key_file_new();
130
131         str = time_updates2string(time_updates_config);
132         if (str)
133                 g_key_file_set_string(keyfile, "global", "TimeUpdates", str);
134         else
135                 g_key_file_remove_key(keyfile, "global", "TimeUpdates", NULL);
136
137         str = timezone_updates2string(timezone_updates_config);
138         if (str)
139                 g_key_file_set_string(keyfile, "global", "TimezoneUpdates",
140                                 str);
141         else
142                 g_key_file_remove_key(keyfile, "global", "TimezoneUpdates",
143                                 NULL);
144
145         __connman_storage_save_global(keyfile);
146
147         g_key_file_free(keyfile);
148 }
149
150 enum time_updates __connman_clock_timeupdates(void)
151 {
152         return time_updates_config;
153 }
154
155 static void append_timeservers(DBusMessageIter *iter, void *user_data)
156 {
157         int i;
158         char **timeservers = __connman_timeserver_system_get();
159
160         if (!timeservers)
161                 return;
162
163         for (i = 0; timeservers[i]; i++) {
164                 dbus_message_iter_append_basic(iter,
165                                 DBUS_TYPE_STRING, &timeservers[i]);
166         }
167
168         g_strfreev(timeservers);
169 }
170
171 static DBusMessage *get_properties(DBusConnection *conn,
172                                         DBusMessage *msg, void *data)
173 {
174         DBusMessage *reply;
175         DBusMessageIter array, dict;
176         struct timeval tv;
177         const char *str;
178
179         DBG("conn %p", conn);
180
181         reply = dbus_message_new_method_return(msg);
182         if (!reply)
183                 return NULL;
184
185         dbus_message_iter_init_append(reply, &array);
186
187         connman_dbus_dict_open(&array, &dict);
188
189         if (gettimeofday(&tv, NULL) == 0) {
190                 dbus_uint64_t val = tv.tv_sec;
191
192                 connman_dbus_dict_append_basic(&dict, "Time",
193                                                 DBUS_TYPE_UINT64, &val);
194         }
195
196         str = time_updates2string(time_updates_config);
197         if (str)
198                 connman_dbus_dict_append_basic(&dict, "TimeUpdates",
199                                                 DBUS_TYPE_STRING, &str);
200
201         if (timezone_config)
202                 connman_dbus_dict_append_basic(&dict, "Timezone",
203                                         DBUS_TYPE_STRING, &timezone_config);
204
205         str = timezone_updates2string(timezone_updates_config);
206         if (str)
207                 connman_dbus_dict_append_basic(&dict, "TimezoneUpdates",
208                                                 DBUS_TYPE_STRING, &str);
209
210         connman_dbus_dict_append_array(&dict, "Timeservers",
211                                 DBUS_TYPE_STRING, append_timeservers, NULL);
212
213         connman_dbus_dict_close(&array, &dict);
214
215         return reply;
216 }
217
218 static DBusMessage *set_property(DBusConnection *conn,
219                                         DBusMessage *msg, void *data)
220 {
221         DBusMessageIter iter, value;
222         const char *name;
223         int type;
224
225         DBG("conn %p", conn);
226
227         if (!dbus_message_iter_init(msg, &iter))
228                 return __connman_error_invalid_arguments(msg);
229
230         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
231                 return __connman_error_invalid_arguments(msg);
232
233         dbus_message_iter_get_basic(&iter, &name);
234         dbus_message_iter_next(&iter);
235
236         if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
237                 return __connman_error_invalid_arguments(msg);
238
239         dbus_message_iter_recurse(&iter, &value);
240
241         type = dbus_message_iter_get_arg_type(&value);
242
243         if (g_str_equal(name, "Time")) {
244                 struct timeval tv;
245                 dbus_uint64_t newval;
246
247                 if (type != DBUS_TYPE_UINT64)
248                         return __connman_error_invalid_arguments(msg);
249
250                 if (time_updates_config != TIME_UPDATES_MANUAL)
251                         return __connman_error_permission_denied(msg);
252
253                 dbus_message_iter_get_basic(&value, &newval);
254
255                 tv.tv_sec = newval;
256                 tv.tv_usec = 0;
257
258                 if (settimeofday(&tv, NULL) < 0)
259                         return __connman_error_invalid_arguments(msg);
260
261                 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
262                                 CONNMAN_CLOCK_INTERFACE, "Time",
263                                 DBUS_TYPE_UINT64, &newval);
264         } else if (g_str_equal(name, "TimeUpdates")) {
265                 const char *strval;
266                 enum time_updates newval;
267
268                 if (type != DBUS_TYPE_STRING)
269                         return __connman_error_invalid_arguments(msg);
270
271                 dbus_message_iter_get_basic(&value, &strval);
272                 newval = string2time_updates(strval);
273
274                 if (newval == TIME_UPDATES_UNKNOWN)
275                         return __connman_error_invalid_arguments(msg);
276
277                 if (newval == time_updates_config)
278                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
279
280                 time_updates_config = newval;
281
282                 clock_properties_save();
283                 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
284                                 CONNMAN_CLOCK_INTERFACE, "TimeUpdates",
285                                 DBUS_TYPE_STRING, &strval);
286         } else if (g_str_equal(name, "Timezone")) {
287                 const char *strval;
288
289                 if (type != DBUS_TYPE_STRING)
290                         return __connman_error_invalid_arguments(msg);
291
292                 if (timezone_updates_config != TIMEZONE_UPDATES_MANUAL)
293                         return __connman_error_permission_denied(msg);
294
295                 dbus_message_iter_get_basic(&value, &strval);
296
297                 if (__connman_timezone_change(strval) < 0)
298                         return __connman_error_invalid_arguments(msg);
299         } else if (g_str_equal(name, "TimezoneUpdates")) {
300                 const char *strval;
301                 enum timezone_updates newval;
302
303                 if (type != DBUS_TYPE_STRING)
304                         return __connman_error_invalid_arguments(msg);
305
306                 dbus_message_iter_get_basic(&value, &strval);
307                 newval = string2timezone_updates(strval);
308
309                 if (newval == TIMEZONE_UPDATES_UNKNOWN)
310                         return __connman_error_invalid_arguments(msg);
311
312                 if (newval == timezone_updates_config)
313                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
314
315                 timezone_updates_config = newval;
316
317                 clock_properties_save();
318                 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
319                                 CONNMAN_CLOCK_INTERFACE, "TimezoneUpdates",
320                                 DBUS_TYPE_STRING, &strval);
321         } else if (g_str_equal(name, "Timeservers")) {
322                 DBusMessageIter entry;
323                 char **str = NULL;
324                 GSList *list = NULL;
325                 int count = 0;
326
327                 if (type != DBUS_TYPE_ARRAY)
328                         return __connman_error_invalid_arguments(msg);
329
330                 dbus_message_iter_recurse(&value, &entry);
331
332                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
333                         const char *val;
334                         GSList *new_head;
335
336                         dbus_message_iter_get_basic(&entry, &val);
337
338                         new_head = __connman_timeserver_add_list(list, val);
339                         if (list != new_head) {
340                                 count++;
341                                 list = new_head;
342                         }
343
344                         dbus_message_iter_next(&entry);
345                 }
346
347                 if (list) {
348                         str = g_new0(char *, count+1);
349
350                         while (list) {
351                                 count--;
352                                 str[count] = list->data;
353                                 list = g_slist_delete_link(list, list);
354                         };
355                 }
356
357                 __connman_timeserver_system_set(str);
358
359                 if (str)
360                         g_strfreev(str);
361
362                 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
363                                 CONNMAN_CLOCK_INTERFACE, "Timeservers",
364                                 DBUS_TYPE_STRING, append_timeservers, NULL);
365         } else
366                 return __connman_error_invalid_property(msg);
367
368         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
369 }
370
371 static const GDBusMethodTable clock_methods[] = {
372         { GDBUS_METHOD("GetProperties",
373                         NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
374                         get_properties) },
375         { GDBUS_METHOD("SetProperty",
376                         GDBUS_ARGS({ "name", "s" }, { "value", "v" }), NULL,
377                         set_property)   },
378         { },
379 };
380
381 static const GDBusSignalTable clock_signals[] = {
382         { GDBUS_SIGNAL("PropertyChanged",
383                         GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
384         { },
385 };
386
387 static DBusConnection *connection = NULL;
388
389 void __connman_clock_update_timezone(void)
390 {
391         DBG("");
392
393         g_free(timezone_config);
394         timezone_config = __connman_timezone_lookup();
395
396         if (!timezone_config)
397                 return;
398
399         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
400                                 CONNMAN_CLOCK_INTERFACE, "Timezone",
401                                 DBUS_TYPE_STRING, &timezone_config);
402 }
403
404 int __connman_clock_init(void)
405 {
406         DBG("");
407
408         connection = connman_dbus_get_connection();
409         if (!connection)
410                 return -1;
411
412         __connman_timezone_init();
413
414         timezone_config = __connman_timezone_lookup();
415
416         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
417                                                 CONNMAN_CLOCK_INTERFACE,
418                                                 clock_methods, clock_signals,
419                                                 NULL, NULL, NULL);
420
421         clock_properties_load();
422         return 0;
423 }
424
425 void __connman_clock_cleanup(void)
426 {
427         DBG("");
428
429         if (!connection)
430                 return;
431
432         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
433                                                 CONNMAN_CLOCK_INTERFACE);
434
435         dbus_connection_unref(connection);
436
437         __connman_timezone_cleanup();
438
439         g_free(timezone_config);
440 }