Use rand_r() instead of rand()
[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
36         if (netconfig_wifi_bgscan_get_timer_id(interface_name) > 0)
37                 netconfig_wifi_bgscan_stop_timer(interface_name);
38
39         wifi_tech_state = wifi_state_get_technology_state(interface_name);
40         wifi_service_state = wifi_state_get_service_state(interface_name);
41         DBG("[%s] Wi-Fi tech state [%s] service state [%s]", interface_name,
42                 _convert_wifi_technology_state_to_string(wifi_tech_state),
43                 _convert_wifi_service_state_to_string(wifi_service_state));
44
45         if (wifi_tech_state < NETCONFIG_WIFI_TECH_POWERED)
46                 return;
47
48         if (netconfig_wifi_bgscan_get_mode(interface_name) == WIFI_BGSCAN_MODE_EXPONENTIAL &&
49                 wifi_service_state == NETCONFIG_WIFI_CONNECTED)
50                 return;
51
52         DBG("[%s] Wi-Fi background scan started or re-started(%d)",
53                         interface_name, immediate_scan);
54
55         netconfig_wifi_bgscan_start_timer(interface_name, immediate_scan);
56 }
57
58 void netconfig_wifi_bgscan_stop(const char *interface_name)
59 {
60         netconfig_wifi_bgscan_stop_timer(interface_name);
61 }
62
63 gboolean handle_set_bgscan(Wifi *wifi, GDBusMethodInvocation *context,
64                 const gchar *ifname, guint scan_mode)
65 {
66         gint old_mode = 0;
67         int pm_state = VCONFKEY_PM_STATE_NORMAL;
68
69         old_mode = netconfig_wifi_bgscan_get_mode(ifname);
70         if (old_mode == scan_mode) {
71                 wifi_complete_set_bgscan(wifi, context);
72                 return TRUE;
73         }
74
75         if (netconfig_wifi_bgscan_set_mode(ifname, scan_mode) != TRUE) {
76                 ERR("Invalid mode [%d]", scan_mode);
77                 netconfig_error_invalid_parameter(context);
78                 return TRUE;
79         }
80
81         INFO("[%s] Wi-Fi scan mode is changed [%d]", ifname, scan_mode);
82
83         /* In case of LCD off, we don't need Wi-Fi scan right now */
84         netconfig_vconf_get_int(VCONFKEY_PM_STATE, &pm_state);
85         if (pm_state >= VCONFKEY_PM_STATE_LCDOFF)
86                 netconfig_wifi_bgscan_start(ifname, FALSE);
87         else
88                 netconfig_wifi_bgscan_start(ifname, TRUE);
89
90         wifi_complete_set_bgscan(wifi, context);
91         return TRUE;
92 }
93
94 gboolean handle_resume_bgscan(Wifi *wifi, GDBusMethodInvocation *context,
95                 const gchar *ifname)
96 {
97         netconfig_wifi_bgscan_set_pause(ifname, FALSE);
98
99         wifi_complete_resume_bgscan(wifi, context);
100         return TRUE;
101 }
102
103 gboolean handle_pause_bgscan(Wifi *wifi, GDBusMethodInvocation *context,
104                 const gchar *ifname)
105 {
106         netconfig_wifi_bgscan_set_pause(ifname, TRUE);
107
108         wifi_complete_pause_bgscan(wifi, context);
109         return TRUE;
110 }
111
112 gboolean handle_reset_bgscan_interval(Wifi *wifi, GDBusMethodInvocation *context,
113                 const gchar *ifname)
114 {
115         netconfig_wifi_bgscan_set_interval(ifname, SCAN_EXPONENTIAL_MIN);
116
117         wifi_complete_reset_bgscan_interval(wifi, context);
118         return TRUE;
119 }
120
121 gboolean handle_get_autoscan(Wifi *wifi, GDBusMethodInvocation *context,
122                 const gchar *ifname)
123 {
124
125         gboolean autoscan = 0;
126
127         autoscan = netconfig_wifi_bgscan_is_paused(ifname);
128
129         wifi_complete_get_autoscan(wifi, context, autoscan);
130         return TRUE;
131 }
132
133 gboolean handle_get_autoscanmode(Wifi *wifi, GDBusMethodInvocation *context,
134                 const gchar *ifname)
135 {
136         guint autoscanmode = 0;
137
138         autoscanmode = netconfig_wifi_bgscan_get_mode(ifname);
139
140         wifi_complete_get_autoscanmode(wifi, context, autoscanmode);
141         return TRUE;
142 }
143