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"
31 static void __netconfig_clock_clear_timeserver(void)
33 GKeyFile *keyfile = NULL;
35 keyfile = netconfig_keyfile_load(CONNMAN_GLOBAL_SETTING);
40 g_key_file_remove_key(keyfile, "global", "Timeservers", NULL);
42 netconfig_keyfile_save(keyfile, CONNMAN_GLOBAL_SETTING);
43 g_key_file_free(keyfile);
46 static gboolean __netconfig_clock_clear_timeserver_timer(gpointer data)
48 INFO("Clear NTP server");
50 __netconfig_clock_clear_timeserver();
55 static void __netconfig_clock_set_timeserver(const char *server)
57 GVariant* reply = NULL;
58 const char param0[] = "Timeservers";
59 GVariant *params = NULL;
60 GVariantBuilder *builder;
62 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
63 g_variant_builder_add(builder, "s", server);
65 params = g_variant_new("(sv)", param0, g_variant_builder_end(builder));
66 g_variant_builder_unref(builder);
68 reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
69 CONNMAN_MANAGER_PATH, CONNMAN_CLOCK_INTERFACE,
70 "SetProperty", params);
73 ERR("Failed to configure NTP server");
75 g_variant_unref(reply);
80 static void __netconfig_clock_unset_timeserver(void)
82 GVariant* reply = NULL;
83 const char param0[] = "Timeservers";
84 const char server[] = "";
85 GVariant *params = NULL;
86 GVariantBuilder *builder;
88 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
89 g_variant_builder_add(builder, "s", server);
91 params = g_variant_new("(sv)", param0, g_variant_builder_end(builder));
92 g_variant_builder_unref(builder);
94 reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
95 CONNMAN_MANAGER_PATH, CONNMAN_CLOCK_INTERFACE,
96 "SetProperty", params);
99 ERR("Failed to clear NTP server");
101 g_variant_unref(reply);
106 static void __netconfig_set_timeserver(void)
108 guint timeserver_clear_timer = 0;
109 int nitz_updated = 0;
111 netconfig_vconf_get_int(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT, &nitz_updated);
113 if (nitz_updated != 0)
116 __netconfig_clock_set_timeserver((const char *)NTP_SERVER);
118 netconfig_start_timer_seconds(5, __netconfig_clock_clear_timeserver_timer,
119 NULL, ×erver_clear_timer);
122 static void __network_changed_cb(keynode_t *node, void *user_data)
124 int status = VCONFKEY_NETWORK_OFF;
125 gboolean automatic_time_update = 0;
128 status = vconf_keynode_get_int(node);
130 netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &status);
132 if (status == VCONFKEY_NETWORK_OFF)
135 netconfig_vconf_get_bool(
136 VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL,
137 &automatic_time_update);
139 if (automatic_time_update == FALSE) {
140 INFO("Automatic time update is not set (%d)", automatic_time_update);
144 __netconfig_set_timeserver();
147 static void __automatic_time_update_changed_cb(keynode_t *node, void *user_data)
149 int status = VCONFKEY_NETWORK_OFF;
150 gboolean automatic_time_update = FALSE;
153 automatic_time_update = vconf_keynode_get_bool(node);
155 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL, &automatic_time_update);
157 if (automatic_time_update == FALSE) {
158 INFO("Automatic time update is changed to 'FALSE'");
159 __netconfig_clock_unset_timeserver();
163 netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &status);
165 if (status == VCONFKEY_NETWORK_OFF) {
166 INFO("There is no connection");
170 __netconfig_set_timeserver();
173 static void __nitz_event_cb(keynode_t *node, void *user_data)
175 int nitz_updated = 0;
178 nitz_updated = vconf_keynode_get_int(node);
180 netconfig_vconf_get_int(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT, &nitz_updated);
182 if (nitz_updated != 0) {
183 INFO("Time is updated by NITZ");
184 __netconfig_clock_unset_timeserver();
189 void netconfig_clock_init(void)
191 INFO("netconfig_clock_init is called");
192 vconf_notify_key_changed(VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL,
193 __automatic_time_update_changed_cb, NULL);
194 vconf_notify_key_changed(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT,
195 __nitz_event_cb, NULL);
196 vconf_notify_key_changed(VCONFKEY_NETWORK_STATUS,
197 __network_changed_cb, NULL);
200 void netconfig_clock_deinit(void)
202 vconf_ignore_key_changed(VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL,
203 __automatic_time_update_changed_cb);
204 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT,
206 vconf_ignore_key_changed(VCONFKEY_NETWORK_STATUS,
207 __network_changed_cb);