sysstatemgmt/systemstatemgr/test/tcmn/src/t_stateawaresessionwrapper.cpp
changeset 76 cb32bcc88bad
equal deleted inserted replaced
73:d38941471f1c 76:cb32bcc88bad
       
     1 // Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 //Contributors:
       
    12 //
       
    13 // Description: Test wrapper for RSsmStateAwareSession
       
    14 
       
    15 #include <e32std.h>
       
    16 #include <e32capability.h>
       
    17 #include <test/datawrapper.h>
       
    18 #include <ssm/ssmdomaindefs.h> 
       
    19 #include <ssm/ssmsubstates.hrh>
       
    20 #include <ssm/startupdomainpskeys.h>
       
    21 #include <e32debug.h>
       
    22 
       
    23 #include "t_stateawaresessionwrapper.h"
       
    24 
       
    25 //
       
    26 _LIT(KDomainId,                  "domainId"); 
       
    27 _LIT(KAcknowledge,               "acknowledge");
       
    28 _LIT(KError,                     "error");
       
    29 _LIT(KCancelDeferral,            "cancelDeferral");
       
    30 _LIT(KCount,                     "count"); 
       
    31 _LIT(KAckLastState,              "ackLastState");
       
    32 _LIT(KDeferTwice,                "deferTwice"); 
       
    33 
       
    34 //commands
       
    35 _LIT(KCmdNewL,                                      "NewL");
       
    36 _LIT(KCmdConnect,                                   "Connect");
       
    37 _LIT(KCmdReadIniData,                               "ReadIniData");
       
    38 _LIT(KCmdRequestStateNotification,                  "RequestStateNotification");
       
    39 _LIT(KCmdAcknowledgeStateNotification,              "AcknowledgeStateNotification");
       
    40 _LIT(KCmdClose,                                     "Close");
       
    41 _LIT(KCmdRequestStateNotificationCancel,            "RequestStateNotificationCancel");
       
    42 _LIT(KCmdAcknowledgeAndRequestStateNotification,    "AcknowledgeAndRequestStateNotification");
       
    43 _LIT(KCmdDeferAcknowledgement,                      "DeferAcknowledgement");
       
    44 _LIT(KCmdReturnCount,                               "ReturnCount");
       
    45 _LIT(KCmdDestructor,                                "~");
       
    46 
       
    47 /**
       
    48  * Helper method to map the DomainIds.
       
    49  */
       
    50 static TBool MapToDomainId(TPtrC& aDomainIdName, TDmDomainId& aDomainId);
       
    51 
       
    52 /**
       
    53  * Constructor
       
    54  */
       
    55 TIniData1::TIniData1()
       
    56         : iCancelDeferral(EFalse)
       
    57         , iAckLastState(EFalse)
       
    58         , iDeferTwice(EFalse)
       
    59         , iCount(0)
       
    60         , iAcknowledge(ETrue)
       
    61         {}
       
    62 
       
    63 /**
       
    64  * Constructor
       
    65  */
       
    66 TIniData2::TIniData2()
       
    67         : iCount(0)
       
    68         , iAcknowledge(ETrue)
       
    69         {}
       
    70 
       
    71 /**
       
    72  * Two phase constructor
       
    73  */
       
    74 CTestRStateAwareSession1* CTestRStateAwareSession1::NewL()
       
    75     {
       
    76     CTestRStateAwareSession1*  testStateAwareSession = new (ELeave) CTestRStateAwareSession1();
       
    77     CleanupStack::PushL(testStateAwareSession);
       
    78     testStateAwareSession->ConstructL();
       
    79     CleanupStack::Pop(testStateAwareSession);
       
    80     return testStateAwareSession; 
       
    81     }
       
    82 
       
    83 /**
       
    84 * @return pointer to the object that the data wraps
       
    85 */
       
    86 TAny*  CTestRStateAwareSession1::GetObject()
       
    87     {
       
    88     return iSsmStateAwareSession;
       
    89     }
       
    90 
       
    91 /**
       
    92  * SetObjectL
       
    93  */
       
    94 void CTestRStateAwareSession1::SetObjectL(TAny* aAny)
       
    95     {
       
    96     DoCleanup();
       
    97     iSsmStateAwareSession=static_cast<RSsmStateAwareSession*> (aAny);
       
    98     }
       
    99 
       
   100 /**
       
   101  * DisownObjectL
       
   102  */
       
   103 void CTestRStateAwareSession1::DisownObjectL()
       
   104     {
       
   105     iSsmStateAwareSession=NULL;
       
   106     }
       
   107 
       
   108 /**
       
   109  * Protected constructor. First phase construction
       
   110  */
       
   111 CTestRStateAwareSession1::CTestRStateAwareSession1():
       
   112 CDataWrapper()
       
   113     {
       
   114     }
       
   115 
       
   116 /**
       
   117  * Protected constructor. Second phase construction
       
   118  */
       
   119 void CTestRStateAwareSession1::ConstructL()
       
   120     {
       
   121     iActiveNotifyOnChange = CActiveCallback::NewL(*this);
       
   122     iActiveNotifyOnChange2 = CActiveCallback::NewL(*this);
       
   123     }
       
   124 
       
   125 /**
       
   126  * Destructor.
       
   127  */
       
   128 CTestRStateAwareSession1::~CTestRStateAwareSession1()
       
   129     {
       
   130     DoCleanup();
       
   131     delete iActiveNotifyOnChange;
       
   132     delete iActiveNotifyOnChange2;
       
   133     }
       
   134 
       
   135 /**
       
   136  * Process a command read from the ini file
       
   137  *
       
   138  * @param aCommand  the command to process
       
   139  * @param aSection      the entry in the ini file requiring the command to be processed
       
   140  *
       
   141  * @return ETrue if the command is processed
       
   142  */
       
   143 TBool CTestRStateAwareSession1::DoCommandL(const TTEFFunction& aCommand, const TTEFSectionName& aSection, const TInt aAsyncErrorIndex)
       
   144     {
       
   145     TBool retVal = ETrue;
       
   146     if (aCommand == KCmdNewL)
       
   147         {
       
   148         DoCmdNew();
       
   149         }
       
   150     else if (aCommand == KCmdConnect)
       
   151         {
       
   152         DoCmdConnect(aSection);
       
   153         }
       
   154     else if (aCommand == KCmdReadIniData)
       
   155         {
       
   156         DoCmdReadIniData(aSection);
       
   157         }
       
   158     else if (aCommand == KCmdRequestStateNotification)
       
   159         {
       
   160         DoCmdRequestStateNotification(aAsyncErrorIndex);
       
   161         }
       
   162     else if (aCommand == KCmdAcknowledgeStateNotification)
       
   163         {
       
   164         DoCmdAcknowledgeStateNotification(aSection);
       
   165         }
       
   166     else if (aCommand == KCmdClose)
       
   167         {
       
   168         DoCmdClose();
       
   169         }
       
   170     else if (aCommand == KCmdRequestStateNotificationCancel)
       
   171         {
       
   172         DoCmdRequestStateNotificationCancel();
       
   173         }
       
   174     else if (aCommand == KCmdAcknowledgeAndRequestStateNotification)
       
   175         {
       
   176         DoCmdAcknowledgeAndRequestStateNotification(aSection, aAsyncErrorIndex);
       
   177         }
       
   178     else if (aCommand == KCmdDeferAcknowledgement)
       
   179         {
       
   180         DoCmdDeferAcknowledgement( aAsyncErrorIndex);
       
   181         }
       
   182     else if (aCommand == KCmdReturnCount)
       
   183         {
       
   184         DoCmdReturnCount(aSection);
       
   185         }
       
   186     else if (aCommand == KCmdDestructor)
       
   187         {
       
   188         DoCmdDestructor();
       
   189         }
       
   190     else
       
   191         {
       
   192         retVal = EFalse;
       
   193         }    
       
   194    
       
   195     return retVal;
       
   196     }
       
   197 
       
   198 /**
       
   199  * Creates RSSmStateAwareSession class instance
       
   200  */
       
   201 void CTestRStateAwareSession1::DoCmdNew()
       
   202     {
       
   203     INFO_PRINTF1(_L("CTestRStateAwareSession1: Create RSsmStateAwareSession class instance"));
       
   204     DoCleanup();
       
   205     
       
   206     TRAPD(err, iSsmStateAwareSession = new (ELeave)RSsmStateAwareSession());
       
   207     
       
   208     if (err != KErrNone)
       
   209         {
       
   210         ERR_PRINTF2(_L("CTestRStateAwareSession1: new error %d"), err);
       
   211         SetError(err);
       
   212         }      
       
   213     }
       
   214 
       
   215 /**
       
   216  * Close RSsmStateAwareSession handle
       
   217  */
       
   218 void CTestRStateAwareSession1::DoCmdClose()
       
   219     {
       
   220     INFO_PRINTF1(_L("CTestRStateAwareSession1: Close RSsmStateAwareSession"));
       
   221     iSsmStateAwareSession->Close();
       
   222     }
       
   223 
       
   224 /**
       
   225  * Contains cleanup implementation
       
   226  */
       
   227 void CTestRStateAwareSession1::DoCleanup()
       
   228     {
       
   229     if(iSsmStateAwareSession != NULL)
       
   230         {
       
   231         INFO_PRINTF1(_L("CTestRStateAwareSession1: Deleting current RSsmStateAwareSession"));
       
   232         delete iSsmStateAwareSession;
       
   233         iSsmStateAwareSession = NULL;
       
   234         }
       
   235     }
       
   236 
       
   237 /**
       
   238  * Destroy RSsmStateAwareSession object
       
   239  */
       
   240 void CTestRStateAwareSession1::DoCmdDestructor()
       
   241     {
       
   242     INFO_PRINTF1(_L("CTestRStateAwareSession1: Destroying the RStateAwareSession object"));
       
   243     DoCleanup();
       
   244     }
       
   245 
       
   246 /**
       
   247  * Check whether the client is deferred expected no. of times 
       
   248  */
       
   249 void CTestRStateAwareSession1::DoCmdReturnCount(const TDesC& aSection)
       
   250     {
       
   251     TInt expectedCount = 0;
       
   252     if ( !GetIntFromConfig(aSection, KCount(), expectedCount))
       
   253         {
       
   254         ERR_PRINTF1(_L("CTestRStateAwareSession1: Error reading ini file data"));
       
   255         SetError(KErrNotFound);        
       
   256         }
       
   257     if (iCount == expectedCount)
       
   258         return;
       
   259     ERR_PRINTF3(_L("CTestRStateAwareSession1: Actual count = %d, Expected count = %d"), iCount, expectedCount);
       
   260     SetError(KErrGeneral);
       
   261     }
       
   262 
       
   263 /*
       
   264  * Helper function to read ini file data
       
   265  */
       
   266 void CTestRStateAwareSession1::DoCmdReadIniData(const TDesC& aSection)
       
   267     {
       
   268     if( !GetIntFromConfig(aSection, KCount(), iIniData.iCount) ||
       
   269         !GetBoolFromConfig(aSection, KCancelDeferral(), iIniData.iCancelDeferral) ||
       
   270         !GetBoolFromConfig(aSection, KAckLastState(), iIniData.iAckLastState) ||
       
   271         !GetBoolFromConfig(aSection, KDeferTwice(), iIniData.iDeferTwice) )
       
   272         {
       
   273         ERR_PRINTF1(_L("CTestRStateAwareSession1: Error reading ini file data"));
       
   274         SetError(KErrNotFound);
       
   275         }
       
   276     }
       
   277 
       
   278 /**
       
   279  * Connects a client to RSsmStateAwareSession with Domain Id
       
   280  */
       
   281 void CTestRStateAwareSession1::DoCmdConnect(const TDesC& aSection)
       
   282     {
       
   283     TPtrC domainIdName;
       
   284     if (!GetStringFromConfig(aSection, KDomainId(), domainIdName))
       
   285         {
       
   286         //set default domainId if value is not provided in .ini file
       
   287         domainIdName.Set(_L("KSM2UiServicesDomain1"));        
       
   288         }
       
   289     TDmDomainId domainId;
       
   290     if(MapToDomainId(domainIdName, domainId ))
       
   291        {
       
   292        INFO_PRINTF2(_L("CTestRStateAwareSession1: Calling RSsmStateAwareSession->Connect with domain id %S"), &domainIdName );
       
   293        TInt  err = iSsmStateAwareSession->Connect(domainId);          
       
   294        if (KErrNone != err)
       
   295             {
       
   296             ERR_PRINTF2(_L("CTestRStateAwareSession1: Connect() error %d"), err);
       
   297             SetError(err);  
       
   298             }       
       
   299        } 
       
   300     else
       
   301         {
       
   302         ERR_PRINTF2(_L("CTestRStateAwareSession1: Could not map %S to domain Id"), &domainIdName );
       
   303         SetBlockResult(EFail);
       
   304         }
       
   305     }
       
   306 
       
   307 /**
       
   308  * DoCmdRequestStateNotification 
       
   309  */
       
   310 void CTestRStateAwareSession1::DoCmdRequestStateNotification(const TInt aAsyncErrorIndex)
       
   311     {
       
   312     INFO_PRINTF2(_L("CTestRStateAwareSession1: RequestStateNotification with Async Error Index: %d"), aAsyncErrorIndex);
       
   313     iSsmStateAwareSession->RequestStateNotification(iActiveNotifyOnChange->iStatus);
       
   314     iActiveNotifyOnChange->Activate(aAsyncErrorIndex);
       
   315     IncOutstanding();    
       
   316     }
       
   317 
       
   318 /**
       
   319  * DoCmdAcknowledgeStateNotification
       
   320  */
       
   321 void CTestRStateAwareSession1::DoCmdAcknowledgeStateNotification(const TDesC& aSection)
       
   322     {
       
   323     INFO_PRINTF1(_L("CTestRStateAwareSession1: AcknowledgeStateNotification"));
       
   324     TInt error;
       
   325     if (!GetIntFromConfig(aSection, KError(), error ))
       
   326         {
       
   327         //set default error value if value is not provided in .ini file
       
   328         error = KErrNone;
       
   329         }
       
   330    iSsmStateAwareSession->AcknowledgeStateNotification(error);    
       
   331     }
       
   332 
       
   333 /**
       
   334  * DoCmdAcknowledgeAndRequestStateNotification
       
   335  */
       
   336 void CTestRStateAwareSession1::DoCmdAcknowledgeAndRequestStateNotification(const TDesC& aSection, const TInt aAsyncErrorIndex)
       
   337     {
       
   338     INFO_PRINTF1(_L("CTestRStateAwareSession1: AcknowledgeAndRequestStateNotification"));
       
   339     TInt error;
       
   340     if (!GetIntFromConfig(aSection, KError(), error ))
       
   341         {
       
   342         //set default error value if value is not provided in .ini file
       
   343         error = KErrNone;
       
   344         }
       
   345     iSsmStateAwareSession->AcknowledgeAndRequestStateNotification(error, iActiveNotifyOnChange->iStatus );
       
   346     iActiveNotifyOnChange->Activate(aAsyncErrorIndex);
       
   347     IncOutstanding();        
       
   348     }
       
   349 
       
   350 /**
       
   351  * DoCmdRequestStateNotificationCancel
       
   352  */
       
   353 void CTestRStateAwareSession1::DoCmdRequestStateNotificationCancel()
       
   354     {
       
   355     INFO_PRINTF1(_L("CTestRStateAwareSession1: RequestStateNotificationCancel is called "));
       
   356     iSsmStateAwareSession->RequestStateNotificationCancel();
       
   357     }
       
   358 
       
   359 void CTestRStateAwareSession1::DoCmdDeferAcknowledgement(const TInt aAsyncErrorIndex)
       
   360     {
       
   361     INFO_PRINTF1(_L("CTestRStateAwareSession1: DeferAcknowledgement is called"));
       
   362     iSsmStateAwareSession->DeferAcknowledgement(iActiveNotifyOnChange->iStatus);
       
   363     iActiveNotifyOnChange->Activate(aAsyncErrorIndex);
       
   364     IncOutstanding();
       
   365     }
       
   366 
       
   367 void CTestRStateAwareSession1::RunL(CActive* aActive, TInt aIndex)
       
   368     {
       
   369     TInt error = KErrNone;
       
   370 
       
   371     if ( aActive == iActiveNotifyOnChange )
       
   372         {
       
   373         error = iActiveNotifyOnChange->iStatus.Int();
       
   374         switch ( error )
       
   375             {
       
   376             case KErrNone:
       
   377                 {  
       
   378                 //Request has completed i.e. The member must either defer again or acknowledge.
       
   379                 TSsmState currentState = iSsmStateAwareSession->State(); 
       
   380                 // This component only responds to Shutdown state events.
       
   381                 if ( (currentState.MainState() == ESsmShutdown) &&
       
   382                      (currentState.SubState() == ESsmShutdownSubStateCritical || currentState.SubState() == ESsmShutdownSubStateNonCritical) )
       
   383                     {
       
   384                     if( iCount < iIniData.iCount )
       
   385                         {
       
   386                         iCount++;
       
   387                         iSsmStateAwareSession->DeferAcknowledgement(iActiveNotifyOnChange->iStatus);
       
   388                         iActiveNotifyOnChange->Activate(aIndex);
       
   389                         IncOutstanding();                    
       
   390                         if( iIniData.iDeferTwice )
       
   391                             {
       
   392                             iCount++;
       
   393                             iSsmStateAwareSession->DeferAcknowledgement(iActiveNotifyOnChange2->iStatus);
       
   394                             iActiveNotifyOnChange2->Activate(aIndex);
       
   395                             IncOutstanding();
       
   396                             }
       
   397                         else if( iIniData.iAckLastState )
       
   398                             {
       
   399                             iSsmStateAwareSession->AcknowledgeStateNotification(KErrNone);
       
   400                             }
       
   401                         else if( iIniData.iCancelDeferral )
       
   402                             {
       
   403                             iSsmStateAwareSession->CancelDeferral();
       
   404                             }
       
   405                         }
       
   406                     else
       
   407                         {
       
   408                         //Acknowledge with success
       
   409                         if (iIniData.iAcknowledge)
       
   410                             {
       
   411                             iSsmStateAwareSession->AcknowledgeStateNotification(KErrNone);
       
   412                             }
       
   413                         }
       
   414                     }  
       
   415                 break;
       
   416                 }
       
   417             default:
       
   418                 {
       
   419                 // Error
       
   420                 INFO_PRINTF2(_L("CTestRStateAwareSession1: Test returned with error = %d"),error);
       
   421                 SetAsyncError(aIndex, error);
       
   422                 }
       
   423             }   
       
   424         }
       
   425     else if ( aActive == iActiveNotifyOnChange2 )
       
   426         {
       
   427         error = iActiveNotifyOnChange2->iStatus.Int();
       
   428         SetAsyncError(aIndex, error);
       
   429         }
       
   430     DecOutstanding();
       
   431     }
       
   432 
       
   433 /**
       
   434  Virtual DoCancel - Request to cancel the asynchronous command
       
   435  @see - MTPActiveCallback
       
   436  @param aActive Active Object that DoCancel has been called on
       
   437  @pre - N/A
       
   438  @post - N/A
       
   439  @leave system wide error code
       
   440 */
       
   441 void CTestRStateAwareSession1::DoCancel(CActive* aActive, TInt aIndex)
       
   442     {
       
   443     INFO_PRINTF1(_L("CTestRStateAwareSession1: DoCancel"));
       
   444     if(aActive ==  iActiveNotifyOnChange) 
       
   445         {
       
   446         TInt err =  iActiveNotifyOnChange->iStatus.Int(); 
       
   447         if (err != KErrNone)
       
   448             {
       
   449             ERR_PRINTF2(_L("CTestRStateAwareSession1: DoCancel Error %d"), err);
       
   450             SetAsyncError(aIndex, err);
       
   451             }
       
   452         }
       
   453     else if(aActive == iActiveNotifyOnChange2)
       
   454         {
       
   455         TInt err =  iActiveNotifyOnChange2->iStatus.Int(); 
       
   456         if (err != KErrNone)
       
   457             {
       
   458             ERR_PRINTF2(_L("CTestRStateAwareSession1: DoCancel Error %d"), err);
       
   459             SetAsyncError(aIndex, err);
       
   460             }
       
   461         }
       
   462     DecOutstanding();   
       
   463     }
       
   464 
       
   465 /**
       
   466  * Helper method to map the DomainIds. This can be extendable for other domain Ids
       
   467  */
       
   468 static TBool MapToDomainId(TPtrC& aDomainIdName, TDmDomainId& aDomainId)
       
   469     {
       
   470     TBool ret = ETrue;
       
   471     if (!(aDomainIdName.Compare(_L("KSM2UiServicesDomain1"))))
       
   472         {
       
   473         aDomainId = KSM2UiServicesDomain1;
       
   474         }
       
   475     else if (!(aDomainIdName.Compare(_L("KSM2HALDomain2"))))
       
   476         {
       
   477         aDomainId = KSM2HALDomain2;
       
   478         }
       
   479     else if (!(aDomainIdName.Compare(_L("KSM2HALDomain3"))))
       
   480         {
       
   481         aDomainId = KSM2HALDomain3;  
       
   482         }
       
   483     else if (!(aDomainIdName.Compare(_L("KSM2AppServicesDomain1"))))
       
   484         {
       
   485         aDomainId = KSM2AppServicesDomain1;  
       
   486         }
       
   487     else if (!(aDomainIdName.Compare(_L("KSM2AppServicesDomain2"))))
       
   488         {
       
   489         aDomainId = KSM2AppServicesDomain2;  
       
   490         }
       
   491     else if (!(aDomainIdName.Compare(_L("KSM2AppServicesDomain3"))))
       
   492         {
       
   493         aDomainId = KSM2AppServicesDomain3;  
       
   494         } 
       
   495     else if (!(aDomainIdName.Compare(_L("KSM2AppServicesDomain4"))))
       
   496         {
       
   497         aDomainId = KSM2AppServicesDomain4;  
       
   498         } 
       
   499     else 
       
   500         {
       
   501         ret = EFalse;
       
   502         }   
       
   503     return ret;
       
   504     }
       
   505 
       
   506 
       
   507 /**
       
   508  * Two phase constructor
       
   509  */
       
   510 CTestRStateAwareSession2* CTestRStateAwareSession2::NewL()
       
   511     {
       
   512     CTestRStateAwareSession2*  testStateAwareSession = new (ELeave) CTestRStateAwareSession2();
       
   513     CleanupStack::PushL(testStateAwareSession);
       
   514     testStateAwareSession->ConstructL();
       
   515     CleanupStack::Pop(testStateAwareSession);
       
   516     return testStateAwareSession; 
       
   517     }
       
   518 
       
   519 /**
       
   520 * @return pointer to the object that the data wraps
       
   521 */
       
   522 TAny*  CTestRStateAwareSession2::GetObject()
       
   523     {
       
   524     return iSsmStateAwareSession;
       
   525     }
       
   526 
       
   527 /**
       
   528  * SetObjectL
       
   529  */
       
   530 void CTestRStateAwareSession2::SetObjectL(TAny* aAny)
       
   531     {
       
   532     DoCleanup();
       
   533     iSsmStateAwareSession=static_cast<RSsmStateAwareSession*> (aAny);
       
   534     }
       
   535 
       
   536 /**
       
   537  * DisownObjectL
       
   538  */
       
   539 void CTestRStateAwareSession2::DisownObjectL()
       
   540     {
       
   541     iSsmStateAwareSession=NULL;
       
   542     }
       
   543 
       
   544 /**
       
   545  * Protected constructor. First phase construction
       
   546  */
       
   547 CTestRStateAwareSession2::CTestRStateAwareSession2():
       
   548 CDataWrapper()
       
   549     {
       
   550     }
       
   551 
       
   552 /**
       
   553  * Protected constructor. Second phase construction
       
   554  */
       
   555 void CTestRStateAwareSession2::ConstructL()
       
   556     {
       
   557     iActiveNotifyOnChange = CActiveCallback::NewL(*this);
       
   558     }
       
   559 
       
   560 /**
       
   561  * Destructor.
       
   562  */
       
   563 CTestRStateAwareSession2::~CTestRStateAwareSession2()
       
   564     {
       
   565     DoCleanup();
       
   566     delete iActiveNotifyOnChange;
       
   567     }
       
   568 
       
   569 /**
       
   570  * Process a command read from the ini file
       
   571  *
       
   572  * @param aCommand  the command to process
       
   573  * @param aSection      the entry in the ini file requiring the command to be processed
       
   574  *
       
   575  * @return ETrue if the command is processed
       
   576  */
       
   577 TBool CTestRStateAwareSession2::DoCommandL(const TTEFFunction& aCommand, const TTEFSectionName& aSection, const TInt aAsyncErrorIndex)
       
   578     {
       
   579     TBool retVal = ETrue;
       
   580     if (aCommand == KCmdNewL)
       
   581         {
       
   582         DoCmdNew();
       
   583         }
       
   584     else if (aCommand == KCmdConnect)
       
   585         {
       
   586         DoCmdConnect(aSection);
       
   587         }
       
   588     else if (aCommand == KCmdReadIniData)
       
   589         {
       
   590         DoCmdReadIniData(aSection);
       
   591         }
       
   592     else if (aCommand == KCmdRequestStateNotification)
       
   593         {
       
   594         DoCmdRequestStateNotification(aAsyncErrorIndex);
       
   595         }
       
   596     else if (aCommand == KCmdAcknowledgeStateNotification)
       
   597         {
       
   598         DoCmdAcknowledgeStateNotification(aSection);
       
   599         }
       
   600     else if (aCommand == KCmdClose)
       
   601         {
       
   602         DoCmdClose();
       
   603         }
       
   604     else if (aCommand == KCmdRequestStateNotificationCancel)
       
   605         {
       
   606         DoCmdRequestStateNotificationCancel();
       
   607         }
       
   608     else if (aCommand == KCmdAcknowledgeAndRequestStateNotification)
       
   609         {
       
   610         DoCmdAcknowledgeAndRequestStateNotification(aSection, aAsyncErrorIndex);
       
   611         }
       
   612     else if (aCommand == KCmdDestructor)
       
   613         {
       
   614         DoCmdDestructor();
       
   615         }
       
   616     else
       
   617         {
       
   618         retVal = EFalse;
       
   619         }    
       
   620    
       
   621     return retVal;
       
   622     }
       
   623 
       
   624 /**
       
   625  * Creates RSSmStateAwareSession class instance
       
   626  */
       
   627 void CTestRStateAwareSession2::DoCmdNew()
       
   628     {
       
   629     INFO_PRINTF1(_L("CTestRStateAwareSession2: Create RSsmStateAwareSession class instance"));
       
   630     DoCleanup();
       
   631     
       
   632     TRAPD(err, iSsmStateAwareSession = new (ELeave)RSsmStateAwareSession());
       
   633     
       
   634     if (err != KErrNone)
       
   635         {
       
   636         ERR_PRINTF2(_L("CTestRStateAwareSession2: new error %d"), err);
       
   637         SetError(err);
       
   638         }      
       
   639     }
       
   640 
       
   641 /**
       
   642  * Close RSsmStateAwareSession handle
       
   643  */
       
   644 void CTestRStateAwareSession2::DoCmdClose()
       
   645     {
       
   646     INFO_PRINTF1(_L("CTestRStateAwareSession2: Close RSsmStateAwareSession"));
       
   647     iSsmStateAwareSession->Close();
       
   648     }
       
   649 
       
   650 /**
       
   651  * Contains cleanup implementation
       
   652  */
       
   653 void CTestRStateAwareSession2::DoCleanup()
       
   654     {
       
   655     if(iSsmStateAwareSession != NULL)
       
   656         {
       
   657         INFO_PRINTF1(_L("CTestRStateAwareSession2: Deleting current RSsmStateAwareSession"));
       
   658         delete iSsmStateAwareSession;
       
   659         iSsmStateAwareSession = NULL;
       
   660         }
       
   661     }
       
   662 
       
   663 /**
       
   664  * Destroy RSsmStateAwareSession object
       
   665  */
       
   666 void CTestRStateAwareSession2::DoCmdDestructor()
       
   667     {
       
   668     INFO_PRINTF1(_L("CTestRStateAwareSession2: Destroying the RStateAwareSession object"));
       
   669     DoCleanup();
       
   670     }
       
   671 
       
   672 /**
       
   673  * Connects a client to RSsmStateAwareSession with Domain Id
       
   674  */
       
   675 void CTestRStateAwareSession2::DoCmdConnect(const TDesC& aSection)
       
   676     {
       
   677     TPtrC domainIdName;
       
   678     if (!GetStringFromConfig(aSection, KDomainId(), domainIdName))
       
   679         {
       
   680         //set default domainId if value is not provided in .ini file
       
   681         domainIdName.Set(_L("KSM2UiServicesDomain1"));        
       
   682         }
       
   683     TDmDomainId domainId;
       
   684     if(MapToDomainId(domainIdName, domainId ))
       
   685        {
       
   686        INFO_PRINTF2(_L("CTestRStateAwareSession2: Calling RSsmStateAwareSession->Connect with domain id %S"), &domainIdName );
       
   687        TInt  err = iSsmStateAwareSession->Connect(domainId);          
       
   688        if (KErrNone != err)
       
   689             {
       
   690             ERR_PRINTF2(_L("CTestRStateAwareSession2: Connect() error %d"), err);
       
   691             SetError(err);  
       
   692             }       
       
   693        } 
       
   694     else
       
   695         {
       
   696         ERR_PRINTF2(_L("CTestRStateAwareSession2: Could not map %S to domain Id"), &domainIdName );
       
   697         SetBlockResult(EFail);
       
   698         }
       
   699     }
       
   700 
       
   701 /*
       
   702  * Helper function to read ini file data
       
   703  */
       
   704 void CTestRStateAwareSession2::DoCmdReadIniData(const TDesC& aSection)
       
   705 {
       
   706     if( !GetIntFromConfig(aSection,KCount(), iIniData.iCount) ||
       
   707         !GetBoolFromConfig(aSection,KAcknowledge(), iIniData.iAcknowledge) )
       
   708         {
       
   709         ERR_PRINTF1(_L("Error reading ini file data"));
       
   710         SetError(KErrNotFound);
       
   711         }
       
   712 }
       
   713 
       
   714 /**
       
   715  * DoCmdRequestStateNotification 
       
   716  */
       
   717 void CTestRStateAwareSession2::DoCmdRequestStateNotification(const TInt aAsyncErrorIndex)
       
   718     {
       
   719     INFO_PRINTF2(_L("CTestRStateAwareSession2: RequestStateNotification with Asyc Error Index: %d"), aAsyncErrorIndex);
       
   720     iSsmStateAwareSession->RequestStateNotification(iActiveNotifyOnChange->iStatus);
       
   721     iActiveNotifyOnChange->Activate(aAsyncErrorIndex);
       
   722     IncOutstanding();    
       
   723     }
       
   724 
       
   725 /**
       
   726  * DoCmdAcknowledgeStateNotification
       
   727  */
       
   728 void CTestRStateAwareSession2::DoCmdAcknowledgeStateNotification(const TDesC& aSection)
       
   729     {
       
   730     TInt error;
       
   731     if (!GetIntFromConfig(aSection, KError(), error ))
       
   732         {
       
   733         //set default error value if value is not provided in .ini file
       
   734         error = KErrNone;
       
   735         }
       
   736    iSsmStateAwareSession->AcknowledgeStateNotification(error);    
       
   737     }
       
   738 
       
   739 /**
       
   740  * DoCmdAcknowledgeAndRequestStateNotification
       
   741  */
       
   742 void CTestRStateAwareSession2::DoCmdAcknowledgeAndRequestStateNotification(const TDesC& aSection, const TInt aAsyncErrorIndex)
       
   743     {
       
   744     TInt error;
       
   745     if (!GetIntFromConfig(aSection, KError(), error ))
       
   746         {
       
   747         //set default error value if value is not provided in .ini file
       
   748         error = KErrNone;
       
   749         }
       
   750     iSsmStateAwareSession->AcknowledgeAndRequestStateNotification(error, iActiveNotifyOnChange->iStatus );
       
   751     iActiveNotifyOnChange->Activate(aAsyncErrorIndex);
       
   752     IncOutstanding();        
       
   753     }
       
   754 
       
   755 /**
       
   756  * DoCmdRequestStateNotificationCancel
       
   757  */
       
   758 void CTestRStateAwareSession2::DoCmdRequestStateNotificationCancel()
       
   759     {
       
   760     INFO_PRINTF1(_L("CTestRStateAwareSession2: RequestStateNotificationCancel is called "));
       
   761     iSsmStateAwareSession->RequestStateNotificationCancel();
       
   762     }
       
   763 
       
   764 /**
       
   765  Virtual RunL - Called on completion of an asynchronous command
       
   766  @see MTPActiveCallback
       
   767  @param aActive Active Object that RunL has been called on
       
   768  @pre N/A
       
   769  @post N/A
       
   770  @leave system wide error code
       
   771 */
       
   772 void CTestRStateAwareSession2::RunL(CActive* aActive, TInt aIndex)
       
   773     {
       
   774     TInt error = KErrNone;
       
   775 
       
   776     if (aActive == iActiveNotifyOnChange)
       
   777         {
       
   778         error = iActiveNotifyOnChange->iStatus.Int();
       
   779         if (error == KErrNone)
       
   780             {
       
   781             // Request has completed i.e. The member must either defer again or acknowledge.
       
   782             TSsmState currentState = iSsmStateAwareSession->State(); 
       
   783             // This component only responds to Shutdown state events.
       
   784             if ( (currentState.MainState() == ESsmShutdown) &&
       
   785                  (currentState.SubState() == ESsmShutdownSubStateCritical || currentState.SubState() == ESsmShutdownSubStateNonCritical) )
       
   786                 {
       
   787                 if(iCount < iIniData.iCount)
       
   788                     {
       
   789                     iCount++;
       
   790                     iSsmStateAwareSession->DeferAcknowledgement(iActiveNotifyOnChange->iStatus);
       
   791                     iActiveNotifyOnChange->Activate(aIndex);
       
   792                     IncOutstanding();
       
   793                     }
       
   794                 else
       
   795                     {
       
   796                     //Acknowledge with success/failure
       
   797                     if (iIniData.iAcknowledge)
       
   798                         {
       
   799                         iSsmStateAwareSession->AcknowledgeStateNotification(KErrNone);
       
   800                         }
       
   801                     }
       
   802                 }
       
   803             else
       
   804                 {
       
   805                 INFO_PRINTF1(_L("CTestRStateAwareSession2: Deferring acknowledgement for a state that doesn't support transition monitoring"));
       
   806                 iSsmStateAwareSession->DeferAcknowledgement(iActiveNotifyOnChange->iStatus);
       
   807                 iActiveNotifyOnChange->Activate(aIndex);
       
   808                 IncOutstanding();
       
   809                 }
       
   810             }
       
   811         else
       
   812             {
       
   813             INFO_PRINTF2(_L("CTestRStateAwareSession2: RunL Error %d"), error);
       
   814             SetAsyncError(aIndex, error);
       
   815             }       
       
   816         }
       
   817     DecOutstanding();
       
   818     }
       
   819 
       
   820 /**
       
   821  Virtual DoCancel - Request to cancel the asynchronous command
       
   822  @see - MTPActiveCallback
       
   823  @param aActive Active Object that DoCancel has been called on
       
   824  @pre - N/A
       
   825  @post - N/A
       
   826  @leave system wide error code
       
   827 */
       
   828 void CTestRStateAwareSession2::DoCancel(CActive* aActive, TInt aIndex)
       
   829     {
       
   830     INFO_PRINTF1(_L("CTestRStateAwareSession2: DoCancel"));
       
   831     if(aActive ==  iActiveNotifyOnChange) 
       
   832         {
       
   833         TInt err =  iActiveNotifyOnChange->iStatus.Int(); 
       
   834         if (err!=KErrNone)
       
   835             {
       
   836             ERR_PRINTF2(_L("CTestRStateAwareSession2: DoCancel Error %d"), err);
       
   837             SetAsyncError(aIndex, err);
       
   838             }
       
   839         }   
       
   840     DecOutstanding();   
       
   841     }