1 | /* $Id: UsbTestServiceGadgetCfg.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * UsbTestServ - Remote USB test configuration and execution server, USB gadget Cfg API.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2016-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/cdefs.h>
|
---|
42 | #include <iprt/ctype.h>
|
---|
43 | #include <iprt/err.h>
|
---|
44 | #include <iprt/mem.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 |
|
---|
47 | #include "UsbTestServiceGadget.h"
|
---|
48 |
|
---|
49 |
|
---|
50 | /*********************************************************************************************************************************
|
---|
51 | * Internal Functions *
|
---|
52 | *********************************************************************************************************************************/
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Returns the gadget configuration item matching the given key.
|
---|
56 | *
|
---|
57 | * @returns Pointer to the configuration item on success or NULL if not found.
|
---|
58 | * @param paCfg The configuration item array.
|
---|
59 | * @param pszKey The key to look for.
|
---|
60 | */
|
---|
61 | static PCUTSGADGETCFGITEM utsGadgetCfgGetItemFromKey(PCUTSGADGETCFGITEM paCfg, const char *pszKey)
|
---|
62 | {
|
---|
63 | while ( paCfg
|
---|
64 | && paCfg->pszKey)
|
---|
65 | {
|
---|
66 | if (!RTStrCmp(paCfg->pszKey, pszKey))
|
---|
67 | return paCfg;
|
---|
68 |
|
---|
69 | paCfg++;
|
---|
70 | }
|
---|
71 | return NULL;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 |
|
---|
76 | DECLHIDDEN(int) utsGadgetCfgQueryBool(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
77 | bool *pf)
|
---|
78 | {
|
---|
79 | int rc = VERR_NOT_FOUND;
|
---|
80 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
81 |
|
---|
82 | if (pCfgItem)
|
---|
83 | {
|
---|
84 | if (pCfgItem->Val.enmType == UTSGADGETCFGTYPE_BOOLEAN)
|
---|
85 | {
|
---|
86 | *pf = pCfgItem->Val.u.f;
|
---|
87 | rc = VINF_SUCCESS;
|
---|
88 | }
|
---|
89 | else
|
---|
90 | rc = VERR_INVALID_PARAMETER;
|
---|
91 | }
|
---|
92 |
|
---|
93 | return rc;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | DECLHIDDEN(int) utsGadgetCfgQueryBoolDef(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
98 | bool *pf, bool fDef)
|
---|
99 | {
|
---|
100 | int rc = VERR_INVALID_PARAMETER;
|
---|
101 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
102 |
|
---|
103 | if ( !pCfgItem
|
---|
104 | || pCfgItem->Val.enmType == UTSGADGETCFGTYPE_BOOLEAN)
|
---|
105 | {
|
---|
106 | *pf = pCfgItem ? pCfgItem->Val.u.f : fDef;
|
---|
107 | rc = VINF_SUCCESS;
|
---|
108 | }
|
---|
109 |
|
---|
110 | return rc;
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | DECLHIDDEN(int) utsGadgetCfgQueryString(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
115 | char **ppszVal)
|
---|
116 | {
|
---|
117 | int rc = VERR_NOT_FOUND;
|
---|
118 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
119 |
|
---|
120 | if (pCfgItem)
|
---|
121 | {
|
---|
122 | if (pCfgItem->Val.enmType == UTSGADGETCFGTYPE_STRING)
|
---|
123 | {
|
---|
124 | *ppszVal = RTStrDup(pCfgItem->Val.u.psz);
|
---|
125 | if (*ppszVal)
|
---|
126 | rc = VINF_SUCCESS;
|
---|
127 | else
|
---|
128 | rc = VERR_NO_STR_MEMORY;
|
---|
129 | }
|
---|
130 | else
|
---|
131 | rc = VERR_INVALID_PARAMETER;
|
---|
132 | }
|
---|
133 |
|
---|
134 | return rc;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | DECLHIDDEN(int) utsGadgetCfgQueryStringDef(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
139 | char **ppszVal, const char *pszDef)
|
---|
140 | {
|
---|
141 | int rc = VERR_NOT_FOUND;
|
---|
142 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
143 |
|
---|
144 | if ( !pCfgItem
|
---|
145 | || pCfgItem->Val.enmType == UTSGADGETCFGTYPE_STRING)
|
---|
146 | {
|
---|
147 | *ppszVal = RTStrDup(pCfgItem ? pCfgItem->Val.u.psz : pszDef);
|
---|
148 | if (*ppszVal)
|
---|
149 | rc = VINF_SUCCESS;
|
---|
150 | else
|
---|
151 | rc = VERR_NO_STR_MEMORY;
|
---|
152 | }
|
---|
153 | else
|
---|
154 | rc = VERR_INVALID_PARAMETER;
|
---|
155 |
|
---|
156 | return rc;
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | DECLHIDDEN(int) utsGadgetCfgQueryU8(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
161 | uint8_t *pu8)
|
---|
162 | {
|
---|
163 | int rc = VERR_NOT_FOUND;
|
---|
164 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
165 |
|
---|
166 | if (pCfgItem)
|
---|
167 | {
|
---|
168 | if (pCfgItem->Val.enmType == UTSGADGETCFGTYPE_UINT8)
|
---|
169 | {
|
---|
170 | *pu8 = pCfgItem->Val.u.u8;
|
---|
171 | rc = VINF_SUCCESS;
|
---|
172 | }
|
---|
173 | else
|
---|
174 | rc = VERR_INVALID_PARAMETER;
|
---|
175 | }
|
---|
176 |
|
---|
177 | return rc;
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | DECLHIDDEN(int) utsGadgetCfgQueryU8Def(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
182 | uint8_t *pu8, uint8_t u8Def)
|
---|
183 | {
|
---|
184 | int rc = VERR_INVALID_PARAMETER;
|
---|
185 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
186 |
|
---|
187 | if ( !pCfgItem
|
---|
188 | || pCfgItem->Val.enmType == UTSGADGETCFGTYPE_UINT8)
|
---|
189 | {
|
---|
190 | *pu8 = pCfgItem ? pCfgItem->Val.u.u8 : u8Def;
|
---|
191 | rc = VINF_SUCCESS;
|
---|
192 | }
|
---|
193 |
|
---|
194 | return rc;
|
---|
195 | }
|
---|
196 |
|
---|
197 |
|
---|
198 | DECLHIDDEN(int) utsGadgetCfgQueryU16(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
199 | uint16_t *pu16)
|
---|
200 | {
|
---|
201 | int rc = VERR_NOT_FOUND;
|
---|
202 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
203 |
|
---|
204 | if (pCfgItem)
|
---|
205 | {
|
---|
206 | if (pCfgItem->Val.enmType == UTSGADGETCFGTYPE_UINT16)
|
---|
207 | {
|
---|
208 | *pu16 = pCfgItem->Val.u.u16;
|
---|
209 | rc = VINF_SUCCESS;
|
---|
210 | }
|
---|
211 | else
|
---|
212 | rc = VERR_INVALID_PARAMETER;
|
---|
213 | }
|
---|
214 |
|
---|
215 | return rc;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | DECLHIDDEN(int) utsGadgetCfgQueryU16Def(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
220 | uint16_t *pu16, uint16_t u16Def)
|
---|
221 | {
|
---|
222 | int rc = VERR_INVALID_PARAMETER;
|
---|
223 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
224 |
|
---|
225 | if ( !pCfgItem
|
---|
226 | || pCfgItem->Val.enmType == UTSGADGETCFGTYPE_UINT16)
|
---|
227 | {
|
---|
228 | *pu16 = pCfgItem ? pCfgItem->Val.u.u16 : u16Def;
|
---|
229 | rc = VINF_SUCCESS;
|
---|
230 | }
|
---|
231 |
|
---|
232 | return rc;
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | DECLHIDDEN(int) utsGadgetCfgQueryU32(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
237 | uint32_t *pu32)
|
---|
238 | {
|
---|
239 | int rc = VERR_NOT_FOUND;
|
---|
240 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
241 |
|
---|
242 | if (pCfgItem)
|
---|
243 | {
|
---|
244 | if (pCfgItem->Val.enmType == UTSGADGETCFGTYPE_UINT32)
|
---|
245 | {
|
---|
246 | *pu32 = pCfgItem->Val.u.u32;
|
---|
247 | rc = VINF_SUCCESS;
|
---|
248 | }
|
---|
249 | else
|
---|
250 | rc = VERR_INVALID_PARAMETER;
|
---|
251 | }
|
---|
252 |
|
---|
253 | return rc;
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | DECLHIDDEN(int) utsGadgetCfgQueryU32Def(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
258 | uint32_t *pu32, uint32_t u32Def)
|
---|
259 | {
|
---|
260 | int rc = VERR_INVALID_PARAMETER;
|
---|
261 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
262 |
|
---|
263 | if ( !pCfgItem
|
---|
264 | || pCfgItem->Val.enmType == UTSGADGETCFGTYPE_UINT32)
|
---|
265 | {
|
---|
266 | *pu32 = pCfgItem ? pCfgItem->Val.u.u32 : u32Def;
|
---|
267 | rc = VINF_SUCCESS;
|
---|
268 | }
|
---|
269 |
|
---|
270 | return rc;
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 | DECLHIDDEN(int) utsGadgetCfgQueryU64(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
275 | uint64_t *pu64)
|
---|
276 | {
|
---|
277 | int rc = VERR_NOT_FOUND;
|
---|
278 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
279 |
|
---|
280 | if (pCfgItem)
|
---|
281 | {
|
---|
282 | if (pCfgItem->Val.enmType == UTSGADGETCFGTYPE_UINT64)
|
---|
283 | {
|
---|
284 | *pu64 = pCfgItem->Val.u.u64;
|
---|
285 | rc = VINF_SUCCESS;
|
---|
286 | }
|
---|
287 | else
|
---|
288 | rc = VERR_INVALID_PARAMETER;
|
---|
289 | }
|
---|
290 |
|
---|
291 | return rc;
|
---|
292 | }
|
---|
293 |
|
---|
294 |
|
---|
295 | DECLHIDDEN(int) utsGadgetCfgQueryU64Def(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
296 | uint64_t *pu64, uint64_t u64Def)
|
---|
297 | {
|
---|
298 | int rc = VERR_INVALID_PARAMETER;
|
---|
299 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
300 |
|
---|
301 | if ( !pCfgItem
|
---|
302 | || pCfgItem->Val.enmType == UTSGADGETCFGTYPE_UINT64)
|
---|
303 | {
|
---|
304 | *pu64 = pCfgItem ? pCfgItem->Val.u.u64 : u64Def;
|
---|
305 | rc = VINF_SUCCESS;
|
---|
306 | }
|
---|
307 |
|
---|
308 | return rc;
|
---|
309 | }
|
---|
310 |
|
---|
311 |
|
---|
312 | DECLHIDDEN(int) utsGadgetCfgQueryS8(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
313 | int8_t *pi8)
|
---|
314 | {
|
---|
315 | int rc = VERR_NOT_FOUND;
|
---|
316 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
317 |
|
---|
318 | if (pCfgItem)
|
---|
319 | {
|
---|
320 | if (pCfgItem->Val.enmType == UTSGADGETCFGTYPE_INT8)
|
---|
321 | {
|
---|
322 | *pi8 = pCfgItem->Val.u.i8;
|
---|
323 | rc = VINF_SUCCESS;
|
---|
324 | }
|
---|
325 | else
|
---|
326 | rc = VERR_INVALID_PARAMETER;
|
---|
327 | }
|
---|
328 |
|
---|
329 | return rc;
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|
333 | DECLHIDDEN(int) utsGadgetCfgQueryS8Def(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
334 | int8_t *pi8, uint8_t i8Def)
|
---|
335 | {
|
---|
336 | int rc = VERR_INVALID_PARAMETER;
|
---|
337 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
338 |
|
---|
339 | if ( !pCfgItem
|
---|
340 | || pCfgItem->Val.enmType == UTSGADGETCFGTYPE_INT8)
|
---|
341 | {
|
---|
342 | *pi8 = pCfgItem ? pCfgItem->Val.u.i8 : i8Def;
|
---|
343 | rc = VINF_SUCCESS;
|
---|
344 | }
|
---|
345 |
|
---|
346 | return rc;
|
---|
347 | }
|
---|
348 |
|
---|
349 |
|
---|
350 | DECLHIDDEN(int) utsGadgetCfgQueryS16(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
351 | uint16_t *pi16)
|
---|
352 | {
|
---|
353 | int rc = VERR_NOT_FOUND;
|
---|
354 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
355 |
|
---|
356 | if (pCfgItem)
|
---|
357 | {
|
---|
358 | if (pCfgItem->Val.enmType == UTSGADGETCFGTYPE_INT16)
|
---|
359 | {
|
---|
360 | *pi16 = pCfgItem->Val.u.i16;
|
---|
361 | rc = VINF_SUCCESS;
|
---|
362 | }
|
---|
363 | else
|
---|
364 | rc = VERR_INVALID_PARAMETER;
|
---|
365 | }
|
---|
366 |
|
---|
367 | return rc;
|
---|
368 | }
|
---|
369 |
|
---|
370 |
|
---|
371 | DECLHIDDEN(int) utsGadgetCfgQueryS16Def(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
372 | uint16_t *pi16, uint16_t i16Def)
|
---|
373 | {
|
---|
374 | int rc = VERR_INVALID_PARAMETER;
|
---|
375 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
376 |
|
---|
377 | if ( !pCfgItem
|
---|
378 | || pCfgItem->Val.enmType == UTSGADGETCFGTYPE_INT16)
|
---|
379 | {
|
---|
380 | *pi16 = pCfgItem ? pCfgItem->Val.u.i16 : i16Def;
|
---|
381 | rc = VINF_SUCCESS;
|
---|
382 | }
|
---|
383 |
|
---|
384 | return rc;
|
---|
385 | }
|
---|
386 |
|
---|
387 |
|
---|
388 | DECLHIDDEN(int) utsGadgetCfgQueryS32(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
389 | uint32_t *pi32)
|
---|
390 | {
|
---|
391 | int rc = VERR_NOT_FOUND;
|
---|
392 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
393 |
|
---|
394 | if (pCfgItem)
|
---|
395 | {
|
---|
396 | if (pCfgItem->Val.enmType == UTSGADGETCFGTYPE_INT32)
|
---|
397 | {
|
---|
398 | *pi32 = pCfgItem->Val.u.i32;
|
---|
399 | rc = VINF_SUCCESS;
|
---|
400 | }
|
---|
401 | else
|
---|
402 | rc = VERR_INVALID_PARAMETER;
|
---|
403 | }
|
---|
404 |
|
---|
405 | return rc;
|
---|
406 | }
|
---|
407 |
|
---|
408 |
|
---|
409 | DECLHIDDEN(int) utsGadgetCfgQueryS32Def(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
410 | uint32_t *pi32, uint32_t i32Def)
|
---|
411 | {
|
---|
412 | int rc = VERR_INVALID_PARAMETER;
|
---|
413 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
414 |
|
---|
415 | if ( !pCfgItem
|
---|
416 | || pCfgItem->Val.enmType == UTSGADGETCFGTYPE_INT32)
|
---|
417 | {
|
---|
418 | *pi32 = pCfgItem ? pCfgItem->Val.u.i32 : i32Def;
|
---|
419 | rc = VINF_SUCCESS;
|
---|
420 | }
|
---|
421 |
|
---|
422 | return rc;
|
---|
423 | }
|
---|
424 |
|
---|
425 |
|
---|
426 | DECLHIDDEN(int) utsGadgetCfgQueryS64(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
427 | uint64_t *pi64)
|
---|
428 | {
|
---|
429 | int rc = VERR_NOT_FOUND;
|
---|
430 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
431 |
|
---|
432 | if (pCfgItem)
|
---|
433 | {
|
---|
434 | if (pCfgItem->Val.enmType == UTSGADGETCFGTYPE_INT64)
|
---|
435 | {
|
---|
436 | *pi64 = pCfgItem->Val.u.i64;
|
---|
437 | rc = VINF_SUCCESS;
|
---|
438 | }
|
---|
439 | else
|
---|
440 | rc = VERR_INVALID_PARAMETER;
|
---|
441 | }
|
---|
442 |
|
---|
443 | return rc;
|
---|
444 | }
|
---|
445 |
|
---|
446 |
|
---|
447 | DECLHIDDEN(int) utsGadgetCfgQueryS64Def(PCUTSGADGETCFGITEM paCfg, const char *pszKey,
|
---|
448 | uint64_t *pi64, uint64_t i64Def)
|
---|
449 | {
|
---|
450 | int rc = VERR_INVALID_PARAMETER;
|
---|
451 | PCUTSGADGETCFGITEM pCfgItem = utsGadgetCfgGetItemFromKey(paCfg, pszKey);
|
---|
452 |
|
---|
453 | if ( !pCfgItem
|
---|
454 | || pCfgItem->Val.enmType == UTSGADGETCFGTYPE_INT64)
|
---|
455 | {
|
---|
456 | *pi64 = pCfgItem ? pCfgItem->Val.u.i64 : i64Def;
|
---|
457 | rc = VINF_SUCCESS;
|
---|
458 | }
|
---|
459 |
|
---|
460 | return rc;
|
---|
461 | }
|
---|
462 |
|
---|