5 * Copyright (C) 2007-2012 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();
160 if (timeservers == NULL)
163 for (i = 0; timeservers[i] != NULL; 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);
201 if (timezone_config != NULL)
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) == FALSE)
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") == TRUE) {
245 dbus_uint64_t newval;
247 if (type != DBUS_TYPE_UINT64)
248 return __connman_error_invalid_arguments(msg);
250 if (time_updates_config != TIME_UPDATES_MANUAL)
251 return __connman_error_permission_denied(msg);
253 dbus_message_iter_get_basic(&value, &newval);
258 if (settimeofday(&tv, NULL) < 0)
259 return __connman_error_invalid_arguments(msg);
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") == TRUE) {
266 enum time_updates newval;
268 if (type != DBUS_TYPE_STRING)
269 return __connman_error_invalid_arguments(msg);
271 dbus_message_iter_get_basic(&value, &strval);
272 newval = string2time_updates(strval);
274 if (newval == TIME_UPDATES_UNKNOWN)
275 return __connman_error_invalid_arguments(msg);
277 if (newval == time_updates_config)
278 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
280 time_updates_config = newval;
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") == TRUE) {
289 if (type != DBUS_TYPE_STRING)
290 return __connman_error_invalid_arguments(msg);
292 if (timezone_updates_config != TIMEZONE_UPDATES_MANUAL)
293 return __connman_error_permission_denied(msg);
295 dbus_message_iter_get_basic(&value, &strval);
297 if (__connman_timezone_change(strval) < 0)
298 return __connman_error_invalid_arguments(msg);
299 } else if (g_str_equal(name, "TimezoneUpdates") == TRUE) {
301 enum timezone_updates newval;
303 if (type != DBUS_TYPE_STRING)
304 return __connman_error_invalid_arguments(msg);
306 dbus_message_iter_get_basic(&value, &strval);
307 newval = string2timezone_updates(strval);
309 if (newval == TIMEZONE_UPDATES_UNKNOWN)
310 return __connman_error_invalid_arguments(msg);
312 if (newval == timezone_updates_config)
313 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
315 timezone_updates_config = newval;
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") == TRUE) {
322 DBusMessageIter entry;
327 if (type != DBUS_TYPE_ARRAY)
328 return __connman_error_invalid_arguments(msg);
330 dbus_message_iter_recurse(&value, &entry);
332 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
336 dbus_message_iter_get_basic(&entry, &val);
338 new_head = __connman_timeserver_add_list(list, val);
339 if (list != new_head) {
344 dbus_message_iter_next(&entry);
348 str = g_new0(char *, count+1);
350 while (list != NULL) {
352 str[count] = list->data;
353 list = g_slist_delete_link(list, list);
357 __connman_timeserver_system_set(str);
362 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
363 CONNMAN_CLOCK_INTERFACE, "Timeservers",
364 DBUS_TYPE_STRING, append_timeservers, NULL);
366 return __connman_error_invalid_property(msg);
368 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
371 static const GDBusMethodTable clock_methods[] = {
372 { GDBUS_METHOD("GetProperties",
373 NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
375 { GDBUS_METHOD("SetProperty",
376 GDBUS_ARGS({ "name", "s" }, { "value", "v" }), NULL,
381 static const GDBusSignalTable clock_signals[] = {
382 { GDBUS_SIGNAL("PropertyChanged",
383 GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
387 static DBusConnection *connection = NULL;
389 void __connman_clock_update_timezone(void)
393 g_free(timezone_config);
394 timezone_config = __connman_timezone_lookup();
396 if (timezone_config == NULL)
399 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
400 CONNMAN_CLOCK_INTERFACE, "Timezone",
401 DBUS_TYPE_STRING, &timezone_config);
404 int __connman_clock_init(void)
408 connection = connman_dbus_get_connection();
409 if (connection == NULL)
412 __connman_timezone_init();
414 timezone_config = __connman_timezone_lookup();
416 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
417 CONNMAN_CLOCK_INTERFACE,
418 clock_methods, clock_signals,
421 clock_properties_load();
425 void __connman_clock_cleanup(void)
429 if (connection == NULL)
432 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
433 CONNMAN_CLOCK_INTERFACE);
435 dbus_connection_unref(connection);
437 __connman_timezone_cleanup();
439 g_free(timezone_config);