udev: fix hex decoding
[platform/upstream/pulseaudio.git] / src / modules / udev-util.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2009 Lennart Poettering
5
6   PulseAudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10
11   PulseAudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with PulseAudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <libudev.h>
27
28 #include <pulse/xmalloc.h>
29 #include <pulse/proplist.h>
30
31 #include <pulsecore/log.h>
32 #include <pulsecore/core-util.h>
33
34 #include "udev-util.h"
35
36 static int read_id(struct udev_device *d, const char *n) {
37     const char *v;
38     unsigned u;
39
40     pa_assert(d);
41     pa_assert(n);
42
43     if (!(v = udev_device_get_property_value(d, n)))
44         return -1;
45
46     if (pa_startswith(v, "0x"))
47         v += 2;
48
49     if (!*v)
50         return -1;
51
52     if (sscanf(v, "%04x", &u) != 1)
53         return -1;
54
55     if (u > 0xFFFFU)
56         return -1;
57
58     return u;
59 }
60
61 static int dehex(char x) {
62     if (x >= '0' && x <= '9')
63         return x - '0';
64
65     if (x >= 'A' && x <= 'F')
66         return x - 'A' + 10;
67
68     if (x >= 'a' && x <= 'f')
69         return x - 'a' + 10;
70
71     return -1;
72 }
73
74 static void proplist_sets_unescape(pa_proplist *p, const char *prop, const char *s) {
75     const char *f;
76     char *t, *r;
77     int c;
78
79     enum {
80         TEXT,
81         BACKSLASH,
82         EX,
83         FIRST
84     } state = TEXT;
85
86     /* The resulting string is definitely shorter than the source string */
87     r = pa_xnew(char, strlen(s)+1);
88
89     for (f = s, t = r; *f; f++) {
90
91         switch (state) {
92
93             case TEXT:
94                 if (*f == '\\')
95                     state = BACKSLASH;
96                 else
97                     *(t++) = *f;
98                 break;
99
100             case BACKSLASH:
101                 if (*f == 'x')
102                     state = EX;
103                 else {
104                     *(t++) = '\\';
105                     *(t++) = *f;
106                     state = TEXT;
107                 }
108                 break;
109
110             case EX:
111                 c = dehex(*f);
112
113                 if (c < 0) {
114                     *(t++) = '\\';
115                     *(t++) = 'x';
116                     *(t++) = *f;
117                     state = TEXT;
118                 } else
119                     state = FIRST;
120
121                 break;
122
123             case FIRST: {
124                 int d = dehex(*f);
125
126                 if (d < 0) {
127                     *(t++) = '\\';
128                     *(t++) = 'x';
129                     *(t++) = *(f-1);
130                     *(t++) = *f;
131                 } else
132                     *(t++) = (char) (c << 4) | d;
133
134                 state = TEXT;
135                 break;
136             }
137         }
138     }
139
140     switch (state) {
141
142         case TEXT:
143             break;
144
145         case BACKSLASH:
146             *(t++) = '\\';
147             break;
148
149         case EX:
150             *(t++) = '\\';
151             *(t++) = 'x';
152             break;
153
154         case FIRST:
155             *(t++) = '\\';
156             *(t++) = 'x';
157             *(t++) = *(f-1);
158             break;
159     }
160
161     *t = 0;
162
163     pa_proplist_sets(p, prop, r);
164     pa_xfree(r);
165 }
166
167 int pa_udev_get_info(int card_idx, pa_proplist *p) {
168     int r = -1;
169     struct udev *udev;
170     struct udev_device *card = NULL;
171     char *t;
172     const char *v;
173     int id;
174
175     pa_assert(p);
176     pa_assert(card_idx >= 0);
177
178     if (!(udev = udev_new())) {
179         pa_log_error("Failed to allocate udev context.");
180         goto finish;
181     }
182
183     t = pa_sprintf_malloc("%s/class/sound/card%i", udev_get_sys_path(udev), card_idx);
184     card = udev_device_new_from_syspath(udev, t);
185     pa_xfree(t);
186
187     if (!card) {
188         pa_log_error("Failed to get card object.");
189         goto finish;
190     }
191
192     if (!pa_proplist_contains(p, PA_PROP_DEVICE_BUS_PATH))
193         if (((v = udev_device_get_property_value(card, "ID_PATH")) && *v) ||
194             (v = udev_device_get_devpath(card)))
195             pa_proplist_sets(p, PA_PROP_DEVICE_BUS_PATH, v);
196
197     if (!pa_proplist_contains(p, "sysfs.path"))
198         if ((v = udev_device_get_devpath(card)))
199             pa_proplist_sets(p, "sysfs.path", v);
200
201     if (!pa_proplist_contains(p, "udev.id"))
202         if ((v = udev_device_get_property_value(card, "ID_ID")) && *v)
203             pa_proplist_sets(p, "udev.id", v);
204
205     if (!pa_proplist_contains(p, PA_PROP_DEVICE_BUS))
206         if ((v = udev_device_get_property_value(card, "ID_BUS")) && *v)
207             pa_proplist_sets(p, PA_PROP_DEVICE_BUS, v);
208
209     if (!pa_proplist_contains(p, PA_PROP_DEVICE_VENDOR_ID))
210         if ((id = read_id(card, "ID_VENDOR_ID")) > 0)
211             pa_proplist_setf(p, PA_PROP_DEVICE_VENDOR_ID, "%04x", id);
212
213     if (!pa_proplist_contains(p, PA_PROP_DEVICE_VENDOR_NAME)) {
214         if ((v = udev_device_get_property_value(card, "ID_VENDOR_FROM_DATABASE")) && *v)
215             pa_proplist_sets(p, PA_PROP_DEVICE_VENDOR_NAME, v);
216         else if ((v = udev_device_get_property_value(card, "ID_VENDOR_ENC")) && *v)
217             proplist_sets_unescape(p, PA_PROP_DEVICE_VENDOR_NAME, v);
218         else if ((v = udev_device_get_property_value(card, "ID_VENDOR")) && *v)
219             pa_proplist_sets(p, PA_PROP_DEVICE_VENDOR_NAME, v);
220     }
221
222     if (!pa_proplist_contains(p, PA_PROP_DEVICE_PRODUCT_ID))
223         if ((id = read_id(card, "ID_MODEL_ID")) >= 0)
224             pa_proplist_setf(p, PA_PROP_DEVICE_PRODUCT_ID, "%04x", id);
225
226     if (!pa_proplist_contains(p, PA_PROP_DEVICE_PRODUCT_NAME)) {
227         if ((v = udev_device_get_property_value(card, "ID_MODEL_FROM_DATABASE")) && *v)
228             pa_proplist_sets(p, PA_PROP_DEVICE_PRODUCT_NAME, v);
229         else if ((v = udev_device_get_property_value(card, "ID_MODEL_ENC")) && *v)
230             proplist_sets_unescape(p, PA_PROP_DEVICE_PRODUCT_NAME, v);
231         else if ((v = udev_device_get_property_value(card, "ID_MODEL")) && *v)
232             pa_proplist_sets(p, PA_PROP_DEVICE_PRODUCT_NAME, v);
233     }
234
235     if (!pa_proplist_contains(p, PA_PROP_DEVICE_SERIAL))
236         if ((v = udev_device_get_property_value(card, "ID_SERIAL")) && *v)
237             pa_proplist_sets(p, PA_PROP_DEVICE_SERIAL, v);
238
239     if (!pa_proplist_contains(p, PA_PROP_DEVICE_CLASS))
240         if ((v = udev_device_get_property_value(card, "SOUND_CLASS")) && *v)
241             pa_proplist_sets(p, PA_PROP_DEVICE_CLASS, v);
242
243     if (!pa_proplist_contains(p, PA_PROP_DEVICE_FORM_FACTOR))
244         if ((v = udev_device_get_property_value(card, "SOUND_FORM_FACTOR")) && *v)
245             pa_proplist_sets(p, PA_PROP_DEVICE_FORM_FACTOR, v);
246
247     /* This is normaly not set by the udev rules but may be useful to
248      * allow administrators to overwrite the device description.*/
249     if (!pa_proplist_contains(p, PA_PROP_DEVICE_DESCRIPTION))
250         if ((v = udev_device_get_property_value(card, "SOUND_DESCRIPTION")) && *v)
251             pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, v);
252
253     r = 0;
254
255 finish:
256
257     if (card)
258         udev_device_unref(card);
259
260     if (udev)
261         udev_unref(udev);
262
263     return r;
264 }
265
266 char* pa_udev_get_property(int card_idx, const char *name) {
267     struct udev *udev;
268     struct udev_device *card = NULL;
269     char *t, *r = NULL;
270     const char *v;
271
272     pa_assert(card_idx >= 0);
273     pa_assert(name);
274
275     if (!(udev = udev_new())) {
276         pa_log_error("Failed to allocate udev context.");
277         goto finish;
278     }
279
280     t = pa_sprintf_malloc("%s/class/sound/card%i", udev_get_sys_path(udev), card_idx);
281     card = udev_device_new_from_syspath(udev, t);
282     pa_xfree(t);
283
284     if (!card) {
285         pa_log_error("Failed to get card object.");
286         goto finish;
287     }
288
289     if ((v = udev_device_get_property_value(card, name)) && *v)
290         r = pa_xstrdup(v);
291
292 finish:
293
294     if (card)
295         udev_device_unref(card);
296
297     if (udev)
298         udev_unref(udev);
299
300     return r;
301 }