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>
34 #include <dbus/dbus.h>
36 #define CONNMAN_API_SUBJECT_TO_CHANGE
37 #include <connman/plugin.h>
38 #include <connman/device.h>
39 #include <connman/driver.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 int wifi_probe(struct connman_device *device)
402 struct wifi_data *data;
404 DBG("device %p", device);
406 data = g_try_new0(struct wifi_data, 1);
410 data->connected = FALSE;
412 connman_device_set_data(device, data);
417 static void wifi_remove(struct connman_device *device)
419 struct wifi_data *data = connman_device_get_data(device);
421 DBG("device %p", device);
423 connman_device_set_data(device, NULL);
425 g_free(data->identifier);
429 static int wifi_enable(struct connman_device *device)
433 DBG("device %p", device);
435 err = supplicant_start(device);
439 connman_device_set_powered(device, TRUE);
441 supplicant_scan(device);
446 static int wifi_disable(struct connman_device *device)
448 struct wifi_data *data = connman_device_get_data(device);
451 DBG("device %p", device);
453 if (data->cleanup_timer > 0) {
454 g_source_remove(data->cleanup_timer);
455 cleanup_pending(data);
458 if (data->inactive_timer > 0) {
459 g_source_remove(data->inactive_timer);
460 data->inactive_timer = 0;
463 for (list = data->current; list; list = list->next) {
464 struct connman_element *network = list->data;
466 if (network->enabled == TRUE)
467 __supplicant_disconnect(network);
469 connman_element_unref(network);
472 g_slist_free(data->current);
473 data->current = NULL;
475 connman_element_unregister_children((struct connman_element *) device);
477 supplicant_stop(device);
479 connman_device_set_powered(device, FALSE);
484 static int wifi_scan(struct connman_device *device)
486 DBG("device %p", device);
488 supplicant_scan(device);
493 static struct connman_device_driver wifi_driver = {
495 .type = CONNMAN_DEVICE_TYPE_WIFI,
497 .remove = wifi_remove,
498 .enable = wifi_enable,
499 .disable = wifi_disable,
503 static void wifi_register(void)
507 if (connman_device_driver_register(&wifi_driver) < 0)
508 connman_error("Failed to register WiFi driver");
511 static void wifi_unregister(void)
515 connman_device_driver_unregister(&wifi_driver);
518 static struct supplicant_driver supplicant = {
520 .probe = wifi_register,
521 .remove = wifi_unregister,
523 .state_change = state_change,
524 .clear_results = clear_results,
525 .scan_result = scan_result,
528 static int wifi_init(void)
532 err = connman_driver_register(&network_driver);
536 err = supplicant_register(&supplicant);
538 connman_driver_unregister(&network_driver);
545 static void wifi_exit(void)
547 supplicant_unregister(&supplicant);
549 connman_driver_unregister(&network_driver);
552 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
553 wifi_init, wifi_exit)