VirtualBox

source: vbox/trunk/src/VBox/Main/testcase/tstHostHardwareLinux.cpp@ 36877

Last change on this file since 36877 was 36348, checked in by vboxsync, 14 years ago

Main/USB/Linux: also show state in testcase device enumeration

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1/* $Id: tstHostHardwareLinux.cpp 36348 2011-03-22 15:27:01Z 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
38int 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
57void 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 const char *pcszState =
67 pDevice->enmState == USBDEVICESTATE_UNSUPPORTED ? "UNSUPPORTED"
68 : pDevice->enmState == USBDEVICESTATE_USED_BY_HOST ? "USED_BY_HOST"
69 : pDevice->enmState == USBDEVICESTATE_USED_BY_HOST_CAPTURABLE ? "USED_BY_HOST_CAPTURABLE"
70 : pDevice->enmState == USBDEVICESTATE_UNUSED ? "UNUSED"
71 : pDevice->enmState == USBDEVICESTATE_HELD_BY_PROXY ? "held by proxy"
72 : pDevice->enmState == USBDEVICESTATE_USED_BY_GUEST ? "used by guest"
73 : "invalid";
74 RTPrintf(" Manufacturer: %s, product: %s, serial number string: %s, state: %s\n",
75 pDevice->pszManufacturer, pDevice->pszProduct,
76 pDevice->pszSerialNumber, pcszState);
77 RTPrintf(" Device address: %s\n", pDevice->pszAddress);
78 pDevice = pDevice->pNext;
79 }
80}
81
82void freeDevices(PUSBDEVICE pDevices)
83{
84 PUSBDEVICE pDevice = pDevices, pDeviceNext;
85
86 while (pDevice)
87 {
88 pDeviceNext = pDevice->pNext;
89 deviceFree(pDevice);
90 pDevice = pDeviceNext;
91 }
92}
93
94int main()
95{
96 RTR3Init();
97 int rc = VINF_SUCCESS;
98 VBoxMainDriveInfo driveInfo;
99 rc = driveInfo.updateFloppies();
100 if (RT_SUCCESS(rc))
101 rc = driveInfo.updateDVDs();
102 if (RT_FAILURE(rc))
103 {
104 RTPrintf("Failed to update the host drive information, error %Rrc\n",
105 rc);
106 return 1;
107 }
108 RTPrintf ("Listing floppy drives detected:\n");
109 for (DriveInfoList::const_iterator it = driveInfo.FloppyBegin();
110 it != driveInfo.FloppyEnd(); ++it)
111 {
112 RTPrintf (" device: %s", it->mDevice.c_str());
113 if (!it->mUdi.isEmpty())
114 RTPrintf (", udi: %s", it->mUdi.c_str());
115 if (!it->mDescription.isEmpty())
116 RTPrintf (", description: %s", it->mDescription.c_str());
117 RTPrintf ("\n");
118 }
119 RTPrintf ("Listing DVD drives detected:\n");
120 for (DriveInfoList::const_iterator it = driveInfo.DVDBegin();
121 it != driveInfo.DVDEnd(); ++it)
122 {
123 RTPrintf (" device: %s", it->mDevice.c_str());
124 if (!it->mUdi.isEmpty())
125 RTPrintf (", udi: %s", it->mUdi.c_str());
126 if (!it->mDescription.isEmpty())
127 RTPrintf (", description: %s", it->mDescription.c_str());
128 RTPrintf ("\n");
129 }
130 PCUSBDEVTREELOCATION pcLocation = USBProxyLinuxGetDeviceRoot(false);
131 if (pcLocation && !pcLocation->fUseSysfs)
132 {
133 PUSBDEVICE pDevice = USBProxyLinuxGetDevices(pcLocation->szDevicesRoot,
134 false);
135 printDevices(pDevice, pcLocation->szDevicesRoot, "usbfs");
136 freeDevices(pDevice);
137 }
138#ifdef VBOX_USB_WITH_SYSFS
139 pcLocation = USBProxyLinuxGetDeviceRoot(true);
140 if (pcLocation && pcLocation->fUseSysfs)
141 {
142 PUSBDEVICE pDevice = USBProxyLinuxGetDevices(pcLocation->szDevicesRoot,
143 true);
144 printDevices(pDevice, pcLocation->szDevicesRoot, "sysfs");
145 freeDevices(pDevice);
146 }
147 VBoxMainHotplugWaiter waiter(pcLocation->szDevicesRoot);
148 RTPrintf ("Waiting for a hotplug event for five seconds...\n");
149 doHotplugEvent(&waiter, 5000);
150 RTPrintf ("Waiting for a hotplug event, Ctrl-C to abort...\n");
151 doHotplugEvent(&waiter, RT_INDEFINITE_WAIT);
152 RTPrintf ("Testing interrupting a hotplug event...\n");
153 waiter.Interrupt();
154 rc = doHotplugEvent(&waiter, 5000);
155 RTPrintf ("%s\n", rc == VERR_INTERRUPTED ? "Success!" : "Failed!");
156#endif /* VBOX_USB_WITH_SYSFS */
157 return 0;
158}
159
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette