VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/AudioTestServiceClient.cpp@ 89480

Last change on this file since 89480 was 89458, checked in by vboxsync, 4 years ago

Audio/ValKit: Simplified code a bit by removing unnecessary parameters. bugref:10008

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.1 KB
Line 
1/* $Id: AudioTestServiceClient.cpp 89458 2021-06-02 09:04:06Z vboxsync $ */
2/** @file
3 * AudioTestServiceClient - Audio Test Service (ATS), Client helpers.
4 *
5 * Note: Only does TCP/IP as transport layer for now.
6 */
7
8/*
9 * Copyright (C) 2021 Oracle Corporation
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 */
19
20
21/*********************************************************************************************************************************
22* Header Files *
23*********************************************************************************************************************************/
24#define LOG_GROUP RTLOGGROUP_DEFAULT
25#include <iprt/crc.h>
26#include <iprt/err.h>
27#include <iprt/mem.h>
28#include <iprt/string.h>
29#include <iprt/tcp.h>
30
31#include "AudioTestService.h"
32#include "AudioTestServiceProtocol.h"
33#include "AudioTestServiceClient.h"
34
35/** @todo Use common defines between server protocol and this client. */
36
37/**
38 * A generic ATS reply, used by the client
39 * to process the incoming packets.
40 */
41typedef struct ATSSRVREPLY
42{
43 char szOp[8];
44 void *pvPayload;
45 size_t cbPayload;
46} ATSSRVREPLY;
47/** Pointer to a generic ATS reply. */
48typedef struct ATSSRVREPLY *PATSSRVREPLY;
49
50
51/**
52 * Initializes an ATS client, internal version.
53 *
54 * @param pClient Client to initialize.
55 */
56static void audioTestSvcClientInit(PATSCLIENT pClient)
57{
58 pClient->cbHdr = 0;
59 pClient->hSock = NIL_RTSOCKET;
60}
61
62/**
63 * Free's an ATS server reply.
64 * @param pReply Reply to free. The pointer is invalid afterwards.
65 */
66static void audioTestSvcClientReplyFree(PATSSRVREPLY pReply)
67{
68 if (!pReply)
69 return;
70
71 if (pReply->pvPayload)
72 {
73 Assert(pReply->cbPayload);
74 RTMemFree(pReply->pvPayload);
75 pReply->pvPayload = NULL;
76 }
77
78 pReply->cbPayload = 0;
79}
80
81/**
82 * Receives a reply from an ATS server.
83 *
84 * @returns VBox status code.
85 * @param pClient Client to receive reply for.
86 * @param pReply Where to store the reply.
87 * @param fNoDataOk If it's okay that the reply is not expected to have any payload.
88 */
89static int audioTestSvcClientRecvReply(PATSCLIENT pClient, PATSSRVREPLY pReply, bool fNoDataOk)
90{
91 int rc;
92
93 ATSPKTHDR Hdr;
94 size_t cbHdr = 0;
95 if (pClient->cbHdr)
96 {
97 memcpy(&Hdr, &pClient->abHdr, sizeof(Hdr));
98 cbHdr = pClient->cbHdr;
99 pClient->cbHdr = 0;
100 rc = VINF_SUCCESS;
101 }
102 else
103 rc = RTTcpRead(pClient->hSock, &Hdr, sizeof(Hdr), &cbHdr);
104
105 /** @todo Use defines for all those numbers below. */
106
107 if (cbHdr != 16)
108 return VERR_NET_PROTOCOL_ERROR;
109
110 if (RT_SUCCESS(rc))
111 {
112 if (Hdr.cb < 16)
113 return VERR_NET_PROTOCOL_ERROR;
114
115 if (Hdr.cb > 1024 * 1024)
116 return VERR_NET_PROTOCOL_ERROR;
117
118 /** @todo Check opcode encoding. */
119
120 if (Hdr.cb > 16)
121 {
122 uint32_t cbPadding;
123 if (Hdr.cb % 16)
124 cbPadding = 16 - (Hdr.cb % 16);
125 else
126 cbPadding = 0;
127
128 pReply->pvPayload = RTMemAlloc(Hdr.cb - 16);
129 pReply->cbPayload = Hdr.cb - 16;
130
131 size_t cbRead = 0;
132 rc = RTTcpRead(pClient->hSock, pReply->pvPayload, RT_MIN(Hdr.cb - 16 + cbPadding, pReply->cbPayload), &cbRead);
133 if (RT_SUCCESS(rc))
134 {
135 if (!cbRead)
136 {
137 memcpy(&pClient->abHdr, &Hdr, sizeof(pClient->abHdr));
138 if (!fNoDataOk)
139 rc = VERR_NET_PROTOCOL_ERROR;
140 }
141 else
142 {
143 while (cbPadding--)
144 {
145 Assert(cbRead);
146 cbRead--;
147 }
148 }
149
150 if (RT_SUCCESS(rc))
151 {
152 /** @todo Check CRC-32. */
153
154 memcpy(pReply->szOp, Hdr.achOpcode, sizeof(pReply->szOp));
155 pReply->cbPayload = cbRead;
156
157 /** @todo Re-allocate pvPayload to not store padding? */
158 }
159 }
160
161 if (RT_FAILURE(rc))
162 audioTestSvcClientReplyFree(pReply);
163 }
164 }
165
166 return rc;
167}
168
169/**
170 * Receives a reply for an ATS server and checks if it is an acknowledge (success) one.
171 *
172 * @returns VBox status code.
173 * @retval VERR_NET_PROTOCOL_ERROR if the reply indicates a failure.
174 * @param pClient Client to receive reply for.
175 */
176static int audioTestSvcClientRecvAck(PATSCLIENT pClient)
177{
178 ATSSRVREPLY Reply;
179 RT_ZERO(Reply);
180
181 int rc = audioTestSvcClientRecvReply(pClient, &Reply, true /* fNoDataOk */);
182 if (RT_SUCCESS(rc))
183 {
184 if (RTStrNCmp(Reply.szOp, "ACK ", 8) != 0) /** @todo Use protocol define. */
185 rc = VERR_NET_PROTOCOL_ERROR;
186
187 audioTestSvcClientReplyFree(&Reply);
188 }
189
190 return rc;
191}
192
193/**
194 * Sends data over the transport to the server.
195 * For now only TCP/IP is implemented.
196 *
197 * @returns VBox status code.
198 * @param pClient Client to send data for.
199 * @param pvData Pointer to data to send.
200 * @param cbData Size (in bytes) of \a pvData to send.
201 */
202static int audioTestSvcClientSend(PATSCLIENT pClient, const void *pvData, size_t cbData)
203{
204 return RTTcpWrite(pClient->hSock, pvData, cbData);
205}
206
207/**
208 * Sends a message plus optional payload to an ATS server.
209 *
210 * @returns VBox status code.
211 * @param pClient Client to send message for.
212 * @param pvHdr Pointer to header data to send.
213 * @param cbHdr Size (in bytes) of \a pvHdr to send.
214 * @param pvPayload Pointer to payload to send. Optional.
215 * @param cbPayload Size (in bytes) of \a pvPayload to send. Set to 0 if no payload needed.
216 */
217static int audioTestSvcClientSendMsg(PATSCLIENT pClient,
218 void *pvHdr, size_t cbHdr, const void *pvPayload, size_t cbPayload)
219{
220 int rc = audioTestSvcClientSend(pClient, pvHdr, cbHdr);
221 if ( RT_SUCCESS(rc)
222 && cbPayload)
223 {
224 rc = audioTestSvcClientSend(pClient, (uint8_t *)pvPayload, cbPayload);
225 }
226
227 return rc;
228}
229
230/**
231 * Initializes a client request header.
232 *
233 * @returns VBox status code.
234 * @param pReqHdr Request header to initialize.
235 * @param cbReq Size (in bytes) the request will have (does *not* include payload).
236 * @param pszOp Operation to perform with the request.
237 * @param cbPayload Size (in bytes) of payload that will follow the header. Optional and can be 0.
238 */
239DECLINLINE (void) audioTestSvcClientReqHdrInit(PATSPKTHDR pReqHdr, size_t cbReq, const char *pszOp, size_t cbPayload)
240{
241 AssertReturnVoid(strlen(pszOp) >= 2);
242
243 /** @todo Validate opcode. */
244
245 memcpy(pReqHdr->achOpcode, pszOp, sizeof(pReqHdr->achOpcode));
246 pReqHdr->uCrc32 = 0; /** @todo Do CRC-32 calculation. */
247 pReqHdr->cb = (uint32_t)cbReq + (uint32_t)cbPayload;
248}
249
250/**
251 * Sends a greeting command (handshake) to an ATS server.
252 *
253 * @returns VBox status code.
254 * @param pClient Client to send command for.
255 */
256static int audioTestSvcClientDoGreet(PATSCLIENT pClient)
257{
258 ATSPKTREQHOWDY Req;
259 Req.uVersion = ATS_PROTOCOL_VS;
260 audioTestSvcClientReqHdrInit(&Req.Hdr, sizeof(Req), ATSPKT_OPCODE_HOWDY, 0);
261 int rc = audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req), NULL, 0);
262 if (RT_SUCCESS(rc))
263 rc = audioTestSvcClientRecvAck(pClient);
264 return rc;
265}
266
267/**
268 * Sends a disconnect command to an ATS server.
269 *
270 * @returns VBox status code.
271 * @param pClient Client to send command for.
272 */
273static int audioTestSvcClientDoBye(PATSCLIENT pClient)
274{
275 ATSPKTHDR Hdr;
276 audioTestSvcClientReqHdrInit(&Hdr, sizeof(Hdr), ATSPKT_OPCODE_BYE, 0);
277 int rc = audioTestSvcClientSendMsg(pClient, &Hdr, sizeof(Hdr), NULL, 0);
278 if (RT_SUCCESS(rc))
279 rc = audioTestSvcClientRecvAck(pClient);
280
281 return rc;
282}
283
284/**
285 * Connects to an ATS server.
286 *
287 * @returns VBox status code.
288 * @param pClient Client to connect.
289 * @param pszAddr Address to connect to. If NULL, 127.0.0.1 (localhost) will be used.
290 * @param uPort Port to connect. If set to 0, port 6052 (ATS_DEFAULT_PORT) will be used.
291 */
292int AudioTestSvcClientConnect(PATSCLIENT pClient, const char *pszAddr, uint32_t uPort)
293{
294 audioTestSvcClientInit(pClient);
295
296 /* For simplicity we always run on the same port, localhost only. */
297 int rc = RTTcpClientConnect(pszAddr ? pszAddr : "127.0.0.1", uPort == 0 ? ATS_DEFAULT_PORT : uPort, &pClient->hSock);
298 if (RT_SUCCESS(rc))
299 {
300 rc = audioTestSvcClientDoGreet(pClient);
301 }
302
303 return rc;
304}
305
306/**
307 * Tells the server to begin a new test set.
308 *
309 * @returns VBox status code.
310 * @param pClient Client to issue command for.
311 * @param pszTag Tag to use for the test set to begin.
312 */
313int AudioTestSvcClientTestSetBegin(PATSCLIENT pClient, const char *pszTag)
314{
315 ATSPKTREQTSETBEG Req;
316
317 int rc = RTStrCopy(Req.szTag, sizeof(Req.szTag), pszTag);
318 AssertRCReturn(rc, rc);
319
320 audioTestSvcClientReqHdrInit(&Req.Hdr, sizeof(Req), ATSPKT_OPCODE_TESTSET_BEGIN, 0);
321
322 rc = audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req), NULL, 0);
323 if (RT_SUCCESS(rc))
324 rc = audioTestSvcClientRecvAck(pClient);
325
326 return rc;
327}
328
329/**
330 * Tells the server to end a runing test set.
331 *
332 * @returns VBox status code.
333 * @param pClient Client to issue command for.
334 * @param pszTag Tag of test set to end.
335 */
336int AudioTestSvcClientTestSetEnd(PATSCLIENT pClient, const char *pszTag)
337{
338 ATSPKTREQTSETEND Req;
339
340 int rc = RTStrCopy(Req.szTag, sizeof(Req.szTag), pszTag);
341 AssertRCReturn(rc, rc);
342
343 audioTestSvcClientReqHdrInit(&Req.Hdr, sizeof(Req), ATSPKT_OPCODE_TESTSET_END, 0);
344
345 rc = audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req), NULL, 0);
346 if (RT_SUCCESS(rc))
347 rc = audioTestSvcClientRecvAck(pClient);
348
349 return rc;
350}
351
352/**
353 * Tells the server to play a (test) tone.
354 *
355 * @returns VBox status code.
356 * @param pClient Client to issue command for.
357 * @param pToneParms Tone parameters to use.
358 * @note How (and if) the server plays a tone depends on the actual implementation side.
359 */
360int AudioTestSvcClientTonePlay(PATSCLIENT pClient, PAUDIOTESTTONEPARMS pToneParms)
361{
362 ATSPKTREQTONEPLAY Req;
363
364 memcpy(&Req.ToneParms, pToneParms, sizeof(AUDIOTESTTONEPARMS));
365
366 audioTestSvcClientReqHdrInit(&Req.Hdr, sizeof(Req), ATSPKT_OPCODE_TONE_PLAY, 0);
367
368 int rc = audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req), NULL, 0);
369 if (RT_SUCCESS(rc))
370 rc = audioTestSvcClientRecvAck(pClient);
371
372 return rc;
373}
374
375/**
376 * Tells the server to record a (test) tone.
377 *
378 * @returns VBox status code.
379 * @param pClient Client to issue command for.
380 * @param pToneParms Tone parameters to use.
381 * @note How (and if) the server plays a tone depends on the actual implementation side.
382 */
383int AudioTestSvcClientToneRecord(PATSCLIENT pClient, PAUDIOTESTTONEPARMS pToneParms)
384{
385 ATSPKTREQTONEREC Req;
386
387 memcpy(&Req.ToneParms, pToneParms, sizeof(AUDIOTESTTONEPARMS));
388
389 audioTestSvcClientReqHdrInit(&Req.Hdr, sizeof(Req), ATSPKT_OPCODE_TONE_RECORD, 0);
390
391 int rc = audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req), NULL, 0);
392 if (RT_SUCCESS(rc))
393 rc = audioTestSvcClientRecvAck(pClient);
394
395 return rc;
396}
397
398/**
399 * Disconnects from an ATS server.
400 *
401 * @returns VBox status code.
402 * @param pClient Client to disconnect.
403 */
404int AudioTestSvcClientClose(PATSCLIENT pClient)
405{
406 int rc = audioTestSvcClientDoBye(pClient);
407 if (RT_SUCCESS(rc))
408 rc = RTTcpClientClose(pClient->hSock);
409
410 return rc;
411}
412
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette