DISIKernelChannel::~DISIKernelChannel(
    // None
)
{
    C_TRACE( ( _T( "DISIKernelChannel::~DISIKernelChannel 0x%x 0x%x>" ), this ) );
    // Send channel destroyed message to complete with EFalse.
    TThreadMessage& m=Kern::Message();
    m.iValue = KDestroyChannelMsg;
    m.SendReceive( &iKernelChMsgQue );
    C_TRACE( ( _T( "DISIKernelChannel::~DISIKernelChannel iRx 0x%x" ), iRx ) );
    // Only modified in constructor, if not created already reseted.
    delete iRx;
    iRx = NULL;
    C_TRACE( ( _T( "DISIKernelChannel::~DISIKernelChannel iEmptyRxDfc 0x%x" ), iEmptyRxDfc ) );
    // Only modified in constructor, if not created already reseted.
    if ( iEmptyRxDfc )
    {
        iEmptyRxDfc->Cancel();
        delete iEmptyRxDfc;
        iEmptyRxDfc = NULL;
    }
    C_TRACE( ( _T( "DISIKernelChannel::~DISIKernelChannel iRequests 0x%x" ), iEmptyRxDfc ) );
    iPtrPtrToRxBuf = NULL;
    iRouterIf = NULL;
    // Only modified in constructor, if not created already reseted.
    delete iRequests;
    iRequests = NULL;
    C_TRACE( ( _T( "DISIKernelChannel::~DISIKernelChannel 0x%x 0x%x<" ), this ) );
}
Example #2
0
// -----------------------------------------------------------------------------
// DIscDevice::Flush
// Dfc to empty control channel and other send queues
// ( other items were commented in a header ).
// -----------------------------------------------------------------------------
//
void DIscDevice::Flush( TAny* aPtr )
    {
    C_TRACE( ( _T( "DIscDevice::Flush(0x%x)" ), aPtr ) );
    DIscDevice* device = ( DIscDevice* )aPtr;

    TDes8* frame = NULL;
    TIscSendFrameInfo* temp = NULL;

    TInt irqLevel(0);

    // If transmission is asynchronous and there can't be
    // several requests at the same time
    if ( !iIscDataTransmissionInterface->IsWritePending() )
        {
        irqLevel = DisableIrqs();
        if ( !iControlSendQueue->Empty() )
            {
            temp = iControlSendQueue->GetFirstFrameInfo();
            frame = ( TDes8* )iControlSendQueue->RemoveFirst();
            }
        else
            {
            temp = iSendQueue->GetFirstFrameInfo();
            frame = ( TDes8* )iSendQueue->RemoveFirst();
            }
        RestoreIrqs( irqLevel );
        C_TRACE( ( _T( "DIscDevice::Flush after RESTOREIRQS" ) ) );    
        if ( frame )    
            iIscDataTransmissionInterface->SendFrame( *frame, device->iSendDfc, temp->iFrameInfo );
        }
    C_TRACE( ( _T( "DIscDevice::Flush - return 0x0" ) ) );

    }
TInt DISIRouter::Send(
        TDes8& aMessage,
        const TUint8 aObjId
        )
    {
    C_TRACE( ( _T( "DISIRouter::Send 0x%x 0x%x>" ), &aMessage, aObjId ) );

    if( ( ( aObjId == EIscNokiaUsbPhonetLink ) && ( GET_SENDER_DEV( aMessage.Ptr() ) == PN_DEV_PC ) ) ||
        ( aObjId == PN_OBJ_ROUTING_REQ ) || 
        ( aObjId == PN_OBJ_EVENT_MULTICAST ) ||
        ( aObjId == ROUTER_OBJECT_IDENTIFIER ) ||
        ( aObjId == PIPEHANDLER_OBJECT_IDENTIFIER ) ) 
        {
        // No need to update sender dev and obj id
        C_TRACE( ( _T( "DISIRouter::Send message tx address not needed to updata 0x%x 0x%x>" ), aObjId, GET_SENDER_DEV( aMessage.Ptr() ) ) );
        }
    else
        {
        TUint8* messageBlockPtr( const_cast<TUint8*>( aMessage.Ptr() ) );        
        SET_SENDER_DEV( messageBlockPtr, PN_DEV_OWN );
        SET_SENDER_OBJ( messageBlockPtr, aObjId );
        }
    TInt error = iShCLTransceiver->RouteISIMessage( aMessage );
    C_TRACE( ( _T( "DISIRouter::Send 0x%x 0x%x<" ), &aMessage, aObjId ) );
    return error;
    }
