Revise unncessary functions
[framework/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  * Contact: Danny JS Seo <S.Seo@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <glib.h>
23 #include <vconf.h>
24 #include <vconf-keys.h>
25
26 #include "log.h"
27 #include "dbus.h"
28 #include "util.h"
29 #include "wifi.h"
30 #include "wifi-state.h"
31 #include "wifi-background-scan.h"
32
33 #define SCAN_INITIAL_DELAY              10
34 #define SCAN_PERIODIC_DELAY             10
35 #define SCAN_EXPONENTIAL_MIN    4
36 #define SCAN_EXPONENTIAL_MAX    128
37
38 enum {
39         WIFI_BGSCAN_MODE_DEFAULT = 0x00,
40         WIFI_BGSCAN_MODE_PERIODIC,
41         WIFI_BGSCAN_MODE_EXPONENTIAL,
42         WIFI_BGSCAN_MODE_MAX,
43 };
44
45 struct bgscan_timer_data {
46         guint time;
47         guint mode;
48         guint timer_id;
49 };
50
51 static struct bgscan_timer_data *__netconfig_wifi_bgscan_get_bgscan_data(void)
52 {
53         static struct bgscan_timer_data timer_data = {SCAN_INITIAL_DELAY, WIFI_BGSCAN_MODE_EXPONENTIAL, 0};
54
55         return &timer_data;
56 }
57
58 static guint __netconfig_wifi_bgscan_mode(gboolean is_set_mode, guint mode)
59 {
60         static guint bgscan_mode = WIFI_BGSCAN_MODE_DEFAULT;
61
62         if (is_set_mode != TRUE)
63                 return bgscan_mode;
64
65         if (mode < WIFI_BGSCAN_MODE_MAX && mode > WIFI_BGSCAN_MODE_DEFAULT)
66                 bgscan_mode = mode;
67
68         DBG("Wi-Fi background scan mode set %d", bgscan_mode);
69
70         return bgscan_mode;
71 }
72
73 static void __netconfig_wifi_bgscan_set_mode(guint mode)
74 {
75         __netconfig_wifi_bgscan_mode(TRUE, mode);
76 }
77
78 static guint __netconfig_wifi_bgscan_get_mode(void)
79 {
80         return __netconfig_wifi_bgscan_mode(FALSE, -1);
81 }
82
83 static gboolean __netconfig_wifi_bgscan_request_connman_scan(void)
84 {
85         DBusMessage *reply = NULL;
86         /** dbus-send --system --print-reply --dest=net.connman / net.connman.Manager.SetProperty string:ScanMode variant:uint16:0/1/2/3 */
87         char request[] = CONNMAN_MANAGER_INTERFACE ".RequestScan";
88         char param1[] = "string:wifi";
89         char path[] = CONNMAN_MANAGER_PATH;
90         char *param_array[] = {
91                 NULL,
92                 NULL,
93                 NULL,
94                 NULL
95         };
96
97         if (netconfig_wifi_state_get_service_state() == NETCONFIG_WIFI_CONNECTED)
98                 if (__netconfig_wifi_bgscan_get_mode() == WIFI_BGSCAN_MODE_EXPONENTIAL)
99                                 return FALSE;
100
101         if (netconfig_wifi_state_get_service_state() == NETCONFIG_WIFI_CONNECTING)
102                 return FALSE;
103
104         param_array[0] = path;
105         param_array[1] = request;
106         param_array[2] = param1;
107
108         DBG("Requesting [%s %s %s]", param_array[0], param_array[1], param_array[2]);
109
110         reply = netconfig_dbus_send_request(CONNMAN_SERVICE, param_array);
111         if (reply == NULL) {
112                 ERR("Error! Request failed");
113
114                 return FALSE;
115         }
116
117         dbus_message_unref(reply);
118
119         return TRUE;
120 }
121
122 static gboolean __netconfig_wifi_bgscan_request_scan(gpointer data);
123
124 static void __netconfig_wifi_bgscan_start_timer(struct bgscan_timer_data *data)
125 {
126         if (data == NULL)
127                 return;
128
129         netconfig_stop_timer(&(data->timer_id));
130
131         data->mode = __netconfig_wifi_bgscan_get_mode();
132
133         switch (data->mode) {
134         case WIFI_BGSCAN_MODE_EXPONENTIAL:
135                 if (data->time == 0)
136                         data->time = SCAN_EXPONENTIAL_MIN;
137                 else if ((data->time >= SCAN_EXPONENTIAL_MAX) || (data->time > SCAN_EXPONENTIAL_MAX / 2))
138                         data->time = SCAN_EXPONENTIAL_MAX;
139                 else
140                         data->time = data->time * 2;
141
142                 DBG("Wi-Fi background scan with exponentially increasing period");
143                 break;
144
145         case WIFI_BGSCAN_MODE_PERIODIC:
146                 data->time = SCAN_PERIODIC_DELAY;
147
148                 DBG("Wi-Fi background scan periodically");
149                 break;
150
151         default:
152                 data->time = SCAN_INITIAL_DELAY;
153                 DBG("Wi-Fi background scan with initial delay");
154         }
155
156         DBG("Register background scan timer with %d seconds", data->time);
157
158         netconfig_start_timer_seconds(data->time, __netconfig_wifi_bgscan_request_scan, data, &(data->timer_id));
159 }
160
161 static void __netconfig_wifi_bgscan_stop_timer(struct bgscan_timer_data *data)
162 {
163         if (data == NULL)
164                 return;
165
166         DBG("Stop Wi-Fi background scan timer");
167
168         netconfig_stop_timer(&(data->timer_id));
169 }
170
171 static gboolean __netconfig_wifi_bgscan_request_scan(gpointer data)
172 {
173         struct bgscan_timer_data *timer = (struct bgscan_timer_data *)data;
174
175         if (timer == NULL)
176                 return FALSE;
177
178         DBG("Request Wi-Fi scan to ConnMan");
179
180         __netconfig_wifi_bgscan_stop_timer(timer);
181
182         __netconfig_wifi_bgscan_request_connman_scan();
183
184         __netconfig_wifi_bgscan_start_timer(timer);
185
186         return FALSE;
187 }
188
189 void netconfig_wifi_bgscan_start(void)
190 {
191         struct bgscan_timer_data *timer_data = __netconfig_wifi_bgscan_get_bgscan_data();
192
193         if (timer_data == NULL)
194                 return;
195
196         DBG("Wi-Fi background scan start");
197
198         __netconfig_wifi_bgscan_request_connman_scan();
199
200         __netconfig_wifi_bgscan_start_timer(timer_data);
201 }
202
203 void netconfig_wifi_bgscan_stop(void)
204 {
205         struct bgscan_timer_data *timer_data = __netconfig_wifi_bgscan_get_bgscan_data();
206
207         if (timer_data == NULL)
208                 return;
209
210         DBG("Wi-Fi background scan stop");
211
212         timer_data->time = SCAN_EXPONENTIAL_MIN;
213
214         __netconfig_wifi_bgscan_stop_timer(timer_data);
215 }
216
217 gboolean netconfig_iface_wifi_set_bgscan(NetconfigWifi *wifi, guint scan_mode, GError **error)
218 {
219         struct bgscan_timer_data *timer_data = __netconfig_wifi_bgscan_get_bgscan_data();
220
221         DBG("Wi-Fi background scan mode set: %d", scan_mode);
222
223         if (scan_mode >= WIFI_BGSCAN_MODE_MAX || scan_mode == WIFI_BGSCAN_MODE_DEFAULT)
224                 return FALSE;
225
226         switch (scan_mode) {
227                 case WIFI_BGSCAN_MODE_PERIODIC:
228                         DBG("[%s]BG scan mode is periodic", __FUNCTION__);
229                         break;
230                 case WIFI_BGSCAN_MODE_EXPONENTIAL:
231                         DBG("[%s]BG scan mode is exponential", __FUNCTION__);
232                         break;
233                 default:
234                         DBG("[%s]strange value [%d]", __FUNCTION__, scan_mode);
235                         break;
236         }
237
238         __netconfig_wifi_bgscan_set_mode(scan_mode);
239
240         if (timer_data->timer_id != 0) {
241                 netconfig_wifi_bgscan_stop();
242                 netconfig_wifi_bgscan_start();
243         }
244
245         return TRUE;
246 }