wim/Scard/src/ScardMessageStack.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 messaging with stack
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    "ScardBase.h"
       
    22 #include    "ScardMessageStack.h"
       
    23 #include    "ScardConnector.h"
       
    24 #include    "WimTrace.h"
       
    25 
       
    26 #ifdef _DEBUG // for logging
       
    27 #include    "ScardLogs.h"
       
    28 #include    <flogger.h> 
       
    29 #endif
       
    30 
       
    31 
       
    32 // ============================ MEMBER FUNCTIONS ===============================
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // CScardMessageStack::CScardMessageStack
       
    36 // C++ default constructor can NOT contain any code, that
       
    37 // might leave.
       
    38 // -----------------------------------------------------------------------------
       
    39 //
       
    40 CScardMessageStack::CScardMessageStack()
       
    41     {
       
    42     _WIMTRACE(_L("WIM|Scard|CScardMessageStack::CScardMessageStack|Begin"));
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CScardMessageStack::ConstructL
       
    47 // Symbian 2nd phase constructor can leave.
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 void CScardMessageStack::ConstructL()
       
    51     {
       
    52     _WIMTRACE(_L("WIM|Scard|CScardMessageStack::ConstructL|Begin"));
       
    53     iMessages = new( ELeave ) CArrayFixFlat<TMessageHandle>( 3 );
       
    54     }
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CScardMessageStack::NewL
       
    58 // Two-phased constructor.
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 CScardMessageStack* CScardMessageStack::NewL()
       
    62     {
       
    63     _WIMTRACE(_L("WIM|Scard|CScardMessageStack::NewL|Begin"));
       
    64     CScardMessageStack* self = new( ELeave ) CScardMessageStack();
       
    65     
       
    66     CleanupStack::PushL( self );
       
    67     self->ConstructL();
       
    68     CleanupStack::Pop( self );
       
    69 
       
    70     return self;
       
    71     }
       
    72 
       
    73     
       
    74 // Destructor
       
    75 CScardMessageStack::~CScardMessageStack()
       
    76     {
       
    77     _WIMTRACE(_L("WIM|Scard|CScardMessageStack::~CScardMessageStack|Begin"));
       
    78     CancelAll( KScErrCancelled );
       
    79     delete iMessages;
       
    80     }
       
    81 
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CScardMessageStack::CancelAll
       
    85 // Cancel all operations for this session ID.
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 void CScardMessageStack::CancelAll(
       
    89     const TInt aSessionID, 
       
    90     const TInt aReason )
       
    91     {
       
    92     _WIMTRACE(_L("WIM|Scard|CScardMessageStack::CancelAll|Begin"));
       
    93     if ( iMessages->Count() )
       
    94         {
       
    95         //  First check if the message currently serviced is one of 
       
    96         //  the cancelled ones (it requires special treatment)
       
    97         if ( (*iMessages)[0].iSessionID == aSessionID ) 
       
    98             {
       
    99             CancelOne( 0, aReason );
       
   100             }
       
   101         //  Then pop out all the others
       
   102         for ( TInt i( 1 ); i < iMessages->Count(); i++ ) 
       
   103             {
       
   104             if ( (*iMessages)[i].iSessionID == aSessionID )
       
   105                 {
       
   106                 CancelOne( i, aReason );
       
   107                 iMessages->Delete( i );
       
   108                 i--;
       
   109                 } // if
       
   110             } // for
       
   111         } // if
       
   112     }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CScardMessageStack::CancelAll
       
   116 // Cancel all messages waiting for service. Delete all others except the 
       
   117 // first one
       
   118 // -----------------------------------------------------------------------------
       
   119 //
       
   120 void CScardMessageStack::CancelAll( const TInt aReason )
       
   121     {
       
   122     _WIMTRACE(_L("WIM|Scard|CScardMessageStack::CancelAll|Begin"));
       
   123     if ( iMessages->Count() )
       
   124         {
       
   125         //  Just cancel the first
       
   126         CancelOne( 0, aReason );
       
   127     
       
   128         //  Then pop out all the others
       
   129         for ( TInt i( 1 ); i < iMessages->Count(); i++ )
       
   130             {
       
   131             CancelOne( i, aReason );
       
   132             iMessages->Delete( i );
       
   133             i--;
       
   134             }
       
   135         }
       
   136     }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 // CScardMessageStack::CancelCardOperations
       
   140 // Effectively the same as the above, except that does not cancel 
       
   141 // OpenReader commands...
       
   142 // -----------------------------------------------------------------------------
       
   143 //
       
   144 void CScardMessageStack::CancelCardOperations()
       
   145     {
       
   146     _WIMTRACE(_L("WIM|Scard|CScardMessageStack::CancelCardOperations|Begin"));
       
   147     if ( iMessages->Count() )
       
   148         {
       
   149         //  Just cancel the first
       
   150         if ( iMessages->At( 0 ).iAdditionalParameter != KOpenReader )
       
   151             {
       
   152             CancelOne( 0, KScReaderErrNoCard );
       
   153             }
       
   154     
       
   155         //  Then pop out all the others
       
   156         for ( TInt i( 1 ); i < iMessages->Count(); i++ )
       
   157             {
       
   158             if ( iMessages->At( i ).iAdditionalParameter != KOpenReader )
       
   159                 {
       
   160                 CancelOne( i, KScReaderErrNoCard );
       
   161                 iMessages->Delete( i );
       
   162                 i--;
       
   163                 }
       
   164             }
       
   165         }
       
   166     }
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // CScardMessageStack::CancelByTimeOut
       
   170 // Cancel message by timeout
       
   171 // -----------------------------------------------------------------------------
       
   172 //
       
   173 void CScardMessageStack::CancelByTimeOut( const CScardCommandTimer* aTimer )
       
   174     {
       
   175     _WIMTRACE(_L("WIM|Scard|CScardMessageStack::CancelByTimeOut|Begin"));
       
   176 #ifdef _DEBUG
       
   177     RFileLogger::WriteFormat( KScardLogDir, KScardLogFileName, 
       
   178         EFileLoggingModeAppend, _L( "CScardMessageStack::CancelByTimeOut\n" ) );
       
   179 #endif
       
   180 
       
   181     if ( iMessages->Count() )
       
   182         {
       
   183         //  First check if the message currently serviced is the cancelled one
       
   184         //  (it requires special treatment)
       
   185         if ( (*iMessages)[0].iTimer == aTimer )
       
   186             {
       
   187             CancelOne( 0, KScErrTimeOut, EFalse );
       
   188             return;
       
   189             }
       
   190 
       
   191         //  Then check out all the others
       
   192         for ( TInt i( 1 ); i < iMessages->Count(); i++ )
       
   193             {
       
   194             if ( (*iMessages)[i].iTimer == aTimer )
       
   195                 {
       
   196                 CancelOne( i, KScErrTimeOut, EFalse );
       
   197                 iMessages->Delete( i );
       
   198                 return;
       
   199                 } // if
       
   200             } // for
       
   201         } // if
       
   202     }
       
   203 
       
   204 // -----------------------------------------------------------------------------
       
   205 // CScardMessageStack::PushToTop
       
   206 // Push message to top
       
   207 // -----------------------------------------------------------------------------
       
   208 //
       
   209 TInt CScardMessageStack::PushToTop( const TMessageHandle& aMessage )
       
   210     {
       
   211     _WIMTRACE(_L("WIM|Scard|CScardMessageStack::PushToTop|Begin"));
       
   212     TRAPD( err, iMessages->InsertL( 0, aMessage ) );
       
   213     _WIMTRACE2(_L("WIM|Scard|CScardMessageStack::PushToTop|End|err=%d"), err);
       
   214     return err;
       
   215     }
       
   216 
       
   217 // -----------------------------------------------------------------------------
       
   218 // CScardMessageStack::FromTop
       
   219 // Message from top
       
   220 // -----------------------------------------------------------------------------
       
   221 //
       
   222 const TMessageHandle CScardMessageStack::FromTop()
       
   223     {
       
   224     _WIMTRACE(_L("WIM|Scard|CScardMessageStack::FromTop|Begin"));
       
   225     if ( !iMessages->Count() )
       
   226         {
       
   227         //  returns the default, which is interpreted as an error
       
   228         TMessageHandle a;
       
   229         return a;
       
   230         }
       
   231     TMessageHandle tmp = (*iMessages)[0];
       
   232     iMessages->Delete( 0 );
       
   233     return tmp;
       
   234     }
       
   235 
       
   236 // -----------------------------------------------------------------------------
       
   237 // CScardMessageStack::PushToBottom
       
   238 // Push message to bottom
       
   239 // -----------------------------------------------------------------------------
       
   240 //
       
   241 TInt CScardMessageStack::PushToBottom( const TMessageHandle& aMessage )
       
   242     {
       
   243     _WIMTRACE(_L("WIM|Scard|CScardMessageStack::PushToBottom|Begin"));
       
   244     TRAPD( err, iMessages->AppendL( aMessage ) );
       
   245     return err;
       
   246     }
       
   247 
       
   248 // -----------------------------------------------------------------------------
       
   249 // CScardMessageStack::NextReservation
       
   250 // This function is called when a channel is freed. It gives priority
       
   251 // for applications who are waiting to reserve either this channel or
       
   252 // any channel
       
   253 // -----------------------------------------------------------------------------
       
   254 //
       
   255 const TMessageHandle CScardMessageStack::NextReservation( const TInt8 aChannel )
       
   256     {
       
   257     _WIMTRACE(_L("WIM|Scard|CScardMessageStack::NextReservation|Begin"));
       
   258     TMessageHandle tmp;
       
   259     for ( TInt i( 0 ); i < iMessages->Count(); i++ )
       
   260         {
       
   261         tmp = (*iMessages)[i];
       
   262         if ( ( tmp.iAdditionalParameter & KReservation ) && 
       
   263            ( tmp.iChannel == aChannel || tmp.iChannel == KAllChannels ) )
       
   264             {
       
   265             iMessages->Delete( i );
       
   266             return tmp;
       
   267             }
       
   268         }
       
   269     tmp.iSessionID = ENoSession;
       
   270     return tmp;
       
   271     }
       
   272 
       
   273 // -----------------------------------------------------------------------------
       
   274 // CScardMessageStack::FromPositionL
       
   275 // Get message from given position
       
   276 // -----------------------------------------------------------------------------
       
   277 //
       
   278 const TMessageHandle CScardMessageStack::FromPositionL( const TInt aPosition )
       
   279     {
       
   280     _WIMTRACE(_L("WIM|Scard|CScardMessageStack::FromPositionL|Begin"));
       
   281     __ASSERT_ALWAYS( aPosition >= 0 && aPosition < iMessages->Count(), 
       
   282         User::Leave( KScErrBadArgument ) );
       
   283     TMessageHandle r = iMessages->At( aPosition );
       
   284     iMessages->Delete( aPosition );
       
   285     return r;
       
   286     }
       
   287 
       
   288 // -----------------------------------------------------------------------------
       
   289 // CScardMessageStack::PushToPositionL
       
   290 // Push message to given position
       
   291 // -----------------------------------------------------------------------------
       
   292 //
       
   293 void CScardMessageStack::PushToPositionL(
       
   294     const TInt aPosition, 
       
   295     TMessageHandle& aHandle )
       
   296     {
       
   297     _WIMTRACE(_L("WIM|Scard|CScardMessageStack::PushToPositionL|Begin"));
       
   298     __ASSERT_ALWAYS( aPosition >= 0 && aPosition <= iMessages->Count(), 
       
   299         User::Leave( KScErrBadArgument ) );
       
   300     iMessages->InsertL( aPosition, aHandle );
       
   301     }
       
   302 
       
   303 //  End of File