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