#include "legato.h"
#include <stdio.h>
#include <pthread.h>
#include "interfaces.h"
static bool isIncoming=false;
static le_mcc_CallRef_t TestCallRef;
static le_audio_StreamRef_t MdmRxAudioRef = NULL;
static le_audio_StreamRef_t MdmTxAudioRef = NULL;
static le_audio_StreamRef_t FeInRef = NULL;
static le_audio_StreamRef_t FeOutRef = NULL;
static le_audio_StreamRef_t PlayerAudioRef = NULL;
static le_audio_ConnectorRef_t AudioInputConnectorRef = NULL;
static le_audio_ConnectorRef_t AudioOutputConnectorRef = NULL;
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 le_audio_DtmfDetectorHandlerRef_t DtmfHandlerRef1 = NULL;
static le_audio_DtmfDetectorHandlerRef_t DtmfHandlerRef2 = NULL;
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
(
le_audio_StreamRef_t streamRef,
char dtmf,
void* contextPtr
)
{
LE_INFO(
"MyDtmfDetectorHandler1 detects %c", dtmf);
}
static void MyDtmfDetectorHandler2
(
le_audio_StreamRef_t streamRef,
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_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_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_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)
{
}
}
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)
{
}
}
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)
{
}
}
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)
{
}
}
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
(
le_mcc_CallRef_t callRef,
le_mcc_Event_t callEvent,
void* contextPtr
)
{
if (callEvent == LE_MCC_EVENT_ALERTING)
{
LE_INFO(
"Call event is LE_MCC_EVENT_ALERTING.");
}
else if (callEvent == LE_MCC_EVENT_CONNECTED)
{
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)
{
{
LE_ERROR(
"Failed to connect Player on input connector!");
return;
}
{
return;
}
}
else
{
LE_ERROR(
"PlayerAudioRef or AudioInputConnectorRef is NULL");
}
}
else if (strcmp(DtmfSendingCase,"outband")==0)
{
{
LE_ERROR(
"Failed to play signalling DTMF!");
return;
}
}
}
else if (callEvent == LE_MCC_EVENT_TERMINATED)
{
LE_INFO(
"Call event is LE_MCC_EVENT_TERMINATED.");
switch(term)
{
case LE_MCC_TERM_NETWORK_FAIL:
LE_INFO(
"Termination reason is LE_MCC_TERM_NETWORK_FAIL");
break;
case LE_MCC_TERM_UNASSIGNED_NUMBER:
LE_INFO(
"Termination reason is LE_MCC_TERM_UNASSIGNED_NUMBER");
break;
case LE_MCC_TERM_USER_BUSY:
LE_INFO(
"Termination reason is LE_MCC_TERM_USER_BUSY");
break;
case LE_MCC_TERM_LOCAL_ENDED:
LE_INFO(
"Termination reason is LE_MCC_TERM_LOCAL_ENDED");
break;
case LE_MCC_TERM_REMOTE_ENDED:
LE_INFO(
"Termination reason is LE_MCC_TERM_REMOTE_ENDED");
break;
case LE_MCC_TERM_UNDEFINED:
LE_INFO(
"Termination reason is LE_MCC_TERM_UNDEFINED");
break;
default:
LE_INFO(
"Termination reason is %d", term);
break;
}
DisconnectAllAudio();
exit(0);
}
else if (callEvent == LE_MCC_EVENT_INCOMING)
{
LE_INFO(
"Call event is LE_MCC_EVENT_INCOMING.");
isIncoming=true;
{
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((PlayerAudioRef==NULL),
"OpenPlayer returns NULL!");
if (PlayerAudioRef && AudioOutputConnectorRef)
{
{
LE_ERROR(
"Failed to connect Player on output connector!");
return;
}
LE_INFO(
"Play DTMF on PlayerAudioRef.%p", PlayerAudioRef);
{
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);
}
}