4 * Copyright 2012 Samsung Electronics Co., Ltd
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
10 * http://www.tizenopensource.org/license
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.
21 #include "wlan_connection.h"
23 struct wlan_connection {
25 wifi_manager_connected_cb callback;
31 static wifi_manager_h *wifi_handle = NULL;
33 static struct wlan_connection current_item = { NULL, NULL, NULL };
34 static struct wlan_connection next_item = { NULL, NULL, NULL };
36 static void wlan_connect_debug(wifi_manager_ap_h ap)
38 char *next_ssid, *ap_ssid;
40 wifi_manager_ap_get_essid(ap, &ap_ssid);
42 if (next_item.ap == NULL) {
43 SECURE_ERROR_LOG(UG_NAME_REQ, "%s will be connected", ap_ssid);
45 wifi_manager_ap_get_essid(next_item.ap, &next_ssid);
47 SECURE_ERROR_LOG(UG_NAME_REQ, "%s will be connected (%s replaced)",
56 static gboolean wlan_is_same_with_next(wifi_manager_ap_h ap)
58 gboolean is_same = FALSE;
59 char *next_ssid, *ap_ssid;
60 wifi_manager_security_type_e next_sec, ap_sec;
62 if (next_item.ap == NULL)
66 wifi_manager_ap_get_security_type(ap, &ap_sec);
67 wifi_manager_ap_get_security_type(next_item.ap, &next_sec);
69 if (ap_sec != next_sec)
73 wifi_manager_ap_get_essid(ap, &ap_ssid);
74 wifi_manager_ap_get_essid(next_item.ap, &next_ssid);
76 if (g_strcmp0(ap_ssid, next_ssid) == 0)
86 void wlan_go_fast_next()
88 bool favorite = false;
90 if (current_item.ap == NULL || next_item.ap == NULL)
94 wifi_manager_ap_is_favorite(current_item.ap, &favorite);
99 wlan_disconnect(current_item.ap, NULL, NULL);
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)
105 if (wlan_is_same_with_next(ap) == TRUE) {
106 wifi_manager_ap_destroy(ap);
112 wlan_connect_debug(ap);
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;
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);
134 void wlan_connect_next()
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;
145 current_item.pin = g_strdup(next_item.pin);
148 if (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;
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);
168 wifi_manager_connect(*wifi_handle, current_item.ap, current_item.callback,
169 current_item.user_data);
173 int wlan_initialize(wifi_manager_h *wifi)
176 return wifi_manager_initialize(wifi);
181 int wlan_disconnect(wifi_manager_ap_h ap, wifi_manager_disconnected_cb callback, void *user_data)
183 return wifi_manager_disconnect(*wifi_handle, ap, callback, user_data);
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)
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);
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);
203 return wifi_manager_connect(*wifi_handle, ap, callback, user_data);
207 wlan_update_next(ap, callback, user_data, type, pin);
209 return WIFI_MANAGER_ERROR_NONE;
213 gboolean wlan_connetion_next_item_exist(void)
215 if (next_item.ap == NULL)
221 gboolean wlan_is_same_with_current(wifi_manager_ap_h ap)
223 gboolean is_same = FALSE;
224 char *current_ssid, *ap_ssid;
225 wifi_manager_security_type_e currect_sec, ap_sec;
227 if (current_item.ap == NULL)
231 wifi_manager_ap_get_security_type(ap, &ap_sec);
232 wifi_manager_ap_get_security_type(current_item.ap, &currect_sec);
234 if (ap_sec != currect_sec)
238 wifi_manager_ap_get_essid(ap, &ap_ssid);
239 wifi_manager_ap_get_essid(current_item.ap, ¤t_ssid);
241 if (g_strcmp0(ap_ssid, current_ssid) == 0)
246 g_free(current_ssid);
251 void wlan_connect_cleanup(void)
253 if (current_item.pin != NULL) {
254 g_free(current_item.pin);
255 current_item.pin = NULL;
258 if (next_item.pin != NULL) {
259 g_free(next_item.pin);
260 next_item.pin = NULL;