Remove usage of GStaticMutex since it causes compiler problems
[platform/upstream/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                 if (temp[i] == '(' || temp[i] == ')')
186                         temp[i] = '_';
187                 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                 element = connman_element_create(temp);
195
196                 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
197                 element->index = parent->index;
198
199                 data->list = g_slist_append(data->list, element);
200
201                 connman_element_add_static_property(element, "WiFi.Name",
202                                 DBUS_TYPE_STRING, &network->identifier);
203
204                 connman_element_add_static_array_property(element, "WiFi.SSID",
205                         DBUS_TYPE_BYTE, &network->ssid, network->ssid_len);
206
207                 connman_element_register(element, parent);
208         }
209
210         g_free(temp);
211 }
212
213 static struct supplicant_callback wifi_callback = {
214         .state_change   = state_change,
215         .scan_result    = scan_result,
216 };
217
218 static int wifi_probe(struct connman_element *element)
219 {
220         struct wifi_data *data;
221
222         DBG("element %p name %s", element, element->name);
223
224         data = g_try_new0(struct wifi_data, 1);
225         if (data == NULL)
226                 return -ENOMEM;
227
228         connman_element_set_data(element, data);
229
230         return 0;
231 }
232
233 static void wifi_remove(struct connman_element *element)
234 {
235         struct wifi_data *data = connman_element_get_data(element);
236
237         DBG("element %p name %s", element, element->name);
238
239         connman_element_set_data(element, NULL);
240
241         g_free(data->identifier);
242         g_free(data);
243 }
244
245 static int wifi_update(struct connman_element *element)
246 {
247         DBG("element %p name %s", element, element->name);
248
249         __supplicant_scan(element);
250
251         return 0;
252 }
253
254 static int wifi_enable(struct connman_element *element)
255 {
256         int err;
257
258         DBG("element %p name %s", element, element->name);
259
260         err = __supplicant_start(element, &wifi_callback);
261         if (err < 0)
262                 return err;
263
264         __supplicant_scan(element);
265
266         return 0;
267 }
268
269 static int wifi_disable(struct connman_element *element)
270 {
271         struct wifi_data *data = connman_element_get_data(element);
272         GSList *list;
273
274         DBG("element %p name %s", element, element->name);
275
276         __supplicant_disconnect(element);
277
278         for (list = data->list; list; list = list->next) {
279                 struct connman_element *network = list->data;
280
281                 connman_element_unref(network);
282         }
283
284         g_slist_free(data->list);
285         data->list = NULL;
286
287         connman_element_unregister_children(element);
288
289         return 0;
290 }
291
292 static struct connman_driver wifi_driver = {
293         .name           = "wifi-device",
294         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
295         .subtype        = CONNMAN_ELEMENT_SUBTYPE_WIFI,
296         .probe          = wifi_probe,
297         .remove         = wifi_remove,
298         .update         = wifi_update,
299         .enable         = wifi_enable,
300         .disable        = wifi_disable,
301 };
302
303 static GSList *device_list = NULL;
304
305 static void wifi_newlink(unsigned short type, int index,
306                                         unsigned flags, unsigned change)
307 {
308         struct connman_element *device;
309         GSList *list;
310         gboolean exists = FALSE;
311         gchar *name;
312         struct iwreq iwr;
313         int sk;
314
315         DBG("index %d", index);
316
317         if (type != ARPHRD_ETHER)
318                 return;
319
320         name = inet_index2name(index);
321
322         memset(&iwr, 0, sizeof(iwr));
323         strncpy(iwr.ifr_ifrn.ifrn_name, name, IFNAMSIZ);
324
325         sk = socket(PF_INET, SOCK_DGRAM, 0);
326
327         if (ioctl(sk, SIOCGIWNAME, &iwr) < 0) {
328                 g_free(name);
329                 close(sk);
330                 return;
331         }
332
333         close(sk);
334
335         for (list = device_list; list; list = list->next) {
336                 struct connman_element *device = list->data;
337
338                 if (device->index == index) {
339                         exists = TRUE;
340                         break;
341                 }
342         }
343
344         if (exists == TRUE) {
345                 g_free(name);
346                 return;
347         }
348
349         device = connman_element_create(NULL);
350         device->type = CONNMAN_ELEMENT_TYPE_DEVICE;
351         device->subtype = CONNMAN_ELEMENT_SUBTYPE_WIFI;
352
353         device->index = index;
354         device->name = name;
355
356         connman_element_register(device, NULL);
357         device_list = g_slist_append(device_list, device);
358 }
359
360 static void wifi_dellink(unsigned short type, int index,
361                                         unsigned flags, unsigned change)
362 {
363         GSList *list;
364
365         DBG("index %d", index);
366
367         for (list = device_list; list; list = list->next) {
368                 struct connman_element *device = list->data;
369
370                 if (device->index == index) {
371                         device_list = g_slist_remove(device_list, device);
372                         connman_element_unregister(device);
373                         connman_element_unref(device);
374                         break;
375                 }
376         }
377 }
378
379 static struct connman_rtnl wifi_rtnl = {
380         .name           = "wifi",
381         .newlink        = wifi_newlink,
382         .dellink        = wifi_dellink,
383 };
384
385 static void supplicant_connect(DBusConnection *connection, void *user_data)
386 {
387         DBG("connection %p", connection);
388
389         __supplicant_init(connection);
390
391         if (connman_rtnl_register(&wifi_rtnl) < 0)
392                 return;
393
394         connman_rtnl_send_getlink();
395 }
396
397 static void supplicant_disconnect(DBusConnection *connection, void *user_data)
398 {
399         GSList *list;
400
401         DBG("connection %p", connection);
402
403         connman_rtnl_unregister(&wifi_rtnl);
404
405         for (list = device_list; list; list = list->next) {
406                 struct connman_element *device = list->data;
407
408                 connman_element_unregister(device);
409                 connman_element_unref(device);
410         }
411
412         g_slist_free(device_list);
413         device_list = NULL;
414
415         __supplicant_exit();
416 }
417
418 static DBusConnection *connection;
419 static guint watch;
420
421 static int wifi_init(void)
422 {
423         int err;
424
425         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
426         if (connection == NULL)
427                 return -EIO;
428
429         err = connman_driver_register(&network_driver);
430         if (err < 0) {
431                 dbus_connection_unref(connection);
432                 return err;
433         }
434
435         err = connman_driver_register(&wifi_driver);
436         if (err < 0) {
437                 connman_driver_unregister(&network_driver);
438                 dbus_connection_unref(connection);
439                 return err;
440         }
441
442         watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
443                         supplicant_connect, supplicant_disconnect, NULL, NULL);
444
445         if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
446                 supplicant_connect(connection, NULL);
447
448         return 0;
449 }
450
451 static void wifi_exit(void)
452 {
453         connman_driver_unregister(&network_driver);
454         connman_driver_unregister(&wifi_driver);
455
456         if (watch > 0)
457                 g_dbus_remove_watch(connection, watch);
458
459         supplicant_disconnect(connection, NULL);
460
461         dbus_connection_unref(connection);
462 }
463
464 CONNMAN_PLUGIN_DEFINE("wifi", "WiFi interface plugin", VERSION,
465                                                         wifi_init, wifi_exit)