phonesrv_plat/call_information_api/tsrc/src/CPEMessageWaiter.cpp
changeset 0 ff3b6d0fd310
child 19 7d48bed6ce0c
equal deleted inserted replaced
-1:000000000000 0:ff3b6d0fd310
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 #include "CPEMessageWaiter.h"
       
    18 
       
    19 #include <EUnitMacros.h>
       
    20 
       
    21 #include "talogger.h"
       
    22 #include "CActiveWait.h"
       
    23 
       
    24 
       
    25 _LIT( KPanic, "CPEMessageWaiter" );
       
    26 enum TPanic 
       
    27     {
       
    28     EArrivedMessagesOOM
       
    29     };
       
    30 
       
    31 // Max time msg will be waited. If msg does not arrive in this time
       
    32 // test will fail.
       
    33 const TInt KDefaultMsgWaitingTimeout = 5000000; 
       
    34 
       
    35 CPEMessageWaiter* CPEMessageWaiter::NewL()
       
    36     {
       
    37     CPEMessageWaiter* self = new( ELeave )CPEMessageWaiter();
       
    38     CleanupStack::PushL( self );
       
    39     self->ConstructL();
       
    40     CleanupStack::Pop( self );
       
    41     return self;
       
    42     }
       
    43     
       
    44 CPEMessageWaiter::~CPEMessageWaiter()
       
    45     {
       
    46     Cancel();
       
    47     delete iWait;
       
    48     iArrivedMsgs.Close();
       
    49     }
       
    50     
       
    51 CPEMessageWaiter::CPEMessageWaiter() : CTimer( CActive::EPriorityStandard )
       
    52     {
       
    53     CActiveScheduler::Add( this );
       
    54     }
       
    55     
       
    56 void CPEMessageWaiter::ConstructL()
       
    57     {
       
    58     CTimer::ConstructL();
       
    59     iWait = new( ELeave )CActiveSchedulerWait();
       
    60     }
       
    61     
       
    62 void CPEMessageWaiter::WaitForMsg( 
       
    63     MEngineMonitor::TPEMessagesFromPhoneEngine aMsg,
       
    64     TInt aTimeoutInSeconds)
       
    65     {
       
    66     TEFLOGSTRING2( KTAREQEND, "CPEMessageWaiter::WaitForMsg, aMessage %d", aMsg );
       
    67     
       
    68     iWaitedMsgArrived = EFalse;
       
    69     iWaitedMsg = aMsg;
       
    70     
       
    71     After( aTimeoutInSeconds * 1000000 ); // Start timeout 
       
    72     iWait->Start();
       
    73     Cancel(); // Cancel the timer.
       
    74     
       
    75     if( !iWaitedMsgArrived )
       
    76         {
       
    77         TEFLOGSTRING2( KTAREQEND, 
       
    78            "CPEMessageWaiter::WaitForMsg, waited for message %d, and message did not arrive in timeout time", iWaitedMsg );
       
    79         EUNIT_FAIL_TEST( "TIMEOUT: Did not receive waited PE message");      
       
    80         }
       
    81     }
       
    82     
       
    83 void CPEMessageWaiter::WaitL(
       
    84     TInt aTimeoutInSeconds )
       
    85     {
       
    86     RTimer timer;
       
    87     User::LeaveIfError( timer.CreateLocal() ); 
       
    88     CleanupClosePushL( timer );
       
    89     CActiveWait* wait = CActiveWait::NewLC();
       
    90     timer.After( wait->Status(), aTimeoutInSeconds * 1000000 );
       
    91     wait->WaitForRequest();
       
    92     CleanupStack::PopAndDestroy( 2 ); // timer, wait*/
       
    93     }
       
    94     
       
    95 TBool CPEMessageWaiter::HasMsgArrived( 
       
    96     MEngineMonitor::TPEMessagesFromPhoneEngine aMsg )
       
    97     {
       
    98     return iArrivedMsgs.Find( aMsg ) != KErrNotFound;
       
    99     }
       
   100 
       
   101 void CPEMessageWaiter::WaitForError( TInt aError )
       
   102     {
       
   103     TEFLOGSTRING2( KTAREQEND, "CPEMessageWaiter::WaitForError, aMessage %d", aError );
       
   104     
       
   105     iWaitedErrorCodeArrived = EFalse;
       
   106     iWaitedErrorCode = aError;
       
   107     
       
   108     After( KDefaultMsgWaitingTimeout ); // Start timeout 
       
   109     iWait->Start();
       
   110     Cancel(); // Cancel the timer.
       
   111     
       
   112     if( !iWaitedErrorCodeArrived )
       
   113         {
       
   114         TEFLOGSTRING2( KTAREQEND, 
       
   115            "CPEMessageWaiter::WaitForError, waited for Error %d, and Error did not arrive in timeout time", iWaitedMsg );
       
   116         EUNIT_FAIL_TEST( "TIMEOUT: Did not receive waited PE Error");      
       
   117         }
       
   118     }
       
   119 
       
   120 void CPEMessageWaiter::HandleError( 
       
   121     const TPEErrorInfo& aErrorInfo )
       
   122     {
       
   123     TEFLOGSTRING2( KTAREQEND, "CPEMessageWaiter::HandleMessage, aMessage %d", aErrorInfo.iErrorCode );
       
   124     TEFLOGSTRING2( KTAREQEND, "CPEMessageWaiter::WaitForMsg, waited msg %d", iWaitedErrorCode );
       
   125     
       
   126     iLastErrorCode = aErrorInfo.iErrorCode;
       
   127     
       
   128     if( iWait->IsStarted() )
       
   129         {
       
   130         if( aErrorInfo.iErrorCode == iWaitedErrorCode && aErrorInfo.iErrorCode  )
       
   131             {
       
   132             TEFLOGSTRING( KTAREQEND, 
       
   133                 "CPEMessageWaiter::HandleError: Waited ErrorCode received );");
       
   134             iWaitedErrorCodeArrived = ETrue;
       
   135             iWait->AsyncStop();
       
   136             }
       
   137         }
       
   138     }
       
   139 
       
   140 void CPEMessageWaiter::HandleMessage( 
       
   141     const TInt aMessage, 
       
   142     const TInt /*aCallId*/ )
       
   143     {
       
   144     TEFLOGSTRING2( KTAREQEND, "CPEMessageWaiter::HandleMessage, aMessage %d", aMessage );
       
   145     TEFLOGSTRING2( KTAREQEND, "CPEMessageWaiter::WaitForMsg, waited msg %d", iWaitedMsg );
       
   146     __ASSERT_ALWAYS( iArrivedMsgs.Append( static_cast<MEngineMonitor::TPEMessagesFromPhoneEngine>( aMessage ) ) == KErrNone, User::Panic( KPanic, EArrivedMessagesOOM ) );
       
   147     
       
   148     if( iWait->IsStarted() )
       
   149         {
       
   150         if( aMessage == iWaitedMsg )
       
   151             {
       
   152             TEFLOGSTRING( KTAREQEND, 
       
   153                 "CPEMessageWaiter::HandleMessage: Waited message received");
       
   154             iWaitedMsgArrived = ETrue;
       
   155             iWait->AsyncStop();
       
   156             }
       
   157         }
       
   158     }
       
   159 
       
   160 void CPEMessageWaiter::RunL()
       
   161     {
       
   162     if( iWait->IsStarted() )
       
   163         {
       
   164         iWait->AsyncStop();
       
   165         }     
       
   166     }
       
   167 
       
   168 TInt CPEMessageWaiter::LastErrorCode()
       
   169     {
       
   170     return iLastErrorCode;
       
   171     }
       
   172