1 | /* $Id: client.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Base class for a host-guest service.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. 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 | #include <VBox/log.h>
|
---|
19 | #include <VBox/hgcmsvc.h>
|
---|
20 |
|
---|
21 | #include <iprt/assert.h>
|
---|
22 | #include <iprt/alloc.h>
|
---|
23 | #include <iprt/cpp/utils.h>
|
---|
24 |
|
---|
25 | #include <VBox/HostServices/Service.h>
|
---|
26 |
|
---|
27 | using namespace HGCM;
|
---|
28 |
|
---|
29 | Client::Client(uint32_t idClient)
|
---|
30 | : m_idClient(idClient)
|
---|
31 | , m_fDeferred(false)
|
---|
32 | {
|
---|
33 | RT_ZERO(m_Deferred);
|
---|
34 | RT_ZERO(m_SvcCtx);
|
---|
35 | }
|
---|
36 |
|
---|
37 | Client::~Client(void)
|
---|
38 | {
|
---|
39 |
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Completes a guest call by returning the control back to the guest side,
|
---|
44 | * together with a status code, internal version.
|
---|
45 | *
|
---|
46 | * @returns IPRT status code.
|
---|
47 | * @param hHandle Call handle to complete guest call for.
|
---|
48 | * @param rcOp Return code to return to the guest side.
|
---|
49 | */
|
---|
50 | int Client::completeInternal(VBOXHGCMCALLHANDLE hHandle, int rcOp) RT_NOEXCEPT
|
---|
51 | {
|
---|
52 | LogFlowThisFunc(("idClient=%RU32\n", m_idClient));
|
---|
53 |
|
---|
54 | if ( m_SvcCtx.pHelpers
|
---|
55 | && m_SvcCtx.pHelpers->pfnCallComplete)
|
---|
56 | {
|
---|
57 | m_SvcCtx.pHelpers->pfnCallComplete(hHandle, rcOp);
|
---|
58 |
|
---|
59 | reset();
|
---|
60 | return VINF_SUCCESS;
|
---|
61 | }
|
---|
62 |
|
---|
63 | return VERR_NOT_AVAILABLE;
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Resets the client's internal state.
|
---|
68 | */
|
---|
69 | void Client::reset(void) RT_NOEXCEPT
|
---|
70 | {
|
---|
71 | m_fDeferred = false;
|
---|
72 |
|
---|
73 | RT_ZERO(m_Deferred);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Completes a guest call by returning the control back to the guest side,
|
---|
78 | * together with a status code.
|
---|
79 | *
|
---|
80 | * @returns IPRT status code.
|
---|
81 | * @param hHandle Call handle to complete guest call for.
|
---|
82 | * @param rcOp Return code to return to the guest side.
|
---|
83 | */
|
---|
84 | int Client::Complete(VBOXHGCMCALLHANDLE hHandle, int rcOp /* = VINF_SUCCESS */) RT_NOEXCEPT
|
---|
85 | {
|
---|
86 | return completeInternal(hHandle, rcOp);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Completes a deferred guest call by returning the control back to the guest side,
|
---|
91 | * together with a status code.
|
---|
92 | *
|
---|
93 | * @returns IPRT status code. VERR_INVALID_STATE if the client is not in deferred mode.
|
---|
94 | * @param rcOp Return code to return to the guest side.
|
---|
95 | */
|
---|
96 | int Client::CompleteDeferred(int rcOp) RT_NOEXCEPT
|
---|
97 | {
|
---|
98 | if (m_fDeferred)
|
---|
99 | {
|
---|
100 | Assert(m_Deferred.hHandle != NULL);
|
---|
101 |
|
---|
102 | int rc = completeInternal(m_Deferred.hHandle, rcOp);
|
---|
103 | if (RT_SUCCESS(rc))
|
---|
104 | m_fDeferred = false;
|
---|
105 |
|
---|
106 | return rc;
|
---|
107 | }
|
---|
108 |
|
---|
109 | AssertMsg(m_fDeferred, ("Client %RU32 is not in deferred mode\n", m_idClient));
|
---|
110 | return VERR_INVALID_STATE;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Returns the HGCM call handle of the client.
|
---|
115 | *
|
---|
116 | * @returns HGCM handle.
|
---|
117 | */
|
---|
118 | VBOXHGCMCALLHANDLE Client::GetHandle(void) const RT_NOEXCEPT
|
---|
119 | {
|
---|
120 | return m_Deferred.hHandle;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Returns the HGCM call handle of the client.
|
---|
125 | *
|
---|
126 | * @returns HGCM handle.
|
---|
127 | */
|
---|
128 | uint32_t Client::GetMsgType(void) const RT_NOEXCEPT
|
---|
129 | {
|
---|
130 | return m_Deferred.uType;
|
---|
131 | }
|
---|
132 |
|
---|
133 | uint32_t Client::GetMsgParamCount(void) const RT_NOEXCEPT
|
---|
134 | {
|
---|
135 | return m_Deferred.cParms;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Returns the client's (HGCM) ID.
|
---|
140 | *
|
---|
141 | * @returns The client's (HGCM) ID.
|
---|
142 | */
|
---|
143 | uint32_t Client::GetClientID(void) const RT_NOEXCEPT
|
---|
144 | {
|
---|
145 | return m_idClient;
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Returns whether the client currently is in deferred mode or not.
|
---|
150 | *
|
---|
151 | * @returns \c True if in deferred mode, \c False if not.
|
---|
152 | */
|
---|
153 | bool Client::IsDeferred(void) const RT_NOEXCEPT
|
---|
154 | {
|
---|
155 | return m_fDeferred;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Set the client's status to deferred, meaning that it does not return to the caller
|
---|
160 | * until CompleteDeferred() has been called.
|
---|
161 | *
|
---|
162 | * @returns VBox status code.
|
---|
163 | * @param hHandle Call handle to save.
|
---|
164 | * @param u32Function Function number to save.
|
---|
165 | * @param cParms Number of HGCM parameters to save.
|
---|
166 | * @param paParms HGCM parameters to save.
|
---|
167 | */
|
---|
168 | void Client::SetDeferred(VBOXHGCMCALLHANDLE hHandle, uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]) RT_NOEXCEPT
|
---|
169 | {
|
---|
170 | LogFlowThisFunc(("uClient=%RU32\n", m_idClient));
|
---|
171 |
|
---|
172 | m_fDeferred = true;
|
---|
173 |
|
---|
174 | m_Deferred.hHandle = hHandle;
|
---|
175 | m_Deferred.uType = u32Function;
|
---|
176 | m_Deferred.cParms = cParms;
|
---|
177 | m_Deferred.paParms = paParms;
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Sets the HGCM service context.
|
---|
182 | *
|
---|
183 | * @param SvcCtx Service context to set.
|
---|
184 | */
|
---|
185 | void Client::SetSvcContext(const VBOXHGCMSVCTX &SvcCtx) RT_NOEXCEPT
|
---|
186 | {
|
---|
187 | m_SvcCtx = SvcCtx;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Sets the deferred parameters to a specific message type and
|
---|
192 | * required parameters. That way the client can re-request that message with
|
---|
193 | * the right amount of parameters from the service.
|
---|
194 | *
|
---|
195 | * @returns IPRT status code.
|
---|
196 | * @param uMsg Message type (number) to set.
|
---|
197 | * @param cParms Number of parameters the message needs.
|
---|
198 | */
|
---|
199 | int Client::SetDeferredMsgInfo(uint32_t uMsg, uint32_t cParms) RT_NOEXCEPT
|
---|
200 | {
|
---|
201 | if (m_fDeferred)
|
---|
202 | {
|
---|
203 | if (m_Deferred.cParms < 2)
|
---|
204 | return VERR_INVALID_PARAMETER;
|
---|
205 |
|
---|
206 | AssertPtrReturn(m_Deferred.paParms, VERR_BUFFER_OVERFLOW);
|
---|
207 |
|
---|
208 | HGCMSvcSetU32(&m_Deferred.paParms[0], uMsg);
|
---|
209 | HGCMSvcSetU32(&m_Deferred.paParms[1], cParms);
|
---|
210 |
|
---|
211 | return VINF_SUCCESS;
|
---|
212 | }
|
---|
213 |
|
---|
214 | AssertFailed();
|
---|
215 | return VERR_INVALID_STATE;
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Sets the deferred parameters to a specific message type and
|
---|
220 | * required parameters. That way the client can re-request that message with
|
---|
221 | * the right amount of parameters from the service.
|
---|
222 | *
|
---|
223 | * @returns IPRT status code.
|
---|
224 | * @param pMessage Message to get message type and required parameters from.
|
---|
225 | */
|
---|
226 | int Client::SetDeferredMsgInfo(const Message *pMessage) RT_NOEXCEPT
|
---|
227 | {
|
---|
228 | AssertPtrReturn(pMessage, VERR_INVALID_POINTER);
|
---|
229 | return SetDeferredMsgInfo(pMessage->GetType(), pMessage->GetParamCount());
|
---|
230 | }
|
---|
231 |
|
---|