void DISIKernelChannel::DoCancel(
    TInt aRequest,
    TInt aMask )
{
    C_TRACE( ( _T( "DISIKernelChannel::DoCancel 0x%x 0x%x>" ), this, iObjId ) );
    ASSERT_RESET_ALWAYS( EISILastAsyncRequest > ( aMask&aRequest ), EISIKernelChannelOverTheLimits | EDISIKernelChannelId << KClassIdentifierShift );
    if( iRequests->IsPending( aMask&aRequest ) )
    {
        switch( aMask&aRequest )
        {
        case EISIAsyncReceive:
        {
            C_TRACE( ( _T( "DISIKernelChannel::DoCancel EIADAsyncReceive 0x%x ptrs 0x%x 0x%x" ), this, iPtrPtrToRxBuf, &iPtrPtrToRxBuf ) );
            iPtrPtrToRxBuf = NULL;
            break;
        }
        default:
        {
            ASSERT_RESET_ALWAYS( 0, EISIKernelChannelWrongRequest | EDISIKernelChannelId << KClassIdentifierShift );
            break;
        }
        }
        EnqueChannelRequestCompleteDfc( aMask&aRequest, KErrCancel );
    }
    else
    {
        C_TRACE( ( _T( "DISIKernelChannel::DoCancel nothing to cancel 0x%x" ), this ) );
    }
    C_TRACE( ( _T( "DISIKernelChannel::DoCancel 0x%x 0x%x<" ), this, iObjId ) );
}
DISIRouter::~DISIRouter(
        // None
        )
    {
    C_TRACE( ( _T( "DISIRouter::~DISIRouter>" ) ) );
    iSelfPtr = NULL;
    for( TUint8 i( 0 ); i < iStaticObjIdTable.Count(); i++)
        {
        delete iStaticObjIdTable[ i ];
        iStaticObjIdTable[ i ] = NULL;
        }
    iStaticObjIdTable.Reset();
    for( TInt i( 0 ); i <= KMaxAmountOfObjId; i++)
        {
        if( iClientTable[ i ] )
            {
            delete iClientTable[ i ];
            iClientTable[ i ] = NULL;
            }
        }
    // All must exist otherways reseted.
    delete iInitializeDfc;
    iInitializeDfc = NULL;
    delete iShClientThreadContainer;
    iShClientThreadContainer = NULL;
    delete iShCLTransceiver;
    iShCLTransceiver = NULL;
    delete iClientTableFastMutex;
    iClientTableFastMutex = NULL;
    C_TRACE( ( _T( "DISIRouter::~DISIRouter<" ) ) );
    }
// KErrBadDescriptor, if message length too small
// KErrUnderFlow, if message length too big.
// KErrCouldNotConnect, if receiver object is out of scope.
TInt DISICLTransceiver::ValidateISIMessage(
        TDes8& aMessage
        )
    {

    C_TRACE( ( _T( "DISICLTransceiver::ValidateISIMessage 0x%x>" ), &aMessage ) );
    const TUint16 descLength( aMessage.Length() );
    TInt msgOk( KErrNone );
    msgOk = ( ISI_HEADER_OFFSET_MESSAGEID >= descLength ) ? KErrBadDescriptor : msgOk;
    TRACE_ASSERT_INFO( msgOk == KErrNone, msgOk );
    // Get ISI message length after known that the descriptor is big enough.
    const TUint8* msgPtr( aMessage.Ptr() );
    const TUint16 isiMsgLength( GET_LENGTH( msgPtr ) + PN_HEADER_SIZE );
    // If the descriptor length is less than ISI message length.
    msgOk = ( ( msgOk == KErrNone && isiMsgLength > descLength ) ? KErrUnderflow : msgOk );
    TRACE_ASSERT_INFO( msgOk == KErrNone, msgOk );
    // If the ISI message length is bigger that the largest supported.
    msgOk = ( ( msgOk == KErrNone && isiMsgLength > KMaxISIMsgSize ) ? KErrUnderflow : msgOk );
    TRACE_ASSERT_INFO( msgOk == KErrNone, msgOk );
    // If the ISI message length with PN_HEADER_SIZE is less or equal than ISI_HEADER_OFFSET_MESSAGEID.
    msgOk = ( ( msgOk == KErrNone && isiMsgLength <= ISI_HEADER_OFFSET_MESSAGEID ) ? KErrUnderflow : msgOk );
    TRACE_ASSERT_INFO( msgOk == KErrNone, msgOk );
    TRACE_ASSERT_INFO( msgOk == KErrNone, isiMsgLength );
    TRACE_ASSERT_INFO( msgOk == KErrNone, descLength );
    C_TRACE( ( _T( "DISICLTransceiver::ValidateISIMessage %d<" ), msgOk ) );
    return msgOk;
    }
Example #7
0
// -----------------------------------------------------------------------------
// DIscDevice::QueueFrame
// Queue frames that will be sent to Domestic OS
// ( other items were commented in a header ).
// -----------------------------------------------------------------------------
//
EXPORT_C TInt DIscDevice::QueueFrame( 
    const TUint16 aChannelId,
    const TDesC8* aFrame,
    const TAny* aChannelPtr,
    TAny* aFrameInfo )
    {
    C_TRACE( ( _T( "DIscDevice::QueueFrame(0x%x, 0x%x, 0x%x, 0x%x)" ), aChannelId, aFrame, aChannelPtr, aFrameInfo ) );
    
    TInt error = KErrNone;
    
    // control channel frame ( highest priority )
    if ( aChannelId == KIscControlChannel )
        {
        C_TRACE( ( _T( "DIscDevice::QueueFrame control frame queue" ) ) );
        //add to control frame queue
        error = iControlSendQueue->Add( ( TDes8* )aFrame, aChannelId, ( DIscChannel* )aChannelPtr, aFrameInfo );
        }
    else
        {        
        C_TRACE( ( _T( "DIscDevice::QueueFrame send queue" ) ) );
        // add to send queue
        error = iSendQueue->Add( ( TDes8* )aFrame, aChannelId, ( DIscChannel* )aChannelPtr, aFrameInfo );
        }
    
    C_TRACE( ( _T( "DIscDevice::QueueFrame - return 0x%x" ), error ) );
    return error;
    }
