wim/WimClient/src/WimSecModuleMgr.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2002 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:  API for list all WIM Security Modules
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 //INCLUDES
       
    21 #include "WimSecModuleMgr.h"
       
    22 #include "WimSecModule.h"
       
    23 #include "WimMgmt.h"
       
    24 #include "WimTrace.h"
       
    25  
       
    26 
       
    27 
       
    28 // ================= MEMBER FUNCTIONS  for CWimSecModuleMgr=======================
       
    29 //
       
    30 //  C++ default constructor. 
       
    31 //
       
    32 //
       
    33 CWimSecModuleMgr::CWimSecModuleMgr():CActive( EPriorityHigh )
       
    34     {
       
    35     }
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CWimSecModuleMgr::NewL()
       
    39 // Symbian 2nd phase constructor can leave.
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 EXPORT_C CWimSecModuleMgr* CWimSecModuleMgr::NewL()
       
    43     {
       
    44     _WIMTRACE ( _L( "CWimSecModuleMgr::NewL()" ) );
       
    45     CWimSecModuleMgr* self = new( ELeave ) CWimSecModuleMgr();
       
    46     CleanupStack::PushL( self );
       
    47     self->ConstructL();
       
    48     CleanupStack::Pop( self );
       
    49     return self;
       
    50     }
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CWimSecModuleMgr::ConstructL()
       
    54 // Second phase
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 void CWimSecModuleMgr::ConstructL()
       
    58     {
       
    59     _WIMTRACE ( _L( "CWimSecModuleMgr::ConstructL()" ) );
       
    60     CActiveScheduler::Add( this );  
       
    61     }
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // CWimSecModuleMgr::~CWimSecModuleMgr()
       
    65 // Frees memory & preferences that have been allocated.
       
    66 // -----------------------------------------------------------------------------
       
    67 //     
       
    68 EXPORT_C CWimSecModuleMgr::~CWimSecModuleMgr()
       
    69     {
       
    70     _WIMTRACE ( _L( "CWimSecModuleMgr::~CWimSecModuleMgr()" ) );
       
    71     Cancel();
       
    72     DeAllocateWimReferences();
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CWimSecModuleMgr::GetEntries()
       
    77 // Returns all WIM Security Modules in array.
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 EXPORT_C void CWimSecModuleMgr::GetEntries( RCPointerArray<HBufC>& aTokens, 
       
    81                                            TRequestStatus& aStatus )
       
    82     {
       
    83     _WIMTRACE ( _L( "CWimSecModuleMgr::GetEntries()" ) );
       
    84     aStatus = KRequestPending;
       
    85     iTokenLabels = &aTokens;  
       
    86     iPhase = ECreateNewSession;
       
    87     iClientStatus = &aStatus;   
       
    88     SignalOwnStatusAndComplete();
       
    89     }
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CWimSecModuleMgr::WimCount()
       
    93 // Returns the number of WIM-cards
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 EXPORT_C TInt CWimSecModuleMgr::WimCount()
       
    97     {
       
    98     _WIMTRACE ( _L( "CWimSecModuleMgr::WimCount()" ) );
       
    99     if( !iWims )
       
   100         {
       
   101         return KErrNotFound;
       
   102         }
       
   103     return iWims->Count(); 
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CWimSecModuleMgr::GetWimSecModuleByIndexL()
       
   108 // Returns CWimSecModule according to received index.
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 EXPORT_C CWimSecModule* CWimSecModuleMgr::GetWimSecModuleByIndexL(
       
   112                                                    const TInt aIndex )
       
   113     {
       
   114     _WIMTRACE ( _L( "CWimSecModuleMgr::GetWimSecModuleByIndexL()" ) );
       
   115     if ( !iWims || aIndex > iWims->Count() || aIndex < 0 )
       
   116         {
       
   117         User::Leave( KErrNotFound );
       
   118         }
       
   119     return iWims->At( aIndex );        
       
   120     }
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // CWimSecModuleMgr::WimLabelByIndexL()
       
   124 // Returns WIM label according to received index
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 EXPORT_C TPtrC CWimSecModuleMgr::WimLabelByIndexL( const TInt aIndex )
       
   128     {
       
   129     _WIMTRACE ( _L( "CWimSecModuleMgr::WimLabelByIndexL()" ) );
       
   130     if ( !iWims || aIndex > iWims->Count() || aIndex < 0 )
       
   131         {
       
   132         User::Leave( KErrNotFound );
       
   133         }
       
   134     return iWims->At( aIndex )->Label();
       
   135     }
       
   136 
       
   137 // -----------------------------------------------------------------------------
       
   138 // CWimSecModuleMgr::WimTokenNumberByIndexL()
       
   139 // Returns the number of "slot" -where current token is attached.
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 EXPORT_C TInt CWimSecModuleMgr::WimTokenNumberByIndexL( const TInt aIndex )
       
   143     {
       
   144     _WIMTRACE ( _L( "CWimSecModuleMgr::WimTokenNumberByIndexL()" ) );
       
   145     if ( !iWims || aIndex > iWims->Count() || aIndex < 0 )
       
   146         {
       
   147         User::Leave( KErrNotFound );
       
   148         }
       
   149     return iWims->At( aIndex )->TokenNumber();
       
   150     }
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // CWimSecModuleMgr::CancelList()
       
   154 // Cancels listing operation.
       
   155 // -----------------------------------------------------------------------------
       
   156 //
       
   157 EXPORT_C void CWimSecModuleMgr::CancelList()
       
   158     {
       
   159     _WIMTRACE ( _L( "CWimSecModuleMgr::CancelList()" ) );
       
   160     Cancel();                          
       
   161     }
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // CWimSecModuleMgr::SignalOwnStatusAndComplete()
       
   165 // Sets own iStatus to KRequestPending, and signals it 
       
   166 // with User::RequestComplete() -request. This gives chance 
       
   167 // to active scheduler to run other active objects. After a quick
       
   168 // visit in active scheduler, signal returns to RunL() and starts next
       
   169 // phase of listing operation. This makes possible the cancelling 
       
   170 // -function to get through.
       
   171 // -----------------------------------------------------------------------------
       
   172 //
       
   173 void CWimSecModuleMgr::SignalOwnStatusAndComplete()
       
   174     {
       
   175     _WIMTRACE ( _L( "CWimSecModuleMgr::SignalOwnStatusAndComplete()" ) );
       
   176     iStatus = KRequestPending;
       
   177     SetActive();
       
   178     TRequestStatus* status = &iStatus;
       
   179     User::RequestComplete( status, KErrNone );
       
   180     }
       
   181 
       
   182 
       
   183 // -----------------------------------------------------------------------------
       
   184 //  CWimSecModuleMgr::WIMModulesL() 
       
   185 //  Allocate pointer to WIM and create CWimSecModules.
       
   186 //  This function calls server one time. It gets WIM references.
       
   187 // -----------------------------------------------------------------------------
       
   188 //
       
   189 TInt CWimSecModuleMgr::WIMModulesL() 
       
   190     {
       
   191     _WIMTRACE ( _L( "CWimSecModuleMgr::WIMModulesL()" ) );
       
   192     ReadWimCount();
       
   193     TInt status = KErrNone;
       
   194     if ( iWimCount > 0 )
       
   195         {
       
   196         iWimAddrLst = new ( ELeave ) TWimAddress[iWimCount];
       
   197         status = iClientSession->WIMRefs( iWimAddrLst, ( TText8 )iWimCount );
       
   198         
       
   199         if ( status == KErrNone )
       
   200             {
       
   201             if( iWims )
       
   202                 {
       
   203                 iWims->ResetAndDestroy();
       
   204                 delete iWims;
       
   205                 iWims = NULL;
       
   206                 }
       
   207             
       
   208             iWims = new( ELeave ) CArrayPtrFlat<CWimSecModule> ( iWimCount );
       
   209             /* Construct WIM security modules */ 
       
   210             TUint8 index;
       
   211             for( index = 0; index < iWimCount; index++ )
       
   212                 {
       
   213                 CWimSecModule* wimSecModule = CWimSecModule::NewL( 
       
   214                                                     iWimAddrLst[index] );
       
   215                 CleanupStack::PushL( wimSecModule );
       
   216                 wimSecModule->SetClientSession( iClientSession );
       
   217                 iWims->AppendL( wimSecModule );
       
   218                 CleanupStack::Pop( wimSecModule );
       
   219                 }
       
   220             AppendTokenLabelsToArrayL();
       
   221             }
       
   222         }
       
   223     else
       
   224         {
       
   225         status = KErrHardwareNotAvailable;
       
   226         }
       
   227     return status;
       
   228     }
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CWimSecModuleMgr::AppendTokenLabelsToArrayL()
       
   232 // Appends labels of available Tokens (wims) to an array.
       
   233 // -----------------------------------------------------------------------------
       
   234 //
       
   235 void CWimSecModuleMgr::AppendTokenLabelsToArrayL()
       
   236     {
       
   237     _WIMTRACE ( _L( "CWimSecModuleMgr::AppendTokenLabelsToArrayL()" ) );
       
   238     TInt count = iWims->Count();
       
   239     TInt i;
       
   240     for ( i = 0; i < count; i++ )
       
   241         {
       
   242         HBufC* Tokenlabel = iWims->At( i )->Label().AllocLC();
       
   243         User::LeaveIfError( iTokenLabels->Append( Tokenlabel ) );
       
   244         CleanupStack::Pop( Tokenlabel ); //Tokenlabel
       
   245         }
       
   246     }
       
   247 
       
   248 // -----------------------------------------------------------------------------
       
   249 // CWimSecModuleMgr::ReadWimCount()
       
   250 // Reads the count of the WIM.
       
   251 // -----------------------------------------------------------------------------
       
   252 //
       
   253 void CWimSecModuleMgr::ReadWimCount()
       
   254     {
       
   255     _WIMTRACE ( _L( "CWimSecModuleMgr::ReadWimCount()" ) );
       
   256     iWimCount = iClientSession->WIMCount();
       
   257     }
       
   258 
       
   259 // -----------------------------------------------------------------------------
       
   260 // CWimSecModuleMgr::DoCancel()
       
   261 // Deallocates member variables and completes client status with
       
   262 // KErrCancel error code..
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 void CWimSecModuleMgr::DoCancel()
       
   266     {
       
   267     if( iCurrentPhase == EInitializeWim )
       
   268 	    {
       
   269 	    iClientSession->CancelInitialize();	
       
   270 	    }
       
   271     User::RequestComplete( iClientStatus, KErrCancel );
       
   272     }
       
   273 
       
   274 
       
   275 // -----------------------------------------------------------------------------
       
   276 // CWimSecModuleMgr::RunL()
       
   277 // Different phases are handled here. This might be considered as state machine. 
       
   278 // iPhase = ECreateNewSession: Gets ClientSession handle. Tries to connect
       
   279 //          to existing session. If there is no existing session, a new session 
       
   280 //          is created.
       
   281 // iPhase = EConnectClientSession: If new session was needed to be created, 
       
   282 //          new connecting request is needed.
       
   283 // iPhase = EInitializeWim: Sends initialize request to server and waits
       
   284 //          asyncronously.
       
   285 // iPhase = EGetWimRefs: Reads information about WIM-cards and
       
   286 //          constructs CWimSecmodules. (one WIM-card  == one CWimSecModule )
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 void CWimSecModuleMgr::RunL()
       
   290     {
       
   291     
       
   292     if ( iStatus.Int() != KErrNone )
       
   293         {
       
   294         //This is possible only when initializing WIM.
       
   295         _WIMTRACE2( _L("CWimSecModuleMgr::RunL unable to initialize =%d"),iStatus.Int() );
       
   296         DeAllocateWimReferences();
       
   297         User::RequestComplete( iClientStatus, KErrHardwareNotAvailable );
       
   298         return;
       
   299         }
       
   300 
       
   301     switch ( iPhase )
       
   302         {
       
   303         case ECreateNewSession:
       
   304             { 
       
   305             iCurrentPhase = ECreateNewSession;
       
   306             
       
   307             if ( !iClientSession )
       
   308                 {
       
   309                  _WIMTRACE ( _L( "CWimSecModuleMgr::RunL() brand new session" ) );
       
   310                 //get ClientSession handle
       
   311                 iClientSession = RWimMgmt::ClientSessionL();
       
   312                 }
       
   313             
       
   314             iPhase = EConnectClientSession;
       
   315             TInt retval = 0;
       
   316             iWimStartErr = iClientSession->Connect();
       
   317             if ( iWimStartErr == KErrNotFound ) //is there existing session?
       
   318                 {
       
   319                 retval = iClientSession->StartWim();
       
   320                 if ( retval != KErrNone )
       
   321                     {
       
   322                     User::RequestComplete( iClientStatus, 
       
   323                                            KErrHardwareNotAvailable ); 
       
   324                     }
       
   325                 else
       
   326                     {
       
   327                     SignalOwnStatusAndComplete();
       
   328                     }
       
   329                 } 
       
   330             else
       
   331                 {
       
   332                  // Yes there is, continue
       
   333                  SignalOwnStatusAndComplete(); 
       
   334                 }
       
   335             break;
       
   336             }
       
   337         case EConnectClientSession:
       
   338             {
       
   339             //Raise iPhase
       
   340             iCurrentPhase  = EConnectClientSession;
       
   341             iPhase = EInitializeWim; 
       
   342             if ( iWimStartErr )
       
   343                 {
       
   344                 //New session was needed to be created. Try to reconnect.
       
   345                 iWimStartErr = iClientSession->Connect();
       
   346                 if ( iWimStartErr != KErrNone )
       
   347                     {
       
   348                     //Failed to connect. No reason to continue
       
   349                     User::RequestComplete( iClientStatus, 
       
   350                                            KErrHardwareNotAvailable );
       
   351                     }
       
   352                 else
       
   353                     {
       
   354                     SignalOwnStatusAndComplete();
       
   355                     }
       
   356                 }
       
   357             else
       
   358                 {
       
   359                 SignalOwnStatusAndComplete();
       
   360                 }
       
   361             break;
       
   362             }
       
   363         case EInitializeWim:
       
   364             {
       
   365             iCurrentPhase = EInitializeWim;
       
   366             //Initialize WIM
       
   367             SetActive();
       
   368             iClientSession->Initialize( iStatus );
       
   369             iPhase = EGetWimRefs;
       
   370             break;   
       
   371             }
       
   372         case EGetWimRefs:
       
   373             {
       
   374             iCurrentPhase = EGetWimRefs;
       
   375             TInt retval = TInt( WIMModulesL() );
       
   376             _WIMTRACE2 ( _L( "CWimSecModuleMgr::RunL(),\
       
   377                 case:EGetWimRefs error =%d" ), retval );
       
   378       
       
   379             User::RequestComplete( iClientStatus, retval );
       
   380             break;
       
   381             }
       
   382 
       
   383         default:
       
   384             {
       
   385             break;
       
   386             }
       
   387         } 
       
   388     }
       
   389 
       
   390 
       
   391 // -----------------------------------------------------------------------------
       
   392 // CWimSecModuleMgr::RunError() 
       
   393 //                                       
       
   394 // The active scheduler calls this function if this active object's RunL() 
       
   395 // function leaves. This gives this active object the opportunity to perform 
       
   396 // any necessary cleanup.
       
   397 // After array's cleanup, complete request with received error code.
       
   398 // -----------------------------------------------------------------------------
       
   399 TInt CWimSecModuleMgr::RunError( TInt aError )
       
   400     {
       
   401     _WIMTRACE ( _L( "CWimSecModuleMgr::RunError()" ) );
       
   402     DeAllocateWimReferences();
       
   403     User::RequestComplete( iClientStatus, aError );
       
   404     return KErrNone;
       
   405     }
       
   406 
       
   407 
       
   408 // -----------------------------------------------------------------------------
       
   409 // CWimSecModuleMgr::DeAllocateWimReferences()
       
   410 // Frees memory & preferences that have been allocated.
       
   411 // -----------------------------------------------------------------------------
       
   412 //     
       
   413 
       
   414 void CWimSecModuleMgr::DeAllocateWimReferences()
       
   415     {
       
   416     _WIMTRACE ( _L( "CWimSecModuleMgr::DeAllocateWimReferences()" ) );
       
   417     if ( iTokenLabels )
       
   418         {
       
   419         iTokenLabels = NULL;
       
   420         }
       
   421     if ( iWims )
       
   422         {
       
   423         iWims->ResetAndDestroy();
       
   424         delete iWims;
       
   425         iWims = NULL;
       
   426         }
       
   427     if ( iClientSession )
       
   428         {
       
   429         if (( iWimCount > 0 ) && iWimAddrLst )
       
   430             {
       
   431             iClientSession->FreeWIMAddrLst( iWimAddrLst, iWimCount );
       
   432             delete[] iWimAddrLst;
       
   433             }
       
   434         }
       
   435     if ( iClientSession )
       
   436         {
       
   437         iClientSession->Close();
       
   438         delete iClientSession;
       
   439         iClientSession = NULL;
       
   440         }
       
   441     }
       
   442 
       
   443 
       
   444 
       
   445 
       
   446