Add detection support for HUAWEI devices
[connman] / src / udev.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 <sys/types.h>
27
28 #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
29 #include <libudev.h>
30
31 #include <glib.h>
32
33 #include "connman.h"
34
35 #ifdef NEED_UDEV_DEVICE_GET_PARENT_WITH_DEVTYPE
36 static struct udev_device *udev_device_get_parent_with_devtype(struct udev_device *device,
37                                                                 const char *devtype)
38 {
39         return NULL;
40 }
41 #endif
42
43 #ifdef NEED_UDEV_ENUMERATE_ADD_MATCH_PROPERTY
44 static int udev_enumerate_add_match_property(struct udev_enumerate *enumerate,
45                                         const char *property, const char *value)
46 {
47         return 0;
48 }
49 #endif
50
51 static GSList *device_list = NULL;
52
53 static struct connman_device *find_device(const char *interface)
54 {
55         GSList *list;
56
57         if (interface == NULL)
58                 return NULL;
59
60         for (list = device_list; list; list = list->next) {
61                 struct connman_device *device = list->data;
62                 const char *device_interface;
63
64                 device_interface = connman_device_get_interface(device);
65                 if (device_interface == NULL)
66                         continue;
67
68                 if (g_str_equal(device_interface, interface) == TRUE)
69                         return device;
70         }
71
72         return NULL;
73 }
74
75 static void add_device(struct udev_device *udev_device)
76 {
77         enum connman_device_type devtype = CONNMAN_DEVICE_TYPE_UNKNOWN;
78         struct connman_device *device;
79         struct udev_list_entry *entry;
80         const char *type = NULL, *interface = NULL;
81
82         DBG("");
83
84         entry = udev_device_get_properties_list_entry(udev_device);
85         while (entry) {
86                 const char *name = udev_list_entry_get_name(entry);
87
88                 if (g_str_has_prefix(name, "CONNMAN_TYPE") == TRUE)
89                         type = udev_list_entry_get_value(entry);
90                 else if (g_str_has_prefix(name, "CONNMAN_INTERFACE") == TRUE)
91                         interface = udev_list_entry_get_value(entry);
92
93                 entry = udev_list_entry_get_next(entry);
94         }
95
96         device = find_device(interface);
97         if (device != NULL)
98                 return;
99
100         if (type == NULL || interface == NULL)
101                 return;
102
103         if (g_str_equal(interface, "ttyUSB0") == FALSE)
104                 return;
105
106         if (g_str_equal(type, "huawei") == TRUE)
107                 devtype = CONNMAN_DEVICE_TYPE_HUAWEI;
108         else
109                 return;
110
111         device = connman_device_create(interface, devtype);
112         if (device == NULL)
113                 return;
114
115         connman_device_set_mode(device, CONNMAN_DEVICE_MODE_NETWORK_SINGLE);
116         connman_device_set_policy(device, CONNMAN_DEVICE_POLICY_MANUAL);
117
118         connman_device_set_interface(device, interface);
119
120         if (connman_device_register(device) < 0) {
121                 connman_device_unref(device);
122                 return;
123         }
124
125         device_list = g_slist_append(device_list, device);
126 }
127
128 static void remove_device(struct udev_device *udev_device)
129 {
130         struct connman_device *device;
131         struct udev_list_entry *entry;
132         const char *interface = NULL;
133
134         DBG("");
135
136         entry = udev_device_get_properties_list_entry(udev_device);
137         while (entry) {
138                 const char *name = udev_list_entry_get_name(entry);
139
140                 if (g_str_has_prefix(name, "CONNMAN_INTERFACE") == TRUE)
141                         interface = udev_list_entry_get_value(entry);
142
143                 entry = udev_list_entry_get_next(entry);
144         }
145
146         device = find_device(interface);
147         if (device == NULL)
148                 return;
149
150         device_list = g_slist_remove(device_list, device);
151
152         connman_device_unregister(device);
153         connman_device_unref(device);
154 }
155
156 static void print_properties(struct udev_device *device, const char *prefix)
157 {
158         struct udev_list_entry *entry;
159
160         entry = udev_device_get_properties_list_entry(device);
161         while (entry) {
162                 const char *name = udev_list_entry_get_name(entry);
163                 const char *value = udev_list_entry_get_value(entry);
164
165                 if (g_str_has_prefix(name, "CONNMAN") == TRUE ||
166                                 g_str_equal(name, "DEVNAME") == TRUE ||
167                                         g_str_equal(name, "DEVPATH") == TRUE)
168                         connman_debug("%s%s = %s", prefix, name, value);
169
170                 entry = udev_list_entry_get_next(entry);
171         }
172 }
173
174 static void print_device(struct udev_device *device, const char *action)
175 {
176         struct udev_device *parent;
177
178         connman_debug("=== %s ===", action);
179         print_properties(device, "");
180
181         parent = udev_device_get_parent_with_devtype(device, "usb_device");
182         print_properties(parent, "    ");
183 }
184
185 static void enumerate_devices(struct udev *context)
186 {
187         struct udev_enumerate *enumerate;
188         struct udev_list_entry *entry;
189
190         enumerate = udev_enumerate_new(context);
191         if (enumerate == NULL)
192                 return;
193
194         udev_enumerate_add_match_property(enumerate, "CONNMAN_TYPE", "?*");
195
196         udev_enumerate_scan_devices(enumerate);
197
198         entry = udev_enumerate_get_list_entry(enumerate);
199         while (entry) {
200                 const char *syspath = udev_list_entry_get_name(entry);
201                 struct udev_device *device;
202
203                 device = udev_device_new_from_syspath(context, syspath);
204
205                 print_device(device, "coldplug");
206
207                 add_device(device);
208
209                 udev_device_unref(device);
210
211                 entry = udev_list_entry_get_next(entry);
212         }
213
214         udev_enumerate_unref(enumerate);
215 }
216
217 static gboolean udev_event(GIOChannel *channel,
218                                 GIOCondition condition, gpointer user_data)
219 {
220         struct udev_monitor *monitor = user_data;
221         struct udev_device *device;
222         const char *action;
223
224         device = udev_monitor_receive_device(monitor);
225         if (device == NULL)
226                 return TRUE;
227
228         action = udev_device_get_action(device);
229         if (action == NULL)
230                 goto done;
231
232         print_device(device, action);
233
234         if (g_str_equal(action, "add") == TRUE)
235                 add_device(device);
236         else if (g_str_equal(action, "remove") == TRUE)
237                 remove_device(device);
238
239 done:
240         udev_device_unref(device);
241
242         return TRUE;
243 }
244
245 static struct udev *udev_ctx;
246 static struct udev_monitor *udev_mon;
247 static guint udev_watch = 0;
248
249 int __connman_udev_init(void)
250 {
251         GIOChannel *channel;
252         int fd;
253
254         DBG("");
255
256         udev_ctx = udev_new();
257         if (udev_ctx == NULL) {
258                 connman_error("Failed to create udev context");
259                 return -1;
260         }
261
262         udev_mon = udev_monitor_new_from_socket(udev_ctx,
263                                                 "@/org/moblin/connman/udev");
264         if (udev_mon == NULL) {
265                 connman_error("Failed to create udev monitor");
266                 udev_unref(udev_ctx);
267                 udev_ctx = NULL;
268                 return -1;
269         }
270
271         if (udev_monitor_enable_receiving(udev_mon) < 0) {
272                 connman_error("Failed to enable udev monitor");
273                 udev_unref(udev_ctx);
274                 udev_ctx = NULL;
275                 udev_monitor_unref(udev_mon);
276                 return -1;
277         }
278
279         enumerate_devices(udev_ctx);
280
281         fd = udev_monitor_get_fd(udev_mon);
282
283         channel = g_io_channel_unix_new(fd);
284         if (channel == NULL)
285                 return 0;
286
287         udev_watch = g_io_add_watch(channel, G_IO_IN, udev_event, udev_mon);
288
289         g_io_channel_unref(channel);
290
291         return 0;
292 }
293
294 void __connman_udev_cleanup(void)
295 {
296         GSList *list;
297
298         DBG("");
299
300         if (udev_watch > 0)
301                 g_source_remove(udev_watch);
302
303         for (list = device_list; list; list = list->next) {
304                 struct connman_device *device = list->data;
305
306                 connman_device_unregister(device);
307                 connman_device_unref(device);
308         }
309
310         g_slist_free(device_list);
311         device_list = NULL;
312
313         if (udev_ctx == NULL)
314                 return;
315
316         udev_monitor_unref(udev_mon);
317         udev_unref(udev_ctx);
318 }