irda/irdastack/irtranp/GLOBAL.CPP
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 1997-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 "debug.h"
       
    17 #include "GLOBAL.H"
       
    18 
       
    19 void IrTranpUtil::DAppend(TDes8* aBuffer, TUint16 aValue)
       
    20 	{
       
    21 	aBuffer->Append((aValue >> 8) & 0xff);
       
    22 	aBuffer->Append(aValue & 0xff);
       
    23 	}
       
    24 
       
    25 /*
       
    26 * Method description:	Appends a 32 bit value to a buffer
       
    27 *
       
    28 * Parameter:			aBuffer - the buffer to append the value to
       
    29 * Parameter:			aValue - the value to append
       
    30 */
       
    31 
       
    32 void IrTranpUtil::LAppend(TDes8* aBuffer, TUint32 aValue)
       
    33 	{
       
    34 	aBuffer->Append((aValue >> 24) & 0xff);
       
    35 	aBuffer->Append((aValue >> 16) & 0xff);
       
    36 	aBuffer->Append((aValue >> 8) & 0xff);
       
    37 	aBuffer->Append(aValue & 0xff);
       
    38 	}
       
    39 
       
    40 /*
       
    41 * Method description:	Extracts a 16 bit value from a buffer
       
    42 *
       
    43 * Parameter:			aBuffer - the buffer to extract the value from
       
    44 * Parameter:			aOffset - the offset into the buffer where the value starts
       
    45 * Returns:				the 16 bit value
       
    46 */
       
    47 
       
    48 TUint16 IrTranpUtil::DExtract(const TDesC8& aBuffer, TInt aOffset)
       
    49 	{
       
    50 	TUint16 integer = (TUint16) ((aBuffer[aOffset] << 8) + (aBuffer[aOffset+1]));
       
    51 	return integer;
       
    52 	}
       
    53 
       
    54 /*
       
    55 * Method description:	Extracts a 32 bit value from a buffer
       
    56 *
       
    57 * Parameter:			aBuffer - the buffer to extract the value from
       
    58 * Parameter:			aOffset - the offset into the buffer where the value starts
       
    59 * Returns:				the 32 bit value
       
    60 */
       
    61 
       
    62 TUint32 IrTranpUtil::LExtract(const TDesC8& aBuffer, TInt aOffset)
       
    63 	{
       
    64 	TUint32 integer = ((aBuffer[aOffset] << 24) + (aBuffer[aOffset+1] << 16) + (aBuffer[aOffset+2] << 8) + (aBuffer[aOffset+3]));
       
    65 	return integer;
       
    66 	}
       
    67 
       
    68 /*
       
    69 * Method description:	binary to ASCII converter - prints to the debugging component
       
    70 *
       
    71 * Parameter:			aBuffer - the buffer containg data we want in hexadecimal ASCII representation
       
    72 */
       
    73 
       
    74 void IrTranpUtil::HexOut(const TDesC8& aBuffer)
       
    75 	{
       
    76 	const TUint KCharBufLen = aBuffer.Length()*2 + 1; //twice length of aBuffer + 1 for null terminator
       
    77 	TText* charbuffer = new TText[KCharBufLen];
       
    78 	if(!charbuffer)
       
    79 		{
       
    80 		_LIT(lit, "WARNING! HexOut failed to allocate sufficient memory!!");
       
    81 		TPtrC outbuf(lit);
       
    82 		SCEPPRINT_2(_L("%S\r\n"),&outbuf);
       
    83 		return;
       
    84 		}
       
    85 	TInt i;
       
    86 	for(i=0;i<aBuffer.Length();i++)
       
    87 		{
       
    88 		TInt hi = ((aBuffer[i] >> 4) & 0x0f);
       
    89 		TInt lo = (aBuffer[i] & 0x0f);
       
    90 		
       
    91 		switch(hi)
       
    92 			{
       
    93 			case 0x0a:
       
    94 				{
       
    95 				charbuffer[i*2] = 'A';
       
    96 				break;
       
    97 				}
       
    98 			case 0x0b:
       
    99 				{
       
   100 				charbuffer[i*2] = 'B';
       
   101 				break;
       
   102 				}
       
   103 			case 0x0c:
       
   104 				{
       
   105 				charbuffer[i*2] = 'C';
       
   106 				break;
       
   107 				}
       
   108 			case 0x0d:
       
   109 				{
       
   110 				charbuffer[i*2] = 'D';
       
   111 				break;
       
   112 				}
       
   113 			case 0x0e:
       
   114 				{
       
   115 				charbuffer[i*2] = 'E';
       
   116 				break;
       
   117 				}
       
   118 			case 0x0f:
       
   119 				{
       
   120 				charbuffer[i*2] = 'F';
       
   121 				break;
       
   122 				}
       
   123 			default:
       
   124 				{
       
   125 				charbuffer[i*2] = TText8('9' - (9 - hi));
       
   126 				break;
       
   127 				}
       
   128 			}
       
   129 		switch(lo)
       
   130 			{
       
   131 			case 0x0a:
       
   132 				{
       
   133 				charbuffer[i*2+1] = 'A';
       
   134 				break;
       
   135 				}
       
   136 			case 0x0b:
       
   137 				{
       
   138 				charbuffer[i*2+1] = 'B';
       
   139 				break;
       
   140 				}
       
   141 			case 0x0c:
       
   142 				{
       
   143 				charbuffer[i*2+1] = 'C';
       
   144 				break;
       
   145 				}
       
   146 			case 0x0d:
       
   147 				{
       
   148 				charbuffer[i*2+1] = 'D';
       
   149 				break;
       
   150 				}
       
   151 			case 0x0e:
       
   152 				{
       
   153 				charbuffer[i*2+1] = 'E';
       
   154 				break;
       
   155 				}
       
   156 			case 0x0f:
       
   157 				{
       
   158 				charbuffer[i*2+1] = 'F';
       
   159 				break;
       
   160 				}
       
   161 			default:
       
   162 				{
       
   163 				charbuffer[i*2+1] = TText8('9' - (9 - lo));
       
   164 				break;
       
   165 				}
       
   166 			}
       
   167 		}
       
   168 	charbuffer[i*2] = '\0';
       
   169 
       
   170 	TPtrC outbuf(charbuffer);
       
   171 	SCEPPRINT(_L("IrTranpUtil: charbuffer"));
       
   172 	SCEPPRINT_2(_L("%S\r\n"),&outbuf);
       
   173 	delete [] charbuffer;
       
   174 	}