1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
|
/* $Id: AudioTestServiceClient.cpp $ */
/** @file
* AudioTestServiceClient - Audio Test Service (ATS), Client helpers.
*
* Note: Only does TCP/IP as transport layer for now.
*/
/*
* Copyright (C) 2021-2022 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_AUDIO_TEST
#include <iprt/crc.h>
#include <iprt/err.h>
#include <iprt/file.h>
#include <iprt/mem.h>
#include <iprt/string.h>
#include <iprt/tcp.h>
#include <VBox/log.h>
#include "AudioTestService.h"
#include "AudioTestServiceInternal.h"
#include "AudioTestServiceClient.h"
/** @todo Use common defines between server protocol and this client. */
/**
* A generic ATS reply, used by the client
* to process the incoming packets.
*/
typedef struct ATSSRVREPLY
{
char szOp[ATSPKT_OPCODE_MAX_LEN];
/** Pointer to payload data.
* This does *not* include the header! */
void *pvPayload;
/** Size (in bytes) of the payload data.
* This does *not* include the header! */
size_t cbPayload;
} ATSSRVREPLY;
/** Pointer to a generic ATS reply. */
typedef struct ATSSRVREPLY *PATSSRVREPLY;
/*********************************************************************************************************************************
* Prototypes *
*********************************************************************************************************************************/
static int audioTestSvcClientDisconnectInternal(PATSCLIENT pClient);
/**
* Initializes an ATS client, internal version.
*
* @param pClient Client to initialize.
*/
static void audioTestSvcClientInit(PATSCLIENT pClient)
{
RT_BZERO(pClient, sizeof(ATSCLIENT));
}
/**
* Destroys an ATS server reply.
*
* @param pReply Reply to destroy.
*/
static void audioTestSvcClientReplyDestroy(PATSSRVREPLY pReply)
{
if (!pReply)
return;
if (pReply->pvPayload)
{
Assert(pReply->cbPayload);
RTMemFree(pReply->pvPayload);
pReply->pvPayload = NULL;
}
pReply->cbPayload = 0;
}
/**
* Receives a reply from an ATS server.
*
* @returns VBox status code.
* @param pClient Client to receive reply for.
* @param pReply Where to store the reply.
* The reply must be destroyed with audioTestSvcClientReplyDestroy() then.
* @param fNoDataOk If it's okay that the reply is not expected to have any payload.
*/
static int audioTestSvcClientRecvReply(PATSCLIENT pClient, PATSSRVREPLY pReply, bool fNoDataOk)
{
LogFlowFuncEnter();
PATSPKTHDR pPktHdr;
int rc = pClient->pTransport->pfnRecvPkt(pClient->pTransportInst, pClient->pTransportClient, &pPktHdr);
if (RT_SUCCESS(rc))
{
AssertReleaseMsgReturn(pPktHdr->cb >= sizeof(ATSPKTHDR),
("audioTestSvcClientRecvReply: Received invalid packet size (%RU32)\n", pPktHdr->cb),
VERR_NET_PROTOCOL_ERROR);
pReply->cbPayload = pPktHdr->cb - sizeof(ATSPKTHDR);
Log3Func(("szOp=%.8s, cb=%RU32\n", pPktHdr->achOpcode, pPktHdr->cb));
if (pReply->cbPayload)
{
pReply->pvPayload = RTMemDup((uint8_t *)pPktHdr + sizeof(ATSPKTHDR), pReply->cbPayload);
}
else
pReply->pvPayload = NULL;
if ( !pReply->cbPayload
&& !fNoDataOk)
{
LogRelFunc(("Payload is empty (%zu), but caller expected data\n", pReply->cbPayload));
rc = VERR_NET_PROTOCOL_ERROR;
}
else
{
memcpy(&pReply->szOp, &pPktHdr->achOpcode, sizeof(pReply->szOp));
}
RTMemFree(pPktHdr);
pPktHdr = NULL;
}
if (RT_FAILURE(rc))
LogRelFunc(("Receiving reply from server failed with %Rrc\n", rc));
LogFlowFuncLeaveRC(rc);
return rc;
}
/**
* Receives a reply for an ATS server and checks if it is an acknowledge (success) one.
*
* @returns VBox status code.
* @retval VERR_NET_PROTOCOL_ERROR if the reply indicates a failure.
* @param pClient Client to receive reply for.
*/
static int audioTestSvcClientRecvAck(PATSCLIENT pClient)
{
ATSSRVREPLY Reply;
RT_ZERO(Reply);
int rc = audioTestSvcClientRecvReply(pClient, &Reply, true /* fNoDataOk */);
if (RT_SUCCESS(rc))
{
/* Most likely cases first. */
if ( RTStrNCmp(Reply.szOp, "ACK ", ATSPKT_OPCODE_MAX_LEN) == 0)
{
/* Nothing to do here. */
}
else if (RTStrNCmp(Reply.szOp, "FAILED ", ATSPKT_OPCODE_MAX_LEN) == 0)
{
LogRelFunc(("Received error from server (cbPayload=%zu)\n", Reply.cbPayload));
if (Reply.cbPayload)
{
if ( Reply.cbPayload >= sizeof(int) /* At least the rc must be present. */
&& Reply.cbPayload <= sizeof(ATSPKTREPFAIL) - sizeof(ATSPKTHDR))
{
rc = *(int *)Reply.pvPayload; /* Reach error code back to caller. */
const char *pcszMsg = (char *)Reply.pvPayload + sizeof(int);
/** @todo Check NULL termination of pcszMsg? */
LogRelFunc(("Error message: %s (%Rrc)\n", pcszMsg, rc));
}
else
{
LogRelFunc(("Received invalid failure payload (cb=%zu)\n", Reply.cbPayload));
rc = VERR_NET_PROTOCOL_ERROR;
}
}
}
else
{
LogRelFunc(("Received invalid opcode ('%.8s')\n", Reply.szOp));
rc = VERR_NET_PROTOCOL_ERROR;
}
audioTestSvcClientReplyDestroy(&Reply);
}
LogRelFlowFuncLeaveRC(rc);
return rc;
}
/**
* Sends a message plus optional payload to an ATS server.
*
* @returns VBox status code.
* @param pClient Client to send message for.
* @param pvHdr Pointer to header data to send.
* @param cbHdr Size (in bytes) of \a pvHdr to send.
*/
static int audioTestSvcClientSendMsg(PATSCLIENT pClient, void *pvHdr, size_t cbHdr)
{
RT_NOREF(cbHdr);
AssertPtrReturn(pClient->pTransport, VERR_INVALID_POINTER);
AssertPtrReturn(pClient->pTransportInst, VERR_INVALID_POINTER);
AssertPtrReturn(pClient->pTransportClient, VERR_INVALID_POINTER);
return pClient->pTransport->pfnSendPkt(pClient->pTransportInst, pClient->pTransportClient, (PCATSPKTHDR)pvHdr);
}
/**
* Initializes a client request header.
*
* @returns VBox status code.
* @param pReqHdr Request header to initialize.
* @param cbReq Size (in bytes) the request will have (does *not* include payload).
* @param pszOp Operation to perform with the request.
* @param cbPayload Size (in bytes) of payload that will follow the header. Optional and can be 0.
*/
DECLINLINE (void) audioTestSvcClientReqHdrInit(PATSPKTHDR pReqHdr, size_t cbReq, const char *pszOp, size_t cbPayload)
{
AssertReturnVoid(strlen(pszOp) >= 2);
AssertReturnVoid(strlen(pszOp) <= ATSPKT_OPCODE_MAX_LEN);
/** @todo Validate opcode. */
RT_BZERO(pReqHdr, sizeof(ATSPKTHDR));
memcpy(pReqHdr->achOpcode, pszOp, strlen(pszOp));
pReqHdr->uCrc32 = 0; /** @todo Do CRC-32 calculation. */
pReqHdr->cb = (uint32_t)cbReq + (uint32_t)cbPayload;
Assert(pReqHdr->cb <= ATSPKT_MAX_SIZE);
}
/**
* Sends an acknowledege response back to the server.
*
* @returns VBox status code.
* @param pClient Client to send command for.
*/
static int audioTestSvcClientSendAck(PATSCLIENT pClient)
{
ATSPKTHDR Req;
audioTestSvcClientReqHdrInit(&Req, sizeof(Req), "ACK ", 0);
return audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req));
}
/**
* Sends a greeting command (handshake) to an ATS server.
*
* @returns VBox status code.
* @param pClient Client to send command for.
*/
static int audioTestSvcClientDoGreet(PATSCLIENT pClient)
{
ATSPKTREQHOWDY Req;
Req.uVersion = ATS_PROTOCOL_VS;
audioTestSvcClientReqHdrInit(&Req.Hdr, sizeof(Req), ATSPKT_OPCODE_HOWDY, 0);
int rc = audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req));
if (RT_SUCCESS(rc))
rc = audioTestSvcClientRecvAck(pClient);
return rc;
}
/**
* Tells the ATS server that we want to disconnect.
*
* @returns VBox status code.
* @param pClient Client to disconnect.
*/
static int audioTestSvcClientDoBye(PATSCLIENT pClient)
{
ATSPKTHDR Req;
audioTestSvcClientReqHdrInit(&Req, sizeof(Req), ATSPKT_OPCODE_BYE, 0);
int rc = audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req));
if (RT_SUCCESS(rc))
rc = audioTestSvcClientRecvAck(pClient);
return rc;
}
/**
* Creates an ATS client.
*
* @returns VBox status code.
* @param pClient Client to create.
*/
int AudioTestSvcClientCreate(PATSCLIENT pClient)
{
audioTestSvcClientInit(pClient);
/*
* The default transporter is the first one.
*/
pClient->pTransport = g_apTransports[0]; /** @todo Make this dynamic. */
return pClient->pTransport->pfnCreate(&pClient->pTransportInst);
}
/**
* Destroys an ATS client.
*
* @returns VBox status code.
* @param pClient Client to destroy.
*/
void AudioTestSvcClientDestroy(PATSCLIENT pClient)
{
if (!pClient)
return;
/* ignore rc */ audioTestSvcClientDisconnectInternal(pClient);
if (pClient->pTransport)
{
pClient->pTransport->pfnDestroy(pClient->pTransportInst);
pClient->pTransportInst = NULL; /* Invalidate pointer. */
}
}
/**
* Handles a command line option.
*
* @returns VBox status code.
* @param pClient Client to handle option for.
* @param ch Option (short) to handle.
* @param pVal Option union to store the result in on success.
*/
int AudioTestSvcClientHandleOption(PATSCLIENT pClient, int ch, PCRTGETOPTUNION pVal)
{
AssertPtrReturn(pClient->pTransport, VERR_WRONG_ORDER); /* Must be created first via AudioTestSvcClientCreate(). */
if (!pClient->pTransport->pfnOption)
return VERR_GETOPT_UNKNOWN_OPTION;
return pClient->pTransport->pfnOption(pClient->pTransportInst, ch, pVal);
}
/**
* Connects to an ATS peer, extended version.
*
* @returns VBox status code.
* @param pClient Client to connect.
* @param msTimeout Timeout (in ms) waiting for a connection to be established.
* Use RT_INDEFINITE_WAIT to wait indefinitely.
*/
int AudioTestSvcClientConnectEx(PATSCLIENT pClient, RTMSINTERVAL msTimeout)
{
if (pClient->pTransportClient)
return VERR_NET_ALREADY_CONNECTED;
int rc = pClient->pTransport->pfnStart(pClient->pTransportInst);
if (RT_SUCCESS(rc))
{
rc = pClient->pTransport->pfnWaitForConnect(pClient->pTransportInst,
msTimeout, NULL /* pfFromServer */, &pClient->pTransportClient);
if (RT_SUCCESS(rc))
{
rc = audioTestSvcClientDoGreet(pClient);
}
}
if (RT_FAILURE(rc))
LogRelFunc(("Connecting to server (%RU32ms timeout) failed with %Rrc\n", msTimeout, rc));
return rc;
}
/**
* Connects to an ATS peer.
*
* @returns VBox status code.
* @param pClient Client to connect.
*/
int AudioTestSvcClientConnect(PATSCLIENT pClient)
{
return AudioTestSvcClientConnectEx(pClient, 30 * 1000 /* msTimeout */);
}
/**
* Tells the server to begin a new test set.
*
* @returns VBox status code.
* @param pClient Client to issue command for.
* @param pszTag Tag to use for the test set to begin.
*/
int AudioTestSvcClientTestSetBegin(PATSCLIENT pClient, const char *pszTag)
{
ATSPKTREQTSETBEG Req;
int rc = RTStrCopy(Req.szTag, sizeof(Req.szTag), pszTag);
AssertRCReturn(rc, rc);
audioTestSvcClientReqHdrInit(&Req.Hdr, sizeof(Req), ATSPKT_OPCODE_TESTSET_BEGIN, 0);
rc = audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req));
if (RT_SUCCESS(rc))
rc = audioTestSvcClientRecvAck(pClient);
return rc;
}
/**
* Tells the server to end a runing test set.
*
* @returns VBox status code.
* @param pClient Client to issue command for.
* @param pszTag Tag of test set to end.
*/
int AudioTestSvcClientTestSetEnd(PATSCLIENT pClient, const char *pszTag)
{
ATSPKTREQTSETEND Req;
int rc = RTStrCopy(Req.szTag, sizeof(Req.szTag), pszTag);
AssertRCReturn(rc, rc);
audioTestSvcClientReqHdrInit(&Req.Hdr, sizeof(Req), ATSPKT_OPCODE_TESTSET_END, 0);
rc = audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req));
if (RT_SUCCESS(rc))
rc = audioTestSvcClientRecvAck(pClient);
return rc;
}
/**
* Tells the server to play a (test) tone.
*
* @returns VBox status code.
* @param pClient Client to issue command for.
* @param pToneParms Tone parameters to use.
* @note How (and if) the server plays a tone depends on the actual implementation side.
*/
int AudioTestSvcClientTonePlay(PATSCLIENT pClient, PAUDIOTESTTONEPARMS pToneParms)
{
ATSPKTREQTONEPLAY Req;
memcpy(&Req.ToneParms, pToneParms, sizeof(AUDIOTESTTONEPARMS));
audioTestSvcClientReqHdrInit(&Req.Hdr, sizeof(Req), ATSPKT_OPCODE_TONE_PLAY, 0);
int rc = audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req));
if (RT_SUCCESS(rc))
rc = audioTestSvcClientRecvAck(pClient);
return rc;
}
/**
* Tells the server to record a (test) tone.
*
* @returns VBox status code.
* @param pClient Client to issue command for.
* @param pToneParms Tone parameters to use.
* @note How (and if) the server plays a tone depends on the actual implementation side.
*/
int AudioTestSvcClientToneRecord(PATSCLIENT pClient, PAUDIOTESTTONEPARMS pToneParms)
{
ATSPKTREQTONEREC Req;
memcpy(&Req.ToneParms, pToneParms, sizeof(AUDIOTESTTONEPARMS));
audioTestSvcClientReqHdrInit(&Req.Hdr, sizeof(Req), ATSPKT_OPCODE_TONE_RECORD, 0);
int rc = audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req));
if (RT_SUCCESS(rc))
rc = audioTestSvcClientRecvAck(pClient);
return rc;
}
/**
* Tells the server to send (download) a (packed up) test set archive.
* The test set must not be running / open anymore.
*
* @returns VBox status code.
* @param pClient Client to issue command for.
* @param pszTag Tag of test set to send.
* @param pszPathOutAbs Absolute path where to store the downloaded test set archive.
*/
int AudioTestSvcClientTestSetDownload(PATSCLIENT pClient, const char *pszTag, const char *pszPathOutAbs)
{
ATSPKTREQTSETSND Req;
int rc = RTStrCopy(Req.szTag, sizeof(Req.szTag), pszTag);
AssertRCReturn(rc, rc);
audioTestSvcClientReqHdrInit(&Req.Hdr, sizeof(Req), ATSPKT_OPCODE_TESTSET_SEND, 0);
RTFILE hFile;
rc = RTFileOpen(&hFile, pszPathOutAbs, RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_DENY_WRITE);
AssertRCReturn(rc, rc);
rc = audioTestSvcClientSendMsg(pClient, &Req, sizeof(Req));
while (RT_SUCCESS(rc))
{
ATSSRVREPLY Reply;
RT_ZERO(Reply);
rc = audioTestSvcClientRecvReply(pClient, &Reply, false /* fNoDataOk */);
if (RT_SUCCESS(rc))
{
/* Extract received CRC32 checksum. */
const size_t cbCrc32 = sizeof(uint32_t); /* Skip CRC32 in payload for actual CRC verification. */
uint32_t uSrcCrc32;
memcpy(&uSrcCrc32, Reply.pvPayload, cbCrc32);
if (uSrcCrc32)
{
const uint32_t uDstCrc32 = RTCrc32((uint8_t *)Reply.pvPayload + cbCrc32, Reply.cbPayload - cbCrc32);
Log2Func(("uSrcCrc32=%#x, cbRead=%zu -> uDstCrc32=%#x\n"
"%.*Rhxd\n",
uSrcCrc32, Reply.cbPayload - cbCrc32, uDstCrc32,
RT_MIN(64, Reply.cbPayload - cbCrc32), (uint8_t *)Reply.pvPayload + cbCrc32));
if (uSrcCrc32 != uDstCrc32)
rc = VERR_TAR_CHKSUM_MISMATCH; /** @todo Fudge! */
}
if (RT_SUCCESS(rc))
{
if ( RTStrNCmp(Reply.szOp, "DATA ", ATSPKT_OPCODE_MAX_LEN) == 0
&& Reply.pvPayload
&& Reply.cbPayload)
{
rc = RTFileWrite(hFile, (uint8_t *)Reply.pvPayload + cbCrc32, Reply.cbPayload - cbCrc32, NULL);
}
else if (RTStrNCmp(Reply.szOp, "DATA EOF", ATSPKT_OPCODE_MAX_LEN) == 0)
{
rc = VINF_EOF;
}
else
{
AssertMsgFailed(("Got unexpected reply '%s'", Reply.szOp));
rc = VERR_NOT_SUPPORTED;
}
}
}
audioTestSvcClientReplyDestroy(&Reply);
int rc2 = audioTestSvcClientSendAck(pClient);
if (rc == VINF_SUCCESS) /* Might be VINF_EOF already. */
rc = rc2;
if (rc == VINF_EOF)
break;
}
int rc2 = RTFileClose(hFile);
if (RT_SUCCESS(rc))
rc = rc2;
return rc;
}
/**
* Disconnects from an ATS server, internal version.
*
* @returns VBox status code.
* @param pClient Client to disconnect.
*/
static int audioTestSvcClientDisconnectInternal(PATSCLIENT pClient)
{
if (!pClient->pTransportClient) /* Not connected (yet)? Bail out early. */
return VINF_SUCCESS;
int rc = audioTestSvcClientDoBye(pClient);
if (RT_SUCCESS(rc))
{
if (pClient->pTransport->pfnNotifyBye)
pClient->pTransport->pfnNotifyBye(pClient->pTransportInst, pClient->pTransportClient);
pClient->pTransport->pfnDisconnect(pClient->pTransportInst, pClient->pTransportClient);
pClient->pTransportClient = NULL;
pClient->pTransport->pfnStop(pClient->pTransportInst);
}
return rc;
}
/**
* Disconnects from an ATS server.
*
* @returns VBox status code.
* @param pClient Client to disconnect.
*/
int AudioTestSvcClientDisconnect(PATSCLIENT pClient)
{
return audioTestSvcClientDisconnectInternal(pClient);
}
|