telephonyserverplugins/simtsy/src/utils.cpp
changeset 0 3553901f7fa8
child 24 6638e7f4bd8f
equal deleted inserted replaced
-1:000000000000 0:3553901f7fa8
       
     1 // Copyright (c) 2001-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 // Contains a number of utility classes that are of general use
       
    15 // thoughout the TSY.
       
    16 // 
       
    17 //
       
    18 
       
    19 /**
       
    20  @file
       
    21 */
       
    22 
       
    23 #include "utils.h"
       
    24 #include "CSimPhone.h"
       
    25 
       
    26 
       
    27 RCall::THookStatus ConvertStateToHook(RMobileCall::TMobileCallStatus aStatus)
       
    28 /**
       
    29 	Convert a call state to a hook state.
       
    30 */
       
    31 	{
       
    32 	if(aStatus==RMobileCall::EStatusUnknown)
       
    33 		return RCall::EHookStatusUnknown;
       
    34 	if((aStatus==RMobileCall::EStatusIdle)||
       
    35 	   (aStatus==RMobileCall::EStatusRinging))
       
    36 	   return RCall::EHookStatusOn;
       
    37 	return RCall::EHookStatusOff;
       
    38 	}
       
    39 
       
    40 RPhone::TMode ConvertStateToMode(RMobileCall::TMobileCallStatus aStatus)
       
    41 /**
       
    42 	Convert a call state to a RPhone::TMode state.
       
    43 	Note that the mapping table is not entirely clear here.
       
    44 	The following mappings have been used here:
       
    45 		EStatusUnknown		=>	EModeUnknown
       
    46 		EStatusIdle			=>	EModeIdle
       
    47 		EStatusDialling		=>	EModeEstablishingLink
       
    48 		EStatusRinging		=>	EModeEstablishingLink
       
    49 		EStatusAnswering	=>	EModeEstablishingLink
       
    50 		EStatusConnecting	=>	EModeEstablishingLink
       
    51 		EStatusConnected	=>	EModeOnlineData
       
    52 		EStatusHold			=>	EModeOnlineData
       
    53 		EStatusHangingUp	=>	EModeOnlineData
       
    54 
       
    55   There is currently no mapping for EModeOnlineCommand, so this mode state will
       
    56   never be returned.
       
    57 */
       
    58 	{
       
    59 	if(aStatus==RMobileCall::EStatusUnknown)
       
    60 		return RPhone::EModeUnknown;
       
    61 	if(aStatus==RMobileCall::EStatusIdle)
       
    62 		return RPhone::EModeIdle;
       
    63 	if((aStatus==RMobileCall::EStatusDialling) ||
       
    64 		(aStatus==RMobileCall::EStatusAnswering) ||
       
    65 		(aStatus==RMobileCall::EStatusRinging) ||
       
    66 		(aStatus==RMobileCall::EStatusConnecting))
       
    67 		return RPhone::EModeEstablishingLink;
       
    68 	if(aStatus==RMobileCall::EStatusConnected)
       
    69 		return RPhone::EModeOnlineData;
       
    70 	if(aStatus==RMobileCall::EStatusDisconnecting)
       
    71 		return RPhone::EModeOnlineData;
       
    72 	if(aStatus==RMobileCall::EStatusHold)
       
    73 		return RPhone::EModeOnlineData;
       
    74 	return RPhone::EModeUnknown;		// Dummy final return value.
       
    75 	}
       
    76 
       
    77 TBool IsStateActive(RMobileCall::TMobileCallStatus aStatus)
       
    78 	{
       
    79 	if((aStatus==RMobileCall::EStatusUnknown) ||
       
    80 	   (aStatus==RMobileCall::EStatusIdle) ||
       
    81 	   (aStatus==RMobileCall::EStatusRinging))
       
    82 	   return EFalse;
       
    83 	return ETrue;
       
    84 	}
       
    85 
       
    86 void ConvertAsciiSms(const TDesC8& aAsciiPdu, TDes8& aOctetPdu)
       
    87 /**
       
    88  * Populate the SMS PDU from an ASCII representation, such as that stored in the configuration file.
       
    89  * This simply involves converting the ASCII representation in the configuration file
       
    90  * into a hex representation to complete the request.
       
    91  */
       
    92 	{
       
    93 	// Check that the ASCII PDU length is even
       
    94 	__ASSERT_ALWAYS((aAsciiPdu.Length()&0x1)==0x0,SimPanic(EIllegalOddNumberOfCharactersInConfigFilePdu));
       
    95 
       
    96 	TUint8 digit;
       
    97 	TInt i;
       
    98 	for(i=0;i<aAsciiPdu.Length();i+=2)
       
    99 		{
       
   100 		TLex8 lex(aAsciiPdu.Mid(i,2));
       
   101 		TInt ret=lex.Val(digit,EHex);
       
   102 		__ASSERT_ALWAYS(ret==KErrNone,SimPanic(EIllegalHexCharacterInConfigFilePdu));
       
   103 		aOctetPdu.Append(&digit,1);
       
   104 		}
       
   105 	}
       
   106 
       
   107 /**
       
   108 Converts a buffer containing string of the hexadecimal characters,
       
   109 representing the binary data, into this binary data!
       
   110 
       
   111 @param aSrc The buffer containing text representation.
       
   112 @param aDst Binary data will be written to this buffer.
       
   113 */
       
   114 void ConvertTextToBinary(const TDesC& aSrc, TDes8& aDst)
       
   115 	{
       
   116 	// Check that the ASCII PDU length is even
       
   117 	__ASSERT_ALWAYS((aSrc.Length()&0x1)==0x0, SimPanic(EInvalidParameterFormatInConfigFile));
       
   118 
       
   119 	aDst.SetLength(aSrc.Length() / 2);
       
   120 
       
   121 	for (TInt ii = 0; ii < aSrc.Length(); ii += 2)
       
   122 		{
       
   123 		TInt val = 0;
       
   124 		if ((aSrc[ii] >= '0') && (aSrc[ii] <= '9'))
       
   125 			{
       
   126 			val = ((aSrc[ii] - '0') << 4);
       
   127 			}
       
   128 		else if ((aSrc[ii] >= 'A') && (aSrc[ii] <= 'F'))
       
   129 			{
       
   130 			val = ((aSrc[ii] - 'A' + 10) << 4);
       
   131 			}
       
   132 
       
   133 		if ((aSrc[ii+1] >= '0') && (aSrc[ii+1] <= '9'))
       
   134 			{
       
   135 			val += (aSrc[ii+1] - '0');
       
   136 			}
       
   137 		else if ((aSrc[ii+1] >= 'A') && (aSrc[ii+1] <= 'F'))
       
   138 			{
       
   139 			val += (aSrc[ii+1] - 'A' + 10);
       
   140 			}
       
   141 
       
   142 		aDst[ii/2] = (TUint8) val;
       
   143 		}
       
   144 	}
       
   145 
       
   146 TInt AsciiToNum(const TPtrC8 aParam, TUint8& aVal)
       
   147 /**
       
   148 * Converts a hexadecimal or decimal value stored in a TPtrC to its TUint value
       
   149 *
       
   150 * @param aParam		TPtr pointing to the hexadecimal or decimal number to convert.
       
   151 * @param aVal		TUint reference to the val converted.
       
   152 * @return TInt		Standard error return.
       
   153 */
       
   154 	{
       
   155 	_LIT8(KHexPrefix,"0x");
       
   156 	TInt ret=KErrNone;
       
   157 	if(aParam.Left(2).Compare(KHexPrefix)==0)
       
   158 		{
       
   159 		TLex8 lex(aParam.Mid(2));
       
   160 		ret=lex.Val(aVal,EHex);
       
   161 		}
       
   162 	else
       
   163 		{
       
   164 		TLex8 lex(aParam);
       
   165 		ret=lex.Val(aVal,EDecimal);
       
   166 		}
       
   167 	return ret;
       
   168 	}
       
   169 
       
   170 TInt AsciiToNum(const TPtrC8 aParam, TUint16& aVal)
       
   171 /**
       
   172 * Converts a hexadecimal or decimal value stored in a TPtrC to its TUint value
       
   173 *
       
   174 * @param aParam		TPtr pointing to the hexadecimal or decimal number to convert.
       
   175 * @param aVal		TUint reference to the val converted.
       
   176 * @return TInt		Standard error return.
       
   177 */
       
   178 	{
       
   179 	_LIT8(KHexPrefix,"0x");
       
   180 	TInt ret=KErrNone;
       
   181 	if(aParam.Left(2).Compare(KHexPrefix)==0)
       
   182 		{
       
   183 		TLex8 lex(aParam.Mid(2));
       
   184 		ret=lex.Val(aVal,EHex);
       
   185 		}
       
   186 	else
       
   187 		{
       
   188 		TLex8 lex(aParam);
       
   189 		ret=lex.Val(aVal,EDecimal);
       
   190 		}
       
   191 	return ret;
       
   192 	}
       
   193 
       
   194 TInt AsciiToNum(const TPtrC8 aParam, TUint32& aVal)
       
   195 /**
       
   196 * Converts a hexadecimal or decimal value stored in a TPtrC to its TUint value
       
   197 *
       
   198 * @param aParam		TPtr pointing to the hexadecimal or decimal number to convert
       
   199 * @param aVal		TUint reference to the val converted.
       
   200 * @return TInt		Standard error return.
       
   201 */
       
   202 	{
       
   203 	_LIT8(KHexPrefix,"0x");
       
   204 	TInt ret=KErrNone;
       
   205 	if(aParam.Left(2).Compare(KHexPrefix)==0)
       
   206 		{
       
   207 		TLex8 lex(aParam.Mid(2));
       
   208 		ret=lex.Val(aVal,EHex);
       
   209 		}
       
   210 	else
       
   211 		{
       
   212 		TLex8 lex(aParam);
       
   213 		ret=lex.Val(aVal,EDecimal);
       
   214 		}
       
   215 	return ret;
       
   216 	}
       
   217 
       
   218 TInt AsciiToNum(const TPtrC8 aParam, TInt32& aVal)
       
   219 /**
       
   220 * Converts a hexadecimal or decimal value stored in a TPtrC to its TUint value
       
   221 *
       
   222 * @param aParam		TPtr pointing to the hexadecimal or decimal number to convert
       
   223 * @param aVal		TUint reference to the val converted.
       
   224 * @return TInt		Standard error return.
       
   225 */
       
   226 	{
       
   227 	_LIT8(KHexPrefix,"0x");
       
   228 	TInt ret=KErrNone;
       
   229 	if(aParam.Left(2).Compare(KHexPrefix)==0)
       
   230 		{
       
   231 		TLex8 lex(aParam.Mid(2));
       
   232 		ret=lex.Val(aVal,EHex);
       
   233 		}
       
   234 	else
       
   235 		{
       
   236 		TLex8 lex(aParam);
       
   237 		ret=lex.Val(aVal,EDecimal);
       
   238 		}
       
   239 	return ret;
       
   240 	}
       
   241 	
       
   242 TInt ParseMixedBinaryAsciiDataL(TDes8& aTextToConvert)
       
   243 /**
       
   244 Parses aTextToConvert based on the following rules:
       
   245 '\\' (double backslash) is used to denote a single '\'
       
   246 (single backslash)
       
   247 '\xnn' denote a byte of binary data where nn is in hex-decimal.
       
   248 The '\xnn' in aTextToConvert is replaced by the binary byte
       
   249 that it represents.
       
   250 
       
   251 For example: If aTextToConvert contains "abc\\def\xFF",
       
   252 after parsing, it will contain "abc\def?" where ? = 0xFF.
       
   253 
       
   254 @param aTextToConvert Modifiable buffer which will be parsed. 
       
   255 
       
   256 @return KErrNone if aTextToConvert is in valid
       
   257         EAdditionalParamDataFormatMixedBinaryAndAscii format.
       
   258         KErrArgument if aTextToConvert is in an incorrect format.
       
   259 
       
   260 @panic KErrNoMemory if there is not enough memory to do the parsing.
       
   261 */
       
   262 	{
       
   263 	// Pointer to unparsed portion of additionalParamDataBuffer
       
   264 	HBufC8* resultBuffer = HBufC8::NewLC(aTextToConvert.Length());
       
   265 
       
   266 	__ASSERT_ALWAYS(resultBuffer, PanicClient(KErrNoMemory));
       
   267 	
       
   268 	TPtr8 result(resultBuffer->Des());
       
   269 	
       
   270 	// Position of backslash
       
   271 	TInt pos = 0;
       
   272 
       
   273 	while ((pos = aTextToConvert.Locate('\\')) != KErrNotFound)
       
   274 		{
       
   275 		// Check that the backslash is followed by at least one more character
       
   276 		if ((pos+1) >= aTextToConvert.Length())
       
   277 			{
       
   278 			return KErrArgument;
       
   279 			}
       
   280 
       
   281 		TUint8 modifier = aTextToConvert[pos+1];
       
   282 
       
   283 		// Parse depending on character after the backslash
       
   284 		switch (modifier)
       
   285 			{
       
   286 		case '\\':
       
   287 			// Next character after the '\' is another '\'.
       
   288 			// Replace it with a single '\' and move
       
   289 			// on.
       
   290 			result.Append(aTextToConvert.Left(pos+1));
       
   291 			aTextToConvert.Delete(0, pos+2);
       
   292 			break;
       
   293 		case 'x':
       
   294 			// Next character is an 'x' so check that there are three 
       
   295 			// characters after the backslash (one for the x and two
       
   296 			// characters of HEX.
       
   297 			if ((pos+3) >= aTextToConvert.Length()) 
       
   298 				{
       
   299 				return KErrArgument;
       
   300 				}
       
   301 			// Convert those to HEX and replace '\xNN' with this.
       
   302 			result.Append(aTextToConvert.Left(pos));
       
   303 			TUint8 hexAsInt;
       
   304 			if (AsciiHexToNum(aTextToConvert.MidTPtr(pos+2,2), hexAsInt) != KErrNone)
       
   305 				{
       
   306 				return KErrArgument;
       
   307 				}
       
   308 			// Append the raw byte to the result
       
   309 			result.SetLength(result.Length()+1);
       
   310 			result[result.Length()-1] = hexAsInt;
       
   311 			aTextToConvert.Delete(0, pos+4);
       
   312 			break;		
       
   313 			}
       
   314 		} // End while
       
   315 	aTextToConvert.Insert(0, result);
       
   316 	
       
   317 	CleanupStack::PopAndDestroy(resultBuffer);
       
   318 	return KErrNone;
       
   319 	}
       
   320 
       
   321 /**
       
   322 Returns the numerical value that corresponds to the numeric character
       
   323 of its parameter (in base 17).
       
   324 
       
   325 @param aDigit A char between '0' to '9' or between 'A' to 'G'
       
   326 */
       
   327 TInt CharToSeptNumL(const TChar& aChar)
       
   328 	{
       
   329 	TInt ret = 0;
       
   330 
       
   331 	if((aChar >= '0') && (aChar <= '9'))
       
   332 		{
       
   333 		ret = TUint(aChar) - '0';
       
   334 		}
       
   335 	else if((aChar >= 'A') && (aChar <= 'G'))
       
   336 		{
       
   337 		ret = TUint(aChar) - 'A' + 10;
       
   338 		}
       
   339 	else
       
   340 		{
       
   341 		User::Leave(KErrArgument);
       
   342 		}
       
   343 
       
   344 	return ret;
       
   345 	}