5 * Copyright (C) 2007-2010 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
31 TIME_UPDATES_UNKNOWN = 0,
32 TIME_UPDATES_MANUAL = 1,
33 TIME_UPDATES_AUTO = 2,
36 enum timezone_updates {
37 TIMEZONE_UPDATES_UNKNOWN = 0,
38 TIMEZONE_UPDATES_MANUAL = 1,
39 TIMEZONE_UPDATES_AUTO = 2,
42 static enum time_updates time_updates_config = TIME_UPDATES_AUTO;
43 static enum timezone_updates timezone_updates_config = TIMEZONE_UPDATES_AUTO;
45 static char **timeservers_config = NULL;
47 static const char *time_updates2string(enum time_updates value)
50 case TIME_UPDATES_UNKNOWN:
52 case TIME_UPDATES_MANUAL:
54 case TIME_UPDATES_AUTO:
61 static enum time_updates string2time_updates(const char *value)
63 if (g_strcmp0(value, "manual") == 0)
64 return TIME_UPDATES_MANUAL;
65 else if (g_strcmp0(value, "auto") == 0)
66 return TIME_UPDATES_AUTO;
68 return TIME_UPDATES_UNKNOWN;
71 static const char *timezone_updates2string(enum timezone_updates value)
74 case TIMEZONE_UPDATES_UNKNOWN:
76 case TIMEZONE_UPDATES_MANUAL:
78 case TIMEZONE_UPDATES_AUTO:
85 static enum timezone_updates string2timezone_updates(const char *value)
87 if (g_strcmp0(value, "manual") == 0)
88 return TIMEZONE_UPDATES_MANUAL;
89 else if (g_strcmp0(value, "auto") == 0)
90 return TIMEZONE_UPDATES_AUTO;
92 return TIMEZONE_UPDATES_UNKNOWN;
95 static void append_timeservers(DBusMessageIter *iter, void *user_data)
99 if (timeservers_config == NULL)
102 for (i = 0; timeservers_config[i] != NULL; i++) {
103 dbus_message_iter_append_basic(iter,
104 DBUS_TYPE_STRING, ×ervers_config[i]);
108 static DBusMessage *get_properties(DBusConnection *conn,
109 DBusMessage *msg, void *data)
112 DBusMessageIter array, dict;
115 DBG("conn %p", conn);
117 reply = dbus_message_new_method_return(msg);
121 dbus_message_iter_init_append(reply, &array);
123 connman_dbus_dict_open(&array, &dict);
125 str = time_updates2string(time_updates_config);
127 connman_dbus_dict_append_basic(&dict, "TimeUpdates",
128 DBUS_TYPE_STRING, &str);
130 str = timezone_updates2string(timezone_updates_config);
132 connman_dbus_dict_append_basic(&dict, "TimezoneUpdates",
133 DBUS_TYPE_STRING, &str);
135 connman_dbus_dict_append_array(&dict, "Timeservers",
136 DBUS_TYPE_STRING, append_timeservers, NULL);
138 connman_dbus_dict_close(&array, &dict);
143 static DBusMessage *set_property(DBusConnection *conn,
144 DBusMessage *msg, void *data)
146 DBusMessageIter iter, value;
150 DBG("conn %p", conn);
152 if (dbus_message_iter_init(msg, &iter) == FALSE)
153 return __connman_error_invalid_arguments(msg);
155 dbus_message_iter_get_basic(&iter, &name);
156 dbus_message_iter_next(&iter);
157 dbus_message_iter_recurse(&iter, &value);
159 type = dbus_message_iter_get_arg_type(&value);
161 if (g_str_equal(name, "TimeUpdates") == TRUE) {
163 enum time_updates newval;
165 if (type != DBUS_TYPE_STRING)
166 return __connman_error_invalid_arguments(msg);
168 dbus_message_iter_get_basic(&value, &strval);
169 newval = string2time_updates(strval);
171 if (newval == TIME_UPDATES_UNKNOWN)
172 return __connman_error_invalid_arguments(msg);
174 if (newval == time_updates_config)
175 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
177 time_updates_config = newval;
179 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
180 CONNMAN_CLOCK_INTERFACE, "TimeUpdates",
181 DBUS_TYPE_STRING, &strval);
182 } else if (g_str_equal(name, "TimezoneUpdates") == TRUE) {
184 enum timezone_updates newval;
186 if (type != DBUS_TYPE_STRING)
187 return __connman_error_invalid_arguments(msg);
189 dbus_message_iter_get_basic(&value, &strval);
190 newval = string2timezone_updates(strval);
192 if (newval == TIMEZONE_UPDATES_UNKNOWN)
193 return __connman_error_invalid_arguments(msg);
195 if (newval == timezone_updates_config)
196 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
198 timezone_updates_config = newval;
200 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
201 CONNMAN_CLOCK_INTERFACE, "TimezoneUpdates",
202 DBUS_TYPE_STRING, &strval);
203 } else if (g_str_equal(name, "Timeservers") == TRUE) {
204 DBusMessageIter entry;
207 if (type != DBUS_TYPE_ARRAY)
208 return __connman_error_invalid_arguments(msg);
210 str = g_string_new(NULL);
212 return __connman_error_invalid_arguments(msg);
214 dbus_message_iter_recurse(&value, &entry);
216 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
219 dbus_message_iter_get_basic(&entry, &val);
220 dbus_message_iter_next(&entry);
223 g_string_append_printf(str, " %s", val);
225 g_string_append(str, val);
228 g_strfreev(timeservers_config);
231 timeservers_config = g_strsplit_set(str->str, " ", 0);
233 timeservers_config = NULL;
235 g_string_free(str, TRUE);
237 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
238 CONNMAN_CLOCK_INTERFACE, "Timeservers",
239 DBUS_TYPE_STRING, append_timeservers, NULL);
241 return __connman_error_invalid_property(msg);
243 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
246 static GDBusMethodTable clock_methods[] = {
247 { "GetProperties", "", "a{sv}", get_properties },
248 { "SetProperty", "sv", "", set_property },
252 static GDBusSignalTable clock_signals[] = {
253 { "PropertyChanged", "sv" },
257 static DBusConnection *connection = NULL;
259 int __connman_clock_init(void)
263 connection = connman_dbus_get_connection();
264 if (connection == NULL)
267 g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
268 CONNMAN_CLOCK_INTERFACE,
269 clock_methods, clock_signals,
275 void __connman_clock_cleanup(void)
279 if (connection == NULL)
282 g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
283 CONNMAN_CLOCK_INTERFACE);
285 dbus_connection_unref(connection);
287 g_strfreev(timeservers_config);