Ignore broken network identifiers
[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 <string.h>
27
28 #include <connman/plugin.h>
29 #include <connman/driver.h>
30 #include <connman/log.h>
31
32 #include "supplicant.h"
33
34 struct wifi_data {
35         GStaticMutex mutex;
36         GSList *list;
37 };
38
39 static struct connman_element *find_element(struct wifi_data *data,
40                                                 const char *identifier)
41 {
42         GSList *list;
43
44         for (list = data->list; list; list = list->next) {
45                 struct connman_element *element = list->data;
46
47                 if (element->network.identifier == NULL)
48                         continue;
49
50                 if (g_str_equal(element->network.identifier,
51                                                         identifier) == TRUE)
52                         return element;
53         }
54
55         return NULL;
56 }
57
58 static void scan_result(struct connman_element *parent,
59                                         struct supplicant_network *network)
60 {
61         struct wifi_data *data = connman_element_get_data(parent);
62         struct connman_element *element;
63         gchar *temp;
64         int i;
65
66         DBG("network %p identifier %s", network, network->identifier);
67
68         if (data == NULL)
69                 return;
70
71         if (network->identifier == NULL)
72                 return;
73
74         if (network->identifier[0] == '\0')
75                 return;
76
77         temp = g_strdup(network->identifier);
78
79         for (i = 0; i < strlen(temp); i++) {
80                 if (temp[i] == ' ' || temp[i] == '.')
81                         temp[i] = '_';
82                 temp[i] = g_ascii_tolower(temp[i]);
83         }
84
85         g_static_mutex_lock(&data->mutex);
86
87         element = find_element(data, temp);
88         if (element == NULL) {
89                 element = connman_element_create();
90
91                 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
92                 element->name = temp;
93
94                 element->network.identifier = g_strdup(temp);
95
96                 data->list = g_slist_append(data->list, element);
97
98                 connman_element_register(element, parent);
99         } else
100                 g_free(temp);
101
102         g_static_mutex_unlock(&data->mutex);
103 }
104
105 static struct supplicant_callback wifi_callback = {
106         .scan_result    = scan_result,
107 };
108
109 static int wifi_probe(struct connman_element *element)
110 {
111         struct wifi_data *data;
112         int err;
113
114         DBG("element %p name %s", element, element->name);
115
116         data = g_try_new0(struct wifi_data, 1);
117         if (data == NULL)
118                 return -ENOMEM;
119
120         g_static_mutex_init(&data->mutex);
121
122         connman_element_set_data(element, data);
123
124         err = __supplicant_start(element, &wifi_callback);
125         if (err < 0)
126                 return err;
127
128         __supplicant_scan(element);
129
130         return 0;
131 }
132
133 static void wifi_remove(struct connman_element *element)
134 {
135         struct wifi_data *data = connman_element_get_data(element);
136         GSList *list;
137
138         DBG("element %p name %s", element, element->name);
139
140         __supplicant_stop(element);
141
142         connman_element_set_data(element, NULL);
143
144         if (data == NULL)
145                 return;
146
147         g_static_mutex_lock(&data->mutex);
148
149         for (list = data->list; list; list = list->next) {
150                 struct connman_element *network = list->data;
151
152                 connman_element_unregister(network);
153                 connman_element_unref(network);
154         }
155
156         g_slist_free(data->list);
157
158         g_static_mutex_unlock(&data->mutex);
159
160         g_free(data);
161 }
162
163 static struct connman_driver wifi_driver = {
164         .name           = "wifi",
165         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
166         .subtype        = CONNMAN_ELEMENT_SUBTYPE_WIFI,
167         .probe          = wifi_probe,
168         .remove         = wifi_remove,
169 };
170
171 static int wifi_init(void)
172 {
173         return connman_driver_register(&wifi_driver);
174 }
175
176 static void wifi_exit(void)
177 {
178         connman_driver_unregister(&wifi_driver);
179 }
180
181 CONNMAN_PLUGIN_DEFINE("WiFi", "WiFi interface plugin", VERSION,
182                                                         wifi_init, wifi_exit)