Create element for every network in range
[framework/connectivity/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 <connman/plugin.h>
27 #include <connman/driver.h>
28 #include <connman/log.h>
29
30 #include "supplicant.h"
31
32 struct wifi_data {
33         GStaticMutex mutex;
34         GSList *list;
35 };
36
37 static struct connman_element *find_element(struct wifi_data *data,
38                                                 const char *identifier)
39 {
40         GSList *list;
41
42         for (list = data->list; list; list = list->next) {
43                 struct connman_element *element = list->data;
44
45                 if (g_str_equal(identifier, element->network.identifier) == TRUE)
46                         return element;
47         }
48
49         return NULL;
50 }
51
52 static void scan_result(struct connman_element *parent,
53                                         struct supplicant_network *network)
54 {
55         struct wifi_data *data = connman_element_get_data(parent);
56         struct connman_element *element;
57
58         DBG("network %p identifier %s", network, network->identifier);
59
60         if (data == NULL)
61                 return;
62
63         g_static_mutex_lock(&data->mutex);
64
65         element = find_element(data, network->identifier);
66         if (element == NULL) {
67                 element = connman_element_create();
68
69                 element->type = CONNMAN_ELEMENT_TYPE_NETWORK;
70                 element->name = g_strdup(network->identifier);
71
72                 data->list = g_slist_append(data->list, element);
73         }
74
75         g_static_mutex_unlock(&data->mutex);
76
77         connman_element_register(element, parent);
78 }
79
80 static struct supplicant_callback wifi_callback = {
81         .scan_result    = scan_result,
82 };
83
84 static int wifi_probe(struct connman_element *element)
85 {
86         struct wifi_data *data;
87         int err;
88
89         DBG("element %p name %s", element, element->name);
90
91         data = g_try_new0(struct wifi_data, 1);
92         if (data == NULL)
93                 return -ENOMEM;
94
95         g_static_mutex_init(&data->mutex);
96
97         connman_element_set_data(element, data);
98
99         err = __supplicant_start(element, &wifi_callback);
100         if (err < 0)
101                 return err;
102
103         __supplicant_scan(element);
104
105         return 0;
106 }
107
108 static void wifi_remove(struct connman_element *element)
109 {
110         struct wifi_data *data = connman_element_get_data(element);
111         GSList *list;
112
113         DBG("element %p name %s", element, element->name);
114
115         __supplicant_stop(element);
116
117         connman_element_set_data(element, NULL);
118
119         if (data == NULL)
120                 return;
121
122         g_static_mutex_lock(&data->mutex);
123
124         for (list = data->list; list; list = list->next) {
125                 struct connman_element *network = list->data;
126
127                 connman_element_unregister(network);
128                 connman_element_unref(network);
129         }
130
131         g_slist_free(data->list);
132
133         g_static_mutex_unlock(&data->mutex);
134
135         g_free(data);
136 }
137
138 static struct connman_driver wifi_driver = {
139         .name           = "wifi",
140         .type           = CONNMAN_ELEMENT_TYPE_DEVICE,
141         .subtype        = CONNMAN_ELEMENT_SUBTYPE_WIFI,
142         .probe          = wifi_probe,
143         .remove         = wifi_remove,
144 };
145
146 static int wifi_init(void)
147 {
148         return connman_driver_register(&wifi_driver);
149 }
150
151 static void wifi_exit(void)
152 {
153         connman_driver_unregister(&wifi_driver);
154 }
155
156 CONNMAN_PLUGIN_DEFINE("WiFi", "WiFi interface plugin", VERSION,
157                                                         wifi_init, wifi_exit)