Base Code merged to SPIN 2.4
[platform/core/connectivity/net-config.git] / src / network-clock.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include <vconf.h>
21 #include <vconf-keys.h>
22
23 #include "log.h"
24 #include "util.h"
25 #include "netdbus.h"
26 #include "wifi-state.h"
27
28 #define NTP_SERVER      "pool.ntp.org"
29 #define CONNMAN_GLOBAL_SETTING  "/var/lib/connman/settings"
30
31 static void __netconfig_clock_clear_timeserver(void)
32 {
33         GKeyFile *keyfile = NULL;
34
35         keyfile = netconfig_keyfile_load(CONNMAN_GLOBAL_SETTING);
36
37         if (keyfile == NULL)
38                 return;
39
40         g_key_file_remove_key(keyfile, "global", "Timeservers", NULL);
41
42         netconfig_keyfile_save(keyfile, CONNMAN_GLOBAL_SETTING);
43 }
44
45 static gboolean __netconfig_clock_clear_timeserver_timer(gpointer data)
46 {
47         INFO("Clear NTP server");
48
49         __netconfig_clock_clear_timeserver();
50
51         return FALSE;
52 }
53
54 static void __netconfig_clock_set_timeserver(const char *server)
55 {
56         GVariant* reply = NULL;
57         const char param0[] = "Timeservers";
58         GVariant *params = NULL;
59         GVariantBuilder *builder;
60
61         builder = g_variant_builder_new(G_VARIANT_TYPE ("as"));
62         g_variant_builder_add(builder, "s", server);
63
64         params = g_variant_new("(sv)",param0, g_variant_builder_end(builder));
65         g_variant_builder_unref(builder);
66
67         reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
68                         CONNMAN_MANAGER_PATH, CONNMAN_CLOCK_INTERFACE,
69                         "SetProperty", params);
70
71         if (reply == NULL)
72                 ERR("Failed to configure NTP server");
73         else
74                 g_variant_unref(reply);
75
76         return;
77 }
78
79 static void __netconfig_set_timeserver(void)
80 {
81         guint timeserver_clear_timer = 0;
82
83         __netconfig_clock_set_timeserver((const char *)NTP_SERVER);
84
85         netconfig_start_timer_seconds(5, __netconfig_clock_clear_timeserver_timer,
86                         NULL, &timeserver_clear_timer);
87 }
88
89 static void __netconfig_clock(
90                 enum netconfig_wifi_service_state state, void *user_data)
91 {
92         gboolean automatic_time_update = 0;
93
94         if (state != NETCONFIG_WIFI_CONNECTED)
95                 return;
96
97         vconf_get_bool(
98                         VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL,
99                         &automatic_time_update);
100
101         if (automatic_time_update == FALSE) {
102                 INFO("Automatic time update is not set (%d)", automatic_time_update);
103                 return;
104         }
105
106         __netconfig_set_timeserver();
107 }
108
109 static struct netconfig_wifi_state_notifier netconfig_clock_notifier = {
110                 .netconfig_wifi_state_changed = __netconfig_clock,
111                 .user_data = NULL,
112 };
113
114 static void __automatic_time_update_changed_cb(keynode_t *node, void *user_data)
115 {
116         gboolean automatic_time_update = FALSE;
117         enum netconfig_wifi_service_state wifi_state = NETCONFIG_WIFI_UNKNOWN;
118
119         if (node != NULL) {
120                 automatic_time_update = vconf_keynode_get_bool(node);
121         } else {
122                 vconf_get_bool(VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL, &automatic_time_update);
123         }
124
125         if (automatic_time_update == FALSE) {
126                 INFO("Automatic time update is changed to 'FALSE'");
127                 return;
128         }
129
130          wifi_state = netconfig_wifi_state_get_service_state();
131
132          if (wifi_state != NETCONFIG_WIFI_CONNECTED) {
133                 INFO("WiFi state is not NETCONFIG_WIFI_CONNECTED");
134                 return;
135          }
136
137         __netconfig_set_timeserver();
138 }
139
140 void netconfig_clock_init(void)
141 {
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);
145
146         netconfig_wifi_state_notifier_register(&netconfig_clock_notifier);
147 }
148
149 void netconfig_clock_deinit(void)
150 {
151         netconfig_wifi_state_notifier_unregister(&netconfig_clock_notifier);
152 }