tracesrv/tracecore/plugins/xti_if/common/src/TraceCoreXtiIf.cpp
changeset 56 aa2539c91954
equal deleted inserted replaced
54:a151135b0cf9 56:aa2539c91954
       
     1 // Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 
       
    15 
       
    16 //- Include Files  ----------------------------------------------------------
       
    17 #include <pn_const.h>
       
    18 #include <phonetisi.h>
       
    19 #include <xtirx.h>
       
    20 #include <xtitx.h>
       
    21 
       
    22 #include "TraceCore.h"
       
    23 #include "TraceCoreXtiIf.h" 
       
    24 #include "TraceCoreDebug.h"
       
    25 #include "TraceCoreMediaIfCallback.h"
       
    26 #include "TraceCoreSubscriber.h"
       
    27 #include "TraceCoreMessageUtils.h"
       
    28 
       
    29 #include "OstTraceDefinitions.h"
       
    30 #ifdef OST_TRACE_COMPILER_IN_USE
       
    31 #include "TraceCoreXtiIfTraces.h"
       
    32 #endif
       
    33 
       
    34 #include "xtipluginvariantconstants.h"
       
    35 
       
    36 /**
       
    37  * 200 millisecond pulse time (0.2 sec)
       
    38  */
       
    39 const TUint KXtiRegisterTimer = 200000;
       
    40 
       
    41 /** 
       
    42  * Default DFC queue priority
       
    43  */
       
    44 const TUint8 KDefaultDfcPriority = 3;
       
    45 
       
    46 /*
       
    47  * ISI header remainder length, 2 + 2 filler bytes
       
    48  */
       
    49 const TInt KIsiHeaderRemainder = 4;
       
    50 
       
    51 
       
    52 /**
       
    53  * Prints a message
       
    54  *
       
    55  * @param aMsg Reference to a message. 
       
    56  */
       
    57 static void DebugPrintMsg( const TDesC8& aMsg )            
       
    58     {
       
    59     // Phonet message structure print
       
    60     OstTraceData( TRACE_DUMP, TRACECOREXTIIF_PS_DEBUGPRINTMSG,
       
    61     		"DTraceCoreXtiIf::DebugPrintMsg - Msg via XTI IF 0x%{hex8[]}", aMsg.Ptr(), aMsg.Size() );
       
    62     }
       
    63 
       
    64 /**
       
    65  * Constructor
       
    66  */
       
    67 DTraceCoreXtiIf::DTraceCoreXtiIf()
       
    68 : iReceiveDfc( DTraceCoreXtiIf::ReceiveDfc, this, DTraceCore::GetActivationQ(), KDefaultDfcPriority )
       
    69 , iXtiRegisterDfc( DTraceCoreXtiIf::XtiRegister, this, DTraceCore::GetActivationQ(), KDefaultDfcPriority )
       
    70 , iStatus( 0 )
       
    71     {
       
    72     // No implementation in constructor
       
    73     }
       
    74 
       
    75 /**
       
    76  * Destructor
       
    77  */
       
    78 DTraceCoreXtiIf::~DTraceCoreXtiIf()
       
    79     {
       
    80     //iCallback = NULL; 
       
    81     }
       
    82 
       
    83 /**
       
    84  * Initializes the XTI IF
       
    85  *
       
    86  * @param aCallBack Callback to TraceCore
       
    87  */
       
    88 TInt DTraceCoreXtiIf::Init( /*MTraceCoreMediaIfCallback& aCallback*/ )
       
    89     {
       
    90     TC_TRACE( ETraceLevelFlow, Kern::Printf( "> DTraceCoreXtiIf::Init" ) );
       
    91     
       
    92     // Register media plugin to TraceCore
       
    93     TInt ret = Register();
       
    94     
       
    95     StartTimer();
       
    96     TC_TRACE( ETraceLevelFlow, Kern::Printf( "< DTraceCoreXtiIf::Init" ) );
       
    97     return KErrNone;
       
    98     }
       
    99 
       
   100 /**
       
   101  * Starts the timer which calls the initialization function
       
   102  */
       
   103 void DTraceCoreXtiIf::StartTimer()
       
   104     {
       
   105     TC_TRACE( ETraceLevelFlow, Kern::Printf( "> DTraceCoreXtiIf::StartTimer" ) );
       
   106     iXtiRegisterTimer.OneShot( KXtiRegisterTimer, XtiRegisterDfc, this );
       
   107     TC_TRACE( ETraceLevelFlow, Kern::Printf( "< DTraceCoreXtiIf::StartTimer" ) );
       
   108     }
       
   109 
       
   110 /**
       
   111  * Add registering to own Dfc
       
   112  * 
       
   113  * @param aPtr Pointer to traceCoreIsaIf class
       
   114  */
       
   115 void DTraceCoreXtiIf::XtiRegisterDfc( TAny* aPtr )
       
   116     {
       
   117     TC_TRACE( ETraceLevelFlow, Kern::Printf( "> DTraceCoreXtiIf::XtiRegisterDfc" ) );
       
   118     reinterpret_cast< DTraceCoreXtiIf* >( aPtr )->iXtiRegisterDfc.Enque();
       
   119     TC_TRACE( ETraceLevelFlow, Kern::Printf( "< DTraceCoreXtiIf::XtiRegisterDfc" ) );
       
   120     }
       
   121 
       
   122 /**
       
   123  * Timer callback function to initialize the XTI IF
       
   124  * 
       
   125  * @param aPtr Pointer to DTraceCoreXtiIf class
       
   126  */
       
   127 void DTraceCoreXtiIf::XtiRegister( TAny* aPtr )
       
   128     {
       
   129     TC_TRACE( ETraceLevelFlow, Kern::Printf( "> DTraceCoreXtiIf::XtiRegister" ) );
       
   130     DTraceCoreXtiIf* traceCoreXtiIf = static_cast< DTraceCoreXtiIf* >( aPtr );
       
   131     __ASSERT_DEBUG( traceCoreXtiIf != NULL, 
       
   132             Kern::Fault( "DTraceCoreXtiIf::XtiRegister - NULL", KErrGeneral ) );
       
   133     traceCoreXtiIf->XtiRegister();
       
   134     TC_TRACE( ETraceLevelFlow, Kern::Printf( "< DTraceCoreXtiIf::XtiRegister" ) );
       
   135     }
       
   136 
       
   137 /**
       
   138  * Called from the static callback to register to XtiRx
       
   139  */
       
   140 void DTraceCoreXtiIf::XtiRegister()
       
   141     {
       
   142     TInt ret = XtiRx::Register( &iReceiveDfc, iReceiveBuffer );
       
   143     if ( ret != KErrNone )
       
   144         {
       
   145         TC_TRACE( ETraceLevelError, Kern::Printf( "DTraceCoreXtiIf::XtiRegister - Register failed: %d", ret ) );
       
   146         }
       
   147     }
       
   148 
       
   149 /**
       
   150  * Handle incoming messages from XTI
       
   151  * 
       
   152  * @param aPtr Pointer to DTraceCoreXtiIf class.
       
   153  */
       
   154 void DTraceCoreXtiIf::ReceiveDfc( TAny* aPtr )
       
   155     {
       
   156     OstTrace0( TRACE_FLOW, DTRACECOREXTIIF_PS_RECEIVEDFC_ENTRY, "> DTraceCoreXtiIf::ReceiveDfc");
       
   157     DTraceCoreXtiIf* traceCoreXtiIf = static_cast< DTraceCoreXtiIf* >( aPtr );
       
   158     __ASSERT_DEBUG( traceCoreXtiIf != NULL, 
       
   159             Kern::Fault( "DTraceCoreXtiIf::ReceiveDfc - NULL", KErrGeneral ) );
       
   160     traceCoreXtiIf->ReceiveDfc();
       
   161     OstTrace0( TRACE_FLOW, DTRACECOREXTIIF_PS_RECEIVEDFC_EXIT, "< DTraceCoreXtiIf::ReceiveDfc");
       
   162     }
       
   163 
       
   164 /**
       
   165  * Processes incoming XTI messages
       
   166  */
       
   167 void DTraceCoreXtiIf::ReceiveDfc()
       
   168     {
       
   169     // Determine the message we received
       
   170     if ( iReceiveBuffer.Length() > ISI_HEADER_OFFSET_MESSAGEID )
       
   171         {
       
   172         TUint8 resource = iReceiveBuffer[ ISI_HEADER_OFFSET_RESOURCEID ];
       
   173         
       
   174         // Check that resource is PN_EPOC_MON
       
   175         if ( resource == PN_EPOC_MON )
       
   176             {
       
   177             DebugPrintMsg ( iReceiveBuffer );
       
   178             
       
   179             NotifyCallback();
       
   180             }
       
   181         }
       
   182     XtiRx::SetReceiveBufferRead();    
       
   183     }
       
   184 
       
   185 /**
       
   186  * Notifies the callback with the incoming message
       
   187  * 
       
   188  * @return one of symbian error codes
       
   189  */
       
   190 void DTraceCoreXtiIf::NotifyCallback()
       
   191     {
       
   192     OstTrace0( TRACE_FLOW, DTRACECOREXTIIF_PS_NOTIFYCALLBACK_ENTRY, "> DTraceCoreXtiIf::NotifyCallback");
       
   193     // Header length is header size + remainders
       
   194     TInt headerLength = ISI_HEADER_SIZE + KIsiHeaderRemainder;
       
   195     if ( iReceiveBuffer.Length() < headerLength ) {
       
   196         headerLength = iReceiveBuffer.Length();
       
   197     }
       
   198     
       
   199     // Create TraceMessage
       
   200     TTraceMessage traceMsg;
       
   201     TPtrC header( iReceiveBuffer.Ptr(), headerLength );
       
   202     TPtrC data( NULL, 0 );
       
   203     if ( iReceiveBuffer.Length() > headerLength ) {
       
   204         data.Set( iReceiveBuffer.Ptr() + headerLength, iReceiveBuffer.Length() - headerLength );
       
   205     }
       
   206 
       
   207     traceMsg.iMsgFormat = EMessageHeaderFormatProprietary;
       
   208     traceMsg.iHeader = &header;
       
   209     traceMsg.iData = &data;
       
   210     traceMsg.iMessageId = iReceiveBuffer[ ISI_HEADER_OFFSET_MESSAGEID ];
       
   211     
       
   212     // Call base class MessageReceived
       
   213     MessageReceived( traceMsg );
       
   214     
       
   215     OstTrace0( TRACE_FLOW, DTRACECOREXTIIF_PS_NOTIFYCALLBACK_EXIT, "< DTraceCoreXtiIf::NotifyCallback");
       
   216     }
       
   217 
       
   218 /**
       
   219  * Sends messages to XTI
       
   220  * 
       
   221  * @param aMsg Message to be sent.
       
   222  * @return KErrNone if send successful
       
   223  */
       
   224 TInt DTraceCoreXtiIf::Send( TTraceMessage& aMsg )
       
   225     {
       
   226     OstTrace0( TRACE_FLOW, DTRACECOREXTIIF_PS_SEND_ENTRY, "> DTraceCoreXtiIf::Send");
       
   227     TInt ret;
       
   228     if ( aMsg.iData != NULL )
       
   229         {
       
   230         ret = TXtiIfMessageUtils::GetMessageLength( aMsg );
       
   231         if ( ret > 0 )
       
   232             {
       
   233             // First send the phone formatted start character
       
   234             ret = XtiTx::StartWrite( KChannel0, KPhoneFormattedMessage );
       
   235             if ( ret == KErrNone )
       
   236                 {
       
   237                 // Send trace data and end trace
       
   238                 SendData( aMsg );
       
   239                 XtiTx::StopWrite( KChannel0 );
       
   240                 if ( aMsg.iHeader != NULL ) 
       
   241                     {
       
   242                     DebugPrintMsg( iSendBuffer );
       
   243                     }
       
   244                 else
       
   245                     {
       
   246                     DebugPrintMsg( *aMsg.iData );
       
   247                     }
       
   248                 }
       
   249             else
       
   250                 {
       
   251                 OstTrace1( TRACE_IMPORTANT , DTRACECOREXTIIF_PS_SEND_START_WRITE_FAILED,
       
   252                 		"DTraceCoreXtiIf::Send - XTI StartWrite failed. Ret:%d", ret );
       
   253                 }
       
   254             }
       
   255         else
       
   256             {
       
   257             ret = KErrArgument;
       
   258             }
       
   259         }
       
   260     else
       
   261         {
       
   262         OstTrace0( TRACE_IMPORTANT , DTRACECOREXTIIF_PS_SEND_INVALID_DATA, "DTraceCoreXtiIf::Send - NULL data in message");
       
   263         ret = KErrArgument;
       
   264         }
       
   265     OstTrace1( TRACE_FLOW, DTRACECOREXTIIF_PS_SEND_EXIT, "< DTraceCoreXtiIf::Send. Ret:%d", ret );
       
   266     return ret;
       
   267     }
       
   268     
       
   269 /**
       
   270  * Called from the TraceCore to send message out.
       
   271  */
       
   272 TInt DTraceCoreXtiIf::SendMessage( TTraceMessage &aMsg )
       
   273     {
       
   274     TInt ret = Send( aMsg );
       
   275     return ret;
       
   276     }
       
   277 
       
   278 /**
       
   279  * Sends message to XTI
       
   280  * 
       
   281  * @param aMsg Message to be sent.
       
   282  */
       
   283 void DTraceCoreXtiIf::SendData( TTraceMessage& aMsg )
       
   284     {
       
   285     const TText8* pS = NULL;
       
   286     const TText8* pE = NULL;
       
   287     if ( aMsg.iHeader != NULL )
       
   288         {
       
   289         // Utility function is used to merge the data
       
   290         TInt ret = TXtiIfMessageUtils::MergeHeaderAndData( aMsg, iSendBuffer );
       
   291         if ( ret == KErrNone )
       
   292             {
       
   293             pS = iSendBuffer.Ptr();
       
   294             pE = pS + iSendBuffer.Length();
       
   295             }
       
   296         }
       
   297     else
       
   298         {
       
   299         // Header doesn't exists, all data is in data part
       
   300         pS = aMsg.iData->Ptr();
       
   301         pE = pS + aMsg.iData->Length();
       
   302         }
       
   303     
       
   304     // Print using 8bit write
       
   305     while ( pS < pE )
       
   306         {
       
   307         XtiTx::WriteData8( KChannel0, *( pS++ ) );
       
   308         }
       
   309     }
       
   310 
       
   311 /**
       
   312  * The entry point for a standard extension. Creates TraceCoreXtiIfPlugin extension.
       
   313  *
       
   314  *  @return KErrNone, if successful
       
   315  */
       
   316 DECLARE_STANDARD_EXTENSION()    //lint !e960 !e1717 ¤/#((& Symbian
       
   317     {
       
   318     TC_TRACE( ETraceLevelNormal, Kern::Printf( "DECLARE_STANDARD_EXTENSION Start Trace Core XTI IF Plugin" ) );
       
   319     
       
   320     TInt ret = KErrNone;
       
   321     
       
   322        // Certificate check should be done here e.g. if ( XtiTx::IsTraceEnabled() )
       
   323        // CpuPage::IsRdcAvailable();
       
   324         {
       
   325         DTraceCoreXtiIf* xtiIfPlugin = new DTraceCoreXtiIf();
       
   326         
       
   327         if ( xtiIfPlugin != NULL )
       
   328             {
       
   329             ret = xtiIfPlugin->Init();
       
   330             if ( ret != KErrNone )
       
   331                 {
       
   332                 delete xtiIfPlugin;
       
   333                 }
       
   334             }
       
   335         else
       
   336             {
       
   337             ret = KErrNoMemory;
       
   338             }
       
   339         }
       
   340 
       
   341 	if (ret == KErrNone) 
       
   342 		{
       
   343         POSTCODE( Kern::Printf("[POST][TraceCoreXtiIfPlugin][Start][OK]"); )
       
   344 		}
       
   345 	else
       
   346 		{
       
   347         POSTCODE( Kern::Printf("[POST][TraceCoreXtiIfPlugin][FAIL][%d]", ret ); )
       
   348 		}           
       
   349         
       
   350     return ret;
       
   351     }
       
   352 
       
   353 // End of File