void DISICLTransceiver::UpdateDynamicDeviceTable( const TUint8 aDynamicDevId, const TUint8 aStaticDevId )
    {
    C_TRACE( ( _T( "DISICLTransceiver::UpdateDynamicDeviceTable 0x%x 0x%x>" ), aDynamicDevId, aStaticDevId) );
    TBool deviceExist(EFalse);
    TInt count(iDynamicDeviceTable.Count());
    TInt err( Kern::MutexWait( *iDynamicDeviceTableMutex ) );
    ASSERT_RESET_ALWAYS( ( err == KErrNone ), ( EISICLTransceiverMutexWaitFailed2 | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
    for( TUint8 i = 0; i < count; i++ )
        {
        C_TRACE( ( _T( "DISICLTransceiver::UpdateDynamicDeviceTable find dynamic device %d" ), i) );
        if( iDynamicDeviceTable[ i ]->iDynamicDevId == aDynamicDevId )
            {                    
            C_TRACE( ( _T( "DISICLTransceiver::UpdateDynamicDeviceTable dyn dev exist i = %d aDynamicDevId 0x%x staticDevId 0x%x previous 0x%x" ), i, iDynamicDeviceTable[ i ]->iDynamicDevId, aStaticDevId, iDynamicDeviceTable[ i ]->iStaticDevId ) );
            iDynamicDeviceTable[ i ]->iStaticDevId = aStaticDevId;
            deviceExist = ETrue;
            break;
            }
        }
        
    if ( !deviceExist )
        {
        C_TRACE( ( _T( "DISICLTransceiver::UpdateDynamicDeviceTable new dynamic device added 0x%x 0x%x" ), aDynamicDevId, aStaticDevId ) );
        iDynamicDeviceTable.Append( new TDynamicDevice( aDynamicDevId, aStaticDevId ) );
        }
    Kern::MutexSignal( *iDynamicDeviceTableMutex );
    C_TRACE( ( _T( "DISICLTransceiver::UpdateDynamicDeviceTable 0x%x 0x%x<" ), aDynamicDevId, aStaticDevId) );
    }
void DISICLTransceiver::GetDeviceConnectionStates(
        RArray<TUint8>& aDeviceIdentifierList,
        const TBool aConnectedDevice
        )
    {
    C_TRACE( ( _T( "DISICLTransceiver::GetDeviceConnectionStates 0x%x 0x%x>" ), &aDeviceIdentifierList, aConnectedDevice ) );

    const TInt count( iShDevices.Count() );
    for( TInt index( 0 ); index < count; index++ )
        {
        DISIDevice* tmpDevice = iShDevices[ index ];
        ASSERT_RESET_ALWAYS( tmpDevice, ( EISICLTransceiverNULLPtr3 | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
        tmpDevice->LockDeviceFM();
        if( tmpDevice->GetDeviceState() == aConnectedDevice )
            {
            const TUint8 deviceIdentifier( tmpDevice->GetDeviceIdentifier() );
            tmpDevice->FreeDeviceFM();
            aDeviceIdentifierList.Append( deviceIdentifier );
            }
        else
            {
            tmpDevice->FreeDeviceFM();
            }
        }
    C_TRACE( ( _T( "DISICLTransceiver::GetDeviceConnectionStates 0x%x 0x%x<" ), &aDeviceIdentifierList, aConnectedDevice ) );
    }
TBool DISIRouter::Receive( TDes8& aMessage, const TUint8 aObjId )
    {
    C_TRACE( ( _T( "DISIRouter::Receive 0x%x 0x%x>" ), &aMessage, aObjId ) );
    TBool error( EFalse );
    TUint8* messageBlockPtr( const_cast<TUint8*>( aMessage.Ptr() ) ); 

    NKern::FMWait( iClientTableFastMutex );
    if( iClientTable[ aObjId ] )
        {
        NKern::FMSignal( iClientTableFastMutex );
        ( iClientTable[ aObjId ]->iChannel )->Receive( aMessage ); //may not be safe, consider receive/delete sync
        C_TRACE( ( _T( "DISIRouter::Receive ok 0x%x 0x%x" ), &aMessage, aObjId ) );
        error = ETrue;
        }
    else
        {
        NKern::FMSignal( iClientTableFastMutex );
        C_TRACE( ( _T( "DISIRouter::Receive failed 0x%x 0x%x" ), &aMessage, aObjId ) );
        error = EFalse;
        }

    C_TRACE( ( _T( "DISIRouter::Receive 0x%x 0x%x %d<" ), &aMessage, aObjId, error ) );
    return error;

    }
TInt DISICLTransceiver::SendCommIsaEntityNotReachableResp(
        TDes8& aNotDeliveredMessage
        )
    {
    C_TRACE( ( _T( "DISICLTransceiver::SendCommIsaEntityNotReachableResp 0x%x>" ), &aNotDeliveredMessage ) );
    const TUint8* notDeliveredMsgPtr( aNotDeliveredMessage.Ptr() );
    TInt error = KErrAlreadyExists;
    // Avoid COMM_ISA_ENTITY_NOT_REACHABLE_RESP loop.
    if( ( notDeliveredMsgPtr[ ISI_HEADER_OFFSET_MESSAGEID ] == COMMON_MESSAGE ) &&
        ( ( notDeliveredMsgPtr[ ISI_HEADER_OFFSET_SUBMESSAGEID ] == COMM_ISA_ENTITY_NOT_REACHABLE_RESP ) || 
          ( notDeliveredMsgPtr[ ISI_HEADER_OFFSET_SUBMESSAGEID ] == COMM_SERVICE_NOT_IDENTIFIED_RESP ) ) )
        {
        C_TRACE( ( _T( "DISICLTransceiver Not sending another CommIsaEntityNotReachableResp 0x%x 0x%x" ), &aNotDeliveredMessage, notDeliveredMsgPtr[ ISI_HEADER_OFFSET_SUBMESSAGEID ] ) );
        }
    else
        {
        // Follows COMM specification: 000.031
        TUint8 length( ISI_HEADER_SIZE + SIZE_COMMON_MESSAGE_COMM_ISA_ENTITY_NOT_REACHABLE_RESP );
        TDes8& respMsg = MemApi::AllocBlock( length );
        ASSERT_RESET_ALWAYS( length > ISI_HEADER_OFFSET_MESSAGEID, ( EISICLTransceiverOverTheLimits | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
        TUint8* respMsgPtr = const_cast<TUint8*>( respMsg.Ptr() );
        // We start to append from transaction id.
        respMsg.SetLength( ISI_HEADER_OFFSET_TRANSID );
        // Get the header until messageid from prev. message.
        // Just turn receiver and sender device and object vice versa.
        respMsgPtr[ ISI_HEADER_OFFSET_MEDIA ] = notDeliveredMsgPtr[ ISI_HEADER_OFFSET_MEDIA ];
        SET_RECEIVER_DEV( respMsgPtr, GET_SENDER_DEV( aNotDeliveredMessage ) );
        SET_SENDER_DEV  ( respMsgPtr, GET_RECEIVER_DEV( aNotDeliveredMessage ) );
        respMsgPtr[ ISI_HEADER_OFFSET_RESOURCEID ] = notDeliveredMsgPtr[ ISI_HEADER_OFFSET_RESOURCEID ];
        SET_LENGTH( respMsgPtr, ( length - PN_HEADER_SIZE ) );
        SET_RECEIVER_OBJ( respMsgPtr, GET_SENDER_OBJ( aNotDeliveredMessage ) );
        SET_SENDER_OBJ( respMsgPtr, GET_RECEIVER_OBJ( aNotDeliveredMessage ) );
        // Set from undelivered message
        respMsg.Append( notDeliveredMsgPtr[ ISI_HEADER_OFFSET_TRANSID ] );
        // Message Identifier
        respMsg.Append( COMMON_MESSAGE );
        // Sub message Identifier.
        respMsg.Append( COMM_ISA_ENTITY_NOT_REACHABLE_RESP );
        // Not Delivered Message from original message.
        respMsg.Append( notDeliveredMsgPtr[ ISI_HEADER_OFFSET_MESSAGEID ] );
        // Status
        respMsg.Append( COMM_ISA_ENTITY_NOT_AVAILABLE );//  different status in a case of device not existing
        // Filler
        const TUint8 KFiller( 0x00 );
        respMsg.Append( KFiller );
        // Filler
        respMsg.Append( KFiller );
        // Filler
        respMsg.Append( KFiller );
        error = RouteISIMessage( respMsg, EFalse );
        // Programming error in this function if below assert is raised
        ASSERT_RESET_ALWAYS( KErrNone == error, ( EISICLTransceiverCommIsaEntityNotReachableResp | EDISICLTransceiverTraceId << KClassIdentifierShift ) );        
        }
    MemApi::DeallocBlock( aNotDeliveredMessage );
    C_TRACE( ( _T( "DISICLTransceiver::SendCommIsaEntityNotReachableResp 0x%x<" ), &aNotDeliveredMessage ) );
    return error;

    }
void DISIRouter::InitializeDfc(
        TAny* aPtr
        )
    {
    C_TRACE( ( _T( "DISIRouter::InitializeDfc>" ) ) );
    DISIRouter* self = reinterpret_cast<DISIRouter*>( aPtr );
    self->Initialize();
    C_TRACE( ( _T( "DISIRouter::InitializeDfc<" ) ) );
    }
void DISIKernelChannel::MsgQDfc(
    TAny* aPtr
)
{
    C_TRACE( ( _T( "DISIKernelChannel::MsgQDfc>" ) ) );
    DISIKernelChannel* tmp = reinterpret_cast<DISIKernelChannel*>( aPtr );
    tmp->HandleThreadMsg( static_cast<TThreadMessage&>(*tmp->iKernelChMsgQue.iMessage ) );
    C_TRACE( ( _T( "DISIKernelChannel::MsgQDfc<" ) ) );
}
// -----------------------------------------------------------------------------
// CUsbPnServer::CUsbPnServer
// C++ default constructor can NOT contain any code, that
// might leave.
// -----------------------------------------------------------------------------
//
CUsbPnServer::CUsbPnServer()
    :CServer2(CActive::EPriorityStandard)
    ,iSessionCount(0)
    {
    OstTrace0( TRACE_NORMAL, CUSBPNSERVER_CUSBPNSERVER_ENTRY, "CUsbPnServer::CUsbPnServer" );
    C_TRACE( ( _T( "CUsbPnServer::CUsbPnServer()" ) ) );

    OstTrace0( TRACE_NORMAL, CUSBPNSERVER_CUSBPNSERVER_EXIT, "CUsbPnServer::CUsbPnServer - return" );
    C_TRACE( ( _T( "CUsbPnServer::CUsbPnServer() - return" ) ) );
    }
DISIKernelChannel::DISIKernelAsyncRequests::DISIKernelAsyncRequests(
    const TInt aSize
)
{
    C_TRACE( ( _T( "DISIKernelAsyncRequests::DISIKernelAsyncRequests size %d>" ), aSize ) );
    iRequestLock = new NFastMutex();
    iDfcFunctionList = new TDfc*[ aSize ];
    iRequestStatusList = new TRequestStatus*[ aSize ];
    C_TRACE( ( _T( "DISIKernelAsyncRequests::DISIKernelAsyncRequests<" ) ) );
}
DISICLTransceiver::DISIDevice::~DISIDevice()
    {
    C_TRACE( ( _T( "DISICLTransceiver::DISIDevice::~DISIDevice>" ) ) );
    iShLink->Release();
    iShLink = NULL;
    // If not exists faulted already.
    delete iShDeviceMutex;
    iShDeviceMutex = NULL;
    C_TRACE( ( _T( "DISICLTransceiver::DISIDevice::~DISIDevice<" ) ) );
    }
void DISICLTransceiver::StateChangedDfc(
        TAny* aPtr
        )
    {
    C_TRACE( ( _T( "DISICLTransceiver::StateChangedDfc>" ) ) );
    DISICLTransceiver* self = reinterpret_cast<DISICLTransceiver*>( aPtr );
    ASSERT_RESET_ALWAYS( self, ( EISICLTransceiverNULLPtr5 | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
    self->ReadStateChanges();
    C_TRACE( ( _T( "DISICLTransceiver::StateChangedDfc<" ) ) );
    }
DISIKernelChannel::DISIKernelAsyncRequests::~DISIKernelAsyncRequests()
{
    C_TRACE( ( _T( "DISIKernelAsyncRequests::~DISIKernelAsyncRequests>" ) ) );
    // Delete space reserved for the array not the contents of the array, so mem behind pointers that are not owned are not deleted.
    delete iDfcFunctionList;
    delete iRequestStatusList;
    // Deletes lock
    delete iRequestLock;
    C_TRACE( ( _T( "DISIKernelAsyncRequests::~DISIKernelAsyncRequests<" ) ) );
}
// Called in 1...N transceivers thread context
void DISIKernelChannel::Receive(
    const TDesC8& aMessage
)
{
    C_TRACE( ( _T( "DISIKernelChannel::Receive 0x%x 0x%x 0x%x>" ), this, &aMessage, iObjId ) );
    ASSERT_THREAD_CONTEXT_ALWAYS( ( EISIKernelChannelNotThreadContext1 | EDISIKernelChannelId << KClassIdentifierShift ) );
    iRx->Add( aMessage );
    iEmptyRxDfc->Enque();
    C_TRACE( ( _T( "DISIKernelChannel::Receive 0x%x 0x%x 0x%x<" ), this, &aMessage, iObjId ) );
}
// Called only in router extension thread context.
void DISIKernelChannel::EnqueChannelRequestCompleteDfc(
    TInt aRequest,
    TInt aStatusToComplete
)
{
    C_TRACE( ( _T( "DISIKernelChannel::EnqueChannelRequestCompleteDfc 0x%x %d %d 0x%x 0x%x>" ), this, aRequest, aStatusToComplete, iObjId ) );
    ASSERT_THREAD_CONTEXT_ALWAYS( ( EISIKernelChannelNotThreadContext2 | EDISIKernelChannelId << KClassIdentifierShift ) );
    iRequests->Complete( aRequest, aStatusToComplete );
    C_TRACE( ( _T( "DISIKernelChannel::EnqueChannelRequestCompleteDfc 0x%x %d %d 0x%x 0x%x<" ), this, aRequest, aStatusToComplete, iObjId ) );
}
// -----------------------------------------------------------------------------
// CUsbPnServer::ConstructL
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CUsbPnServer::ConstructL()
    {
    OstTrace0( TRACE_NORMAL, CUSBPNSERVER_CONSTRUCTL_ENTRY, "CUsbPnServer::ConstructL" );
    C_TRACE( ( _T( "CUsbPnServer::ConstructL()" ) ) );

    StartL( KUsbPnServerName );
    iInterface = CUsbPnInterface::NewL();

    OstTrace0( TRACE_NORMAL, CUSBPNSERVER_CONSTRUCTL_EXIT, "CUsbPnServer::ConstructL - return void" );
    C_TRACE( ( _T( "CUsbPnServer::ConstructL() - return void" ) ) );
    }
// This is called in 1...N thread contextes
void DISICLTransceiver::ReceiveISIMessage(
        const TDesC8& aMessage,
        const TUint8 aTrxId
        )
    {
    C_TRACE( ( _T( "DISICLTransceiver::ReceiveMsg 0x%x 0x%x 0x%x>" ), this, &aMessage, aTrxId ) );
    // Can only be called from thread context.
    ASSERT_THREAD_CONTEXT_ALWAYS( ( EISICLTransceiverNotThreadContext | EDISIUserChannelTraceId << KClassIdentifierShift ) );
    
    TUint8 txDevId = GET_SENDER_DEV( aMessage.Ptr() );    
    if ( DynamicDevice( txDevId ) )
        {
        TUint8 staticDevId(0);
        const TInt count( iShDevices.Count() );        
        TInt index(0);
        TBool staticDeviceFound(EFalse);
        MISIRouterLinkIf* Link = NULL;
        while( !staticDeviceFound && ( index < count ) )
            {
            C_TRACE( ( _T( "DISICLTransceiver::ReceiveMsg find static device for trx 0x%x index %d" ), aTrxId, index ) );
            DISIDevice* tmpDevice = iShDevices[ index ];
            ASSERT_RESET_ALWAYS( tmpDevice, ( EISICLTransceiverNULLPtr4 | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
            tmpDevice->LockDeviceFM();
            Link = tmpDevice->GetLink();
            if( Link->GetTrxId() == aTrxId )
                {                
                staticDevId = tmpDevice->GetDeviceIdentifier();
                tmpDevice->FreeDeviceFM();
                C_TRACE( ( _T( "DISICLTransceiver::ReceiveMsg static device 0x%x trx 0x%x" ), this, &aMessage, aTrxId ) );
                staticDeviceFound = ETrue;
                }
            else
                {
                tmpDevice->FreeDeviceFM();
                }
            index++;
            } 
#ifdef USE_MEDIAAPI 		 
        if ( PN_DEV_PC == GET_SENDER_DEV( aMessage.Ptr() ))
            {
            C_TRACE( ( _T( "DISICLTransceiver::ReceiveMsg 0x%x 0x%x trx id = %d<" ), this, &aMessage, (Link->GetTrxId())  ) ); 
            iDevPcLastActiveTrxId = iDevPcLastSendTrxId;                  	
            iDevPcLastSendTrxId =	Link->GetTrxId();                       	    
            }		     
#endif /* USE_MEDIAAPI */  			
        UpdateDynamicDeviceTable( txDevId, staticDevId );
        }
    // else static device. No need to save trxId

    iRxQueue->Add( aMessage );
    iRxQueueDfc->Enque();
    C_TRACE( ( _T( "DISICLTransceiver::ReceiveMsg 0x%x 0x%x 0x%x<" ), this, &aMessage, aTrxId ) );
    }
// -----------------------------------------------------------------------------
// CUsbPnServer::SessionOpened
// ?implementation_description
// (other items were commented in a header).
// -----------------------------------------------------------------------------
//
void CUsbPnServer::SessionOpened()
    {
    OstTrace0( TRACE_NORMAL, CUSBPNSERVER_SESSIONOPENED_ENTRY, "CUsbPnServer::SessionOpened" );
    C_TRACE( ( _T( "CUsbPnServer::SessionOpened()" ) ) );

    iSessionCount++;
    OstTrace1( TRACE_NORMAL, CUSBPNSERVER_SESSIONOPENED, "CUsbPnServer::SessionOpened - session count = 0x%x", iSessionCount );
    C_TRACE( ( _T( "CUsbPnServer::SessionOpened() - session count = 0x%x" ), iSessionCount ) );

    OstTrace0( TRACE_NORMAL, CUSBPNSERVER_SESSIONOPENED_EXIT, "CUsbPnServer::SessionOpened - return void" );
    C_TRACE( ( _T( "CUsbPnServer::SessionOpened() - return void" ) ) );
    }
DECLARE_STANDARD_EXTENSION()
    {

    Kern::Printf( "ISA Access Extension" );
    C_TRACE( ( _T( "DIsaAccessExtension EntryPoint ->" ) ) );
    // Create a container extension
    DIsaAccessExtension* extension = new DIsaAccessExtension();
    TRACE_ASSERT( extension );
    C_TRACE( ( _T( "DIsaAccessExtension EntryPoint <-" ) ) );
    return extension ? KErrNone : KErrNoMemory;

    }
void DISIRouter::Initialize(
        // None
        )
    {
    C_TRACE( ( _T( "DISIRouter::Initialize> this: 0x%x" ), this ) );
    iSelfPtr = this;
    iClientTableFastMutex = new NFastMutex();
    ASSERT_RESET_ALWAYS( iClientTableFastMutex, ( EISIRouterNULLPtr2 | EDISIRouterTraceId << KClassIdentifierShift ) );
    for( TInt i( 0 ); i < KMaxAmountOfObjId; i++)
        {
        iClientTable[ i ] = NULL;
        }
    //  comment improve hex or des
    //  nameservice, communication manager, routerservice...
    //                                       UID  ObjId
    iStaticObjIdTable.Append( new TStaticId( 123, 0x70 ) ); //  for testing ISI API
    iStaticObjIdTable.Append( new TStaticId( 200, 0x71 ) ); //  for testing ISI API
    iStaticObjIdTable.Append( new TStaticId( 201, 0x72 ) ); //  for testing ISI API
    iStaticObjIdTable.Append( new TStaticId( 202, 0x73 ) ); //  for testing ISI API
    //
#ifndef WITHOUT_WRAPPERS_IN_USE
    // Map all the kernel channels as they used to be
    for( TInt id( KFirstUserChannel ); id < KLastKernelChannel; id++ )
        {
        if ( ( id != KNotInitializedId ) &&
             ( id != PN_OBJ_EVENT_MULTICAST ) &&
             ( id != PN_OBJ_EVENT_MULTICAST ) &&
             ( id != ROUTER_OBJECT_IDENTIFIER ) &&
             ( id != PIPEHANDLER_OBJECT_IDENTIFIER ) ) 
            {        
            iStaticObjIdTable.Append( new TStaticId( id, id ) );
            }
        else
            {
            iStaticObjIdTable.Append( new TStaticId( id, 0xEE ) );
            }
        }
    // Map all the kernel channels as they used to be
#endif // WITHOUT_WRAPPERS_IN_USE
    // ? vaikutus iShClientThreadContainer->DeallocateThread( iInitThread );

    iStaticObjIdTable.Append( new TStaticId( 0xD11BADA1, KNotInitializedId ) );// Make sure no one can get KNotInitializedId obj id
    iStaticObjIdTable.Append( new TStaticId( KCommunicationManagerUID, PN_OBJ_EVENT_MULTICAST ) );
    iStaticObjIdTable.Append( new TStaticId( KNameServiceUID, PN_OBJ_ROUTING_REQ ) );
    iStaticObjIdTable.Append( new TStaticId( KIsiShRouterServiceUID, ROUTER_OBJECT_IDENTIFIER ) );
    iStaticObjIdTable.Append( new TStaticId( KIsiShPipeHandlerUID, PIPEHANDLER_OBJECT_IDENTIFIER ) );
    
    iShCLTransceiver = new DISICLTransceiver( *this, iInitThread );
    ASSERT_RESET_ALWAYS( iShCLTransceiver, ( EISIRouterNULLPtr1 | EDISIRouterTraceId << KClassIdentifierShift ) ); 
    
    C_TRACE( ( _T( "DISIRouter::Initialize<" ) ) );
    }
DISICLTransceiver::DISICLTransceiver(
        DISIRouter& aRouter,
        TDfcQue* aThreadPtr
        )
    : iShRouter( aRouter )
    {
    C_TRACE( ( _T( "DISICLTransceiver::DISICLTransceiver>" ) ) );
    ASSERT_RESET_ALWAYS( aThreadPtr, ( EISICLTransceiverNULLPtr7 | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
    iShStateChangedDfc = new TDfc( StateChangedDfc, this, aThreadPtr, KDfcPriority );
    ASSERT_RESET_ALWAYS( iShStateChangedDfc, ( EISICLTransceiverMemAllocFailure6 | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
    
    iRxQueueDfc = new TDfc( RxQueueDfc, this, aThreadPtr, KDfcPriority );
    ASSERT_RESET_ALWAYS( iShStateChangedDfc, ( EISICLTransceiverMemAllocFailure8 | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
            
    DISIDevice* dev = new DISIDevice( PN_DEV_MODEM,
                                      MISIRouterLinkIf::CreateLinkF( this, PN_MEDIA_MODEM_HOST_IF, ETrxSharedMemory)
                                      );
    ASSERT_RESET_ALWAYS( dev, ( EISICLTransceiverMemAllocFailure3 | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
    iShDevices.Append( dev );
#ifdef USE_MEDIAAPI 
    DISIDevice* dev2 = new DISIDevice( PN_DEV_PC,  // append to dynamic table if needed
                                       MISIRouterLinkIf::CreateLinkF( this, PN_MEDIA_USB, ETrxUSB )
                                      );
    ASSERT_RESET_ALWAYS( dev2, ( EISICLTransceiverMemAllocFailure9 | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
    iShDevices.Append( dev2 );
    
    DISIDevice* dev4 = new DISIDevice( PN_DEV_PC,  // append to dynamic table if needed
                                       MISIRouterLinkIf::CreateLinkF( this, PN_MEDIA_BT, ETrxBT )
                                      );
    ASSERT_RESET_ALWAYS( dev4, ( EISICLTransceiverMemAllocFailure10 | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
    iShDevices.Append( dev4 );
 #endif /* USE_MEDIAAPI */
	  DISIDevice* dev3 = new DISIDevice( PN_DEV_DUMMYIST,
                                       MISIRouterLinkIf::CreateLinkF( this, PN_MEDIA_TEST, ETrxTest )
                                      );                                      
    ASSERT_RESET_ALWAYS( dev3, ( EISICLTransceiverMemAllocFailure5 | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
    iShDevices.Append( dev3 );

    iRouterService = new DIsiRouterService( *this );
    ASSERT_RESET_ALWAYS( iRouterService, ( EISICLTransceiverMemAllocFailure2 | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
    
    iRxQueue = new DISIMsgQueue( KISIMainRxQueueSize );
    ASSERT_RESET_ALWAYS( iRxQueue, ( EISICLTransceiverMemAllocFailure7 | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
    
    TInt err( Kern::MutexCreate( iDynamicDeviceTableMutex, KISICLTransceiverMutex, KMutexOrdGeneral0 ) );
    ASSERT_RESET_ALWAYS( ( KErrNone == err ), ( EISICLTransceiverMutexCreateFailed | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
#ifdef USE_MEDIAAPI 
    iDevPcLastSendTrxId = EAmountOfTrxs;  //put a maxvalue	
    iDevPcLastActiveTrxId = EAmountOfTrxs;  //put a maxvalue
#endif /* USE_MEDIAAPI */
    C_TRACE( ( _T( "DISICLTransceiver::DISICLTransceiver<" ) ) );
    }
TUint8 DISIRouter::ReserveStaticObjId( const TInt32 aUID )
    {
    C_TRACE( ( _T( "DISIRouter::ReserveStaticObjId 0x%x 0x%x>" ), aUID ) );
    for( TUint8 i = 0; i < iStaticObjIdTable.Count(); i++ )
        {
        if( iStaticObjIdTable[ i ]->iUID == aUID )
            {
            C_TRACE( ( _T( "DISIRouter::ReserveStaticObjId 0x%x 0x%x 0x%x<" ), aUID, iStaticObjIdTable[i]->iUID, iStaticObjIdTable[i]->iObjId ) );
            return iStaticObjIdTable[i]->iObjId;
            }
        }
    return KNotInitializedId;
    }
TBool DISIKernelChannel::DISIKernelAsyncRequests::IsPending(
    const TUint aRequest
)
{
    C_TRACE( ( _T( "DISIKernelAsyncRequests::IsPending %d>" ), aRequest ) );
    ASSERT_RESET_ALWAYS( ( EISILastAsyncRequest > aRequest && EISIAsyncReceive <= aRequest ), ( EISIKernelChannelOverTheLimits3 | EDISIKernelChannelId << KClassIdentifierShift ) );
    TBool ret( EFalse );
    NKern::FMWait( iRequestLock );
    ret = ( iDfcFunctionList[ aRequest ] && iRequestStatusList[ aRequest ] ) ? ETrue : EFalse;
    NKern::FMSignal( iRequestLock );
    C_TRACE( ( _T( "DISIKernelAsyncRequests::IsPending %d %d<" ), aRequest, ret ) );
    return ret;
}
void DISIRouter::Disconnect(
        const TUint8 aObjId
        )
    {
    C_TRACE( ( _T( "DISIRouter::Disconnect 0x%x>" ), aObjId ) );
    TISIClient* tmp = iClientTable[ aObjId ];
    NKern::FMWait( iClientTableFastMutex );
    iClientTable[ aObjId ] = NULL;
    NKern::FMSignal( iClientTableFastMutex );
    //Must exist
    delete tmp;
    tmp = NULL;
    C_TRACE( ( _T( "DISIRouter::Disconnect 0x%x<" ), aObjId ) );
    }
DISIRouter::DISIRouter(
        // None
        )
    {
    C_TRACE( ( _T( "DISIRouter::DISIRouter>" ) ) );
    iShClientThreadContainer = new DISIThreadContainer();
    ASSERT_RESET_ALWAYS( iShClientThreadContainer, ( EISIRouterMemAllocFailure1 | EDISIRouterTraceId << KClassIdentifierShift ) );
    iInitThread = iShClientThreadContainer->AllocateThread( MISIObjectRouterIf::EISIKernelMainThread );
    ASSERT_RESET_ALWAYS( iInitThread, ( EISIRouterNULLThreadPointer | EDISIRouterTraceId << KClassIdentifierShift ) );
    iInitializeDfc = new TDfc( InitializeDfc, this, iInitThread, KDfcPriority );
    ASSERT_RESET_ALWAYS( iInitializeDfc, ( EISIRouterMemAllocFailure2 | EDISIRouterTraceId << KClassIdentifierShift ) );
    iInitializeDfc->Enque();
    C_TRACE( ( _T( "DISIRouter::DISIRouter<" ) ) );
    }