bluetooth/btsdp/database/sdputil.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2000-2009 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 <es_sock.h>
       
    17 #include <btsdp.h>
       
    18 #include "sdputil.h"
       
    19 
       
    20 void DbPanic(TSdpDbPanic aCode)
       
    21 	{
       
    22 	User::Panic(KSdpDatabasePanicString, aCode);
       
    23 	}
       
    24 
       
    25 void ParsePanic(TSdpParsePanic aCode)
       
    26 	{
       
    27 	User::Panic(KSdpParsePanicString, aCode);
       
    28 	}
       
    29 
       
    30 EXPORT_C TUint SdpUtil::GetUint(const TDesC8& aData)
       
    31 /** Gets an unsigned integer from a descriptor.
       
    32 
       
    33 @param aData The descriptor containing the integer
       
    34 @return The unsigned integer contained in the descriptor
       
    35 */
       
    36 	{
       
    37 	switch(aData.Length())
       
    38 		{
       
    39 	case 0:
       
    40 		return 0;
       
    41 	case 1:
       
    42 		return aData[0];
       
    43 	case 2:
       
    44 		return BigEndian::Get16(aData.Ptr());
       
    45 	case 4:
       
    46 		return BigEndian::Get32(aData.Ptr());
       
    47 	default:
       
    48 		ParsePanic(EGetUintBadDescriptorLength);
       
    49 		return 0;
       
    50 		}
       
    51 	}
       
    52 
       
    53 EXPORT_C void SdpUtil::PutUint(TUint8* aPtr, TInt64 aInt, TInt aNumberOfBytes)
       
    54 /** Puts an unsigned integer into a descriptor.
       
    55 
       
    56 @param aPtr Pointer to the descriptor's data field
       
    57 @param aInt Integer to be copied into descriptor
       
    58 @param aNumberOfBytes Number of bytes occupied by integer
       
    59 */
       
    60 	{
       
    61 	for(TInt i = aNumberOfBytes - 1; i >= 0; --i)
       
    62 		{
       
    63 		aPtr[i] = (static_cast<TUint8>(I64LOW(aInt)));
       
    64 		aInt >>= 8;
       
    65 		} 
       
    66 	}
       
    67 
       
    68 /*
       
    69 Puts a TUint64 into a descriptor
       
    70 
       
    71 @param aPtr Pointer to the descriptor's data field
       
    72 @param aNumber The unsigned 64 bit integer to be copied into the descriptor
       
    73 */
       
    74 EXPORT_C void SdpUtil::PutUint64(TUint8* aPtr, const TUint64& aNumber)
       
    75 	{
       
    76 	TUint8 temp;
       
    77 	TUint64 number = aNumber;
       
    78 	
       
    79 	for (TInt i = sizeof(TUint64) -1; i >= 0; i--)
       
    80 		{
       
    81 		temp = number & 0xFF; 	// transfer the lowest byte..
       
    82 		aPtr[i] = temp;			//.. into the right hand side of the descriptor
       
    83 		number >>= 8;
       
    84 		} 
       
    85 	}
       
    86 	
       
    87 
       
    88 /*
       
    89 Gets a TUint64 from a descriptor
       
    90 
       
    91 @param aData The descriptor containing the integer
       
    92 @param aNumber The unsigned 64 bit integer contained in the descriptor
       
    93 */
       
    94 EXPORT_C void SdpUtil::GetUint64(const TDesC8& aData, TUint64& aNumber)
       
    95 	{
       
    96 	if (aData.Length() != 8) //i.e. 64/8
       
    97 		{
       
    98 		ParsePanic(EGetUint64BadDescriptorLength);
       
    99 		return;
       
   100 		}
       
   101 		
       
   102 	const TUint8* dataPtr = aData.Ptr();
       
   103 	
       
   104 	aNumber = 	(static_cast<TUint64>(dataPtr[0])<<56) | (static_cast<TUint64>(dataPtr[1])<<48) | 
       
   105 				(static_cast<TUint64>(dataPtr[2])<<40) | (static_cast<TUint64>(dataPtr[3])<<32) |
       
   106 				(static_cast<TUint64>(dataPtr[4])<<24) | (static_cast<TUint64>(dataPtr[5])<<16) |
       
   107 				(static_cast<TUint64>(dataPtr[6])<<8) | (static_cast<TUint64>(dataPtr[7]) );
       
   108 				
       
   109 	}
       
   110 
       
   111 
       
   112 EXPORT_C void SdpUtil::PutUint128(TUint8* aPtr, const TUint64& aLo, const TUint64& aHi)
       
   113 /**
       
   114 Puts two unsigned 64 bit integers into a descriptor.
       
   115 
       
   116 @param aPtr Pointer to the descriptor's data field
       
   117 @param aLo The 64 lower bits of a 128 bit integer to be copied into the descriptor
       
   118 @param aHi  The 64 higher bits of a 128 bit integer to be copied into the descriptor
       
   119 */
       
   120 	{
       
   121 	TUint8 tempLow;
       
   122 	TUint8 tempHigh;	
       
   123 	TUint64 lowNumber = aLo;
       
   124 	TUint64 highNumber = aHi;
       
   125 	TInt offset = sizeof(TUint64);
       
   126 	
       
   127 	for(TInt i = offset -1; i >= 0; i--)
       
   128 		{
       
   129 		tempLow = lowNumber & 0xFF; // transfer the lowest byte..
       
   130 		aPtr[i+offset] = tempLow;	//.. into the right hand side of the descriptor
       
   131 		lowNumber >>= 8;
       
   132 		
       
   133 		tempHigh = highNumber & 0xFF; 
       
   134 		aPtr[i] = tempHigh;
       
   135 		highNumber >>= 8;
       
   136 		} 
       
   137 	
       
   138 	}
       
   139 
       
   140 	
       
   141 /*
       
   142 Gets two TUint64s, one being the high 64 bits and one being the low 64 bits of a 128 bit integer 
       
   143 
       
   144 @param aData The descriptor containing the integer
       
   145 @param aLo The unsigned 64 bit integer contains the lower bits of the 128 bit integer contained in the descriptor
       
   146 @param aHi  The unsigned 64 bit integer contains the higher bits of the 128 bit integer contained in the descriptor
       
   147 */
       
   148 EXPORT_C void SdpUtil::GetUint128(const TDesC8& aData, TUint64& aLo, TUint64& aHi)
       
   149 	{
       
   150 	if (aData.Length() != 16) //i.e. 128/8
       
   151 		{
       
   152 		ParsePanic(EGetUint128BadDescriptorLength);
       
   153 		}
       
   154 		
       
   155 	const TUint8* dataPtr = aData.Ptr();
       
   156 
       
   157 	// left hand side of the descriptor transfers to the high TUint64
       
   158 	aHi = 	(static_cast<TUint64>(dataPtr[0])<<56) | (static_cast<TUint64>(dataPtr[1])<<48) | 
       
   159 			(static_cast<TUint64>(dataPtr[2])<<40) | (static_cast<TUint64>(dataPtr[3])<<32) |
       
   160 			(static_cast<TUint64>(dataPtr[4])<<24) | (static_cast<TUint64>(dataPtr[5])<<16) |
       
   161 			(static_cast<TUint64>(dataPtr[6])<<8) | (static_cast<TUint64>(dataPtr[7]) );
       
   162 				
       
   163 	// right hand side of the descriptor transfers to the low TUint64
       
   164 	aLo = 	(static_cast<TUint64>(dataPtr[8])<<56) | (static_cast<TUint64>(dataPtr[9])<<48) | 
       
   165 			(static_cast<TUint64>(dataPtr[10])<<40) | (static_cast<TUint64>(dataPtr[11])<<32) |
       
   166 			(static_cast<TUint64>(dataPtr[12])<<24) | (static_cast<TUint64>(dataPtr[13])<<16) |
       
   167 			(static_cast<TUint64>(dataPtr[14])<<8) | (static_cast<TUint64>(dataPtr[15]) );
       
   168 
       
   169 	}