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_set_timeserver(void)
82 guint timeserver_clear_timer = 0;
84 __netconfig_clock_set_timeserver((const char *)NTP_SERVER);
86 netconfig_start_timer_seconds(5, __netconfig_clock_clear_timeserver_timer,
87 NULL, ×erver_clear_timer);
90 static void __netconfig_clock(
91 wifi_service_state_e state, void *user_data)
93 gboolean automatic_time_update = 0;
95 if (state != NETCONFIG_WIFI_CONNECTED)
99 VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL,
100 &automatic_time_update);
102 if (automatic_time_update == FALSE) {
103 INFO("Automatic time update is not set (%d)", automatic_time_update);
107 __netconfig_set_timeserver();
110 static wifi_state_notifier netconfig_clock_notifier = {
111 .wifi_state_changed = __netconfig_clock,
115 static void __automatic_time_update_changed_cb(keynode_t *node, void *user_data)
117 gboolean automatic_time_update = FALSE;
118 wifi_service_state_e wifi_state = NETCONFIG_WIFI_UNKNOWN;
121 automatic_time_update = vconf_keynode_get_bool(node);
123 vconf_get_bool(VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL, &automatic_time_update);
125 if (automatic_time_update == FALSE) {
126 INFO("Automatic time update is changed to 'FALSE'");
130 wifi_state = wifi_state_get_service_state();
132 if (wifi_state != NETCONFIG_WIFI_CONNECTED) {
133 INFO("WiFi state is not NETCONFIG_WIFI_CONNECTED");
137 __netconfig_set_timeserver();
140 void netconfig_clock_init(void)
142 INFO("netconfig_clock_init is called");
143 vconf_notify_key_changed(VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL,
144 __automatic_time_update_changed_cb, NULL);
146 wifi_state_notifier_register(&netconfig_clock_notifier);
149 void netconfig_clock_deinit(void)
151 wifi_state_notifier_unregister(&netconfig_clock_notifier);