#include <stdio.h>
#include <pthread.h>
#include "interfaces.h"
static bool isIncoming=false;
static const char* DestinationNumber;
static const char* DtmfSendingCase;
static const char* DtmfString;
static const char* InterfaceString;
static uint32_t Duration;
static uint32_t Pause;
static void PrintUsage
(
void
)
{
int idx;
bool sandboxed = (getuid() != 0);
const char * usagePtr[] = {
"Usage of the 'dtmfTest' app is:",
" app runProc dtmfTest --exe=dtmfTest -- <loc/rem>"
"<MIC/I2S/PCM/USB/USBTXI2SRX/USBTXPCMRX/USBRXI2STX/USBRXPCMTX>"
"<dtmfs> <duration in ms> <pause in ms> [<tel number> <inband/outband>]",
"",
};
{
if(sandboxed)
{
}
else
{
fprintf(stderr, "%s\n", usagePtr[idx]);
}
}
}
static void MyDtmfDetectorHandler1
(
char dtmf,
void* contextPtr
)
{
LE_INFO(
"MyDtmfDetectorHandler1 detects %c", dtmf);
}
static void MyDtmfDetectorHandler2
(
char dtmf,
void* contextPtr
)
{
LE_INFO(
"MyDtmfDetectorHandler2 detects %c", dtmf);
}
static void ConnectAudioToPcm
(
void
)
{
LE_ERROR_IF((MdmRxAudioRef==NULL),
"GetRxAudioStream returns NULL!");
LE_ERROR_IF((MdmTxAudioRef==NULL),
"GetTxAudioStream returns NULL!");
LE_ERROR_IF((FeOutRef==NULL),
"OpenPcmTx returns NULL!");
LE_ERROR_IF((FeInRef==NULL),
"OpenPcmRx returns NULL!");
LE_ERROR_IF((AudioInputConnectorRef==NULL),
"AudioInputConnectorRef is NULL!");
LE_ERROR_IF((AudioOutputConnectorRef==NULL),
"AudioOutputConnectorRef is NULL!");
if (MdmRxAudioRef && MdmTxAudioRef && FeOutRef && FeInRef &&
AudioInputConnectorRef && AudioOutputConnectorRef)
{
LE_ERROR_IF((res!=LE_OK),
"Failed to connect PCM RX on Input connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect mdmTx on Input connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect PCM TX on Output connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect mdmRx on Output connector!");
}
LE_INFO(
"Audio connected to PCM interface");
}
static void ConnectAudioToI2S
(
void
)
{
LE_ERROR_IF((MdmRxAudioRef==NULL),
"GetRxAudioStream returns NULL!");
LE_ERROR_IF((MdmTxAudioRef==NULL),
"GetTxAudioStream returns NULL!");
LE_ERROR_IF((FeOutRef==NULL),
"OpenI2sTx returns NULL!");
LE_ERROR_IF((FeInRef==NULL),
"OpenI2sRx returns NULL!");
LE_ERROR_IF((AudioInputConnectorRef==NULL),
"AudioInputConnectorRef is NULL!");
LE_ERROR_IF((AudioOutputConnectorRef==NULL),
"AudioOutputConnectorRef is NULL!");
if (MdmRxAudioRef && MdmTxAudioRef && FeOutRef && FeInRef &&
AudioInputConnectorRef && AudioOutputConnectorRef)
{
LE_ERROR_IF((res!=LE_OK),
"Failed to connect PCM RX on Input connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect mdmTx on Input connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect PCM TX on Output connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect mdmRx on Output connector!");
}
LE_INFO(
"Audio connected to I2S interface");
}
static void ConnectAudioToCodec
(
void
)
{
LE_ERROR_IF((MdmRxAudioRef==NULL),
"GetRxAudioStream returns NULL!");
LE_ERROR_IF((MdmTxAudioRef==NULL),
"GetTxAudioStream returns NULL!");
LE_ERROR_IF((FeOutRef==NULL),
"OpenSpeaker returns NULL!");
LE_ERROR_IF((AudioInputConnectorRef==NULL),
"AudioInputConnectorRef is NULL!");
LE_ERROR_IF((AudioOutputConnectorRef==NULL),
"AudioOutputConnectorRef is NULL!");
if (MdmRxAudioRef && MdmTxAudioRef && FeOutRef && FeInRef &&
AudioInputConnectorRef && AudioOutputConnectorRef)
{
LE_ERROR_IF((res!=LE_OK),
"Failed to connect Mic on Input connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect mdmTx on Input connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect Speaker on Output connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect mdmRx on Output connector!");
}
LE_INFO(
"Audio connected to Codec interface");
}
static void ConnectAudioToUsbTxI2sRx
(
void
)
{
LE_ERROR_IF((MdmRxAudioRef == NULL),
"GetRxAudioStream returns NULL!");
LE_ERROR_IF((MdmTxAudioRef == NULL),
"GetTxAudioStream returns NULL!");
LE_ERROR_IF((FeOutRef == NULL),
"OpenUsbTx returns NULL!");
LE_ERROR_IF((FeInRef == NULL),
"OpenI2sRx returns NULL!");
LE_ERROR_IF((AudioInputConnectorRef == NULL),
"AudioInputConnectorRef is NULL!");
LE_ERROR_IF((AudioOutputConnectorRef == NULL),
"AudioOutputConnectorRef is NULL!");
if (MdmRxAudioRef && MdmTxAudioRef && FeOutRef && FeInRef &&
AudioInputConnectorRef && AudioOutputConnectorRef)
{
LE_ERROR_IF((res!=LE_OK),
"Failed to connect I2S Rx on Input connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect mdmTx on Input connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect USB Tx on Output connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect mdmRx on Output connector!");
}
}
static void ConnectAudioToUsbTxPcmRx
(
void
)
{
LE_ERROR_IF((MdmRxAudioRef == NULL),
"GetRxAudioStream returns NULL!");
LE_ERROR_IF((MdmTxAudioRef == NULL),
"GetTxAudioStream returns NULL!");
LE_ERROR_IF((FeOutRef == NULL),
"OpenUsbTx returns NULL!");
LE_ERROR_IF((FeInRef == NULL),
"OpenPcmRx returns NULL!");
LE_ERROR_IF((AudioInputConnectorRef == NULL),
"AudioInputConnectorRef is NULL!");
LE_ERROR_IF((AudioOutputConnectorRef == NULL),
"AudioOutputConnectorRef is NULL!");
if (MdmRxAudioRef && MdmTxAudioRef && FeOutRef && FeInRef &&
AudioInputConnectorRef && AudioOutputConnectorRef)
{
LE_ERROR_IF((res!=LE_OK),
"Failed to connect PCM Rx on Input connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect mdmTx on Input connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect USB Tx on Output connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect mdmRx on Output connector!");
}
}
static void ConnectAudioToUsbRxI2sTx
(
void
)
{
LE_ERROR_IF((MdmRxAudioRef == NULL),
"GetRxAudioStream returns NULL!");
LE_ERROR_IF((MdmTxAudioRef == NULL),
"GetTxAudioStream returns NULL!");
LE_ERROR_IF((FeOutRef == NULL),
"OpenI2sTx returns NULL!");
LE_ERROR_IF((FeInRef == NULL),
"OpenUsbRx returns NULL!");
LE_ERROR_IF((AudioInputConnectorRef == NULL),
"AudioInputConnectorRef is NULL!");
LE_ERROR_IF((AudioOutputConnectorRef == NULL),
"AudioOutputConnectorRef is NULL!");
if (MdmRxAudioRef && MdmTxAudioRef && FeOutRef && FeInRef &&
AudioInputConnectorRef && AudioOutputConnectorRef)
{
LE_ERROR_IF((res!=LE_OK),
"Failed to connect USB Rx on Input connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect mdmTx on Input connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect I2S Tx on Output connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect mdmRx on Output connector!");
}
}
static void ConnectAudioToUsbRxPcmTx
(
void
)
{
LE_ERROR_IF((MdmRxAudioRef == NULL),
"GetRxAudioStream returns NULL!");
LE_ERROR_IF((MdmTxAudioRef == NULL),
"GetTxAudioStream returns NULL!");
LE_ERROR_IF((FeOutRef == NULL),
"OpenPcmTx returns NULL!");
LE_ERROR_IF((FeInRef == NULL),
"OpenUsbRx returns NULL!");
LE_ERROR_IF((AudioInputConnectorRef == NULL),
"AudioInputConnectorRef is NULL!");
LE_ERROR_IF((AudioOutputConnectorRef == NULL),
"AudioOutputConnectorRef is NULL!");
if (MdmRxAudioRef && MdmTxAudioRef && FeOutRef && FeInRef &&
AudioInputConnectorRef && AudioOutputConnectorRef)
{
LE_ERROR_IF((res!=LE_OK),
"Failed to connect USB Rx on Input connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect mdmTx on Input connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect PCM Tx on Output connector!");
LE_ERROR_IF((res!=LE_OK),
"Failed to connect mdmRx on Output connector!");
}
}
static void DisconnectAllAudio
(
void
)
{
if (DtmfHandlerRef1)
{
LE_INFO(
"delete DTMF handler 1\n");
DtmfHandlerRef1 = NULL;
sleep(1);
}
if (DtmfHandlerRef2)
{
LE_INFO(
"delete DTMF handler 2\n");
DtmfHandlerRef2 = NULL;
}
if (AudioInputConnectorRef)
{
if (FeInRef)
{
LE_INFO(
"Disconnect %p from connector.%p", FeInRef, AudioInputConnectorRef);
}
if(MdmTxAudioRef)
{
LE_INFO(
"Disconnect %p from connector.%p", MdmTxAudioRef, AudioInputConnectorRef);
}
if(PlayerAudioRef)
{
LE_INFO(
"Disconnect %p from connector.%p", PlayerAudioRef, AudioInputConnectorRef);
}
}
if(AudioOutputConnectorRef)
{
if(FeOutRef)
{
LE_INFO(
"Disconnect %p from connector.%p", FeOutRef, AudioOutputConnectorRef);
}
if(MdmRxAudioRef)
{
LE_INFO(
"Disconnect %p from connector.%p", MdmRxAudioRef, AudioOutputConnectorRef);
}
if(PlayerAudioRef)
{
LE_INFO(
"Disconnect %p from connector.%p", PlayerAudioRef, AudioOutputConnectorRef);
}
}
if(AudioInputConnectorRef)
{
AudioInputConnectorRef = NULL;
}
if(AudioOutputConnectorRef)
{
AudioOutputConnectorRef = NULL;
}
if(FeInRef)
{
FeInRef = NULL;
}
if(FeOutRef)
{
FeOutRef = NULL;
}
if(MdmRxAudioRef)
{
FeOutRef = NULL;
}
if(MdmTxAudioRef)
{
FeOutRef = NULL;
}
if(PlayerAudioRef)
{
PlayerAudioRef = NULL;
}
}
static void MyCallEventHandler
(
void* contextPtr
)
{
{
LE_INFO(
"Call event is LE_MCC_EVENT_ALERTING.");
}
{
LE_INFO(
"Call event is LE_MCC_EVENT_CONNECTED.");
if (strcmp(DtmfSendingCase,"inband")==0)
{
LE_ERROR_IF((PlayerAudioRef==NULL),
"OpenPlayer returns NULL!");
if (PlayerAudioRef && AudioInputConnectorRef)
{
if(res != LE_OK)
{
LE_ERROR(
"Failed to connect Player on input connector!");
return;
}
if(res != LE_OK)
{
return;
}
}
else
{
LE_ERROR(
"PlayerAudioRef or AudioInputConnectorRef is NULL");
}
}
else if (strcmp(DtmfSendingCase,"outband")==0)
{
if(res != LE_OK)
{
LE_ERROR(
"Failed to play signalling DTMF!");
return;
}
}
}
{
LE_INFO(
"Call event is LE_MCC_EVENT_TERMINATED.");
switch(term)
{
LE_INFO(
"Termination reason is LE_MCC_TERM_NETWORK_FAIL");
break;
LE_INFO(
"Termination reason is LE_MCC_TERM_UNASSIGNED_NUMBER");
break;
LE_INFO(
"Termination reason is LE_MCC_TERM_USER_BUSY");
break;
LE_INFO(
"Termination reason is LE_MCC_TERM_LOCAL_ENDED");
break;
LE_INFO(
"Termination reason is LE_MCC_TERM_REMOTE_ENDED");
break;
LE_INFO(
"Termination reason is LE_MCC_TERM_UNDEFINED");
break;
default:
LE_INFO(
"Termination reason is %d", term);
break;
}
DisconnectAllAudio();
exit(0);
}
{
LE_INFO(
"Call event is LE_MCC_EVENT_INCOMING.");
isIncoming=true;
if (res != LE_OK)
{
LE_INFO(
"Failed to answer the call.");
}
}
else
{
}
}
static void PlayLocalDtmf
(
const char * argString
)
{
if (strncmp(argString, "PCM", strlen("PCM")) == 0)
{
LE_INFO(
"Play DTMF on PCM output interface");
LE_ERROR_IF((FeOutRef==NULL),
"OpenPcmTx returns NULL!");
}
else if (strncmp(argString, "I2S", strlen("I2S")) == 0)
{
LE_INFO(
"Play DTMF on I2S output interface");
LE_ERROR_IF((FeOutRef==NULL),
"OpenI2STx returns NULL!");
}
else if(strncmp(argString, "MIC", strlen("MIC")) == 0)
{
LE_ERROR_IF((FeOutRef==NULL),
"OpenSpeaker returns NULL!");
}
else if(strncmp(argString, "USB", strlen("USB")) == 0)
{
LE_ERROR_IF((FeOutRef==NULL),
"OpenUsbTx returns NULL!");
}
LE_ERROR_IF((AudioOutputConnectorRef==NULL),
"AudioOutputConnectorRef is NULL!");
{
LE_ERROR_IF((res!=LE_OK),
"Failed to connect Speaker on Output connector!");
}
LE_ERROR_IF((PlayerAudioRef==NULL),
"OpenPlayer returns NULL!");
if (PlayerAudioRef && AudioOutputConnectorRef)
{
if(res != LE_OK)
{
LE_ERROR(
"Failed to connect Player on output connector!");
return;
}
LE_INFO(
"Play DTMF on PlayerAudioRef.%p", PlayerAudioRef);
if(res != LE_OK)
{
return;
}
}
}
static void SigHandler
(
int sigNum
)
{
DisconnectAllAudio();
if(TestCallRef)
{
}
exit(EXIT_SUCCESS);
}
{
bool isLocalTest = false;
signal(SIGINT, SigHandler);
{
if (NULL == InterfaceString)
{
exit(EXIT_FAILURE);
}
if (NULL == durationPtr)
{
exit(EXIT_FAILURE);
}
if (NULL == pausePtr)
{
exit(EXIT_FAILURE);
}
Duration = atoi(durationPtr);
Pause = atoi(pausePtr);
if (NULL == DestinationNumber)
{
exit(EXIT_FAILURE);
}
LE_INFO(
" DTMF to play.\"%s\"", DtmfString);
LE_INFO(
" Duration.%dms", Duration);
LE_INFO(
" Phone number.%s", DestinationNumber);
LE_INFO(
" DTMF Sending case.%s", DtmfSendingCase);
LE_INFO(
" Interface.%s", InterfaceString);
}
{
if (NULL == locPtr)
{
exit(EXIT_FAILURE);
}
if(strncmp(locPtr, "loc", strlen("loc"))==0)
{
LE_INFO(
" Play DTMF on local interface");
if (NULL == InterfaceString)
{
exit(EXIT_FAILURE);
}
if (NULL == durationPtr)
{
exit(EXIT_FAILURE);
}
Duration = atoi(durationPtr);
if (NULL == pausePtr)
{
exit(EXIT_FAILURE);
}
Pause = atoi(pausePtr);
LE_INFO(
" DTMF to play.\"%s\"", DtmfString);
LE_INFO(
" Duration.%dms", Duration);
LE_INFO(
" Interface.%s", InterfaceString);
isLocalTest = true;
}
else
{
PrintUsage();
exit(EXIT_FAILURE);
}
}
else
{
PrintUsage();
}
if (!isLocalTest)
{
if(strncmp(InterfaceString, "I2S", strlen("I2S"))==0)
{
ConnectAudioToI2S();
}
else if(strncmp(InterfaceString, "PCM", strlen("PCM"))==0)
{
ConnectAudioToPcm();
}
else if(strncmp(InterfaceString, "MIC", strlen("MIC"))==0)
{
ConnectAudioToCodec();
}
else if(strncmp(InterfaceString,"USBTXI2SRX", strlen("USBTXI2SRX"))==0)
{
ConnectAudioToUsbTxI2sRx();
}
else if (strncmp(InterfaceString,"USBTXPCMRX", strlen("USBTXPCMRX"))==0)
{
ConnectAudioToUsbTxPcmRx();
}
else if (strncmp(InterfaceString,"USBRXI2STX", strlen("USBRXI2STX"))==0)
{
ConnectAudioToUsbRxI2sTx();
}
else if (strncmp(InterfaceString,"USBRXPCMTX", strlen("USBRXPCMTX"))==0)
{
ConnectAudioToUsbRxPcmTx();
}
else
{
}
MyDtmfDetectorHandler1,
NULL);
MyDtmfDetectorHandler2,
NULL);
}
else
{
if (NULL == InterfaceString)
{
exit(EXIT_FAILURE);
}
PlayLocalDtmf(InterfaceString);
}
}