tracesrv/tracecore/btrace_handler/src/TraceCoreMessageUtils.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 // Utility functions for ISI messaging
       
    15 //
       
    16 
       
    17 /*
       
    18 
       
    19 NOTE these following values are lifted from s60 env
       
    20 for example pn_const.h is in epoc32\internal
       
    21 
       
    22 but this just includes another version accordinng to variant
       
    23 these are
       
    24 
       
    25 icpr82
       
    26 icpr91
       
    27 icpr92
       
    28 
       
    29 The same is true for  phonetisi.h
       
    30 
       
    31 The #defined values ARE THE SAME for all these variants - thats not to say they wont change in future
       
    32 */
       
    33 
       
    34 #define ISI_HEADER_OFFSET_MEDIA          0
       
    35 #define ISI_HEADER_OFFSET_RECEIVERDEVICE 1
       
    36 #define ISI_HEADER_OFFSET_SENDERDEVICE   2
       
    37 #define ISI_HEADER_OFFSET_RESOURCEID     3
       
    38 #define ISI_HEADER_OFFSET_LENGTH         4
       
    39 #define ISI_HEADER_OFFSET_RECEIVEROBJECT 6
       
    40 #define ISI_HEADER_OFFSET_SENDEROBJECT   7
       
    41 #define ISI_HEADER_OFFSET_TRANSID        8
       
    42 #define ISI_HEADER_OFFSET_MESSAGEID      9
       
    43 #define ISI_HEADER_OFFSET_SUBMESSAGEID  10
       
    44 
       
    45 #define ISI_HEADER_SIZE                  8
       
    46 
       
    47 // For extended resourc id handling
       
    48 #define ISI_HEADER_OFFSET_TYPE          10
       
    49 #define ISI_HEADER_OFFSET_SUBTYPE       11
       
    50 
       
    51 
       
    52 #define PN_HEADER_SIZE                  0x06 /* 6, Note data[0]/[1] excluded */
       
    53 
       
    54 #include "TraceCoreMessageUtils.h"
       
    55 #include "TraceCoreSubscriber.h"
       
    56 #include "TraceCoreOstHeader.h"
       
    57 #include "OstTraceDefinitions.h"
       
    58 #ifdef OST_TRACE_COMPILER_IN_USE
       
    59 #include "TraceCoreMessageUtilsTraces.h"
       
    60 #endif
       
    61 
       
    62 /*
       
    63  * ISI header remainder length, 2 + 2 filler bytes
       
    64  */
       
    65 #ifdef __ISI_SUPPORTED__
       
    66 const TInt KIsiHeaderRemainder = 4;
       
    67 #endif
       
    68 
       
    69 /*
       
    70  * Shift one byte
       
    71  */
       
    72 const TInt KByteShift = 8;
       
    73 
       
    74 /*
       
    75  * Byte mask value
       
    76  */
       
    77 const TInt KByteMask = 0xFF;
       
    78 
       
    79 
       
    80 /**
       
    81  * Merges the header and data into a single buffer
       
    82  *
       
    83  * @param aMsg Message to be sent.
       
    84  * @param aMsgBlock The message block where data is merged
       
    85  * @return Symbian error code
       
    86  */
       
    87 TInt TTraceCoreMessageUtils::MergeHeaderAndData( const TTraceMessage& aMsg, TDes8& aTarget )
       
    88 	{
       
    89     TInt ret(KErrNotSupported);
       
    90     if ( aMsg.iMsgFormat == EMessageHeaderFormatProprietary )
       
    91         {
       
    92 #ifdef __ISI_SUPPORTED__
       
    93         // Merge header and data
       
    94         aTarget.Copy( *aMsg.iHeader );
       
    95         aTarget.Append( *aMsg.iData );
       
    96 
       
    97         // Flip receiver and sender devices
       
    98         TUint8 tmp = aTarget[ ISI_HEADER_OFFSET_RECEIVERDEVICE ];
       
    99         aTarget[ ISI_HEADER_OFFSET_RECEIVERDEVICE ] = aTarget[ ISI_HEADER_OFFSET_SENDERDEVICE ];
       
   100         aTarget[ ISI_HEADER_OFFSET_SENDERDEVICE ] = tmp;
       
   101 
       
   102         // Flip receiver and sender objects
       
   103         tmp = aTarget[ ISI_HEADER_OFFSET_RECEIVEROBJECT ];
       
   104         aTarget[ ISI_HEADER_OFFSET_RECEIVEROBJECT ] = aTarget[ ISI_HEADER_OFFSET_SENDEROBJECT ];
       
   105         aTarget[ ISI_HEADER_OFFSET_SENDEROBJECT ] = tmp;
       
   106 
       
   107         // Assign message length
       
   108         TInt length = aMsg.iHeader->Length() + aMsg.iData->Length() - PN_HEADER_SIZE;
       
   109 
       
   110 #ifndef __WINS__
       
   111         aTarget[ ISI_HEADER_OFFSET_LENGTH ] = length & 0xFF; // CodForChk_Dis_Magic
       
   112         aTarget[ ISI_HEADER_OFFSET_LENGTH + 1 ] = ( length >> 8 ) & 0xFF; // CodForChk_Dis_Magic
       
   113 #else
       
   114         aTarget[ ISI_HEADER_OFFSET_LENGTH + 1 ] = length & 0xFF; // CodForChk_Dis_Magic
       
   115         aTarget[ ISI_HEADER_OFFSET_LENGTH] = ( length >> 8 ) & 0xFF; // CodForChk_Dis_Magic
       
   116 #endif
       
   117 
       
   118         // Assign new message ID
       
   119         aTarget[ ISI_HEADER_OFFSET_MESSAGEID ] = aMsg.iMessageId;
       
   120         ret = KErrNone;
       
   121 #endif  // __ISI_SUPPORTED__
       
   122         }
       
   123     else
       
   124         if (aMsg.iMsgFormat == EMessageHeaderFormatOst)
       
   125             {
       
   126             // Merge header and data
       
   127             aTarget.Copy( *aMsg.iHeader);
       
   128             aTarget.Append( *aMsg.iData);
       
   129 
       
   130 
       
   131             // Assign message length
       
   132             TUint16 length = aMsg.iHeader->Length() + aMsg.iData->Length()
       
   133                     - OstHeader::OstBaseProtocol::KOstBaseHeaderSize;
       
   134 
       
   135             aTarget[ OstHeader::OstBaseProtocol::KOstHeaderLengthOffset ] = (length >> KByteShift ) & KByteMask;
       
   136             aTarget[ OstHeader::OstBaseProtocol::KOstHeaderLengthOffset + 1 ] = length & KByteMask;
       
   137 
       
   138             // Assign new message ID
       
   139             aTarget[ OstHeader::OstBaseProtocol::KOstHeaderProtocolIdOffset ] = aMsg.iMessageId;
       
   140             ret = KErrNone;
       
   141             }
       
   142     return ret;
       
   143     }
       
   144 
       
   145 
       
   146 /**
       
   147  * Get the message length
       
   148  *
       
   149  * @param aMsg Message to be sent.
       
   150  * @return length or -1 if not valid
       
   151  */
       
   152 TInt TTraceCoreMessageUtils::GetMessageLength( const TTraceMessage& aMsg )
       
   153     {
       
   154     TInt msgLength = -1;
       
   155     if ( aMsg.iMsgFormat == EMessageHeaderFormatProprietary )
       
   156         {
       
   157 #ifdef __ISI_SUPPORTED__
       
   158         // Get message length
       
   159         msgLength = aMsg.iData->Length();
       
   160         if ( aMsg.iHeader != NULL )
       
   161             {
       
   162             TInt hdrLen( aMsg.iHeader->Length() );
       
   163             // Message header length must be valid
       
   164             if ( hdrLen == ( ISI_HEADER_SIZE + KIsiHeaderRemainder ) )
       
   165                 {
       
   166                 msgLength += hdrLen;
       
   167                 }
       
   168             else
       
   169                 {
       
   170                 OstTrace1( TRACE_IMPORTANT , TTRACECOREISAUTILS_GETMESSAGELENGTH_INVALID_HEADER_LENGTH, "TTraceCoreIsaUtils::GetMessageLength - ISI header length was not valid. Len:%d", hdrLen );
       
   171                 msgLength = -1;
       
   172                 }
       
   173             }
       
   174         else
       
   175             {
       
   176             // If message length is not enough for valid ISI message, an error is returned
       
   177             if ( msgLength < ( ISI_HEADER_SIZE + KIsiHeaderRemainder ) )
       
   178                 {
       
   179                 OstTrace1( TRACE_IMPORTANT , TTRACECOREISAUTILS_GETMESSAGELENGTH_MESSAGE_TOO_SHORT, "TTraceCoreIsaUtils::GetMessageLength - Message is too short. Len:%d", msgLength );
       
   180                 msgLength = -1;
       
   181                 }
       
   182             }
       
   183 #endif // __ISI_SUPPORTED__
       
   184         }
       
   185     else
       
   186         if (aMsg.iMsgFormat == EMessageHeaderFormatOst)
       
   187             {
       
   188             // Get message length
       
   189             msgLength = aMsg.iData->Length();
       
   190             if (aMsg.iHeader != NULL)
       
   191                 {
       
   192                 TInt hdrLen(aMsg.iHeader->Length() );
       
   193 
       
   194                 // Message header length must be valid
       
   195                 if (hdrLen == ( OstHeader::OstBaseProtocol::KOstBaseHeaderSize ))
       
   196                     {
       
   197                     msgLength += hdrLen;
       
   198                     }
       
   199                 else
       
   200                     {
       
   201                     msgLength = -1;
       
   202                     }
       
   203                 }
       
   204             else
       
   205                 {
       
   206 #ifdef __ISI_SUPPORTED__
       
   207                 // If message length is not enough for valid ISI message, an error is returned
       
   208                 if (msgLength < ( ISI_HEADER_SIZE + KIsiHeaderRemainder ))
       
   209                     {
       
   210                     msgLength = -1;
       
   211                     }
       
   212 #endif // __ISI_SUPPORTED__
       
   213                 }
       
   214 
       
   215             }
       
   216         else
       
   217 			{
       
   218 			OstTrace0( TRACE_IMPORTANT , TRACECOREMESSAGEUTILS_GETMESSAGELENGTH_NOT_SUPPORTED, "TraceCoreMessageUtils::GetMessageLength - Format not supported");
       
   219 			msgLength = -1;
       
   220 			}
       
   221     OstTrace1( TRACE_FLOW, TRACECOREMESSAGEUTILS_GETMESSAGELENGTH_EXIT, "< TraceCoreMessageUtils::GetMessageLength. Len:%d", msgLength );
       
   222     return msgLength;
       
   223     }