wim/Scard/src/ScardNotifyRegistry.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:  Notification Registry for SmartCard
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    "ScardBase.h"
       
    22 #include    "ScardNotifyRegistry.h"
       
    23 #include    "ScardServer.h"
       
    24 #include    "ScardNotifier.h"
       
    25 #include    "WimTrace.h"
       
    26 
       
    27 // ============================ MEMBER FUNCTIONS ===============================
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // CScardNotifyRegistry::CScardNotifyRegistry
       
    31 // C++ default constructor can NOT contain any code, that
       
    32 // might leave.
       
    33 // -----------------------------------------------------------------------------
       
    34 //
       
    35 CScardNotifyRegistry::CScardNotifyRegistry() 
       
    36     {
       
    37     _WIMTRACE(_L("WIM|Scard|CScardNotifyRegistry::CScardNotifyRegistry|Begin"));
       
    38     }
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CScardNotifyRegistry::ConstructL
       
    42 // Symbian 2nd phase constructor can leave.
       
    43 // -----------------------------------------------------------------------------
       
    44 //
       
    45 void CScardNotifyRegistry::ConstructL( CScardServer* aServer )
       
    46     {
       
    47     _WIMTRACE(_L("WIM|Scard|CScardNotifyRegistry::ConstructL|Begin"));
       
    48     iNotifiers = new( ELeave ) CArrayPtrFlat<CScardNotifier>( 1 );
       
    49 
       
    50     //  The server is notified of all reader events. 
       
    51     RMessage2 message; // Empty message
       
    52     CScardNotifier* serverNotifier = CScardNotifier::NewL( this, 
       
    53                                                            message,
       
    54                                                            NULL,
       
    55                                                            KServerNotifier );
       
    56 
       
    57     iNotifiers->AppendL( serverNotifier );
       
    58 
       
    59     iServer = aServer;
       
    60     }
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CScardNotifyRegistry::NewL
       
    64 // Two-phased constructor.
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 CScardNotifyRegistry* CScardNotifyRegistry::NewL( CScardServer* aServer )
       
    68     {
       
    69     _WIMTRACE(_L("WIM|Scard|CScardNotifyRegistry::NewL|Begin"));
       
    70     CScardNotifyRegistry* self = new( ELeave ) CScardNotifyRegistry;
       
    71     
       
    72     CleanupStack::PushL( self );
       
    73     self->ConstructL( aServer );
       
    74     CleanupStack::Pop( self );
       
    75 
       
    76     return self;
       
    77     }
       
    78 
       
    79     
       
    80 // Destructor
       
    81 CScardNotifyRegistry::~CScardNotifyRegistry() 
       
    82     {
       
    83     _WIMTRACE(_L("WIM|Scard|CScardNotifyRegistry::~CScardNotifyRegistry|Begin"));
       
    84     if ( iNotifiers )
       
    85         {
       
    86         iNotifiers->ResetAndDestroy();
       
    87         }
       
    88     delete iNotifiers;
       
    89     }
       
    90 
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // CScardNotifyRegistry::NotifyCardEvent
       
    94 // Notify all notifiers when event occurs
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 EXPORT_C void CScardNotifyRegistry::NotifyCardEvent(
       
    98     TScardServiceStatus aEvent,
       
    99     TReaderID aReaderID )
       
   100     {
       
   101     _WIMTRACE2(_L("WIM|Scard|CScardNotifyRegistry::NotifyCardEvent|Begin, notifierCount=%d"), iNotifiers->Count());
       
   102     // Tell the server of the event
       
   103     CScardNotifier* notifier = (*iNotifiers)[0];
       
   104     notifier->NotifyCardEvent( aEvent, aReaderID );
       
   105 
       
   106     // notify clients (if any)
       
   107     for ( TInt i( 1 ); i < iNotifiers->Count(); i++ )
       
   108         {
       
   109         notifier = (*iNotifiers)[i];
       
   110         if ( notifier->ReaderId() == aReaderID )
       
   111             {
       
   112             _WIMTRACE(_L("WIM|Scard|CScardNotifyRegistry::NotifyCardEvent|NotifyCardEvent Regular starts"));
       
   113             notifier->NotifyCardEvent( aEvent, aReaderID );
       
   114             _WIMTRACE(_L("WIM|Scard|CScardNotifyRegistry::NotifyCardEvent|NotifyCardEvent Regular DONE"));
       
   115 
       
   116             // when creating notifier in AddReaderEventListenerL,
       
   117             // the RMessage pointer is passed as parameter to notifier.
       
   118             // that will be used later to notify the client about smart 
       
   119             // card event.
       
   120             // So that each notifier can only be used once.
       
   121             // so we must remove the notifier from the array.
       
   122             // the scard server's notified can be kept.
       
   123             iNotifiers->Delete( i );
       
   124             delete notifier;
       
   125             }
       
   126         }
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // CScardNotifyRegistry::Server
       
   131 // Return CScardServer object
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 CScardServer* CScardNotifyRegistry::Server()
       
   135     {
       
   136     _WIMTRACE(_L("WIM|Scard|CScardNotifyRegistry::Server|Begin"));
       
   137     return iServer;
       
   138     }
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // CScardNotifyRegistry::Cancel
       
   142 //
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 void CScardNotifyRegistry::Cancel( TRequestStatus& aStatus )
       
   146     {
       
   147     _WIMTRACE(_L("WIM|Scard|CScardNotifyRegistry::Cancel|Begin"));
       
   148     TRequestStatus* sp = &aStatus;
       
   149     User::RequestComplete( sp, KScErrCancelled );
       
   150     }
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // CScardNotifyRegistry::RemoveReaderEventListeners
       
   154 // Remove event listener from registry and delete it
       
   155 // -----------------------------------------------------------------------------
       
   156 //
       
   157 void CScardNotifyRegistry::RemoveReaderEventListeners(
       
   158     const RThread& /*aClient*/ )
       
   159     {
       
   160     _WIMTRACE(_L("WIM|Scard|CScardNotifyRegistry::RemoveReaderEventListeners|Begin"));
       
   161     //find and delete from array
       
   162     TInt numOfItems = iNotifiers->Count();
       
   163 
       
   164     //do while better here EPR
       
   165     for ( TInt i( 1 ); i < numOfItems; i++ )
       
   166         {
       
   167         iNotifiers->Delete( i );
       
   168         }
       
   169     _WIMTRACE(_L("WIM|Scard|CScardNotifyRegistry::RemoveReaderEventListeners|End"));
       
   170     }
       
   171 
       
   172 // -----------------------------------------------------------------------------
       
   173 // CScardNotifyRegistry::RemoveReaderEventListener
       
   174 // Remove event listener from registry and delete it
       
   175 // -----------------------------------------------------------------------------
       
   176 //
       
   177 void CScardNotifyRegistry::RemoveReaderEventListener(
       
   178     const RMessage2& /*aMessage*/ )
       
   179     {
       
   180     _WIMTRACE(_L("WIM|Scard|CScardNotifyRegistry::RemoveReaderEventListener|Begin"));
       
   181 
       
   182     TInt notifierCount = iNotifiers->Count();
       
   183 
       
   184     _WIMTRACE2(_L("WIM|Scard|CScardNotifyRegistry::RemoveReaderEventListener|Notifier count=%d"), notifierCount);
       
   185 
       
   186     //Delete all but our own listener which is the first
       
   187     for ( TInt i( 1 ); i < notifierCount; i++ )
       
   188         {
       
   189         CScardNotifier* notifier = (*iNotifiers)[i];
       
   190         delete notifier;
       
   191         iNotifiers->Delete( i );
       
   192         break;
       
   193         }
       
   194     _WIMTRACE(_L("WIM|Scard|CScardNotifyRegistry::RemoveReaderEventListener|END"));
       
   195 
       
   196     }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CScardNotifyRegistry::AddReaderEventListenerL
       
   200 // Add new event listener
       
   201 // -----------------------------------------------------------------------------
       
   202 //
       
   203 void CScardNotifyRegistry::AddReaderEventListenerL( const RMessage2& aMessage )
       
   204     {
       
   205     _WIMTRACE(_L("WIM|Scard|CScardNotifyRegistry::AddReaderEventListenerL|Begin"));
       
   206 
       
   207     RMessage2 message( aMessage );
       
   208     // extract the TRequestStatus from message
       
   209     TPckgBuf<TRequestStatus*> pckg;
       
   210 
       
   211     aMessage.ReadL( 0, pckg );
       
   212 
       
   213     TRequestStatus* status = pckg();
       
   214     _WIMTRACE2(_L("WIM|Scard|CScardNotifyRegistry::AddReaderEventListenerL|status=%d"), status);
       
   215     
       
   216     //  extract the reader's friendly name
       
   217     TScardReaderName name;
       
   218     aMessage.ReadL( 1, name );
       
   219 
       
   220     const TReaderID readerID = iServer->ReaderID( name );
       
   221 
       
   222     if ( !readerID )
       
   223         {
       
   224         aMessage.Complete( KScErrUnknownReader );
       
   225         _WIMTRACE(_L("WIM|Scard|CScardNotifyRegistry::AddReaderEventListenerL|Leave"));
       
   226         User::Leave( KScErrNotSupported );
       
   227         }
       
   228 
       
   229     // add item to listener table
       
   230     CScardNotifier* notifier2 = CScardNotifier::NewL( this,
       
   231                                                       message,
       
   232                                                       status,
       
   233                                                       readerID );
       
   234     CleanupStack::PushL( notifier2 );
       
   235     iNotifiers->AppendL( notifier2 );
       
   236     CleanupStack::Pop( notifier2 );
       
   237     _WIMTRACE(_L("WIM|Scard|CScardNotifyRegistry::AddReaderEventListenerL|End"));
       
   238     }
       
   239 
       
   240 
       
   241 //  End of File