systemswstubs/examplecommonisc/IscDriver/src/IscMainRcvBuffer.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 #include <IscDefinitions.h>
       
    22 #include <IscMultiplexerBase.h>
       
    23 #include "IscMainRcvBuffer.h"
       
    24 #include "IscQueue.h"
       
    25 #include "IscDevice.h"
       
    26 #include "IscChannel.h"
       
    27 #include "IscChannelContainer.h"
       
    28 #include "IscTrace.h"
       
    29 
       
    30 
       
    31 // EXTERNAL DATA STRUCTURES
       
    32 
       
    33 // EXTERNAL FUNCTION PROTOTYPES  
       
    34 
       
    35 // CONSTANTS
       
    36 DIscMainRcvBuffer* DIscMainRcvBuffer::iThisPointer = NULL;
       
    37 const TInt KBufferCleanUpDfcPriority( 4 );
       
    38 
       
    39 // MACROS
       
    40 
       
    41 // LOCAL CONSTANTS AND MACROS
       
    42 
       
    43 // MODULE DATA STRUCTURES
       
    44 
       
    45 // LOCAL FUNCTION PROTOTYPES
       
    46 
       
    47 // FORWARD DECLARATIONS
       
    48 
       
    49 
       
    50 // ============================ MEMBER FUNCTIONS ===============================
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // DIscMainRcvBuffer::DIscMainRcvBuffer
       
    54 // C++ default constructor
       
    55 // ( other items were commented in a header ).
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 DIscMainRcvBuffer::DIscMainRcvBuffer( DIscDevice *aDevice, TUint16 aIscMainRcvBufferQueueSize )
       
    59     :iDevice ( aDevice ),
       
    60      iDfc( NULL ),
       
    61      iMainRcvBufferQueue( NULL )
       
    62     {
       
    63     iIscMainRcvBufferQueueSize = aIscMainRcvBufferQueueSize;
       
    64     iMainRcvBuffer = new TUint32*[iIscMainRcvBufferQueueSize];
       
    65     ASSERT_RESET_ALWAYS( iMainRcvBuffer, "IscDriver",EIscMemoryAllocationFailure );
       
    66 
       
    67     for ( TInt i = 0; i < iIscMainRcvBufferQueueSize; i++ )
       
    68         {
       
    69         iMainRcvBuffer[i] = NULL;
       
    70         }
       
    71     }
       
    72 
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // DIscMainRcvBuffer::DoCreate
       
    76 // Second-phase constructor to complete construction.
       
    77 // ( other items were commented in a header ).
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 void DIscMainRcvBuffer::DoCreate()
       
    81     {
       
    82     if ( iThisPointer ) //Only one instance of this class is allowed
       
    83         {
       
    84         ASSERT_RESET_ALWAYS( 0,"IscDriver",EIscMainRcvBufferInitialize );
       
    85         }
       
    86 
       
    87     iDfc = new TDfc( BufferCleanUp, this, Kern::DfcQue0(), KBufferCleanUpDfcPriority );
       
    88     ASSERT_RESET_ALWAYS( iDfc, "IscDriver",EIscMemoryAllocationFailure );
       
    89     
       
    90     iMainRcvBufferQueue = new DIscQueue( iMainRcvBuffer, iIscMainRcvBufferQueueSize );
       
    91     ASSERT_RESET_ALWAYS( iMainRcvBufferQueue, "IscDriver",EIscMemoryAllocationFailure );
       
    92 
       
    93     //Store pointer of this instance that can be used from static functions
       
    94     iThisPointer = this;
       
    95     }
       
    96 
       
    97 //Destructor
       
    98 DIscMainRcvBuffer::~DIscMainRcvBuffer()
       
    99     {
       
   100     delete iDfc;
       
   101     delete iMainRcvBufferQueue;
       
   102     iDfc = NULL;
       
   103     iMainRcvBufferQueue = NULL;
       
   104     iThisPointer = NULL;
       
   105     iDevice = NULL;
       
   106     }
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // DIscMainRcvBuffer::MsgReceive
       
   110 // Function that should be called to store incoming frame
       
   111 // ( other items were commented in a header ).
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 void DIscMainRcvBuffer::MsgReceive( TDesC8* aData )
       
   115     {    
       
   116     E_TRACE( ( _T( "IMRB:MR(0x%x)" ), aData ) );
       
   117     //Check that instance of this class is created
       
   118     if ( !iThisPointer )
       
   119         {
       
   120         // MainRcvBuffer not initialized -> panic
       
   121         ASSERT_RESET_ALWAYS( 0,"IscDriver",EIscMainRcvBufferInitialize );
       
   122         }
       
   123     
       
   124     TInt r = iThisPointer->iMainRcvBufferQueue->Add( aData );
       
   125     ASSERT_RESET_ALWAYS( r == KErrNone, "IscDriver",EIscMainRcvBufferOverflow );
       
   126     iThisPointer->AddDfc();
       
   127     }
       
   128 
       
   129 // -----------------------------------------------------------------------------
       
   130 // DIscMainRcvBuffer::AddDfc
       
   131 // Function for adding Dfc
       
   132 // ( other items were commented in a header ).
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 void DIscMainRcvBuffer::AddDfc()
       
   136     {    
       
   137 #ifndef __WINS__
       
   138     if ( NKern::CurrentContext() == NKern::EInterrupt )
       
   139         {
       
   140         iDfc->Add();
       
   141         }
       
   142     else
       
   143         {
       
   144         iDfc->Enque();
       
   145         }
       
   146 #else
       
   147     // Have to do DoEnque since Enque() & Add() cannot be called from pure 
       
   148     // win32 thread context. And this call might come directly in win32 thread
       
   149     // since the implementation of ISC Data Transmission API can do virtually 
       
   150     // anything in WINS
       
   151     iDfc->DoEnque();
       
   152 #endif
       
   153     }
       
   154 
       
   155 // -----------------------------------------------------------------------------
       
   156 // DIscMainRcvBuffer::BufferCleanUp
       
   157 // Function to empty messages in main buffer
       
   158 // ( other items were commented in a header ).
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 void DIscMainRcvBuffer::BufferCleanUp( TAny* aPtr )
       
   162     {
       
   163     C_TRACE( ( _T( "DIscMainRcvBuffer::BufferCleanUp(0x%x)" ), aPtr ) );
       
   164     
       
   165     DIscMainRcvBuffer* Buffer = ( DIscMainRcvBuffer* )aPtr;
       
   166     
       
   167     
       
   168     TUint16 rcvChannelId( KIscFrameReceiverNotFound );
       
   169     TAny* channelPtr = NULL;
       
   170     DIscChannel* tempPtr = NULL;
       
   171     TBool channelFound( EFalse );
       
   172 
       
   173     TDesC8* ptr = NULL;
       
   174     ptr = ( TDesC8* )Buffer->iMainRcvBufferQueue->RemoveFirst();
       
   175         
       
   176     while ( ptr )
       
   177         {
       
   178         Buffer->iDevice->iIscMultiplexerInterface->GetRcvChannelId( *ptr, rcvChannelId, channelPtr );
       
   179     
       
   180         C_TRACE( ( _T( "DIscMainRcvBuffer::BufferCleanUp channelId (0x%x) channelPtr (0x%x)" ), rcvChannelId, channelPtr ) );
       
   181         if ( rcvChannelId >= KIscFirstChannel &&  rcvChannelId < KIscNumberOfUnits && channelPtr )
       
   182             {
       
   183             channelFound = EFalse;
       
   184             for ( TUint16 i( 0 ); ( i < KIscMaxNumberOfChannelSharers ) && ( !channelFound ); i++ )
       
   185                 {
       
   186                 tempPtr = IscChannelContainer::Channel( rcvChannelId, i );
       
   187                 if ( tempPtr == channelPtr )
       
   188                     {
       
   189                     // correct channel found
       
   190                     ( ( DIscChannel* )channelPtr )->StoreFrame( ptr );
       
   191                     channelFound = ETrue;
       
   192                     }
       
   193                 tempPtr = NULL;
       
   194                 }
       
   195             if ( !channelFound )
       
   196                 {
       
   197                 TRACE_ASSERT_ALWAYS;
       
   198                 // correct channel was not found -> release frame
       
   199                 Buffer->iDevice->ReleaseMemoryBlock( ( TDes8* ) ptr );
       
   200                 }
       
   201             }
       
   202         else if ( rcvChannelId == KIscFrameReceiverNotFound )
       
   203             {
       
   204             C_TRACE( ( _T( "DIscMainRcvBuffer::BufferCleanUp Frame Receiver not found!" ) ) );
       
   205             Buffer->iDevice->ReleaseMemoryBlock( ( TDes8* ) ptr );
       
   206             }
       
   207         // Frame going to control channel
       
   208         else if ( rcvChannelId == 0x00 )
       
   209             {
       
   210             Buffer->iDevice->iIscMultiplexerInterface->HandleControlFrame( *ptr );
       
   211             }
       
   212         else
       
   213             {
       
   214             TRACE_ASSERT_ALWAYS;
       
   215             Buffer->iDevice->ReleaseMemoryBlock( ( TDes8* ) ptr );
       
   216             }
       
   217 
       
   218         // get the next frame from the queue
       
   219         ptr = ( TDes8* )Buffer->iMainRcvBufferQueue->RemoveFirst();
       
   220         }
       
   221 
       
   222     IscChannelContainer::ChannelComplition( NULL );
       
   223 
       
   224     C_TRACE( ( _T( "DIscMainRcvBuffer::BufferCleanUp - return 0x%x" ) ) );
       
   225 
       
   226     }
       
   227 
       
   228 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   229 
       
   230 //  End of File