wim/Scard/src/ScardAccessControlRegistry.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2003 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:  Handles the creation and destruction of individual access 
       
    15 *                controllers
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include    "ScardAccessControlRegistry.h"
       
    23 #include    "ScardAccessControl.h"
       
    24 #include    "WimTrace.h"
       
    25 
       
    26 #ifdef _DEBUG // for logging
       
    27 #include    "ScardLogs.h"
       
    28 #include    <flogger.h> 
       
    29 #endif
       
    30 // ============================ MEMBER FUNCTIONS ===============================
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 // CScardAccessControlRegistry::CScardAccessControlRegistry
       
    34 // C++ default constructor can NOT contain any code, that
       
    35 // might leave.
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 CScardAccessControlRegistry::CScardAccessControlRegistry()
       
    39     {
       
    40     }
       
    41 
       
    42 // -----------------------------------------------------------------------------
       
    43 // CScardAccessControlRegistry::ConstructL
       
    44 // Symbian 2nd phase constructor can leave.
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 void CScardAccessControlRegistry::ConstructL( CScardServer* aServer )
       
    48     {
       
    49     _WIMTRACE(_L("WIM|Scard|CScardAccessControlRegistry::ConstructL|Begin"));
       
    50     iAccessControllers = new( ELeave ) CArrayFixFlat<CScardAccessControl*>( 1 );
       
    51     iServer = aServer;
       
    52     }
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // CScardAccessControlRegistry::NewL
       
    56 // Two-phased constructor.
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 CScardAccessControlRegistry* CScardAccessControlRegistry::NewL(
       
    60     CScardServer* aServer )
       
    61     {
       
    62     _WIMTRACE(_L("WIM|Scard|CScardAccessControlRegistry::NewL|Begin"));
       
    63     CScardAccessControlRegistry* self = 
       
    64         new( ELeave ) CScardAccessControlRegistry;
       
    65     
       
    66     CleanupStack::PushL( self );
       
    67     self->ConstructL( aServer );
       
    68     CleanupStack::Pop( self );
       
    69 
       
    70     return self;
       
    71     }
       
    72 
       
    73     
       
    74 // Destructor
       
    75 CScardAccessControlRegistry::~CScardAccessControlRegistry()
       
    76     {
       
    77     _WIMTRACE(_L("WIM|Scard|CScardAccessControlRegistry::~CScardAccessControlRegistry|Begin"));
       
    78     while ( iAccessControllers->Count() )
       
    79         {
       
    80         RemoveAccessController( 0 );
       
    81         }
       
    82     delete iAccessControllers;
       
    83     }
       
    84 
       
    85 
       
    86 // -----------------------------------------------------------------------------
       
    87 // CScardAccessControlRegistry::ControllerRetired
       
    88 // An access controller has been disconnected
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 void CScardAccessControlRegistry::ControllerRetired(
       
    92     CScardAccessControl* aControl )
       
    93     {
       
    94     _WIMTRACE(_L("WIM|Scard|CScardAccessControlRegistry::ControllerRetired|Begin"));
       
    95     for ( TInt i( 0 ); i < iAccessControllers->Count(); i++ )
       
    96         {
       
    97         if ( iAccessControllers->At( i ) == aControl )
       
    98             {
       
    99             iAccessControllers->Delete( i );
       
   100             return;
       
   101             }
       
   102         }
       
   103     }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // CScardAccessControlRegistry::RemoveAccessController
       
   107 // Remove Access controller from registry
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 void CScardAccessControlRegistry::RemoveAccessController(
       
   111     const TInt aIndex )
       
   112     {
       
   113     _WIMTRACE(_L("WIM|Scard|CScardAccessControlRegistry::RemoveAccessController|Begin"));
       
   114     __ASSERT_ALWAYS( aIndex < iAccessControllers->Count(), 
       
   115         User::Panic( _L( "Registry corruption" ), 
       
   116             KScServerPanicInternalError ) );
       
   117 
       
   118     //  we allocated the controller, so we must also delete it..
       
   119     delete ( (*iAccessControllers)[aIndex] );
       
   120     
       
   121     //  ...and remove it from the registy as well
       
   122     //Check first it is not removed yet...
       
   123     if ( aIndex < iAccessControllers->Count() )
       
   124         {
       
   125         iAccessControllers->Delete( aIndex );
       
   126         }
       
   127     return;
       
   128     }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // CScardAccessControlRegistry::Reader
       
   132 // Get reader object by Reader ID
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 MScardReader* CScardAccessControlRegistry::Reader(
       
   136     const TReaderID aReaderID) const
       
   137     {
       
   138     _WIMTRACE(_L("WIM|Scard|CScardAccessControlRegistry::Reader|Begin"));
       
   139     for ( TInt i( 0 ); i < iAccessControllers->Count(); i++ )
       
   140         {
       
   141         if ( (*iAccessControllers)[i]->ReaderID() == aReaderID )
       
   142             {
       
   143             return (*iAccessControllers)[i]->Reader();
       
   144             }
       
   145         }
       
   146     return NULL;
       
   147     }
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // CScardAccessControlRegistry::FindAccessController
       
   151 // Returns the access controller for given reader. 
       
   152 // Returns NULL if access controlled is not created
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 CScardAccessControl* CScardAccessControlRegistry::FindAccessController(
       
   156     const TReaderID aReaderID )
       
   157     {
       
   158     _WIMTRACE(_L("WIM|Scard|CScardAccessControlRegistry::FindAccessController|Begin"));
       
   159     for ( TInt i( 0 ); i < iAccessControllers->Count(); i++ )
       
   160         {
       
   161         if ( (*iAccessControllers)[i]->ReaderID() == aReaderID )
       
   162             {
       
   163             return (*iAccessControllers)[i];
       
   164             }
       
   165         }
       
   166     return NULL;
       
   167     }
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // CScardAccessControlRegistry::ReaderHandlerLoaded
       
   171 // Check if this reader has a controller loaded
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 TBool CScardAccessControlRegistry::ReaderHandlerLoaded(
       
   175     const TReaderID aReaderID ) const 
       
   176     {
       
   177     _WIMTRACE(_L("WIM|Scard|CScardAccessControlRegistry::ReaderHandlerLoaded|Begin"));
       
   178     for ( TInt i( 0 ); i < iAccessControllers->Count(); i++ )
       
   179         {
       
   180         if ( (*iAccessControllers)[i]->ReaderID() == aReaderID )
       
   181             {
       
   182             return ETrue;
       
   183             }
       
   184         }
       
   185     return EFalse;
       
   186     }
       
   187 
       
   188 // -----------------------------------------------------------------------------
       
   189 // CScardAccessControlRegistry::Server
       
   190 // Returns the server
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 CScardServer* CScardAccessControlRegistry::Server()
       
   194     {
       
   195     _WIMTRACE(_L("WIM|Scard|CScardAccessControlRegistry::Server|Begin"));
       
   196     return iServer;
       
   197     }
       
   198 
       
   199 // -----------------------------------------------------------------------------
       
   200 // CScardAccessControlRegistry::AccessController
       
   201 // Creates access controller if necessary
       
   202 // -----------------------------------------------------------------------------
       
   203 //
       
   204 CScardAccessControl* CScardAccessControlRegistry::AccessController(
       
   205     const TReaderID aReaderID )
       
   206     {
       
   207     _WIMTRACE(_L("WIM|Scard|CScardAccessControlRegistry::AccessController|Begin"));
       
   208 #ifdef _DEBUG
       
   209     RFileLogger::WriteFormat( KScardLogDir, KScardLogFileName, 
       
   210         EFileLoggingModeAppend, 
       
   211         _L( "CScardAccessControlRegistry::GetAccessController entered.\n" ) );
       
   212 #endif
       
   213     //  If a controller is already made for the reader, just return it
       
   214     CScardAccessControl* ctrl = FindAccessController( aReaderID );
       
   215     if ( ctrl )
       
   216         {
       
   217 #ifdef _DEBUG
       
   218         RFileLogger::WriteFormat( KScardLogDir, KScardLogFileName, 
       
   219             EFileLoggingModeAppend, 
       
   220             _L( "CScardAccessControlRegistry::GetAccessController access\
       
   221             controller found.\n" ) );
       
   222 #endif
       
   223         return ctrl;
       
   224         }
       
   225         
       
   226     //  If we're here, the controller has not yet been made, so create it
       
   227 #ifdef _DEBUG
       
   228     RFileLogger::WriteFormat( KScardLogDir, KScardLogFileName, 
       
   229         EFileLoggingModeAppend, 
       
   230         _L( "CScardAccessControlRegistry::GetAccessController create new access\
       
   231         controller.\n" ) );
       
   232 #endif
       
   233     TRAPD( err,  ctrl = CScardAccessControl::NewL( aReaderID, this ) );
       
   234     if ( !ctrl || err )
       
   235         {
       
   236 #ifdef _DEBUG
       
   237         RFileLogger::WriteFormat( KScardLogDir, KScardLogFileName, 
       
   238             EFileLoggingModeAppend, 
       
   239             _L( "CScardAccessControlRegistry::GetAccessController access\
       
   240             controller creation failed: %d.\n" ), err );
       
   241 #endif
       
   242         return NULL;
       
   243         }
       
   244 
       
   245     TRAP( err, iAccessControllers->AppendL( ctrl ) );
       
   246     if ( err )
       
   247         {
       
   248         return NULL;
       
   249         }
       
   250 
       
   251     return ctrl;
       
   252     }
       
   253 
       
   254 //  End of File