5 * Copyright (C) 2007-2013 Intel Corporation. All rights reserved.
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.
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.
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
32 enum timezone_updates {
33 TIMEZONE_UPDATES_UNKNOWN = 0,
34 TIMEZONE_UPDATES_MANUAL = 1,
35 TIMEZONE_UPDATES_AUTO = 2,
38 static enum time_updates time_updates_config = TIME_UPDATES_AUTO;
39 static enum timezone_updates timezone_updates_config = TIMEZONE_UPDATES_AUTO;
41 static char *timezone_config = NULL;
43 static const char *time_updates2string(enum time_updates value)
46 case TIME_UPDATES_UNKNOWN:
48 case TIME_UPDATES_MANUAL:
50 case TIME_UPDATES_AUTO:
57 static enum time_updates string2time_updates(const char *value)
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;
64 return TIME_UPDATES_UNKNOWN;
67 static const char *timezone_updates2string(enum timezone_updates value)
70 case TIMEZONE_UPDATES_UNKNOWN:
72 case TIMEZONE_UPDATES_MANUAL:
74 case TIMEZONE_UPDATES_AUTO:
81 static enum timezone_updates string2timezone_updates(const char *value)
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;
88 return TIMEZONE_UPDATES_UNKNOWN;
91 static void clock_properties_load(void)
95 enum time_updates time_value;
96 enum timezone_updates timezone_value;
98 keyfile = __connman_storage_load_global();
102 str = g_key_file_get_string(keyfile, "global", "TimeUpdates", NULL);
104 time_value = string2time_updates(str);
105 if (time_value != TIME_UPDATES_UNKNOWN)
106 time_updates_config = time_value;
110 str = g_key_file_get_string(keyfile, "global", "TimezoneUpdates",
113 timezone_value = string2timezone_updates(str);
114 if (timezone_value != TIMEZONE_UPDATES_UNKNOWN)
115 timezone_updates_config = timezone_value;
119 g_key_file_free(keyfile);
122 static void clock_properties_save(void)
127 keyfile = __connman_storage_load_global();
129 keyfile = g_key_file_new();
131 str = time_updates2string(time_updates_config);
133 g_key_file_set_string(keyfile, "global", "TimeUpdates", str);
135 g_key_file_remove_key(keyfile, "global", "TimeUpdates", NULL);
137 str = timezone_updates2string(timezone_updates_config);
139 g_key_file_set_string(keyfile, "global", "TimezoneUpdates",
142 g_key_file_remove_key(keyfile, "global", "TimezoneUpdates",
145 __connman_storage_save_global(keyfile);
147 g_key_file_free(keyfile);
150 enum time_updates __connman_clock_timeupdates(void)
152 return time_updates_config;
155 static void append_timeservers(DBusMessageIter *iter, void *user_data)
158 char **timeservers = __connman_timeserver_system_get();
163 for (i = 0; timeservers[i]; i++) {
164 dbus_message_iter_append_basic(iter,
165 DBUS_TYPE_STRING, ×ervers[i]);
168 g_strfreev(timeservers);
171 static DBusMessage *get_properties(DBusConnection *conn,
172 DBusMessage *msg, void *data)
175 DBusMessageIter array, dict;
179 DBG("conn %p", conn);
181 reply = dbus_message_new_method_return(msg);
185 dbus_message_iter_init_append(reply, &array);
187 connman_dbus_dict_open(&array, &dict);
189 if (gettimeofday(&tv, NULL) == 0) {
190 dbus_uint64_t val = tv.tv_sec;
192 connman_dbus_dict_append_basic(&dict, "Time",
193 DBUS_TYPE_UINT64, &val);
196 str = time_updates2string(time_updates_config);
198 connman_dbus_dict_append_basic(&dict, "TimeUpdates",
199 DBUS_TYPE_STRING, &str);
202 connman_dbus_dict_append_basic(&dict, "Timezone",
203 DBUS_TYPE_STRING, &timezone_config);
205 str = timezone_updates2string(timezone_updates_config);
207 connman_dbus_dict_append_basic(&dict, "TimezoneUpdates",
208 DBUS_TYPE_STRING, &str);
210 connman_dbus_dict_append_array(&dict, "Timeservers",
211 DBUS_TYPE_STRING, append_timeservers, NULL);
213 connman_dbus_dict_close(&array, &dict);
218 static DBusMessage *set_property(DBusConnection *conn,
219 DBusMessage *msg, void *data)
221 DBusMessageIter iter, value;
225 DBG("conn %p", conn);
227 if (!dbus_message_iter_init(msg, &iter))
228 return __connman_error_invalid_arguments(msg);
230 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
231 return __connman_error_invalid_arguments(msg);
233 dbus_message_iter_get_basic(&iter, &name);
234 dbus_message_iter_next(&iter);
236 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
237 return __connman_error_invalid_arguments(msg);
239 dbus_message_iter_recurse(&iter, &value);
241 type = dbus_message_iter_get_arg_type(&value);
243 if (g_str_equal(name, "Time")) {
244 #if defined TIZEN_EXT
245 /* Tizen updates time (ntp) by system service */
247 return __connman_error_permission_denied(msg);
250 dbus_uint64_t newval;
252 if (type != DBUS_TYPE_UINT64)
253 return __connman_error_invalid_arguments(msg);
255 if (time_updates_config != TIME_UPDATES_MANUAL)
256 return __connman_error_permission_denied(msg);
258 dbus_message_iter_get_basic(&value, &newval);
263 if (settimeofday(&tv, NULL) < 0)
264 return __connman_error_invalid_arguments(msg);
266 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
267 CONNMAN_CLOCK_INTERFACE, "Time",
268 DBUS_TYPE_UINT64, &newval);
270 } else if (g_str_equal(name, "TimeUpdates")) {
272 enum time_updates newval;
274 if (type != DBUS_TYPE_STRING)
275 return __connman_error_invalid_arguments(msg);
277 dbus_message_iter_get_basic(&value, &strval);
278 newval = string2time_updates(strval);
280 if (newval == TIME_UPDATES_UNKNOWN)
281 return __connman_error_invalid_arguments(msg);
283 if (newval == time_updates_config)
284 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
286 time_updates_config = newval;
288 clock_properties_save();
289 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
290 CONNMAN_CLOCK_INTERFACE, "TimeUpdates",
291 DBUS_TYPE_STRING, &strval);
292 } else if (g_str_equal(name, "Timezone")) {
295 if (type != DBUS_TYPE_STRING)
296 return __connman_error_invalid_arguments(msg);
298 if (timezone_updates_config != TIMEZONE_UPDATES_MANUAL)
299 return __connman_error_permission_denied(msg);
301 dbus_message_iter_get_basic(&value, &strval);
303 if (__connman_timezone_change(strval) < 0)
304 return __connman_error_invalid_arguments(msg);
305 } else if (g_str_equal(name, "TimezoneUpdates")) {
307 enum timezone_updates newval;
309 if (type != DBUS_TYPE_STRING)
310 return __connman_error_invalid_arguments(msg);
312 dbus_message_iter_get_basic(&value, &strval);
313 newval = string2timezone_updates(strval);
315 if (newval == TIMEZONE_UPDATES_UNKNOWN)
316 return __connman_error_invalid_arguments(msg);
318 if (newval == timezone_updates_config)
319 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
321 timezone_updates_config = newval;
323 clock_properties_save();
324 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
325 CONNMAN_CLOCK_INTERFACE, "TimezoneUpdates",
326 DBUS_TYPE_STRING, &strval);
327 } else if (g_str_equal(name, "Timeservers")) {
328 DBusMessageIter entry;
333 if (type != DBUS_TYPE_ARRAY)
334 return __connman_error_invalid_arguments(msg);
336 dbus_message_iter_recurse(&value, &entry);
338 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
342 dbus_message_iter_get_basic(&entry, &val);
344 new_head = __connman_timeserver_add_list(list, val);
345 if (list != new_head) {
350 dbus_message_iter_next(&entry);
354 str = g_new0(char *, count+1);
358 str[count] = list->data;
359 list = g_slist_delete_link(list, list);
363 __connman_timeserver_system_set(str);
368 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
369 CONNMAN_CLOCK_INTERFACE, "Timeservers",
370 DBUS_TYPE_STRING, append_timeservers, NULL);
372 return __connman_error_invalid_property(msg);
374 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
377 static const GDBusMethodTable clock_methods[] = {
378 { GDBUS_METHOD("GetProperties",
379 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
381 { GDBUS_METHOD("SetProperty",
382 GDBUS_ARGS({ "name", "s" }, { "value", "v" }), NULL,
387 static const GDBusSignalTable clock_signals[] = {
388 { GDBUS_SIGNAL("PropertyChanged",
389 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
393 static DBusConnection *connection = NULL;
395 void __connman_clock_update_timezone(void)
399 g_free(timezone_config);
400 timezone_config = __connman_timezone_lookup();
402 if (!timezone_config)
405 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
406 CONNMAN_CLOCK_INTERFACE, "Timezone",
407 DBUS_TYPE_STRING, &timezone_config);
410 int __connman_clock_init(void)
414 connection = connman_dbus_get_connection();
418 __connman_timezone_init();
420 timezone_config = __connman_timezone_lookup();
422 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
423 CONNMAN_CLOCK_INTERFACE,
424 clock_methods, clock_signals,
427 clock_properties_load();
431 void __connman_clock_cleanup(void)
438 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
439 CONNMAN_CLOCK_INTERFACE);
441 dbus_connection_unref(connection);
443 __connman_timezone_cleanup();
445 g_free(timezone_config);