VirtualBox

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

Last change on this file since 32142 was 32142, checked in by vboxsync, 15 years ago

Main/linux/USB: moved a vector container-based implementation detail out of the interface

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1/* $Id: tstHostHardwareLinux.cpp 32142 2010-08-31 13:00:46Z 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.215389.xyz. 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
22#include <VBox/err.h>
23
24#include <iprt/initterm.h>
25#include <iprt/param.h>
26#include <iprt/stream.h>
27#include <iprt/thread.h>
28#include <iprt/linux/sysfs.h>
29
30#include <iprt/cdefs.h>
31#include <iprt/types.h>
32
33#include <errno.h>
34#include <string.h>
35#include <stdlib.h>
36
37int doHotplugEvent(VBoxMainHotplugWaiter *waiter, RTMSINTERVAL cMillies)
38{
39 int rc;
40 while (true)
41 {
42 rc = waiter->Wait (cMillies);
43 if (rc == VERR_TIMEOUT || rc == VERR_INTERRUPTED)
44 break;
45 if (RT_FAILURE(rc))
46 {
47 RTPrintf("Failed!\n");
48 exit(1);
49 }
50 if (RT_SUCCESS(rc))
51 break;
52 }
53 return rc;
54}
55
56int main()
57{
58 RTR3Init();
59 int rc = VINF_SUCCESS;
60 VBoxMainDriveInfo driveInfo;
61 rc = driveInfo.updateFloppies();
62 if (RT_SUCCESS(rc))
63 rc = driveInfo.updateDVDs();
64 if (RT_FAILURE(rc))
65 {
66 RTPrintf("Failed to update the host drive information, error %Rrc\n",
67 rc);
68 return 1;
69 }
70 RTPrintf ("Listing floppy drives detected:\n");
71 for (DriveInfoList::const_iterator it = driveInfo.FloppyBegin();
72 it != driveInfo.FloppyEnd(); ++it)
73 {
74 RTPrintf (" device: %s", it->mDevice.c_str());
75 if (!it->mUdi.isEmpty())
76 RTPrintf (", udi: %s", it->mUdi.c_str());
77 if (!it->mDescription.isEmpty())
78 RTPrintf (", description: %s", it->mDescription.c_str());
79 RTPrintf ("\n");
80 }
81 RTPrintf ("Listing DVD drives detected:\n");
82 for (DriveInfoList::const_iterator it = driveInfo.DVDBegin();
83 it != driveInfo.DVDEnd(); ++it)
84 {
85 RTPrintf (" device: %s", it->mDevice.c_str());
86 if (!it->mUdi.isEmpty())
87 RTPrintf (", udi: %s", it->mUdi.c_str());
88 if (!it->mDescription.isEmpty())
89 RTPrintf (", description: %s", it->mDescription.c_str());
90 RTPrintf ("\n");
91 }
92#ifdef VBOX_USB_WITH_SYSFS
93 VBoxMainUSBDeviceInfo deviceInfo;
94 AssertReturn(VBoxMainUSBDevInfoInit(&deviceInfo), 1);
95 rc = USBDevInfoUpdateDevices(&deviceInfo);
96 if (RT_FAILURE(rc))
97 {
98 RTPrintf ("Failed to update the host USB device information, error %Rrc\n",
99 rc);
100 return 1;
101 }
102 RTPrintf ("Listing USB devices detected:\n");
103 USBDeviceInfoList_iterator it;
104 USBDeviceInfoList_iter_init(&it, USBDevInfoBegin(&deviceInfo));
105 for (; !USBDeviceInfoList_iter_eq(&it, USBDevInfoEnd(&deviceInfo));
106 USBDeviceInfoList_iter_incr(&it))
107 {
108 char szProduct[1024];
109 USBDeviceInfo *pInfo = USBDeviceInfoList_iter_target(&it);
110 if (RTLinuxSysFsReadStrFile(szProduct, sizeof(szProduct),
111 "%s/product", pInfo->mSysfsPath) == -1)
112 {
113 if (errno != ENOENT)
114 {
115 RTPrintf ("Failed to get the product name for device %s: error %s\n",
116 pInfo->mDevice, strerror(errno));
117 return 1;
118 }
119 else
120 szProduct[0] = '\0';
121 }
122 RTPrintf (" device: %s (%s), sysfs path: %s\n", szProduct, pInfo->mDevice,
123 pInfo->mSysfsPath);
124 RTPrintf (" interfaces:\n");
125 char *pszIf;
126 for (pszIf = USBDevInfoFirstInterface(pInfo->mInterfaces); pszIf;
127 pszIf = USBDevInfoNextInterface(pInfo->mInterfaces))
128 {
129 char szDriver[RTPATH_MAX];
130 strcpy(szDriver, "none");
131 ssize_t size = RTLinuxSysFsGetLinkDest(szDriver, sizeof(szDriver),
132 "%s/driver", pszIf);
133 if (size == -1 && errno != ENOENT)
134 {
135 RTPrintf ("Failed to get the driver for interface %s of device %s: error %s\n",
136 pszIf, pInfo->mDevice, strerror(errno));
137 return 1;
138 }
139 if (RTLinuxSysFsExists("%s/driver", pszIf) != (size != -1))
140 {
141 RTPrintf ("RTLinuxSysFsExists did not return the expected value for the driver link of interface %s of device %s.\n",
142 pszIf, pInfo->mDevice);
143 return 1;
144 }
145 uint64_t u64InterfaceClass;
146 u64InterfaceClass = RTLinuxSysFsReadIntFile(16, "%s/bInterfaceClass",
147 pszIf);
148 RTPrintf (" sysfs path: %s, driver: %s, interface class: 0x%x\n",
149 pszIf, szDriver, u64InterfaceClass);
150 }
151 }
152 VBoxMainHotplugWaiter waiter;
153 RTPrintf ("Waiting for a hotplug event for five seconds...\n");
154 doHotplugEvent(&waiter, 5000);
155 RTPrintf ("Waiting for a hotplug event, Ctrl-C to abort...\n");
156 doHotplugEvent(&waiter, RT_INDEFINITE_WAIT);
157 RTPrintf ("Testing interrupting a hotplug event...\n");
158 waiter.Interrupt();
159 rc = doHotplugEvent(&waiter, 5000);
160 RTPrintf ("%s\n", rc == VERR_INTERRUPTED ? "Success!" : "Failed!");
161#endif /* VBOX_USB_WITH_SYSFS */
162 return 0;
163}
164
Note: See TracBrowser for help on using the repository browser.

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