tracesrv/tracecore/plugins/xti_if/common/src/XtiIfMessageUtils.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 <pn_const.h>
       
    17 #include <phonetisi.h>
       
    18 #include "TraceCoreMessageUtils.h"
       
    19 #include "TraceCoreSubscriber.h"
       
    20 #include "TraceCoreOstHeader.h"
       
    21 #include "OstTraceDefinitions.h"
       
    22 #ifdef OST_TRACE_COMPILER_IN_USE
       
    23 #include "XtiIfMessageUtilsTraces.h"
       
    24 #endif
       
    25 
       
    26 
       
    27 /*
       
    28  * ISI header remainder length, 2 + 2 filler bytes
       
    29  */
       
    30 const TInt KIsiHeaderRemainder = 4;
       
    31 
       
    32 /*
       
    33  * Shift one byte
       
    34  */
       
    35 const TInt KByteShift = 8;
       
    36 
       
    37 /*
       
    38  * Byte mask value
       
    39  */
       
    40 const TInt KByteMask = 0xFF;
       
    41 
       
    42 
       
    43 /**
       
    44  * Merges the header and data into a single buffer
       
    45  * 
       
    46  * @param aMsg Message to be sent.
       
    47  * @param aMsgBlock The message block where data is merged
       
    48  * @return Symbian error code
       
    49  */
       
    50 TInt TXtiIfMessageUtils::MergeHeaderAndData( const TTraceMessage& aMsg, TDes8& aTarget )
       
    51 	{
       
    52     TInt ret(KErrNotSupported);
       
    53     if ( aMsg.iMsgFormat == EMessageHeaderFormatProprietary )
       
    54         {
       
    55         // Merge header and data
       
    56         aTarget.Copy( *aMsg.iHeader );
       
    57         aTarget.Append( *aMsg.iData );
       
    58          
       
    59         // Flip receiver and sender devices
       
    60         TUint8 tmp = aTarget[ ISI_HEADER_OFFSET_RECEIVERDEVICE ];
       
    61         aTarget[ ISI_HEADER_OFFSET_RECEIVERDEVICE ] = aTarget[ ISI_HEADER_OFFSET_SENDERDEVICE ];
       
    62         aTarget[ ISI_HEADER_OFFSET_SENDERDEVICE ] = tmp;
       
    63          
       
    64         // Flip receiver and sender objects
       
    65         tmp = aTarget[ ISI_HEADER_OFFSET_RECEIVEROBJECT ];
       
    66         aTarget[ ISI_HEADER_OFFSET_RECEIVEROBJECT ] = aTarget[ ISI_HEADER_OFFSET_SENDEROBJECT ];
       
    67         aTarget[ ISI_HEADER_OFFSET_SENDEROBJECT ] = tmp;
       
    68         
       
    69         // Assign message length
       
    70         TInt length = aMsg.iHeader->Length() + aMsg.iData->Length() - PN_HEADER_SIZE;
       
    71         
       
    72 #ifndef __WINS__
       
    73         aTarget[ ISI_HEADER_OFFSET_LENGTH ] = length & 0xFF; // CodForChk_Dis_Magic
       
    74         aTarget[ ISI_HEADER_OFFSET_LENGTH + 1 ] = ( length >> 8 ) & 0xFF; // CodForChk_Dis_Magic
       
    75 #else
       
    76         aTarget[ ISI_HEADER_OFFSET_LENGTH + 1 ] = length & 0xFF; // CodForChk_Dis_Magic
       
    77         aTarget[ ISI_HEADER_OFFSET_LENGTH] = ( length >> 8 ) & 0xFF; // CodForChk_Dis_Magic
       
    78 #endif
       
    79                 
       
    80         // Assign new message ID
       
    81         aTarget[ ISI_HEADER_OFFSET_MESSAGEID ] = aMsg.iMessageId;
       
    82         ret = KErrNone;
       
    83         }
       
    84     else
       
    85         if (aMsg.iMsgFormat == EMessageHeaderFormatOst)
       
    86             {
       
    87             // Merge header and data
       
    88             aTarget.Copy( *aMsg.iHeader);
       
    89             aTarget.Append( *aMsg.iData);
       
    90 
       
    91             
       
    92             // Assign message length
       
    93             TUint16 length = aMsg.iHeader->Length() + aMsg.iData->Length()
       
    94                     - OstHeader::OstBaseProtocol::KOstBaseHeaderSize;
       
    95             
       
    96             aTarget[ OstHeader::OstBaseProtocol::KOstHeaderLengthOffset ] = (length >> KByteShift ) & KByteMask;
       
    97             aTarget[ OstHeader::OstBaseProtocol::KOstHeaderLengthOffset + 1 ] = length & KByteMask;
       
    98 
       
    99             // Assign new message ID
       
   100             aTarget[ OstHeader::OstBaseProtocol::KOstHeaderProtocolIdOffset ] = aMsg.iMessageId;
       
   101             ret = KErrNone;
       
   102             }
       
   103     return ret;
       
   104     }
       
   105 
       
   106 
       
   107 /**
       
   108  * Get the message length 
       
   109  * 
       
   110  * @param aMsg Message to be sent.
       
   111  * @return length or -1 if not valid
       
   112  */
       
   113 TInt TXtiIfMessageUtils::GetMessageLength( const TTraceMessage& aMsg )
       
   114     {
       
   115     OstTrace0( TRACE_FLOW, TRACECOREMESSAGEUTILS_GETMESSAGELENGTH_ENTRY, "> TraceCoreMessageUtils::GetMessageLength");
       
   116     TInt msgLength;
       
   117     if ( aMsg.iMsgFormat == EMessageHeaderFormatProprietary )
       
   118         {
       
   119         // Get message length
       
   120         msgLength = aMsg.iData->Length();
       
   121         if ( aMsg.iHeader != NULL )
       
   122             {
       
   123             TInt hdrLen( aMsg.iHeader->Length() );
       
   124             // Message header length must be valid
       
   125             if ( hdrLen == ( ISI_HEADER_SIZE + KIsiHeaderRemainder ) )
       
   126                 {
       
   127                 msgLength += hdrLen;
       
   128                 }
       
   129             else
       
   130                 {
       
   131                 OstTrace1( TRACE_IMPORTANT , TTRACECOREISAUTILS_GETMESSAGELENGTH_INVALID_HEADER_LENGTH,
       
   132                 		"TTraceCoreIsaUtils::GetMessageLength - ISI header length was not valid. Len:%d", hdrLen );
       
   133                 msgLength = -1;
       
   134                 }
       
   135             }
       
   136         else
       
   137             {
       
   138             // If message length is not enough for valid ISI message, an error is returned
       
   139             if ( msgLength < ( ISI_HEADER_SIZE + KIsiHeaderRemainder ) )
       
   140                 {
       
   141                 OstTrace1( TRACE_IMPORTANT , TTRACECOREISAUTILS_GETMESSAGELENGTH_MESSAGE_TOO_SHORT,
       
   142                 		"TTraceCoreIsaUtils::GetMessageLength - Message is too short. Len:%d", msgLength );
       
   143                 msgLength = -1;
       
   144                 }
       
   145             }
       
   146         }
       
   147     else
       
   148         if (aMsg.iMsgFormat == EMessageHeaderFormatOst)
       
   149             {
       
   150             // Get message length
       
   151             msgLength = aMsg.iData->Length();
       
   152             if (aMsg.iHeader != NULL)
       
   153                 {
       
   154                 TInt hdrLen(aMsg.iHeader->Length() );
       
   155 
       
   156                 // Message header length must be valid
       
   157                 if (hdrLen == ( OstHeader::OstBaseProtocol::KOstBaseHeaderSize ))
       
   158                     {
       
   159                     msgLength += hdrLen;
       
   160                     }
       
   161                 else
       
   162                     {
       
   163                     msgLength = -1;
       
   164                     }
       
   165                 }
       
   166             else
       
   167                 {
       
   168                 // If message length is not enough for valid ISI message, an error is returned
       
   169                 if (msgLength < ( ISI_HEADER_SIZE + KIsiHeaderRemainder ))
       
   170                     {
       
   171                     msgLength = -1;
       
   172                     }
       
   173                 }
       
   174             }
       
   175         else
       
   176         {
       
   177         OstTrace0( TRACE_IMPORTANT , TRACECOREMESSAGEUTILS_GETMESSAGELENGTH_NOT_SUPPORTED,
       
   178         		"TraceCoreMessageUtils::GetMessageLength - Format not supported");
       
   179         msgLength = -1;
       
   180         }
       
   181     OstTrace1( TRACE_FLOW, TRACECOREMESSAGEUTILS_GETMESSAGELENGTH_EXIT,
       
   182     		"< TraceCoreMessageUtils::GetMessageLength. Len:%d", msgLength );
       
   183     return msgLength;
       
   184     }