Add first attempt for the property system
[connman] / plugins / hal.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 <dbus/dbus.h>
27 #include <hal/libhal.h>
28
29 #include <connman/plugin.h>
30 #include <connman/element.h>
31 #include <connman/log.h>
32
33 static struct {
34         const char *name;
35         enum connman_element_subtype subtype;
36 } capabilities[] = {
37         { "net.80203", CONNMAN_ELEMENT_SUBTYPE_ETHERNET },
38         { "net.80211", CONNMAN_ELEMENT_SUBTYPE_WIFI     },
39         { }
40 };
41
42 static GStaticMutex element_mutex = G_STATIC_MUTEX_INIT;
43 static GSList *element_list = NULL;
44
45 static void device_info(LibHalContext *ctx, const char *udi,
46                                         struct connman_element *element)
47 {
48         char *parent, *subsys, *value;
49
50         parent = libhal_device_get_property_string(ctx, udi,
51                                                 "info.parent", NULL);
52
53         subsys = libhal_device_get_property_string(ctx, udi,
54                                                 "linux.subsystem", NULL);
55
56         value = libhal_device_get_property_string(ctx, udi,
57                                                 "info.linux.driver", NULL);
58         if (value == NULL) {
59                 value = libhal_device_get_property_string(ctx, parent,
60                                                 "info.linux.driver", NULL);
61                 if (value != NULL)
62                         connman_element_add_static_property(element,
63                                         "Driver", DBUS_TYPE_STRING, &value);
64         }
65
66         if (g_str_equal(subsys, "net") == TRUE) {
67                 value = libhal_device_get_property_string(ctx, parent,
68                                                         "info.vendor", NULL);
69                 if (value != NULL)
70                         connman_element_add_static_property(element,
71                                         "Vendor", DBUS_TYPE_STRING, &value);
72
73                 value = libhal_device_get_property_string(ctx, parent,
74                                                         "info.product", NULL);
75                 if (value != NULL)
76                         connman_element_add_static_property(element,
77                                         "Product", DBUS_TYPE_STRING, &value);
78         }
79 }
80
81 static void device_netdev(LibHalContext *ctx, const char *udi,
82                                         struct connman_element *element)
83 {
84         if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_ETHERNET ||
85                         element->subtype == CONNMAN_ELEMENT_SUBTYPE_WIFI) {
86                 element->netdev.index = libhal_device_get_property_int(ctx,
87                                                 udi, "net.linux.ifindex", NULL);
88
89                 element->netdev.name = libhal_device_get_property_string(ctx,
90                                                 udi, "net.interface", NULL);
91         }
92 }
93
94 static void create_element(LibHalContext *ctx, const char *udi,
95                                         enum connman_element_subtype subtype)
96 {
97         struct connman_element *element;
98
99         DBG("ctx %p udi %s", ctx, udi);
100
101         element = connman_element_create();
102
103         element->name = g_path_get_basename(udi);
104         element->type = CONNMAN_ELEMENT_TYPE_DEVICE;
105         element->subtype = subtype;
106
107         device_info(ctx, udi, element);
108         device_netdev(ctx, udi, element);
109
110         g_static_mutex_lock(&element_mutex);
111
112         connman_element_register(element, NULL);
113
114         element_list = g_slist_append(element_list, element);
115
116         g_static_mutex_unlock(&element_mutex);
117 }
118
119 static void device_added(LibHalContext *ctx, const char *udi)
120 {
121         int i;
122
123         DBG("ctx %p udi %s", ctx, udi);
124
125         for (i = 0; capabilities[i].name; i++) {
126                 if (libhal_device_query_capability(ctx, udi,
127                                         capabilities[i].name, NULL) == TRUE)
128                         create_element(ctx, udi, capabilities[i].subtype);
129         }
130 }
131
132 static void device_removed(LibHalContext *ctx, const char *udi)
133 {
134         GSList *list;
135         gchar *name;
136
137         DBG("ctx %p udi %s", ctx, udi);
138
139         name = g_path_get_basename(udi);
140
141         g_static_mutex_lock(&element_mutex);
142
143         for (list = element_list; list; list = list->next) {
144                 struct connman_element *element = list->data;
145
146                 if (g_str_equal(element->name, name) == TRUE) {
147                         element_list = g_slist_remove(element_list, element);
148
149                         connman_element_unregister(element);
150                         connman_element_unref(element);
151                         break;
152                 }
153         }
154
155         g_static_mutex_unlock(&element_mutex);
156
157         g_free(name);
158 }
159
160 static void probe_capability(LibHalContext *ctx, const char *capability,
161                                         enum connman_element_subtype subtype)
162 {
163         char **list;
164         int num;
165
166         DBG("ctx %p capability %s", ctx, capability);
167
168         list = libhal_find_device_by_capability(ctx, capability, &num, NULL);
169         if (list) {
170                 char **tmp = list;
171
172                 while (*tmp) {
173                         create_element(ctx, *tmp, subtype);
174                         tmp++;
175                 }
176
177                 libhal_free_string_array(list);
178         }
179 }
180
181 static void find_devices(LibHalContext *ctx)
182 {
183         int i;
184
185         DBG("ctx %p", ctx);
186
187         for (i = 0; capabilities[i].name; i++)
188                 probe_capability(ctx, capabilities[i].name,
189                                                 capabilities[i].subtype);
190 }
191
192 static LibHalContext *hal_ctx = NULL;
193
194 static void libhal_init(void *data)
195 {
196         DBusConnection *conn = data;
197
198         DBG("conn %p", conn);
199
200         if (hal_ctx != NULL)
201                 return;
202
203         hal_ctx = libhal_ctx_new();
204         if (hal_ctx == NULL)
205                 return;
206
207         if (libhal_ctx_set_dbus_connection(hal_ctx, conn) == FALSE) {
208                 libhal_ctx_free(hal_ctx);
209                 return;
210         }
211
212         if (libhal_ctx_init(hal_ctx, NULL) == FALSE) {
213                 libhal_ctx_free(hal_ctx);
214                 return ;
215         }
216
217         libhal_ctx_set_device_added(hal_ctx, device_added);
218         libhal_ctx_set_device_removed(hal_ctx, device_removed);
219
220         //libhal_ctx_set_device_new_capability(hal_ctx, new_capability);
221         //libhal_ctx_set_device_lost_capability(hal_ctx, lost_capability);
222
223         find_devices(hal_ctx);
224 }
225
226 static void libhal_cleanup(void *data)
227 {
228         DBusConnection *conn = data;
229         GSList *list;
230
231         DBG("conn %p", conn);
232
233         g_static_mutex_lock(&element_mutex);
234
235         for (list = element_list; list; list = list->next) {
236                 struct connman_element *element = list->data;
237
238                 connman_element_unregister(element);
239                 connman_element_unref(element);
240         }
241
242         g_slist_free(element_list);
243         element_list = NULL;
244
245         g_static_mutex_unlock(&element_mutex);
246
247         if (hal_ctx == NULL)
248                 return;
249
250         libhal_ctx_shutdown(hal_ctx, NULL);
251
252         libhal_ctx_free(hal_ctx);
253
254         hal_ctx = NULL;
255 }
256
257 static int hal_init(void)
258 {
259         DBusConnection *conn;
260
261         conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
262         if (conn == NULL)
263                 return -EIO;
264
265         libhal_init(conn);
266
267         return 0;
268 }
269
270 static void hal_exit(void)
271 {
272         DBusConnection *conn;
273
274         conn = libhal_ctx_get_dbus_connection(hal_ctx);
275         if (conn == NULL)
276                 return;
277
278         libhal_cleanup(conn);
279
280         dbus_connection_unref(conn);
281 }
282
283 CONNMAN_PLUGIN_DEFINE("HAL", "Hardware detection plugin", VERSION,
284                                                         hal_init, hal_exit)