systemswstubs/examplecommonisc/IscDriver/src/IscChannelContainer.cpp
changeset 43 e71858845f73
parent 40 b7e5ed8c1342
child 46 e1758cbb96ac
equal deleted inserted replaced
40:b7e5ed8c1342 43:e71858845f73
     1 /*
       
     2 * Copyright (c) 2005 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:  An example implementation for ISC Driver Reference
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 
       
    22 #include <kernel.h>
       
    23 #include <IscDefinitions.h>
       
    24 #include "IscChannel.h"
       
    25 #include "IscChannelContainer.h"
       
    26 #include "IscTrace.h"
       
    27 
       
    28 #ifdef __WINS__
       
    29 #include <windows.h>
       
    30 #endif
       
    31 
       
    32 // EXTERNAL DATA STRUCTURES
       
    33 
       
    34 // EXTERNAL FUNCTION PROTOTYPES  
       
    35 
       
    36 // CONSTANTS
       
    37 const TInt KChannelComplitionDfcPriority( 2 );
       
    38 
       
    39 // MACROS
       
    40 
       
    41 // LOCAL CONSTANTS AND MACROS
       
    42 DIscChannel* IscChannelContainer::iIscChannels[KIscNumberOfUnits][KIscMaxNumberOfChannelSharers];
       
    43 TDfc* IscChannelContainer::iDfc = NULL;
       
    44 
       
    45 // MODULE DATA STRUCTURES
       
    46 
       
    47 // LOCAL FUNCTION PROTOTYPES
       
    48 
       
    49 // FORWARD DECLARATIONS
       
    50 #ifdef __WINS__
       
    51 CRITICAL_SECTION g_IscCriticalSection;
       
    52 #endif
       
    53 
       
    54 // ============================ MEMBER FUNCTIONS ===============================
       
    55 
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // IscChannelContainer::SetChannel
       
    59 // Set pointer to channel
       
    60 // ( other items were commented in a header ).
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 EXPORT_C TInt IscChannelContainer::SetChannel( 
       
    64     DIscChannel* aChannel,
       
    65     const TUint16 aUnit )
       
    66     {
       
    67     C_TRACE( ( _T( "IscChannelContainer::SetChannel(0x%x, 0x%x)" ), aChannel, aUnit ) );
       
    68     TInt error( KErrNoMemory );
       
    69 
       
    70     //check values
       
    71     if ( aUnit < KIscNumberOfUnits )
       
    72         {
       
    73         for ( TInt i( 0 ); i < KIscMaxNumberOfChannelSharers; i++ )
       
    74             {
       
    75             if ( !iIscChannels[aUnit][i] )
       
    76                 {
       
    77                 iIscChannels[aUnit][i] = aChannel;
       
    78                 error = KErrNone;
       
    79                 break;
       
    80                 }
       
    81             }
       
    82         }
       
    83     else
       
    84         {
       
    85         ASSERT_RESET_ALWAYS( 0, "IscDriver",EIscPanicSetChannel );
       
    86         }
       
    87     
       
    88     C_TRACE( ( _T( "IscChannelContainer::SetChannel - return 0x%x" ), error ) );
       
    89     return error;    
       
    90     }
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // IscChannelContainer::Channel
       
    94 // Return pointer to channel
       
    95 // ( other items were commented in a header ).
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 DIscChannel* IscChannelContainer::Channel( 
       
    99     const TUint16 aChannel, 
       
   100     const TUint16 aChannelSharingIndex )    
       
   101     {
       
   102     TRACE_ASSERT( aChannel < KIscNumberOfUnits );
       
   103     TRACE_ASSERT( aChannelSharingIndex < KIscMaxNumberOfChannelSharers );
       
   104     return( iIscChannels[ aChannel ][ aChannelSharingIndex ] );    
       
   105     }
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // IscChannelContainer::RemoveChannel
       
   109 // 
       
   110 // ( other items were commented in a header ).
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 TInt IscChannelContainer::RemoveChannel
       
   114 		( 
       
   115     	DIscChannel* aChannel
       
   116     	)
       
   117     {
       
   118     
       
   119     TRACE_ASSERT( aChannel );
       
   120     C_TRACE( ( _T( "IscChannelContainer::RemoveChannel(0x%x)" ), aChannel ) );
       
   121     TUint16 channelNumber = aChannel->GetChannelNumber();
       
   122 
       
   123     TInt error( KErrNotFound );
       
   124     for ( TInt i( KErrNone ); ( i < KIscMaxNumberOfChannelSharers ) && ( KErrNone != error ); i++ )
       
   125         {
       
   126         if ( iIscChannels[channelNumber][i] == aChannel )
       
   127             {
       
   128             iIscChannels[channelNumber][i] = NULL;
       
   129             error = KErrNone;
       
   130             }
       
   131         }
       
   132 
       
   133     C_TRACE( ( _T( "IscChannelContainer::RemoveChannel - return %d" ), error ) );
       
   134     return error;
       
   135     
       
   136     }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 // IscChannelContainer::ChannelComplition
       
   140 // Goes through channels and open ones are emptied
       
   141 // ( other items were commented in a header ).
       
   142 // -----------------------------------------------------------------------------
       
   143 //
       
   144 void IscChannelContainer::ChannelComplition( 
       
   145     TAny* /*a*/ )
       
   146     {
       
   147     C_TRACE( ( _T( "IscChannelContainer::ChannelComplition()" ) ) );
       
   148 #ifdef __WINS__
       
   149     EnterCriticalSection( &g_IscCriticalSection );
       
   150 #endif
       
   151     
       
   152     DIscChannel* channelPtr = NULL;
       
   153 
       
   154     for ( TUint16 channel( KIscFirstChannel ) ; channel < KIscNumberOfUnits; channel++ )
       
   155         {
       
   156         for ( TUint16 i( 0 ); i < KIscMaxNumberOfChannelSharers; i++ )
       
   157             {
       
   158             channelPtr = iIscChannels[channel][i];
       
   159             if ( channelPtr )
       
   160                 {
       
   161                 channelPtr->EmptyBuffers();
       
   162                 }
       
   163             }
       
   164         }
       
   165 #ifdef __WINS__
       
   166     LeaveCriticalSection( &g_IscCriticalSection );
       
   167 #endif
       
   168 
       
   169     C_TRACE( ( _T( "IscChannelContainer::ChannelComplition - return 0x%x" ), KErrNone ) );
       
   170     
       
   171     }
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // IscChannelContainer::AddDfc
       
   175 // Adds ChannelComplition Dfc
       
   176 // ( other items were commented in a header ).
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 void IscChannelContainer::AddDfc()
       
   180     {
       
   181     C_TRACE( ( _T( "IscChannelContainer::AddDfc()" ) ) );
       
   182 
       
   183     if ( NKern::CurrentContext() == NKern::EInterrupt )
       
   184         {
       
   185         iDfc->Add();
       
   186         }
       
   187     else
       
   188         {
       
   189         iDfc->Enque();
       
   190         }    
       
   191     
       
   192     }
       
   193 
       
   194 // -----------------------------------------------------------------------------
       
   195 // IscChannelContainer::Initialize
       
   196 // Allocates Dfc function dynamically and formats data tables.
       
   197 // ( other items were commented in a header ).
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 void IscChannelContainer::Initialize()
       
   201     {    
       
   202     C_TRACE( ( _T( "IscChannelContainer::Initialize()" ) ) );
       
   203 
       
   204     if ( !iDfc )
       
   205         {
       
   206         iDfc = new TDfc( IscChannelContainer::ChannelComplition, NULL, Kern::DfcQue0(),
       
   207                          KChannelComplitionDfcPriority );
       
   208         ASSERT_RESET_ALWAYS( iDfc, "IscDriver",EIscMemoryAllocationFailure );
       
   209         }
       
   210     
       
   211     for ( TUint16 channel( 0 ); channel < KIscNumberOfUnits; channel++ )
       
   212         {
       
   213         for ( TUint16 i( 0 ); i < KIscMaxNumberOfChannelSharers; i++ )
       
   214             {
       
   215             iIscChannels[channel][i] = NULL;
       
   216             }
       
   217         }
       
   218 
       
   219 #ifdef __WINS__
       
   220     InitializeCriticalSection( &g_IscCriticalSection );
       
   221 #endif
       
   222     }
       
   223 
       
   224 // -----------------------------------------------------------------------------
       
   225 // IscChannelContainer::DeActivate
       
   226 // Deallocates Dfc function
       
   227 // ( other items were commented in a header ).
       
   228 // -----------------------------------------------------------------------------
       
   229 //
       
   230 void IscChannelContainer::DeActivate()
       
   231     {    
       
   232     if ( iDfc )
       
   233         {
       
   234         delete iDfc;
       
   235         iDfc = NULL;
       
   236         }
       
   237 #ifdef __WINS__
       
   238     DeleteCriticalSection( &g_IscCriticalSection );
       
   239 #endif
       
   240     
       
   241     }
       
   242 
       
   243 // -----------------------------------------------------------------------------
       
   244 // IscChannelContainer::ValidateChannel
       
   245 // 
       
   246 // (other items were commented in a header).
       
   247 // -----------------------------------------------------------------------------
       
   248 //
       
   249 TInt IscChannelContainer::ValidateChannel( 
       
   250     DIscChannel* aChannelPtr )
       
   251     {
       
   252     C_TRACE( ( _T( "IscChannelContainer::ValidateChannel(0x%x)" ), aChannelPtr ) );
       
   253     TInt error( KErrNotFound );
       
   254     if( !aChannelPtr )
       
   255     	{
       
   256     	C_TRACE( ( _T( "IscChannelContainer::ValidateChannel aChannelPtr was NULL" ) ) );
       
   257     	error = KErrArgument;	
       
   258     	}
       
   259     else
       
   260     	{
       
   261 	    for ( TUint16 channelNumber( 0 ); channelNumber < KIscNumberOfUnits ; channelNumber++ )
       
   262 	        {
       
   263 	        for ( TInt i( KErrNone ); i < KIscMaxNumberOfChannelSharers ; i++ )
       
   264 	            {
       
   265 	            if ( iIscChannels[channelNumber][i] == aChannelPtr )
       
   266 	                {
       
   267 	                error = KErrNone;
       
   268 	                break;
       
   269 	                }
       
   270 	            }
       
   271 	        }    		
       
   272     	}
       
   273 
       
   274     C_TRACE( ( _T( "IscChannelContainer::ValidateChannel - return %d" ), error ) );
       
   275     return error;
       
   276     
       
   277     }
       
   278 
       
   279 //  End of File