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, *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_PASSPHRASE, &passphrase);
89 DBG("identifier %s passhprase %s", identifier, passphrase);
91 if (__supplicant_connect(element, ssid, ssid_len, passphrase) < 0)
92 connman_error("Failed to initiate connect");
97 static int network_disable(struct connman_element *element)
99 DBG("element %p name %s", element, element->name);
101 connman_element_unregister_children(element);
103 __supplicant_disconnect(element);
108 static struct connman_driver network_driver = {
109 .name = "wifi-network",
110 .type = CONNMAN_ELEMENT_TYPE_NETWORK,
111 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
112 .probe = network_probe,
113 .remove = network_remove,
114 .enable = network_enable,
115 .disable = network_disable,
118 static struct connman_element *find_element(struct wifi_data *data,
119 const char *identifier)
123 for (list = data->list; list; list = list->next) {
124 struct connman_element *element = list->data;
126 if (connman_element_match_static_property(element,
127 "WiFi.Name", &identifier) == TRUE)
134 static void state_change(struct connman_element *parent,
135 enum supplicant_state state)
137 struct wifi_data *data = connman_element_get_data(parent);
138 struct connman_element *element;
140 DBG("state %d", state);
142 if (data->identifier == NULL)
145 element = find_element(data, data->identifier);
149 if (state == STATE_COMPLETED) {
150 struct connman_element *dhcp;
152 dhcp = connman_element_create(NULL);
154 dhcp->type = CONNMAN_ELEMENT_TYPE_DHCP;
155 dhcp->index = element->index;
157 connman_element_register(dhcp, element);
161 static void scan_result(struct connman_element *parent,
162 struct supplicant_network *network)
164 struct wifi_data *data = connman_element_get_data(parent);
165 struct connman_element *element;
169 DBG("network %p identifier %s", network, network->identifier);
174 if (network->identifier == NULL)
177 if (network->identifier[0] == '\0')
180 temp = g_strdup(network->identifier);
182 for (i = 0; i < strlen(temp); i++) {
183 if (temp[i] == ' ' || temp[i] == '.' || temp[i] == '-')
185 if (temp[i] == '(' || temp[i] == ')')
187 if (g_ascii_isprint(temp[i]) == FALSE)
189 temp[i] = g_ascii_tolower(temp[i]);
192 element = find_element(data, network->identifier);
193 if (element == NULL) {
194 element = connman_element_create(temp);
196 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
197 element->index = parent->index;
199 data->list = g_slist_append(data->list, element);
201 connman_element_add_static_property(element, "WiFi.Name",
202 DBUS_TYPE_STRING, &network->identifier);
204 connman_element_add_static_array_property(element, "WiFi.SSID",
205 DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
207 connman_element_register(element, parent);
213 static struct supplicant_callback wifi_callback = {
214 .state_change = state_change,
215 .scan_result = scan_result,
218 static int wifi_probe(struct connman_element *element)
220 struct wifi_data *data;
222 DBG("element %p name %s", element, element->name);
224 data = g_try_new0(struct wifi_data, 1);
228 connman_element_set_data(element, data);
233 static void wifi_remove(struct connman_element *element)
235 struct wifi_data *data = connman_element_get_data(element);
237 DBG("element %p name %s", element, element->name);
239 connman_element_set_data(element, NULL);
241 g_free(data->identifier);
245 static int wifi_update(struct connman_element *element)
247 DBG("element %p name %s", element, element->name);
249 __supplicant_scan(element);
254 static int wifi_enable(struct connman_element *element)
258 DBG("element %p name %s", element, element->name);
260 err = __supplicant_start(element, &wifi_callback);
264 __supplicant_scan(element);
269 static int wifi_disable(struct connman_element *element)
271 struct wifi_data *data = connman_element_get_data(element);
274 DBG("element %p name %s", element, element->name);
276 __supplicant_disconnect(element);
278 for (list = data->list; list; list = list->next) {
279 struct connman_element *network = list->data;
281 connman_element_unref(network);
284 g_slist_free(data->list);
287 connman_element_unregister_children(element);
292 static struct connman_driver wifi_driver = {
293 .name = "wifi-device",
294 .type = CONNMAN_ELEMENT_TYPE_DEVICE,
295 .subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI,
297 .remove = wifi_remove,
298 .update = wifi_update,
299 .enable = wifi_enable,
300 .disable = wifi_disable,
303 static GSList *device_list = NULL;
305 static void wifi_newlink(unsigned short type, int index,
306 unsigned flags, unsigned change)
308 struct connman_element *device;
310 gboolean exists = FALSE;
315 DBG("index %d", index);
317 if (type != ARPHRD_ETHER)
320 name = inet_index2name(index);
322 memset(&iwr, 0, sizeof(iwr));
323 strncpy(iwr.ifr_ifrn.ifrn_name, name, IFNAMSIZ);
325 sk = socket(PF_INET, SOCK_DGRAM, 0);
327 if (ioctl(sk, SIOCGIWNAME, &iwr) < 0) {
335 for (list = device_list; list; list = list->next) {
336 struct connman_element *device = list->data;
338 if (device->index == index) {
344 if (exists == TRUE) {
349 device = connman_element_create(NULL);
350 device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
351 device->subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
353 device->index = index;
356 connman_element_register(device, NULL);
357 device_list = g_slist_append(device_list, device);
360 static void wifi_dellink(unsigned short type, int index,
361 unsigned flags, unsigned change)
365 DBG("index %d", index);
367 for (list = device_list; list; list = list->next) {
368 struct connman_element *device = list->data;
370 if (device->index == index) {
371 device_list = g_slist_remove(device_list, device);
372 connman_element_unregister(device);
373 connman_element_unref(device);
379 static struct connman_rtnl wifi_rtnl = {
381 .newlink = wifi_newlink,
382 .dellink = wifi_dellink,
385 static void supplicant_connect(DBusConnection *connection, void *user_data)
387 DBG("connection %p", connection);
389 __supplicant_init(connection);
391 if (connman_rtnl_register(&wifi_rtnl) < 0)
394 connman_rtnl_send_getlink();
397 static void supplicant_disconnect(DBusConnection *connection, void *user_data)
401 DBG("connection %p", connection);
403 connman_rtnl_unregister(&wifi_rtnl);
405 for (list = device_list; list; list = list->next) {
406 struct connman_element *device = list->data;
408 connman_element_unregister(device);
409 connman_element_unref(device);
412 g_slist_free(device_list);
418 static DBusConnection *connection;
421 static int wifi_init(void)
425 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
426 if (connection == NULL)
429 err = connman_driver_register(&network_driver);
431 dbus_connection_unref(connection);
435 err = connman_driver_register(&wifi_driver);
437 connman_driver_unregister(&network_driver);
438 dbus_connection_unref(connection);
442 watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
443 supplicant_connect, supplicant_disconnect, NULL, NULL);
445 if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
446 supplicant_connect(connection, NULL);
451 static void wifi_exit(void)
453 connman_driver_unregister(&network_driver);
454 connman_driver_unregister(&wifi_driver);
457 g_dbus_remove_watch(connection, watch);
459 supplicant_disconnect(connection, NULL);
461 dbus_connection_unref(connection);
464 CONNMAN_PLUGIN_DEFINE("wifi", "WiFi interface plugin", VERSION,
465 wifi_init, wifi_exit)