wifi: Make gsupplicant debug depend on CONNMAN_GSUPPLICANT_DEBUG
[platform/upstream/connman.git] / plugins / wifi-legacy.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 <errno.h>
27 #include <net/if.h>
28
29 #ifndef IFF_LOWER_UP
30 #define IFF_LOWER_UP    0x10000
31 #endif
32
33 #include <dbus/dbus.h>
34 #include <glib.h>
35
36 #define CONNMAN_API_SUBJECT_TO_CHANGE
37 #include <connman/plugin.h>
38 #include <connman/device.h>
39 #include <connman/rtnl.h>
40 #include <connman/log.h>
41
42 #include "supplicant.h"
43
44 #define CLEANUP_TIMEOUT   8     /* in seconds */
45 #define INACTIVE_TIMEOUT  12    /* in seconds */
46
47 struct wifi_data {
48         char *identifier;
49         connman_bool_t connected;
50         int index;
51         unsigned flags;
52         unsigned int watch;
53 };
54
55 static int network_probe(struct connman_network *network)
56 {
57         DBG("network %p", network);
58
59         return 0;
60 }
61
62 static void network_remove(struct connman_network *network)
63 {
64         DBG("network %p", network);
65
66         supplicant_remove_network(network);
67 }
68
69 static int network_connect(struct connman_network *network)
70 {
71         DBG("network %p", network);
72
73         return supplicant_connect(network);
74 }
75
76 static int network_disconnect(struct connman_network *network)
77 {
78         DBG("network %p", network);
79
80         connman_network_set_associating(network, FALSE);
81
82         return supplicant_disconnect(network);
83 }
84
85 static struct connman_network_driver network_driver = {
86         .name           = "wifi",
87         .type           = CONNMAN_NETWORK_TYPE_WIFI,
88         .priority       = CONNMAN_NETWORK_PRIORITY_HIGH,
89         .probe          = network_probe,
90         .remove         = network_remove,
91         .connect        = network_connect,
92         .disconnect     = network_disconnect,
93 };
94
95 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
96 {
97         struct connman_device *device = user_data;
98         struct wifi_data *wifi = connman_device_get_data(device);
99
100         DBG("index %d flags %d change %d", wifi->index, flags, change);
101
102         if (!change)
103                 return;
104
105         if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
106                 if (flags & IFF_UP) {
107                         DBG("interface up");
108                 } else {
109                         DBG("interface down");
110                 }
111         }
112
113         if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
114                 if (flags & IFF_LOWER_UP) {
115                         DBG("carrier on");
116                 } else {
117                         DBG("carrier off");
118                 }
119         }
120
121         wifi->flags = flags;
122 }
123
124 static int wifi_probe(struct connman_device *device)
125 {
126         struct wifi_data *wifi;
127
128         DBG("device %p", device);
129
130         wifi = g_try_new0(struct wifi_data, 1);
131         if (wifi == NULL)
132                 return -ENOMEM;
133
134         wifi->connected = FALSE;
135
136         connman_device_set_data(device, wifi);
137
138         wifi->index = connman_device_get_index(device);
139         wifi->flags = 0;
140
141         wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
142                                                         wifi_newlink, device);
143
144         return 0;
145 }
146
147 static void wifi_remove(struct connman_device *device)
148 {
149         struct wifi_data *wifi = connman_device_get_data(device);
150
151         DBG("device %p", device);
152
153         connman_device_set_data(device, NULL);
154
155         connman_rtnl_remove_watch(wifi->watch);
156
157         g_free(wifi->identifier);
158         g_free(wifi);
159 }
160
161 static int wifi_enable(struct connman_device *device)
162 {
163         DBG("device %p", device);
164
165         return supplicant_start(device);
166 }
167
168 static int wifi_disable(struct connman_device *device)
169 {
170         struct wifi_data *wifi = connman_device_get_data(device);
171
172         DBG("device %p", device);
173
174         wifi->connected = FALSE;
175
176         return supplicant_stop(device);
177 }
178
179 static int wifi_scan(struct connman_device *device)
180 {
181         DBG("device %p", device);
182
183         return supplicant_scan(device);
184 }
185
186 static struct connman_device_driver wifi_driver = {
187         .name           = "wifi",
188         .type           = CONNMAN_DEVICE_TYPE_WIFI,
189         .priority       = CONNMAN_DEVICE_PRIORITY_HIGH,
190         .probe          = wifi_probe,
191         .remove         = wifi_remove,
192         .enable         = wifi_enable,
193         .disable        = wifi_disable,
194         .scan           = wifi_scan,
195 };
196
197 static void wifi_register(void)
198 {
199         DBG("");
200
201         if (connman_device_driver_register(&wifi_driver) < 0)
202                 connman_error("Failed to register WiFi driver");
203 }
204
205 static void wifi_unregister(void)
206 {
207         DBG("");
208
209         connman_device_driver_unregister(&wifi_driver);
210 }
211
212 static struct supplicant_driver supplicant = {
213         .name           = "wifi",
214         .probe          = wifi_register,
215         .remove         = wifi_unregister,
216 };
217
218 static int wifi_init(void)
219 {
220         int err;
221
222         err = connman_network_driver_register(&network_driver);
223         if (err < 0)
224                 return err;
225
226         err = supplicant_register(&supplicant);
227         if (err < 0) {
228                 connman_network_driver_unregister(&network_driver);
229                 return err;
230         }
231
232         return 0;
233 }
234
235 static void wifi_exit(void)
236 {
237         supplicant_unregister(&supplicant);
238
239         connman_network_driver_unregister(&network_driver);
240 }
241
242 CONNMAN_PLUGIN_DEFINE(wifi_legacy, "WiFi interface plugin", VERSION,
243                 CONNMAN_PLUGIN_PRIORITY_HIGH, wifi_init, wifi_exit)