5 * Copyright (C) 2007-2008 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 #include <connman/plugin.h>
37 #include <connman/driver.h>
38 #include <connman/rtnl.h>
39 #include <connman/log.h>
42 #include "supplicant.h"
44 #define CLEANUP_TIMEOUT 8 /* in seconds */
45 #define INACTIVE_TIMEOUT 12 /* in seconds */
56 static int network_probe(struct connman_element *element)
58 DBG("element %p name %s", element, element->name);
63 static void network_remove(struct connman_element *element)
65 DBG("element %p name %s", element, element->name);
68 static int network_enable(struct connman_element *element)
70 struct connman_element *device = element->parent;
71 char *name, *security = NULL, *passphrase = NULL;
75 DBG("element %p name %s", element, element->name);
77 if (connman_element_get_static_property(element,
78 "Name", &name) == FALSE)
81 if (connman_element_get_static_array_property(element,
82 "WiFi.SSID", &ssid, &ssid_len) == FALSE)
86 struct wifi_data *data = connman_element_get_data(device);
89 if (data->connected == TRUE)
92 g_free(data->identifier);
93 data->identifier = g_strdup(name);
97 connman_element_get_value(element,
98 CONNMAN_PROPERTY_ID_WIFI_SECURITY, &security);
100 connman_element_get_value(element,
101 CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &passphrase);
103 DBG("name %s security %s passhprase %s",
104 name, security, passphrase);
106 if (__supplicant_connect(element, ssid, ssid_len,
107 security, passphrase) < 0)
108 connman_error("Failed to initiate connect");
113 static int network_disable(struct connman_element *element)
115 DBG("element %p name %s", element, element->name);
117 connman_element_unregister_children(element);
119 __supplicant_disconnect(element);
124 static struct connman_driver network_driver = {
125 .name = "wifi-network",
126 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
127 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
128 .probe = network_probe,
129 .remove = network_remove,
130 .enable = network_enable,
131 .disable = network_disable,
134 static struct connman_element *find_current_element(struct wifi_data *data,
135 const char *identifier)
139 for (list = data->current; list; list = list->next) {
140 struct connman_element *element = list->data;
142 if (connman_element_match_static_property(element,
143 "Name", &identifier) == TRUE)
150 static struct connman_element *find_pending_element(struct wifi_data *data,
151 const char *identifier)
155 for (list = data->pending; list; list = list->next) {
156 struct connman_element *element = list->data;
158 if (connman_element_match_static_property(element,
159 "Name", &identifier) == TRUE)
166 static gboolean inactive_scan(gpointer user_data)
168 struct connman_element *device = user_data;
169 struct wifi_data *data = connman_element_get_data(device);
173 if (data->cleanup_timer > 0) {
174 g_source_remove(data->cleanup_timer);
175 data->cleanup_timer = 0;
178 __supplicant_scan(device);
180 data->inactive_timer = 0;
185 static void state_change(struct connman_element *device,
186 enum supplicant_state state)
188 struct wifi_data *data = connman_element_get_data(device);
189 struct connman_element *element;
191 DBG("state %d", state);
193 if (state == STATE_INACTIVE && data->inactive_timer == 0)
194 data->inactive_timer = g_timeout_add_seconds(INACTIVE_TIMEOUT,
195 inactive_scan, device);
200 if (data->identifier == NULL)
203 element = find_current_element(data, data->identifier);
207 if (state == STATE_COMPLETED) {
208 struct connman_element *dhcp;
210 data->connected = TRUE;
212 dhcp = connman_element_create(NULL);
214 dhcp->type = CONNMAN_ELEMENT_TYPE_DHCP;
215 dhcp->index = element->index;
217 connman_element_register(dhcp, element);
218 } else if (state == STATE_DISCONNECTED || state == STATE_INACTIVE)
219 data->connected = FALSE;
222 static gboolean cleanup_pending(gpointer user_data)
224 struct wifi_data *data = user_data;
229 for (list = data->pending; list; list = list->next) {
230 struct connman_element *element = list->data;
232 DBG("element %p name %s", element, element->name);
234 connman_element_unregister(element);
235 connman_element_unref(element);
238 g_slist_free(data->pending);
239 data->pending = NULL;
241 data->cleanup_timer = 0;
246 static void clear_results(struct connman_element *device)
248 struct wifi_data *data = connman_element_get_data(device);
250 DBG("pending %d", g_slist_length(data->pending));
251 DBG("current %d", g_slist_length(data->current));
253 data->pending = data->current;
254 data->current = NULL;
256 if (data->cleanup_timer > 0)
259 data->cleanup_timer = g_timeout_add_seconds(CLEANUP_TIMEOUT,
260 cleanup_pending, data);
263 static void scan_result(struct connman_element *device,
264 struct supplicant_network *network)
266 struct wifi_data *data = connman_element_get_data(device);
267 struct connman_element *element;
271 DBG("network %p identifier %s", network, network->identifier);
276 if (network->identifier == NULL)
279 if (network->identifier[0] == '\0')
282 temp = g_strdup(network->identifier);
284 for (i = 0; i < strlen(temp); i++) {
285 gchar tmp = g_ascii_tolower(temp[i]);
287 if (tmp < 'a' || tmp > 'z')
291 element = find_pending_element(data, network->identifier);
292 if (element == NULL) {
295 element = connman_element_create(temp);
297 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
298 element->index = device->index;
300 connman_element_add_static_property(element, "Name",
301 DBUS_TYPE_STRING, &network->identifier);
303 connman_element_add_static_array_property(element, "WiFi.SSID",
304 DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
306 if (element->wifi.security == NULL) {
307 const char *security;
309 if (network->has_rsn == TRUE)
311 else if (network->has_wpa == TRUE)
313 else if (network->has_wep == TRUE)
318 element->wifi.security = g_strdup(security);
321 strength = network->quality;
323 connman_element_add_static_property(element, "WiFi.Strength",
324 DBUS_TYPE_BYTE, &strength);
326 //connman_element_add_static_property(element, "WiFi.Noise",
327 // DBUS_TYPE_INT32, &network->noise);
329 DBG("%s (%s) strength %d", network->identifier,
330 element->wifi.security, strength);
332 connman_element_register(element, device);
334 data->pending = g_slist_remove(data->pending, element);
336 data->current = g_slist_append(data->current, element);
338 element->available = TRUE;
343 static struct supplicant_callback wifi_callback = {
344 .state_change = state_change,
345 .clear_results = clear_results,
346 .scan_result = scan_result,
349 static int wifi_probe(struct connman_element *element)
351 struct wifi_data *data;
353 DBG("element %p name %s", element, element->name);
355 data = g_try_new0(struct wifi_data, 1);
359 data->connected = FALSE;
361 connman_element_set_data(element, data);
366 static void wifi_remove(struct connman_element *element)
368 struct wifi_data *data = connman_element_get_data(element);
370 DBG("element %p name %s", element, element->name);
372 connman_element_set_data(element, NULL);
374 g_free(data->identifier);
378 static int wifi_update(struct connman_element *element)
380 DBG("element %p name %s", element, element->name);
382 __supplicant_scan(element);
387 static int wifi_enable(struct connman_element *element)
391 DBG("element %p name %s", element, element->name);
393 err = __supplicant_start(element, &wifi_callback);
397 __supplicant_scan(element);
402 static int wifi_disable(struct connman_element *element)
404 struct wifi_data *data = connman_element_get_data(element);
407 DBG("element %p name %s", element, element->name);
409 if (data->cleanup_timer > 0) {
410 g_source_remove(data->cleanup_timer);
411 data->cleanup_timer = 0;
414 if (data->inactive_timer > 0) {
415 g_source_remove(data->inactive_timer);
416 data->inactive_timer = 0;
419 __supplicant_disconnect(element);
421 for (list = data->pending; list; list = list->next) {
422 struct connman_element *network = list->data;
424 connman_element_unref(network);
427 g_slist_free(data->pending);
428 data->pending = NULL;
430 for (list = data->current; list; list = list->next) {
431 struct connman_element *network = list->data;
433 connman_element_unref(network);
436 g_slist_free(data->current);
437 data->current = NULL;
439 connman_element_unregister_children(element);
441 __supplicant_stop(element);
446 static struct connman_driver wifi_driver = {
447 .name = "wifi-device",
448 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
449 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
451 .remove = wifi_remove,
452 .update = wifi_update,
453 .enable = wifi_enable,
454 .disable = wifi_disable,
457 static GSList *device_list = NULL;
459 static void wifi_newlink(unsigned short type, int index,
460 unsigned flags, unsigned change)
462 struct connman_element *device;
464 gboolean exists = FALSE;
465 gchar *name, *devname;
469 DBG("index %d", index);
471 if (type != ARPHRD_ETHER)
474 name = inet_index2ident(index, "dev_");
475 devname = inet_index2name(index);
477 memset(&iwr, 0, sizeof(iwr));
478 strncpy(iwr.ifr_ifrn.ifrn_name, devname, IFNAMSIZ);
480 sk = socket(PF_INET, SOCK_DGRAM, 0);
482 if (ioctl(sk, SIOCGIWNAME, &iwr) < 0) {
490 for (list = device_list; list; list = list->next) {
491 struct connman_element *device = list->data;
493 if (device->index == index) {
499 if (exists == TRUE) {
504 device = connman_element_create(NULL);
505 device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
506 device->subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
508 device->index = index;
510 device->devname = devname;
512 connman_element_register(device, NULL);
513 device_list = g_slist_append(device_list, device);
516 static void wifi_dellink(unsigned short type, int index,
517 unsigned flags, unsigned change)
521 DBG("index %d", index);
523 for (list = device_list; list; list = list->next) {
524 struct connman_element *device = list->data;
526 if (device->index == index) {
527 device_list = g_slist_remove(device_list, device);
528 connman_element_unregister(device);
529 connman_element_unref(device);
535 static struct connman_rtnl wifi_rtnl = {
537 .newlink = wifi_newlink,
538 .dellink = wifi_dellink,
541 static void supplicant_connect(DBusConnection *connection, void *user_data)
543 DBG("connection %p", connection);
545 __supplicant_init(connection);
547 if (connman_rtnl_register(&wifi_rtnl) < 0)
550 connman_rtnl_send_getlink();
553 static void supplicant_disconnect(DBusConnection *connection, void *user_data)
557 DBG("connection %p", connection);
559 connman_rtnl_unregister(&wifi_rtnl);
561 for (list = device_list; list; list = list->next) {
562 struct connman_element *device = list->data;
564 connman_element_unregister(device);
565 connman_element_unref(device);
568 g_slist_free(device_list);
574 static DBusConnection *connection;
577 static int wifi_init(void)
581 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
582 if (connection == NULL)
585 err = connman_driver_register(&network_driver);
587 dbus_connection_unref(connection);
591 err = connman_driver_register(&wifi_driver);
593 connman_driver_unregister(&network_driver);
594 dbus_connection_unref(connection);
598 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
599 supplicant_connect, supplicant_disconnect, NULL, NULL);
601 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
602 supplicant_connect(connection, NULL);
607 static void wifi_exit(void)
609 connman_driver_unregister(&network_driver);
610 connman_driver_unregister(&wifi_driver);
613 g_dbus_remove_watch(connection, watch);
615 supplicant_disconnect(connection, NULL);
617 dbus_connection_unref(connection);
620 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
621 wifi_init, wifi_exit)