2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Danny JS Seo <S.Seo@samsung.com>
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <vconf-keys.h>
30 #include "wifi-state.h"
31 #include "wifi-background-scan.h"
33 #define SCAN_INITIAL_DELAY 10
34 #define SCAN_PERIODIC_DELAY 10
35 #define SCAN_EXPONENTIAL_MIN 4
36 #define SCAN_EXPONENTIAL_MAX 128
39 WIFI_BGSCAN_MODE_DEFAULT = 0x00,
40 WIFI_BGSCAN_MODE_PERIODIC,
41 WIFI_BGSCAN_MODE_EXPONENTIAL,
45 struct bgscan_timer_data {
51 static struct bgscan_timer_data *__netconfig_wifi_bgscan_get_bgscan_data(void)
53 static struct bgscan_timer_data timer_data = {SCAN_INITIAL_DELAY, WIFI_BGSCAN_MODE_EXPONENTIAL, 0};
58 static guint __netconfig_wifi_bgscan_mode(gboolean is_set_mode, guint mode)
60 static guint bgscan_mode = WIFI_BGSCAN_MODE_DEFAULT;
62 if (is_set_mode != TRUE)
65 if (mode < WIFI_BGSCAN_MODE_MAX && mode > WIFI_BGSCAN_MODE_DEFAULT)
68 DBG("Wi-Fi background scan mode set %d", bgscan_mode);
73 static void __netconfig_wifi_bgscan_set_mode(guint mode)
75 __netconfig_wifi_bgscan_mode(TRUE, mode);
78 static guint __netconfig_wifi_bgscan_get_mode(void)
80 return __netconfig_wifi_bgscan_mode(FALSE, -1);
83 static gboolean __netconfig_wifi_bgscan_request_connman_scan(void)
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[] = {
97 if (netconfig_wifi_state_get_service_state() == NETCONFIG_WIFI_CONNECTED)
98 if (__netconfig_wifi_bgscan_get_mode() == WIFI_BGSCAN_MODE_EXPONENTIAL)
101 if (netconfig_wifi_state_get_service_state() == NETCONFIG_WIFI_CONNECTING)
104 param_array[0] = path;
105 param_array[1] = request;
106 param_array[2] = param1;
108 DBG("Requesting [%s %s %s]", param_array[0], param_array[1], param_array[2]);
110 reply = netconfig_dbus_send_request(CONNMAN_SERVICE, param_array);
112 ERR("Error! Request failed");
117 dbus_message_unref(reply);
122 static gboolean __netconfig_wifi_bgscan_request_scan(gpointer data);
124 static void __netconfig_wifi_bgscan_start_timer(struct bgscan_timer_data *data)
129 netconfig_stop_timer(&(data->timer_id));
131 data->mode = __netconfig_wifi_bgscan_get_mode();
133 switch (data->mode) {
134 case WIFI_BGSCAN_MODE_EXPONENTIAL:
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;
140 data->time = data->time * 2;
142 DBG("Wi-Fi background scan with exponentially increasing period");
145 case WIFI_BGSCAN_MODE_PERIODIC:
146 data->time = SCAN_PERIODIC_DELAY;
148 DBG("Wi-Fi background scan periodically");
152 data->time = SCAN_INITIAL_DELAY;
153 DBG("Wi-Fi background scan with initial delay");
156 DBG("Register background scan timer with %d seconds", data->time);
158 netconfig_start_timer_seconds(data->time, __netconfig_wifi_bgscan_request_scan, data, &(data->timer_id));
161 static void __netconfig_wifi_bgscan_stop_timer(struct bgscan_timer_data *data)
166 DBG("Stop Wi-Fi background scan timer");
168 netconfig_stop_timer(&(data->timer_id));
171 static gboolean __netconfig_wifi_bgscan_request_scan(gpointer data)
173 struct bgscan_timer_data *timer = (struct bgscan_timer_data *)data;
178 DBG("Request Wi-Fi scan to ConnMan");
180 __netconfig_wifi_bgscan_stop_timer(timer);
182 __netconfig_wifi_bgscan_request_connman_scan();
184 __netconfig_wifi_bgscan_start_timer(timer);
189 void netconfig_wifi_bgscan_start(void)
191 struct bgscan_timer_data *timer_data = __netconfig_wifi_bgscan_get_bgscan_data();
193 if (timer_data == NULL)
196 DBG("Wi-Fi background scan start");
198 __netconfig_wifi_bgscan_request_connman_scan();
200 __netconfig_wifi_bgscan_start_timer(timer_data);
203 void netconfig_wifi_bgscan_stop(void)
205 struct bgscan_timer_data *timer_data = __netconfig_wifi_bgscan_get_bgscan_data();
207 if (timer_data == NULL)
210 DBG("Wi-Fi background scan stop");
212 timer_data->time = SCAN_EXPONENTIAL_MIN;
214 __netconfig_wifi_bgscan_stop_timer(timer_data);
217 gboolean netconfig_iface_wifi_set_bgscan(NetconfigWifi *wifi, guint scan_mode, GError **error)
219 struct bgscan_timer_data *timer_data = __netconfig_wifi_bgscan_get_bgscan_data();
221 DBG("Wi-Fi background scan mode set: %d", scan_mode);
223 if (scan_mode >= WIFI_BGSCAN_MODE_MAX || scan_mode == WIFI_BGSCAN_MODE_DEFAULT)
227 case WIFI_BGSCAN_MODE_PERIODIC:
228 DBG("[%s]BG scan mode is periodic", __FUNCTION__);
230 case WIFI_BGSCAN_MODE_EXPONENTIAL:
231 DBG("[%s]BG scan mode is exponential", __FUNCTION__);
234 DBG("[%s]strange value [%d]", __FUNCTION__, scan_mode);
238 __netconfig_wifi_bgscan_set_mode(scan_mode);
240 if (timer_data->timer_id != 0) {
241 netconfig_wifi_bgscan_stop();
242 netconfig_wifi_bgscan_start();