5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <linux/if_arp.h>
32 #include <linux/wireless.h>
36 #define CONNMAN_API_SUBJECT_TO_CHANGE
37 #include <connman/plugin.h>
38 #include <connman/driver.h>
39 #include <connman/dbus.h>
40 #include <connman/log.h>
43 #include "supplicant.h"
45 #define CLEANUP_TIMEOUT 8 /* in seconds */
46 #define INACTIVE_TIMEOUT 12 /* in seconds */
57 static int network_probe(struct connman_element *element)
59 DBG("element %p name %s", element, element->name);
64 static void network_remove(struct connman_element *element)
66 DBG("element %p name %s", element, element->name);
69 static int network_enable(struct connman_element *element)
71 struct connman_device *device = (struct connman_device *) element->parent;
72 char *name, *security = NULL, *passphrase = NULL;
76 DBG("element %p name %s", element, element->name);
78 if (connman_element_get_static_property(element,
79 "Name", &name) == FALSE)
82 if (connman_element_get_static_array_property(element,
83 "WiFi.SSID", &ssid, &ssid_len) == FALSE)
87 struct wifi_data *data = connman_device_get_data(device);
90 if (data->connected == TRUE)
93 g_free(data->identifier);
94 data->identifier = g_strdup(name);
98 connman_element_get_value(element,
99 CONNMAN_PROPERTY_ID_WIFI_SECURITY, &security);
101 connman_element_get_value(element,
102 CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &passphrase);
104 DBG("name %s security %s passhprase %s",
105 name, security, passphrase);
107 if (__supplicant_connect(element, ssid, ssid_len,
108 security, passphrase) < 0)
109 connman_error("Failed to initiate connect");
114 static int network_disable(struct connman_element *element)
116 DBG("element %p name %s", element, element->name);
118 connman_element_unregister_children(element);
120 __supplicant_disconnect(element);
125 static struct connman_driver network_driver = {
126 .name = "wifi-network",
127 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
128 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
129 .probe = network_probe,
130 .remove = network_remove,
131 .enable = network_enable,
132 .disable = network_disable,
135 static struct connman_element *find_current_element(struct wifi_data *data,
136 const char *identifier)
140 for (list = data->current; list; list = list->next) {
141 struct connman_element *element = list->data;
143 if (connman_element_match_static_property(element,
144 "Name", &identifier) == TRUE)
151 static struct connman_element *find_pending_element(struct wifi_data *data,
152 const char *identifier)
156 for (list = data->pending; list; list = list->next) {
157 struct connman_element *element = list->data;
159 if (connman_element_match_static_property(element,
160 "Name", &identifier) == TRUE)
167 static gboolean inactive_scan(gpointer user_data)
169 struct connman_device *device = user_data;
170 struct wifi_data *data = connman_device_get_data(device);
172 DBG("device %p", device);
174 __supplicant_scan(device);
176 data->inactive_timer = 0;
181 static void connect_known_networks(struct connman_device *device)
183 struct wifi_data *data = connman_device_get_data(device);
186 DBG("device %p", device);
188 if (data->inactive_timer > 0) {
189 g_source_remove(data->inactive_timer);
190 data->inactive_timer = 0;
193 for (list = data->current; list; list = list->next) {
194 struct connman_element *element = list->data;
196 if (element->policy == CONNMAN_ELEMENT_POLICY_AUTO &&
197 element->remember == TRUE &&
198 element->available == TRUE) {
199 if (network_enable(element) == 0)
204 data->inactive_timer = g_timeout_add_seconds(INACTIVE_TIMEOUT,
205 inactive_scan, device);
208 static void state_change(struct connman_device *device,
209 enum supplicant_state state)
211 struct wifi_data *data = connman_device_get_data(device);
212 struct connman_element *element;
214 DBG("device %p state %d", device, state);
216 if (state == STATE_SCANNING)
217 connman_device_set_scanning(device, TRUE);
219 connman_device_set_scanning(device, FALSE);
224 DBG("identifier %s", data->identifier);
226 if (data->identifier == NULL)
229 element = find_current_element(data, data->identifier);
233 if (state == STATE_COMPLETED && data->connected == FALSE) {
234 struct connman_element *dhcp;
236 data->connected = TRUE;
237 connman_element_set_enabled(element, TRUE);
239 dhcp = connman_element_create(NULL);
241 dhcp->type = CONNMAN_ELEMENT_TYPE_DHCP;
242 dhcp->index = element->index;
244 if (connman_element_register(dhcp, element) < 0)
245 connman_element_unref(dhcp);
246 } else if (state == STATE_INACTIVE || state == STATE_DISCONNECTED) {
247 data->connected = FALSE;
248 connman_element_set_enabled(element, FALSE);
250 connman_element_unregister_children(element);
254 if (state == STATE_INACTIVE) {
255 data->connected = FALSE;
256 connect_known_networks(device);
260 static gboolean cleanup_pending(gpointer user_data)
262 struct wifi_data *data = user_data;
267 for (list = data->pending; list; list = list->next) {
268 struct connman_element *element = list->data;
270 DBG("element %p name %s", element, element->name);
272 connman_element_unregister(element);
273 connman_element_unref(element);
276 g_slist_free(data->pending);
277 data->pending = NULL;
279 data->cleanup_timer = 0;
284 static void clear_results(struct connman_device *device)
286 struct wifi_data *data = connman_device_get_data(device);
288 DBG("pending %d", g_slist_length(data->pending));
289 DBG("current %d", g_slist_length(data->current));
291 if (data->cleanup_timer > 0) {
292 g_source_remove(data->cleanup_timer);
293 cleanup_pending(data);
296 data->pending = data->current;
297 data->current = NULL;
299 data->cleanup_timer = g_timeout_add_seconds(CLEANUP_TIMEOUT,
300 cleanup_pending, data);
303 static void scan_result(struct connman_device *device,
304 struct supplicant_network *network)
306 struct wifi_data *data = connman_device_get_data(device);
307 struct connman_element *element;
311 DBG("device %p identifier %s", device, network->identifier);
316 if (network->identifier == NULL)
319 if (network->identifier[0] == '\0')
322 temp = g_strdup(network->identifier);
324 for (i = 0; i < strlen(temp); i++) {
326 if ((tmp < '0' || tmp > '9') && (tmp < 'A' || tmp > 'Z') &&
327 (tmp < 'a' || tmp > 'z'))
331 element = find_pending_element(data, network->identifier);
332 if (element == NULL) {
335 element = connman_element_create(temp);
337 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
338 element->subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
339 element->index = connman_device_get_index(device);
341 connman_element_add_static_property(element, "Name",
342 DBUS_TYPE_STRING, &network->identifier);
344 connman_element_add_static_array_property(element, "WiFi.SSID",
345 DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
347 mode = (network->adhoc == TRUE) ? "adhoc" : "managed";
348 connman_element_add_static_property(element, "WiFi.Mode",
349 DBUS_TYPE_STRING, &mode);
351 if (element->wifi.security == NULL) {
352 const char *security;
354 if (network->has_rsn == TRUE)
356 else if (network->has_wpa == TRUE)
358 else if (network->has_wep == TRUE)
363 element->wifi.security = g_strdup(security);
366 element->strength = network->quality;
368 connman_element_add_static_property(element, "Strength",
369 DBUS_TYPE_BYTE, &element->strength);
371 DBG("%s (%s %s) strength %d", network->identifier, mode,
372 element->wifi.security, element->strength);
374 if (connman_element_register(element,
375 (struct connman_element *) device) < 0) {
376 connman_element_unref(element);
380 data->pending = g_slist_remove(data->pending, element);
382 if (element->strength != network->quality) {
383 element->strength = network->quality;
385 connman_element_set_static_property(element, "Strength",
386 DBUS_TYPE_BYTE, &element->strength);
388 connman_element_update(element);
392 data->current = g_slist_append(data->current, element);
394 element->available = TRUE;
400 static struct supplicant_callback wifi_callback = {
401 .state_change = state_change,
402 .clear_results = clear_results,
403 .scan_result = scan_result,
406 static int wifi_probe(struct connman_device *device)
408 struct wifi_data *data;
410 DBG("device %p", device);
412 data = g_try_new0(struct wifi_data, 1);
416 data->connected = FALSE;
418 connman_device_set_data(device, data);
423 static void wifi_remove(struct connman_device *device)
425 struct wifi_data *data = connman_device_get_data(device);
427 DBG("device %p", device);
429 connman_device_set_data(device, NULL);
431 g_free(data->identifier);
435 static int wifi_enable(struct connman_device *device)
439 DBG("device %p", device);
441 err = __supplicant_start(device, &wifi_callback);
445 connman_device_set_powered(device, TRUE);
447 __supplicant_scan(device);
452 static int wifi_disable(struct connman_device *device)
454 struct wifi_data *data = connman_device_get_data(device);
457 DBG("device %p", device);
459 if (data->cleanup_timer > 0) {
460 g_source_remove(data->cleanup_timer);
461 cleanup_pending(data);
464 if (data->inactive_timer > 0) {
465 g_source_remove(data->inactive_timer);
466 data->inactive_timer = 0;
469 for (list = data->current; list; list = list->next) {
470 struct connman_element *network = list->data;
472 if (network->enabled == TRUE)
473 __supplicant_disconnect(network);
475 connman_element_unref(network);
478 g_slist_free(data->current);
479 data->current = NULL;
481 connman_element_unregister_children((struct connman_element *) device);
483 __supplicant_stop(device);
485 connman_device_set_powered(device, FALSE);
490 static int wifi_scan(struct connman_device *device)
492 DBG("device %p", device);
494 __supplicant_scan(device);
499 static struct connman_device_driver wifi_driver = {
501 .type = CONNMAN_DEVICE_TYPE_WIFI,
503 .remove = wifi_remove,
504 .enable = wifi_enable,
505 .disable = wifi_disable,
509 static void supplicant_connect(DBusConnection *connection, void *user_data)
511 DBG("connection %p", connection);
513 __supplicant_init(connection);
515 connman_device_driver_register(&wifi_driver);
518 static void supplicant_disconnect(DBusConnection *connection, void *user_data)
520 DBG("connection %p", connection);
522 connman_device_driver_unregister(&wifi_driver);
527 static DBusConnection *connection;
530 static int wifi_init(void)
534 connection = connman_dbus_get_connection();
535 if (connection == NULL)
538 err = connman_driver_register(&network_driver);
540 dbus_connection_unref(connection);
544 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
545 supplicant_connect, supplicant_disconnect, NULL, NULL);
547 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
548 supplicant_connect(connection, NULL);
550 __supplicant_activate(connection);
555 static void wifi_exit(void)
557 connman_driver_unregister(&network_driver);
560 g_dbus_remove_watch(connection, watch);
562 supplicant_disconnect(connection, NULL);
564 dbus_connection_unref(connection);
567 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
568 wifi_init, wifi_exit)