//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 05/11/2003 // PROJECT: // SUBSYSTEM: // FILE: LgVoiceModemCallCenter.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of LgVoiceModemCallCenter.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include "LgVoiceModemCallCenter.h" #define MESSAGE_MAX_TIMEOUT 60000UL // 60 seconds CLgVoiceModemCallCenter::CLgVoiceModemCallCenter() { memset( m_pEvent, 0, sizeof(m_pEvent) ); m_eState = STATE_NONE; m_pHandler = NULL; m_nDevice = 0; LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgVoiceModemCallCenter::CLgVoiceModemCallCenter" ); } CLgVoiceModemCallCenter::~CLgVoiceModemCallCenter() { Destroy(); LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgVoiceModemCallCenter::~CLgVoiceModemCallCenter" ); } BOOL CLgVoiceModemCallCenter::Create( INT nDevice, CLgVoiceModemCallCenterHandler *pHandler ) { // create command event for( INT i = 0; i < EVENT_COUNT; i++ ) { m_pEvent[i] = ::CreateEvent( NULL, FALSE, FALSE, NULL ); if( !m_pEvent[i] ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgVoiceModemCallCenter::Create", "CreateEvent failed with GetLastError %d", GetLastError() ); return FALSE; } } // store handler m_pHandler = pHandler; // store device id m_nDevice = nDevice; if( !CLgVoiceModem::Create() ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgVoiceModemCallCenter::Create", "CLgVoiceModem::Create failed" ); return FALSE; } // start thread return Start( FALSE ); } void CLgVoiceModemCallCenter::Destroy() { // stop thread Stop( TRUE ); //stop modem CLgVoiceModem::Destroy(); // close events for( INT i = 0; i < EVENT_COUNT; i++ ) { if( m_pEvent[i] ) { CloseHandle( m_pEvent[i] ); m_pEvent[i] = NULL; } } } BOOL CLgVoiceModemCallCenter::Call( LPCTSTR pNumber, DWORD dwCallDialTime, DWORD dwCallDropTime, INT nCallRepeatCount, LPCTSTR pAccessCode, LPCTSTR pCancelCode, E_VOICE_TYPE eWelcomeType, LPCTSTR pWelcomeData, E_VOICE_TYPE eMessageType, LPCTSTR pMessageData, INT nMessageRepeatCount, E_PRIORITY ePriority ) { MODEM_COMMAND CallCommand; CallCommand.ePriority = ePriority; _tcscpy( CallCommand.pPhoneNumber, pNumber ); CallCommand.dwPhoneDialTime = dwCallDialTime; CallCommand.dwPhoneDropTime = dwCallDropTime; CallCommand.nPhoneRedialCount = nCallRepeatCount; _tcscpy( CallCommand.pPhoneAccessCode, pAccessCode ); _tcscpy( CallCommand.pPhoneCancelCode, pCancelCode ); CallCommand.ePhoneWelcomeType = eWelcomeType; _tcscpy( CallCommand.pPhoneWelcomeData, pWelcomeData ); CallCommand.ePhoneMessageType = eMessageType; _tcscpy( CallCommand.pPhoneMessageData, pMessageData ); CallCommand.nPhoneMessageRepeatCount = nMessageRepeatCount; return Call( &CallCommand ); } BOOL CLgVoiceModemCallCenter::Call( PMODEM_COMMAND pCallCommand ) { // insert into command array ( take into acount priority ) CLgLocker Lock( &m_CommandQueue ); BOOL bInserted = FALSE; for( INT i = 0; i < m_CommandQueue.GetCount(); i++ ) { if( pCallCommand->ePriority > m_CommandQueue[i].ePriority ) { m_CommandQueue.InsertAt( i, *pCallCommand ); bInserted = TRUE; break; } } if( !bInserted ) { m_CommandQueue.Add( *pCallCommand ); } // log info LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgVoiceModemCallCenter::Call", "New call added <%s>", pCallCommand->pPhoneNumber ); // notify thread about new command return ::SetEvent( m_pEvent[EVENT_COMMAND] ); } BOOL CLgVoiceModemCallCenter::CancelAll() { // log info LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgVoiceModemCallCenter::CancelAll" ); // insert into command array ( take into acount priority ) CLgLocker Lock( &m_CommandQueue ); // remove all call tasks m_CommandQueue.RemoveAll(); // notify thread about canceling return ::SetEvent( m_pEvent[EVENT_CANCEL_ALL] ); } DWORD CLgVoiceModemCallCenter::ThreadProc() { __try { HANDLE pEvent[] = { GetThreadStopEvent(), m_pEvent[EVENT_COMMAND], m_pEvent[EVENT_CONNECT], m_pEvent[EVENT_DIGIT], m_pEvent[EVENT_WAVE], m_pEvent[EVENT_DISCONNECT], m_pEvent[EVENT_BUSY], m_pEvent[EVENT_CANCEL_ALL] }; INT nEventCount = sizeof(pEvent) / sizeof( *pEvent ); DWORD dwTimeout = INFINITE; BOOL bLoop = TRUE; while( bLoop ) { DWORD dwTime = ::GetTickCount(); switch( ::WaitForMultipleObjects( nEventCount, pEvent, FALSE, dwTimeout ) ) { default: case WAIT_FAILED: LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgVoiceModemCallCenter::ThreadProc", "WaitForMultipleObjects failed" ); case WAIT_OBJECT_0: bLoop = FALSE; continue; case WAIT_TIMEOUT: dwTimeout = OnWaitTimeout(); break; case WAIT_OBJECT_0+1: dwTime = ::GetTickCount() - dwTime; dwTimeout = OnEventCommand( ( dwTimeout > dwTime ) ? dwTimeout - dwTime : 0 ); break; case WAIT_OBJECT_0+2: dwTime = ::GetTickCount() - dwTime; dwTimeout = OnEventConnect( ( dwTimeout > dwTime ) ? dwTimeout - dwTime : 0 ); break; case WAIT_OBJECT_0+3: dwTime = ::GetTickCount() - dwTime; dwTimeout = OnEventDigit( ( dwTimeout > dwTime ) ? dwTimeout - dwTime : 0 ); break; case WAIT_OBJECT_0+4: dwTime = ::GetTickCount() - dwTime; dwTimeout = OnEventWave( ( dwTimeout > dwTime ) ? dwTimeout - dwTime : 0 ); break; case WAIT_OBJECT_0+5: dwTime = ::GetTickCount() - dwTime; dwTimeout = OnEventDisconnect( ( dwTimeout > dwTime ) ? dwTimeout - dwTime : 0 ); break; case WAIT_OBJECT_0+6: dwTime = ::GetTickCount() - dwTime; dwTimeout = OnEventBusy( ( dwTimeout > dwTime ) ? dwTimeout - dwTime : 0 ); break; case WAIT_OBJECT_0+7: dwTime = ::GetTickCount() - dwTime; dwTimeout = OnEventCancelAll( ( dwTimeout > dwTime ) ? dwTimeout - dwTime : 0 ); break; } } // stop wave CLgVoiceModem::Drop(); } __except( EXCEPTION_EXECUTE_HANDLER ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgVoiceModemCallCenter::ThreadProc"), _T("Exception 0x%08X"), _exception_code() ); } return 0UL; } DWORD CLgVoiceModemCallCenter::OnEventCommand( DWORD dwTimeout ) { // log info LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgVoiceModemCallCenter::OnEventCommand") ); // insert into command array ( take into acount priority ) CLgLocker Lock( &m_CommandQueue ); // get first command PMODEM_COMMAND pCallComand = &m_CommandQueue[0]; // if command is performing if( m_eState != STATE_NONE ) { // if new command with priority less or equal with current if( pCallComand->ePriority <= m_Command.ePriority ) { return dwTimeout; } // mark as availible to perform new call m_eState = STATE_DROP; } // process new command return OnWaitTimeout(); } DWORD CLgVoiceModemCallCenter::OnWaitTimeout() { LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgVoiceModemCallCenter::OnWaitTimeout") ); // lock command queue CLgLocker Lock( &m_CommandQueue ); switch( m_eState ) { case STATE_WELCOME: // cancel current call & voice CLgVoiceModem::Drop(); // mark state as no action m_eState = STATE_WELCOME_DROP; // pause between calls return m_Command.dwPhoneDropTime; case STATE_WELCOME_DROP: // if nessessary to perform recall if( m_Command.nPhoneRedialCount ) { m_Command.nPhoneRedialCount--; // log LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgVoiceModemCallCenter::OnWaitTimeout", "Redial %d number <%s>", m_Command.nPhoneRedialCount, m_Command.pPhoneNumber ); // dial number if( !CLgVoiceModem::Dial( m_nDevice, m_Command.pPhoneNumber ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgVoiceModemCallCenter::OnWaitTimeout", "Can not dial" ); return 5UL; } // mark state as welcome m_eState = STATE_WELCOME; // return call processing time return m_Command.dwPhoneDialTime; } // mark state as no action m_eState = STATE_NONE; return 0; case STATE_DROP: // cancel current call & voice CLgVoiceModem::Drop(); // mark state as no action m_eState = STATE_NONE; // pause between calls return m_Command.dwPhoneDropTime; case STATE_NONE: // check command queue if( !m_CommandQueue.GetCount() ) { LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgVoiceModemCallCenter::OnWaitTimeout", "Command queue is empty" ); return INFINITE; } // store new command m_Command = m_CommandQueue[0]; // remove command from list m_CommandQueue.Remove(0); // log LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgVoiceModemCallCenter::OnWaitTimeout", "Dial new number <%s>", m_Command.pPhoneNumber ); // dial number if( !CLgVoiceModem::Dial( m_nDevice, m_Command.pPhoneNumber ) ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgVoiceModemCallCenter::OnWaitTimeout", "Can not dial" ); return 5UL; } // mark state as welcome m_eState = STATE_WELCOME; // return call processing time return m_Command.dwPhoneDialTime; case STATE_MESSAGE: // mark state as no action m_eState = STATE_DROP; LGLOG( LOG_LEVEL_LOW, LOG_TYPE_WARNING, this, "CLgVoiceModemCallCenter::OnWaitTimeout", "Unexpected state" ); return 0; } return INFINITE; } DWORD CLgVoiceModemCallCenter::OnEventConnect( DWORD dwTimeout ) { // log info LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgVoiceModemCallCenter::OnEventConnect") ); switch( m_Command.ePhoneWelcomeType ) { case VOICE_TYPE_NONE: break; case VOICE_TYPE_WAVE: // start welcome wave if( !CLgVoiceModem::Play( m_Command.pPhoneWelcomeData ) ) { // log error LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgVoiceModemCallCenter::OnEventConnect", "Can't play welcome wave" ); } break; case VOICE_TYPE_TEXT2SPEECH: // start welcome text if( !CLgVoiceModem::Speak( m_Command.pPhoneWelcomeData, m_Command.nPhoneWelcomeText2SpeechVolume, m_Command.nPhoneWelcomeText2SpeechRate, m_Command.pPhoneWelcomeText2SpeechVoice ) ) { // log error LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgVoiceModemCallCenter::OnEventConnect", "Can't speak welcome text" ); } break; } // current state is welcome playing state m_eState = STATE_WELCOME; return dwTimeout; } DWORD CLgVoiceModemCallCenter::OnEventDigit( DWORD dwTimeout ) { // log info LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgVoiceModemCallCenter::OnEventDigit") ); if( m_eState != STATE_WELCOME ) return dwTimeout; // stop welcome wave (if any) CLgVoiceModem::ResetWave(); // stop welcome text (if any) CLgVoiceModem::ResetSpeech(); // current state is message playing state m_eState = STATE_MESSAGE; switch( m_Command.ePhoneMessageType ) { case VOICE_TYPE_NONE: break; case VOICE_TYPE_WAVE: // start message wave if( !CLgVoiceModem::Play( m_Command.pPhoneMessageData ) ) { // log error LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgVoiceModemCallCenter::OnEventDigit", "Can't play message wave" ); // cancel playing m_eState = STATE_DROP; dwTimeout = 0; } else dwTimeout = MESSAGE_MAX_TIMEOUT; // maximal message time break; case VOICE_TYPE_TEXT2SPEECH: // start message text if( !CLgVoiceModem::Speak( m_Command.pPhoneMessageData, m_Command.nPhoneMessageText2SpeechVolume, m_Command.nPhoneMessageText2SpeechRate, m_Command.pPhoneMessageText2SpeechVoice ) ) { // log error LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgVoiceModemCallCenter::OnEventDigit", "Can't speak message text" ); // cancel playing m_eState = STATE_DROP; dwTimeout = 0; } else dwTimeout = MESSAGE_MAX_TIMEOUT; // maximal message time break; } return dwTimeout; } DWORD CLgVoiceModemCallCenter::OnEventWave( DWORD dwTimeout ) { // log info LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgVoiceModemCallCenter::OnEventWave") ); switch( m_eState ) { case STATE_WELCOME: switch( m_Command.ePhoneWelcomeType ) { case VOICE_TYPE_NONE: break; case VOICE_TYPE_WAVE: // play wave again and again if( !CLgVoiceModem::Play( m_Command.pPhoneWelcomeData ) ) LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgVoiceModemCallCenter::OnEventWave", "Can't play welcome wave" ); break; case VOICE_TYPE_TEXT2SPEECH: // speak text again and again if( !CLgVoiceModem::Speak( m_Command.pPhoneWelcomeData, m_Command.nPhoneWelcomeText2SpeechVolume, m_Command.nPhoneWelcomeText2SpeechRate, m_Command.pPhoneWelcomeText2SpeechVoice ) ) LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgVoiceModemCallCenter::OnEventWave", "Can't speak welcome text" ); break; } break; case STATE_MESSAGE: switch( m_Command.ePhoneMessageType ) { case VOICE_TYPE_NONE: break; case VOICE_TYPE_WAVE: // play only requared number of times if( !CLgVoiceModem::Play( m_Command.pPhoneMessageData ) ) { // log error LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgVoiceModemCallCenter::OnEventWave", "Can't play message wave" ); // cancel playing m_eState = STATE_DROP; dwTimeout = 0; } else { // if it needs to play more times if( m_Command.nPhoneMessageRepeatCount-- ) dwTimeout = MESSAGE_MAX_TIMEOUT; else { // stop playing m_eState = STATE_DROP; dwTimeout = 0; } } break; case VOICE_TYPE_TEXT2SPEECH: // speak only requared number of times if( !CLgVoiceModem::Speak( m_Command.pPhoneMessageData, m_Command.nPhoneMessageText2SpeechVolume, m_Command.nPhoneMessageText2SpeechRate, m_Command.pPhoneMessageText2SpeechVoice ) ) { // log error LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, "CLgVoiceModemCallCenter::OnEventWave", "Can't speak message text" ); // cancel playing m_eState = STATE_DROP; dwTimeout = 0; } else { // if it needs to play more times if( m_Command.nPhoneMessageRepeatCount-- ) dwTimeout = MESSAGE_MAX_TIMEOUT; else { // stop playing m_eState = STATE_DROP; dwTimeout = 0; } } break; } break; } return dwTimeout; } DWORD CLgVoiceModemCallCenter::OnEventDisconnect( DWORD dwTimeout ) { // log info LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgVoiceModemCallCenter::OnEventDisconnect") ); switch( m_eState ) { case STATE_NONE: break; case STATE_WELCOME: // set timeout to 0 for immidiate caling OnTimeout for redial dwTimeout = 0; break; case STATE_MESSAGE: // finish current call m_eState = STATE_DROP; // set timeout to 0 for immidiate caling OnTimeout for new call establishing dwTimeout = 0; break; } return dwTimeout; } DWORD CLgVoiceModemCallCenter::OnEventCancelAll( DWORD dwTimeout ) { // log info LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgVoiceModemCallCenter::OnEventCancelAll") ); switch( m_eState ) { case STATE_WELCOME_DROP: case STATE_WELCOME: case STATE_MESSAGE: // finish current call m_eState = STATE_DROP; case STATE_NONE: // set timeout to 0 for immidiate caling OnTimeout for redial dwTimeout = 0; break; } return dwTimeout; } DWORD CLgVoiceModemCallCenter::OnEventBusy( DWORD dwTimeout ) { // log info LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgVoiceModemCallCenter::OnEventBusy") ); return OnEventDisconnect( dwTimeout ); } void CLgVoiceModemCallCenter::OnModemProceeding() { // call base class CLgVoiceModem::OnModemProceeding(); // set dial monitoring CLgVoiceModem::MonitorDigits( TRUE ); } void CLgVoiceModemCallCenter::OnModemConnect() { // call base class CLgVoiceModem::OnModemConnect(); // notify ::SetEvent( m_pEvent[EVENT_CONNECT] ); } void CLgVoiceModemCallCenter::OnModemDigit( INT nDigit ) { // call base class CLgVoiceModem::OnModemDigit( nDigit ); // notify ::SetEvent( m_pEvent[EVENT_DIGIT] ); } void CLgVoiceModemCallCenter::OnModemBusy() { // call base class CLgVoiceModem::OnModemBusy(); // notify ::SetEvent( m_pEvent[EVENT_BUSY] ); } void CLgVoiceModemCallCenter::OnWaveDone( BOOL bReset ) { // call base class CLgVoiceModem::OnWaveDone( bReset ); // notify if( !bReset ) ::SetEvent( m_pEvent[EVENT_WAVE] ); } void CLgVoiceModemCallCenter::OnSpeechEndInputStream() { // call base class CLgVoiceModem::OnSpeechEndInputStream(); ::SetEvent( m_pEvent[EVENT_WAVE] ); }