Modified NTP to work with NITZ
[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         g_key_file_free(keyfile);
44 }
45
46 static gboolean __netconfig_clock_clear_timeserver_timer(gpointer data)
47 {
48         INFO("Clear NTP server");
49
50         __netconfig_clock_clear_timeserver();
51
52         return FALSE;
53 }
54
55 static void __netconfig_clock_set_timeserver(const char *server)
56 {
57         GVariant* reply = NULL;
58         const char param0[] = "Timeservers";
59         GVariant *params = NULL;
60         GVariantBuilder *builder;
61
62         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
63         g_variant_builder_add(builder, "s", server);
64
65         params = g_variant_new("(sv)", param0, g_variant_builder_end(builder));
66         g_variant_builder_unref(builder);
67
68         reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
69                         CONNMAN_MANAGER_PATH, CONNMAN_CLOCK_INTERFACE,
70                         "SetProperty", params);
71
72         if (reply == NULL)
73                 ERR("Failed to configure NTP server");
74         else
75                 g_variant_unref(reply);
76
77         return;
78 }
79
80 static void __netconfig_clock_unset_timeserver(void)
81 {
82         GVariant* reply = NULL;
83         const char param0[] = "Timeservers";
84         const char server[] = "";
85         GVariant *params = NULL;
86         GVariantBuilder *builder;
87
88         builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
89         g_variant_builder_add(builder, "s", server);
90
91         params = g_variant_new("(sv)", param0, g_variant_builder_end(builder));
92         g_variant_builder_unref(builder);
93
94         reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
95                         CONNMAN_MANAGER_PATH, CONNMAN_CLOCK_INTERFACE,
96                         "SetProperty", params);
97
98         if (reply == NULL)
99                 ERR("Failed to clear NTP server");
100         else
101                 g_variant_unref(reply);
102
103         return;
104 }
105
106 static void __netconfig_set_timeserver(void)
107 {
108         guint timeserver_clear_timer = 0;
109         int nitz_updated = 0;
110
111         netconfig_vconf_get_int(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT, &nitz_updated);
112
113         if (nitz_updated != 0)
114                 return;
115
116         __netconfig_clock_set_timeserver((const char *)NTP_SERVER);
117
118         netconfig_start_timer_seconds(5, __netconfig_clock_clear_timeserver_timer,
119                         NULL, &timeserver_clear_timer);
120 }
121
122 static void __network_changed_cb(keynode_t *node, void *user_data)
123 {
124         int status = VCONFKEY_NETWORK_OFF;
125         gboolean automatic_time_update = 0;
126
127         if (node != NULL)
128                 status = vconf_keynode_get_int(node);
129         else
130                 netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &status);
131
132         if (status == VCONFKEY_NETWORK_OFF)
133                 return;
134
135         netconfig_vconf_get_bool(
136                         VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL,
137                         &automatic_time_update);
138
139         if (automatic_time_update == FALSE) {
140                 INFO("Automatic time update is not set (%d)", automatic_time_update);
141                 return;
142         }
143
144         __netconfig_set_timeserver();
145 }
146
147 static void __automatic_time_update_changed_cb(keynode_t *node, void *user_data)
148 {
149         int status = VCONFKEY_NETWORK_OFF;
150         gboolean automatic_time_update = FALSE;
151
152         if (node != NULL)
153                 automatic_time_update = vconf_keynode_get_bool(node);
154         else
155                 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL, &automatic_time_update);
156
157         if (automatic_time_update == FALSE) {
158                 INFO("Automatic time update is changed to 'FALSE'");
159                 __netconfig_clock_unset_timeserver();
160                 return;
161         }
162
163         netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &status);
164
165          if (status == VCONFKEY_NETWORK_OFF) {
166                 INFO("There is no connection");
167                 return;
168          }
169
170         __netconfig_set_timeserver();
171 }
172
173 static void __nitz_event_cb(keynode_t *node, void *user_data)
174 {
175         int nitz_updated = 0;
176
177         if (node != NULL)
178                 nitz_updated = vconf_keynode_get_int(node);
179         else
180                 netconfig_vconf_get_int(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT, &nitz_updated);
181
182         if (nitz_updated != 0) {
183                 INFO("Time is updated by NITZ");
184                 __netconfig_clock_unset_timeserver();
185                 return;
186         }
187 }
188
189 void netconfig_clock_init(void)
190 {
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);
198 }
199
200 void netconfig_clock_deinit(void)
201 {
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,
205                         __nitz_event_cb);
206         vconf_ignore_key_changed(VCONFKEY_NETWORK_STATUS,
207                         __network_changed_cb);
208 }