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"
49 static int network_probe(struct connman_element *element)
51 DBG("element %p name %s", element, element->name);
56 static void network_remove(struct connman_element *element)
58 DBG("element %p name %s", element, element->name);
61 static int network_enable(struct connman_element *element)
63 char *identifier, *security = NULL, *passphrase = NULL;
67 DBG("element %p name %s", element, element->name);
69 if (connman_element_get_static_property(element,
70 "WiFi.Name", &identifier) == FALSE)
73 if (connman_element_get_static_array_property(element,
74 "WiFi.SSID", &ssid, &ssid_len) == FALSE)
77 if (element->parent != NULL) {
78 struct wifi_data *data = connman_element_get_data(element->parent);
81 g_free(data->identifier);
82 data->identifier = g_strdup(identifier);
86 connman_element_get_value(element,
87 CONNMAN_PROPERTY_ID_WIFI_SECURITY, &security);
89 connman_element_get_value(element,
90 CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &passphrase);
92 DBG("identifier %s security %s passhprase %s",
93 identifier, security, passphrase);
95 if (__supplicant_connect(element, ssid, ssid_len,
96 security, passphrase) < 0)
97 connman_error("Failed to initiate connect");
102 static int network_disable(struct connman_element *element)
104 DBG("element %p name %s", element, element->name);
106 connman_element_unregister_children(element);
108 __supplicant_disconnect(element);
113 static struct connman_driver network_driver = {
114 .name = "wifi-network",
115 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
116 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
117 .probe = network_probe,
118 .remove = network_remove,
119 .enable = network_enable,
120 .disable = network_disable,
123 static struct connman_element *find_element(struct wifi_data *data,
124 const char *identifier)
128 for (list = data->list; list; list = list->next) {
129 struct connman_element *element = list->data;
131 if (connman_element_match_static_property(element,
132 "WiFi.Name", &identifier) == TRUE)
139 static void state_change(struct connman_element *parent,
140 enum supplicant_state state)
142 struct wifi_data *data = connman_element_get_data(parent);
143 struct connman_element *element;
145 DBG("state %d", state);
147 if (data->identifier == NULL)
150 element = find_element(data, data->identifier);
154 if (state == STATE_COMPLETED) {
155 struct connman_element *dhcp;
157 dhcp = connman_element_create(NULL);
159 dhcp->type = CONNMAN_ELEMENT_TYPE_DHCP;
160 dhcp->index = element->index;
162 connman_element_register(dhcp, element);
166 static void scan_result(struct connman_element *parent,
167 struct supplicant_network *network)
169 struct wifi_data *data = connman_element_get_data(parent);
170 struct connman_element *element;
174 DBG("network %p identifier %s", network, network->identifier);
179 if (network->identifier == NULL)
182 if (network->identifier[0] == '\0')
185 temp = g_strdup(network->identifier);
187 for (i = 0; i < strlen(temp); i++) {
188 if (temp[i] == ' ' || temp[i] == '.')
190 else if (temp[i] == '-' || temp[i] == '+')
192 else if (temp[i] == '!' || temp[i] == '?')
194 else if (temp[i] == '(' || temp[i] == ')')
196 else if (g_ascii_isprint(temp[i]) == FALSE)
198 temp[i] = g_ascii_tolower(temp[i]);
201 element = find_element(data, network->identifier);
202 if (element == NULL) {
205 element = connman_element_create(temp);
207 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
208 element->index = parent->index;
210 data->list = g_slist_append(data->list, element);
212 connman_element_add_static_property(element, "WiFi.Name",
213 DBUS_TYPE_STRING, &network->identifier);
215 connman_element_add_static_array_property(element, "WiFi.SSID",
216 DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
218 if (element->wifi.security == NULL) {
219 const char *security;
221 if (network->has_rsn == TRUE)
223 else if (network->has_wpa == TRUE)
225 else if (network->has_wep == TRUE)
230 element->wifi.security = g_strdup(security);
233 strength = network->quality;
235 //connman_element_add_static_property(element, "WiFi.Strength",
236 // DBUS_TYPE_BYTE, &strength);
238 //connman_element_add_static_property(element, "WiFi.Noise",
239 // DBUS_TYPE_INT32, &network->noise);
241 DBG("%s (%s) strength %d", network->identifier,
242 element->wifi.security, strength);
244 connman_element_register(element, parent);
250 static struct supplicant_callback wifi_callback = {
251 .state_change = state_change,
252 .scan_result = scan_result,
255 static int wifi_probe(struct connman_element *element)
257 struct wifi_data *data;
259 DBG("element %p name %s", element, element->name);
261 data = g_try_new0(struct wifi_data, 1);
265 connman_element_set_data(element, data);
270 static void wifi_remove(struct connman_element *element)
272 struct wifi_data *data = connman_element_get_data(element);
274 DBG("element %p name %s", element, element->name);
276 connman_element_set_data(element, NULL);
278 g_free(data->identifier);
282 static int wifi_update(struct connman_element *element)
284 DBG("element %p name %s", element, element->name);
286 __supplicant_scan(element);
291 static int wifi_enable(struct connman_element *element)
295 DBG("element %p name %s", element, element->name);
297 err = __supplicant_start(element, &wifi_callback);
301 __supplicant_scan(element);
306 static int wifi_disable(struct connman_element *element)
308 struct wifi_data *data = connman_element_get_data(element);
311 DBG("element %p name %s", element, element->name);
313 __supplicant_disconnect(element);
315 for (list = data->list; list; list = list->next) {
316 struct connman_element *network = list->data;
318 connman_element_unref(network);
321 g_slist_free(data->list);
324 connman_element_unregister_children(element);
329 static struct connman_driver wifi_driver = {
330 .name = "wifi-device",
331 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
332 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
334 .remove = wifi_remove,
335 .update = wifi_update,
336 .enable = wifi_enable,
337 .disable = wifi_disable,
340 static GSList *device_list = NULL;
342 static void wifi_newlink(unsigned short type, int index,
343 unsigned flags, unsigned change)
345 struct connman_element *device;
347 gboolean exists = FALSE;
352 DBG("index %d", index);
354 if (type != ARPHRD_ETHER)
357 name = inet_index2name(index);
359 memset(&iwr, 0, sizeof(iwr));
360 strncpy(iwr.ifr_ifrn.ifrn_name, name, IFNAMSIZ);
362 sk = socket(PF_INET, SOCK_DGRAM, 0);
364 if (ioctl(sk, SIOCGIWNAME, &iwr) < 0) {
372 for (list = device_list; list; list = list->next) {
373 struct connman_element *device = list->data;
375 if (device->index == index) {
381 if (exists == TRUE) {
386 device = connman_element_create(NULL);
387 device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
388 device->subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
390 device->index = index;
393 connman_element_register(device, NULL);
394 device_list = g_slist_append(device_list, device);
397 static void wifi_dellink(unsigned short type, int index,
398 unsigned flags, unsigned change)
402 DBG("index %d", index);
404 for (list = device_list; list; list = list->next) {
405 struct connman_element *device = list->data;
407 if (device->index == index) {
408 device_list = g_slist_remove(device_list, device);
409 connman_element_unregister(device);
410 connman_element_unref(device);
416 static struct connman_rtnl wifi_rtnl = {
418 .newlink = wifi_newlink,
419 .dellink = wifi_dellink,
422 static void supplicant_connect(DBusConnection *connection, void *user_data)
424 DBG("connection %p", connection);
426 __supplicant_init(connection);
428 if (connman_rtnl_register(&wifi_rtnl) < 0)
431 connman_rtnl_send_getlink();
434 static void supplicant_disconnect(DBusConnection *connection, void *user_data)
438 DBG("connection %p", connection);
440 connman_rtnl_unregister(&wifi_rtnl);
442 for (list = device_list; list; list = list->next) {
443 struct connman_element *device = list->data;
445 connman_element_unregister(device);
446 connman_element_unref(device);
449 g_slist_free(device_list);
455 static DBusConnection *connection;
458 static int wifi_init(void)
462 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
463 if (connection == NULL)
466 err = connman_driver_register(&network_driver);
468 dbus_connection_unref(connection);
472 err = connman_driver_register(&wifi_driver);
474 connman_driver_unregister(&network_driver);
475 dbus_connection_unref(connection);
479 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
480 supplicant_connect, supplicant_disconnect, NULL, NULL);
482 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
483 supplicant_connect(connection, NULL);
488 static void wifi_exit(void)
490 connman_driver_unregister(&network_driver);
491 connman_driver_unregister(&wifi_driver);
494 g_dbus_remove_watch(connection, watch);
496 supplicant_disconnect(connection, NULL);
498 dbus_connection_unref(connection);
501 CONNMAN_PLUGIN_DEFINE("wifi", "WiFi interface plugin", VERSION,
502 wifi_init, wifi_exit)