Create device_data if not found it before use
[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         int nitz_updated = 0;
109
110         netconfig_vconf_get_int(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT, &nitz_updated);
111
112         if (nitz_updated != 0)
113                 return;
114
115         __netconfig_clock_set_timeserver((const char *)NTP_SERVER);
116 }
117
118 static void __network_changed_cb(keynode_t *node, void *user_data)
119 {
120         int status = VCONFKEY_NETWORK_OFF;
121         gboolean automatic_time_update = 0;
122
123         if (node != NULL)
124                 status = vconf_keynode_get_int(node);
125         else
126                 netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &status);
127
128         if (status == VCONFKEY_NETWORK_OFF)
129                 return;
130
131         netconfig_vconf_get_bool(
132                         VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL,
133                         &automatic_time_update);
134
135         if (automatic_time_update == FALSE) {
136                 INFO("Automatic time update is not set (%d)", automatic_time_update);
137                 return;
138         }
139
140         __netconfig_set_timeserver();
141 }
142
143 static void __automatic_time_update_changed_cb(keynode_t *node, void *user_data)
144 {
145         int status = VCONFKEY_NETWORK_OFF;
146         gboolean automatic_time_update = FALSE;
147
148         if (node != NULL)
149                 automatic_time_update = vconf_keynode_get_bool(node);
150         else
151                 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL, &automatic_time_update);
152
153         if (automatic_time_update == FALSE) {
154                 INFO("Automatic time update is changed to 'FALSE'");
155                 __netconfig_clock_unset_timeserver();
156                 return;
157         }
158
159         netconfig_vconf_get_int(VCONFKEY_NETWORK_STATUS, &status);
160
161          if (status == VCONFKEY_NETWORK_OFF) {
162                 INFO("There is no connection");
163                 return;
164          }
165
166         __netconfig_set_timeserver();
167 }
168
169 static void __nitz_event_cb(keynode_t *node, void *user_data)
170 {
171         int nitz_updated = 0;
172
173         if (node != NULL)
174                 nitz_updated = vconf_keynode_get_int(node);
175         else
176                 netconfig_vconf_get_int(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT, &nitz_updated);
177
178         if (nitz_updated != 0) {
179                 INFO("Time is updated by NITZ");
180                 __netconfig_clock_unset_timeserver();
181                 return;
182         }
183 }
184
185 void netconfig_clock_init(void)
186 {
187         gboolean automatic_time_update = FALSE;
188
189         INFO("netconfig_clock_init is called");
190         vconf_notify_key_changed(VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL,
191                         __automatic_time_update_changed_cb, NULL);
192         vconf_notify_key_changed(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT,
193                         __nitz_event_cb, NULL);
194         vconf_notify_key_changed(VCONFKEY_NETWORK_STATUS,
195                         __network_changed_cb, NULL);
196
197         netconfig_vconf_get_bool(VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL, &automatic_time_update);
198
199         if (automatic_time_update == FALSE)
200                 __netconfig_clock_clear_timeserver_timer(NULL);
201 }
202
203 void netconfig_clock_deinit(void)
204 {
205         vconf_ignore_key_changed(VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL,
206                         __automatic_time_update_changed_cb);
207         vconf_ignore_key_changed(VCONFKEY_TELEPHONY_NITZ_EVENT_GMT,
208                         __nitz_event_cb);
209         vconf_ignore_key_changed(VCONFKEY_NETWORK_STATUS,
210                         __network_changed_cb);
211 }