clock: Add support for timeserver modifications
[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 <gdbus.h>
27
28 #include "connman.h"
29
30 static char **timeservers_config = NULL;
31
32 static void append_timeservers(DBusMessageIter *iter, void *user_data)
33 {
34         int i;
35
36         if (timeservers_config == NULL)
37                 return;
38
39         for (i = 0; timeservers_config[i] != NULL; i++) {
40                 dbus_message_iter_append_basic(iter,
41                                 DBUS_TYPE_STRING, &timeservers_config[i]);
42         }
43 }
44
45 static DBusMessage *get_properties(DBusConnection *conn,
46                                         DBusMessage *msg, void *data)
47 {
48         DBusMessage *reply;
49         DBusMessageIter array, dict;
50
51         DBG("conn %p", conn);
52
53         reply = dbus_message_new_method_return(msg);
54         if (reply == NULL)
55                 return NULL;
56
57         dbus_message_iter_init_append(reply, &array);
58
59         connman_dbus_dict_open(&array, &dict);
60
61         connman_dbus_dict_append_array(&dict, "Timeservers",
62                                 DBUS_TYPE_STRING, append_timeservers, NULL);
63
64         connman_dbus_dict_close(&array, &dict);
65
66         return reply;
67 }
68
69 static DBusMessage *set_property(DBusConnection *conn,
70                                         DBusMessage *msg, void *data)
71 {
72         DBusMessageIter iter, value;
73         const char *name;
74         int type;
75
76         DBG("conn %p", conn);
77
78         if (dbus_message_iter_init(msg, &iter) == FALSE)
79                 return __connman_error_invalid_arguments(msg);
80
81         dbus_message_iter_get_basic(&iter, &name);
82         dbus_message_iter_next(&iter);
83         dbus_message_iter_recurse(&iter, &value);
84
85         type = dbus_message_iter_get_arg_type(&value);
86
87         if (g_str_equal(name, "Timeservers") == TRUE) {
88                 DBusMessageIter entry;
89                 GString *str;
90
91                 if (type != DBUS_TYPE_ARRAY)
92                         return __connman_error_invalid_arguments(msg);
93
94                 str = g_string_new(NULL);
95                 if (str == NULL)
96                         return __connman_error_invalid_arguments(msg);
97
98                 dbus_message_iter_recurse(&value, &entry);
99
100                 while (dbus_message_iter_get_arg_type(&entry) == DBUS_TYPE_STRING) {
101                         const char *val;
102
103                         dbus_message_iter_get_basic(&entry, &val);
104                         dbus_message_iter_next(&entry);
105
106                         if (str->len > 0)
107                                 g_string_append_printf(str, " %s", val);
108                         else
109                                 g_string_append(str, val);
110                 }
111
112                 g_strfreev(timeservers_config);
113
114                 if (str->len > 0)
115                         timeservers_config = g_strsplit_set(str->str, " ", 0);
116                 else
117                         timeservers_config = NULL;
118
119                 g_string_free(str, TRUE);
120
121                 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
122                                 CONNMAN_CLOCK_INTERFACE, "Timeservers",
123                                 DBUS_TYPE_STRING, append_timeservers, NULL);
124         } else
125                 return __connman_error_invalid_property(msg);
126
127         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
128 }
129
130 static GDBusMethodTable clock_methods[] = {
131         { "GetProperties", "",   "a{sv}", get_properties },
132         { "SetProperty",   "sv", "",      set_property   },
133         { },
134 };
135
136 static GDBusSignalTable clock_signals[] = {
137         { "PropertyChanged", "sv" },
138         { },
139 };
140
141 static DBusConnection *connection = NULL;
142
143 int __connman_clock_init(void)
144 {
145         DBG("");
146
147         connection = connman_dbus_get_connection();
148         if (connection == NULL)
149                 return -1;
150
151         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
152                                                 CONNMAN_CLOCK_INTERFACE,
153                                                 clock_methods, clock_signals,
154                                                 NULL, NULL, NULL);
155
156         return 0;
157 }
158
159 void __connman_clock_cleanup(void)
160 {
161         DBG("");
162
163         if (connection == NULL)
164                 return;
165
166         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
167                                                 CONNMAN_CLOCK_INTERFACE);
168
169         dbus_connection_unref(connection);
170
171         g_strfreev(timeservers_config);
172 }