irda/irdastack/SSRC/client-side/IASResponse.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 1999-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 /**
       
    17  @file
       
    18  @internalAll
       
    19 */
       
    20 
       
    21 #include <ir_sock.h>
       
    22 #include "client_util.H"
       
    23 
       
    24 EXPORT_C TIASResponse::TIASResponse()
       
    25 /** Default constructor. Marks the response type as missing.
       
    26 
       
    27 @see TIASDataType */
       
    28 	{
       
    29 	SetLength(1);
       
    30 	operator[](0)=EIASDataMissing;
       
    31 	}
       
    32 
       
    33 void TIASResponse::SetToInteger(TUint anInteger)
       
    34 //
       
    35 // set the response data to the specified integer
       
    36 //
       
    37 	{
       
    38 	SetLength(5);
       
    39 	operator[](0)=(TUint8)EIASDataInteger;
       
    40 	BigEndian::Put32(WPtr()+1,anInteger);
       
    41 	}
       
    42 
       
    43 void TIASResponse::SetToCharString(const TDesC8& aCharString)
       
    44 //
       
    45 // set the response data to the specified integer
       
    46 //
       
    47 	{
       
    48 	__ASSERT_ALWAYS(aCharString.Length()<=256,IrdaUtil::Panic(EIASResponseDes8Overflow));
       
    49 	SetLength(3);
       
    50 	operator[](0)=(TUint8)EIASDataUserString;				// Type field
       
    51 	operator[](1)=(TUint8)EIASCharSetUserStringASCII;		// Character set
       
    52 	operator[](2)=(TUint8)aCharString.Size();				// String Size
       
    53 
       
    54 	Append(aCharString);
       
    55 	}
       
    56 
       
    57 #ifdef _UNICODE
       
    58 void TIASResponse::SetToCharString(const TDesC16& aWideString)
       
    59 //
       
    60 // set the response data to the specified integer
       
    61 //
       
    62 	{
       
    63 	__ASSERT_ALWAYS(aWideString.Size()<=256,IrdaUtil::Panic(EIASResponseDes8Overflow));
       
    64 	SetLength(3);
       
    65 	operator[](0)=(TUint8)EIASDataUserString;				// Type field
       
    66 	operator[](1)=(TUint8)EIASCharSetUserStringUnicode;		// Character Set
       
    67 	operator[](2)=(TUint8)aWideString.Size();				// String Size
       
    68 
       
    69 	TUint8* narrowPtr = (TUint8*)(aWideString.Ptr());
       
    70 	TPtr8  narrowString(narrowPtr,aWideString.Size(),aWideString.Size());
       
    71 	Append(narrowString);
       
    72 	}
       
    73 #endif
       
    74 
       
    75 void TIASResponse::SetToOctetSeq(const TDesC8& aBinData)
       
    76 //
       
    77 // set the response data to the specified integer
       
    78 //
       
    79 	{
       
    80 	__ASSERT_ALWAYS(aBinData.Length()<=256,IrdaUtil::Panic(EIASResponseDes8Overflow));
       
    81 	SetLength(3);
       
    82 	operator[](0)=(TUint8)EIASDataOctetSequence;			// Type field
       
    83 	BigEndian::Put16(WPtr()+1,(TUint16)aBinData.Length());
       
    84 	Append(aBinData);
       
    85 	}
       
    86 
       
    87 //
       
    88 // Suck out an octet sequence if there is one.
       
    89 //
       
    90 EXPORT_C TInt TIASResponse::GetInteger(TInt &aResult,TInt anIndex) const
       
    91 /** Returns the response integer.
       
    92 
       
    93 This is called if the response type indicates an integer.
       
    94 
       
    95 @param aResult On return, contains the 32-bit response integer value. 
       
    96 @param anIndex Reserved for future use. This argument must be allowed to default 
       
    97 to 0 and must not be overriden. 
       
    98 @return KErrNone, if successful. KErrCorrupt, if the response type is not an 
       
    99 integer. */
       
   100 	{
       
   101 	if (Type()!=EIASDataInteger)
       
   102 		return KErrCorrupt;
       
   103 	// IAS Integers are 4 bytes, zero indexed with one length byte.
       
   104 	if (Length()<5+anIndex*4)
       
   105 		return KErrCorrupt;
       
   106 	aResult=BigEndian::Get32(Ptr()+1);
       
   107 	return KErrNone;
       
   108 	}
       
   109 
       
   110 //
       
   111 // Suck out an octet sequence if there is one.
       
   112 //
       
   113 EXPORT_C TInt TIASResponse::GetOctetSeq(TDes8 &aResult,TInt anIndex) const
       
   114 /** Returns the response binary data.
       
   115 
       
   116 @param aResult On return, contains the the binary data. The number of bytes 
       
   117 returned can never be greater than (KMaxQueryStringLength - 3). 
       
   118 @param anIndex Reserved for future use. 
       
   119 @return KErrNone, if successful. KErrCorrupt, if the response type is not binary 
       
   120 data. KErrNotSupported, if a non-zero value has been specified for anIndex. */
       
   121 	{
       
   122 	if (anIndex>0)
       
   123 		return KErrNotSupported;
       
   124 	if (Type()!=EIASDataOctetSequence)
       
   125 		return KErrCorrupt;
       
   126 
       
   127 	aResult.Copy(Ptr()+3,operator[](2));
       
   128 	return KErrNone;	
       
   129 	}
       
   130 
       
   131 //
       
   132 // Suck out an ASCII/ISO string if there is one.
       
   133 //
       
   134 EXPORT_C TInt TIASResponse::GetCharString(TDes8 &aResult,TInt anIndex) const
       
   135 /** Returns the response string.
       
   136 
       
   137 This is called if the response type indicates a string.
       
   138 
       
   139 @param aResult On return, an 8 bit modifiable descriptor containing the response 
       
   140 string. The length of the response string can never be greater than the value 
       
   141 of (KMaxQueryStringLength - 3). 
       
   142 @param anIndex Reserved for future use. This argument must be allowed to default 
       
   143 to 0 and must not be overriden. 
       
   144 @return If successful, one of the TIASCharSet enumerator values defining the 
       
   145 character set encoding of the response string. KErrNotSupported, if a non-zero 
       
   146 value has been specified for anIndex. KErrCorrupt, if the response type is 
       
   147 not a string. 
       
   148 @see TIASResponse::Type() */
       
   149 	{
       
   150 	if (anIndex>0)
       
   151 		return KErrNotSupported;
       
   152 	if (Type()!=EIASDataUserString)
       
   153 		return KErrCorrupt;
       
   154 
       
   155 	TUint8 charSet = operator[](1);
       
   156 	TInt returnValue = static_cast<TInt>(charSet);
       
   157 	aResult.Copy(Ptr()+3,operator[](2));
       
   158 
       
   159 	return returnValue;
       
   160 	}
       
   161 
       
   162 //
       
   163 // Suck out an ASCII/ISO string if there is one.
       
   164 //
       
   165 EXPORT_C const TPtrC8 TIASResponse::GetCharString8(TInt anIndex) const
       
   166 /** Returns a non-modifiable pointer descriptor representing the response string.
       
   167 
       
   168 This is called if the response type indicates a string.
       
   169 
       
   170 @param anIndex Reserved for future use. This argument must be allowed to default 
       
   171 to 0 and must not be overriden. 
       
   172 @return A non-modifiable pointer descriptor representing the response string. 
       
   173 If the response type is not a string or the string is flagged as being UNICODE, 
       
   174 then this pointer descriptor is empty, i.e. it has zero length. 
       
   175 @see TIASResponse::Type() */
       
   176 	{
       
   177 	TPtr8 p(NULL,0);
       
   178 
       
   179 	if (anIndex>0)
       
   180 		return p;
       
   181 	if (Type()!=EIASDataUserString || operator[](3)==EIASCharSetUserStringUnicode)
       
   182 		return p;
       
   183 
       
   184 	p.Set(WPtr()+3,operator[](2),operator[](2));
       
   185 
       
   186 	return TPtrC8(p);
       
   187 	}
       
   188 
       
   189 //
       
   190 // Suck out a unicode string if there is one.
       
   191 //
       
   192 EXPORT_C TInt TIASResponse::GetCharString(TDes16 &/*aResult*/,TInt /*anIndex*/) const
       
   193 /** Gets a TDes16 string of unicode characters from the TIASResponse object. */
       
   194 	{
       
   195 	if (Type()!=EIASDataUserString && operator[](3)!=EIASCharSetUserStringUnicode)
       
   196 		return KErrCorrupt;
       
   197 	return KErrNotSupported;
       
   198 	}
       
   199 
       
   200 //
       
   201 // Suck out a unicode string if there is one.
       
   202 //
       
   203 EXPORT_C const TPtrC16 TIASResponse::GetCharString16(TInt /*anIndex*/) const
       
   204 /** Gets a TPtrC16 string of unicode characters from the TIASResponse object. */
       
   205 	{
       
   206 	return TPtr16(NULL,0);
       
   207 	}
       
   208 
       
   209 EXPORT_C TBool TIASResponse::IsList() const
       
   210 	{
       
   211 	return operator[](0)>0;
       
   212 	}
       
   213 
       
   214 EXPORT_C TInt TIASResponse::NumItems() const
       
   215 	{
       
   216 	return operator[](0);
       
   217 	}
       
   218 
       
   219 EXPORT_C TIASDataType TIASResponse::Type() const
       
   220 	{
       
   221 	if ((operator[](0)>EIASDataMissing)&&(operator[](0)<=EIASDataUserString))
       
   222 		return  (TIASDataType)operator[](0);
       
   223 	else
       
   224 		return EIASDataMissing;
       
   225 	}
       
   226 
       
   227 // EOF