Tizen 2.1 base
[platform/core/connectivity/net-config.git] / src / network-clock.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2012-2013 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         DBusMessage* reply = NULL;
57         char param1[] = "string:Timeservers";
58         char *param2 = NULL;
59         char *param_array[] = {NULL, NULL, NULL};
60
61         param2 = g_strdup_printf("variant:array:string:%s", server);
62
63         param_array[0] = param1;
64         param_array[1] = param2;
65
66         reply = netconfig_invoke_dbus_method(CONNMAN_SERVICE,
67                         CONNMAN_MANAGER_PATH, CONNMAN_CLOCK_INTERFACE,
68                         "SetProperty", param_array);
69         if (reply == NULL) {
70                 ERR("Failed to configure NTP server");
71                 return;
72         }
73
74         dbus_message_unref(reply);
75 }
76
77 static void __netconfig_clock(
78                 enum netconfig_wifi_service_state state, void *user_data)
79 {
80         gboolean automatic_time_update = 0;
81         guint timeserver_clear_timer = 0;
82
83         if (state != NETCONFIG_WIFI_CONNECTED)
84                 return;
85
86         vconf_get_bool(
87                         VCONFKEY_SETAPPL_STATE_AUTOMATIC_TIME_UPDATE_BOOL,
88                         &automatic_time_update);
89
90         if (automatic_time_update == FALSE) {
91                 INFO("Automatic time update is not set (%d)", automatic_time_update);
92                 return;
93         }
94
95         __netconfig_clock_set_timeserver((const char *)NTP_SERVER);
96
97         netconfig_start_timer_seconds(5, __netconfig_clock_clear_timeserver_timer,
98                         NULL, &timeserver_clear_timer);
99 }
100
101 static struct netconfig_wifi_state_notifier netconfig_clock_notifier = {
102                 .netconfig_wifi_state_changed = __netconfig_clock,
103                 .user_data = NULL,
104 };
105
106 void netconfig_clock_init(void)
107 {
108         netconfig_wifi_state_notifier_register(&netconfig_clock_notifier);
109 }
110
111 void netconfig_clock_deinit(void)
112 {
113         netconfig_wifi_state_notifier_unregister(&netconfig_clock_notifier);
114 }