os/ossrv/syslibsapitest/syslibssvs/ecom/src/T_EComSessionData.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 
    20 #include "T_EComSessionData.h"
    21 #include "T_ImplementationInformationData.h"
    22 #include "TestEComInterface.h"
    23 
    24 /*@{*/
    25 ///	Parameters
    26 _LIT(KUid,							"uid");
    27 _LIT(KResolver,						"resolver");
    28 _LIT(KExpectedResult,				"expected");
    29 _LIT(KResolverUid,					"resolver_uid");
    30 _LIT(KOffset,						"offset");
    31 _LIT(KInteger,						"integer");
    32 _LIT(KDescriptor,					"descriptor");
    33 _LIT(KEmpty,						"empty");
    34 _LIT(KSaveTo,						"saveto");
    35 _LIT(KSaveIndex,					"saveindex");
    36 
    37 ///	REComSession
    38 _LIT(KCmdOpenL,		 				"OpenL");
    39 _LIT(KCmdClose,					 	"Close");
    40 _LIT(KCmdFinalClose,				"FinalClose");
    41 _LIT(KCmdNotifyOnChange,			"NotifyOnChange");
    42 _LIT(KCmdCancelNotifyOnChange,		"CancelNotifyOnChange");
    43 _LIT(KCmdListImplementationsL,		"ListImplementationsL");
    44 _LIT(KCmdCreateImplementationL,		"CreateImplementationL");
    45 _LIT(KCmdDestroyedImplementation,	"DestroyedImplementation");
    46 _LIT(KCmdClean,						"~");
    47 
    48 _LIT(KEmptyString,					"");
    49 _LIT(KDefault,						"default");
    50 /*@}*/
    51 
    52 //////////////////////////////////////////////////////////////////////
    53 // Construction/Destruction
    54 //////////////////////////////////////////////////////////////////////
    55 
    56 /**
    57  * Two phase constructor
    58  */
    59 CT_EComSessionData* CT_EComSessionData::NewL()
    60 	{
    61 	CT_EComSessionData*	ret=new (ELeave) CT_EComSessionData();
    62 	CleanupStack::PushL(ret);
    63 	ret->ConstructL();
    64 	CleanupStack::Pop(ret);
    65 	return ret;
    66 	}
    67 
    68 /**
    69  * Constructor. First phase construction
    70  */
    71 CT_EComSessionData::CT_EComSessionData()
    72 :	iActiveNotifyOnChange(NULL)
    73 ,	iImplementationInterface(NULL)
    74 	{
    75 	iInitParams.iInteger=0;
    76 	iInitParams.iDescriptor=&iInterfaceDesc;
    77 	}
    78 
    79 /**
    80  * Second phase construction
    81  */
    82 void CT_EComSessionData::ConstructL()
    83 	{
    84 	iInterfaceDesc=KEmptyString;
    85 	iDTorKey=TUid::Uid(0);
    86 	iActiveNotifyOnChange=CActiveCallback::NewL(*this);
    87 	}
    88 
    89 /**
    90  * Public destructor
    91  */
    92 CT_EComSessionData::~CT_EComSessionData()
    93 	{
    94 	DestroyData();
    95 	iInfoArray.ResetAndDestroy();
    96 	iInfoArray.Close();
    97 	delete iActiveNotifyOnChange;
    98 	iActiveNotifyOnChange=NULL;
    99 	}
   100 
   101 TAny* CT_EComSessionData::GetObject()
   102 /**
   103  * Return a pointer to the object that the data wraps
   104  *
   105  * @return	Pointer to the object that the data wraps
   106  */
   107 	{
   108 	return &iSession;
   109 	}
   110 
   111 void CT_EComSessionData::DestroyData()
   112 	{
   113 	if (iImplementationInterface)
   114 		{
   115 		delete static_cast<CBase*>(iImplementationInterface);
   116 		iImplementationInterface=NULL;
   117 		};
   118 	}
   119 
   120 //////////////////////////////////////////////////////////////////////
   121 // Read data from INI file
   122 //////////////////////////////////////////////////////////////////////
   123 
   124 void CT_EComSessionData::GetUid(const TDesC& aSection, TUid& aUid)
   125 	{
   126 	TInt	uidValue=0;
   127 
   128 	GetHexFromConfig(aSection, KUid(), uidValue );
   129 	aUid = TUid::Uid(uidValue);
   130 	}
   131 
   132 TBool CT_EComSessionData::GetResolver(const TDesC& aSection, TPtrC& aResolver)
   133 	{
   134 	return GetStringFromConfig(aSection, KResolver(), aResolver);
   135 	}
   136 
   137 TBool CT_EComSessionData::GetResolverUid(const TDesC& aSection, TUid& aUid)
   138 	{
   139 	TInt	uidValue=0;
   140 	TBool	ret=GetHexFromConfig(aSection, KResolverUid(), uidValue );
   141 	aUid = TUid::Uid(uidValue);
   142 	return ret;
   143 	}
   144 
   145 TBool CT_EComSessionData::GetEmpty(const TDesC& aSection, TBool& aEmpty)
   146 	{
   147 	aEmpty = EFalse;
   148 	return GetBoolFromConfig(aSection, KEmpty(), aEmpty );
   149 	}
   150 
   151 TBool CT_EComSessionData::GetExpectedResult(const TDesC& aSection, TInt& aExpectedResult)
   152 	{
   153 	aExpectedResult = 0;
   154 	return GetIntFromConfig(aSection, KExpectedResult(), aExpectedResult );
   155 	}
   156 
   157 TBool CT_EComSessionData::GetOffset(const TDesC& aSection, TInt& aOffset)
   158 	{
   159 	aOffset=_FOFF(CTestEComInterface, iDtor_ID_Key);
   160 
   161 	TPtrC	offset;
   162 	TBool	ret=GetStringFromConfig(aSection, KOffset(), offset);
   163 	if ( ret )
   164 		{
   165 		if ( offset == KDefault )
   166 			{
   167 			INFO_PRINTF2(_L("Default offset %d"), aOffset);
   168 			}
   169 		else
   170 			{
   171 			ret=GetIntFromConfig(aSection, KOffset(), aOffset);
   172 			}
   173 		}
   174 
   175 	return ret;
   176 	}
   177 
   178 TBool CT_EComSessionData::GetDescriptor(const TDesC& aSection, TPtrC& aDescriptor)
   179 	{
   180 	aDescriptor.Set(KEmptyString);
   181 	return GetStringFromConfig(aSection, KDescriptor(), aDescriptor );
   182 	}
   183 
   184 TBool CT_EComSessionData::GetInteger(const TDesC& aSection, TInt& aInt)
   185 	{
   186 	return GetIntFromConfig(aSection, KInteger(), aInt );
   187 	}
   188 
   189 
   190 TBool CT_EComSessionData::DoCommandL(const TTEFFunction& aCommand, const TTEFSectionName& aSection, const TInt aAsyncErrorIndex)
   191 /**
   192  * Process a command read from the ini file
   193  *
   194  * @param aCommand			The command to process
   195  * @param aSection			The section in the ini containing data for the command
   196  * @param aAsyncErrorIndex	Command index for async calls to return errors to
   197  *
   198  * @return					ETrue if the command is processed
   199  * @pre						N/A
   200  * @post					N/A
   201  *
   202  * @leave					System wide error
   203  */
   204 	{
   205 	TBool	retVal=ETrue;
   206 
   207 	if ( aCommand==KCmdOpenL )
   208 		{
   209 		DoCmdOpenL();
   210 		}
   211 	else if ( aCommand==KCmdClose )
   212 		{
   213 		DoCmdClose();
   214 		}
   215 	else if ( aCommand==KCmdFinalClose )
   216 		{
   217 		DoCmdFinalClose();
   218 		}
   219 	else if ( aCommand==KCmdNotifyOnChange )
   220 		{
   221 		DoCmdNotifyOnChange(aAsyncErrorIndex);
   222 		}
   223 	else if ( aCommand==KCmdCancelNotifyOnChange )
   224 		{
   225 		DoCmdCancelNotifyOnChange();
   226 		}
   227 	else if ( aCommand==KCmdListImplementationsL )
   228 		{
   229 		DoCmdListImplementationsL(aSection);
   230 		}
   231 	else if ( aCommand==KCmdCreateImplementationL )
   232 		{
   233 		DoCmdCreateImplementationL(aSection);
   234 		}
   235 	else if ( aCommand==KCmdDestroyedImplementation )
   236 		{
   237 		DoCmdDestroyedImplementation(aSection);
   238 		}
   239 	else if ( aCommand==KCmdClean )
   240 		{
   241 		DestroyData();
   242 		}
   243 	else
   244 		{
   245 		retVal=EFalse;
   246 		}
   247 	return retVal;
   248 	}
   249 
   250 void CT_EComSessionData::DoCmdOpenL()
   251 	{
   252 	INFO_PRINTF1(_L("OpenL Call"));
   253 	TRAPD ( err, iSession = REComSession::OpenL() );
   254 	if ( err!=KErrNone )
   255 		{
   256 		ERR_PRINTF2(_L("OpenL Error %d"), err);
   257 		SetError(err);
   258 		}
   259 	else
   260 		{
   261 		INFO_PRINTF1(_L("Session opened"));
   262 		}
   263 	}
   264 
   265 void CT_EComSessionData::DoCmdClose()
   266 	{
   267 	INFO_PRINTF1(_L("Close"));
   268 	iSession.Close();
   269 	}
   270 
   271 void CT_EComSessionData::DoCmdFinalClose()
   272 	{
   273 	INFO_PRINTF1(_L("Final Close"));
   274 	REComSession::FinalClose();
   275 	}
   276 
   277 void CT_EComSessionData::DoCmdNotifyOnChange(const TInt aIndex)
   278 	{
   279 	INFO_PRINTF1(_L("NotifyOnChange"));
   280 	iSession.NotifyOnChange(iActiveNotifyOnChange->iStatus);
   281 	iActiveNotifyOnChange->Activate(aIndex);
   282    	IncOutstanding();
   283 	}
   284 
   285 void CT_EComSessionData::DoCmdCancelNotifyOnChange()
   286 	{
   287 	INFO_PRINTF1(_L("CancelNotifyOnChange"));
   288 	iSession.CancelNotifyOnChange(iActiveNotifyOnChange->iStatus);
   289 	}
   290 
   291 // Found defects:
   292 // 1. ListImplementationsL with impossible aResolutionParameters argument don't leave
   293 //    Status: possible defect
   294 void CT_EComSessionData::DoCmdListImplementationsL(const TDesC& aSection)
   295 	{
   296 	INFO_PRINTF1(_L("Getting Implementnation List"));
   297 
   298 	TUid	impUid;
   299 	GetUid(aSection, impUid);
   300 
   301 	TUid	resolverUid;
   302 	TBool	hasResolverUid=GetResolverUid(aSection, resolverUid);
   303 
   304 	TInt	err = KErrNone;
   305 
   306 	TPtrC	resolver;
   307 	TBool	hasResolver=GetResolver(aSection, resolver);
   308 	if ( hasResolver )
   309 		{
   310 		TEComResolverParams*	resolverParams=static_cast<TEComResolverParams*>(GetDataObjectL(resolver));
   311 		if ( hasResolverUid )
   312 			{
   313 			INFO_PRINTF1(_L("Getting Implementnation List(3) by Uid, ResolverParams as a filter params && Resolver Uid as a filter"));
   314 			TRAP(err, REComSession::ListImplementationsL(impUid, *resolverParams, resolverUid, iInfoArray));
   315 			}
   316 		else
   317 			{
   318 			INFO_PRINTF1(_L("Getting Implementnation List(1) by Uid && ResolverParams as a filter"));
   319 			TRAP(err, REComSession::ListImplementationsL(impUid, *resolverParams, iInfoArray));
   320 			}
   321 		}
   322 	else
   323 		{
   324 		INFO_PRINTF1(_L("Getting Implementnation List(2) by Uid"));
   325 		TRAP(err, REComSession::ListImplementationsL(impUid, iInfoArray));
   326 		}
   327 
   328 	if ( err==KErrNone )
   329 		{
   330 		//	Check to see if we the data object has to be set in a CT_ImplementationInformationData
   331 		TPtrC	saveTo;
   332 		if ( GetStringFromConfig(aSection, KSaveTo(), saveTo) )
   333 			{
   334 			TInt	index=0;
   335 			GetIntFromConfig(aSection, KSaveIndex(), index);
   336 			CDataWrapper*	wrapper=static_cast<CDataWrapper*>(GetDataWrapperL(saveTo));
   337 			wrapper->SetObjectL(iInfoArray[index]);
   338 			}
   339 		}
   340 
   341 	if( err != KErrNone )
   342 		{
   343 		ERR_PRINTF2(_L("Failed to get implementation list with error %d"), err);
   344 		SetError(err);
   345 		}
   346 	else
   347 		{
   348 		TInt	expectedResult;
   349 		if ( GetExpectedResult(aSection, expectedResult) )
   350 			{
   351 			if (iInfoArray.Count() != expectedResult)
   352 				{
   353 				ERR_PRINTF3(_L("List retrieved. Found %d imlementations, expected %d"),
   354 				iInfoArray.Count(), expectedResult );
   355 				SetBlockResult(EFail);
   356 				}
   357 			}
   358 
   359 		for (TInt i = 0; i < iInfoArray.Count(); i++)
   360 			{
   361 			const TDesC8&	name8=iInfoArray[i]->DataType();
   362 			TBuf<KMaxTestExecuteCommandLength>	name16;
   363 			name16.Copy(name8);
   364 			INFO_PRINTF3(_L("Implementation UID %d: - \"%S\""), iInfoArray[i]->ImplementationUid().iUid, &(iInfoArray[i]->DisplayName()));
   365 			INFO_PRINTF2(_L("Data Type is \"%S\""), &name16);
   366 			};
   367 		}
   368 	}
   369 
   370 // Found defects:
   371 // 1. CreateImplementationL with any aKeyOffset argument - aKeyOffset can have any value (-1, 0, 10000, ...)
   372 //    but CreateImplementationL returns no error and generates memory leak.
   373 //    Status: possible defects
   374 // 2. CreateImplementationL with impossible aResolutionParameters argument - returns KErrnotFound, but in documentation
   375 //    CreateImplementationL levaes only with KErrNoMemory, KErrNotConnected, KErrArgument or KErrPermissionDenied error
   376 //    Status: documentation  defect
   377 void CT_EComSessionData::DoCmdCreateImplementationL(const TDesC& aSection)
   378 	{
   379 	DestroyData();
   380 
   381 	TUid	impUid;
   382 	GetUid(aSection,impUid);
   383 
   384 	TInt	offset;
   385 	TBool	hasOffset=GetOffset(aSection,offset);
   386 
   387 	TUid	resolverUid;
   388 	TBool	hasResolverUid=GetResolverUid(aSection,	resolverUid);
   389 
   390 	TBool	hasIntegerValue=GetInteger(aSection, iInitParams.iInteger);
   391 
   392 	TBool	emptyArgs;
   393 	TBool	hasEmptyArgs=GetEmpty(aSection, emptyArgs);
   394 
   395 	TBool	hasInitParams=hasIntegerValue || hasEmptyArgs;
   396 	TAny*	initParamsPtr=NULL;
   397 	if (hasInitParams)
   398 		{
   399 		if (!emptyArgs)
   400 			{
   401 			initParamsPtr=&iInitParams;
   402 			}
   403 		}
   404 
   405 	TInt	err = KErrNone;
   406 	TBool	getKeyFromOffset=EFalse;
   407 
   408 	TPtrC	resolver;
   409 	if ( GetResolver(aSection, resolver) )
   410 		{
   411 		TEComResolverParams*	resolverParams=static_cast<TEComResolverParams*>(GetDataObjectL(resolver));
   412 		if (hasResolverUid && hasInitParams && hasOffset)
   413 			{
   414 			INFO_PRINTF1(_L("Creating Implementnation(12) by Interface Uid,  Offset, Init Params, ResolverParams as a filter params && Resolver Uid as a filter"));
   415 		
   416 			TRAP( err, iImplementationInterface =
   417 				REComSession::CreateImplementationL(impUid, offset, initParamsPtr, *resolverParams, resolverUid));
   418 			getKeyFromOffset=(err==KErrNone);
   419 			}
   420 		else if (hasResolverUid && hasInitParams)
   421 			{
   422 			INFO_PRINTF1(_L("Creating Implementnation(11) by Interface Uid,  Init Params, ResolverParams as a filter params && Resolver Uid as a filter"));
   423 			TRAP( err, iImplementationInterface =
   424 				REComSession::CreateImplementationL(impUid, iDTorKey, initParamsPtr, *resolverParams, resolverUid));
   425 			}
   426 		else if (hasResolverUid && hasOffset)
   427 			{
   428 			INFO_PRINTF1(_L("Creating Implementnation(10) by Interface Uid, Offset, ResolverParams as a filter params && Resolver Uid as a filter"));
   429 			TRAP( err, iImplementationInterface =
   430 				REComSession::CreateImplementationL(impUid, offset, *resolverParams, resolverUid));
   431 			getKeyFromOffset=(err==KErrNone);
   432 			}
   433 		else if (hasResolverUid)
   434 			{
   435 			INFO_PRINTF1(_L("Creating Implementnation(9) by Interface Uid, ResolverParams as a filter params && Resolver Uid as a filter"));
   436 			TRAP( err, iImplementationInterface =
   437 				REComSession::CreateImplementationL(impUid, iDTorKey, *resolverParams, resolverUid));
   438 			}
   439 		else if (hasInitParams && hasOffset)
   440 			{
   441 			INFO_PRINTF1(_L("Creating Implementnation(8) by Interface Uid, Offset, Init Params && Resolver Parameters as a filter"));
   442 			TRAP( err, iImplementationInterface =
   443 				REComSession::CreateImplementationL(impUid, offset, initParamsPtr, *resolverParams));
   444 
   445 			getKeyFromOffset=(err==KErrNone);
   446 			}
   447 		else if (hasInitParams)
   448 			{
   449 			INFO_PRINTF1(_L("Creating Implementnation(7) by Interface Uid, Init Params && Resolver Parameters as a filter"));
   450 			TRAP( err, iImplementationInterface =
   451 				REComSession::CreateImplementationL(impUid, iDTorKey, initParamsPtr, *resolverParams));
   452 			}
   453 		else if (hasOffset)
   454 			{
   455 			INFO_PRINTF1(_L("Creating Implementnation(6) by Interface Uid, Offset && Resolver Parameters as a filter"));
   456 
   457 			TRAP( err, iImplementationInterface =
   458 				REComSession::CreateImplementationL(impUid, offset, *resolverParams));
   459 
   460 			getKeyFromOffset=(err==KErrNone);
   461 			}
   462 		else
   463 			{
   464 			INFO_PRINTF1(_L("Creating Implementnation(5) by Interface Uid && Resolver Parameters as a filter"));
   465 
   466 			TRAP( err, iImplementationInterface =
   467 				REComSession::CreateImplementationL(impUid, iDTorKey, *resolverParams));
   468 
   469 			}
   470 		}
   471 	else
   472 		{
   473 		if (hasInitParams && hasOffset)
   474 			{
   475 			INFO_PRINTF1(_L("Creating Implementnation(4) by Uid, Offset && Construction Parameters"));
   476 
   477 			TRAP( err, iImplementationInterface =
   478 				REComSession::CreateImplementationL(impUid, offset, initParamsPtr));
   479 
   480 			getKeyFromOffset=(err==KErrNone);
   481 			}
   482 		else if (hasInitParams)
   483 			{
   484 			INFO_PRINTF1(_L("Creating Implementnation(3) by Uid && Construction Parameters"));
   485 
   486 			TRAP( err, iImplementationInterface =
   487 				REComSession::CreateImplementationL(impUid, iDTorKey, initParamsPtr));
   488 
   489 			}
   490 		else if (hasOffset)
   491 			{
   492 			INFO_PRINTF1(_L("Creating Implementnation(2) by Uid && offset defined by interface"));
   493 
   494 			TRAP( err, iImplementationInterface =
   495 				REComSession::CreateImplementationL(impUid, offset));
   496 
   497 			getKeyFromOffset=(err==KErrNone);
   498 			}
   499 		else
   500 			{
   501 			INFO_PRINTF1(_L("Creating Implementnation(1) by Uid"));
   502 
   503 			TRAP( err, iImplementationInterface =
   504 				REComSession::CreateImplementationL(impUid, iDTorKey ));
   505 
   506 			}
   507 		}
   508 
   509 	if ( getKeyFromOffset )
   510 		{
   511 		TUint8*	uidPtr=REINTERPRET_CAST(TUint8*, iImplementationInterface) + offset;
   512 		TUid*	uid=REINTERPRET_CAST(TUid*, uidPtr);
   513 		iDTorKey=*uid;
   514 		*uid=TUid::Null();
   515 		}
   516 
   517 	INFO_PRINTF2(_L("Implementation created successfully. iDTorKey is %d"), iDTorKey.iUid);
   518 	if (BlockResult()==EPass)
   519 		{
   520 		if( err != KErrNone )
   521 			{
   522 			ERR_PRINTF2(_L("Failed to create implementation with error %d"), err);
   523 			SetError(err);
   524 			if (iImplementationInterface != NULL)
   525 				{
   526 				SetBlockResult(EFail);
   527 				ERR_PRINTF1(_L("Data verification failed: CreateImplementationL returns an error, but implementation was created"));
   528 				}
   529 			else
   530 				{
   531 				INFO_PRINTF1(_L("Data verification succcess: CreateImplementationL returns an error and implementation was not created as expected"));
   532 				}
   533 			}
   534 		}
   535 	}
   536 
   537 void CT_EComSessionData::DoCmdDestroyedImplementation(const TDesC& aSection)
   538 	{
   539 	TInt	uidValue;
   540 	if ( GetIntFromConfig(aSection, KUid(), uidValue) )
   541 		{
   542 		iDTorKey = TUid::Uid(uidValue);
   543 		}
   544 
   545 	INFO_PRINTF2(_L("Destroy Interface Implementation. UID is %d"), iDTorKey.iUid);
   546 	REComSession::DestroyedImplementation(iDTorKey);
   547 	INFO_PRINTF1(_L("REComSession::DestroyedImplementation(iDTorKey) completed"));
   548 	DestroyData();
   549 	INFO_PRINTF1(_L("Interface implementation destroyed"));
   550 	}
   551 
   552 /**
   553  Virtual RunL - Called on completion of an asynchronous command
   554  @internalComponent
   555  @see MTPActiveCallback
   556  @param aActive Active Object that RunL has been called on
   557  @pre N/A
   558  @post N/A
   559  @leave system wide error code
   560 */
   561 void CT_EComSessionData::RunL(CActive* aActive, const TInt aIndex)
   562 	{
   563 	if ( aActive==iActiveNotifyOnChange )
   564 		{
   565 		RunNotifyOnChangeL( aIndex );
   566 		}
   567 	else
   568 		{
   569 		ERR_PRINTF1(_L("Stray signal"));
   570 		SetBlockResult(EFail);
   571 		}
   572 	}
   573 
   574 /**
   575  RunNotifyOnChangeL - Process RunL for iActiveNotifyOnChange
   576  @internalComponent
   577  @pre N/A
   578  @post N/A
   579  @leave system wide error code
   580 */
   581 void CT_EComSessionData::RunNotifyOnChangeL(const TInt aIndex)
   582 	{
   583 	TInt	err=iActiveNotifyOnChange->iStatus.Int();
   584 	if ( err!=KErrNone )
   585 		{
   586 		ERR_PRINTF2(_L("RunL Error %d"), err);
   587 		SetAsyncError(aIndex, err);
   588 		}
   589 	else
   590 		{
   591 		INFO_PRINTF1(_L("RunNotifyOnChangeL OK"));
   592 		}
   593 
   594    	DecOutstanding();
   595 	}
   596