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"
50 static int network_probe(struct connman_element *element)
52 DBG("element %p name %s", element, element->name);
57 static void network_remove(struct connman_element *element)
59 DBG("element %p name %s", element, element->name);
62 static int network_enable(struct connman_element *element)
64 char *identifier, *security = NULL, *passphrase = NULL;
68 DBG("element %p name %s", element, element->name);
70 if (connman_element_get_static_property(element,
71 "WiFi.Name", &identifier) == FALSE)
74 if (connman_element_get_static_array_property(element,
75 "WiFi.SSID", &ssid, &ssid_len) == FALSE)
78 if (element->parent != NULL) {
79 struct wifi_data *data = connman_element_get_data(element->parent);
81 if (data->connected == TRUE)
85 g_free(data->identifier);
86 data->identifier = g_strdup(identifier);
90 connman_element_get_value(element,
91 CONNMAN_PROPERTY_ID_WIFI_SECURITY, &security);
93 connman_element_get_value(element,
94 CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &passphrase);
96 DBG("identifier %s security %s passhprase %s",
97 identifier, security, passphrase);
99 if (__supplicant_connect(element, ssid, ssid_len,
100 security, passphrase) < 0)
101 connman_error("Failed to initiate connect");
106 static int network_disable(struct connman_element *element)
108 DBG("element %p name %s", element, element->name);
110 connman_element_unregister_children(element);
112 __supplicant_disconnect(element);
117 static struct connman_driver network_driver = {
118 .name = "wifi-network",
119 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
120 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
121 .probe = network_probe,
122 .remove = network_remove,
123 .enable = network_enable,
124 .disable = network_disable,
127 static struct connman_element *find_element(struct wifi_data *data,
128 const char *identifier)
132 for (list = data->list; list; list = list->next) {
133 struct connman_element *element = list->data;
135 if (connman_element_match_static_property(element,
136 "WiFi.Name", &identifier) == TRUE)
143 static void state_change(struct connman_element *parent,
144 enum supplicant_state state)
146 struct wifi_data *data = connman_element_get_data(parent);
147 struct connman_element *element;
149 DBG("state %d", state);
151 if (data->identifier == NULL)
154 element = find_element(data, data->identifier);
158 if (state == STATE_COMPLETED) {
159 struct connman_element *dhcp;
161 data->connected = TRUE;
163 dhcp = connman_element_create(NULL);
165 dhcp->type = CONNMAN_ELEMENT_TYPE_DHCP;
166 dhcp->index = element->index;
168 connman_element_register(dhcp, element);
169 } else if (state == STATE_DISCONNECTED || state == STATE_INACTIVE)
170 data->connected = FALSE;
173 static void scan_result(struct connman_element *parent,
174 struct supplicant_network *network)
176 struct wifi_data *data = connman_element_get_data(parent);
177 struct connman_element *element;
181 DBG("network %p identifier %s", network, network->identifier);
186 if (network->identifier == NULL)
189 if (network->identifier[0] == '\0')
192 temp = g_strdup(network->identifier);
194 for (i = 0; i < strlen(temp); i++) {
195 if (temp[i] == ' ' || temp[i] == '.')
197 else if (temp[i] == '-' || temp[i] == '+')
199 else if (temp[i] == '!' || temp[i] == '?')
201 else if (temp[i] == '(' || temp[i] == ')')
203 else if (g_ascii_isprint(temp[i]) == FALSE)
205 temp[i] = g_ascii_tolower(temp[i]);
208 element = find_element(data, network->identifier);
209 if (element == NULL) {
212 element = connman_element_create(temp);
214 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
215 element->index = parent->index;
217 data->list = g_slist_append(data->list, element);
219 connman_element_add_static_property(element, "WiFi.Name",
220 DBUS_TYPE_STRING, &network->identifier);
222 connman_element_add_static_array_property(element, "WiFi.SSID",
223 DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
225 if (element->wifi.security == NULL) {
226 const char *security;
228 if (network->has_rsn == TRUE)
230 else if (network->has_wpa == TRUE)
232 else if (network->has_wep == TRUE)
237 element->wifi.security = g_strdup(security);
240 strength = network->quality;
242 //connman_element_add_static_property(element, "WiFi.Strength",
243 // DBUS_TYPE_BYTE, &strength);
245 //connman_element_add_static_property(element, "WiFi.Noise",
246 // DBUS_TYPE_INT32, &network->noise);
248 DBG("%s (%s) strength %d", network->identifier,
249 element->wifi.security, strength);
251 connman_element_register(element, parent);
257 static struct supplicant_callback wifi_callback = {
258 .state_change = state_change,
259 .scan_result = scan_result,
262 static int wifi_probe(struct connman_element *element)
264 struct wifi_data *data;
266 DBG("element %p name %s", element, element->name);
268 data = g_try_new0(struct wifi_data, 1);
272 data->connected = FALSE;
274 connman_element_set_data(element, data);
279 static void wifi_remove(struct connman_element *element)
281 struct wifi_data *data = connman_element_get_data(element);
283 DBG("element %p name %s", element, element->name);
285 connman_element_set_data(element, NULL);
287 g_free(data->identifier);
291 static int wifi_update(struct connman_element *element)
293 DBG("element %p name %s", element, element->name);
295 __supplicant_scan(element);
300 static int wifi_enable(struct connman_element *element)
304 DBG("element %p name %s", element, element->name);
306 err = __supplicant_start(element, &wifi_callback);
310 __supplicant_scan(element);
315 static int wifi_disable(struct connman_element *element)
317 struct wifi_data *data = connman_element_get_data(element);
320 DBG("element %p name %s", element, element->name);
322 __supplicant_disconnect(element);
324 for (list = data->list; list; list = list->next) {
325 struct connman_element *network = list->data;
327 connman_element_unref(network);
330 g_slist_free(data->list);
333 connman_element_unregister_children(element);
338 static struct connman_driver wifi_driver = {
339 .name = "wifi-device",
340 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
341 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
343 .remove = wifi_remove,
344 .update = wifi_update,
345 .enable = wifi_enable,
346 .disable = wifi_disable,
349 static GSList *device_list = NULL;
351 static void wifi_newlink(unsigned short type, int index,
352 unsigned flags, unsigned change)
354 struct connman_element *device;
356 gboolean exists = FALSE;
361 DBG("index %d", index);
363 if (type != ARPHRD_ETHER)
366 name = inet_index2name(index);
368 memset(&iwr, 0, sizeof(iwr));
369 strncpy(iwr.ifr_ifrn.ifrn_name, name, IFNAMSIZ);
371 sk = socket(PF_INET, SOCK_DGRAM, 0);
373 if (ioctl(sk, SIOCGIWNAME, &iwr) < 0) {
381 for (list = device_list; list; list = list->next) {
382 struct connman_element *device = list->data;
384 if (device->index == index) {
390 if (exists == TRUE) {
395 device = connman_element_create(NULL);
396 device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
397 device->subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
399 device->index = index;
402 connman_element_register(device, NULL);
403 device_list = g_slist_append(device_list, device);
406 static void wifi_dellink(unsigned short type, int index,
407 unsigned flags, unsigned change)
411 DBG("index %d", index);
413 for (list = device_list; list; list = list->next) {
414 struct connman_element *device = list->data;
416 if (device->index == index) {
417 device_list = g_slist_remove(device_list, device);
418 connman_element_unregister(device);
419 connman_element_unref(device);
425 static struct connman_rtnl wifi_rtnl = {
427 .newlink = wifi_newlink,
428 .dellink = wifi_dellink,
431 static void supplicant_connect(DBusConnection *connection, void *user_data)
433 DBG("connection %p", connection);
435 __supplicant_init(connection);
437 if (connman_rtnl_register(&wifi_rtnl) < 0)
440 connman_rtnl_send_getlink();
443 static void supplicant_disconnect(DBusConnection *connection, void *user_data)
447 DBG("connection %p", connection);
449 connman_rtnl_unregister(&wifi_rtnl);
451 for (list = device_list; list; list = list->next) {
452 struct connman_element *device = list->data;
454 connman_element_unregister(device);
455 connman_element_unref(device);
458 g_slist_free(device_list);
464 static DBusConnection *connection;
467 static int wifi_init(void)
471 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
472 if (connection == NULL)
475 err = connman_driver_register(&network_driver);
477 dbus_connection_unref(connection);
481 err = connman_driver_register(&wifi_driver);
483 connman_driver_unregister(&network_driver);
484 dbus_connection_unref(connection);
488 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
489 supplicant_connect, supplicant_disconnect, NULL, NULL);
491 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
492 supplicant_connect(connection, NULL);
497 static void wifi_exit(void)
499 connman_driver_unregister(&network_driver);
500 connman_driver_unregister(&wifi_driver);
503 g_dbus_remove_watch(connection, watch);
505 supplicant_disconnect(connection, NULL);
507 dbus_connection_unref(connection);
510 CONNMAN_PLUGIN_DEFINE("wifi", "WiFi interface plugin", VERSION,
511 wifi_init, wifi_exit)