Rectify gvariant type for frequency
[platform/core/connectivity/net-config.git] / src / wifi-background-scan.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 <glib.h>
21 #include <vconf.h>
22 #include <vconf-keys.h>
23
24 #include "log.h"
25 #include "util.h"
26 #include "netdbus.h"
27 #include "wifi-state.h"
28 #include "wifi-scan.h"
29 #include "wifi-background-scan.h"
30
31 void netconfig_wifi_bgscan_start(const char *interface_name, gboolean immediate_scan)
32 {
33         wifi_tech_state_e wifi_tech_state;
34         wifi_service_state_e wifi_service_state;
35         int ug_state = VCONFKEY_WIFI_UG_RUN_STATE_OFF;
36         guint bg_mode = WIFI_BGSCAN_MODE_EXPONENTIAL;
37
38         netconfig_vconf_get_int(VCONFKEY_WIFI_UG_RUN_STATE, &ug_state);
39
40         if (netconfig_wifi_bgscan_get_timer_id(interface_name) > 0)
41                 netconfig_wifi_bgscan_stop_timer(interface_name);
42
43         wifi_tech_state = wifi_state_get_technology_state(interface_name);
44         wifi_service_state = wifi_state_get_service_state(interface_name);
45         DBG("[%s] Wi-Fi tech state [%s] service state [%s]", interface_name,
46                 _convert_wifi_technology_state_to_string(wifi_tech_state),
47                 _convert_wifi_service_state_to_string(wifi_service_state));
48
49         if (wifi_tech_state < NETCONFIG_WIFI_TECH_POWERED)
50                 return;
51
52         bg_mode = netconfig_wifi_bgscan_get_mode(interface_name);
53
54         if (wifi_service_state == NETCONFIG_WIFI_CONNECTED &&
55                         (bg_mode == WIFI_BGSCAN_MODE_EXPONENTIAL ||
56                         (bg_mode == WIFI_BGSCAN_MODE_PERIODIC &&
57                          ug_state != VCONFKEY_WIFI_UG_RUN_STATE_ON_FOREGROUND)))
58                 return;
59
60         DBG("[%s] Wi-Fi background scan started or re-started(%d)",
61                         interface_name, immediate_scan);
62
63         netconfig_wifi_bgscan_start_timer(interface_name, immediate_scan);
64 }
65
66 void netconfig_wifi_bgscan_stop(const char *interface_name)
67 {
68         netconfig_wifi_bgscan_stop_timer(interface_name);
69 }
70
71 void netconfig_wifi_bgscan_restart(const gchar *interface_name)
72 {
73         int pm_state = VCONFKEY_PM_STATE_NORMAL;
74
75         /* In case of LCD off, we don't need Wi-Fi scan right now */
76         netconfig_vconf_get_int(VCONFKEY_PM_STATE, &pm_state);
77         if (pm_state >= VCONFKEY_PM_STATE_LCDOFF)
78                 netconfig_wifi_bgscan_start(interface_name, FALSE);
79         else
80                 netconfig_wifi_bgscan_start(interface_name, TRUE);
81 }
82
83 gboolean handle_set_bgscan(Wifi *wifi, GDBusMethodInvocation *context,
84                 const gchar *ifname, guint scan_mode)
85 {
86         gint old_mode = 0;
87
88         old_mode = netconfig_wifi_bgscan_get_mode(ifname);
89         if (old_mode == scan_mode) {
90                 wifi_complete_set_bgscan(wifi, context);
91                 return TRUE;
92         }
93
94         if (netconfig_wifi_bgscan_set_mode(ifname, scan_mode) != TRUE) {
95                 ERR("Invalid mode [%d]", scan_mode);
96                 netconfig_error_invalid_parameter(context);
97                 return TRUE;
98         }
99
100         INFO("[%s] Wi-Fi scan mode is changed [%d]", ifname, scan_mode);
101
102         netconfig_wifi_bgscan_restart(ifname);
103
104         wifi_complete_set_bgscan(wifi, context);
105         return TRUE;
106 }
107
108 gboolean handle_set_bgscan_interval(Wifi *wifi, GDBusMethodInvocation *context,
109                 const gchar *ifname, const gchar *mode, guint interval)
110 {
111         guint old_interval = 0;
112         gboolean rv = FALSE;
113
114         if (g_strcmp0(mode, "Periodic") == 0) {
115                 old_interval = netconfig_wifi_bgscan_get_periodic_interval(ifname);
116         } else if (g_strcmp0(mode, "Exponential") == 0) {
117                 old_interval = netconfig_wifi_bgscan_get_exp_interval(ifname);
118         } else {
119                 ERR("invalid mode %s", mode);
120                 netconfig_error_invalid_parameter(context);
121                 return TRUE;
122         }
123
124         if (old_interval == interval) {
125                 wifi_complete_set_bgscan_interval(wifi, context);
126                 return TRUE;
127         }
128
129         if (g_strcmp0(mode, "Periodic") == 0)
130                 rv = netconfig_wifi_bgscan_set_periodic_interval(ifname, interval);
131         else
132                 rv = netconfig_wifi_bgscan_set_exp_interval(ifname, interval);
133
134         if (!rv) {
135                 netconfig_error_invalid_parameter(context);
136                 return TRUE;
137         }
138
139         INFO("[%s] Wi-Fi periodic scan interval is changed to [%d] from [%d]",
140                         ifname, interval, old_interval);
141
142         netconfig_wifi_bgscan_restart(ifname);
143
144         wifi_complete_set_bgscan_interval(wifi, context);
145         return TRUE;
146 }
147
148 gboolean handle_get_bgscan_interval(Wifi *wifi, GDBusMethodInvocation *context,
149                 const gchar *ifname, const gchar *mode)
150 {
151         guint bgscan_interval = 0;
152
153         if (g_strcmp0(mode, "Periodic") == 0) {
154                 bgscan_interval = netconfig_wifi_bgscan_get_periodic_interval(ifname);
155         } else if (g_strcmp0(mode, "Exponential") == 0) {
156                 bgscan_interval = netconfig_wifi_bgscan_get_exp_interval(ifname);
157         } else {
158                 ERR("invalid mode %s", mode);
159                 netconfig_error_invalid_parameter(context);
160                 return TRUE;
161         }
162
163         wifi_complete_get_bgscan_interval(wifi, context, bgscan_interval);
164         return TRUE;
165 }
166
167 gboolean handle_resume_bgscan(Wifi *wifi, GDBusMethodInvocation *context,
168                 const gchar *ifname)
169 {
170         netconfig_wifi_bgscan_set_pause(ifname, FALSE);
171
172         wifi_complete_resume_bgscan(wifi, context);
173         return TRUE;
174 }
175
176 gboolean handle_pause_bgscan(Wifi *wifi, GDBusMethodInvocation *context,
177                 const gchar *ifname)
178 {
179         netconfig_wifi_bgscan_set_pause(ifname, TRUE);
180
181         wifi_complete_pause_bgscan(wifi, context);
182         return TRUE;
183 }
184
185 gboolean handle_reset_bgscan_interval(Wifi *wifi, GDBusMethodInvocation *context,
186                 const gchar *ifname)
187 {
188         netconfig_wifi_bgscan_set_exp_interval(ifname, SCAN_EXPONENTIAL_MIN);
189
190         wifi_complete_reset_bgscan_interval(wifi, context);
191         return TRUE;
192 }
193
194 gboolean handle_get_autoscan(Wifi *wifi, GDBusMethodInvocation *context,
195                 const gchar *ifname)
196 {
197
198         gboolean autoscan = 0;
199
200         autoscan = netconfig_wifi_bgscan_is_paused(ifname);
201
202         wifi_complete_get_autoscan(wifi, context, autoscan);
203         return TRUE;
204 }
205
206 gboolean handle_get_autoscanmode(Wifi *wifi, GDBusMethodInvocation *context,
207                 const gchar *ifname)
208 {
209         guint autoscanmode = 0;
210
211         autoscanmode = netconfig_wifi_bgscan_get_mode(ifname);
212
213         wifi_complete_get_autoscanmode(wifi, context, autoscanmode);
214         return TRUE;
215 }
216