1 /* This program is free software; you can redistribute it and/or modify
2 * it under the terms of the GNU General Public License as published by
3 * the Free Software Foundation; either version 2 of the License, or
4 * (at your option) any later version.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License along
12 * with this program; if not, write to the Free Software Foundation, Inc.,
13 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15 * Copyright (C) 2009 Red Hat, Inc.
16 * Copyright (c) 2010 Michal Cihar <michal@cihar.com>
20 #define G_UDEV_API_IS_SUBJECT_TO_CHANGE
21 #include <gudev/gudev.h>
28 #include <gammu.h> /* For PRINTF_STYLE and locales */
29 #include "../helper/locales.h" /* For gettext */
37 static void println(guint indent
, const char *fmt
, ...)
40 char real_fmt
[1000] = "; ";
43 g_return_if_fail(fmt
!= NULL
);
44 g_return_if_fail(indent
< sizeof(real_fmt
) - 2 - strlen(fmt
));
46 for (i
= 0; i
< indent
; i
++)
47 real_fmt
[i
+ 2] = ' ';
48 strcpy(&real_fmt
[i
+ 2], fmt
);
49 real_fmt
[i
+ 2 + strlen(fmt
)] = '\n';
50 real_fmt
[i
+ 2 + strlen(fmt
) + 1] = '\0';
54 vprintf(real_fmt
, args
);
58 static void dump_device_and_parent(GUdevDevice
* device
, guint indent
)
60 const gchar
*const *list
;
61 const gchar
*const *iter
;
64 guint32 namelen
= 0, i
;
66 println(indent
, "------------------------------------------------------");
67 println(indent
, "%-20s %s", _("Name:"), g_udev_device_get_name(device
));
68 println(indent
, "%-20s %s", _("Type:"), g_udev_device_get_devtype(device
));
69 println(indent
, "%-20s %s", _("Subsystem:"), g_udev_device_get_subsystem(device
));
70 println(indent
, "%-20s %s", _("Number:"), g_udev_device_get_number(device
));
71 println(indent
, "%-20s %s", _("Path:"), g_udev_device_get_sysfs_path(device
));
72 println(indent
, "%-20s %s", _("Driver:"), g_udev_device_get_driver(device
));
73 println(indent
, "%-20s %lld", _("Sequential Number:"), (long long int)g_udev_device_get_seqnum(device
));
74 println(indent
, "%-20s %s", _("Device File:"), g_udev_device_get_device_file(device
));
77 println(indent
, _("Properties:"));
79 /* Get longest property name length for alignment */
80 list
= g_udev_device_get_property_keys(device
);
81 for (iter
= list
; iter
&& *iter
; iter
++) {
82 if (strlen(*iter
) > namelen
)
83 namelen
= strlen(*iter
);
87 for (iter
= list
; iter
&& *iter
; iter
++) {
88 strcpy(propstr
, *iter
);
90 for (i
= 0; i
< namelen
- strlen(*iter
); i
++)
92 strcat(propstr
, g_udev_device_get_property(device
, *iter
));
93 println(indent
+ 2, "%s", propstr
);
98 parent
= g_udev_device_get_parent(device
);
100 dump_device_and_parent(parent
, indent
+ 4);
101 g_object_unref(parent
);
105 static gboolean
device_is_acm(GUdevDevice
* device
)
108 parent
= g_udev_device_get_parent(device
);
111 if (g_strcmp0(g_udev_device_get_driver(parent
), "cdc_acm") == 0) {
112 g_object_unref(parent
);
115 g_object_unref(parent
);
120 static gboolean
device_is_usb_serial(GUdevDevice
* device
)
123 parent
= g_udev_device_get_parent(device
);
126 if (g_strcmp0(g_udev_device_get_subsystem(parent
), "usb-serial") == 0) {
127 g_object_unref(parent
);
130 g_object_unref(parent
);
135 static gboolean
device_is_serial(GUdevDevice
* device
)
138 parent
= g_udev_device_get_parent(device
);
141 if (g_strcmp0(g_udev_device_get_name(parent
), "serial8250") == 0) {
142 g_object_unref(parent
);
145 g_object_unref(parent
);
150 static gboolean
device_is_valid(GUdevDevice
* device
)
152 if (device_is_serial(device
)) {
155 if (device_is_acm(device
)) {
158 if (device_is_usb_serial(device
)) {
164 static void device_dump_config(GUdevDevice
* device
)
166 gchar
*device_name
, *name
;
167 device_name
= g_strdup_printf("/dev/%s", g_udev_device_get_name(device
));
169 if (device_is_serial(device
)) {
170 name
= g_strdup_printf(_("Phone on serial port %s"), g_udev_device_get_number(device
));
171 } else if (device_is_usb_serial(device
)) {
172 name
= g_strdup_printf(_("Phone on USB serial port %s %s"), g_udev_device_get_property(device
, "ID_VENDOR"), g_udev_device_get_property(device
, "ID_MODEL"));
173 } else if (device_is_acm(device
)) {
174 name
= g_strdup_printf("%s %s", g_udev_device_get_property(device
, "ID_VENDOR"), g_udev_device_get_property(device
, "ID_MODEL"));
178 print_config(device_name
, name
, "at");
183 void udev_detect(void)
186 const char *subsys
[2] = { "tty", NULL
};
189 client
= g_udev_client_new(subsys
);
191 list
= g_udev_client_query_by_subsystem(client
, subsys
[0]);
192 for (iter
= list
; iter
; iter
= g_list_next(iter
)) {
193 dump_device_and_parent(G_UDEV_DEVICE(iter
->data
), 0);
194 if (device_is_valid(G_UDEV_DEVICE(iter
->data
))) {
195 device_dump_config(G_UDEV_DEVICE(iter
->data
));
197 g_object_unref(G_UDEV_DEVICE(iter
->data
));
201 /* How should editor hadle tabs in this file? Add editor commands here.
202 * vim: noexpandtab sw=8 ts=8 sts=8: