Remove DPM codes
[apps/native/ug-wifi-efl.git] / sources / libraries / WlanManager / wlan_connection.c
1 /*
2  * Wi-Fi
3  *
4  * Copyright 2012 Samsung Electronics Co., Ltd
5  *
6  * Licensed under the Flora License, Version 1.1 (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.tizenopensource.org/license
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 "common.h"
21 #include "wlan_connection.h"
22
23 struct wlan_connection {
24         wifi_manager_ap_h ap;
25         wifi_manager_connected_cb callback;
26         void *user_data;
27         char *pin;
28         wps_type_t type;
29 };
30
31 static wifi_manager_h *wifi_handle = NULL;
32
33 static struct wlan_connection current_item = { NULL, NULL, NULL };
34 static struct wlan_connection next_item = { NULL, NULL, NULL };
35
36 static void wlan_connect_debug(wifi_manager_ap_h ap)
37 {
38         char *next_ssid, *ap_ssid;
39
40         wifi_manager_ap_get_essid(ap, &ap_ssid);
41
42         if (next_item.ap == NULL) {
43                 SECURE_ERROR_LOG(UG_NAME_REQ, "%s will be connected", ap_ssid);
44         } else {
45                 wifi_manager_ap_get_essid(next_item.ap, &next_ssid);
46
47                 SECURE_ERROR_LOG(UG_NAME_REQ, "%s will be connected (%s replaced)",
48                                 ap_ssid, next_ssid);
49
50                 g_free(next_ssid);
51         }
52
53         g_free(ap_ssid);
54 }
55
56 static gboolean wlan_is_same_with_next(wifi_manager_ap_h ap)
57 {
58         gboolean is_same = FALSE;
59         char *next_ssid, *ap_ssid;
60         wifi_manager_security_type_e next_sec, ap_sec;
61
62         if (next_item.ap == NULL)
63                 return FALSE;
64
65
66         wifi_manager_ap_get_security_type(ap, &ap_sec);
67         wifi_manager_ap_get_security_type(next_item.ap, &next_sec);
68
69         if (ap_sec != next_sec)
70                 return is_same;
71
72
73         wifi_manager_ap_get_essid(ap, &ap_ssid);
74         wifi_manager_ap_get_essid(next_item.ap, &next_ssid);
75
76         if (g_strcmp0(ap_ssid, next_ssid) == 0)
77                 is_same = TRUE;
78
79
80         g_free(ap_ssid);
81         g_free(next_ssid);
82
83         return is_same;
84 }
85
86 void wlan_go_fast_next()
87 {
88         bool favorite = false;
89
90         if (current_item.ap == NULL || next_item.ap == NULL)
91                 return;
92
93
94         wifi_manager_ap_is_favorite(current_item.ap, &favorite);
95         if (favorite == true)
96                 return;
97
98
99         wlan_disconnect(current_item.ap, NULL, NULL);
100 }
101
102 static void wlan_update_next(wifi_manager_ap_h ap, wifi_manager_connected_cb callback,
103                 void *user_data, wps_type_t type, const char *pin)
104 {
105         if (wlan_is_same_with_next(ap) == TRUE) {
106                 wifi_manager_ap_destroy(ap);
107                 g_free(user_data);
108
109                 return;
110         }
111
112         wlan_connect_debug(ap);
113
114         if (next_item.ap != NULL) {
115                 wifi_manager_ap_destroy(next_item.ap);
116                 g_free(next_item.user_data);
117                 if (next_item.type == WPS_PIN && next_item.pin != NULL) {
118                         g_free(next_item.pin);
119                         next_item.pin = NULL;
120                 }
121         }
122
123         next_item.ap = ap;
124         next_item.callback = callback;
125         next_item.user_data = user_data;
126         next_item.type = type;
127         if (next_item.type == WPS_PIN && pin != NULL)
128                 next_item.pin = g_strdup(pin);
129
130
131         wlan_go_fast_next();
132 }
133
134 void wlan_connect_next()
135 {
136         current_item.ap = next_item.ap;
137         current_item.callback = next_item.callback;
138         current_item.user_data = next_item.user_data;
139         current_item.type = next_item.type;
140         if (current_item.type == WPS_PIN && next_item.pin != NULL) {
141                 if (current_item.pin != NULL) {
142                         g_free(current_item.pin);
143                         current_item.pin = NULL;
144                 }
145                 current_item.pin = g_strdup(next_item.pin);
146         }
147
148         if (next_item.ap == NULL)
149                 return;
150
151
152         next_item.ap = NULL;
153         next_item.callback = NULL;
154         next_item.user_data = NULL;
155         if (next_item.type == WPS_PIN && next_item.pin != NULL) {
156                 g_free(next_item.pin);
157                 next_item.pin = NULL;
158         }
159
160         if (current_item.type == WPS_BTN) {
161                 wifi_manager_connect_by_wps_pbc(*wifi_handle, current_item.ap,
162                                 current_item.callback, current_item.user_data);
163         } else if (current_item.type == WPS_PIN) {
164                 wifi_manager_connect_by_wps_pin(*wifi_handle, current_item.ap,
165                                 current_item.pin, current_item.callback,
166                                 current_item.user_data);
167         } else {
168                 wifi_manager_connect(*wifi_handle, current_item.ap, current_item.callback,
169                                 current_item.user_data);
170         }
171 }
172
173 int wlan_initialize(wifi_manager_h *wifi)
174 {
175         wifi_handle = wifi;
176         return wifi_manager_initialize(wifi);
177 }
178
179
180
181 int wlan_disconnect(wifi_manager_ap_h ap, wifi_manager_disconnected_cb callback, void *user_data)
182 {
183         return wifi_manager_disconnect(*wifi_handle, ap, callback, user_data);
184 }
185
186 int wlan_connect(wifi_manager_ap_h ap, wifi_manager_connected_cb callback, void *user_data,
187                 wps_type_t type, const char *pin)
188 {
189         if (current_item.ap == NULL) {
190                 current_item.ap = ap;
191                 current_item.callback = callback;
192                 current_item.user_data = user_data;
193                 current_item.type = type;
194                 if (current_item.type == WPS_PIN)
195                         current_item.pin = g_strdup(pin);
196
197
198                 if (type == WPS_BTN)
199                         return wifi_manager_connect_by_wps_pbc(*wifi_handle, ap, callback, user_data);
200                 else if (type == WPS_PIN)
201                         return wifi_manager_connect_by_wps_pin(*wifi_handle, ap, pin, callback, user_data);
202                 else
203                         return wifi_manager_connect(*wifi_handle, ap, callback, user_data);
204
205         }
206
207         wlan_update_next(ap, callback, user_data, type, pin);
208
209         return WIFI_MANAGER_ERROR_NONE;
210 }
211
212
213 gboolean wlan_connetion_next_item_exist(void)
214 {
215         if (next_item.ap == NULL)
216                 return FALSE;
217         else
218                 return TRUE;
219 }
220
221 gboolean wlan_is_same_with_current(wifi_manager_ap_h ap)
222 {
223         gboolean is_same = FALSE;
224         char *current_ssid, *ap_ssid;
225         wifi_manager_security_type_e currect_sec, ap_sec;
226
227         if (current_item.ap == NULL)
228                 return FALSE;
229
230
231         wifi_manager_ap_get_security_type(ap, &ap_sec);
232         wifi_manager_ap_get_security_type(current_item.ap, &currect_sec);
233
234         if (ap_sec != currect_sec)
235                 return is_same;
236
237
238         wifi_manager_ap_get_essid(ap, &ap_ssid);
239         wifi_manager_ap_get_essid(current_item.ap, &current_ssid);
240
241         if (g_strcmp0(ap_ssid, current_ssid) == 0)
242                 is_same = TRUE;
243
244
245         g_free(ap_ssid);
246         g_free(current_ssid);
247
248         return is_same;
249 }
250
251 void wlan_connect_cleanup(void)
252 {
253         if (current_item.pin != NULL) {
254                 g_free(current_item.pin);
255                 current_item.pin = NULL;
256         }
257
258         if (next_item.pin != NULL) {
259                 g_free(next_item.pin);
260                 next_item.pin = NULL;
261         }
262 }