Remove unmatched quotes
[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_set_timeserver(void)
81 {
82         guint timeserver_clear_timer = 0;
83
84         __netconfig_clock_set_timeserver((const char *)NTP_SERVER);
85
86         netconfig_start_timer_seconds(5, __netconfig_clock_clear_timeserver_timer,
87                         NULL, &timeserver_clear_timer);
88 }
89
90 static void __netconfig_clock(
91                 wifi_service_state_e state, void *user_data)
92 {
93         gboolean automatic_time_update = 0;
94
95         if (state != NETCONFIG_WIFI_CONNECTED)
96                 return;
97
98         netconfig_vconf_get_bool(
99                         VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL,
100                         &automatic_time_update);
101
102         if (automatic_time_update == FALSE) {
103                 INFO("Automatic time update is not set (%d)", automatic_time_update);
104                 return;
105         }
106
107         __netconfig_set_timeserver();
108 }
109
110 static wifi_state_notifier netconfig_clock_notifier = {
111                 .wifi_state_changed = __netconfig_clock,
112                 .user_data = NULL,
113 };
114
115 static void __automatic_time_update_changed_cb(keynode_t *node, void *user_data)
116 {
117         gboolean automatic_time_update = FALSE;
118         wifi_service_state_e wifi_state = NETCONFIG_WIFI_UNKNOWN;
119
120         if (node != NULL)
121                 automatic_time_update = vconf_keynode_get_bool(node);
122         else
123                 netconfig_vconf_get_bool(VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL, &automatic_time_update);
124
125         if (automatic_time_update == FALSE) {
126                 INFO("Automatic time update is changed to 'FALSE'");
127                 return;
128         }
129
130          wifi_state = 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         wifi_state_notifier_register(&netconfig_clock_notifier);
147 }
148
149 void netconfig_clock_deinit(void)
150 {
151         wifi_state_notifier_unregister(&netconfig_clock_notifier);
152 }