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, *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);
82 g_free(data->identifier);
83 data->identifier = g_strdup(identifier);
87 connman_element_get_value(element,
88 CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &passphrase);
90 DBG("identifier %s passhprase %s", identifier, passphrase);
92 if (__supplicant_connect(element, ssid, ssid_len, passphrase) < 0)
93 connman_error("Failed to initiate connect");
98 static int network_disable(struct connman_element *element)
100 DBG("element %p name %s", element, element->name);
102 connman_element_unregister_children(element);
104 __supplicant_disconnect(element);
109 static struct connman_driver network_driver = {
110 .name = "wifi-network",
111 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
112 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
113 .probe = network_probe,
114 .remove = network_remove,
115 .enable = network_enable,
116 .disable = network_disable,
119 static struct connman_element *find_element(struct wifi_data *data,
120 const char *identifier)
124 for (list = data->list; list; list = list->next) {
125 struct connman_element *element = list->data;
127 if (connman_element_match_static_property(element,
128 "WiFi.Name", &identifier) == TRUE)
135 static void state_change(struct connman_element *parent,
136 enum supplicant_state state)
138 struct wifi_data *data = connman_element_get_data(parent);
139 struct connman_element *element;
141 DBG("state %d", state);
143 if (data->identifier == NULL)
146 element = find_element(data, data->identifier);
150 if (state == STATE_COMPLETED) {
151 struct connman_element *dhcp;
153 dhcp = connman_element_create(NULL);
155 dhcp->type = CONNMAN_ELEMENT_TYPE_DHCP;
156 dhcp->index = element->index;
158 connman_element_register(dhcp, element);
162 static void scan_result(struct connman_element *parent,
163 struct supplicant_network *network)
165 struct wifi_data *data = connman_element_get_data(parent);
166 struct connman_element *element;
170 DBG("network %p identifier %s", network, network->identifier);
175 if (network->identifier == NULL)
178 if (network->identifier[0] == '\0')
181 temp = g_strdup(network->identifier);
183 for (i = 0; i < strlen(temp); i++) {
184 if (temp[i] == ' ' || temp[i] == '.' || temp[i] == '-')
186 if (temp[i] == '(' || temp[i] == ')')
188 if (g_ascii_isprint(temp[i]) == FALSE)
190 temp[i] = g_ascii_tolower(temp[i]);
193 g_static_mutex_lock(&data->mutex);
195 element = find_element(data, network->identifier);
196 if (element == NULL) {
197 element = connman_element_create(temp);
199 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
200 element->index = parent->index;
202 data->list = g_slist_append(data->list, element);
204 connman_element_add_static_property(element, "WiFi.Name",
205 DBUS_TYPE_STRING, &network->identifier);
207 connman_element_add_static_array_property(element, "WiFi.SSID",
208 DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
210 connman_element_register(element, parent);
213 g_static_mutex_unlock(&data->mutex);
218 static struct supplicant_callback wifi_callback = {
219 .state_change = state_change,
220 .scan_result = scan_result,
223 static int wifi_probe(struct connman_element *element)
225 struct wifi_data *data;
227 DBG("element %p name %s", element, element->name);
229 data = g_try_new0(struct wifi_data, 1);
233 g_static_mutex_init(&data->mutex);
235 connman_element_set_data(element, data);
240 static void wifi_remove(struct connman_element *element)
242 struct wifi_data *data = connman_element_get_data(element);
244 DBG("element %p name %s", element, element->name);
246 connman_element_set_data(element, NULL);
248 g_free(data->identifier);
252 static int wifi_update(struct connman_element *element)
254 DBG("element %p name %s", element, element->name);
256 __supplicant_scan(element);
261 static int wifi_enable(struct connman_element *element)
265 DBG("element %p name %s", element, element->name);
267 err = __supplicant_start(element, &wifi_callback);
271 __supplicant_scan(element);
276 static int wifi_disable(struct connman_element *element)
278 struct wifi_data *data = connman_element_get_data(element);
281 DBG("element %p name %s", element, element->name);
283 __supplicant_disconnect(element);
285 g_static_mutex_lock(&data->mutex);
287 for (list = data->list; list; list = list->next) {
288 struct connman_element *network = list->data;
290 connman_element_unref(network);
293 g_slist_free(data->list);
296 g_static_mutex_unlock(&data->mutex);
298 connman_element_unregister_children(element);
303 static struct connman_driver wifi_driver = {
304 .name = "wifi-device",
305 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
306 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
308 .remove = wifi_remove,
309 .update = wifi_update,
310 .enable = wifi_enable,
311 .disable = wifi_disable,
314 static GStaticMutex device_mutex = G_STATIC_MUTEX_INIT;
315 static GSList *device_list = NULL;
317 static void wifi_newlink(unsigned short type, int index,
318 unsigned flags, unsigned change)
320 struct connman_element *device;
322 gboolean exists = FALSE;
327 DBG("index %d", index);
329 if (type != ARPHRD_ETHER)
332 name = inet_index2name(index);
334 memset(&iwr, 0, sizeof(iwr));
335 strncpy(iwr.ifr_ifrn.ifrn_name, name, IFNAMSIZ);
337 sk = socket(PF_INET, SOCK_DGRAM, 0);
339 if (ioctl(sk, SIOCGIWNAME, &iwr) < 0) {
347 g_static_mutex_lock(&device_mutex);
349 for (list = device_list; list; list = list->next) {
350 struct connman_element *device = list->data;
352 if (device->index == index) {
358 g_static_mutex_unlock(&device_mutex);
360 if (exists == TRUE) {
365 device = connman_element_create(NULL);
366 device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
367 device->subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
369 device->index = index;
372 g_static_mutex_lock(&device_mutex);
374 connman_element_register(device, NULL);
375 device_list = g_slist_append(device_list, device);
377 g_static_mutex_unlock(&device_mutex);
380 static void wifi_dellink(unsigned short type, int index,
381 unsigned flags, unsigned change)
385 DBG("index %d", index);
387 g_static_mutex_lock(&device_mutex);
389 for (list = device_list; list; list = list->next) {
390 struct connman_element *device = list->data;
392 if (device->index == index) {
393 device_list = g_slist_remove(device_list, device);
394 connman_element_unregister(device);
395 connman_element_unref(device);
400 g_static_mutex_unlock(&device_mutex);
403 static struct connman_rtnl wifi_rtnl = {
405 .newlink = wifi_newlink,
406 .dellink = wifi_dellink,
409 static void supplicant_connect(DBusConnection *connection, void *user_data)
411 DBG("connection %p", connection);
413 __supplicant_init(connection);
415 if (connman_rtnl_register(&wifi_rtnl) < 0)
418 connman_rtnl_send_getlink();
421 static void supplicant_disconnect(DBusConnection *connection, void *user_data)
425 DBG("connection %p", connection);
427 connman_rtnl_unregister(&wifi_rtnl);
429 g_static_mutex_lock(&device_mutex);
431 for (list = device_list; list; list = list->next) {
432 struct connman_element *device = list->data;
434 connman_element_unregister(device);
435 connman_element_unref(device);
438 g_slist_free(device_list);
441 g_static_mutex_unlock(&device_mutex);
446 static DBusConnection *connection;
449 static int wifi_init(void)
453 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
454 if (connection == NULL)
457 err = connman_driver_register(&network_driver);
459 dbus_connection_unref(connection);
463 err = connman_driver_register(&wifi_driver);
465 connman_driver_unregister(&network_driver);
466 dbus_connection_unref(connection);
470 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
471 supplicant_connect, supplicant_disconnect, NULL, NULL);
473 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
474 supplicant_connect(connection, NULL);
479 static void wifi_exit(void)
481 connman_driver_unregister(&network_driver);
482 connman_driver_unregister(&wifi_driver);
485 g_dbus_remove_watch(connection, watch);
487 supplicant_disconnect(connection, NULL);
489 dbus_connection_unref(connection);
492 CONNMAN_PLUGIN_DEFINE("wifi", "WiFi interface plugin", VERSION,
493 wifi_init, wifi_exit)