VirtualBox

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

Last change on this file since 89805 was 89802, checked in by vboxsync, 4 years ago

Audio/ValKit: Added support for CRC32 checksum calculation; now receiving data also works with bigger chunks. bugref:10008

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.1 KB
Line 
1/* $Id: AudioTestServiceClient.cpp 89802 2021-06-21 06:19:21Z 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 <VBox/log.h>
26
27#include <iprt/crc.h>
28#include <iprt/err.h>
29#include <iprt/file.h>
30#include <iprt/mem.h>
31#include <iprt/string.h>
32#include <iprt/tcp.h>
33
34#include "AudioTestService.h"
35#include "AudioTestServiceProtocol.h"
36#include "AudioTestServiceClient.h"
37
38/** @todo Use common defines between server protocol and this client. */
39
40/**
41 * A generic ATS reply, used by the client
42 * to process the incoming packets.
43 */
44typedef struct ATSSRVREPLY
45{
46 char szOp[ATSPKT_OPCODE_MAX_LEN];
47 void *pvPayload;
48 size_t cbPayload;
49} ATSSRVREPLY;
50/** Pointer to a generic ATS reply. */
51typedef struct ATSSRVREPLY *PATSSRVREPLY;
52
53
54/**
55 * Initializes an ATS client, internal version.
56 *
57 * @param pClient Client to initialize.
58 */
59static void audioTestSvcClientInit(PATSCLIENT pClient)
60{
61 pClient->cbHdr = 0;
62 pClient->hSock = NIL_RTSOCKET;
63}
64
65/**
66 * Destroys an ATS server reply.
67 *
68 * @param pReply Reply to destroy.
69 */
70static void audioTestSvcClientReplyDestroy(PATSSRVREPLY pReply)
71{
72 if (!pReply)
73 return;
74
75 if (pReply->pvPayload)
76 {
77 Assert(pReply->cbPayload);
78 RTMemFree(pReply->pvPayload);
79 pReply->pvPayload = NULL;
80 }
81
82 pReply->cbPayload = 0;
83}
84
85/**
86 * Receives a reply from an ATS server.
87 *
88 * @returns VBox status code.
89 * @param pClient Client to receive reply for.
90 * @param pReply Where to store the reply.
91 * The reply must be destroyed with audioTestSvcClientReplyDestroy() then.
92 * @param fNoDataOk If it's okay that the reply is not expected to have any payload.
93 */
94static int audioTestSvcClientRecvReply(PATSCLIENT pClient, PATSSRVREPLY pReply, bool fNoDataOk)
95{
96 int rc;
97
98 LogFlowFuncEnter();
99
100 ATSPKTHDR Hdr;
101 RT_ZERO(Hdr);
102 size_t cbHdr = 0;
103 if (pClient->cbHdr) /* Header already received? */
104 {
105 memcpy(&Hdr, &pClient->abHdr, sizeof(Hdr));
106 cbHdr = pClient->cbHdr;
107 pClient->cbHdr = 0;
108 rc = VINF_SUCCESS;
109 }
110 else /* No, receive header. */
111 {
112 rc = RTTcpRead(pClient->hSock, &Hdr, sizeof(Hdr), &cbHdr);
113 }
114
115 LogFlowFunc(("rc=%Rrc, hdr=%zu, hdr.cb=%zu, %.8s -> %.*Rhxd\n", rc, cbHdr, Hdr.cb, Hdr.achOpcode, RT_MIN(cbHdr, sizeof(ATSPKTHDR)), &Hdr));
116
117 if (RT_FAILURE(rc))
118 return rc;
119
120 if (cbHdr != sizeof(Hdr)) /* Check for bad packet sizes. */
121 {
122 AssertMsgFailed(("Packet is invalid (%RU32 bytes), must be at least %zu bytes\n", cbHdr, sizeof(Hdr)));
123 return VERR_NET_INCOMPLETE_TX_PACKET;
124 }
125
126 if (Hdr.cb < sizeof(ATSPKTHDR))
127 {
128 AssertMsgFailed(("Packet is too small (%RU32 bytes), must be at least %zu bytes\n", Hdr.cb, sizeof(Hdr)));
129 return VERR_NET_INCOMPLETE_TX_PACKET;
130 }
131
132 if (Hdr.cb > ATSPKT_MAX_SIZE)
133 {
134 AssertMsgFailed(("Packet is %RU32 bytes, only %zu bytes allowed\n", Hdr.cb, ATSPKT_MAX_SIZE));
135 return VERR_BUFFER_OVERFLOW;
136 }
137
138 if (Hdr.cb > ATSPKT_MAX_SIZE)
139 {
140 AssertMsgFailed(("Packet is %RU32 bytes, only %zu bytes allowed\n", Hdr.cb, ATSPKT_MAX_SIZE));
141 return VERR_BUFFER_OVERFLOW;
142 }
143
144 pReply->cbPayload = Hdr.cb - sizeof(ATSPKTHDR);
145 Log3Func(("cbHdr=%zu, Hdr.szOp=%s, Hdr.cb=%RU32 -> %zu bytes payload\n", cbHdr, Hdr.achOpcode, Hdr.cb, pReply->cbPayload));
146 if (pReply->cbPayload)
147 {
148 pReply->pvPayload = RTMemAlloc(pReply->cbPayload);
149 if (pReply->pvPayload)
150 {
151 uint32_t cbPayloadRead = 0;
152 while (cbPayloadRead < pReply->cbPayload)
153 {
154 size_t cbRead = 0;
155 rc = RTTcpRead(pClient->hSock, (uint8_t *)pReply->pvPayload + cbPayloadRead, pReply->cbPayload - cbPayloadRead, &cbRead);
156 if (RT_SUCCESS(rc))
157 {
158 if (!cbRead)
159 {
160 memcpy(&pClient->abHdr, &Hdr, sizeof(pClient->abHdr));
161 if (!fNoDataOk)
162 rc = VERR_NET_PROTOCOL_ERROR;
163 }
164
165 cbPayloadRead += cbRead;
166 Assert(cbPayloadRead <= pReply->cbPayload);
167 }
168 }
169
170 if (RT_SUCCESS(rc))
171 {
172 Assert(cbPayloadRead == pReply->cbPayload);
173 if (Hdr.uCrc32) /* Checksum given? */
174 {
175 uint32_t uMyCrc32 = RTCrc32Start();
176 uMyCrc32 = RTCrc32Process(uMyCrc32, Hdr.achOpcode, sizeof(Hdr.achOpcode));
177 uMyCrc32 = RTCrc32Process(uMyCrc32, pReply->pvPayload, pReply->cbPayload);
178
179 if (Hdr.uCrc32 != RTCrc32Finish(uMyCrc32))
180 AssertFailedStmt(rc = VERR_TAR_CHKSUM_MISMATCH /** @todo Fudge! */);
181
182 if (RT_SUCCESS(rc))
183 {
184 /* Make sure to align the payload data (if not done by the sender already). */
185 size_t const cbRestAlignment = RT_ALIGN_Z(pReply->cbPayload, ATSPKT_ALIGNMENT) - pReply->cbPayload;
186 if (cbRestAlignment)
187 {
188 uint8_t abAlignment[ATSPKT_ALIGNMENT];
189 rc = RTTcpRead(pClient->hSock, abAlignment, RT_MIN(cbRestAlignment, sizeof(abAlignment)), NULL);
190 }
191 }
192 }
193 }
194
195 if (RT_FAILURE(rc))
196 audioTestSvcClientReplyDestroy(pReply);
197 }
198 else
199 rc = VERR_NO_MEMORY;
200 }
201
202 if (RT_SUCCESS(rc))
203 memcpy(pReply->szOp, Hdr.achOpcode, sizeof(pReply->szOp));
204
205 LogFlowFuncLeaveRC(rc);
206 return rc;
207}
208
209/**
210 * Receives a reply for an ATS server and checks if it is an acknowledge (success) one.
211 *
212 * @returns VBox status code.
213 * @retval VERR_NET_PROTOCOL_ERROR if the reply indicates a failure.
214 * @param pClient Client to receive reply for.
215 */
216static int audioTestSvcClientRecvAck(PATSCLIENT pClient)
217{
218 ATSSRVREPLY Reply;
219 RT_ZERO(Reply);
220
221 int rc = audioTestSvcClientRecvReply(pClient, &Reply, true /* fNoDataOk */);
222 if (RT_SUCCESS(rc))
223 {
224 if (RTStrNCmp(Reply.szOp, "ACK ", ATSPKT_OPCODE_MAX_LEN) != 0)
225 rc = VERR_NET_PROTOCOL_ERROR;
226
227 audioTestSvcClientReplyDestroy(&Reply);
228 }
229
230 return rc;
231}
232
233/**
234 * Sends data over the transport to the server.
235 * For now only TCP/IP is implemented.
236 *
237 * @returns VBox status code.
238 * @param pClient Client to send data for.
239 * @param pvData Pointer to data to send.
240 * @param cbData Size (in bytes) of \a pvData to send.
241 */
242static int audioTestSvcClientSend(PATSCLIENT pClient, const void *pvData, size_t cbData)
243{
244 return RTTcpWrite(pClient->hSock, pvData, cbData);
245}
246
247/**
248 * Sends a message plus optional payload to an ATS server.
249 *
250 * @returns VBox status code.
251 * @param pClient Client to send message for.
252 * @param pvHdr Pointer to header data to send.
253 * @param cbHdr Size (in bytes) of \a pvHdr to send.
254 * @param pvPayload Pointer to payload to send. Optional.
255 * @param cbPayload Size (in bytes) of \a pvPayload to send. Set to 0 if no payload needed.
256 */
257static int audioTestSvcClientSendMsg(PATSCLIENT pClient,
258 void *pvHdr, size_t cbHdr, const void *pvPayload, size_t cbPayload)
259{
260 int rc = audioTestSvcClientSend(pClient, pvHdr, cbHdr);
261 if ( RT_SUCCESS(rc)
262 && cbPayload)
263 {
264 rc = audioTestSvcClientSend(pClient, (uint8_t *)pvPayload, cbPayload);
265 }
266
267 return rc;
268}
269
270/**
271 * Initializes a client request header.
272 *
273 * @returns VBox status code.
274 * @param pReqHdr Request header to initialize.
275 * @param cbReq Size (in bytes) the request will have (does *not* include payload).
276 * @param pszOp Operation to perform with the request.
277 * @param cbPayload Size (in bytes) of payload that will follow the header. Optional and can be 0.
278 */
279DECLINLINE (void) audioTestSvcClientReqHdrInit(PATSPKTHDR pReqHdr, size_t cbReq, const char *pszOp, size_t cbPayload)
280{
281 AssertReturnVoid(strlen(pszOp) >= 2);
282 AssertReturnVoid(strlen(pszOp) <= ATSPKT_OPCODE_MAX_LEN);
283
284 /** @todo Validate opcode. */
285
286 RT_BZERO(pReqHdr, sizeof(ATSPKTHDR));
287
288 memcpy(pReqHdr->achOpcode, pszOp, strlen(pszOp));
289 pReqHdr->uCrc32 = 0; /** @todo Do CRC-32 calculation. */
290 pReqHdr->cb = (uint32_t)cbReq + (uint32_t)cbPayload;
291
292 Assert(pReqHdr->cb <= ATSPKT_MAX_SIZE);
293}
294
295/**
296 * Sends an acknowledege response back to the server.
297 *
298 * @returns VBox status code.
299 * @param pClient Client to send command for.
300 */
301static int audioTestSvcClientSendAck(PATSCLIENT pClient)
302{
303 ATSPKTHDR Req;
304 audioTestSvcClientReqHdrInit(&Req, sizeof(Req), "ACK ", 0);
305
306 return audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req), NULL, 0);
307}
308
309/**
310 * Sends a greeting command (handshake) to an ATS server.
311 *
312 * @returns VBox status code.
313 * @param pClient Client to send command for.
314 */
315static int audioTestSvcClientDoGreet(PATSCLIENT pClient)
316{
317 ATSPKTREQHOWDY Req;
318 Req.uVersion = ATS_PROTOCOL_VS;
319 audioTestSvcClientReqHdrInit(&Req.Hdr, sizeof(Req), ATSPKT_OPCODE_HOWDY, 0);
320 int rc = audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req), NULL, 0);
321 if (RT_SUCCESS(rc))
322 rc = audioTestSvcClientRecvAck(pClient);
323 return rc;
324}
325
326/**
327 * Sends a disconnect command to an ATS server.
328 *
329 * @returns VBox status code.
330 * @param pClient Client to send command for.
331 */
332static int audioTestSvcClientDoBye(PATSCLIENT pClient)
333{
334 ATSPKTHDR Hdr;
335 audioTestSvcClientReqHdrInit(&Hdr, sizeof(Hdr), ATSPKT_OPCODE_BYE, 0);
336 int rc = audioTestSvcClientSendMsg(pClient, &Hdr, sizeof(Hdr), NULL, 0);
337 if (RT_SUCCESS(rc))
338 rc = audioTestSvcClientRecvAck(pClient);
339
340 return rc;
341}
342
343/**
344 * Connects to an ATS server.
345 *
346 * @returns VBox status code.
347 * @param pClient Client to connect.
348 * @param pszAddr Address to connect to. If NULL, 127.0.0.1 (localhost) will be used.
349 * @param uPort Port to connect. If set to 0, port 6052 (ATS_DEFAULT_PORT) will be used.
350 */
351int AudioTestSvcClientConnect(PATSCLIENT pClient, const char *pszAddr, uint32_t uPort)
352{
353 audioTestSvcClientInit(pClient);
354
355 int rc = RTTcpClientConnect(pszAddr ? pszAddr : ATS_TCP_HOST_DEFAULT_ADDR_STR, uPort == 0 ? ATS_TCP_HOST_DEFAULT_PORT : uPort, &pClient->hSock);
356 if (RT_SUCCESS(rc))
357 {
358 rc = audioTestSvcClientDoGreet(pClient);
359 }
360
361 return rc;
362}
363
364/**
365 * Tells the server to begin a new test set.
366 *
367 * @returns VBox status code.
368 * @param pClient Client to issue command for.
369 * @param pszTag Tag to use for the test set to begin.
370 */
371int AudioTestSvcClientTestSetBegin(PATSCLIENT pClient, const char *pszTag)
372{
373 ATSPKTREQTSETBEG Req;
374
375 int rc = RTStrCopy(Req.szTag, sizeof(Req.szTag), pszTag);
376 AssertRCReturn(rc, rc);
377
378 audioTestSvcClientReqHdrInit(&Req.Hdr, sizeof(Req), ATSPKT_OPCODE_TESTSET_BEGIN, 0);
379
380 rc = audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req), NULL, 0);
381 if (RT_SUCCESS(rc))
382 rc = audioTestSvcClientRecvAck(pClient);
383
384 return rc;
385}
386
387/**
388 * Tells the server to end a runing test set.
389 *
390 * @returns VBox status code.
391 * @param pClient Client to issue command for.
392 * @param pszTag Tag of test set to end.
393 */
394int AudioTestSvcClientTestSetEnd(PATSCLIENT pClient, const char *pszTag)
395{
396 ATSPKTREQTSETEND Req;
397
398 int rc = RTStrCopy(Req.szTag, sizeof(Req.szTag), pszTag);
399 AssertRCReturn(rc, rc);
400
401 audioTestSvcClientReqHdrInit(&Req.Hdr, sizeof(Req), ATSPKT_OPCODE_TESTSET_END, 0);
402
403 rc = audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req), NULL, 0);
404 if (RT_SUCCESS(rc))
405 rc = audioTestSvcClientRecvAck(pClient);
406
407 return rc;
408}
409
410/**
411 * Tells the server to play a (test) tone.
412 *
413 * @returns VBox status code.
414 * @param pClient Client to issue command for.
415 * @param pToneParms Tone parameters to use.
416 * @note How (and if) the server plays a tone depends on the actual implementation side.
417 */
418int AudioTestSvcClientTonePlay(PATSCLIENT pClient, PAUDIOTESTTONEPARMS pToneParms)
419{
420 ATSPKTREQTONEPLAY Req;
421
422 memcpy(&Req.ToneParms, pToneParms, sizeof(AUDIOTESTTONEPARMS));
423
424 audioTestSvcClientReqHdrInit(&Req.Hdr, sizeof(Req), ATSPKT_OPCODE_TONE_PLAY, 0);
425
426 int rc = audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req), NULL, 0);
427 if (RT_SUCCESS(rc))
428 rc = audioTestSvcClientRecvAck(pClient);
429
430 return rc;
431}
432
433/**
434 * Tells the server to record a (test) tone.
435 *
436 * @returns VBox status code.
437 * @param pClient Client to issue command for.
438 * @param pToneParms Tone parameters to use.
439 * @note How (and if) the server plays a tone depends on the actual implementation side.
440 */
441int AudioTestSvcClientToneRecord(PATSCLIENT pClient, PAUDIOTESTTONEPARMS pToneParms)
442{
443 ATSPKTREQTONEREC Req;
444
445 memcpy(&Req.ToneParms, pToneParms, sizeof(AUDIOTESTTONEPARMS));
446
447 audioTestSvcClientReqHdrInit(&Req.Hdr, sizeof(Req), ATSPKT_OPCODE_TONE_RECORD, 0);
448
449 int rc = audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req), NULL, 0);
450 if (RT_SUCCESS(rc))
451 rc = audioTestSvcClientRecvAck(pClient);
452
453 return rc;
454}
455
456/**
457 * Tells the server to send (download) a (packed up) test set archive.
458 * The test set must not be running / open anymore.
459 *
460 * @returns VBox status code.
461 * @param pClient Client to issue command for.
462 * @param pszTag Tag of test set to send.
463 * @param pszPathOutAbs Absolute path where to store the downloaded test set archive.
464 */
465int AudioTestSvcClientTestSetDownload(PATSCLIENT pClient, const char *pszTag, const char *pszPathOutAbs)
466{
467 ATSPKTREQTSETSND Req;
468
469 int rc = RTStrCopy(Req.szTag, sizeof(Req.szTag), pszTag);
470 AssertRCReturn(rc, rc);
471
472 audioTestSvcClientReqHdrInit(&Req.Hdr, sizeof(Req), ATSPKT_OPCODE_TESTSET_SEND, 0);
473
474 RTFILE hFile;
475 rc = RTFileOpen(&hFile, pszPathOutAbs, RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_DENY_WRITE);
476 AssertRCReturn(rc, rc);
477
478 rc = audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req), NULL, 0);
479 while (RT_SUCCESS(rc))
480 {
481 ATSSRVREPLY Reply;
482 RT_ZERO(Reply);
483 rc = audioTestSvcClientRecvReply(pClient, &Reply, false /* fNoDataOk */);
484 if (RT_SUCCESS(rc))
485 {
486 /* Calculate and validate checksum. */
487 uint32_t uDataCrc32;
488 memcpy(&uDataCrc32, Reply.pvPayload, sizeof(uint32_t));
489
490 if ( uDataCrc32
491 && uDataCrc32 != RTCrc32((uint8_t *)Reply.pvPayload + 4, Reply.cbPayload - 4))
492 rc = VERR_TAR_CHKSUM_MISMATCH; /** @todo Fudge! */
493
494 if (RT_SUCCESS(rc))
495 {
496 if ( RTStrNCmp(Reply.szOp, "DATA ", ATSPKT_OPCODE_MAX_LEN) == 0
497 && Reply.pvPayload
498 && Reply.cbPayload)
499 {
500 /** @todo Skip uCrc32 for now. */
501 rc = RTFileWrite(hFile, (uint8_t *)Reply.pvPayload + 4, Reply.cbPayload - 4, NULL);
502 }
503 else if (RTStrNCmp(Reply.szOp, "DATA EOF", ATSPKT_OPCODE_MAX_LEN) == 0)
504 {
505 rc = VINF_EOF;
506 }
507 else
508 {
509 AssertMsgFailed(("Got unexpected reply '%s'", Reply.szOp));
510 rc = VERR_NOT_SUPPORTED;
511 }
512 }
513 }
514
515 audioTestSvcClientReplyDestroy(&Reply);
516
517 int rc2 = audioTestSvcClientSendAck(pClient);
518 if (rc == VINF_SUCCESS) /* Might be VINF_EOF already. */
519 rc = rc2;
520
521 if (rc == VINF_EOF)
522 break;
523 }
524
525 int rc2 = RTFileClose(hFile);
526 if (RT_SUCCESS(rc))
527 rc = rc2;
528
529 return rc;
530}
531
532/**
533 * Disconnects from an ATS server.
534 *
535 * @returns VBox status code.
536 * @param pClient Client to disconnect.
537 */
538int AudioTestSvcClientClose(PATSCLIENT pClient)
539{
540 int rc = audioTestSvcClientDoBye(pClient);
541 if (RT_SUCCESS(rc))
542 rc = RTTcpClientClose(pClient->hSock);
543
544 return rc;
545}
546
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