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);
45 static gboolean __netconfig_clock_clear_timeserver_timer(gpointer data)
47 INFO("Clear NTP server");
49 __netconfig_clock_clear_timeserver();
54 static void __netconfig_clock_set_timeserver(const char *server)
56 GVariant* reply = NULL;
57 const char param0[] = "Timeservers";
58 GVariant *params = NULL;
59 GVariantBuilder *builder;
61 builder = g_variant_builder_new(G_VARIANT_TYPE ("as"));
62 g_variant_builder_add(builder, "s", server);
64 params = g_variant_new("(sv)",param0, g_variant_builder_end(builder));
65 g_variant_builder_unref(builder);
67 reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
68 CONNMAN_MANAGER_PATH, CONNMAN_CLOCK_INTERFACE,
69 "SetProperty", params);
72 ERR("Failed to configure NTP server");
74 g_variant_unref(reply);
79 static void __netconfig_set_timeserver(void)
81 guint timeserver_clear_timer = 0;
83 __netconfig_clock_set_timeserver((const char *)NTP_SERVER);
85 netconfig_start_timer_seconds(5, __netconfig_clock_clear_timeserver_timer,
86 NULL, ×erver_clear_timer);
89 static void __netconfig_clock(
90 enum netconfig_wifi_service_state state, void *user_data)
92 gboolean automatic_time_update = 0;
94 if (state != NETCONFIG_WIFI_CONNECTED)
98 VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL,
99 &automatic_time_update);
101 if (automatic_time_update == FALSE) {
102 INFO("Automatic time update is not set (%d)", automatic_time_update);
106 __netconfig_set_timeserver();
109 static struct netconfig_wifi_state_notifier netconfig_clock_notifier = {
110 .netconfig_wifi_state_changed = __netconfig_clock,
114 static void __automatic_time_update_changed_cb(keynode_t *node, void *user_data)
116 gboolean automatic_time_update = FALSE;
117 enum netconfig_wifi_service_state wifi_state = NETCONFIG_WIFI_UNKNOWN;
120 automatic_time_update = vconf_keynode_get_bool(node);
122 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 = netconfig_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 netconfig_wifi_state_notifier_register(&netconfig_clock_notifier);
149 void netconfig_clock_deinit(void)
151 netconfig_wifi_state_notifier_unregister(&netconfig_clock_notifier);