datacommsserver/esockserver/csock/ConnPref.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 2002-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  @internalTechnology 
       
    19 */
       
    20 
       
    21 #include "ConnPref.h"
       
    22 #include <es_connpref.h>
       
    23 
       
    24 #include <comms-infras/metadata.h>
       
    25 #include <comms-infras/metatype.h>
       
    26 
       
    27 using namespace Meta;
       
    28 
       
    29 
       
    30 EXPORT_C TConnPref::TConnPref()
       
    31 : TBuf8<KMaxConnPrefSize>()
       
    32 /**
       
    33 Default Constructor 
       
    34 Setting length to 0x40 and Extension ID to 0x00
       
    35 
       
    36 */
       
    37 	{
       
    38 	SetLength(KMaxConnPrefSize);
       
    39 	SetExtensionId(TConnPref::EConnPrefUnknown);
       
    40 	}
       
    41 
       
    42 EXPORT_C TConnPref::TConnPref(TInt aExtensionId)
       
    43 : TBuf8<KMaxConnPrefSize>()
       
    44 /**
       
    45 Constructor 
       
    46 Setting length to 0x40 and Extension ID to aExtensionId
       
    47 
       
    48 @param aExtensionId
       
    49 */
       
    50 	{
       
    51 	SetLength(KMaxConnPrefSize);
       
    52 	SetExtensionId(aExtensionId);
       
    53 	}
       
    54 
       
    55 EXPORT_C void TConnPref::SetExtensionId(TInt aExtensionId)
       
    56 /**
       
    57 Set the extension (derrived class) id
       
    58 
       
    59 @param aExtensionId
       
    60 */
       
    61 	{
       
    62 	BasePtr()->iExtensionId = aExtensionId;
       
    63 	}
       
    64 
       
    65 EXPORT_C TInt TConnPref::ExtensionId() const
       
    66 /**
       
    67 Get the extension (derrived class) id 
       
    68 
       
    69 @return Extension ID.
       
    70 */
       
    71 	{
       
    72 	return BasePtr()->iExtensionId;
       
    73 	}
       
    74 
       
    75 EXPORT_C void TConnPref::SetUserLen(TInt aLen)
       
    76 /**
       
    77 Set descriptor length to actual length of subclass
       
    78 
       
    79 @param aLen,length of subclass.
       
    80 */
       
    81 	{
       
    82 
       
    83 	SetLength(aLen+Align4(sizeof(SConnPref)));
       
    84 	}
       
    85 
       
    86 EXPORT_C TInt TConnPref::GetUserLen()
       
    87 /**
       
    88 Get the length of the subclass 
       
    89 
       
    90 @return length of the subclass 
       
    91 */
       
    92 	{
       
    93 
       
    94 	return Length()-Align4(sizeof(SConnPref));
       
    95 	}
       
    96 
       
    97 struct SCommApPref
       
    98 /**
       
    99 @internalTechnology
       
   100 @released since v9.1
       
   101 */
       
   102 	{
       
   103 	TUint32 iAp;
       
   104 	};
       
   105 
       
   106 EXPORT_C TConnSnapPref::TConnSnapPref() :
       
   107 	TConnPref(EConnPrefSnap)
       
   108 	{
       
   109 	SetSnap( 0 );
       
   110 	}
       
   111 		
       
   112 EXPORT_C TConnSnapPref::TConnSnapPref(TUint32 aSnap) :
       
   113 	TConnPref(EConnPrefSnap)
       
   114 	{
       
   115 	SetSnap( aSnap );
       
   116 	}
       
   117 
       
   118 EXPORT_C void TConnSnapPref::SetSnap(TUint32 aSnap)
       
   119 /**
       
   120 Sets SNAP
       
   121 @publishedAll
       
   122 @param aSnap - new SNAP value
       
   123 @released since v9.1
       
   124 */
       
   125 	{
       
   126 	SCommApPref* prefPtr = reinterpret_cast<SCommApPref*>(UserPtr());
       
   127 	prefPtr->iAp = aSnap;
       
   128 	}
       
   129 
       
   130 EXPORT_C TUint32 TConnSnapPref::Snap() const
       
   131 /**
       
   132 @publishedPartner
       
   133 @return stored SNAP value
       
   134 @released since v9.1
       
   135 */
       
   136 	{
       
   137 	return reinterpret_cast<SCommApPref*>(UserPtr())->iAp;
       
   138 	}
       
   139 
       
   140 
       
   141 EXPORT_C TConnIdList::TConnIdList() :
       
   142 	TConnPref(EConnPrefIdList)
       
   143 	{
       
   144 	CountRef() = 0;
       
   145 	}
       
   146 
       
   147 EXPORT_C TInt TConnIdList::MaxCount()
       
   148 /**
       
   149 @publishedPartner
       
   150 @return max number of APs to store
       
   151 @released since v9.1
       
   152 */
       
   153 	{
       
   154 	return (KMaxConnPrefSize - sizeof(TInt))/sizeof(TInt);
       
   155 	}
       
   156 
       
   157 EXPORT_C TInt& TConnIdList::operator[](TInt aIndex)
       
   158 /**
       
   159 operator to access the list based on index
       
   160 @publishedPartner
       
   161 @param aIndex - index
       
   162 @released since v9.1
       
   163 */
       
   164 	{
       
   165 	if (! (aIndex >= 0 && aIndex < Count()))
       
   166 		{
       
   167 	  	User::Panic (KNullDesC, KErrOverflow);
       
   168      	}
       
   169 	
       
   170 	return *(IdList() + aIndex);
       
   171 	}
       
   172 
       
   173 EXPORT_C void TConnIdList::Append(TInt aId)
       
   174 /**
       
   175 Appends new Id
       
   176 @publishedPartner
       
   177 @param aAp - Id to add
       
   178 @released since v9.1
       
   179 */
       
   180 	{
       
   181 	TUint16& count = CountRef();
       
   182 	if (count >= MaxCount())
       
   183 		{
       
   184 		User::Panic (KNullDesC, KErrOverflow);
       
   185 		}
       
   186 	
       
   187 	(*this)[count++] = aId;
       
   188 	}
       
   189 
       
   190 EXPORT_C TInt TConnIdList::Get(TInt aIndex) const
       
   191 /**
       
   192 Allows read only access to Id based on index
       
   193 @publishedPartner
       
   194 @released since v9.1
       
   195 */
       
   196 	{
       
   197 	//cast the const away to reuse the op[] tha's not changing anything
       
   198 	return (*(const_cast<TConnIdList*>(this)))[aIndex];
       
   199 	}
       
   200 
       
   201 EXPORT_C TInt TConnIdList::Count() const
       
   202 /**
       
   203 @publishedPartner
       
   204 @return number of Ids in the list
       
   205 @released since v9.1
       
   206 */
       
   207 	{
       
   208 	//cast the const away to reuse the CountRef() fn that's not changing anything
       
   209 	return (const_cast<TConnIdList*>(this))->CountRef();
       
   210 	}
       
   211 
       
   212 
       
   213 EXPORT_C TConnPrefList::TConnPrefList()
       
   214 /**
       
   215 Constructor for TConnPrefList
       
   216 @publishedAll
       
   217 */
       
   218 	{
       
   219 	SetExtensionId(TConnPref::EConnPrefEComList);
       
   220 	}
       
   221 
       
   222 EXPORT_C TConnPrefList::~TConnPrefList()
       
   223 /**
       
   224 Destrcutor - closes iPrefs deleting the pointers but not the objects they point
       
   225 at. These should be deleted explicitly.
       
   226 @publishedAll
       
   227 */
       
   228 	{
       
   229 	iPrefs.Close();
       
   230 	}
       
   231 
       
   232 EXPORT_C TConnPrefList* TConnPrefList::NewL()
       
   233 	{
       
   234 	TConnPrefList* self = new (ELeave) TConnPrefList();
       
   235 	return self;	
       
   236 	}
       
   237 
       
   238 EXPORT_C TConnPrefList* TConnPrefList::LoadL(TDesC8& aDes)
       
   239 /**
       
   240 This function loads an instance of TConnPrefList from the given descriptor and
       
   241 returns a pointer to that oject in memory is possible, otherwise leaves
       
   242 @publishedAll
       
   243 @param aDes - a descriptor containing a serialized form of TConnPrefList
       
   244 @return TConnPrefList* to the instance of the object in memory
       
   245 */
       
   246 	{
       
   247 	TConnPrefList* obj = TConnPrefList::NewL();
       
   248 	CleanupStack::PushL(obj);
       
   249 	User::LeaveIfError(obj->Load(aDes));
       
   250 	CleanupStack::Pop(obj);
       
   251 	return obj;
       
   252 	}
       
   253 
       
   254 EXPORT_C TInt TConnPrefList::Length()
       
   255 /**
       
   256 Returns the size of the desciptor needed to store the object using the store
       
   257 function
       
   258 @publishedAll
       
   259 @return Integer representing the length of descriptor need to store the object
       
   260 */
       
   261 	{
       
   262 	return KMaxConnPrefSize + iPrefs.Length();
       
   263 	}
       
   264 
       
   265 EXPORT_C TInt TConnPrefList::Load(TDesC8& aDes)
       
   266 /**
       
   267 This function loads an instance of TConnPrefList from the given descriptor into 
       
   268 the object its called on. It returns an integer error on failiure
       
   269 @publishedAll
       
   270 @param - a descriptor containing a serialized instance of TConnPrefList
       
   271 @return - KErrNone if successful otherwise KErrArgument
       
   272 */
       
   273 	{	
       
   274 	TPtrC8 aBuffer(aDes);
       
   275 	
       
   276 	Mem::Copy(&iBuf, aBuffer.Ptr() ,sizeof(iBuf));
       
   277 	aBuffer.Set(aBuffer.Ptr() + sizeof( iBuf), aBuffer.Length() - sizeof(iBuf));
       
   278 	
       
   279 	if(ExtensionId() == TConnPref::EConnPrefUnknown || ExtensionId() == TConnPref::EConnPrefCommDb || 
       
   280 			ExtensionId() == TConnPref::EConnPrefCommDbMulti || ExtensionId() == TConnPref::EConnPrefSnap ||
       
   281 			ExtensionId() == TConnPref::EConnPrefIdList || ExtensionId() == TConnPref::EConnPrefSip ||
       
   282 			ExtensionId() == TConnPref::EConnPrefProviderInfo)
       
   283 		{
       
   284 			return KErrNone;
       
   285 		}
       
   286 	else if(ExtensionId() != TConnPref::EConnPrefEComList)
       
   287 		{
       
   288 			return KErrArgument;
       
   289 		}
       
   290 		
       
   291 	TInt ret = iPrefs.GetTypeId().Check(aBuffer);
       
   292 	if(ret != KErrNone)
       
   293 		{
       
   294 		return ret;
       
   295 		}
       
   296 	return ret = iPrefs.Load(aBuffer);
       
   297 	}
       
   298 
       
   299 EXPORT_C TInt TConnPrefList::Store(TDes8& aDes)
       
   300 /**
       
   301 This puts a serilized form of the object that the function is called on into the
       
   302 descriptor supplied
       
   303 @publishedAll
       
   304 @param - a descriptor that is sufficiently long enough to contain the serialized
       
   305 version of the object
       
   306 @return - KErrNone if successful
       
   307  		- KErrOverflow if the descriptor is too short
       
   308  		- otherwise one of the standard error codes
       
   309 */
       
   310 	{
       
   311 	TUint length = Length();
       
   312 	
       
   313 	aDes.Append((TUint8*)&iBuf,sizeof(iBuf));
       
   314 	
       
   315 	if(aDes.MaxLength() < length)
       
   316 		{
       
   317 		return KErrOverflow;
       
   318 		}
       
   319 	
       
   320 	TInt ret = iPrefs.Store(aDes);
       
   321 	if(ret != KErrNone)
       
   322 		{
       
   323 		return ret;
       
   324 		}
       
   325 		 
       
   326 	return KErrNone;
       
   327 	}
       
   328 
       
   329 EXPORT_C void TConnPrefList::AppendL(SMetaDataECom* aFamily)
       
   330 /**
       
   331 This adds the pointer supplied to the list. Also see TConnAPPref
       
   332 Only one instance of TConnAPPref can be appended to the list
       
   333 and passed to RConnection::Start. More than one will force
       
   334 RConnection::Start to return KErrArgument
       
   335 @publishedAll
       
   336 @param - a pointer to an SMetaDataECom object
       
   337 */
       
   338 	{
       
   339 	if(aFamily == NULL)
       
   340 		{
       
   341 		User::Leave(KErrArgument);
       
   342 		}
       
   343 	iPrefs.AppendL(aFamily);
       
   344 	}
       
   345 
       
   346 EXPORT_C SMetaData* TConnPrefList::operator[](TInt aIndex)
       
   347 /**
       
   348 This retrieves a pointer to the object at the given index
       
   349 @publishedAll
       
   350 @param - aIndex - the position of the object in the list
       
   351 @return a pointer to the object in memory
       
   352 */
       
   353 	{
       
   354 	if(iPrefs.Count() > aIndex)
       
   355 		{
       
   356 		return iPrefs[aIndex];
       
   357 		}
       
   358 	User::Leave(KErrArgument);
       
   359 	return NULL;
       
   360 	}
       
   361 
       
   362 EXPORT_C void TConnPrefList::Remove(TInt aIndex)
       
   363 /**
       
   364 This removes the objet at the given index from the list
       
   365 @publishedAll
       
   366 @param - aIndex - the position of the object in the list
       
   367 */
       
   368 	{
       
   369 	if(iPrefs.Count() > aIndex)
       
   370 		{
       
   371 		iPrefs.Remove(aIndex);
       
   372 		}
       
   373 	}
       
   374 
       
   375 EXPORT_C TInt TConnPrefList::Count()
       
   376 /**
       
   377 This returns an integer value representing the number of objects in the list
       
   378 @publishedAll
       
   379 @return - integer representing the number of objects in the list
       
   380 */
       
   381 	{
       
   382 	return iPrefs.Count();
       
   383 	}
       
   384 
       
   385