VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/utils/usb/UsbTestServiceGadgetHost.cpp@ 60801

Last change on this file since 60801 was 60517, checked in by vboxsync, 9 years ago

ValidationKit/usb: Updates for UsbTestService

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
1/* $Id: UsbTestServiceGadgetHost.cpp 60517 2016-04-15 12:20:51Z vboxsync $ */
2/** @file
3 * UsbTestServ - Remote USB test configuration and execution server, USB gadget host API.
4 */
5
6/*
7 * Copyright (C) 2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22
23#include <iprt/asm.h>
24#include <iprt/cdefs.h>
25#include <iprt/ctype.h>
26#include <iprt/mem.h>
27#include <iprt/string.h>
28#include <iprt/types.h>
29
30#include "UsbTestServiceGadget.h"
31#include "UsbTestServiceGadgetHostInternal.h"
32
33/*********************************************************************************************************************************
34* Constants And Macros, Structures and Typedefs *
35*********************************************************************************************************************************/
36
37/**
38 * Internal UTS gadget host instance data.
39 */
40typedef struct UTSGADGETHOSTINT
41{
42 /** Reference counter. */
43 volatile uint32_t cRefs;
44 /** Pointer to the gadget host callback table. */
45 PCUTSGADGETHOSTIF pHstIf;
46 /** Interface specific instance data - variable in size. */
47 uint8_t abIfInst[1];
48} UTSGADGETHOSTINT;
49/** Pointer to the internal gadget host instance data. */
50typedef UTSGADGETHOSTINT *PUTSGADGETHOSTINT;
51
52
53/*********************************************************************************************************************************
54* Global variables *
55*********************************************************************************************************************************/
56
57/** Known gadget host interfaces. */
58static const PCUTSGADGETHOSTIF g_apUtsGadgetHostIf[] =
59{
60 &g_UtsGadgetHostIfUsbIp,
61};
62
63/*********************************************************************************************************************************
64* Internal Functions *
65*********************************************************************************************************************************/
66
67
68/**
69 * Destroys a gadget host instance.
70 *
71 * @returns nothing.
72 * @param pThis The gadget host instance.
73 */
74static void utsGadgetHostDestroy(PUTSGADGETHOSTINT pThis)
75{
76 /** @todo: Remove all gadgets. */
77 pThis->pHstIf->pfnTerm((PUTSGADGETHOSTTYPEINT)&pThis->abIfInst[0]);
78 RTMemFree(pThis);
79}
80
81
82DECLHIDDEN(int) utsGadgetHostCreate(UTSGADGETHOSTTYPE enmType, PCUTSGADGETCFGITEM paCfg,
83 PUTSGADGETHOST phGadgetHost)
84{
85 int rc = VINF_SUCCESS;
86 PCUTSGADGETHOSTIF pIf = NULL;
87
88 /* Get the interface. */
89 for (unsigned i = 0; i < RT_ELEMENTS(g_apUtsGadgetHostIf); i++)
90 {
91 if (g_apUtsGadgetHostIf[i]->enmType == enmType)
92 {
93 pIf = g_apUtsGadgetHostIf[i];
94 break;
95 }
96 }
97
98 if (RT_LIKELY(pIf))
99 {
100 PUTSGADGETHOSTINT pThis = (PUTSGADGETHOSTINT)RTMemAllocZ(RT_OFFSETOF(UTSGADGETHOSTINT, abIfInst[pIf->cbIf]));
101 if (RT_LIKELY(pThis))
102 {
103 pThis->cRefs = 1;
104 pThis->pHstIf = pIf;
105 rc = pIf->pfnInit((PUTSGADGETHOSTTYPEINT)&pThis->abIfInst[0], paCfg);
106 if (RT_SUCCESS(rc))
107 *phGadgetHost = pThis;
108 else
109 RTMemFree(pThis);
110 }
111 else
112 rc = VERR_NO_MEMORY;
113 }
114 else
115 rc = VERR_INVALID_PARAMETER;
116
117 return rc;
118}
119
120
121DECLHIDDEN(uint32_t) utsGadgetHostRetain(UTSGADGETHOST hGadgetHost)
122{
123 PUTSGADGETHOSTINT pThis = hGadgetHost;
124
125 AssertPtrReturn(pThis, 0);
126
127 return ASMAtomicIncU32(&pThis->cRefs);
128}
129
130
131DECLHIDDEN(uint32_t) utsGadgetHostRelease(UTSGADGETHOST hGadgetHost)
132{
133 PUTSGADGETHOSTINT pThis = hGadgetHost;
134
135 AssertPtrReturn(pThis, 0);
136
137 uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
138 if (!cRefs)
139 utsGadgetHostDestroy(pThis);
140
141 return cRefs;
142}
143
144
145DECLHIDDEN(int) utsGadgetHostGadgetConnect(UTSGADGETHOST hGadgetHost, UTSGADGET hGadget)
146{
147 PUTSGADGETHOSTINT pThis = hGadgetHost;
148
149 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
150 return pThis->pHstIf->pfnGadgetConnect((PUTSGADGETHOSTTYPEINT)&pThis->abIfInst[0], hGadget);
151}
152
153
154DECLHIDDEN(int) utsGadgetHostGadgetDisconnect(UTSGADGETHOST hGadgetHost, UTSGADGET hGadget)
155{
156 PUTSGADGETHOSTINT pThis = hGadgetHost;
157
158 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
159 return pThis->pHstIf->pfnGadgetDisconnect((PUTSGADGETHOSTTYPEINT)&pThis->abIfInst[0], hGadget);
160}
161
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