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