connectivitylayer/isce/isirouter_dll/src/isicltransceiver.cpp
changeset 0 63b37f68c1ce
child 5 8ccc39f9d787
equal deleted inserted replaced
-1:000000000000 0:63b37f68c1ce
       
     1 /*
       
     2 * Copyright (c) 2009 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 the License "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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <kernel.h>               // For Kern
       
    21 #include "isirouter.h"            // For DISIRouter
       
    22 #include "isiroutertrace.h"       // For C_TRACE, ASSERT_RESET.. and fault codes
       
    23 #include "misirouterlinkif.h"     // For MISIRouterLinkIf
       
    24 #include "isihelpers.h"           // For GET_SENDER_DEV...
       
    25 #include "memapi.h"               // For MemApi
       
    26 #include <trxdefs.h>              // For ETrxTest...
       
    27 #include "isicltransceiver.h"     // For DISICLTransceiver
       
    28 
       
    29 
       
    30 #define PN_MEDIA_TEST 0xBB //not real
       
    31 #define PN_DEV_MODEM 0x12
       
    32 #define PN_DEV_DUMMYIST 0x13
       
    33 #define PN_DEV_APE 0x14
       
    34 
       
    35 // Faults
       
    36 enum TISICLTransceiverFaults
       
    37     {
       
    38     EISICLTransceiverMemAllocFailure = 0x01,
       
    39     EISICLTransceiverMemAllocFailure1,
       
    40     EISICLTransceiverNotSupportedMedia,
       
    41     EISICLTransceiverOverTheLimits,
       
    42     EISICLTransceiverNULLPtr,
       
    43     EISICLTransceiverNotSupportedDevice,
       
    44     };
       
    45 
       
    46 DISICLTransceiver::DISICLTransceiver( DISIRouter* aPtr )
       
    47     : iRouter( aPtr )
       
    48     {
       
    49     C_TRACE( ( _T( "DISICLTransceiver::DISICLTransceiver>" ) ) );
       
    50     iLinksArray = new MISIRouterLinkIf*[ EISIAmountOfMedias ];
       
    51     ASSERT_RESET_ALWAYS( iLinksArray, ( EISICLTransceiverMemAllocFailure | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
       
    52     // Initialize links
       
    53     for( TInt i( 0 ); i < EISIAmountOfMedias; i++ )
       
    54         {
       
    55         iLinksArray[ i ] = NULL;
       
    56         }
       
    57     //TODO add real media and trx when known
       
    58     iLinksArray[ EISIMediaTest ] = MISIRouterLinkIf::CreateLinkF( this, PN_MEDIA_TEST, ETrxTest );
       
    59     C_TRACE( ( _T( "DISICLTransceiver::DISICLTransceiver<" ) ) );
       
    60     }
       
    61 
       
    62 DISICLTransceiver::~DISICLTransceiver()
       
    63     {
       
    64     C_TRACE( ( _T( "DISICLTransceiver::~DISICLTransceiver>" ) ) );
       
    65     for( TUint8 i( 0 ); i < EISIAmountOfMedias; i++ )
       
    66         {
       
    67         MISIRouterLinkIf* tmp = iLinksArray[ i ];
       
    68         if( tmp )
       
    69             {
       
    70             tmp->Release();
       
    71             tmp = NULL;
       
    72             iLinksArray[ i ] = NULL;
       
    73             }
       
    74         }
       
    75     delete []iLinksArray;
       
    76     C_TRACE( ( _T( "DISICLTransceiver::~DISICLTransceiver<" ) ) );
       
    77     }
       
    78     
       
    79 void DISICLTransceiver::SendCommIsaEntityNotReachableResp( TDes8& aMessage )
       
    80     {
       
    81     C_TRACE( ( _T( "DISICLTransceiver::SendCommIsaEntityNotReachableResp 0x%x>" ), &aMessage ) );
       
    82     const TUint8* msgTmpPtr( aMessage.Ptr() );
       
    83     // To avoid COMM_ISA_ENTITY_NOT_REACHABLE_RESP loop
       
    84     if( msgTmpPtr[ ISI_HEADER_OFFSET_MESSAGEID ] != COMMON_MESSAGE &&
       
    85         msgTmpPtr[ ISI_HEADER_OFFSET_SUBMESSAGEID ] != COMM_ISA_ENTITY_NOT_REACHABLE_RESP )
       
    86         {
       
    87         // Make channel opening request followinfg COMM specification: 000.026
       
    88         // Length is sixteen bytes.
       
    89 		    TUint8 length( 16 );
       
    90 		    TDes8& tempPtr = MemApi::AllocBlock( ISI_HEADER_SIZE + SIZE_COMMON_MESSAGE_COMM_ISA_ENTITY_NOT_REACHABLE_RESP );
       
    91 		    ASSERT_RESET_ALWAYS( &tempPtr, ( EISICLTransceiverMemAllocFailure1 | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
       
    92   	    ASSERT_RESET_ALWAYS( ISI_HEADER_SIZE + SIZE_COMMON_MESSAGE_COMM_ISA_ENTITY_NOT_REACHABLE_RESP > ISI_HEADER_OFFSET_MESSAGEID, ( EISICLTransceiverOverTheLimits | EDISICLTransceiverTraceId << KClassIdentifierShift ) );    
       
    93   	    TUint8* ptr = const_cast<TUint8*>( tempPtr.Ptr() );
       
    94   	    // We start to append from transaction id.
       
    95   	    tempPtr.SetLength( ISI_HEADER_OFFSET_TRANSID );
       
    96   	    // Get the header until messageid from prev. message.
       
    97 		    // Just turn receiver and sender device and object vice versa.
       
    98 		    ptr[ ISI_HEADER_OFFSET_MEDIA ] = msgTmpPtr[ ISI_HEADER_OFFSET_MEDIA ];
       
    99 		    SET_RECEIVER_DEV( ptr, GET_SENDER_DEV( aMessage ) );
       
   100 		    SET_SENDER_DEV  ( ptr, GET_RECEIVER_DEV( aMessage ) );
       
   101 		    ptr[ ISI_HEADER_OFFSET_RESOURCEID ] = msgTmpPtr[ ISI_HEADER_OFFSET_RESOURCEID ];
       
   102 		    SET_LENGTH( ptr, length - PN_HEADER_SIZE );
       
   103 		    SET_RECEIVER_OBJ( ptr, GET_SENDER_OBJ( aMessage ) );
       
   104 		    SET_SENDER_OBJ( ptr, GET_RECEIVER_OBJ( aMessage ) );
       
   105 		    // Transactionid. Set to 0x01 since this is the first.
       
   106 		    tempPtr.Append( msgTmpPtr[ ISI_HEADER_OFFSET_TRANSID ] );
       
   107 		    // Message ID
       
   108 		    tempPtr.Append( ISI_HEADER_OFFSET_MESSAGEID );
       
   109 		    // Sub message ID.
       
   110 		    tempPtr.Append( COMM_ISA_ENTITY_NOT_REACHABLE_RESP );
       
   111 		    // Not Delivered Message from original req.
       
   112 		    tempPtr.Append( msgTmpPtr[ ISI_HEADER_OFFSET_MESSAGEID ] );
       
   113 		    // Status - COMM_ISA_ENTITY_NOT_AVAILABLE
       
   114 		    tempPtr.Append( 0x00 );
       
   115 		    // Filler
       
   116 		    tempPtr.Append( 0x00 );
       
   117 		    // Filler
       
   118 		    tempPtr.Append( 0x00 );
       
   119 		    // Filler
       
   120         tempPtr.Append( 0x00 );
       
   121 		    RouteISIMessage( tempPtr );
       
   122 		    }
       
   123 		else
       
   124 		    {
       
   125 		    C_TRACE( ( _T( "DISICLTransceiver Not sending another CommIsaEntityNotReachableResp" ) ) );
       
   126 		    }
       
   127     C_TRACE( ( _T( "DISICLTransceiver::SendCommIsaEntityNotReachableResp 0x%x<" ), &aMessage ) );
       
   128     }
       
   129 
       
   130 void DISICLTransceiver::RouteISIMessage( TDes8& aMessage )
       
   131     {
       
   132 	  C_TRACE( ( _T( "DISICLTransceiver::RouteISIMessage 0x%x>" ), &aMessage ) );
       
   133 	  TInt error( ValidateISIMessage( aMessage ) ); //TODO what to do with error?
       
   134 	 	TBool sendOk( EFalse );
       
   135 	 	TUint8* messageBlockPtr( const_cast<TUint8*>( aMessage.Ptr() ) );
       
   136 	  switch( messageBlockPtr[ ISI_HEADER_OFFSET_RECEIVERDEVICE ] )
       
   137         {
       
   138         case PN_DEV_APE:
       
   139             {
       
   140             C_TRACE( ( _T( "DISIRouter msg to PN_DEV_APE" ) ) );
       
   141             if( messageBlockPtr[ ISI_HEADER_OFFSET_RECEIVEROBJECT ] == PN_OBJ_ROUTING_REQ )
       
   142                 {
       
   143                 //route with resource and nameservice
       
   144                 }
       
   145             else{
       
   146             	  sendOk = iRouter->Receive( aMessage, messageBlockPtr[ ISI_HEADER_OFFSET_RECEIVEROBJECT ] );
       
   147                 C_TRACE( ( _T( "DISIRouter router sendOk %d" ), sendOk ) );
       
   148                 }
       
   149             break;
       
   150             }
       
   151         default:
       
   152             {
       
   153             C_TRACE( ( _T( "DISIRouter msg to other device" ) ) );
       
   154             TUint8 linkId = MapDeviceToMedia( messageBlockPtr[ ISI_HEADER_OFFSET_RECEIVERDEVICE ] );
       
   155 	          ASSERT_RESET_ALWAYS( linkId < EISIAmountOfMedias, ( EISICLTransceiverNotSupportedMedia | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
       
   156             MISIRouterLinkIf* link = iLinksArray[ linkId ];
       
   157             ASSERT_RESET_ALWAYS( link, ( EISICLTransceiverNULLPtr | EDISICLTransceiverTraceId << KClassIdentifierShift ) );  
       
   158             sendOk = link->Send( aMessage );
       
   159             C_TRACE( ( _T( "DISIRouter link sendOk %d" ), sendOk ) );
       
   160             break;
       
   161             }
       
   162         }
       
   163     if( !sendOk )
       
   164         {
       
   165         SendCommIsaEntityNotReachableResp( aMessage );
       
   166         MemApi::DeallocBlock( aMessage );
       
   167         }   
       
   168 	  C_TRACE( ( _T( "DISICLTransceiver::RouteISIMessage 0x%x<" ), &aMessage ) );
       
   169     }
       
   170 
       
   171 // KErrBadDescriptor, if message length too small
       
   172 // KErrUnderFlow, if message length too big.
       
   173 // KErrCouldNotConnect, if receiver object is out of scope.
       
   174 TInt DISICLTransceiver::ValidateISIMessage(
       
   175         TDes8& aMessage
       
   176         )
       
   177     {
       
   178     C_TRACE( ( _T( "DISICLTransceiver::ValidateISIMessage 0x%x>" ), &aMessage ) );
       
   179     const TUint16 descLength( aMessage.Length() );
       
   180     TInt msgOk( KErrNone );
       
   181     msgOk = ( ISI_HEADER_OFFSET_MESSAGEID >= descLength ) ? KErrBadDescriptor : msgOk;
       
   182     TRACE_ASSERT_INFO( msgOk == KErrNone, msgOk );
       
   183     // Get ISI message length after known that the descriptor is big enough.
       
   184     const TUint8* msgPtr( aMessage.Ptr() );
       
   185     const TUint16 isiMsgLength( GET_LENGTH( msgPtr ) + PN_HEADER_SIZE );
       
   186     // If the descriptor length is less than ISI message length.
       
   187     msgOk = ( ( msgOk == KErrNone && isiMsgLength > descLength ) ? KErrUnderflow : msgOk );
       
   188     TRACE_ASSERT_INFO( msgOk == KErrNone, msgOk );
       
   189     // If the ISI message length is bigger that the largest supported.
       
   190     msgOk = ( ( msgOk == KErrNone && isiMsgLength > KMaxISIMsgSize ) ? KErrUnderflow : msgOk );
       
   191     TRACE_ASSERT_INFO( msgOk == KErrNone, msgOk );
       
   192     // If the ISI message length with PN_HEADER_SIZE is less or equal than ISI_HEADER_OFFSET_MESSAGEID. 
       
   193     msgOk = ( ( msgOk == KErrNone && isiMsgLength <= ISI_HEADER_OFFSET_MESSAGEID ) ? KErrUnderflow : msgOk );
       
   194     TRACE_ASSERT_INFO( msgOk == KErrNone, msgOk );
       
   195     TRACE_ASSERT_INFO( msgOk == KErrNone, msgOk );
       
   196     TRACE_ASSERT_INFO( msgOk == KErrNone, isiMsgLength );
       
   197     TRACE_ASSERT_INFO( msgOk == KErrNone, descLength );
       
   198     TRACE_ASSERT_INFO( msgOk == KErrNone, msgOk );
       
   199     C_TRACE( ( _T( "DISICLTransceiver::ValidateISIMessage %d<" ), msgOk ) );
       
   200     return msgOk;
       
   201     }
       
   202 
       
   203 TUint8 DISICLTransceiver::MapDeviceToMedia(
       
   204         const TUint8 aDevice
       
   205         )
       
   206     {
       
   207     C_TRACE( ( _T( "DISICLTransceiver::MapDeviceToMedia aDevice %d>" ), aDevice ) );
       
   208     TUint8 media( KErrNone );
       
   209     switch( aDevice )
       
   210         {
       
   211         case PN_DEV_MODEM:
       
   212             {
       
   213             media = EISIMediaSharedMemory;
       
   214             break;
       
   215             }
       
   216         case PN_DEV_DUMMYIST:
       
   217             {
       
   218             media = EISIMediaTest;
       
   219             break;
       
   220             }
       
   221         // Not supported device
       
   222         default:
       
   223             {
       
   224            	ASSERT_RESET_ALWAYS( 0, ( EISICLTransceiverNotSupportedDevice | EDISICLTransceiverTraceId << KClassIdentifierShift ) );
       
   225             break;
       
   226             }
       
   227         }
       
   228     C_TRACE( ( _T( "DISICLTransceiver::MapDeviceToMedia media %d<" ), media ) );
       
   229     return media;
       
   230     }
       
   231