Prioritize wifi plugins
[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
67 static int network_connect(struct connman_network *network)
68 {
69         DBG("network %p", network);
70
71         return supplicant_connect(network);
72 }
73
74 static int network_disconnect(struct connman_network *network)
75 {
76         DBG("network %p", network);
77
78         return supplicant_disconnect(network);
79 }
80
81 static struct connman_network_driver network_driver = {
82         .name           = "wifi",
83         .type           = CONNMAN_NETWORK_TYPE_WIFI,
84         .priority       = CONNMAN_NETWORK_PRIORITY_HIGH,
85         .probe          = network_probe,
86         .remove         = network_remove,
87         .connect        = network_connect,
88         .disconnect     = network_disconnect,
89 };
90
91 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
92 {
93         struct connman_device *device = user_data;
94         struct wifi_data *wifi = connman_device_get_data(device);
95
96         DBG("index %d flags %d change %d", wifi->index, flags, change);
97
98         if (!change)
99                 return;
100
101         if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
102                 if (flags & IFF_UP) {
103                         DBG("interface up");
104                 } else {
105                         DBG("interface down");
106                 }
107         }
108
109         if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
110                 if (flags & IFF_LOWER_UP) {
111                         DBG("carrier on");
112                 } else {
113                         DBG("carrier off");
114                 }
115         }
116
117         wifi->flags = flags;
118 }
119
120 static int wifi_probe(struct connman_device *device)
121 {
122         struct wifi_data *wifi;
123
124         DBG("device %p", device);
125
126         wifi = g_try_new0(struct wifi_data, 1);
127         if (wifi == NULL)
128                 return -ENOMEM;
129
130         wifi->connected = FALSE;
131
132         connman_device_set_data(device, wifi);
133
134         wifi->index = connman_device_get_index(device);
135         wifi->flags = 0;
136
137         wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
138                                                         wifi_newlink, device);
139
140         return 0;
141 }
142
143 static void wifi_remove(struct connman_device *device)
144 {
145         struct wifi_data *wifi = connman_device_get_data(device);
146
147         DBG("device %p", device);
148
149         connman_device_set_data(device, NULL);
150
151         connman_rtnl_remove_watch(wifi->watch);
152
153         g_free(wifi->identifier);
154         g_free(wifi);
155 }
156
157 static int wifi_enable(struct connman_device *device)
158 {
159         DBG("device %p", device);
160
161         return supplicant_start(device);
162 }
163
164 static int wifi_disable(struct connman_device *device)
165 {
166         struct wifi_data *wifi = connman_device_get_data(device);
167
168         DBG("device %p", device);
169
170         wifi->connected = FALSE;
171
172         return supplicant_stop(device);
173 }
174
175 static int wifi_scan(struct connman_device *device)
176 {
177         DBG("device %p", device);
178
179         return supplicant_scan(device);
180 }
181
182 static struct connman_device_driver wifi_driver = {
183         .name           = "wifi",
184         .type           = CONNMAN_DEVICE_TYPE_WIFI,
185         .priority       = CONNMAN_DEVICE_PRIORITY_HIGH,
186         .probe          = wifi_probe,
187         .remove         = wifi_remove,
188         .enable         = wifi_enable,
189         .disable        = wifi_disable,
190         .scan           = wifi_scan,
191 };
192
193 static void wifi_register(void)
194 {
195         DBG("");
196
197         if (connman_device_driver_register(&wifi_driver) < 0)
198                 connman_error("Failed to register WiFi driver");
199 }
200
201 static void wifi_unregister(void)
202 {
203         DBG("");
204
205         connman_device_driver_unregister(&wifi_driver);
206 }
207
208 static struct supplicant_driver supplicant = {
209         .name           = "wifi",
210         .probe          = wifi_register,
211         .remove         = wifi_unregister,
212 };
213
214 static int wifi_init(void)
215 {
216         int err;
217
218         err = connman_network_driver_register(&network_driver);
219         if (err < 0)
220                 return err;
221
222         err = supplicant_register(&supplicant);
223         if (err < 0) {
224                 connman_network_driver_unregister(&network_driver);
225                 return err;
226         }
227
228         return 0;
229 }
230
231 static void wifi_exit(void)
232 {
233         supplicant_unregister(&supplicant);
234
235         connman_network_driver_unregister(&network_driver);
236 }
237
238 CONNMAN_PLUGIN_DEFINE(wifi_legacy, "WiFi interface plugin", VERSION,
239                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)