wim/Scard/src/ScardNotifier.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 of events
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    "ScardServer.h"
       
    22 #include    "ScardNotifier.h"
       
    23 #include    "WimTrace.h"
       
    24 
       
    25 // ============================ MEMBER FUNCTIONS ===============================
       
    26 
       
    27 // -----------------------------------------------------------------------------
       
    28 // CScardNotifier::CScardNotifier
       
    29 // C++ default constructor can NOT contain any code, that
       
    30 // might leave.
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 CScardNotifier::CScardNotifier(
       
    34     CScardNotifyRegistry* aRegistry, 
       
    35     RMessage2& aMessage,
       
    36     TRequestStatus* aClientStatus,
       
    37     const TReaderID& aReaderID )
       
    38     : CActive( EPriorityNormal ), 
       
    39       iRegistry( aRegistry ),
       
    40       iMessage( aMessage ),
       
    41       iClientStatus( aClientStatus ),
       
    42       iReaderID( aReaderID ),
       
    43       iMessageCompleted( EFalse )
       
    44     {
       
    45     _WIMTRACE(_L("WIM|Scard|CScardNotifier::CScardNotifier|Begin"));
       
    46     CActiveScheduler::Add( this );
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CScardNotifier::ConstructL
       
    51 // Symbian 2nd phase constructor can leave.
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 void CScardNotifier::ConstructL()
       
    55     {
       
    56     _WIMTRACE(_L("WIM|Scard|CScardNotifier::ConstructL|Begin"));
       
    57     //if the notifier is regular, no need to activate the ActiveObject.
       
    58     //since the notifier can only be used once and will be deleted after
       
    59     //the one-time notification is done.
       
    60     if(!iClientStatus)
       
    61        {
       
    62        iEventStack = new( ELeave ) CArrayFixFlat<TCardEvent>( 1 );
       
    63        iStatus = KRequestPending; 	
       
    64        SetActive();
       
    65        }
       
    66     }
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // CScardNotifier::NewL
       
    70 // Two-phased constructor.
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 CScardNotifier* CScardNotifier::NewL(
       
    74     CScardNotifyRegistry* aRegistry,
       
    75     RMessage2& aMessage,
       
    76     TRequestStatus* aClientStatus,
       
    77     const TReaderID& aReaderID )
       
    78     {
       
    79     _WIMTRACE(_L("WIM|Scard|CScardNotifier::NewL|Begin"));
       
    80     CScardNotifier* self = new( ELeave ) CScardNotifier( 
       
    81         aRegistry, aMessage, aClientStatus, aReaderID );
       
    82     
       
    83     CleanupStack::PushL( self );
       
    84     self->ConstructL();
       
    85     CleanupStack::Pop( self );
       
    86 
       
    87     return self;
       
    88     }
       
    89 
       
    90     
       
    91 // Destructor
       
    92 CScardNotifier::~CScardNotifier()
       
    93     {
       
    94     _WIMTRACE(_L("WIM|Scard|CScardNotifier::~CScardNotifier|Begin"));
       
    95     if(!iClientStatus)
       
    96        {
       
    97        delete iEventStack;
       
    98        Cancel();
       
    99        }
       
   100     else
       
   101        {
       
   102        if ( !iMessageCompleted ) 
       
   103           {
       
   104           iMessage.Complete( KErrNone );
       
   105           }	
       
   106        }
       
   107     _WIMTRACE(_L("WIM|Scard|CScardNotifier::~CScardNotifier|End"));
       
   108     }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CScardNotifier::NotifyCardEvent
       
   112 // Complete our own request, or if that's done already, store the event
       
   113 // until it can be serviced...
       
   114 // -----------------------------------------------------------------------------
       
   115 //
       
   116 void CScardNotifier::NotifyCardEvent(
       
   117     TScardServiceStatus aEvent,
       
   118     TReaderID aReaderID )
       
   119     {
       
   120     _WIMTRACE(_L("WIM|Scard|CScardNotifier::NotifyCardEvent|Begin"));
       
   121     
       
   122     //regular notifier
       
   123     if( iClientStatus )
       
   124        {
       
   125        _WIMTRACE(_L("WIM|Scard|CScardNotifier::NotifyCardEvent| a regular notifier"));
       
   126        if(iMessageCompleted )
       
   127           {
       
   128        	  return;
       
   129           }
       
   130        _WIMTRACE(_L("WIM|Scard|CScardNotifier::NotifyCardEvent|iMessage not used"));   
       
   131        _WIMTRACE(_L("WIM|Scard|CScardNotifier::NotifyCardEvent|Notify client"));
       
   132        iMessage.Complete( aEvent );
       
   133        iMessageCompleted = ETrue;
       
   134        _WIMTRACE(_L("WIM|Scard|CScardNotifier::NotifyCardEvent|Notify client DONE"));
       
   135        return;	
       
   136        }
       
   137     
       
   138     _WIMTRACE(_L("WIM|Scard|CScardNotifier::NotifyCardEvent| Server notifier"));   
       
   139     //  No need to store the event, just notify it
       
   140     if ( iStatus == KRequestPending )
       
   141         {
       
   142         TUint code( 0 );
       
   143         
       
   144         //  If this is the server notifier, 
       
   145         //  it needs a different event encoding
       
   146         if ( !iClientStatus )
       
   147             {
       
   148             //  The server needs to know both the event code and 
       
   149             //  the reader ID, because it listens to all readers
       
   150 
       
   151             //  Place the reader ID in the higher bytes...
       
   152             code = aReaderID;
       
   153             code <<= 4;
       
   154             //  ...and the event code in the low 4 bytes
       
   155             code |= aEvent;
       
   156             }
       
   157         TRequestStatus* sp = &iStatus;
       
   158         User::RequestComplete( sp, code );
       
   159         }
       
   160     //  Well, previous notify hasn't finished, so store the event
       
   161     else
       
   162         {
       
   163         TCardEvent event;
       
   164         event.iEvent = aEvent;
       
   165         event.iReaderID = aReaderID;
       
   166         TRAPD( err, iEventStack->AppendL( event ) );
       
   167 
       
   168         if ( err != KErrNone ) // No reason to continue
       
   169             {
       
   170             User::Panic( _L( "Memory allocation error" ), err );
       
   171             }
       
   172         }
       
   173     }
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CScardNotifier::RunL
       
   177 // Asynchronous request has finished
       
   178 // -----------------------------------------------------------------------------
       
   179 //
       
   180 void CScardNotifier::RunL()
       
   181     {
       
   182     _WIMTRACE2(_L("WIM|Scard|CScardNotifier::RunL|Begin, iClientStatus=%d"), iClientStatus);
       
   183     //  if this is a regular notifier, 
       
   184     //  then some thing goes wrong.
       
   185     //  only server notifier should come to RunL().
       
   186     if ( iClientStatus )
       
   187         {
       
   188         _WIMTRACE(_L("WIM|Scard|CScardNotifier::RunL|Only server notifer will come to RunL"));
       
   189         }
       
   190     //  Otherwise this is the server notifier, so tell the server
       
   191     else
       
   192         {
       
   193         _WIMTRACE(_L("WIM|Scard|CScardNotifier::RunL|server notifer"));
       
   194         // the bit-shifting that was done when notifying is now reversed
       
   195         TScardServiceStatus event = static_cast< TScardServiceStatus >
       
   196                                                 ( iStatus.Int() & 0x0f );
       
   197         TReaderID readerID = static_cast< TReaderID >( iStatus.Int() >> 4 );
       
   198         iRegistry->Server()->CardEvent( event, readerID );
       
   199         _WIMTRACE(_L("WIM|Scard|CScardNotifier::RunL|Server notification DONE"));
       
   200         }
       
   201     iStatus = KRequestPending;
       
   202     SetActive();
       
   203 
       
   204     //  If there are any queued events, notify (first one of) them now
       
   205     if ( iEventStack->Count() )
       
   206         {
       
   207         _WIMTRACE(_L("WIM|Scard|CScardNotifier::RunL| Some queued event in server notifier"));
       
   208         iRegistry->NotifyCardEvent( iEventStack->At( 0 ).iEvent, 
       
   209             iEventStack->At( 0 ).iReaderID );
       
   210         iEventStack->Delete( 0 );
       
   211         }
       
   212     _WIMTRACE(_L("WIM|Scard|CScardNotifier::RunL|Exit"));
       
   213     }
       
   214 
       
   215 // -----------------------------------------------------------------------------
       
   216 // CScardNotifier::DoCancel
       
   217 // Active object cancelled
       
   218 // -----------------------------------------------------------------------------
       
   219 //
       
   220 void CScardNotifier::DoCancel()
       
   221     {
       
   222     _WIMTRACE3(_L("WIM|Scard|CScardNotifier::DoCancel|Begin, status=%d"),
       
   223     iStatus.Int(), iClientStatus );
       
   224     iRegistry->Cancel( iStatus );
       
   225     _WIMTRACE(_L("WIM|Scard|CScardNotifier::DoCancel|Cancel called"));
       
   226 
       
   227     // If message not already completed do it here
       
   228     if ( !iMessageCompleted && iClientStatus ) 
       
   229         {
       
   230         iMessage.Complete( KErrCancel );
       
   231         _WIMTRACE(_L("WIM|Scard|CScardNotifier::DoCancel|Message completed"));
       
   232         }
       
   233     
       
   234     _WIMTRACE(_L("WIM|Scard|CScardNotifier::DoCancel|End"));
       
   235     }
       
   236 
       
   237 // -----------------------------------------------------------------------------
       
   238 // CScardNotifier::ReaderID
       
   239 // Return readerID
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 TReaderID CScardNotifier::ReaderId()
       
   243     {
       
   244     return iReaderID;
       
   245     }
       
   246 //  End of File