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