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 void print_properties(struct udev_device *device, const char *prefix)
53 struct udev_list_entry *entry;
55 entry = udev_device_get_properties_list_entry(device);
57 const char *name = udev_list_entry_get_name(entry);
58 const char *value = udev_list_entry_get_value(entry);
60 if (g_str_has_prefix(name, "CONNMAN") == TRUE ||
61 g_str_equal(name, "DEVNAME") == TRUE ||
62 g_str_equal(name, "DEVPATH") == TRUE)
63 connman_debug("%s%s = %s", prefix, name, value);
65 entry = udev_list_entry_get_next(entry);
69 static void print_device(struct udev_device *device, const char *action)
71 struct udev_device *parent;
73 connman_debug("=== %s ===", action);
74 print_properties(device, "");
76 parent = udev_device_get_parent_with_devtype(device, "usb_device");
77 print_properties(parent, " ");
80 static void enumerate_devices(struct udev *context)
82 struct udev_enumerate *enumerate;
83 struct udev_list_entry *entry;
85 enumerate = udev_enumerate_new(context);
86 if (enumerate == NULL)
89 udev_enumerate_add_match_property(enumerate, "CONNMAN_TYPE", "?*");
91 udev_enumerate_scan_devices(enumerate);
93 entry = udev_enumerate_get_list_entry(enumerate);
95 const char *syspath = udev_list_entry_get_name(entry);
96 struct udev_device *device;
98 device = udev_device_new_from_syspath(context, syspath);
100 print_device(device, "coldplug");
102 udev_device_unref(device);
104 entry = udev_list_entry_get_next(entry);
107 udev_enumerate_unref(enumerate);
110 static gboolean udev_event(GIOChannel *channel,
111 GIOCondition condition, gpointer user_data)
113 struct udev_monitor *monitor = user_data;
114 struct udev_device *device;
117 device = udev_monitor_receive_device(monitor);
121 action = udev_device_get_action(device);
125 print_device(device, action);
128 udev_device_unref(device);
133 static struct udev *udev_ctx;
134 static struct udev_monitor *udev_mon;
135 static guint udev_watch = 0;
137 int __connman_udev_init(void)
144 udev_ctx = udev_new();
145 if (udev_ctx == NULL) {
146 connman_error("Failed to create udev context");
150 udev_mon = udev_monitor_new_from_socket(udev_ctx,
151 "@/org/moblin/connman/udev");
152 if (udev_mon == NULL) {
153 connman_error("Failed to create udev monitor");
154 udev_unref(udev_ctx);
159 if (udev_monitor_enable_receiving(udev_mon) < 0) {
160 connman_error("Failed to enable udev monitor");
161 udev_unref(udev_ctx);
163 udev_monitor_unref(udev_mon);
167 enumerate_devices(udev_ctx);
169 fd = udev_monitor_get_fd(udev_mon);
171 channel = g_io_channel_unix_new(fd);
175 udev_watch = g_io_add_watch(channel, G_IO_IN, udev_event, udev_mon);
177 g_io_channel_unref(channel);
182 void __connman_udev_cleanup(void)
187 g_source_remove(udev_watch);
189 if (udev_ctx == NULL)
192 udev_monitor_unref(udev_mon);
193 udev_unref(udev_ctx);