hythmbox-2.98/lib/libmediaplayerid/mpid-udev.c

No issues found

 1 /*
 2  * Copyright (C) 2009 Jonathan Matthew  <jonathan@d14n.org>
 3  *
 4  * This library is free software; you can redistribute it and/or
 5  * modify it under the terms of the GNU Lesser General Public
 6  * License as published by the Free Software Foundation; either
 7  * version 2 of the License, or (at your option) any later version.
 8  *
 9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 
20 #include <glib.h>
21 
22 #define G_UDEV_API_IS_SUBJECT_TO_CHANGE
23 #include <gudev/gudev.h>
24 
25 #include "mediaplayerid.h"
26 #include "mpid-private.h"
27 
28 void
29 mpid_device_db_lookup (MPIDDevice *device)
30 {
31 	GUdevClient *client;
32 	GUdevDevice *udevice = NULL;
33 	char *devpath;
34 	const char *device_file;
35 	char *subsystems[] = { "usb", NULL };
36 
37 	devpath = mpid_device_get_device_path (device);
38 	if (devpath == NULL) {
39 		device->error = MPID_ERROR_NO_DEVICE_PATH;
40 		return;
41 	}
42 	
43 	client = g_udev_client_new ((const char * const *)subsystems);
44 	if (client != NULL) {
45 		udevice = g_udev_client_query_by_device_file (client, devpath);
46 		if (udevice != NULL) {
47 
48 			/* get vendor and model names, UUID, and serial */
49 			device->model = g_strdup (g_udev_device_get_property (udevice, "ID_MODEL"));
50 			device->vendor = g_strdup (g_udev_device_get_property (udevice, "ID_VENDOR"));
51 			device->fs_uuid = g_strdup (g_udev_device_get_property (udevice, "ID_FS_UUID"));
52 			device->serial = g_strdup (g_udev_device_get_property (udevice, "ID_SERIAL"));
53 
54 			/* get media player information */
55 			device_file = g_udev_device_get_property (udevice, "ID_MEDIA_PLAYER");
56 			if (device_file != NULL) {
57 				mpid_debug ("found ID_MEDIA_PLAYER tag %s for device %s\n", device_file, devpath);
58 				mpid_find_and_read_device_file (device, device_file);
59 			} else {
60 				mpid_debug ("device %s has no ID_MEDIA_PLAYER tag in udev\n", devpath);
61 				device->error = MPID_ERROR_NOT_MEDIA_PLAYER;
62 			}
63 		} else {
64 			mpid_debug ("unable to find device %s in udev\n", devpath);
65 			device->error = MPID_ERROR_MECHANISM_FAILED;
66 		}
67 	} else {
68 		mpid_debug ("unable to create udev client\n");
69 		device->error = MPID_ERROR_MECHANISM_FAILED;
70 	}
71 
72 	g_free (devpath);
73 	if (udevice != NULL) {
74 		g_object_unref (udevice);
75 	}
76 	if (client != NULL) {
77 		g_object_unref (client);
78 	}
79 }