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_PENDING_TIMEOUT 8 /* in seconds */
54 static int network_probe(struct connman_element *element)
56 DBG("element %p name %s", element, element->name);
61 static void network_remove(struct connman_element *element)
63 DBG("element %p name %s", element, element->name);
66 static int network_enable(struct connman_element *element)
68 char *name, *security = NULL, *passphrase = NULL;
72 DBG("element %p name %s", element, element->name);
74 if (connman_element_get_static_property(element,
75 "Name", &name) == FALSE)
78 if (connman_element_get_static_array_property(element,
79 "WiFi.SSID", &ssid, &ssid_len) == FALSE)
82 if (element->parent != NULL) {
83 struct wifi_data *data = connman_element_get_data(element->parent);
85 if (data->connected == TRUE)
89 g_free(data->identifier);
90 data->identifier = g_strdup(name);
94 connman_element_get_value(element,
95 CONNMAN_PROPERTY_ID_WIFI_SECURITY, &security);
97 connman_element_get_value(element,
98 CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &passphrase);
100 DBG("name %s security %s passhprase %s",
101 name, security, passphrase);
103 if (__supplicant_connect(element, ssid, ssid_len,
104 security, passphrase) < 0)
105 connman_error("Failed to initiate connect");
110 static int network_disable(struct connman_element *element)
112 DBG("element %p name %s", element, element->name);
114 connman_element_unregister_children(element);
116 __supplicant_disconnect(element);
121 static struct connman_driver network_driver = {
122 .name = "wifi-network",
123 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
124 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
125 .probe = network_probe,
126 .remove = network_remove,
127 .enable = network_enable,
128 .disable = network_disable,
131 static struct connman_element *find_current_element(struct wifi_data *data,
132 const char *identifier)
136 for (list = data->current; list; list = list->next) {
137 struct connman_element *element = list->data;
139 if (connman_element_match_static_property(element,
140 "Name", &identifier) == TRUE)
147 static struct connman_element *find_pending_element(struct wifi_data *data,
148 const char *identifier)
152 for (list = data->pending; list; list = list->next) {
153 struct connman_element *element = list->data;
155 if (connman_element_match_static_property(element,
156 "Name", &identifier) == TRUE)
163 static void state_change(struct connman_element *parent,
164 enum supplicant_state state)
166 struct wifi_data *data = connman_element_get_data(parent);
167 struct connman_element *element;
169 DBG("state %d", state);
174 if (data->identifier == NULL)
177 element = find_current_element(data, data->identifier);
181 if (state == STATE_COMPLETED) {
182 struct connman_element *dhcp;
184 data->connected = TRUE;
186 dhcp = connman_element_create(NULL);
188 dhcp->type = CONNMAN_ELEMENT_TYPE_DHCP;
189 dhcp->index = element->index;
191 connman_element_register(dhcp, element);
192 } else if (state == STATE_DISCONNECTED || state == STATE_INACTIVE)
193 data->connected = FALSE;
196 static gboolean cleanup_pending(gpointer user_data)
198 struct wifi_data *data = user_data;
203 for (list = data->pending; list; list = list->next) {
204 struct connman_element *element = list->data;
206 DBG("element %p name %s", element, element->name);
208 connman_element_unregister(element);
209 connman_element_unref(element);
212 g_slist_free(data->pending);
213 data->pending = NULL;
220 static void clear_results(struct connman_element *parent)
222 struct wifi_data *data = connman_element_get_data(parent);
225 DBG("pending %d", g_slist_length(data->pending));
226 DBG("current %d", g_slist_length(data->current));
228 data->pending = data->current;
229 data->current = NULL;
231 if (data->timer == 0)
232 data->timer = g_timeout_add_seconds(CLEANUP_PENDING_TIMEOUT,
233 cleanup_pending, data);
236 static void scan_result(struct connman_element *parent,
237 struct supplicant_network *network)
239 struct wifi_data *data = connman_element_get_data(parent);
240 struct connman_element *element;
244 DBG("network %p identifier %s", network, network->identifier);
249 if (network->identifier == NULL)
252 if (network->identifier[0] == '\0')
255 temp = g_strdup(network->identifier);
257 for (i = 0; i < strlen(temp); i++) {
258 if (temp[i] == ' ' || temp[i] == '.')
260 else if (temp[i] == '-' || temp[i] == '+')
262 else if (temp[i] == '!' || temp[i] == '?')
264 else if (temp[i] == '(' || temp[i] == ')')
266 else if (g_ascii_isprint(temp[i]) == FALSE)
268 temp[i] = g_ascii_tolower(temp[i]);
271 element = find_pending_element(data, network->identifier);
272 if (element == NULL) {
275 element = connman_element_create(temp);
277 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
278 element->index = parent->index;
280 connman_element_add_static_property(element, "Name",
281 DBUS_TYPE_STRING, &network->identifier);
283 connman_element_add_static_array_property(element, "WiFi.SSID",
284 DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
286 if (element->wifi.security == NULL) {
287 const char *security;
289 if (network->has_rsn == TRUE)
291 else if (network->has_wpa == TRUE)
293 else if (network->has_wep == TRUE)
298 element->wifi.security = g_strdup(security);
301 strength = network->quality;
303 connman_element_add_static_property(element, "WiFi.Strength",
304 DBUS_TYPE_BYTE, &strength);
306 //connman_element_add_static_property(element, "WiFi.Noise",
307 // DBUS_TYPE_INT32, &network->noise);
309 DBG("%s (%s) strength %d", network->identifier,
310 element->wifi.security, strength);
312 connman_element_register(element, parent);
314 data->pending = g_slist_remove(data->pending, element);
316 data->current = g_slist_append(data->current, element);
318 element->available = TRUE;
323 static struct supplicant_callback wifi_callback = {
324 .state_change = state_change,
325 .clear_results = clear_results,
326 .scan_result = scan_result,
329 static int wifi_probe(struct connman_element *element)
331 struct wifi_data *data;
333 DBG("element %p name %s", element, element->name);
335 data = g_try_new0(struct wifi_data, 1);
339 data->connected = FALSE;
341 connman_element_set_data(element, data);
346 static void wifi_remove(struct connman_element *element)
348 struct wifi_data *data = connman_element_get_data(element);
350 DBG("element %p name %s", element, element->name);
352 connman_element_set_data(element, NULL);
354 g_free(data->identifier);
358 static int wifi_update(struct connman_element *element)
360 DBG("element %p name %s", element, element->name);
362 __supplicant_scan(element);
367 static int wifi_enable(struct connman_element *element)
371 DBG("element %p name %s", element, element->name);
373 err = __supplicant_start(element, &wifi_callback);
377 __supplicant_scan(element);
382 static int wifi_disable(struct connman_element *element)
384 struct wifi_data *data = connman_element_get_data(element);
387 DBG("element %p name %s", element, element->name);
389 if (data->timer > 0) {
390 g_source_remove(data->timer);
394 __supplicant_disconnect(element);
396 for (list = data->pending; list; list = list->next) {
397 struct connman_element *network = list->data;
399 connman_element_unref(network);
402 g_slist_free(data->pending);
403 data->pending = NULL;
405 for (list = data->current; list; list = list->next) {
406 struct connman_element *network = list->data;
408 connman_element_unref(network);
411 g_slist_free(data->current);
412 data->current = NULL;
414 connman_element_unregister_children(element);
416 __supplicant_stop(element);
421 static struct connman_driver wifi_driver = {
422 .name = "wifi-device",
423 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
424 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
426 .remove = wifi_remove,
427 .update = wifi_update,
428 .enable = wifi_enable,
429 .disable = wifi_disable,
432 static GSList *device_list = NULL;
434 static void wifi_newlink(unsigned short type, int index,
435 unsigned flags, unsigned change)
437 struct connman_element *device;
439 gboolean exists = FALSE;
440 gchar *name, *devname;
444 DBG("index %d", index);
446 if (type != ARPHRD_ETHER)
449 name = inet_index2ident(index, "dev_");
450 devname = inet_index2name(index);
452 memset(&iwr, 0, sizeof(iwr));
453 strncpy(iwr.ifr_ifrn.ifrn_name, devname, IFNAMSIZ);
455 sk = socket(PF_INET, SOCK_DGRAM, 0);
457 if (ioctl(sk, SIOCGIWNAME, &iwr) < 0) {
465 for (list = device_list; list; list = list->next) {
466 struct connman_element *device = list->data;
468 if (device->index == index) {
474 if (exists == TRUE) {
479 device = connman_element_create(NULL);
480 device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
481 device->subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
483 device->index = index;
485 device->devname = devname;
487 connman_element_register(device, NULL);
488 device_list = g_slist_append(device_list, device);
491 static void wifi_dellink(unsigned short type, int index,
492 unsigned flags, unsigned change)
496 DBG("index %d", index);
498 for (list = device_list; list; list = list->next) {
499 struct connman_element *device = list->data;
501 if (device->index == index) {
502 device_list = g_slist_remove(device_list, device);
503 connman_element_unregister(device);
504 connman_element_unref(device);
510 static struct connman_rtnl wifi_rtnl = {
512 .newlink = wifi_newlink,
513 .dellink = wifi_dellink,
516 static void supplicant_connect(DBusConnection *connection, void *user_data)
518 DBG("connection %p", connection);
520 __supplicant_init(connection);
522 if (connman_rtnl_register(&wifi_rtnl) < 0)
525 connman_rtnl_send_getlink();
528 static void supplicant_disconnect(DBusConnection *connection, void *user_data)
532 DBG("connection %p", connection);
534 connman_rtnl_unregister(&wifi_rtnl);
536 for (list = device_list; list; list = list->next) {
537 struct connman_element *device = list->data;
539 connman_element_unregister(device);
540 connman_element_unref(device);
543 g_slist_free(device_list);
549 static DBusConnection *connection;
552 static int wifi_init(void)
556 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
557 if (connection == NULL)
560 err = connman_driver_register(&network_driver);
562 dbus_connection_unref(connection);
566 err = connman_driver_register(&wifi_driver);
568 connman_driver_unregister(&network_driver);
569 dbus_connection_unref(connection);
573 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
574 supplicant_connect, supplicant_disconnect, NULL, NULL);
576 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
577 supplicant_connect(connection, NULL);
582 static void wifi_exit(void)
584 connman_driver_unregister(&network_driver);
585 connman_driver_unregister(&wifi_driver);
588 g_dbus_remove_watch(connection, watch);
590 supplicant_disconnect(connection, NULL);
592 dbus_connection_unref(connection);
595 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
596 wifi_init, wifi_exit)