VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/AudioTestService.cpp@ 89803

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

Audio/ValKit: Sending bigger data chunks now also works. bugref:10008

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 40.1 KB
Line 
1/* $Id: AudioTestService.cpp 89803 2021-06-21 06:19:50Z vboxsync $ */
2/** @file
3 * AudioTestService - Audio test execution server.
4 */
5
6/*
7 * Copyright (C) 2021 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
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP RTLOGGROUP_DEFAULT
23#include <iprt/alloca.h>
24#include <iprt/asm.h>
25#include <iprt/assert.h>
26#include <iprt/critsect.h>
27#include <iprt/crc.h>
28#include <iprt/ctype.h>
29#include <iprt/dir.h>
30#include <iprt/env.h>
31#include <iprt/err.h>
32#include <iprt/getopt.h>
33#include <iprt/handle.h>
34#include <iprt/initterm.h>
35#include <iprt/json.h>
36#include <iprt/list.h>
37#include <iprt/log.h>
38#include <iprt/mem.h>
39#include <iprt/message.h>
40#include <iprt/param.h>
41#include <iprt/path.h>
42#include <iprt/pipe.h>
43#include <iprt/poll.h>
44#include <iprt/process.h>
45#include <iprt/stream.h>
46#include <iprt/string.h>
47#include <iprt/thread.h>
48
49#include "AudioTestService.h"
50#include "AudioTestServiceInternal.h"
51
52
53/*********************************************************************************************************************************
54* Structures and Typedefs *
55*********************************************************************************************************************************/
56
57
58/*********************************************************************************************************************************
59* Global Variables *
60*********************************************************************************************************************************/
61/**
62 * Transport layers.
63 */
64static const PCATSTRANSPORT g_apTransports[] =
65{
66 &g_TcpTransport
67};
68
69/**
70 * ATS client state.
71 */
72typedef enum ATSCLIENTSTATE
73{
74 /** Invalid client state. */
75 ATSCLIENTSTATE_INVALID = 0,
76 /** Client is initialising, only the HOWDY and BYE packets are allowed. */
77 ATSCLIENTSTATE_INITIALISING,
78 /** Client is in fully cuntional state and ready to process all requests. */
79 ATSCLIENTSTATE_READY,
80 /** Client is destroying. */
81 ATSCLIENTSTATE_DESTROYING,
82 /** 32bit hack. */
83 ATSCLIENTSTATE_32BIT_HACK = 0x7fffffff
84} ATSCLIENTSTATE;
85
86/**
87 * ATS client instance.
88 */
89typedef struct ATSCLIENTINST
90{
91 /** List node for new clients. */
92 RTLISTNODE NdLst;
93 /** The current client state. */
94 ATSCLIENTSTATE enmState;
95 /** Transport backend specific data. */
96 PATSTRANSPORTCLIENT pTransportClient;
97 /** Client hostname. */
98 char *pszHostname;
99} ATSCLIENTINST;
100/** Pointer to a ATS client instance. */
101typedef ATSCLIENTINST *PATSCLIENTINST;
102
103/**
104 * Returns the string represenation of the given state.
105 */
106static const char *atsClientStateStringify(ATSCLIENTSTATE enmState)
107{
108 switch (enmState)
109 {
110 case ATSCLIENTSTATE_INVALID:
111 return "INVALID";
112 case ATSCLIENTSTATE_INITIALISING:
113 return "INITIALISING";
114 case ATSCLIENTSTATE_READY:
115 return "READY";
116 case ATSCLIENTSTATE_DESTROYING:
117 return "DESTROYING";
118 case ATSCLIENTSTATE_32BIT_HACK:
119 default:
120 break;
121 }
122
123 AssertMsgFailed(("Unknown state %#x\n", enmState));
124 return "UNKNOWN";
125}
126
127/**
128 * Calculates the checksum value, zero any padding space and send the packet.
129 *
130 * @returns IPRT status code.
131 * @param pThis The ATS instance.
132 * @param pClient The ATS client structure.
133 * @param pPkt The packet to send. Must point to a correctly
134 * aligned buffer.
135 */
136static int atsSendPkt(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTHDR pPkt)
137{
138 Assert(pPkt->cb >= sizeof(*pPkt));
139 pPkt->uCrc32 = RTCrc32(pPkt->achOpcode, pPkt->cb - RT_UOFFSETOF(ATSPKTHDR, achOpcode));
140 if (pPkt->cb != RT_ALIGN_32(pPkt->cb, ATSPKT_ALIGNMENT))
141 memset((uint8_t *)pPkt + pPkt->cb, '\0', RT_ALIGN_32(pPkt->cb, ATSPKT_ALIGNMENT) - pPkt->cb);
142
143 LogFlowFunc(("cb=%RU32 (%#x), payload=%RU32 (%#x), opcode=%.8s\n",
144 pPkt->cb, pPkt->cb, pPkt->cb - sizeof(ATSPKTHDR), pPkt->cb - sizeof(ATSPKTHDR), pPkt->achOpcode));
145 LogFlowFunc(("%.*Rhxd\n", RT_MIN(pPkt->cb, 256), pPkt));
146 int rc = pThis->pTransport->pfnSendPkt(&pThis->TransportInst, pClient->pTransportClient, pPkt);
147 while (RT_UNLIKELY(rc == VERR_INTERRUPTED) && !pThis->fTerminate)
148 rc = pThis->pTransport->pfnSendPkt(&pThis->TransportInst, pClient->pTransportClient, pPkt);
149
150 return rc;
151}
152
153/**
154 * Sends a babble reply and disconnects the client (if applicable).
155 *
156 * @param pThis The ATS instance.
157 * @param pClient The ATS client structure.
158 * @param pszOpcode The BABBLE opcode.
159 */
160static void atsReplyBabble(PATSSERVER pThis, PATSCLIENTINST pClient, const char *pszOpcode)
161{
162 ATSPKTHDR Reply;
163 Reply.cb = sizeof(Reply);
164 Reply.uCrc32 = 0;
165 memcpy(Reply.achOpcode, pszOpcode, sizeof(Reply.achOpcode));
166
167 pThis->pTransport->pfnBabble(&pThis->TransportInst, pClient->pTransportClient, &Reply, 20*1000);
168}
169
170/**
171 * Receive and validate a packet.
172 *
173 * Will send bable responses to malformed packets that results in a error status
174 * code.
175 *
176 * @returns IPRT status code.
177 * @param pThis The ATS instance.
178 * @param pClient The ATS client structure.
179 * @param ppPktHdr Where to return the packet on success. Free
180 * with RTMemFree.
181 * @param fAutoRetryOnFailure Whether to retry on error.
182 */
183static int atsRecvPkt(PATSSERVER pThis, PATSCLIENTINST pClient, PPATSPKTHDR ppPktHdr, bool fAutoRetryOnFailure)
184{
185 for (;;)
186 {
187 PATSPKTHDR pPktHdr;
188 int rc = pThis->pTransport->pfnRecvPkt(&pThis->TransportInst, pClient->pTransportClient, &pPktHdr);
189 if (RT_SUCCESS(rc))
190 {
191 /* validate the packet. */
192 if ( pPktHdr->cb >= sizeof(ATSPKTHDR)
193 && pPktHdr->cb < ATSPKT_MAX_SIZE)
194 {
195 Log2(("atsRecvPkt: pPktHdr=%p cb=%#x crc32=%#x opcode=%.8s\n"
196 "%.*Rhxd\n",
197 pPktHdr, pPktHdr->cb, pPktHdr->uCrc32, pPktHdr->achOpcode, RT_MIN(pPktHdr->cb, 256), pPktHdr));
198 uint32_t uCrc32Calc = pPktHdr->uCrc32 != 0
199 ? RTCrc32(&pPktHdr->achOpcode[0], pPktHdr->cb - RT_UOFFSETOF(ATSPKTHDR, achOpcode))
200 : 0;
201 if (pPktHdr->uCrc32 == uCrc32Calc)
202 {
203 AssertCompileMemberSize(ATSPKTHDR, achOpcode, 8);
204 if ( RT_C_IS_UPPER(pPktHdr->achOpcode[0])
205 && RT_C_IS_UPPER(pPktHdr->achOpcode[1])
206 && (RT_C_IS_UPPER(pPktHdr->achOpcode[2]) || pPktHdr->achOpcode[2] == ' ')
207 && (RT_C_IS_PRINT(pPktHdr->achOpcode[3]) || pPktHdr->achOpcode[3] == ' ')
208 && (RT_C_IS_PRINT(pPktHdr->achOpcode[4]) || pPktHdr->achOpcode[4] == ' ')
209 && (RT_C_IS_PRINT(pPktHdr->achOpcode[5]) || pPktHdr->achOpcode[5] == ' ')
210 && (RT_C_IS_PRINT(pPktHdr->achOpcode[6]) || pPktHdr->achOpcode[6] == ' ')
211 && (RT_C_IS_PRINT(pPktHdr->achOpcode[7]) || pPktHdr->achOpcode[7] == ' ')
212 )
213 {
214 Log(("atsRecvPkt: cb=%#x opcode=%.8s\n", pPktHdr->cb, pPktHdr->achOpcode));
215 *ppPktHdr = pPktHdr;
216 return rc;
217 }
218
219 rc = VERR_IO_BAD_COMMAND;
220 }
221 else
222 {
223 Log(("atsRecvPkt: cb=%#x opcode=%.8s crc32=%#x actual=%#x\n",
224 pPktHdr->cb, pPktHdr->achOpcode, pPktHdr->uCrc32, uCrc32Calc));
225 rc = VERR_IO_CRC;
226 }
227 }
228 else
229 rc = VERR_IO_BAD_LENGTH;
230
231 /* Send babble reply and disconnect the client if the transport is
232 connection oriented. */
233 if (rc == VERR_IO_BAD_LENGTH)
234 atsReplyBabble(pThis, pClient, "BABBLE L");
235 else if (rc == VERR_IO_CRC)
236 atsReplyBabble(pThis, pClient, "BABBLE C");
237 else if (rc == VERR_IO_BAD_COMMAND)
238 atsReplyBabble(pThis, pClient, "BABBLE O");
239 else
240 atsReplyBabble(pThis, pClient, "BABBLE ");
241 RTMemFree(pPktHdr);
242 }
243
244 /* Try again or return failure? */
245 if ( pThis->fTerminate
246 || rc != VERR_INTERRUPTED
247 || !fAutoRetryOnFailure
248 )
249 {
250 Log(("atsRecvPkt: rc=%Rrc\n", rc));
251 return rc;
252 }
253 }
254}
255
256/**
257 * Make a simple reply, only status opcode.
258 *
259 * @returns IPRT status code of the send.
260 * @param pThis The ATS instance.
261 * @param pClient The ATS client structure.
262 * @param pReply The reply packet.
263 * @param pszOpcode The status opcode. Exactly 8 chars long, padd
264 * with space.
265 * @param cbExtra Bytes in addition to the header.
266 */
267static int atsReplyInternal(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTHDR pReply, const char *pszOpcode, size_t cbExtra)
268{
269 /* copy the opcode, don't be too strict in case of a padding screw up. */
270 size_t cchOpcode = strlen(pszOpcode);
271 if (RT_LIKELY(cchOpcode == sizeof(pReply->achOpcode)))
272 memcpy(pReply->achOpcode, pszOpcode, sizeof(pReply->achOpcode));
273 else
274 {
275 Assert(cchOpcode == sizeof(pReply->achOpcode));
276 while (cchOpcode > 0 && pszOpcode[cchOpcode - 1] == ' ')
277 cchOpcode--;
278 AssertMsgReturn(cchOpcode < sizeof(pReply->achOpcode), ("%d/'%.8s'\n", cchOpcode, pszOpcode), VERR_INTERNAL_ERROR_4);
279 memcpy(pReply->achOpcode, pszOpcode, cchOpcode);
280 memset(&pReply->achOpcode[cchOpcode], ' ', sizeof(pReply->achOpcode) - cchOpcode);
281 }
282
283 pReply->cb = (uint32_t)sizeof(ATSPKTHDR) + (uint32_t)cbExtra;
284 pReply->uCrc32 = 0;
285
286 return atsSendPkt(pThis, pClient, pReply);
287}
288
289/**
290 * Make a simple reply, only status opcode.
291 *
292 * @returns IPRT status code of the send.
293 * @param pThis The ATS instance.
294 * @param pClient The ATS client structure.
295 * @param pPktHdr The original packet (for future use).
296 * @param pszOpcode The status opcode. Exactly 8 chars long, padd
297 * with space.
298 */
299static int atsReplySimple(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTHDR pPktHdr, const char *pszOpcode)
300{
301 return atsReplyInternal(pThis, pClient, pPktHdr, pszOpcode, 0);
302}
303
304/**
305 * Acknowledges a packet with success.
306 *
307 * @returns IPRT status code of the send.
308 * @param pThis The ATS instance.
309 * @param pClient The ATS client structure.
310 * @param pPktHdr The original packet (for future use).
311 */
312static int atsReplyAck(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTHDR pPktHdr)
313{
314 return atsReplySimple(pThis, pClient, pPktHdr, "ACK ");
315}
316
317/**
318 * Replies with a failure.
319 *
320 * @returns IPRT status code of the send.
321 * @param pThis The ATS instance.
322 * @param pClient The ATS client structure.
323 * @param pPktHdr The original packet (for future use).
324 * @param pszOpcode The status opcode. Exactly 8 chars long, padd
325 * with space.
326 * @param rcReq The status code of the request.
327 * @param pszDetailFmt Longer description of the problem (format string).
328 * @param va Format arguments.
329 */
330static int atsReplyFailureV(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTHDR pPktHdr,
331 const char *pszOpcode, int rcReq, const char *pszDetailFmt, va_list va)
332{
333 RT_NOREF(pPktHdr);
334 union
335 {
336 ATSPKTHDR Hdr;
337 int rc;
338 char ach[256];
339 } uPkt;
340 RT_ZERO(uPkt);
341
342 size_t cchDetail = RTStrPrintfV(&uPkt.ach[sizeof(ATSPKTHDR)],
343 sizeof(uPkt) - sizeof(ATSPKTHDR),
344 pszDetailFmt, va);
345
346 uPkt.rc = rcReq;
347
348 return atsReplyInternal(pThis, pClient, &uPkt.Hdr, pszOpcode, sizeof(int) + cchDetail + 1);
349}
350
351/**
352 * Replies with a failure.
353 *
354 * @returns IPRT status code of the send.
355 * @param pThis The ATS instance.
356 * @param pClient The ATS client structure.
357 * @param pPktHdr The original packet (for future use).
358 * @param pszOpcode The status opcode. Exactly 8 chars long, padd
359 * with space.
360 * @param rcReq Status code.
361 * @param pszDetailFmt Longer description of the problem (format string).
362 * @param ... Format arguments.
363 */
364static int atsReplyFailure(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTHDR pPktHdr,
365 const char *pszOpcode, int rcReq, const char *pszDetailFmt, ...)
366{
367 va_list va;
368 va_start(va, pszDetailFmt);
369 int rc = atsReplyFailureV(pThis, pClient, pPktHdr, pszOpcode, rcReq, pszDetailFmt, va);
370 va_end(va);
371 return rc;
372}
373
374/**
375 * Replies according to the return code.
376 *
377 * @returns IPRT status code of the send.
378 * @param pThis The ATS instance.
379 * @param pClient The ATS client structure.
380 * @param pPktHdr The packet to reply to.
381 * @param rcOperation The status code to report.
382 * @param pszOperationFmt The operation that failed. Typically giving the
383 * function call with important arguments.
384 * @param ... Arguments to the format string.
385 */
386static int atsReplyRC(PATSSERVER pThis,
387 PATSCLIENTINST pClient, PATSPKTHDR pPktHdr, int rcOperation, const char *pszOperationFmt, ...)
388{
389 if (RT_SUCCESS(rcOperation))
390 return atsReplyAck(pThis, pClient, pPktHdr);
391
392 char szOperation[128];
393 va_list va;
394 va_start(va, pszOperationFmt);
395 RTStrPrintfV(szOperation, sizeof(szOperation), pszOperationFmt, va);
396 va_end(va);
397
398 return atsReplyFailure(pThis, pClient, pPktHdr, "FAILED ", rcOperation, "%s failed with rc=%Rrc (opcode '%.8s')",
399 szOperation, rcOperation, pPktHdr->achOpcode);
400}
401
402/**
403 * Signal a bad packet exact size.
404 *
405 * @returns IPRT status code of the send.
406 * @param pThis The ATS instance.
407 * @param pClient The ATS client structure.
408 * @param pPktHdr The packet to reply to.
409 * @param cb The wanted size.
410 */
411static int atsReplyBadSize(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTHDR pPktHdr, size_t cb)
412{
413 return atsReplyFailure(pThis, pClient, pPktHdr, "BAD SIZE", VERR_INVALID_PARAMETER, "Expected at %zu bytes, got %u (opcode '%.8s')",
414 cb, pPktHdr->cb, pPktHdr->achOpcode);
415}
416
417/**
418 * Deals with a unknown command.
419 *
420 * @returns IPRT status code of the send.
421 * @param pThis The ATS instance.
422 * @param pClient The ATS client structure.
423 * @param pPktHdr The packet to reply to.
424 */
425static int atsReplyUnknown(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTHDR pPktHdr)
426{
427 return atsReplyFailure(pThis, pClient, pPktHdr, "UNKNOWN ", VERR_NOT_FOUND, "Opcode '%.8s' is not known", pPktHdr->achOpcode);
428}
429
430#if 0
431/**
432 * Deals with a command which contains an unterminated string.
433 *
434 * @returns IPRT status code of the send.
435 * @param pClient The ATS client structure.
436 * @param pPktHdr The packet containing the unterminated string.
437 */
438static int atsReplyBadStrTermination(PATSCLIENT pClient, PATSPKTHDR pPktHdr)
439{
440 return atsReplyFailure(pClient, pPktHdr, "BAD TERM", VERR_INVALID_PARAMETER, "Opcode '%.8s' contains an unterminated string", pPktHdr->achOpcode);
441}
442#endif
443
444/**
445 * Deals with a command sent in an invalid client state.
446 *
447 * @returns IPRT status code of the send.
448 * @param pThis The ATS instance.
449 * @param pClient The ATS client structure.
450 * @param pPktHdr The packet containing the unterminated string.
451 */
452static int atsReplyInvalidState(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTHDR pPktHdr)
453{
454 return atsReplyFailure(pThis, pClient, pPktHdr, "INVSTATE", VERR_INVALID_STATE, "Opcode '%.8s' is not supported at client state '%s",
455 pPktHdr->achOpcode, atsClientStateStringify(pClient->enmState));
456}
457
458/**
459 * Verifies and acknowledges a "BYE" request.
460 *
461 * @returns IPRT status code.
462 * @param pThis The ATS instance.
463 * @param pClient The ATS client structure.
464 * @param pPktHdr The bye packet.
465 */
466static int atsDoBye(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTHDR pPktHdr)
467{
468 int rc;
469 if (pPktHdr->cb == sizeof(ATSPKTHDR))
470 {
471 rc = atsReplyAck(pThis, pClient, pPktHdr);
472 }
473 else
474 rc = atsReplyBadSize(pThis, pClient, pPktHdr, sizeof(ATSPKTHDR));
475 return rc;
476}
477
478/**
479 * Verifies and acknowledges a "HOWDY" request.
480 *
481 * @returns IPRT status code.
482 * @param pThis The ATS instance.
483 * @param pClient The ATS client structure.
484 * @param pPktHdr The howdy packet.
485 */
486static int atsDoHowdy(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTHDR pPktHdr)
487{
488 int rc = VINF_SUCCESS;
489
490 if (pPktHdr->cb != sizeof(ATSPKTREQHOWDY))
491 return atsReplyBadSize(pThis, pClient, pPktHdr, sizeof(ATSPKTREQHOWDY));
492
493 if (pClient->enmState != ATSCLIENTSTATE_INITIALISING)
494 return atsReplyInvalidState(pThis, pClient, pPktHdr);
495
496 PATSPKTREQHOWDY pReq = (PATSPKTREQHOWDY)pPktHdr;
497
498 if (pReq->uVersion != ATS_PROTOCOL_VS)
499 return atsReplyRC(pThis, pClient, pPktHdr, VERR_VERSION_MISMATCH, "The given version %#x is not supported", pReq->uVersion);
500
501 ATSPKTREPHOWDY Rep;
502 RT_ZERO(Rep);
503
504 Rep.uVersion = ATS_PROTOCOL_VS;
505
506 rc = atsReplyInternal(pThis, pClient, &Rep.Hdr, "ACK ", sizeof(Rep) - sizeof(ATSPKTHDR));
507 if (RT_SUCCESS(rc))
508 {
509 pThis->pTransport->pfnNotifyHowdy(&pThis->TransportInst, pClient->pTransportClient);
510 pClient->enmState = ATSCLIENTSTATE_READY;
511 }
512
513 return rc;
514}
515
516/**
517 * Verifies and acknowledges a "TSET BEG" request.
518 *
519 * @returns IPRT status code.
520 * @param pThis The ATS instance.
521 * @param pClient The ATS client structure.
522 * @param pPktHdr The test set begin packet.
523 */
524static int atsDoTestSetBegin(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTHDR pPktHdr)
525{
526 if (pPktHdr->cb != sizeof(ATSPKTREQTSETBEG))
527 return atsReplyBadSize(pThis, pClient, pPktHdr, sizeof(ATSPKTREQTSETBEG));
528
529 PATSPKTREQTSETBEG pReq = (PATSPKTREQTSETBEG)pPktHdr;
530
531 int rc = VINF_SUCCESS;
532
533 if (pThis->Callbacks.pfnTestSetBegin)
534 {
535 rc = pThis->Callbacks.pfnTestSetBegin(pThis->Callbacks.pvUser, pReq->szTag);
536 if (RT_FAILURE(rc))
537 return atsReplyRC(pThis, pClient, pPktHdr, rc, "Beginning test set '%s' failed", pReq->szTag);
538 }
539
540 if (RT_SUCCESS(rc))
541 {
542 rc = atsReplyAck(pThis, pClient, pPktHdr);
543 }
544 else
545 rc = atsReplyRC(pThis, pClient, pPktHdr, rc, "Beginning test set failed");
546
547 return rc;
548}
549
550/**
551 * Verifies and acknowledges a "TSET END" request.
552 *
553 * @returns IPRT status code.
554 * @param pThis The ATS instance.
555 * @param pClient The ATS client structure.
556 * @param pPktHdr The test set end packet.
557 */
558static int atsDoTestSetEnd(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTHDR pPktHdr)
559{
560 if (pPktHdr->cb != sizeof(ATSPKTREQTSETEND))
561 return atsReplyBadSize(pThis, pClient, pPktHdr, sizeof(ATSPKTREQTSETEND));
562
563 PATSPKTREQTSETEND pReq = (PATSPKTREQTSETEND)pPktHdr;
564
565 int rc = VINF_SUCCESS;
566
567 if (pThis->Callbacks.pfnTestSetEnd)
568 {
569 rc = pThis->Callbacks.pfnTestSetEnd(pThis->Callbacks.pvUser, pReq->szTag);
570 if (RT_FAILURE(rc))
571 return atsReplyRC(pThis, pClient, pPktHdr, rc, "Ending test set '%s' failed", pReq->szTag);
572 }
573 if (RT_SUCCESS(rc))
574 {
575 rc = atsReplyAck(pThis, pClient, pPktHdr);
576 }
577 else
578 rc = atsReplyRC(pThis, pClient, pPktHdr, rc, "Ending test set failed");
579
580 return rc;
581}
582
583/**
584 * Used by atsDoTestSetSend to wait for a reply ACK from the client.
585 *
586 * @returns VINF_SUCCESS on ACK, VERR_GENERAL_FAILURE on NACK,
587 * VERR_NET_NOT_CONNECTED on unknown response (sending a bable reply),
588 * or whatever atsRecvPkt returns.
589 * @param pThis The ATS instance.
590 * @param pClient The ATS client structure.
591 * @param pPktHdr The original packet (for future use).
592 */
593static int atsWaitForAck(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTHDR pPktHdr)
594{
595 RT_NOREF(pPktHdr);
596 /** @todo timeout? */
597 PATSPKTHDR pReply;
598 int rc = atsRecvPkt(pThis, pClient, &pReply, false /*fAutoRetryOnFailure*/);
599 if (RT_SUCCESS(rc))
600 {
601 if (atsIsSameOpcode(pReply, "ACK"))
602 rc = VINF_SUCCESS;
603 else if (atsIsSameOpcode(pReply, "NACK"))
604 rc = VERR_GENERAL_FAILURE;
605 else
606 {
607 atsReplyBabble(pThis, pClient, "BABBLE ");
608 rc = VERR_NET_NOT_CONNECTED;
609 }
610 RTMemFree(pReply);
611 }
612 return rc;
613}
614
615/**
616 * Verifies and acknowledges a "TSET SND" request.
617 *
618 * @returns IPRT status code.
619 * @param pThis The ATS instance.
620 * @param pClient The ATS client structure.
621 * @param pPktHdr The test set end packet.
622 */
623static int atsDoTestSetSend(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTHDR pPktHdr)
624{
625 if (pPktHdr->cb != sizeof(ATSPKTREQTSETSND))
626 return atsReplyBadSize(pThis, pClient, pPktHdr, sizeof(ATSPKTREQTSETSND));
627
628 PATSPKTREQTSETSND pReq = (PATSPKTREQTSETSND)pPktHdr;
629
630 int rc = VINF_SUCCESS;
631
632 if (!pThis->Callbacks.pfnTestSetSendRead)
633 return atsReplyRC(pThis, pClient, pPktHdr, VERR_NOT_SUPPORTED, "Sending test set not implemented");
634
635 if (pThis->Callbacks.pfnTestSetSendBegin)
636 {
637 rc = pThis->Callbacks.pfnTestSetSendBegin(pThis->Callbacks.pvUser, pReq->szTag);
638 if (RT_FAILURE(rc))
639 return atsReplyRC(pThis, pClient, pPktHdr, rc, "Beginning sending test set '%s' failed", pReq->szTag);
640 }
641
642 for (;;)
643 {
644 uint32_t uMyCrc32 = RTCrc32Start();
645 struct
646 {
647 ATSPKTHDR Hdr;
648 uint32_t uCrc32;
649 char ab[_64K];
650 char abPadding[ATSPKT_ALIGNMENT];
651 } Pkt;
652 size_t cbRead = 0;
653 rc = pThis->Callbacks.pfnTestSetSendRead(pThis->Callbacks.pvUser, pReq->szTag, &Pkt.ab, sizeof(Pkt.ab), &cbRead);
654 if ( RT_FAILURE(rc)
655 || cbRead == 0)
656 {
657 if ( rc == VERR_EOF
658 || (RT_SUCCESS(rc) && cbRead == 0))
659 {
660 Pkt.uCrc32 = RTCrc32Finish(uMyCrc32);
661 rc = atsReplyInternal(pThis, pClient, &Pkt.Hdr, "DATA EOF", sizeof(uint32_t) /* uCrc32 */);
662 if (RT_SUCCESS(rc))
663 rc = atsWaitForAck(pThis, pClient, &Pkt.Hdr);
664 }
665 else
666 rc = atsReplyRC(pThis, pClient, pPktHdr, rc, "Sending data for test set '%s' failed", pReq->szTag);
667 break;
668 }
669
670 uMyCrc32 = RTCrc32Process(uMyCrc32, &Pkt.ab[0], cbRead);
671 Pkt.uCrc32 = RTCrc32Finish(uMyCrc32);
672
673 Assert(cbRead <= sizeof(Pkt.ab));
674
675 rc = atsReplyInternal(pThis, pClient, &Pkt.Hdr, "DATA ", sizeof(uint32_t) /* uCrc32 */ + cbRead);
676 if (RT_FAILURE(rc))
677 break;
678
679 rc = atsWaitForAck(pThis, pClient, &Pkt.Hdr);
680 if (RT_FAILURE(rc))
681 break;
682 }
683
684 if (pThis->Callbacks.pfnTestSetSendEnd)
685 {
686 int rc2 = pThis->Callbacks.pfnTestSetSendEnd(pThis->Callbacks.pvUser, pReq->szTag);
687 if (RT_FAILURE(rc2))
688 return atsReplyRC(pThis, pClient, pPktHdr, rc2, "Ending sending test set '%s' failed", pReq->szTag);
689 }
690
691 return rc;
692}
693
694/**
695 * Verifies and processes a "TN PLY" request.
696 *
697 * @returns IPRT status code.
698 * @param pThis The ATS instance.
699 * @param pClient The ATS client structure.
700 * @param pPktHdr The packet header.
701 */
702static int atsDoTonePlay(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTHDR pPktHdr)
703{
704 int rc = VINF_SUCCESS;
705
706 if (pPktHdr->cb < sizeof(ATSPKTREQTONEPLAY))
707 return atsReplyBadSize(pThis, pClient, pPktHdr, sizeof(ATSPKTREQTONEPLAY));
708
709 if (pClient->enmState != ATSCLIENTSTATE_READY)
710 return atsReplyInvalidState(pThis, pClient, pPktHdr);
711
712 if (!pThis->Callbacks.pfnTonePlay)
713 return atsReplyRC(pThis, pClient, pPktHdr, VERR_NOT_SUPPORTED, "Playing tones not supported");
714
715 PATSPKTREQTONEPLAY pReq = (PATSPKTREQTONEPLAY)pPktHdr;
716 rc = pThis->Callbacks.pfnTonePlay(pThis->Callbacks.pvUser, &pReq->ToneParms);
717
718 int rc2 = atsReplyAck(pThis, pClient, pPktHdr);
719 if (RT_SUCCESS(rc))
720 rc = rc2;
721
722 return rc;
723}
724
725/**
726 * Verifies and processes a "TN REC" request.
727 *
728 * @returns IPRT status code.
729 * @param pThis The ATS instance.
730 * @param pClient The ATS client structure.
731 * @param pPktHdr The packet header.
732 */
733static int atsDoToneRecord(PATSSERVER pThis, PATSCLIENTINST pClient, PATSPKTHDR pPktHdr)
734{
735 int rc = VINF_SUCCESS;
736
737 if (pPktHdr->cb < sizeof(ATSPKTREQTONEREC))
738 return atsReplyBadSize(pThis, pClient, pPktHdr, sizeof(ATSPKTREQTONEREC));
739
740 if (pClient->enmState != ATSCLIENTSTATE_READY)
741 return atsReplyInvalidState(pThis, pClient, pPktHdr);
742
743 if (!pThis->Callbacks.pfnToneRecord)
744 return atsReplyRC(pThis, pClient, pPktHdr, VERR_NOT_SUPPORTED, "Recording tones not supported");
745
746 PATSPKTREQTONEREC pReq = (PATSPKTREQTONEREC)pPktHdr;
747 rc = pThis->Callbacks.pfnToneRecord(pThis->Callbacks.pvUser, &pReq->ToneParms);
748
749 int rc2 = atsReplyAck(pThis, pClient, pPktHdr);
750 if (RT_SUCCESS(rc))
751 rc = rc2;
752
753 return rc;
754}
755
756/**
757 * Main request processing routine for each client.
758 *
759 * @returns IPRT status code.
760 * @param pThis The ATS instance.
761 * @param pClient The ATS client structure sending the request.
762 */
763static int atsClientReqProcess(PATSSERVER pThis, PATSCLIENTINST pClient)
764{
765 /*
766 * Read client command packet and process it.
767 */
768 PATSPKTHDR pPktHdr = NULL;
769 int rc = atsRecvPkt(pThis, pClient, &pPktHdr, true /*fAutoRetryOnFailure*/);
770 if (RT_FAILURE(rc))
771 return rc;
772
773 /*
774 * Do a string switch on the opcode bit.
775 */
776 /* Connection: */
777 if ( atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_HOWDY))
778 rc = atsDoHowdy(pThis, pClient, pPktHdr);
779 else if (atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_BYE))
780 rc = atsDoBye(pThis, pClient, pPktHdr);
781 /* Test set handling: */
782 else if (atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_TESTSET_BEGIN))
783 rc = atsDoTestSetBegin(pThis, pClient, pPktHdr);
784 else if (atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_TESTSET_END))
785 rc = atsDoTestSetEnd(pThis, pClient, pPktHdr);
786 else if (atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_TESTSET_SEND))
787 rc = atsDoTestSetSend(pThis, pClient, pPktHdr);
788 /* Audio testing: */
789 else if (atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_TONE_PLAY))
790 rc = atsDoTonePlay(pThis, pClient, pPktHdr);
791 else if (atsIsSameOpcode(pPktHdr, ATSPKT_OPCODE_TONE_RECORD))
792 rc = atsDoToneRecord(pThis, pClient, pPktHdr);
793 /* Misc: */
794 else
795 rc = atsReplyUnknown(pThis, pClient, pPktHdr);
796
797 RTMemFree(pPktHdr);
798
799 return rc;
800}
801
802/**
803 * Destroys a client instance.
804 *
805 * @returns nothing.
806 * @param pClient The ATS client structure.
807 */
808static void atsClientDestroy(PATSCLIENTINST pClient)
809{
810 if (pClient->pszHostname)
811 RTStrFree(pClient->pszHostname);
812 RTMemFree(pClient);
813}
814
815/**
816 * The main thread worker serving the clients.
817 */
818static DECLCALLBACK(int) atsClientWorker(RTTHREAD hThread, void *pvUser)
819{
820 RT_NOREF(hThread);
821
822 PATSSERVER pThis = (PATSSERVER)pvUser;
823 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
824
825 unsigned cClientsMax = 0;
826 unsigned cClientsCur = 0;
827 PATSCLIENTINST *papClients = NULL;
828
829 /* Add the pipe to the poll set. */
830 int rc = RTPollSetAddPipe(pThis->hPollSet, pThis->hPipeR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, 0);
831 if (RT_SUCCESS(rc))
832 {
833 while (!pThis->fTerminate)
834 {
835 uint32_t fEvts;
836 uint32_t uId;
837 rc = RTPoll(pThis->hPollSet, RT_INDEFINITE_WAIT, &fEvts, &uId);
838 if (RT_SUCCESS(rc))
839 {
840 if (uId == 0)
841 {
842 if (fEvts & RTPOLL_EVT_ERROR)
843 break;
844
845 /* We got woken up because of a new client. */
846 Assert(fEvts & RTPOLL_EVT_READ);
847
848 uint8_t bRead;
849 size_t cbRead = 0;
850 rc = RTPipeRead(pThis->hPipeR, &bRead, 1, &cbRead);
851 AssertRC(rc);
852
853 RTCritSectEnter(&pThis->CritSectClients);
854 /* Walk the list and add all new clients. */
855 PATSCLIENTINST pIt, pItNext;
856 RTListForEachSafe(&pThis->LstClientsNew, pIt, pItNext, ATSCLIENTINST, NdLst)
857 {
858 RTListNodeRemove(&pIt->NdLst);
859 Assert(cClientsCur <= cClientsMax);
860 if (cClientsCur == cClientsMax)
861 {
862 /* Realloc to accommodate for the new clients. */
863 PATSCLIENTINST *papClientsNew = (PATSCLIENTINST *)RTMemRealloc(papClients, (cClientsMax + 10) * sizeof(PATSCLIENTINST));
864 if (RT_LIKELY(papClientsNew))
865 {
866 cClientsMax += 10;
867 papClients = papClientsNew;
868 }
869 }
870 if (cClientsCur < cClientsMax)
871 {
872 /* Find a free slot in the client array. */
873 unsigned idxSlt = 0;
874 while ( idxSlt < cClientsMax
875 && papClients[idxSlt] != NULL)
876 idxSlt++;
877
878 rc = pThis->pTransport->pfnPollSetAdd(&pThis->TransportInst, pThis->hPollSet, pIt->pTransportClient, idxSlt + 1);
879 if (RT_SUCCESS(rc))
880 {
881 cClientsCur++;
882 papClients[idxSlt] = pIt;
883 }
884 else
885 {
886 pThis->pTransport->pfnNotifyBye(&pThis->TransportInst, pIt->pTransportClient);
887 atsClientDestroy(pIt);
888 }
889 }
890 else
891 {
892 pThis->pTransport->pfnNotifyBye(&pThis->TransportInst, pIt->pTransportClient);
893 atsClientDestroy(pIt);
894 }
895 }
896 RTCritSectLeave(&pThis->CritSectClients);
897 }
898 else
899 {
900 /* Client sends a request, pick the right client and process it. */
901 PATSCLIENTINST pClient = papClients[uId - 1];
902 AssertPtr(pClient);
903 if (fEvts & RTPOLL_EVT_READ)
904 rc = atsClientReqProcess(pThis, pClient);
905
906 if ( (fEvts & RTPOLL_EVT_ERROR)
907 || RT_FAILURE(rc))
908 {
909 /* Close connection and remove client from array. */
910 rc = pThis->pTransport->pfnPollSetRemove(&pThis->TransportInst, pThis->hPollSet, pClient->pTransportClient, uId);
911 AssertRC(rc);
912
913 pThis->pTransport->pfnNotifyBye(&pThis->TransportInst, pClient->pTransportClient);
914 papClients[uId - 1] = NULL;
915 cClientsCur--;
916 atsClientDestroy(pClient);
917 }
918 }
919 }
920 }
921 }
922
923 return rc;
924}
925
926/**
927 * The main thread waiting for new client connections.
928 *
929 * @returns VBox status code.
930 */
931static DECLCALLBACK(int) atsMainThread(RTTHREAD hThread, void *pvUser)
932{
933 RT_NOREF(hThread);
934
935 PATSSERVER pThis = (PATSSERVER)pvUser;
936 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
937
938 int rc = RTThreadUserSignal(hThread);
939 AssertRCReturn(rc, rc);
940
941 while (!pThis->fTerminate)
942 {
943 /*
944 * Wait for new connection and spin off a new thread
945 * for every new client.
946 */
947 PATSTRANSPORTCLIENT pTransportClient;
948 rc = pThis->pTransport->pfnWaitForConnect(&pThis->TransportInst, &pTransportClient);
949 if (RT_FAILURE(rc))
950 continue;
951
952 /*
953 * New connection, create new client structure and spin of
954 * the request handling thread.
955 */
956 PATSCLIENTINST pClient = (PATSCLIENTINST)RTMemAllocZ(sizeof(ATSCLIENTINST));
957 if (RT_LIKELY(pClient))
958 {
959 pClient->enmState = ATSCLIENTSTATE_INITIALISING;
960 pClient->pTransportClient = pTransportClient;
961 pClient->pszHostname = NULL;
962
963 /* Add client to the new list and inform the worker thread. */
964 RTCritSectEnter(&pThis->CritSectClients);
965 RTListAppend(&pThis->LstClientsNew, &pClient->NdLst);
966 RTCritSectLeave(&pThis->CritSectClients);
967
968 size_t cbWritten = 0;
969 rc = RTPipeWrite(pThis->hPipeW, "", 1, &cbWritten);
970 if (RT_FAILURE(rc))
971 RTMsgError("Failed to inform worker thread of a new client");
972 }
973 else
974 {
975 RTMsgError("Creating new client structure failed with out of memory error\n");
976 pThis->pTransport->pfnNotifyBye(&pThis->TransportInst, pTransportClient);
977 }
978 }
979
980 return rc;
981}
982
983/**
984 * Initializes the global ATS state.
985 *
986 * @returns VBox status code.
987 * @param pThis The ATS instance.
988 * @param pszBindAddr Bind address. Empty means any address.
989 * If set to NULL, "127.0.0.1" will be used.
990 * @param uBindPort Bind port. If set to 0, ATS_DEFAULT_PORT is being used.
991 * @param pCallbacks The callbacks table to use.
992 * */
993int AudioTestSvcInit(PATSSERVER pThis,
994 const char *pszBindAddr, uint32_t uBindPort, PCATSCALLBACKS pCallbacks)
995{
996 memcpy(&pThis->Callbacks, pCallbacks, sizeof(ATSCALLBACKS));
997
998 pThis->fStarted = false;
999 pThis->fTerminate = false;
1000
1001 pThis->hPipeR = NIL_RTPIPE;
1002 pThis->hPipeW = NIL_RTPIPE;
1003
1004 RTListInit(&pThis->LstClientsNew);
1005
1006 /*
1007 * The default transporter is the first one.
1008 */
1009 pThis->pTransport = g_apTransports[0];
1010
1011 /*
1012 * Initialize the transport layer.
1013 */
1014 int rc = pThis->pTransport->pfnInit(&pThis->TransportInst, pszBindAddr ? pszBindAddr : ATS_TCP_HOST_DEFAULT_ADDR_STR,
1015 uBindPort ? uBindPort : ATS_TCP_HOST_DEFAULT_PORT);
1016 if (RT_SUCCESS(rc))
1017 {
1018 rc = RTCritSectInit(&pThis->CritSectClients);
1019 if (RT_SUCCESS(rc))
1020 {
1021 rc = RTPollSetCreate(&pThis->hPollSet);
1022 if (RT_SUCCESS(rc))
1023 {
1024 rc = RTPipeCreate(&pThis->hPipeR, &pThis->hPipeW, 0);
1025 if (RT_SUCCESS(rc))
1026 {
1027 /* Spin off the thread serving connections. */
1028 rc = RTThreadCreate(&pThis->hThreadServing, atsClientWorker, pThis, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE,
1029 "AUDTSTSRVC");
1030 if (RT_SUCCESS(rc))
1031 return VINF_SUCCESS;
1032 else
1033 RTMsgError("Creating the client worker thread failed with %Rrc\n", rc);
1034
1035 RTPipeClose(pThis->hPipeR);
1036 RTPipeClose(pThis->hPipeW);
1037 }
1038 else
1039 RTMsgError("Creating communications pipe failed with %Rrc\n", rc);
1040
1041 RTPollSetDestroy(pThis->hPollSet);
1042 }
1043 else
1044 RTMsgError("Creating pollset failed with %Rrc\n", rc);
1045
1046 RTCritSectDelete(&pThis->CritSectClients);
1047 }
1048 else
1049 RTMsgError("Creating global critical section failed with %Rrc\n", rc);
1050 }
1051 else
1052 RTMsgError("Initializing the transport layer failed with %Rrc\n", rc);
1053
1054 return rc;
1055}
1056
1057/**
1058 * Starts a formerly initialized ATS instance.
1059 *
1060 * @returns VBox status code.
1061 * @param pThis The ATS instance.
1062 */
1063int AudioTestSvcStart(PATSSERVER pThis)
1064{
1065 /* Spin off the main thread. */
1066 int rc = RTThreadCreate(&pThis->hThreadMain, atsMainThread, pThis, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE,
1067 "AUDTSTSRVM");
1068 if (RT_SUCCESS(rc))
1069 {
1070 rc = RTThreadUserWait(pThis->hThreadMain, RT_MS_30SEC);
1071 if (RT_SUCCESS(rc))
1072 pThis->fStarted = true;
1073 }
1074
1075 return rc;
1076}
1077
1078/**
1079 * Shuts down a formerly started ATS instance.
1080 *
1081 * @returns VBox status code.
1082 * @param pThis The ATS instance.
1083 */
1084int AudioTestSvcShutdown(PATSSERVER pThis)
1085{
1086 if (!pThis->fStarted)
1087 return VINF_SUCCESS;
1088
1089 ASMAtomicXchgBool(&pThis->fTerminate, true);
1090
1091 if (pThis->pTransport)
1092 pThis->pTransport->pfnTerm(&pThis->TransportInst);
1093
1094 size_t cbWritten;
1095 int rc = RTPipeWrite(pThis->hPipeW, "", 1, &cbWritten);
1096 AssertRCReturn(rc, rc);
1097
1098 /* First close serving thread. */
1099 int rcThread;
1100 rc = RTThreadWait(pThis->hThreadServing, RT_MS_30SEC, &rcThread);
1101 if (RT_SUCCESS(rc))
1102 {
1103 rc = rcThread;
1104 if (RT_SUCCESS(rc))
1105 {
1106 /* Close the main thread last. */
1107 rc = RTThreadWait(pThis->hThreadMain, RT_MS_30SEC, &rcThread);
1108 if (RT_SUCCESS(rc))
1109 rc = rcThread;
1110
1111 if (rc == VERR_TCP_SERVER_DESTROYED)
1112 rc = VINF_SUCCESS;
1113 }
1114 }
1115
1116 if (RT_SUCCESS(rc))
1117 pThis->fStarted = false;
1118
1119 return rc;
1120}
1121
1122/**
1123 * Destroys an ATS instance, internal version.
1124 *
1125 * @returns VBox status code.
1126 * @param pThis ATS instance to destroy.
1127 */
1128static int audioTestSvcDestroyInternal(PATSSERVER pThis)
1129{
1130 int rc = VINF_SUCCESS;
1131
1132 if (pThis->hPipeR != NIL_RTPIPE)
1133 {
1134 rc = RTPipeClose(pThis->hPipeR);
1135 AssertRCReturn(rc, rc);
1136 pThis->hPipeR = NIL_RTPIPE;
1137 }
1138
1139 if (pThis->hPipeW != NIL_RTPIPE)
1140 {
1141 rc = RTPipeClose(pThis->hPipeW);
1142 AssertRCReturn(rc, rc);
1143 pThis->hPipeW = NIL_RTPIPE;
1144 }
1145
1146 RTPollSetDestroy(pThis->hPollSet);
1147 pThis->hPollSet = NIL_RTPOLLSET;
1148
1149 pThis->pTransport = NULL;
1150
1151 PATSCLIENTINST pIt, pItNext;
1152 RTListForEachSafe(&pThis->LstClientsNew, pIt, pItNext, ATSCLIENTINST, NdLst)
1153 {
1154 RTListNodeRemove(&pIt->NdLst);
1155
1156 RTMemFree(pIt);
1157 pIt = NULL;
1158 }
1159
1160 if (RTCritSectIsInitialized(&pThis->CritSectClients))
1161 {
1162 rc = RTCritSectDelete(&pThis->CritSectClients);
1163 AssertRCReturn(rc, rc);
1164 }
1165
1166 return rc;
1167}
1168
1169/**
1170 * Destroys an ATS instance.
1171 *
1172 * @returns VBox status code.
1173 * @param pThis ATS instance to destroy.
1174 */
1175int AudioTestSvcDestroy(PATSSERVER pThis)
1176{
1177 return audioTestSvcDestroyInternal(pThis);
1178}
1179
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