rtp/rtpstack/src/rtputil.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 /*
       
     2 * Copyright (c) 2002-2003 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 "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 
       
    21 // INCLUDE FILES
       
    22 #include <e32math.h>
       
    23 #include <e32hal.h>
       
    24 #include "rtputil.h"
       
    25 
       
    26 // ================= MEMBER FUNCTIONS =======================
       
    27 
       
    28 // ---------------------------------------------------------------------------
       
    29 // Parameters:
       
    30 //  x   16-LSB of a positive integer
       
    31 //  y   16-LSB of a positive integer  
       
    32 //
       
    33 // Function: The function is taking care of wrap. If the distance
       
    34 //           these two number is longer than 1 << 15, then we assume
       
    35 //           the larger one is the min16 of these two numbers.
       
    36 //           
       
    37 // Returns:
       
    38 //    the min of the two input values 
       
    39 // ---------------------------------------------------------------------------
       
    40 TUint16 TRtpUtil::Min16( TUint16 aX, TUint16 aY )
       
    41     {
       
    42     if ( ( aX <= aY && aX > aY - ( 1 << 15 ) ) || ( aX >= aY + ( 1 << 15 ) ) )
       
    43         {
       
    44         return aX;
       
    45         }
       
    46     else
       
    47        {
       
    48         return aY;
       
    49        }
       
    50     }
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // Parameters:
       
    54 //  x   16-bit unsigned int 
       
    55 //  y   16-bit unsigned int   
       
    56 //
       
    57 // Function: if the distance between aX and aY is longer than 1 << 15,
       
    58 //           we assume these two number are wrapped.
       
    59 // Returns:
       
    60 //    1 if there is wrap around between this two values 
       
    61 // ---------------------------------------------------------------------------
       
    62 TUint16 TRtpUtil::Wrap16( TUint16 aX, TUint16 aY )
       
    63     {
       
    64     if ( aX < aY - ( 1 << 15 ) || aY < aX - ( 1 << 15 ) )
       
    65         {
       
    66         return 1;
       
    67         }
       
    68     else
       
    69         {
       
    70         return 0;
       
    71         }
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // Parameters:
       
    76 //  x   32-LSB of a positive integer
       
    77 //  y   32-LSB of a positive integer 
       
    78 // ---------------------------------------------------------------------------
       
    79 TUint32 TRtpUtil::Sub32( TUint32 aX, TUint32 aY )
       
    80     {
       
    81     if ( aX < aY )
       
    82         return ( 0xFFFFFFFF - ( aY - aX ) + 1 );
       
    83     else
       
    84         return ( aX - aY );
       
    85     }
       
    86 
       
    87 // ---------------------------------------------------------------------------
       
    88 // TUint8* TRtpUtil::Strcpy()
       
    89 // 
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 TUint8* TRtpUtil::Strcpy( TUint8* aTarget, const TUint8* aSource )
       
    93     {
       
    94     TPtr8 ptrTarget( aTarget, ( User::StringLength( aSource ) ) + 1 );
       
    95 
       
    96     ptrTarget.SetMax();
       
    97     ptrTarget.Copy( _L8( aSource ) );
       
    98     ptrTarget.ZeroTerminate();
       
    99 
       
   100     return aTarget;
       
   101     }
       
   102 
       
   103 // ---------------------------------------------------------------------------
       
   104 // TUint TRtpUtil::GtGetTime()
       
   105 // Return time elapse from 01/01/1970 in tenth of millsecond
       
   106 // ---------------------------------------------------------------------------
       
   107 //
       
   108 TUint TRtpUtil::GtGetTime()
       
   109     {
       
   110     TTimeIntervalMicroSeconds32 uspertick;     // scale factor: number of microseconds per system tick
       
   111     UserHal::TickPeriod( uspertick );
       
   112     return static_cast<TUint>( uspertick.Int() // number of '1us periods' per system tick 
       
   113             * User::TickCount()                // number of system ticks
       
   114             / 100 );                           // in tenths of ms, note still wraps after 5 days.
       
   115     }
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 // TUint32 TRtpUtil::Random()
       
   119 // 
       
   120 // ---------------------------------------------------------------------------
       
   121 //
       
   122 TUint32 TRtpUtil::Random( TInt64& aSeed )
       
   123     {
       
   124     TInt32 rand = Math::Rand( aSeed );
       
   125     return Abs( rand );
       
   126     }
       
   127 
       
   128 // ---------------------------------------------------------------------------
       
   129 // TReal TRtpUtil::FloatRandom()
       
   130 // 
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 TReal TRtpUtil::FloatRandom( TInt64& aSeed )
       
   134     {
       
   135     TReal rand = Math::FRand( aSeed );
       
   136     return Abs( rand );
       
   137     }
       
   138 
       
   139 
       
   140 #ifdef _DEBUG 
       
   141 // ---------------------------------------------------------------------------
       
   142 // TInt TRtpUtil::Print()
       
   143 // 
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 void TRtpUtil::Print(  const TDesC8& aP )
       
   147     {
       
   148     const TUint KPrintLength = 5;
       
   149     const TUint KPrintExtLength = 20;
       
   150     TChar notAllowed('%');
       
   151 	if ( aP.Locate( notAllowed ) == KErrNotFound )
       
   152 		{
       
   153 		HBufC8* msg = NULL;
       
   154 	    TRAPD( msgError, msg = HBufC8::NewL( aP.Length() + KPrintExtLength ) );
       
   155 	    if ( msgError != KErrNone )
       
   156 	        return ;
       
   157 
       
   158 	    TPtr8 b( msg->Des() );
       
   159 
       
   160 	    b.Append( _L8( "RTP : " ) );
       
   161 	    //    b = p;
       
   162 	    //apend current time
       
   163 	    b.AppendFormat( _L8( "%u - " ), ( TUint * ) ( TRtpUtil::GtGetTime() ) );
       
   164 
       
   165 	    b.Append( aP );
       
   166 	    b.Append( _L8( "\n" ) );
       
   167 
       
   168 	    TInt ptrSize = static_cast <TInt>( b.Size() );
       
   169 	    TBuf<256> buf;
       
   170 	    if ( ptrSize < buf.MaxSize() )
       
   171 	    	{
       
   172 	   	    buf.Copy( b );
       
   173 		    RDebug::Print( buf );
       
   174 	    	}
       
   175 	    else
       
   176 	    	{
       
   177 	    	_LIT(KRTPWarning, "RTP: Text is too long to be print all");
       
   178 	    	RDebug::Print( KRTPWarning );	
       
   179 	    	buf.Copy( b.Left( buf.MaxSize() - KPrintLength ));
       
   180 	    	}
       
   181 	    delete msg;
       
   182 		}
       
   183 	else
       
   184 		{
       
   185 		_LIT(KRTPWarning, "RTP: Text contains not allowed characters, log ignored");
       
   186 		RDebug::Print( KRTPWarning );							
       
   187 		}	
       
   188 
       
   189     }
       
   190 
       
   191 // ---------------------------------------------------------------------------
       
   192 // TInt TRtpUtil::Print()
       
   193 // 
       
   194 // ---------------------------------------------------------------------------
       
   195 //
       
   196 void TRtpUtil::Print(  const TDesC8& aP, TInt aValue )
       
   197     {
       
   198     const TUint KPrintLength = 5;
       
   199     const TUint KPrintExtLength = 106;
       
   200     TChar notAllowed('%');
       
   201 	if ( aP.Locate( notAllowed ) == KErrNotFound )
       
   202 		{
       
   203 		HBufC8* msg = NULL;
       
   204 	    TRAPD( msgError, msg = HBufC8::NewL( aP.Length() + KPrintExtLength ) );
       
   205 	    if ( msgError != KErrNone )
       
   206 	        return;
       
   207 
       
   208 	    TPtr8 b( msg->Des() );
       
   209 	    b.Append( _L8( "RTP : " ) );
       
   210 
       
   211 	    //    b = p;
       
   212 	    //apend current time
       
   213 	    b.AppendFormat( _L8( "%u - " ), ( TUint * ) ( TRtpUtil::GtGetTime() ) );
       
   214 	    b.Append( aP );
       
   215 	    b.AppendFormat( _L8( "= <%d> " ), aValue );
       
   216 	    b.Append( _L8( "\n" ) );
       
   217 
       
   218 		TInt ptrSize = static_cast <TInt>( b.Size() );
       
   219 	    TBuf<256> buf;
       
   220 	    if ( ptrSize < buf.MaxSize() )
       
   221 	    	{
       
   222 	   	    buf.Copy( b );
       
   223 		    RDebug::Print( buf );
       
   224 	    	}
       
   225 	    else
       
   226 	    	{
       
   227 	    	_LIT(KRTPWarning, "RTP: Text is too long to be print all");
       
   228 	    	RDebug::Print( KRTPWarning );	
       
   229 	    	buf.Copy( b.Left( buf.MaxSize() - KPrintLength ));
       
   230 	    	}
       
   231 	    delete msg;
       
   232 		}
       
   233 	else
       
   234 		{
       
   235 		_LIT(KRTPWarning, "RTP: Text contains not allowed characters, log ignored");
       
   236 		RDebug::Print( KRTPWarning );							
       
   237 		}	
       
   238     }
       
   239    
       
   240     
       
   241 #endif
       
   242 
       
   243 
       
   244 #ifdef _RTP_LOG_FILE 
       
   245 
       
   246 // ---------------------------------------------------------------------------
       
   247 // TInt TRtpUtil::Print()
       
   248 // 
       
   249 // ---------------------------------------------------------------------------
       
   250 //
       
   251 void TRtpUtil::Print( const TDesC& aName, const TDesC8& aP )
       
   252     {
       
   253     HBufC8* msg = NULL;
       
   254     TRAPD( msgError, msg = HBufC8::NewL( aP.Length() + 20 ) );
       
   255     if ( msgError != KErrNone )
       
   256         return;
       
   257 
       
   258     TPtr8 b( msg->Des() );
       
   259 
       
   260     b.Append( _L8( "RTP : " ) );
       
   261     //    b = p;
       
   262     //apend current time
       
   263     b.AppendFormat( _L8( "%u - " ), ( TUint * ) ( TRtpUtil::GtGetTime() ) );
       
   264 
       
   265     b.Append( aP );
       
   266     b.Append( _L8( "\n" ) );
       
   267 
       
   268     TFileUtil::LogMessage( aName, b );
       
   269 
       
   270     delete msg;
       
   271     }
       
   272 
       
   273 // ---------------------------------------------------------------------------
       
   274 // TInt TRtpUtil::Print()
       
   275 // 
       
   276 // ---------------------------------------------------------------------------
       
   277 //
       
   278 void TRtpUtil::Print( const TDesC& aName, const TDesC8& aP, TUint32 aValue )
       
   279     {
       
   280     HBufC8* msg = NULL;
       
   281     TRAPD( msgError, msg = HBufC8::NewL( aP.Length() + 106 ) );
       
   282     if ( msgError != KErrNone )
       
   283         return;
       
   284 
       
   285     TPtr8 b( msg->Des() );
       
   286     b.Append( _L8( "RTP : " ) );
       
   287 
       
   288     //    b = p;
       
   289     //apend current time
       
   290     b.AppendFormat( _L8( "%u - " ), ( TUint * ) ( TRtpUtil::GtGetTime() ) );
       
   291     b.Append( aP );
       
   292     b.AppendFormat( _L8( "<%d> " ), aValue );
       
   293     b.Append( _L8( "\n" ) );
       
   294 
       
   295     TFileUtil::LogMessage( aName, b );
       
   296 
       
   297 
       
   298     delete msg;
       
   299 	}
       
   300 
       
   301 // ---------------------------------------------------------------------------
       
   302 // TInt TFileUtil::LogMessage()
       
   303 // 
       
   304 // ---------------------------------------------------------------------------
       
   305 //
       
   306 TInt TFileUtil::LogMessage( const TFileName& name, const TDesC8& mess )
       
   307     {
       
   308     RFs fs;
       
   309     RFile file;
       
   310     TFileName nameWithPath;
       
   311 
       
   312     nameWithPath = KDefaultPath;
       
   313     nameWithPath.Append( name );
       
   314     nameWithPath.ZeroTerminate();
       
   315     TInt err = fs.Connect();
       
   316 
       
   317     if ( err != KErrNone )
       
   318         return err;
       
   319 
       
   320     err = file.Open( fs, nameWithPath, EFileStream | EFileWrite | EFileShareExclusive );
       
   321     if ( err == KErrNotFound )
       
   322         err = file.Create( fs, nameWithPath, EFileStream | EFileWrite | EFileShareExclusive );
       
   323     if ( err != KErrNone )
       
   324         return err;
       
   325 
       
   326     TInt off( 0 );
       
   327     err = file.Seek( ESeekEnd, off );
       
   328     if ( err != KErrNone )
       
   329         return err;
       
   330 
       
   331     file.Write( mess );
       
   332     file.Flush();
       
   333     file.Close();
       
   334     fs.Close();
       
   335 
       
   336     return KErrNone;
       
   337     }
       
   338 
       
   339 // ---------------------------------------------------------------------------
       
   340 // TInt TFileUtil::InitLogFile()
       
   341 // 
       
   342 // ---------------------------------------------------------------------------
       
   343 //
       
   344 TInt TFileUtil::InitLogFile( const TFileName& name )
       
   345     {
       
   346     RFs fs;
       
   347     RFile file;
       
   348     TFileName nameWithPath;
       
   349 
       
   350     nameWithPath = KDefaultPath;
       
   351     nameWithPath.Append( name );
       
   352     nameWithPath.ZeroTerminate();
       
   353     TInt err = fs.Connect();
       
   354 
       
   355     if ( err != KErrNone )
       
   356         return err;
       
   357 
       
   358     err = file.Replace( fs, nameWithPath, EFileStream | EFileWrite | EFileShareExclusive );
       
   359     if ( err != KErrNone )
       
   360         return err;
       
   361 
       
   362     file.Close();
       
   363     fs.Close();
       
   364 
       
   365     return KErrNone;
       
   366     }
       
   367 #endif
       
   368 
       
   369 
       
   370 
       
   371 //  End of File