2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <vconf-keys.h>
26 #include "wifi-state.h"
28 #define NTP_SERVER "pool.ntp.org"
29 #define CONNMAN_GLOBAL_SETTING "/var/lib/connman/settings"
30 #define TIME_UPDATE_CONFIG_AUTO "auto"
31 #define TIME_UPDATE_CONFIG_MANUAL "manual"
33 static Clock *clock_object = NULL;
35 Clock *get_clock_object(void)
40 static void __netconfig_clock_set_timeserver(const char *server)
42 GVariant* reply = NULL;
43 const char param0[] = "Timeservers";
44 GVariant *params = NULL;
45 GVariantBuilder *builder;
47 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
48 g_variant_builder_add(builder, "s", server);
50 params = g_variant_new("(sv)", param0, g_variant_builder_end(builder));
51 g_variant_builder_unref(builder);
53 reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
54 CONNMAN_MANAGER_PATH, CONNMAN_CLOCK_INTERFACE,
55 "SetProperty", params);
58 ERR("Failed to configure NTP server");
60 g_variant_unref(reply);
65 static void __netconfig_clock_unset_timeserver(void)
67 GVariant* reply = NULL;
68 const char param0[] = "Timeservers";
69 const char server[] = "";
70 GVariant *params = NULL;
71 GVariantBuilder *builder;
73 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
74 g_variant_builder_add(builder, "s", server);
76 params = g_variant_new("(sv)", param0, g_variant_builder_end(builder));
77 g_variant_builder_unref(builder);
79 reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
80 CONNMAN_MANAGER_PATH, CONNMAN_CLOCK_INTERFACE,
81 "SetProperty", params);
84 ERR("Failed to clear NTP server");
86 g_variant_unref(reply);
91 static void __netconfig_clock_set_time_update_policy(const char *time_update_method)
93 GVariant* reply = NULL;
95 reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
96 CONNMAN_MANAGER_PATH, CONNMAN_CLOCK_INTERFACE,
97 "SetProperty", g_variant_new("(sv)", "TimeUpdates",
98 g_variant_new_string(time_update_method)));
101 ERR("Failed to set Time Update Policy");
103 g_variant_unref(reply);
108 static void __automatic_time_update_changed_cb(keynode_t *node, void *user_data)
110 int status = VCONFKEY_NETWORK_OFF;
111 gboolean automatic_time_update = FALSE;
114 automatic_time_update = vconf_keynode_get_bool(node);
116 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL, &automatic_time_update);
118 if (automatic_time_update == FALSE) {
119 INFO("Automatic time update is changed to 'FALSE'");
120 __netconfig_clock_set_time_update_policy((const char *)TIME_UPDATE_CONFIG_MANUAL);
124 netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &status);
126 if (status == VCONFKEY_NETWORK_OFF) {
127 INFO("There is no connection");
131 __netconfig_clock_set_time_update_policy((const char *)TIME_UPDATE_CONFIG_AUTO);
134 static void __nitz_event_cb(keynode_t *node, void *user_data)
136 int nitz_updated = 0;
139 nitz_updated = vconf_keynode_get_int(node);
141 netconfig_vconf_get_int(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT, &nitz_updated);
143 if (nitz_updated != 0) {
144 INFO("Time is updated by NITZ");
145 __netconfig_clock_set_time_update_policy((const char *)TIME_UPDATE_CONFIG_MANUAL);
150 void netconfig_clock_init(void)
152 gboolean automatic_time_update = FALSE;
154 INFO("netconfig_clock_init is called");
155 vconf_notify_key_changed(VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL,
156 __automatic_time_update_changed_cb, NULL);
157 vconf_notify_key_changed(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT,
158 __nitz_event_cb, NULL);
160 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL, &automatic_time_update);
162 if (automatic_time_update == FALSE)
163 __netconfig_clock_set_time_update_policy((const char *)TIME_UPDATE_CONFIG_MANUAL);
165 __netconfig_clock_set_time_update_policy((const char *)TIME_UPDATE_CONFIG_AUTO);
168 void netconfig_clock_deinit(void)
170 vconf_ignore_key_changed(VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL,
171 __automatic_time_update_changed_cb);
172 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT,
176 gboolean handle_set_ntp_server(Clock *object, GDBusMethodInvocation *invocation,
180 netconfig_error_dbus_method_return(invocation,
181 NETCONFIG_ERROR_INTERNAL, "SetNtpServer Failed");
185 __netconfig_clock_set_timeserver(server);
187 DBG("NTP Server:%s set successfully",server);
189 clock_complete_set_ntp_server(object, invocation);
194 gboolean handle_unset_ntp_server(Clock *object, GDBusMethodInvocation *invocation)
196 __netconfig_clock_unset_timeserver();
198 DBG("NTP Server unset successfully");
200 clock_complete_unset_ntp_server(object, invocation);
205 /*****************************
206 * Initializations Functions *
207 ****************************/
208 void clock_object_create_and_init(void)
210 DBG("Create clock object.");
211 GDBusInterfaceSkeleton *interface_clock = NULL;
212 GDBusConnection *connection = NULL;
213 GDBusObjectManagerServer *server = netdbus_get_clock_manager();
217 connection = netdbus_get_connection();
218 g_dbus_object_manager_server_set_connection(server, connection);
221 clock_object = clock_skeleton_new();
222 interface_clock = G_DBUS_INTERFACE_SKELETON(clock_object);
225 g_signal_connect(clock_object, "handle-set-ntp-server",
226 G_CALLBACK(handle_set_ntp_server), NULL);
228 /* Unset NTP Server */
229 g_signal_connect(clock_object, "handle-unset-ntp-server",
230 G_CALLBACK(handle_unset_ntp_server), NULL);
232 if (!g_dbus_interface_skeleton_export(interface_clock, connection,
233 NETCONFIG_CLOCK_PATH, NULL)) {
234 ERR("Export NETCONFIG_CLOCK_PATH for clock failed");
240 void clock_object_deinit(void)
242 DBG("Deinit clock object.");
244 g_object_unref(clock_object);