1 | /** @file
|
---|
2 | * USBLib - Library for wrapping up the VBoxUSB functionality. (DEV,HDrv,Main)
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef VBOX_INCLUDED_usblib_h
|
---|
27 | #define VBOX_INCLUDED_usblib_h
|
---|
28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
29 | # pragma once
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <VBox/cdefs.h>
|
---|
33 | #include <VBox/types.h>
|
---|
34 | #include <VBox/usb.h>
|
---|
35 | #include <VBox/usbfilter.h>
|
---|
36 | #include <iprt/ctype.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 |
|
---|
39 | #ifdef RT_OS_WINDOWS
|
---|
40 | # include <VBox/usblib-win.h>
|
---|
41 | #endif
|
---|
42 | #ifdef RT_OS_SOLARIS
|
---|
43 | # include <VBox/usblib-solaris.h>
|
---|
44 | #endif
|
---|
45 | #ifdef RT_OS_DARWIN
|
---|
46 | # include <VBox/usblib-darwin.h>
|
---|
47 | #endif
|
---|
48 | /** @todo merge the usblib-win.h interface into the darwin and linux ports where suitable. */
|
---|
49 |
|
---|
50 | RT_C_DECLS_BEGIN
|
---|
51 | /** @defgroup grp_usblib USBLib - USB Support Library
|
---|
52 | * This module implements the basic low-level OS interfaces and common USB code.
|
---|
53 | * @{
|
---|
54 | */
|
---|
55 |
|
---|
56 | #ifdef IN_RING3
|
---|
57 | /**
|
---|
58 | * Initializes the USBLib component.
|
---|
59 | *
|
---|
60 | * The USBLib keeps a per process connection to the kernel driver
|
---|
61 | * and all USBLib users within a process will share the same
|
---|
62 | * connection. USBLib does reference counting to make sure that
|
---|
63 | * the connection remains open until all users has called USBLibTerm().
|
---|
64 | *
|
---|
65 | * @returns VBox status code.
|
---|
66 | *
|
---|
67 | * @remark The users within the process are responsible for not calling
|
---|
68 | * this function at the same time (because I'm lazy).
|
---|
69 | */
|
---|
70 | USBLIB_DECL(int) USBLibInit(void);
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Terminates the USBLib component.
|
---|
74 | *
|
---|
75 | * Must match successful USBLibInit calls.
|
---|
76 | *
|
---|
77 | * @returns VBox status code.
|
---|
78 | */
|
---|
79 | USBLIB_DECL(int) USBLibTerm(void);
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Adds a filter.
|
---|
83 | *
|
---|
84 | * This function will validate and transfer the specified filter
|
---|
85 | * to the kernel driver and make it start using it. The kernel
|
---|
86 | * driver will return a filter id that this function passes on
|
---|
87 | * to its caller.
|
---|
88 | *
|
---|
89 | * The kernel driver will associate the added filter with the
|
---|
90 | * calling process and automatically remove all filters when
|
---|
91 | * the process terminates the connection to it or dies.
|
---|
92 | *
|
---|
93 | * @returns Filter id for passing to USBLibRemoveFilter on success.
|
---|
94 | * @returns NULL on failure.
|
---|
95 | *
|
---|
96 | * @param pFilter The filter to add.
|
---|
97 | */
|
---|
98 | USBLIB_DECL(void *) USBLibAddFilter(PCUSBFILTER pFilter);
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Removes a filter.
|
---|
102 | *
|
---|
103 | * @param pvId The ID returned by USBLibAddFilter.
|
---|
104 | */
|
---|
105 | USBLIB_DECL(void) USBLibRemoveFilter(void *pvId);
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Calculate the hash of the serial string.
|
---|
109 | *
|
---|
110 | * 64bit FNV1a, chosen because it is designed to hash in to a power of two
|
---|
111 | * space, and is much quicker and simpler than, say, a half MD4.
|
---|
112 | *
|
---|
113 | * @returns the hash.
|
---|
114 | * @param pszSerial The serial string.
|
---|
115 | */
|
---|
116 | USBLIB_DECL(uint64_t) USBLibHashSerial(const char *pszSerial);
|
---|
117 |
|
---|
118 | #endif /* IN_RING3 */
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Purge string of non-UTF-8 encodings and control characters.
|
---|
122 | *
|
---|
123 | * Control characters creates problems when presented to the user and currently
|
---|
124 | * also when used in XML settings. So, we must purge them in the USB vendor,
|
---|
125 | * product, and serial number strings.
|
---|
126 | *
|
---|
127 | * @returns String length (excluding terminator).
|
---|
128 | * @param psz The string to purge.
|
---|
129 | *
|
---|
130 | * @remarks The return string may be shorter than the input, left over space
|
---|
131 | * after the end of the string will be filled with zeros.
|
---|
132 | */
|
---|
133 | DECLINLINE(size_t) USBLibPurgeEncoding(char *psz)
|
---|
134 | {
|
---|
135 | if (psz)
|
---|
136 | {
|
---|
137 | size_t offSrc;
|
---|
138 |
|
---|
139 | /* Beat it into valid UTF-8 encoding. */
|
---|
140 | RTStrPurgeEncoding(psz);
|
---|
141 |
|
---|
142 | /* Look for control characters. */
|
---|
143 | for (offSrc = 0; ; offSrc++)
|
---|
144 | {
|
---|
145 | char ch = psz[offSrc];
|
---|
146 | if (RT_UNLIKELY(RT_C_IS_CNTRL(ch) && ch != '\0'))
|
---|
147 | {
|
---|
148 | /* Found a control character! Replace tab by space and remove all others. */
|
---|
149 | size_t offDst = offSrc;
|
---|
150 | for (;; offSrc++)
|
---|
151 | {
|
---|
152 | ch = psz[offSrc];
|
---|
153 | if (RT_C_IS_CNTRL(ch) && ch != '\0')
|
---|
154 | {
|
---|
155 | if (ch == '\t')
|
---|
156 | ch = ' ';
|
---|
157 | else
|
---|
158 | continue;
|
---|
159 | }
|
---|
160 | psz[offDst++] = ch;
|
---|
161 | if (ch == '\0')
|
---|
162 | break;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /* Wind back to the zero terminator and zero fill any gap to make
|
---|
166 | USBFilterValidate happy. (offSrc is at zero terminator too.) */
|
---|
167 | offDst--;
|
---|
168 | while (offSrc > offDst)
|
---|
169 | psz[offSrc--] = '\0';
|
---|
170 |
|
---|
171 | return offDst;
|
---|
172 | }
|
---|
173 | if (ch == '\0')
|
---|
174 | break;
|
---|
175 | }
|
---|
176 | return offSrc;
|
---|
177 | }
|
---|
178 | return 0;
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | /** @} */
|
---|
183 | RT_C_DECLS_END
|
---|
184 |
|
---|
185 | #endif /* !VBOX_INCLUDED_usblib_h */
|
---|
186 |
|
---|