5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
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.
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.
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
26 #include <sys/types.h>
28 #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
35 #ifdef NEED_UDEV_DEVICE_GET_PARENT_WITH_DEVTYPE
36 static struct udev_device *udev_device_get_parent_with_devtype(struct udev_device *device,
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)
51 static GSList *device_list = NULL;
53 static struct connman_device *find_device(const char *interface)
57 if (interface == NULL)
60 for (list = device_list; list; list = list->next) {
61 struct connman_device *device = list->data;
62 const char *device_interface;
64 device_interface = connman_device_get_interface(device);
65 if (device_interface == NULL)
68 if (g_str_equal(device_interface, interface) == TRUE)
75 static void add_device(struct udev_device *udev_device)
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;
84 entry = udev_device_get_properties_list_entry(udev_device);
86 const char *name = udev_list_entry_get_name(entry);
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);
93 entry = udev_list_entry_get_next(entry);
96 device = find_device(interface);
100 if (type == NULL || interface == NULL)
103 if (g_str_equal(interface, "ttyUSB0") == FALSE)
106 if (g_str_equal(type, "huawei") == TRUE)
107 devtype = CONNMAN_DEVICE_TYPE_HUAWEI;
108 else if (g_str_equal(type, "novatel") == TRUE)
109 devtype = CONNMAN_DEVICE_TYPE_NOVATEL;
113 device = connman_device_create(interface, devtype);
117 connman_device_set_mode(device, CONNMAN_DEVICE_MODE_NETWORK_SINGLE);
118 connman_device_set_policy(device, CONNMAN_DEVICE_POLICY_MANUAL);
120 connman_device_set_interface(device, interface);
122 if (connman_device_register(device) < 0) {
123 connman_device_unref(device);
127 device_list = g_slist_append(device_list, device);
130 static void remove_device(struct udev_device *udev_device)
132 struct connman_device *device;
133 struct udev_list_entry *entry;
134 const char *interface = NULL;
138 entry = udev_device_get_properties_list_entry(udev_device);
140 const char *name = udev_list_entry_get_name(entry);
142 if (g_str_has_prefix(name, "CONNMAN_INTERFACE") == TRUE)
143 interface = udev_list_entry_get_value(entry);
145 entry = udev_list_entry_get_next(entry);
148 device = find_device(interface);
152 device_list = g_slist_remove(device_list, device);
154 connman_device_unregister(device);
155 connman_device_unref(device);
158 static void print_properties(struct udev_device *device, const char *prefix)
160 struct udev_list_entry *entry;
162 entry = udev_device_get_properties_list_entry(device);
164 const char *name = udev_list_entry_get_name(entry);
165 const char *value = udev_list_entry_get_value(entry);
167 if (g_str_has_prefix(name, "CONNMAN") == TRUE ||
168 g_str_equal(name, "DEVNAME") == TRUE ||
169 g_str_equal(name, "DEVPATH") == TRUE)
170 connman_debug("%s%s = %s", prefix, name, value);
172 entry = udev_list_entry_get_next(entry);
176 static void print_device(struct udev_device *device, const char *action)
178 struct udev_device *parent;
180 connman_debug("=== %s ===", action);
181 print_properties(device, "");
183 parent = udev_device_get_parent_with_devtype(device, "usb_device");
184 print_properties(parent, " ");
187 static void enumerate_devices(struct udev *context)
189 struct udev_enumerate *enumerate;
190 struct udev_list_entry *entry;
192 enumerate = udev_enumerate_new(context);
193 if (enumerate == NULL)
196 udev_enumerate_add_match_property(enumerate, "CONNMAN_TYPE", "?*");
198 udev_enumerate_scan_devices(enumerate);
200 entry = udev_enumerate_get_list_entry(enumerate);
202 const char *syspath = udev_list_entry_get_name(entry);
203 struct udev_device *device;
205 device = udev_device_new_from_syspath(context, syspath);
207 print_device(device, "coldplug");
211 udev_device_unref(device);
213 entry = udev_list_entry_get_next(entry);
216 udev_enumerate_unref(enumerate);
219 static gboolean udev_event(GIOChannel *channel,
220 GIOCondition condition, gpointer user_data)
222 struct udev_monitor *monitor = user_data;
223 struct udev_device *device;
226 device = udev_monitor_receive_device(monitor);
230 action = udev_device_get_action(device);
234 print_device(device, action);
236 if (g_str_equal(action, "add") == TRUE)
238 else if (g_str_equal(action, "remove") == TRUE)
239 remove_device(device);
242 udev_device_unref(device);
247 static struct udev *udev_ctx;
248 static struct udev_monitor *udev_mon;
249 static guint udev_watch = 0;
251 int __connman_udev_init(void)
258 udev_ctx = udev_new();
259 if (udev_ctx == NULL) {
260 connman_error("Failed to create udev context");
264 udev_mon = udev_monitor_new_from_socket(udev_ctx,
265 "@/org/moblin/connman/udev");
266 if (udev_mon == NULL) {
267 connman_error("Failed to create udev monitor");
268 udev_unref(udev_ctx);
273 if (udev_monitor_enable_receiving(udev_mon) < 0) {
274 connman_error("Failed to enable udev monitor");
275 udev_unref(udev_ctx);
277 udev_monitor_unref(udev_mon);
281 enumerate_devices(udev_ctx);
283 fd = udev_monitor_get_fd(udev_mon);
285 channel = g_io_channel_unix_new(fd);
289 udev_watch = g_io_add_watch(channel, G_IO_IN, udev_event, udev_mon);
291 g_io_channel_unref(channel);
296 void __connman_udev_cleanup(void)
303 g_source_remove(udev_watch);
305 for (list = device_list; list; list = list->next) {
306 struct connman_device *device = list->data;
308 connman_device_unregister(device);
309 connman_device_unref(device);
312 g_slist_free(device_list);
315 if (udev_ctx == NULL)
318 udev_monitor_unref(udev_mon);
319 udev_unref(udev_ctx);