Add support for connecting WiFi networks
[framework/connectivity/connman.git] / plugins / wifi.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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
28 #include <dbus/dbus.h>
29 #include <glib.h>
30
31 #define CONNMAN_API_SUBJECT_TO_CHANGE
32 #include <connman/plugin.h>
33 #include <connman/device.h>
34 #include <connman/log.h>
35
36 #include "inet.h"
37 #include "supplicant.h"
38
39 #define CLEANUP_TIMEOUT   8     /* in seconds */
40 #define INACTIVE_TIMEOUT  12    /* in seconds */
41
42 struct wifi_data {
43         char *identifier;
44         connman_bool_t connected;
45 };
46
47 static int network_probe(struct connman_network *network)
48 {
49         DBG("network %p", network);
50
51         return 0;
52 }
53
54 static void network_remove(struct connman_network *network)
55 {
56         DBG("network %p", network);
57 }
58
59 static int network_connect(struct connman_network *network)
60 {
61         DBG("network %p", network);
62
63         return supplicant_connect(network);
64 }
65
66 static int network_disconnect(struct connman_network *network)
67 {
68         DBG("network %p", network);
69
70         //connman_element_unregister_children((struct connman_element *) network);
71
72         return supplicant_disconnect(network);
73 }
74
75 static struct connman_network_driver network_driver = {
76         .name           = "wifi",
77         .type           = CONNMAN_NETWORK_TYPE_WIFI,
78         .probe          = network_probe,
79         .remove         = network_remove,
80         .connect        = network_connect,
81         .disconnect     = network_disconnect,
82 };
83
84 static int wifi_probe(struct connman_device *device)
85 {
86         struct wifi_data *data;
87
88         DBG("device %p", device);
89
90         data = g_try_new0(struct wifi_data, 1);
91         if (data == NULL)
92                 return -ENOMEM;
93
94         data->connected = FALSE;
95
96         connman_device_set_data(device, data);
97
98         return 0;
99 }
100
101 static void wifi_remove(struct connman_device *device)
102 {
103         struct wifi_data *data = connman_device_get_data(device);
104
105         DBG("device %p", device);
106
107         connman_device_set_data(device, NULL);
108
109         g_free(data->identifier);
110         g_free(data);
111 }
112
113 static int wifi_enable(struct connman_device *device)
114 {
115         DBG("device %p", device);
116
117         return supplicant_start(device);
118 }
119
120 static int wifi_disable(struct connman_device *device)
121 {
122         struct wifi_data *data = connman_device_get_data(device);
123
124         DBG("device %p", device);
125
126         //connman_element_unregister_children((struct connman_element *) device);
127
128         data->connected = FALSE;
129
130         return supplicant_stop(device);
131 }
132
133 static int wifi_scan(struct connman_device *device)
134 {
135         DBG("device %p", device);
136
137         return supplicant_scan(device);
138 }
139
140 static struct connman_device_driver wifi_driver = {
141         .name           = "wifi",
142         .type           = CONNMAN_DEVICE_TYPE_WIFI,
143         .probe          = wifi_probe,
144         .remove         = wifi_remove,
145         .enable         = wifi_enable,
146         .disable        = wifi_disable,
147         .scan           = wifi_scan,
148 };
149
150 static void wifi_register(void)
151 {
152         DBG("");
153
154         if (connman_device_driver_register(&wifi_driver) < 0)
155                 connman_error("Failed to register WiFi driver");
156 }
157
158 static void wifi_unregister(void)
159 {
160         DBG("");
161
162         connman_device_driver_unregister(&wifi_driver);
163 }
164
165 static struct supplicant_driver supplicant = {
166         .name           = "wifi",
167         .probe          = wifi_register,
168         .remove         = wifi_unregister,
169 };
170
171 static int wifi_init(void)
172 {
173         int err;
174
175         err = connman_network_driver_register(&network_driver);
176         if (err < 0)
177                 return err;
178
179         err = supplicant_register(&supplicant);
180         if (err < 0) {
181                 connman_network_driver_unregister(&network_driver);
182                 return err;
183         }
184
185         return 0;
186 }
187
188 static void wifi_exit(void)
189 {
190         supplicant_unregister(&supplicant);
191
192         connman_network_driver_unregister(&network_driver);
193 }
194
195 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
196                                                         wifi_init, wifi_exit)