2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include "netsupplicant.h"
25 #include "wifi-ssid-scan.h"
26 #include "wifi-background-scan.h"
29 WIFI_SECURITY_UNKNOWN = 0x00,
30 WIFI_SECURITY_NONE = 0x01,
31 WIFI_SECURITY_WEP = 0x02,
32 WIFI_SECURITY_PSK = 0x03,
33 WIFI_SECURITY_IEEE8021X = 0x04,
37 unsigned char ssid[33];
38 wifi_security_e security;
43 static gboolean g_ssid_scan_state = FALSE;
44 static GSList *bss_info_list = NULL;
45 static guint ssid_scan_timer = 0;
46 static char *g_ssid = NULL;
48 static void __check_security(const char *str_keymgmt, bss_info_t *bss_info)
50 INFO("keymgmt : %s", str_keymgmt);
52 if (g_strcmp0(str_keymgmt, "ieee8021x") == 0)
53 bss_info->security = WIFI_SECURITY_IEEE8021X;
54 else if (g_strcmp0(str_keymgmt, "wpa-psk") == 0)
55 bss_info->security = WIFI_SECURITY_PSK;
56 else if (g_strcmp0(str_keymgmt, "wpa-psk-sha256") == 0)
57 bss_info->security = WIFI_SECURITY_PSK;
58 else if (g_strcmp0(str_keymgmt, "wpa-ft-psk") == 0)
59 bss_info->security = WIFI_SECURITY_PSK;
60 else if (g_strcmp0(str_keymgmt, "wpa-ft-eap") == 0)
61 bss_info->security = WIFI_SECURITY_IEEE8021X;
62 else if (g_strcmp0(str_keymgmt, "wpa-eap") == 0)
63 bss_info->security = WIFI_SECURITY_IEEE8021X;
64 else if (g_strcmp0(str_keymgmt, "wpa-eap-sha256") == 0)
65 bss_info->security = WIFI_SECURITY_IEEE8021X;
66 else if (g_strcmp0(str_keymgmt, "wps") == 0)
70 static gboolean __ssid_scan_timeout(gpointer data)
72 wifi_ssid_scan_emit_scan_completed();
77 static void _start_ssid_scan_timer(void)
79 INFO("Wi-Fi SSID scan started");
80 g_ssid_scan_state = TRUE;
82 netconfig_start_timer_seconds(5, __ssid_scan_timeout, NULL, &ssid_scan_timer);
85 static void _stop_ssid_scan_timer(void)
87 INFO("Wi-Fi SSID scan finished");
88 g_ssid_scan_state = FALSE;
90 netconfig_stop_timer(&ssid_scan_timer);
93 static void _parse_keymgmt_message(GVariant *param, bss_info_t *bss_info)
99 g_variant_get(param, "a{sv}", &iter1);
100 while (g_variant_iter_loop(iter1, "{sv}", &key, &var)) {
101 if (g_strcmp0(key, "KeyMgmt") == 0) {
103 g_variant_get(var, "as", &iter2);
105 while (g_variant_iter_loop(iter2, "s", &str)) {
108 __check_security(str, bss_info);
110 g_variant_iter_free(iter2);
114 g_variant_iter_free(iter1);
119 static gboolean _request_ssid_scan(const char *object_path, const char *ssid)
121 /* TODO: Revise following code */
123 #define NETCONFIG_DBUS_REPLY_TIMEOUT (10 * 1000)
124 GDBusConnection *connection = NULL;
125 GVariant *reply = NULL;
126 GVariant *params = NULL;
127 GError *error = NULL;
128 GVariantBuilder *builder1 = NULL;
129 GVariantBuilder *builder2 = NULL;
130 GVariantBuilder *builder3 = NULL;
131 const gchar *key1 = "Type";
132 const gchar *val1 = "active";
133 const gchar *key2 = "SSIDs";
136 connection = netdbus_get_connection();
137 if (connection == NULL) {
138 DBG("Failed to get GDBusconnection");
142 builder1 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
143 g_variant_builder_add(builder1, "{sv}", key1, g_variant_new_string(val1));
145 builder2 = g_variant_builder_new(G_VARIANT_TYPE("aay"));
146 builder3 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
148 for (i = 0; i < strlen(ssid); i++)
149 g_variant_builder_add(builder3, "y", ssid[i]);
151 g_variant_builder_add(builder2, "@ay", g_variant_builder_end(builder3));
152 g_variant_builder_add(builder1, "{sv}", key2, g_variant_builder_end(builder2));
154 params = g_variant_new("(@a{sv})", g_variant_builder_end(builder1));
156 g_variant_builder_unref(builder1);
157 g_variant_builder_unref(builder2);
158 g_variant_builder_unref(builder3);
160 reply = g_dbus_connection_call_sync(
164 SUPPLICANT_INTERFACE ".Interface",
168 G_DBUS_CALL_FLAGS_NONE,
169 NETCONFIG_DBUS_REPLY_TIMEOUT,
170 netdbus_get_cancellable(),
175 ERR("Error!!! dbus_connection_send_with_reply_and_block() failed. "
176 "DBus error [%d: %s]", error->code, error->message);
179 ERR("Error!!! Failed to get properties");
187 g_ssid = g_strdup(ssid);
189 g_variant_unref(reply);
194 static void _emit_ssid_scan_completed(void)
196 GVariantBuilder *builder = NULL;
198 const char *prop_ssid = "ssid";
199 const char *prop_security = "security";
200 const char *prop_wps = "wps";
202 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
203 for (list = bss_info_list; list != NULL; list = list->next) {
204 bss_info_t *bss_info = (bss_info_t *)list->data;
205 if (bss_info && g_strcmp0((char *)bss_info->ssid, g_ssid) == 0) {
206 const gchar *ssid = (char *)bss_info->ssid;
207 wifi_security_e security = bss_info->security;
208 gboolean wps = bss_info->wps;
209 DBG("BSS found; SSID:%s security:%d WPS:%d", ssid, security, wps);
210 g_variant_builder_add(builder, "{sv}", prop_ssid, g_variant_new_string(ssid));
211 g_variant_builder_add(builder, "{sv}", prop_security, g_variant_new_int32(security));
213 g_variant_builder_add(builder, "{sv}", prop_wps, g_variant_new_boolean(wps));
217 wifi_emit_specific_scan_completed((Wifi *)get_wifi_object(), g_variant_builder_end(builder));
220 g_variant_builder_unref(builder);
222 if (bss_info_list != NULL) {
223 g_slist_free_full(bss_info_list, g_free);
224 bss_info_list = NULL;
227 if (g_ssid != NULL) {
232 INFO("SpecificScanCompleted");
237 gboolean wifi_ssid_scan(const char *ssid)
240 static char *scan_ssid = NULL;
242 netconfig_wifi_bgscan_stop();
245 if (scan_ssid != NULL)
247 scan_ssid = g_strdup(ssid);
250 if (scan_ssid == NULL)
253 if_path = netconfig_wifi_get_supplicant_interface();
254 if (if_path == NULL) {
255 DBG("Fail to get wpa_supplicant DBus path");
259 if (netconfig_wifi_get_scanning() == TRUE) {
260 DBG("Wi-Fi scan in progress, %s scan will be delayed", scan_ssid);
266 g_slist_free_full(bss_info_list, g_free);
267 bss_info_list = NULL;
270 INFO("Start Wi-Fi scan with %s(%d)", scan_ssid, strlen(scan_ssid));
271 if (_request_ssid_scan(if_path, (const char *)scan_ssid) == TRUE) {
272 _start_ssid_scan_timer();
279 if (scan_ssid != NULL) {
284 netconfig_wifi_bgscan_start(FALSE);
289 gboolean wifi_ssid_scan_get_state(void)
291 return g_ssid_scan_state;
294 void wifi_ssid_scan_emit_scan_completed(void)
296 if (g_ssid_scan_state != TRUE)
299 _stop_ssid_scan_timer();
300 _emit_ssid_scan_completed();
303 void wifi_ssid_scan_add_bss(GVariant *message)
309 bss_info_t *bss_info;
311 if (g_ssid_scan_state != TRUE)
314 INFO("NEW BSS added");
316 if (message == NULL) {
317 DBG("Message does not have parameters");
322 INFO("Object path of BSS added is %s", path);
324 bss_info = g_try_new0(bss_info_t, 1);
325 if (bss_info == NULL)
328 g_variant_get(message, "(oa{sv})", &path, &iter);
329 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
330 if (g_strcmp0(key, "SSID") == 0) {
333 ssid = g_variant_get_fixed_array(value, &ssid_len, sizeof(guchar));
334 if (ssid != NULL && ssid_len > 0 && ssid_len < 33)
335 memcpy(bss_info->ssid, ssid, ssid_len);
337 memset(bss_info->ssid, 0, sizeof(bss_info->ssid));
338 } else if (g_strcmp0(key, "Privacy") == 0) {
339 gboolean privacy = FALSE;
340 privacy = g_variant_get_boolean(value);
341 bss_info->privacy = privacy;
342 } else if ((g_strcmp0(key, "RSN") == 0) || (g_strcmp0(key, "WPA") == 0)) {
343 _parse_keymgmt_message(value, bss_info);
344 } else if (g_strcmp0(key, "IEs") == 0) {
347 ie = g_variant_get_fixed_array(value, &ie_len, sizeof(guchar));
348 DBG("The IE : %s", ie);
352 g_variant_iter_free(iter);
356 if (bss_info->ssid[0] == '\0') {
361 if (bss_info->security == WIFI_SECURITY_UNKNOWN) {
362 if (bss_info->privacy == TRUE)
363 bss_info->security = WIFI_SECURITY_WEP;
365 bss_info->security = WIFI_SECURITY_NONE;
368 bss_info_list = g_slist_append(bss_info_list, bss_info);
371 gboolean handle_request_specific_scan(Wifi *wifi,
372 GDBusMethodInvocation *context, const gchar *ssid)
374 gboolean result = FALSE;
376 g_return_val_if_fail(wifi != NULL, FALSE);
377 g_return_val_if_fail(ssid != NULL, FALSE);
379 result = wifi_ssid_scan((const char *)ssid);
382 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSpecificScan");
384 wifi_complete_request_wps_scan(wifi, context);