Add signal strength and noise properties
[framework/connectivity/connman.git] / plugins / wifi.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <linux/if_arp.h>
32 #include <linux/wireless.h>
33
34 #include <gdbus.h>
35
36 #include <connman/plugin.h>
37 #include <connman/driver.h>
38 #include <connman/rtnl.h>
39 #include <connman/log.h>
40
41 #include "inet.h"
42 #include "supplicant.h"
43
44 struct wifi_data {
45         GSList *list;
46         gchar *identifier;
47 };
48
49 static int network_probe(struct connman_element *element)
50 {
51         DBG("element %p name %s", element, element->name);
52
53         return 0;
54 }
55
56 static void network_remove(struct connman_element *element)
57 {
58         DBG("element %p name %s", element, element->name);
59 }
60
61 static int network_enable(struct connman_element *element)
62 {
63         char *identifier, *passphrase = NULL;
64         unsigned char *ssid;
65         int ssid_len;
66
67         DBG("element %p name %s", element, element->name);
68
69         if (connman_element_get_static_property(element,
70                                         "WiFi.Name", &identifier) == FALSE)
71                 return -EIO;
72
73         if (connman_element_get_static_array_property(element,
74                                 "WiFi.SSID", &ssid, &ssid_len) == FALSE)
75                 return -EIO;
76
77         if (element->parent != NULL) {
78                 struct wifi_data *data = connman_element_get_data(element->parent);
79
80                 if (data != NULL) {
81                         g_free(data->identifier);
82                         data->identifier = g_strdup(identifier);
83                 }
84         }
85
86         connman_element_get_value(element,
87                         CONNMAN_PROPERTY_ID_WIFI_PASSPHRASE, &passphrase);
88
89         DBG("identifier %s passhprase %s", identifier, passphrase);
90
91         if (__supplicant_connect(element, ssid, ssid_len, passphrase) < 0)
92                 connman_error("Failed to initiate connect");
93
94         return 0;
95 }
96
97 static int network_disable(struct connman_element *element)
98 {
99         DBG("element %p name %s", element, element->name);
100
101         connman_element_unregister_children(element);
102
103         __supplicant_disconnect(element);
104
105         return 0;
106 }
107
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,
116 };
117
118 static struct connman_element *find_element(struct wifi_data *data,
119                                                 const char *identifier)
120 {
121         GSList *list;
122
123         for (list = data->list; list; list = list->next) {
124                 struct connman_element *element = list->data;
125
126                 if (connman_element_match_static_property(element,
127                                         "WiFi.Name", &identifier) == TRUE)
128                         return element;
129         }
130
131         return NULL;
132 }
133
134 static void state_change(struct connman_element *parent,
135                                                 enum supplicant_state state)
136 {
137         struct wifi_data *data = connman_element_get_data(parent);
138         struct connman_element *element;
139
140         DBG("state %d", state);
141
142         if (data->identifier == NULL)
143                 return;
144
145         element = find_element(data, data->identifier);
146         if (element == NULL)
147                 return;
148
149         if (state == STATE_COMPLETED) {
150                 struct connman_element *dhcp;
151
152                 dhcp = connman_element_create(NULL);
153
154                 dhcp->type = CONNMAN_ELEMENT_TYPE_DHCP;
155                 dhcp->index = element->index;
156
157                 connman_element_register(dhcp, element);
158         }
159 }
160
161 static void scan_result(struct connman_element *parent,
162                                         struct supplicant_network *network)
163 {
164         struct wifi_data *data = connman_element_get_data(parent);
165         struct connman_element *element;
166         gchar *temp;
167         int i;
168
169         DBG("network %p identifier %s", network, network->identifier);
170
171         if (data == NULL)
172                 return;
173
174         if (network->identifier == NULL)
175                 return;
176
177         if (network->identifier[0] == '\0')
178                 return;
179
180         temp = g_strdup(network->identifier);
181
182         for (i = 0; i < strlen(temp); i++) {
183                 if (temp[i] == ' ' || temp[i] == '.' || temp[i] == '-')
184                         temp[i] = '_';
185                 else if (temp[i] == '(' || temp[i] == ')')
186                         temp[i] = '_';
187                 else if (g_ascii_isprint(temp[i]) == FALSE)
188                         temp[i] = '_';
189                 temp[i] = g_ascii_tolower(temp[i]);
190         }
191
192         element = find_element(data, network->identifier);
193         if (element == NULL) {
194                 const char *security;
195                 guint8 strength;
196
197                 element = connman_element_create(temp);
198
199                 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
200                 element->index = parent->index;
201
202                 data->list = g_slist_append(data->list, element);
203
204                 connman_element_add_static_property(element, "WiFi.Name",
205                                 DBUS_TYPE_STRING, &network->identifier);
206
207                 connman_element_add_static_array_property(element, "WiFi.SSID",
208                         DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
209
210                 if (network->has_rsn == TRUE)
211                         security = "wpa2";
212                 else if (network->has_wpa == TRUE)
213                         security = "wpa";
214                 else if (network->has_wep == TRUE)
215                         security = "wep";
216                 else
217                         security = "none";
218
219                 connman_element_add_static_property(element, "WiFi.Security",
220                                                 DBUS_TYPE_STRING, &security);
221
222                 strength = network->quality;
223
224                 connman_element_add_static_property(element, "WiFi.Strength",
225                                                 DBUS_TYPE_BYTE, &strength);
226
227                 connman_element_add_static_property(element, "WiFi.Noise",
228                                         DBUS_TYPE_INT32, &network->noise);
229
230                 DBG("%s (%s) strength %d", network->identifier,
231                                                         security, strength);
232
233                 connman_element_register(element, parent);
234         }
235
236         g_free(temp);
237 }
238
239 static struct supplicant_callback wifi_callback = {
240         .state_change   = state_change,
241         .scan_result    = scan_result,
242 };
243
244 static int wifi_probe(struct connman_element *element)
245 {
246         struct wifi_data *data;
247
248         DBG("element %p name %s", element, element->name);
249
250         data = g_try_new0(struct wifi_data, 1);
251         if (data == NULL)
252                 return -ENOMEM;
253
254         connman_element_set_data(element, data);
255
256         return 0;
257 }
258
259 static void wifi_remove(struct connman_element *element)
260 {
261         struct wifi_data *data = connman_element_get_data(element);
262
263         DBG("element %p name %s", element, element->name);
264
265         connman_element_set_data(element, NULL);
266
267         g_free(data->identifier);
268         g_free(data);
269 }
270
271 static int wifi_update(struct connman_element *element)
272 {
273         DBG("element %p name %s", element, element->name);
274
275         __supplicant_scan(element);
276
277         return 0;
278 }
279
280 static int wifi_enable(struct connman_element *element)
281 {
282         int err;
283
284         DBG("element %p name %s", element, element->name);
285
286         err = __supplicant_start(element, &wifi_callback);
287         if (err < 0)
288                 return err;
289
290         __supplicant_scan(element);
291
292         return 0;
293 }
294
295 static int wifi_disable(struct connman_element *element)
296 {
297         struct wifi_data *data = connman_element_get_data(element);
298         GSList *list;
299
300         DBG("element %p name %s", element, element->name);
301
302         __supplicant_disconnect(element);
303
304         for (list = data->list; list; list = list->next) {
305                 struct connman_element *network = list->data;
306
307                 connman_element_unref(network);
308         }
309
310         g_slist_free(data->list);
311         data->list = NULL;
312
313         connman_element_unregister_children(element);
314
315         return 0;
316 }
317
318 static struct connman_driver wifi_driver = {
319         .name           = "wifi-device",
320         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
321         .subtype        = CONNMAN_ELEMENT_SUBTYPE_WIFI,
322         .probe          = wifi_probe,
323         .remove         = wifi_remove,
324         .update         = wifi_update,
325         .enable         = wifi_enable,
326         .disable        = wifi_disable,
327 };
328
329 static GSList *device_list = NULL;
330
331 static void wifi_newlink(unsigned short type, int index,
332                                         unsigned flags, unsigned change)
333 {
334         struct connman_element *device;
335         GSList *list;
336         gboolean exists = FALSE;
337         gchar *name;
338         struct iwreq iwr;
339         int sk;
340
341         DBG("index %d", index);
342
343         if (type != ARPHRD_ETHER)
344                 return;
345
346         name = inet_index2name(index);
347
348         memset(&iwr, 0, sizeof(iwr));
349         strncpy(iwr.ifr_ifrn.ifrn_name, name, IFNAMSIZ);
350
351         sk = socket(PF_INET, SOCK_DGRAM, 0);
352
353         if (ioctl(sk, SIOCGIWNAME, &iwr) < 0) {
354                 g_free(name);
355                 close(sk);
356                 return;
357         }
358
359         close(sk);
360
361         for (list = device_list; list; list = list->next) {
362                 struct connman_element *device = list->data;
363
364                 if (device->index == index) {
365                         exists = TRUE;
366                         break;
367                 }
368         }
369
370         if (exists == TRUE) {
371                 g_free(name);
372                 return;
373         }
374
375         device = connman_element_create(NULL);
376         device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
377         device->subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
378
379         device->index = index;
380         device->name = name;
381
382         connman_element_register(device, NULL);
383         device_list = g_slist_append(device_list, device);
384 }
385
386 static void wifi_dellink(unsigned short type, int index,
387                                         unsigned flags, unsigned change)
388 {
389         GSList *list;
390
391         DBG("index %d", index);
392
393         for (list = device_list; list; list = list->next) {
394                 struct connman_element *device = list->data;
395
396                 if (device->index == index) {
397                         device_list = g_slist_remove(device_list, device);
398                         connman_element_unregister(device);
399                         connman_element_unref(device);
400                         break;
401                 }
402         }
403 }
404
405 static struct connman_rtnl wifi_rtnl = {
406         .name           = "wifi",
407         .newlink        = wifi_newlink,
408         .dellink        = wifi_dellink,
409 };
410
411 static void supplicant_connect(DBusConnection *connection, void *user_data)
412 {
413         DBG("connection %p", connection);
414
415         __supplicant_init(connection);
416
417         if (connman_rtnl_register(&wifi_rtnl) < 0)
418                 return;
419
420         connman_rtnl_send_getlink();
421 }
422
423 static void supplicant_disconnect(DBusConnection *connection, void *user_data)
424 {
425         GSList *list;
426
427         DBG("connection %p", connection);
428
429         connman_rtnl_unregister(&wifi_rtnl);
430
431         for (list = device_list; list; list = list->next) {
432                 struct connman_element *device = list->data;
433
434                 connman_element_unregister(device);
435                 connman_element_unref(device);
436         }
437
438         g_slist_free(device_list);
439         device_list = NULL;
440
441         __supplicant_exit();
442 }
443
444 static DBusConnection *connection;
445 static guint watch;
446
447 static int wifi_init(void)
448 {
449         int err;
450
451         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
452         if (connection == NULL)
453                 return -EIO;
454
455         err = connman_driver_register(&network_driver);
456         if (err < 0) {
457                 dbus_connection_unref(connection);
458                 return err;
459         }
460
461         err = connman_driver_register(&wifi_driver);
462         if (err < 0) {
463                 connman_driver_unregister(&network_driver);
464                 dbus_connection_unref(connection);
465                 return err;
466         }
467
468         watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
469                         supplicant_connect, supplicant_disconnect, NULL, NULL);
470
471         if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
472                 supplicant_connect(connection, NULL);
473
474         return 0;
475 }
476
477 static void wifi_exit(void)
478 {
479         connman_driver_unregister(&network_driver);
480         connman_driver_unregister(&wifi_driver);
481
482         if (watch > 0)
483                 g_dbus_remove_watch(connection, watch);
484
485         supplicant_disconnect(connection, NULL);
486
487         dbus_connection_unref(connection);
488 }
489
490 CONNMAN_PLUGIN_DEFINE("wifi", "WiFi interface plugin", VERSION,
491                                                         wifi_init, wifi_exit)