📄 mwitest.cpp
字号:
///////////////////////////////////////////////////////////
// NAME: mwitest.cpp
// DESCRIPTION: Siemens Message Waiting Indicator (MWI) demo
//
//////////////////////////////////////////////////////////
#define STRICT
#include <windows.h>
#include <process.h>
#include <winuser.h>
#include <stdio.h>
#include <fcntl.h>
#include <math.h>
#include <time.h>
#include "srllib.h"
#include "dxxxlib.h"
#include "main.h"
#include "mwitest.h"
extern TESTINFO g_TestInfo;
extern CHANINFO g_ChanInfo[MAXCHAN+1];
extern MSIINFO g_MsiInfo[MAXCHAN+1];
extern char *g_pszPlayBuff;
///////////////////////////////////////////////////////////
// NAME: MWIBegin
// DESCRIPTION:
//
//
//////////////////////////////////////////////////////////
void MWIBegin (void * tInfo)
{
int nIndex;
// Set device handles to -1
for(nIndex=1; nIndex <= MAXCHAN; nIndex++)
{
g_ChanInfo[nIndex].nDevHandle = -1;
}
// Open all voice channels
OUTPUT(0,"Opening Devices ...");
if (OpenVOXChannels() == -1)
{
LOGERROR(0,"Initialization FAILED");
ExitTest(ABNORMTERM);
}
ECTestBody();
// Close all and clean up
ExitTest(NORMTERM); // Function does not return
}
///////////////////////////////////////////////////////////
// NAME: ECTestBody
// DESCRIPTION:
//
//
//////////////////////////////////////////////////////////
int ECTestBody(void)
{
int nLoop, nEvtCnt, nEvtType, nEvtMult;
int nGroup, nDevh;
int nRcode;
int nTimer;
float fNoECREnergy, fECREnergy, fECRChanEnergy;
char szBuffer[80];
for (nLoop=0; nLoop < g_TestInfo.nLoops; nLoop++)
{
UpdateLoopCount(nLoop+1);
CHECKENDTESTFLAG;
// Perform routing, routing reinitializes the ECr
OUTPUT(0,"Routing Resources ...");
if (g_TestInfo.bRandomMSI)
{
RouteResources(nLoop%6);
}
else
{
RouteResources(0);
}
for (nGroup=0; nGroup < g_TestInfo.nTotalChan/4; nGroup++)
{
CHECKENDTESTFLAG;
if (g_TestInfo.nECRecChan)
{
OUTPUT(g_TestInfo.nECRecChan+nGroup,"Recording EC Vox File ...");
RecordFile(g_TestInfo.nECRecChan+nGroup); // ECR record
CHECKENDTESTFLAG;
}
// Record the echo file if true
if(g_TestInfo.bNoECRecord) {
OUTPUT(g_TestInfo.nNoECRecChan+nGroup,"Recording NON EC Vox File ...");
RecordFile(g_TestInfo.nNoECRecChan+nGroup); // Standard record
CHECKENDTESTFLAG;
}
// Disable the ECR channel from recording
if(g_TestInfo.bECRRecord) {
// ECR Channel
OUTPUT(g_TestInfo.nECRStartChan+nGroup,"Recording EC Vox File on ECR Chan...");
RecordFile(g_TestInfo.nECRStartChan+nGroup);
CHECKENDTESTFLAG;
}
// Perform the play and resords
CHECKENDTESTFLAG;
OUTPUT(g_TestInfo.nStartChan+nGroup,"Playing Noise.vox ...");
PlayFile(g_TestInfo.nStartChan+nGroup); // Play the noise file
}
// Delay to check for events
OUTPUT(0,"Waiting for Events ...");
nTimer = 0;
while(nTimer<MAXPLAYTIME*2) {
CHECKENDTESTFLAG;
Sleep(500);
nTimer++;
}
CHECKENDTESTFLAG;
Sleep(1000);
// Calculate how many events wait for
nEvtMult = 4;
// Decrement event multiplier if EC chan is not recording
if(!g_TestInfo.bECRRecord) {
nEvtMult--;
}
// Decrement event multiplier if not recording echo
if(!g_TestInfo.bNoECRecord) {
nEvtMult--;
}
// Decrement event multiplier no recording of EC tslot
if(!g_TestInfo.bECRecChan) {
nEvtMult--;
}
// Wait for all the events
for(nEvtCnt=0;nEvtCnt<nGroup*nEvtMult;nEvtCnt++) {
CHECKENDTESTFLAG;
nRcode = sr_waitevt(2001);
if(nRcode == -1) {
// ERROR
LOGERROR(0,"SRL Timeout Event");
}
nEvtType = sr_getevttype(0);
if(nEvtType==TDX_ERROR) {
// ERROR
nDevh = sr_getevtdev(0);
sprintf(szBuffer,"%s TDX_ERROR Event: %s",ATDV_NAMEP(nDevh),ATDV_ERRMSGP(nDevh));
LOGERROR(0,szBuffer);
}
}
CHECKENDTESTFLAG;
// Allow the to empty buffers
Sleep(2000);
for(nGroup=0;nGroup<g_TestInfo.nTotalChan/4;nGroup++) {
CHECKENDTESTFLAG;
// If true close the echo file
if(g_TestInfo.bNoECRecord) {
dx_fileclose(g_ChanInfo[g_TestInfo.nECRecChan+nGroup].iott.io_fhandle); // ECR record
}
CHECKENDTESTFLAG;
if(g_TestInfo.bECRecChan) {
dx_fileclose(g_ChanInfo[g_TestInfo.nNoECRecChan+nGroup].iott.io_fhandle); // Standard record
}
CHECKENDTESTFLAG;
// ECR Channel file to close if recording
if(g_TestInfo.bECRRecord) {
dx_fileclose(g_ChanInfo[g_TestInfo.nECRStartChan+nGroup].iott.io_fhandle); // ECR Chan record
}
}
// Compute and compare all the energy for groups
for(nGroup=0;nGroup<g_TestInfo.nTotalChan/4;nGroup++) {
// Convert the files from mu-law pcm to linear
CHECKENDTESTFLAG;
if(g_TestInfo.bNoECRecord) {
CHECKENDTESTFLAG;
OUTPUT(g_TestInfo.nNoECRecChan+nGroup,"Converting No EC vox PCM to Linear");
ConvertFile2Linear(g_TestInfo.nNoECRecChan+nGroup);
}
// ECR record channel on the EC tslot
if(g_TestInfo.bECRecChan) {
CHECKENDTESTFLAG;
OUTPUT(g_TestInfo.nECRecChan+nGroup,"Converting EC vox PCM to Linear");
ConvertFile2Linear(g_TestInfo.nECRecChan+nGroup);
}
// ECR Channel skip conversion if didn't record
if(g_TestInfo.bECRRecord) {
CHECKENDTESTFLAG;
OUTPUT(g_TestInfo.nECRStartChan+nGroup,"Converting EC Chan vox PCM to Linear");
ConvertFile2Linear(g_TestInfo.nECRStartChan+nGroup);
CHECKENDTESTFLAG;
}
// Compute the energy of the recorded files
if(g_TestInfo.bNoECRecord) {
OUTPUT(g_TestInfo.nNoECRecChan+nGroup,"Computing energy of No EC linear vox");
fNoECREnergy = ComputeEnergy(g_TestInfo.nNoECRecChan+nGroup); // Standard vox file
CHECKENDTESTFLAG;
}
// EC Record channel tslot
if(g_TestInfo.bECRecChan) {
OUTPUT(g_TestInfo.nECRecChan+nGroup,"Computing energy of EC linear vox");
fECREnergy = ComputeEnergy(g_TestInfo.nECRecChan+nGroup); // ECR vox file
CHECKENDTESTFLAG;
}
// EC Channel Record tslot
if(g_TestInfo.bECRRecord) {
OUTPUT(g_TestInfo.nECRStartChan+nGroup,"Converting EC Chan vox PCM to Linear");
fECRChanEnergy = ComputeEnergy(g_TestInfo.nECRStartChan+nGroup); // ECR chan vox file
CHECKENDTESTFLAG;
}
// compare the energy in the vox files 99% lower if allowed
if(g_TestInfo.bNoECRecord && g_TestInfo.bECRecChan) {
OUTPUT(0,"Comparing Energy EC Tslot");
CHECKENDTESTFLAG;
if(CompareEnergy(fNoECREnergy, fECREnergy)>0) {
sprintf(szBuffer,"LP: %06d GROUP: %02d Invalid Energy Record Tslot level",g_TestInfo.nCurrLoop,nGroup);
LOGERROR(0,szBuffer);
}
}
// Compare energy of EC Record and noise
if(g_TestInfo.bNoECRecord && g_TestInfo.bECRRecord) {
OUTPUT(0,"Comparing Energy ECChan");
CHECKENDTESTFLAG;
if(CompareEnergy(fNoECREnergy, fECRChanEnergy)>0) {
sprintf(szBuffer,"LP: %06d GROUP: %02d Invalid Energy EC Chan level",g_TestInfo.nCurrLoop,nGroup);
LOGERROR(0,szBuffer);
}
}
// Display EC rec channel energy
if(g_TestInfo.bECRecChan) {
CHECKENDTESTFLAG;
sprintf(szBuffer,"EC Energy: %10.0f,",fECREnergy);
OUTPUT(g_TestInfo.nECRecChan+nGroup,szBuffer);
}
// Display ECR Channel record
if(g_TestInfo.bECRRecord) {
CHECKENDTESTFLAG;
sprintf(szBuffer,"EC Chan Energy: %10.0f,",fECRChanEnergy);
OUTPUT(g_TestInfo.nECRStartChan+nGroup,szBuffer);
}
// Echo energy if available
CHECKENDTESTFLAG;
if(g_TestInfo.bNoECRecord) {
sprintf(szBuffer,"NO EC Energy: %10.0f",fNoECREnergy);
OUTPUT(g_TestInfo.nNoECRecChan+nGroup,szBuffer);
}
// Group, MsiIndex, ECRChanEnergy, ECRecEnergy, NOECEnergy, dB,
CHECKENDTESTFLAG;
if(g_TestInfo.bNoECRecord) {
sprintf(szBuffer,"%d, %d, %d, %10.0f, %10.0f, %10.0f, %4.0f, %4.0f",g_TestInfo.nCurrLoop,nGroup+1,g_ChanInfo[g_TestInfo.nStartChan+nGroup].nMSIIndex,fECRChanEnergy,fECREnergy,fNoECREnergy,(10*log10(fECREnergy/fNoECREnergy)),(10*log10(fECRChanEnergy/fNoECREnergy)));
}
else {
sprintf(szBuffer,"%d, %d, %d, %10.0f, %10.0f, 0, 0",g_TestInfo.nCurrLoop,nGroup+1,g_ChanInfo[g_TestInfo.nStartChan+nGroup].nMSIIndex,fECRChanEnergy,fECREnergy);
}
OUTPUT(0,"Logging Data ...");
LOGDATA(nGroup,szBuffer);
CHECKENDTESTFLAG;
}
// Disable ECR use dx_unlistenecr() and dx_unlisten
OUTPUT(0,"Unrouting Resources ...");
CHECKENDTESTFLAG;
UnRouteResources();
}
OUTPUT(0,"Test Completed!");
return(0);
}
///////////////////////////////////////////////////////////
// NAME: ExitTest
// DESCRIPTION:
//
//
//////////////////////////////////////////////////////////
static void ExitTest(DWORD dwExitType)
{
int nIndex;
// Unroute the MSI and Voice resources
if(dwExitType!=NORMTERM) {
OUTPUT(0,"Exiting Test ...");
}
// Free the play buffer
if(g_pszPlayBuff) {
free(g_pszPlayBuff);
g_pszPlayBuff = NULL;
}
// If errors exceed Max errors then exit on failure
if(g_TestInfo.nErrors>=g_TestInfo.nMaxErrors) {
MessageBox(NULL,"Exceeded MaxErrors freeze\nsystem to verify files","Error Information",MB_OK);
}
// Unroute the recources and prepare to exit
UnRouteResources();
// Stop all the channels synchronously
for(nIndex=1;nIndex<=MAXCHAN;nIndex++) {
if(g_ChanInfo[nIndex].nDevHandle != -1) {
// Stop the channel
dx_stopch(g_ChanInfo[nIndex].nDevHandle, EV_SYNC);
if(ATDX_STATE(g_ChanInfo[nIndex].nDevHandle)!=CS_IDLE) {
// Retry a stop channel
dx_stopch(g_ChanInfo[nIndex].nDevHandle, EV_SYNC);
}
// Set hook state to onhook if analog interface
if(g_ChanInfo[nIndex].nNetType == CT_NTANALOG) {
SetHook(nIndex, DX_ONHOOK);
}
// Close the channel
dx_close(g_ChanInfo[nIndex].nDevHandle);
}
}
// Unroute the MSI station sets
for(nIndex=1;nIndex<=24;nIndex++) {
if(ms_close(g_MsiInfo[nIndex].nDevHandle) == -1) {
// ERROR
LOGERROR(0,"ms_close FAILED");
}
}
// Set flag so tests can run again
RESETENDTESTFLAG;
// Exit the thread
if(dwExitType!=NORMTERM) {
OUTPUT(0,"Test Completed with Abnormal Termination");
}
_endthreadex(dwExitType);
}
///////////////////////////////////////////////////////////
// NAME: UnRouteResources
// DESCRIPTION: Does unlistens
//
//
//////////////////////////////////////////////////////////
int UnRouteResources(void)
{
int nIndex;
TRACE(0,"In UnRouteResources");
// Unlisten and reset all channels
for(nIndex=1;nIndex<=MAXCHAN;nIndex++) {
if(g_ChanInfo[nIndex].nDevHandle!=-1) {
if(dx_unlisten(g_ChanInfo[nIndex].nDevHandle)==-1) {
LOGERROR(nIndex,"Failed dx_unlisten");
}
// Unlistenecr should reset the EC Resource
if(dx_unlistenecr(g_ChanInfo[nIndex].nDevHandle)==-1) {
LOGERROR(nIndex,"Failed dx_unlistenecr");
}
}
}
// Unroute the MSI station sets
for(nIndex=1;nIndex<=16;nIndex++) {
if(ms_unlisten(g_MsiInfo[nIndex].nDevHandle) == -1) {
// ERROR
LOGERROR(0,"ms_unlisten FAILED");
}
}
return(0);
}
///////////////////////////////////////////////////////////
// NAME: RouteResources
// DESCRIPTION:
//
//
//////////////////////////////////////////////////////////
int RouteResources(int nSeed)
{
int nIndex, nMSIIndex, nMSIBrd;
int nVoxRes1, nVoxRes2, nVoxRes3, nVoxRes4;
char szBuffer[80];
SC_TSINFO TsInfo;
TRACE(0,"In RouteResources");
// Initialize the starting vox channel indexes
nVoxRes1 = g_TestInfo.nStartChan;
nVoxRes2 = g_TestInfo.nECRStartChan;
nVoxRes3 = g_TestInfo.nECRecChan;
nVoxRes4 = g_TestInfo.nNoECRecChan;
for(nIndex=1;nIndex<=g_TestInfo.nTotalChan/4;nIndex++) {
// Adjust the MSI index to vary the impedance with each loop
nMSIBrd = (int)floor((nIndex-1)/24);
nMSIIndex = ((nIndex+(nSeed*4))%24);
if(nMSIIndex==0) {
nMSIIndex = 24;
}
g_ChanInfo[nVoxRes1].nMSIIndex = nMSIIndex+(nMSIBrd*24);
g_ChanInfo[nVoxRes2].nMSIIndex = nMSIIndex+(nMSIBrd*24);
g_ChanInfo[nVoxRes3].nMSIIndex = nMSIIndex+(nMSIBrd*24);
g_ChanInfo[nVoxRes4].nMSIIndex = nMSIIndex+(nMSIBrd*24);
///////////////////// ROUTE 1 //////////////////////////
TsInfo.sc_numts = 1;
TsInfo.sc_tsarrayp = &g_ChanInfo[nVoxRes1].nXmitSlot;
// Resource(1) to MSI
if(ms_listen(g_MsiInfo[nMSIIndex].nDevHandle,&TsInfo) == -1){
// ERROR
LOGERROR(0,"ms_listen FAILED");
}
sprintf(szBuffer,"MSI Listening to VOX tslot %d",g_ChanInfo[nVoxRes1].nXmitSlot);
TRACE(nMSIIndex,szBuffer);
TsInfo.sc_numts = 1;
TsInfo.sc_tsarrayp = &g_ChanInfo[nVoxRes2].nXmitSlot;
// MSI to Resource(1);
if(dx_listen(g_ChanInfo[nVoxRes1].nDevHandle,&TsInfo) == -1) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -