1 | /* $Id: tstHostHardwareLinux.cpp 34341 2010-11-24 20:14:36Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * Test executable for quickly excercising/debugging the Linux host hardware
|
---|
5 | * bits.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2008 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include <HostHardwareLinux.h>
|
---|
21 | #include <USBGetDevices.h>
|
---|
22 |
|
---|
23 | #include <VBox/err.h>
|
---|
24 |
|
---|
25 | #include <iprt/initterm.h>
|
---|
26 | #include <iprt/param.h>
|
---|
27 | #include <iprt/stream.h>
|
---|
28 | #include <iprt/thread.h>
|
---|
29 | #include <iprt/linux/sysfs.h>
|
---|
30 |
|
---|
31 | #include <iprt/cdefs.h>
|
---|
32 | #include <iprt/types.h>
|
---|
33 |
|
---|
34 | #include <errno.h>
|
---|
35 | #include <string.h>
|
---|
36 | #include <stdlib.h>
|
---|
37 |
|
---|
38 | int doHotplugEvent(VBoxMainHotplugWaiter *waiter, RTMSINTERVAL cMillies)
|
---|
39 | {
|
---|
40 | int rc;
|
---|
41 | while (true)
|
---|
42 | {
|
---|
43 | rc = waiter->Wait (cMillies);
|
---|
44 | if (rc == VERR_TIMEOUT || rc == VERR_INTERRUPTED)
|
---|
45 | break;
|
---|
46 | if (RT_FAILURE(rc))
|
---|
47 | {
|
---|
48 | RTPrintf("Failed!\n");
|
---|
49 | exit(1);
|
---|
50 | }
|
---|
51 | if (RT_SUCCESS(rc))
|
---|
52 | break;
|
---|
53 | }
|
---|
54 | return rc;
|
---|
55 | }
|
---|
56 |
|
---|
57 | void printDevices(PUSBDEVICE pDevices,
|
---|
58 | const char *pcszDevices,
|
---|
59 | const char *pcszMethod)
|
---|
60 | {
|
---|
61 | PUSBDEVICE pDevice = pDevices;
|
---|
62 |
|
---|
63 | RTPrintf("Enumerating usb devices using %s at %s\n", pcszMethod, pcszDevices);
|
---|
64 | while (pDevice)
|
---|
65 | {
|
---|
66 | RTPrintf(" Manufacturer: %s, product: %s, serial number string: %s\n",
|
---|
67 | pDevice->pszManufacturer, pDevice->pszProduct,
|
---|
68 | pDevice->pszSerialNumber);
|
---|
69 | RTPrintf(" Device address: %s\n", pDevice->pszAddress);
|
---|
70 | pDevice = pDevice->pNext;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | void freeDevices(PUSBDEVICE pDevices)
|
---|
75 | {
|
---|
76 | PUSBDEVICE pDevice = pDevices, pDeviceNext;
|
---|
77 |
|
---|
78 | while (pDevice)
|
---|
79 | {
|
---|
80 | pDeviceNext = pDevice->pNext;
|
---|
81 | deviceFree(pDevice);
|
---|
82 | pDevice = pDeviceNext;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | int main()
|
---|
87 | {
|
---|
88 | RTR3Init();
|
---|
89 | int rc = VINF_SUCCESS;
|
---|
90 | VBoxMainDriveInfo driveInfo;
|
---|
91 | rc = driveInfo.updateFloppies();
|
---|
92 | if (RT_SUCCESS(rc))
|
---|
93 | rc = driveInfo.updateDVDs();
|
---|
94 | if (RT_FAILURE(rc))
|
---|
95 | {
|
---|
96 | RTPrintf("Failed to update the host drive information, error %Rrc\n",
|
---|
97 | rc);
|
---|
98 | return 1;
|
---|
99 | }
|
---|
100 | RTPrintf ("Listing floppy drives detected:\n");
|
---|
101 | for (DriveInfoList::const_iterator it = driveInfo.FloppyBegin();
|
---|
102 | it != driveInfo.FloppyEnd(); ++it)
|
---|
103 | {
|
---|
104 | RTPrintf (" device: %s", it->mDevice.c_str());
|
---|
105 | if (!it->mUdi.isEmpty())
|
---|
106 | RTPrintf (", udi: %s", it->mUdi.c_str());
|
---|
107 | if (!it->mDescription.isEmpty())
|
---|
108 | RTPrintf (", description: %s", it->mDescription.c_str());
|
---|
109 | RTPrintf ("\n");
|
---|
110 | }
|
---|
111 | RTPrintf ("Listing DVD drives detected:\n");
|
---|
112 | for (DriveInfoList::const_iterator it = driveInfo.DVDBegin();
|
---|
113 | it != driveInfo.DVDEnd(); ++it)
|
---|
114 | {
|
---|
115 | RTPrintf (" device: %s", it->mDevice.c_str());
|
---|
116 | if (!it->mUdi.isEmpty())
|
---|
117 | RTPrintf (", udi: %s", it->mUdi.c_str());
|
---|
118 | if (!it->mDescription.isEmpty())
|
---|
119 | RTPrintf (", description: %s", it->mDescription.c_str());
|
---|
120 | RTPrintf ("\n");
|
---|
121 | }
|
---|
122 | PCUSBDEVTREELOCATION pcLocation = USBProxyLinuxGetDeviceRoot(false);
|
---|
123 | if (pcLocation && !pcLocation->fUseSysfs)
|
---|
124 | {
|
---|
125 | PUSBDEVICE pDevice = USBProxyLinuxGetDevices(pcLocation->szDevicesRoot,
|
---|
126 | false);
|
---|
127 | printDevices(pDevice, pcLocation->szDevicesRoot, "usbfs");
|
---|
128 | freeDevices(pDevice);
|
---|
129 | }
|
---|
130 | #ifdef VBOX_USB_WITH_SYSFS
|
---|
131 | pcLocation = USBProxyLinuxGetDeviceRoot(true);
|
---|
132 | if (pcLocation && pcLocation->fUseSysfs)
|
---|
133 | {
|
---|
134 | PUSBDEVICE pDevice = USBProxyLinuxGetDevices(pcLocation->szDevicesRoot,
|
---|
135 | true);
|
---|
136 | printDevices(pDevice, pcLocation->szDevicesRoot, "sysfs");
|
---|
137 | freeDevices(pDevice);
|
---|
138 | }
|
---|
139 | VBoxMainHotplugWaiter waiter(pcLocation->szDevicesRoot);
|
---|
140 | RTPrintf ("Waiting for a hotplug event for five seconds...\n");
|
---|
141 | doHotplugEvent(&waiter, 5000);
|
---|
142 | RTPrintf ("Waiting for a hotplug event, Ctrl-C to abort...\n");
|
---|
143 | doHotplugEvent(&waiter, RT_INDEFINITE_WAIT);
|
---|
144 | RTPrintf ("Testing interrupting a hotplug event...\n");
|
---|
145 | waiter.Interrupt();
|
---|
146 | rc = doHotplugEvent(&waiter, 5000);
|
---|
147 | RTPrintf ("%s\n", rc == VERR_INTERRUPTED ? "Success!" : "Failed!");
|
---|
148 | #endif /* VBOX_USB_WITH_SYSFS */
|
---|
149 | return 0;
|
---|
150 | }
|
---|
151 |
|
---|