kerneltest/e32test/resourceman/d_rescontrolcli.cpp
changeset 0 a41df078684a
child 31 56f325a607ea
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2007-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 the License "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 // e32test\resourceman\d_rescontrol_cli.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <kernel/kern_priv.h>
       
    19 #include <drivers/resource_extend.h>
       
    20 #include <drivers/resourceman.h>
       
    21 #include "d_rescontrolcli.h"
       
    22 #ifdef PRM_ENABLE_EXTENDED_VERSION
       
    23 #include "dynamicresource.h"
       
    24 #endif
       
    25 #include "./resourceman_psl/rescontrol_psl.h"
       
    26 
       
    27 #ifndef PRM_ENABLE_EXTENDED_VERSION
       
    28 _LIT(KTestPowerRCName, "D_RESCONTROLCLI.LDD");
       
    29 #else
       
    30 _LIT(KTestPowerRCName, "D_EXTENDEDRESCONTROLCLI.LDD");
       
    31 #endif
       
    32 
       
    33 const TInt KTestResManLddThreadPriority  = 0x5;
       
    34 _LIT(KTestResManLddThread, "TestResManLddThread");
       
    35 
       
    36 #ifdef PRM_ENABLE_EXTENDED_VERSION
       
    37 #define EXPECTED_POST_NOTI_COUNT 6 //Expected number of notifications as a result of post boot level setting. 
       
    38 #else
       
    39 #define EXPECTED_POST_NOTI_COUNT 5
       
    40 #endif
       
    41 #define MAX_DYNAMIC_RES_NUM 5
       
    42 /** 
       
    43 Structure for holding resource information
       
    44 */
       
    45 struct SClientInfo
       
    46 	{
       
    47 	HBuf8* pName;
       
    48 	TUint iClientId;
       
    49 	};
       
    50 /**
       
    51 The logical device (factory class) for the resource manager client side test.
       
    52 */
       
    53 class DTestResManLddFactory : public DLogicalDevice
       
    54 	{
       
    55 public:
       
    56 	DTestResManLddFactory();
       
    57 	~DTestResManLddFactory();
       
    58 	virtual TInt Install();
       
    59 	virtual void GetCaps(TDes8 &aDes) const;
       
    60 	virtual TInt Create(DLogicalChannelBase*& aChannel);
       
    61 	static void PostBootNotificationFunc(TUint aClientId, TUint aResId, TInt aLevel, TInt aLevelOwnerId, TInt aResult, TAny* aParam);
       
    62 	void SetPostBootLevelAndRequestNotification(TUint aClientId, TUint aResId, TInt aPostBootLevel);
       
    63 	TDynamicDfcQue* iDfcQ;
       
    64 	DStaticPowerResource *iStaticRes;
       
    65 	DStaticPowerResource *iStaticResArray[3];
       
    66 	static SClientInfo iClient; //Need to be static to access them in PostBootNotificationFunc (callback function).
       
    67 	static TUint iPostBootNotiCount;
       
    68 	};
       
    69 
       
    70 /** Logical channel class for Resource manager test LDD */
       
    71 class DTestResManLdd : public DLogicalChannel
       
    72 	{
       
    73 public:
       
    74 	DTestResManLdd();
       
    75 	virtual ~DTestResManLdd();
       
    76 	// Inherited from DLogicalChannel
       
    77 	virtual TInt DoCreate(TInt aUnit, const TDesC8* anInfo, const TVersion& aVer);
       
    78 	virtual void HandleMsg(TMessageBase* aMsg);
       
    79 private:
       
    80 	TInt DoControl(TInt aFunction, TAny* a1, TAny* a2);
       
    81 	TInt DoRequest(TInt aFunction, TRequestStatus* aStatus, TAny* a1, TAny* a2);
       
    82 	TInt DoCancel(TUint aMask);
       
    83 	static void CallbackFunc(TUint aClientId, TUint aResId, TInt aLevel, TInt aLevelOwnerId, TInt aResult, TAny* aParam);
       
    84 	static void CondNotificationFunc(TUint aClientId, TUint aResId, TInt aLevel, TInt aLevelOwnerId, TInt aResult, TAny* aParam);
       
    85 	static void UnCondNotificationFunc(TUint aClientId, TUint aResId, TInt aLevel, TInt aLevelOwnerId, TInt aResult, TAny* aParam);
       
    86 private:
       
    87    	DThread* iClientThreadPtr;
       
    88    	TPowerResourceCb iAsyncResourceCallback; //Callback object for handling long latency resources.
       
    89 	//Structure for storing the notification information
       
    90 	struct SNotificationList
       
    91 		{
       
    92 		TUint iClientId;
       
    93 		TUint iResourceId;
       
    94 		TUint iCount;
       
    95 		DPowerResourceNotification *iNoti;
       
    96 		SNotificationList *iNext;
       
    97 		};
       
    98 	SNotificationList *iCondList; //List for maintaining conditional notification
       
    99 	SNotificationList *iUnCondList; //List for maintaining unconditional notification
       
   100 	TRequestStatus *iStatus; //Store the status and complete on callback function.
       
   101 	TBool iCallbackCancel; 
       
   102 	TInt iClientId; //ID of the client that requested long latency operation, to compare in callback and notifications.
       
   103 	TUint iResourceId; //Id of the resource of long latency operation, to compare in callback and notifications.
       
   104 	//Below 2 variables are used only for asynchronous get resource state operation
       
   105 	TInt *iStatePtr; //Pointer to hold the address of state variable, where state is updated in callback function.
       
   106 	TInt *iLevelOwnerIdPtr; //Pointer to hold the address of level owner Id variable, where owner Id is updated in callback function.
       
   107 	HBuf *pBuf; //Buffer for testing caching of resource information for Idle power management 
       
   108 	SClientInfo clientInfo[MAX_CLIENTS];
       
   109 #ifdef PRM_ENABLE_EXTENDED_VERSION
       
   110 	TUint iTestParallelResourceId; //Variable to hold the long latency resource id, this is used only in test parallel execution of DFC's
       
   111 	static void TestParallelExecutionCallback(TUint aClientId, TUint aResId, TInt aLevel, TInt aLevelOwnerId, TInt aResult, TAny* aParam);
       
   112 	TBool iCallbackReceived;
       
   113 	TBool iValidateCallbackReceived;
       
   114 	TPowerResourceCb iAsyncTestParallelCallback; //Callback object for handling long latency resources change state while deregistering non-dependency resource.
       
   115 	RArray <DDynamicPowerResource*> iDynamicArray; //Array to hold dynamic resources. This includes dynamic dependent resource
       
   116 #endif
       
   117 	};
       
   118 
       
   119 //class definition for multilevel single instantaneous positive resource
       
   120 class DLaterRegisterStaticResource : public DStaticPowerResource
       
   121 	{
       
   122 public:
       
   123 	DLaterRegisterStaticResource();
       
   124 	TInt DoRequest(TPowerRequest &req);
       
   125 	TInt GetInfo(TDes8* aInfo) const;
       
   126 private:
       
   127 	TInt iMinLevel;
       
   128 	TInt iMaxLevel;
       
   129 	TInt iCurrentLevel;
       
   130 	};
       
   131 
       
   132 //Constructors of the resource
       
   133 _LIT(KLaterRegisterStaticResource, "LaterRegisterStaticResource");
       
   134 DLaterRegisterStaticResource::DLaterRegisterStaticResource() : DStaticPowerResource(KLaterRegisterStaticResource, E_OFF), iMinLevel(0), iMaxLevel(1), iCurrentLevel(0)
       
   135 	{
       
   136 	iFlags = KBinary;
       
   137 	}
       
   138 
       
   139 TInt DLaterRegisterStaticResource::DoRequest(TPowerRequest& req)
       
   140 	{
       
   141 	Kern::Printf("DLaterRegisterStaticResource::DoRequest\n");
       
   142 	if(req.ReqType() == TPowerRequest::EGet)
       
   143 		{
       
   144 		req.Level() = iCurrentLevel;
       
   145 		}
       
   146 	else if(req.ReqType() == TPowerRequest::EChange)
       
   147 		{
       
   148 		iCurrentLevel = req.Level();
       
   149 		}
       
   150 	else if(req.ReqType() == TPowerRequest::ESetDefaultLevel)
       
   151 		{
       
   152 		req.Level() = iDefaultLevel;
       
   153 		iCurrentLevel = iDefaultLevel;
       
   154 		}
       
   155 	else
       
   156 		return KErrNotSupported;
       
   157 	return KErrNone;
       
   158 	}
       
   159 
       
   160 TInt DLaterRegisterStaticResource::GetInfo(TDes8* info) const
       
   161 	{
       
   162 	DStaticPowerResource::GetInfo((TDes8*)info);
       
   163 	TPowerResourceInfoV01 *buf1 = (TPowerResourceInfoV01*)info;
       
   164 	buf1->iDefaultLevel = iDefaultLevel;
       
   165 	buf1->iMinLevel = iMinLevel;
       
   166 	buf1->iMaxLevel = iMaxLevel;
       
   167 	return KErrNone;
       
   168 	}
       
   169 
       
   170 //class definition for multilevel single instantaneous positive resource
       
   171 class DLaterRegisterStaticResource1 : public DStaticPowerResource
       
   172 	{
       
   173 public:
       
   174 	DLaterRegisterStaticResource1();
       
   175 	TInt DoRequest(TPowerRequest &req);
       
   176 	TInt GetInfo(TDes8* aInfo) const;
       
   177 private:
       
   178 	TInt iMinLevel;
       
   179 	TInt iMaxLevel;
       
   180 	TInt iCurrentLevel;
       
   181 	};
       
   182 
       
   183 //Constructors of the resource
       
   184 _LIT(KLaterRegisterStaticResource1, "LaterRegisterStaticResource1");
       
   185 DLaterRegisterStaticResource1::DLaterRegisterStaticResource1() : DStaticPowerResource(KLaterRegisterStaticResource1, E_OFF), iMinLevel(0), iMaxLevel(1), iCurrentLevel(0)
       
   186 	{
       
   187 	iFlags = KBinary;
       
   188 	}
       
   189 
       
   190 TInt DLaterRegisterStaticResource1::DoRequest(TPowerRequest& req)
       
   191 	{
       
   192 	Kern::Printf("DLaterRegisterStaticResource1::DoRequest\n");
       
   193 	if(req.ReqType() == TPowerRequest::EGet)
       
   194 		{
       
   195 		req.Level() = iCurrentLevel;
       
   196 		}
       
   197 	else if(req.ReqType() == TPowerRequest::EChange)
       
   198 		{
       
   199 		iCurrentLevel = req.Level();
       
   200 		}
       
   201 	else if(req.ReqType() == TPowerRequest::ESetDefaultLevel)
       
   202 		{
       
   203 		req.Level() = iDefaultLevel;
       
   204 		iCurrentLevel = iDefaultLevel;
       
   205 		}
       
   206 	else
       
   207 		return KErrNotSupported;
       
   208 	return KErrNone;
       
   209 	}
       
   210 
       
   211 TInt DLaterRegisterStaticResource1::GetInfo(TDes8* info) const
       
   212 	{
       
   213 	DStaticPowerResource::GetInfo((TDes8*)info);
       
   214 	TPowerResourceInfoV01 *buf1 = (TPowerResourceInfoV01*)info;
       
   215 	buf1->iDefaultLevel = iDefaultLevel;
       
   216 	buf1->iMinLevel = iMinLevel;
       
   217 	buf1->iMaxLevel = iMaxLevel;
       
   218 	return KErrNone;
       
   219 	}
       
   220 
       
   221 //class definition for multilevel single instantaneous positive resource
       
   222 class DLaterRegisterStaticResource2 : public DStaticPowerResource
       
   223 	{
       
   224 public:
       
   225 	DLaterRegisterStaticResource2();
       
   226 	TInt DoRequest(TPowerRequest &req);
       
   227 	TInt GetInfo(TDes8* aInfo) const;
       
   228 private:
       
   229 	TInt iMinLevel;
       
   230 	TInt iMaxLevel;
       
   231 	TInt iCurrentLevel;
       
   232 	};
       
   233 
       
   234 //Constructors of the resource
       
   235 _LIT(KLaterRegisterStaticResource2, "LaterRegisterStaticResource2");
       
   236 DLaterRegisterStaticResource2::DLaterRegisterStaticResource2() : DStaticPowerResource(KLaterRegisterStaticResource2, E_OFF), iMinLevel(0), iMaxLevel(1), iCurrentLevel(0)
       
   237 	{
       
   238 	iFlags = KBinary;
       
   239 	}
       
   240 
       
   241 TInt DLaterRegisterStaticResource2::DoRequest(TPowerRequest& req)
       
   242 	{
       
   243 	Kern::Printf("DLaterRegisterStaticResource2::DoRequest\n");
       
   244 	if(req.ReqType() == TPowerRequest::EGet)
       
   245 		{
       
   246 		req.Level() = iCurrentLevel;
       
   247 		}
       
   248 	else if(req.ReqType() == TPowerRequest::EChange)
       
   249 		{
       
   250 		iCurrentLevel = req.Level();
       
   251 		}
       
   252 	else if(req.ReqType() == TPowerRequest::ESetDefaultLevel)
       
   253 		{
       
   254 		req.Level() = iDefaultLevel;
       
   255 		iCurrentLevel = iDefaultLevel;
       
   256 		}
       
   257 	else
       
   258 		return KErrNotSupported;
       
   259 	return KErrNone;
       
   260 	}
       
   261 
       
   262 TInt DLaterRegisterStaticResource2::GetInfo(TDes8* info) const
       
   263 	{
       
   264 	DStaticPowerResource::GetInfo((TDes8*)info);
       
   265 	TPowerResourceInfoV01 *buf1 = (TPowerResourceInfoV01*)info;
       
   266 	buf1->iDefaultLevel = iDefaultLevel;
       
   267 	buf1->iMinLevel = iMinLevel;
       
   268 	buf1->iMaxLevel = iMaxLevel;
       
   269 	return KErrNone;
       
   270 	}
       
   271 
       
   272 TUint DTestResManLddFactory::iPostBootNotiCount = 0;
       
   273 SClientInfo DTestResManLddFactory::iClient = {NULL, 0};
       
   274 DTestResManLddFactory::DTestResManLddFactory()
       
   275 	{
       
   276 	iParseMask=0; // Allow info and pdd, but not units
       
   277 	iUnitsMask=0;
       
   278 	// Set version number for this device
       
   279 	iVersion=RTestResMan::VersionRequired();
       
   280 	}
       
   281 
       
   282 DTestResManLddFactory::~DTestResManLddFactory()
       
   283 	{
       
   284 	if(iDfcQ)
       
   285 	  iDfcQ->Destroy();
       
   286 	if(iStaticRes)
       
   287 		delete iStaticRes;
       
   288 	if(iStaticResArray[0])
       
   289 		delete iStaticResArray[0];
       
   290 	if(iStaticResArray[2])
       
   291 		delete iStaticResArray[2];
       
   292 	}
       
   293 
       
   294 /** Entry point for this driver */
       
   295 DECLARE_STANDARD_LDD()
       
   296 	{
       
   297 	DTestResManLddFactory* p = new DTestResManLddFactory;
       
   298 	if(!p)
       
   299 		return NULL;
       
   300 	TInt r = Kern::DynamicDfcQCreate(p->iDfcQ, KTestResManLddThreadPriority, KTestResManLddThread);
       
   301 	if(r != KErrNone)
       
   302 		{
       
   303 		Kern::Printf("Memory not allocated");
       
   304 		p->AsyncDelete();
       
   305 		return NULL;
       
   306 		}
       
   307 	//Register client with Resource Controller
       
   308 	TBuf8<32> ClientName(_L8("Client"));
       
   309 	p->iClient.pName = HBuf::New((const TDesC&)ClientName);
       
   310 	if(!p->iClient.pName)
       
   311 		{
       
   312 		p->iDfcQ->Destroy();
       
   313 		p->AsyncDelete();
       
   314 		return NULL;
       
   315 		}
       
   316 	//Allocating memory earlier so that during failure conditions can cleanup easily
       
   317 	p->iStaticRes = new DLaterRegisterStaticResource();
       
   318 	if(!p->iStaticRes)
       
   319 		{
       
   320 		delete p->iClient.pName;
       
   321 		p->iDfcQ->Destroy();
       
   322 		p->AsyncDelete();
       
   323 		return NULL;
       
   324 		}
       
   325 	p->iStaticResArray[0] = new DLaterRegisterStaticResource1();
       
   326 	if(!p->iStaticResArray[0])
       
   327 		{
       
   328 		delete p->iStaticRes;
       
   329 		delete p->iClient.pName;
       
   330 		p->iDfcQ->Destroy();
       
   331 		p->AsyncDelete();
       
   332 		return NULL;
       
   333 		}	
       
   334 	p->iStaticResArray[2] = new DLaterRegisterStaticResource2();
       
   335 	if(!p->iStaticResArray[2])
       
   336 		{
       
   337 		delete p->iStaticRes;
       
   338 		delete p->iStaticResArray[0];
       
   339 		delete p->iClient.pName;
       
   340 		p->iDfcQ->Destroy();
       
   341 		p->AsyncDelete();
       
   342 		return NULL;
       
   343 		}
       
   344 	r = PowerResourceManager::RegisterClient(DTestResManLddFactory::iClient.iClientId, (const TDesC&)*DTestResManLddFactory::iClient.pName);
       
   345 	if(r != KErrNone)
       
   346 		{
       
   347 		Kern::Printf("RegisterClient Failed\n");
       
   348 		Kern::Fault("PRM REGISTER CLIENT FAILED", __LINE__);
       
   349 		}
       
   350 	//Set postbootlevel for these resources.
       
   351 	p->SetPostBootLevelAndRequestNotification(DTestResManLddFactory::iClient.iClientId, 1, 0);
       
   352 	p->SetPostBootLevelAndRequestNotification(DTestResManLddFactory::iClient.iClientId, 6, 12);
       
   353 	p->SetPostBootLevelAndRequestNotification(DTestResManLddFactory::iClient.iClientId, 7, 1);
       
   354 	p->SetPostBootLevelAndRequestNotification(DTestResManLddFactory::iClient.iClientId, 12, 10);
       
   355 	p->SetPostBootLevelAndRequestNotification(DTestResManLddFactory::iClient.iClientId, 15, 0);
       
   356 #ifdef PRM_ENABLE_EXTENDED_VERSION
       
   357 	p->SetPostBootLevelAndRequestNotification(DTestResManLddFactory::iClient.iClientId, 65537, -50);
       
   358 #endif
       
   359 	//Test the later registration of static resources.
       
   360 	r = DPowerResourceController::RegisterStaticResource(DTestResManLddFactory::iClient.iClientId, p->iStaticRes);
       
   361 	if(r != KErrNone)
       
   362 		Kern::Fault("PRM REGISTER STATIC RESOURCE FAILED", __LINE__);
       
   363 	DStaticPowerResource **resPtr = &p->iStaticResArray[0];
       
   364 	r = DPowerResourceController::RegisterArrayOfStaticResources(DTestResManLddFactory::iClient.iClientId, resPtr, 3);
       
   365 	if(r != KErrNone)
       
   366 		Kern::Fault("PRM REGISTER STATIC RESOURCE FAILED", __LINE__);
       
   367 	r = DSimulatedPowerResourceController::CompleteResourceControllerInitialisation();
       
   368 	if(r != KErrNone)
       
   369 		Kern::Fault("PRM INIT FAILED", __LINE__);
       
   370 	return p;
       
   371 	}
       
   372 
       
   373 void DTestResManLddFactory::SetPostBootLevelAndRequestNotification(TUint aClientId, TUint aResId, TInt aPostBootLevel)
       
   374 	{
       
   375 	DPowerResourceController::PostBootLevel(aResId, aPostBootLevel);
       
   376 	DPowerResourceNotification* iNoti = new DPowerResourceNotification(PostBootNotificationFunc, (TAny*)NULL, Kern::DfcQue0(), 3);
       
   377 	if(!iNoti)
       
   378 		{
       
   379 		Kern::Fault("PRM NOTI FAILED", __LINE__);
       
   380 		}
       
   381 	//Passing the address of the object so that it could be deleted in callback function
       
   382 	new (iNoti) DPowerResourceNotification(PostBootNotificationFunc, iNoti, Kern::DfcQue0(), 3);
       
   383 	TInt r = PowerResourceManager::RequestNotification(aClientId, aResId, *iNoti);
       
   384 	if(r != KErrNone)
       
   385 		Kern::Fault("PRM REQ NOTI FAILED", __LINE__);
       
   386 	}
       
   387 
       
   388 
       
   389 /** Second stage constuctor */
       
   390 TInt DTestResManLddFactory::Install()
       
   391 	{
       
   392    	return(SetName(&KTestPowerRCName));
       
   393 	}
       
   394 
       
   395 /** Device capabilities */
       
   396 void DTestResManLddFactory::GetCaps(TDes8& aDes)const
       
   397 	{
       
   398 	// Create a capabilities object
       
   399 	RTestResMan::TCaps caps;
       
   400 	caps.iVersion = iVersion;
       
   401 	// Write it back to user memory
       
   402 	Kern::InfoCopy(aDes,(TUint8*)&caps,sizeof(caps));
       
   403 	}
       
   404 
       
   405 /** Create logical channel, only open of one channel is allowed */
       
   406 TInt DTestResManLddFactory::Create(DLogicalChannelBase*& aChannel)
       
   407 	{
       
   408    	if (iOpenChannels != 0) //A Channel is already open
       
   409 		return KErrInUse;
       
   410 	//Deregister the client registered in ldd init. 
       
   411 	TInt r = PowerResourceManager::DeRegisterClient(DTestResManLddFactory::iClient.iClientId);
       
   412 	if(r != KErrNone)
       
   413 		Kern::Fault("PRM CLIENT DEREGISTER FAILED", __LINE__);
       
   414 	delete DTestResManLddFactory::iClient.pName;
       
   415 	DTestResManLddFactory::iClient.pName = NULL;
       
   416 	DTestResManLddFactory::iClient.iClientId = 0;
       
   417 	aChannel = new DTestResManLdd;
       
   418 	if(!aChannel)
       
   419 		return KErrNoMemory;
       
   420 	return KErrNone;
       
   421 	}
       
   422 
       
   423 /** Constructor */
       
   424 DTestResManLdd::DTestResManLdd(): iAsyncResourceCallback(CallbackFunc, this, 3)
       
   425 #ifdef PRM_ENABLE_EXTENDED_VERSION
       
   426 				, iAsyncTestParallelCallback(TestParallelExecutionCallback, this, 3)
       
   427 #endif
       
   428 	{
       
   429 	iCondList = NULL;
       
   430 	iUnCondList = NULL;
       
   431 	iCallbackCancel = EFalse;
       
   432 	iClientId = 0;
       
   433 	iResourceId = 0;
       
   434 	iStatePtr = NULL;
       
   435 	iLevelOwnerIdPtr = NULL;
       
   436 	for(TUint c = 0; c < MAX_CLIENTS; c++)
       
   437 		{
       
   438 		clientInfo[c].iClientId = 0;
       
   439 		clientInfo[c].pName = NULL;
       
   440 		}
       
   441 	iClientThreadPtr=&Kern::CurrentThread();
       
   442 	// Increase the DThread's ref count so that it does not close without us
       
   443 	((DObject*)iClientThreadPtr)->Open();;
       
   444 	}
       
   445 
       
   446 /** Destructor */
       
   447 DTestResManLdd::~DTestResManLdd()
       
   448 	{
       
   449 	if(pBuf) //Buffer created for storing idle resource information
       
   450 		delete pBuf;
       
   451 #ifdef PRM_ENABLE_EXTENDED_VERSION
       
   452 	for(TInt c = 0; c < iDynamicArray.Count(); c++)
       
   453 		{
       
   454 		delete iDynamicArray[c]; //Delete the dynamic array. This includes dynamic dependent resource.
       
   455 		}
       
   456 	iDynamicArray.Close();
       
   457 #endif
       
   458 	// Close our reference on the client thread
       
   459 	Kern::SafeClose((DObject*&)iClientThreadPtr,NULL);
       
   460 	}
       
   461 
       
   462 /** Second stage constructor. */
       
   463 TInt DTestResManLdd::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& aVer)
       
   464 	{
       
   465    	// Check version
       
   466 	if (!Kern::QueryVersionSupported(RTestResMan::VersionRequired(),aVer))
       
   467 		return KErrNotSupported;
       
   468 	SetDfcQ(((DTestResManLddFactory*)iDevice)->iDfcQ);
       
   469 	iAsyncResourceCallback.SetDfcQ(iDfcQ);
       
   470 #ifdef PRM_ENABLE_EXTENDED_VERSION
       
   471 	iAsyncTestParallelCallback.SetDfcQ(iDfcQ);
       
   472 #endif
       
   473  	iMsgQ.Receive();
       
   474 	return KErrNone;
       
   475 	}
       
   476 
       
   477 /** Process a message for this logical channel */
       
   478 void DTestResManLdd::HandleMsg(TMessageBase* aMsg)
       
   479 	{
       
   480 	TThreadMessage& m=*(TThreadMessage*)aMsg;
       
   481 	TInt id=m.iValue;
       
   482 
       
   483 	if (id==(TInt)ECloseMsg)
       
   484 		{
       
   485 		// Channel close.
       
   486 		m.Complete(KErrNone,EFalse);
       
   487 		return;
       
   488 		}
       
   489 	else if (id==KMaxTInt)
       
   490 		{
       
   491 		// DoCancel
       
   492 		m.Complete(KErrNone,ETrue);
       
   493 		return;
       
   494 		}
       
   495 	else if (id<0)
       
   496 		{
       
   497 		// DoRequest
       
   498 		TRequestStatus* pS=(TRequestStatus*)m.Ptr0();
       
   499 		TInt r=DoRequest(~id,pS,m.Ptr1(),m.Ptr2());
       
   500 		if (r!=KErrNone)
       
   501 			Kern::RequestComplete(iClientThreadPtr,pS,r);
       
   502 		m.Complete(KErrNone,ETrue);
       
   503 		}
       
   504 	else
       
   505 		{
       
   506 		// DoControl
       
   507 		TInt r=DoControl(id,m.Ptr0(),m.Ptr1());
       
   508 		m.Complete(r,ETrue);
       
   509 		}
       
   510 	}
       
   511 
       
   512 
       
   513 /**
       
   514   Process synchronous 'control' requests
       
   515 */
       
   516 TInt DTestResManLdd::DoControl(TInt aFunction, TAny* a1, TAny* a2)
       
   517 	{
       
   518 	TInt r = KErrNone;
       
   519 	TParameterListInfo ptr = {0, 0, 0, 0, 0};
       
   520 	//Copy parameter structure from user space.
       
   521 	if((aFunction != RTestResMan::EDeRegisterClient) && (aFunction != RTestResMan::ERequestNotificationUncond) && 
       
   522 		   (aFunction != RTestResMan::ERegisterForIdleResourcesInfo) && (aFunction != RTestResMan::EGetIdleResourcesInfo) 
       
   523 		   && (aFunction != RTestResMan::EDeRegisterClientLevelFromResource) && (aFunction != RTestResMan::ECheckPostBootLevelNotifications)
       
   524 		   && (aFunction != RTestResMan::EGetControllerVersion)
       
   525 #ifdef PRM_ENABLE_EXTENDED_VERSION
       
   526 		   && (aFunction != RTestResMan::ERegisterDynamicResource))
       
   527 #else
       
   528 		   )
       
   529 #endif
       
   530 		{
       
   531 		r = Kern::ThreadRawRead(iClientThreadPtr, (TParameterListInfo*)a1, &ptr, sizeof(TParameterListInfo));
       
   532 		if(r != KErrNone)
       
   533 			return r;
       
   534 		}
       
   535 	switch(aFunction)
       
   536 		{
       
   537 		case RTestResMan::ERegisterClient:
       
   538 			{
       
   539 			TBuf8<256> aName;
       
   540 			TUint c;
       
   541 			//Copy name from user address space
       
   542 			r = Kern::ThreadDesRead(iClientThreadPtr, (const TDesC*)ptr.iPtr2, (TDes8&)aName, 0);
       
   543 			if(r != KErrNone)
       
   544 				{
       
   545 				Kern::Printf("RTestResMan::ERegisterClient ThreadDesRead failed with %d", r);
       
   546 				break;
       
   547 				}
       
   548 			for(c = 0; c < MAX_CLIENTS; c++)
       
   549 				{
       
   550 				if(clientInfo[c].pName == NULL)
       
   551 					break;
       
   552 				if(!clientInfo[c].pName->Compare(aName))
       
   553 					return KErrAlreadyExists;
       
   554 				}
       
   555 			if(c == MAX_CLIENTS)
       
   556 				return KErrOverflow;
       
   557 			clientInfo[c].pName = HBuf::New((const TDesC8&)aName);
       
   558 			if(!clientInfo[c].pName)
       
   559 				return KErrNoMemory;
       
   560 			r = PowerResourceManager::RegisterClient(clientInfo[c].iClientId, (const TDesC&)*clientInfo[c].pName, (TOwnerType)(TInt)ptr.iPtr3);
       
   561 			if(r != KErrNone)
       
   562 				{
       
   563 				delete clientInfo[c].pName;
       
   564 				clientInfo[c].pName = NULL;
       
   565 				clientInfo[c].iClientId = 0;
       
   566 				}
       
   567 			if(r == KErrNone)
       
   568 				{
       
   569 				r = Kern::ThreadRawWrite(iClientThreadPtr, ptr.iPtr1, &clientInfo[c].iClientId, sizeof(TUint));
       
   570 				if(r != KErrNone)
       
   571 					Kern::Printf("RTestResMan::ERegisterClient ThreadRawWrite failed with %d", r);
       
   572 				}
       
   573 			break;
       
   574 			}
       
   575 		case RTestResMan::EDeRegisterClient: //Deregister client from RM
       
   576 			{
       
   577 			//Cancel all notification pending for this client
       
   578 			SNotificationList *pL;
       
   579 			SNotificationList* pN = iCondList;
       
   580 			while(pN != NULL)
       
   581 				{
       
   582 				if(pN->iClientId == (TUint)a1)
       
   583 					{
       
   584 					PowerResourceManager::CancelNotification(pN->iClientId, pN->iResourceId, *pN->iNoti);
       
   585 					pL = pN;
       
   586 					pN = pN->iNext;
       
   587 					LIST_REMOVE(iCondList, pL, iNext, SNotificationList);
       
   588 					delete pL->iNoti;
       
   589 					delete pL;
       
   590 					}
       
   591 				else
       
   592 					pN = pN->iNext;
       
   593 				}
       
   594 			pN = iUnCondList;
       
   595 			while(pN != NULL)
       
   596 				{
       
   597 				if(pN->iClientId == (TUint)a1)
       
   598 					{
       
   599 					PowerResourceManager::CancelNotification(pN->iClientId, pN->iResourceId, *pN->iNoti);
       
   600 					pL = pN;
       
   601 					pN = pN->iNext;
       
   602 					LIST_REMOVE(iUnCondList, pL, iNext, SNotificationList);
       
   603 					delete pL->iNoti;
       
   604 					delete pL;
       
   605 					}
       
   606 				else
       
   607 				   pN = pN->iNext;
       
   608 				}
       
   609 			iClientId = -1;
       
   610 			r = PowerResourceManager::DeRegisterClient((TUint)a1);
       
   611 			if(r != KErrNone)
       
   612 				break;
       
   613 			r = KErrNotFound;
       
   614 			for(TUint c = 0; c < MAX_CLIENTS; c++)
       
   615 				{
       
   616 				if(clientInfo[c].iClientId == (TUint)a1)
       
   617 					{
       
   618 					delete clientInfo[c].pName;
       
   619 					clientInfo[c].pName = 0;
       
   620 					clientInfo[c].iClientId = 0;
       
   621 					r = KErrNone;
       
   622 					break;
       
   623 					}
       
   624 				}
       
   625 
       
   626 			break;
       
   627 			}
       
   628 		case RTestResMan::EGetClientName: //Get Client Name
       
   629 			{
       
   630 			TBuf8<32> aName; 
       
   631 			r = PowerResourceManager::GetClientName((TUint)ptr.iClientId, (TUint)ptr.iPtr1, (TDes &)aName);
       
   632 			if(r== KErrNone)
       
   633 				{
       
   634 				r = Kern::ThreadDesWrite(iClientThreadPtr, ptr.iPtr2, (const TDesC8&)aName, 0);
       
   635 				if(r != KErrNone)
       
   636 					Kern::Printf("RTestResMan::EGetClientName ThreadDesWrite failed with %d", r);
       
   637 				}
       
   638 			break;
       
   639 			}
       
   640 		case RTestResMan::EGetClientId: //Get Client Id
       
   641 			{
       
   642 			TBuf8<256> aName;
       
   643 			TUint aClientId; 
       
   644 			r = Kern::ThreadDesRead(iClientThreadPtr, (const TDesC*)ptr.iPtr1, (TDes8&)aName, 0,KChunkShiftBy0);
       
   645 			if(r != KErrNone)
       
   646 				{
       
   647 				Kern::Printf("RTestResMan::EGetClientId ThreadDesRead failed with %d", r);
       
   648 				break;
       
   649 				}
       
   650 			r = PowerResourceManager::GetClientId(ptr.iClientId, (TDesC&)aName, aClientId);
       
   651 			if(r == KErrNone)
       
   652 				{
       
   653 				r = Kern::ThreadRawWrite(iClientThreadPtr, ptr.iPtr2, &aClientId, sizeof(TUint));
       
   654 				if(r != KErrNone)
       
   655 					Kern::Printf("RTestResMan::EGetClientId ThreadRawWrite failed with %d", r);
       
   656 				}
       
   657 			break;
       
   658 			}
       
   659 		case RTestResMan::EGetResourceId: //Get Resource Id
       
   660 			{
       
   661 			TBuf8<256> aName; 
       
   662 			TUint aResourceId;
       
   663 			r = Kern::ThreadDesRead(iClientThreadPtr, (const TDesC*)ptr.iPtr1, (TDes8&)aName, 0,KChunkShiftBy0);
       
   664 			if(r != KErrNone)
       
   665 				{
       
   666 				Kern::Printf("RTestResMan::EGetResourceId ThreadDesRead failed with %d", r);
       
   667 				break;
       
   668 				}
       
   669 			r = PowerResourceManager::GetResourceId(ptr.iClientId, (TDesC&)aName, aResourceId);
       
   670 			if(r == KErrNone)
       
   671 				{
       
   672 				r = Kern::ThreadRawWrite(iClientThreadPtr, ptr.iPtr2, &aResourceId, sizeof(TUint));
       
   673 				if(r != KErrNone)
       
   674 					Kern::Printf("RTestResMan::EGetResourceId ThreadRawWrite failed with %d", r);
       
   675 				}
       
   676 			break;
       
   677 			}
       
   678 		case RTestResMan::EGetResourceInfo: //Get resource information
       
   679 			{
       
   680 			NKern::ThreadEnterCS();
       
   681 			HBuf *info = HBuf::New(sizeof(TPowerResourceInfoV01));
       
   682 			NKern::ThreadLeaveCS();
       
   683 			r = PowerResourceManager::GetResourceInfo(ptr.iClientId, (TUint)ptr.iPtr1, (TAny*)info);
       
   684 			if(r == KErrNone)
       
   685 				{
       
   686 				r = Kern::ThreadDesWrite(iClientThreadPtr, ptr.iPtr2, (const TDesC8&)*info, 0);
       
   687 				if(r != KErrNone)
       
   688 					Kern::Printf("RTestResMan::EGetResourceInfo ThreadRawWrite failed with %d", r);
       
   689 				}
       
   690 			Kern::Free(info);
       
   691 			break;
       
   692 			} 
       
   693 		case RTestResMan::EGetNumResourcesInUseByClient: 
       
   694 			{
       
   695 			TUint numResource;
       
   696 			r = PowerResourceManager::GetNumResourcesInUseByClient(ptr.iClientId, (TUint)ptr.iPtr1, numResource);
       
   697 			if(r == KErrNone)
       
   698 				{
       
   699 				r = Kern::ThreadRawWrite(iClientThreadPtr, ptr.iPtr2, &numResource, sizeof(TUint));
       
   700 				if(r != KErrNone)
       
   701 					Kern::Printf("RTestResMan::EGetNumResourcesInUseByClient ThreadRawWrite failed with %d", r);
       
   702 				}
       
   703 			 break;
       
   704 			}
       
   705 		case RTestResMan::EGetInfoOnResourcesInUseByClient:
       
   706 			{
       
   707 			TUint numResource;
       
   708 			HBuf* info = NULL;
       
   709 			r = Kern::ThreadRawRead(iClientThreadPtr, ptr.iPtr2, &numResource, sizeof(TUint));
       
   710 			if(r != KErrNone)
       
   711 				{
       
   712 				Kern::Printf("RTestResMan::GetInfoOnResourceInUseByClient ThreadRawRead failed with %d", r);
       
   713 				break;
       
   714 				}
       
   715 			if(ptr.iPtr3 != NULL)
       
   716 				{
       
   717 				NKern::ThreadEnterCS();
       
   718 				info = HBuf::New(numResource * sizeof(TPowerResourceInfoV01));
       
   719 				NKern::ThreadLeaveCS();
       
   720 				}
       
   721 			r = PowerResourceManager::GetInfoOnResourcesInUseByClient(ptr.iClientId, (TUint)ptr.iPtr1, numResource, (TAny*)info);
       
   722 			if(r == KErrNone)
       
   723 				{
       
   724 				r = Kern::ThreadRawWrite(iClientThreadPtr, ptr.iPtr2, &numResource, sizeof(TUint));
       
   725 				if(r !=KErrNone)
       
   726 					{
       
   727 					Kern::Printf("RTestResMan::GetInfoOnResourceInUseByClient ThreadRawWrite failed with %d", r);
       
   728 					Kern::Free(info);
       
   729 					break;
       
   730 					}
       
   731 				if(ptr.iPtr3)
       
   732 					r = Kern::ThreadDesWrite(iClientThreadPtr, ptr.iPtr3, (const TDesC8&)*info, 0);
       
   733 				}
       
   734 			Kern::Free(info);
       
   735 			break;
       
   736 			}
       
   737 		case RTestResMan::EGetNumClientsUsingResource:
       
   738 			{
       
   739 			TUint numClient;
       
   740 			r = PowerResourceManager::GetNumClientsUsingResource(ptr.iClientId, (TUint)ptr.iPtr1, numClient);
       
   741 			if(r == KErrNone)
       
   742 				{
       
   743 				r = Kern::ThreadRawWrite(iClientThreadPtr, ptr.iPtr2, &numClient, sizeof(TUint));
       
   744 				if(r != KErrNone)
       
   745 					Kern::Printf("RTestResMan::EGetNumResourcesInUseByClient ThreadRawWrite failed with %d", r);
       
   746 				}
       
   747 			break;
       
   748 			}
       
   749 		case RTestResMan::EGetInfoOnClientsUsingResource:
       
   750 			{
       
   751 			TUint numClient;
       
   752 			HBuf* info = NULL;
       
   753 			r = Kern::ThreadRawRead(iClientThreadPtr, ptr.iPtr2, &numClient, sizeof(TUint));
       
   754 			if(r != KErrNone)
       
   755 				{
       
   756 				Kern::Printf("RTestResMan::GetInfoOnResourceInUseByClient ThreadRawRead failed with %d", r);
       
   757 				break;
       
   758 				}
       
   759 			if(ptr.iPtr3 != NULL)
       
   760 				{
       
   761 				NKern::ThreadEnterCS();
       
   762 				info = HBuf::New(numClient * sizeof(TPowerClientInfoV01));
       
   763 				NKern::ThreadLeaveCS();
       
   764 				}
       
   765 			r = PowerResourceManager::GetInfoOnClientsUsingResource(ptr.iClientId, (TUint)ptr.iPtr1, numClient, (TAny*)info);
       
   766 			if(r == KErrNone)
       
   767 				{
       
   768 				r = Kern::ThreadRawWrite(iClientThreadPtr, ptr.iPtr2, &numClient, sizeof(TUint));
       
   769 				if(r !=KErrNone)
       
   770 					{
       
   771 					Kern::Printf("RTestResMan::GetInfoOnResourceInUseByClient ThreadRawWrite failed with %d", r);
       
   772 					Kern::Free(info);
       
   773 					break;
       
   774 					}
       
   775 				 if(ptr.iPtr3)
       
   776 					r = Kern::ThreadDesWrite(iClientThreadPtr, ptr.iPtr3, (const TDesC8&)*info, 0);
       
   777 				}
       
   778 			Kern::Free(info);
       
   779 			break;
       
   780 			}
       
   781 		case RTestResMan::EAllocReserve:
       
   782 			{
       
   783 			r = PowerResourceManager::AllocReserve(ptr.iClientId, (TUint8)(TUint)ptr.iPtr1, (TUint8)(TUint)ptr.iPtr2);
       
   784 			break;
       
   785 			}
       
   786 		case RTestResMan::EChangeResourceStateSync:
       
   787 			{
       
   788 			SNotificationList* pN; 
       
   789 			//Zero the conditional notification count for the resource id.
       
   790 			for(pN=iCondList; pN != NULL; pN=pN->iNext)
       
   791 				{
       
   792 				if(pN->iResourceId == (TUint)ptr.iPtr1)
       
   793 					{
       
   794 					pN->iCount = 0;
       
   795 					break;
       
   796 					}
       
   797 				}
       
   798 			//Zero the unconditional notification count for the resource id.
       
   799 			for(pN=iUnCondList; pN != NULL; pN=pN->iNext)
       
   800 				{
       
   801 				if(pN->iResourceId == (TUint)ptr.iPtr1)
       
   802 					{
       
   803 					pN->iCount = 0;
       
   804 					break;
       
   805 					}
       
   806 				}
       
   807 			iClientId = ptr.iClientId;
       
   808 			iResourceId = (TUint)ptr.iPtr1;
       
   809 			r = PowerResourceManager::ChangeResourceState(ptr.iClientId, (TUint)ptr.iPtr1, (TInt)ptr.iPtr2);
       
   810 			break;
       
   811 			}
       
   812 		case RTestResMan::EGetResourceStateSync:
       
   813 			{
       
   814 			TInt newState, levelOwnerId;
       
   815 			r = PowerResourceManager::GetResourceState(ptr.iClientId, (TUint)ptr.iPtr1, (TBool)ptr.iPtr2, newState, levelOwnerId);
       
   816 			if(r == KErrNone)
       
   817 				{
       
   818 				r = Kern::ThreadRawWrite(iClientThreadPtr, ptr.iPtr3, (TAny*)&newState, sizeof(TInt));
       
   819 				if(r != KErrNone)
       
   820 					{
       
   821 					Kern::Printf("RTestResMan::GetResourceStateSync ThreadRawWrite failed with %d", r);
       
   822 					break;
       
   823 					}
       
   824 				r = Kern::ThreadRawWrite(iClientThreadPtr, ptr.iPtr4, (TAny*)&levelOwnerId, sizeof(TInt));
       
   825 				if(r != KErrNone)
       
   826 					Kern::Printf("RTestResMan::GetResourceStateSync ThreadRawWrite failed with %d", r);
       
   827 				}
       
   828 			break;
       
   829 			}
       
   830 		case RTestResMan::ERequestNotificationUncond:
       
   831 			{
       
   832 			SNotificationList *notiList;
       
   833 			notiList = new SNotificationList; //Create new notification list
       
   834 			if(!notiList)
       
   835 				{
       
   836 				r = KErrNoMemory;
       
   837 				break;
       
   838 				}
       
   839 			//Create notification object to pass to RM
       
   840 			notiList->iNoti = new DPowerResourceNotification(UnCondNotificationFunc, this, ((DTestResManLddFactory*)iDevice)->iDfcQ, 3);
       
   841 			if(!notiList->iNoti)
       
   842 				{
       
   843 				delete notiList;
       
   844 				r = KErrNoMemory;
       
   845 				break;
       
   846 				}
       
   847 			notiList->iClientId = (TUint)a1;
       
   848 			notiList->iResourceId = (TUint)a2;
       
   849 			notiList->iCount = 0;
       
   850 			LIST_PUSH(iUnCondList, notiList, iNext); //Add to unconditional list.							 
       
   851 			r = PowerResourceManager::RequestNotification((TUint)a1, (TUint)a2, *notiList->iNoti);
       
   852 			break;
       
   853 			}
       
   854 		case RTestResMan::ERequestNotificationCond:
       
   855 			{
       
   856 			SNotificationList *notiList;
       
   857 			notiList = new SNotificationList; // Create new notification list
       
   858 			if(!notiList)
       
   859 				{
       
   860 				r = KErrNoMemory;
       
   861 				break;
       
   862 				}
       
   863 			//Create notification object to pass to RM
       
   864 			notiList->iNoti = new DPowerResourceNotification(CondNotificationFunc, this, ((DTestResManLddFactory*)iDevice)->iDfcQ, 3);
       
   865 			if(!notiList->iNoti)
       
   866 				{
       
   867 				delete notiList;
       
   868 				r = KErrNoMemory;
       
   869 				break;
       
   870 				}
       
   871 			notiList->iClientId = ptr.iClientId;
       
   872 			notiList->iResourceId = (TUint)ptr.iPtr1;
       
   873 			notiList->iCount = 0;
       
   874 			LIST_PUSH(iCondList, notiList, iNext); //Add to conditional list.
       
   875 			r = PowerResourceManager::RequestNotification((TUint)ptr.iClientId, (TUint)ptr.iPtr1, *notiList->iNoti, (TInt)ptr.iPtr2, (TBool)ptr.iPtr3);
       
   876 			break;
       
   877 			}
       
   878 		case RTestResMan::EDeRegisterClientLevelFromResource:	
       
   879 			{
       
   880 				r = PowerResourceManager::DeRegisterClientLevelFromResource((TUint)a1, (TUint)a2);
       
   881 				break;
       
   882 			} 
       
   883 		case RTestResMan::ECancelNotification:
       
   884 			{
       
   885 			r = KErrNotFound;
       
   886 			if(ptr.iPtr2) //Check for notification in conditional list if it is true.
       
   887 				{ 
       
   888 				for(SNotificationList* pN=iCondList; pN != NULL; pN=pN->iNext)
       
   889 					{
       
   890 					if((pN->iClientId == ptr.iClientId) && (pN->iResourceId == (TUint)ptr.iPtr1))
       
   891 						{
       
   892 						r = PowerResourceManager::CancelNotification(pN->iClientId, pN->iResourceId, *pN->iNoti);
       
   893 						LIST_REMOVE(iCondList, pN, iNext, SNotificationList);
       
   894 						delete pN->iNoti;
       
   895 						delete pN;
       
   896 						break;
       
   897 						}
       
   898 					}
       
   899 				}
       
   900 			else //Check for notification in unconditional list.
       
   901 				{
       
   902 				for(SNotificationList* pN=iUnCondList; pN != NULL; pN=pN->iNext)
       
   903 					{
       
   904 					if((pN->iClientId == ptr.iClientId) && (pN->iResourceId == (TUint)ptr.iPtr1))
       
   905 						{
       
   906 						r = PowerResourceManager::CancelNotification(pN->iClientId, pN->iResourceId, *pN->iNoti);
       
   907 						LIST_REMOVE(iUnCondList, pN, iNext, SNotificationList);
       
   908 						delete pN->iNoti;
       
   909 						delete pN;
       
   910 						break;
       
   911 						}
       
   912 					}
       
   913 				}
       
   914 			break;
       
   915 			}
       
   916 		case RTestResMan::ECheckNotifications:
       
   917 			{
       
   918 			NKern::Sleep(NKern::TimerTicks(300)); //This is required as sometimes check is done before callback is called.
       
   919 			TUint countCond = 0, countUncond = 0;
       
   920 			SNotificationList* pN; 
       
   921 			//Get the conditional notification callback functions called for the resource id.
       
   922 			for(pN=iCondList; pN != NULL; pN=pN->iNext)
       
   923 				{
       
   924 				if(pN->iResourceId == (TUint)ptr.iPtr1)
       
   925 					{
       
   926 					countCond = pN->iCount;
       
   927 					pN->iCount = 0;
       
   928 					break;
       
   929 					}
       
   930 				}
       
   931 			//Get the unconditional notification callback functions called for the resource id.
       
   932 			for(pN=iUnCondList; pN != NULL; pN=pN->iNext)
       
   933 				{
       
   934 				if(pN->iResourceId == (TUint)ptr.iPtr1)
       
   935 					{
       
   936 					countUncond = pN->iCount;
       
   937 					pN->iCount = 0;
       
   938 					break;
       
   939 					}
       
   940 				}
       
   941 			//If the notifications count is not as expected return error.
       
   942 			if((countCond != (TUint)ptr.iPtr3) || (countUncond != (TUint)ptr.iPtr2))
       
   943 				r = KErrUnderflow;
       
   944 			break;
       
   945 			}
       
   946 		case RTestResMan::ERegisterForIdleResourcesInfo:
       
   947 			{	
       
   948 			if(pBuf)
       
   949 				{
       
   950 				r = KErrAlreadyExists;
       
   951 				break;
       
   952 				}
       
   953 			NKern::ThreadEnterCS();
       
   954 			pBuf = HBuf::New((TUint)a2 * sizeof(SIdleResourceInfo)); //Allocate buffer for requested resources
       
   955 			NKern::ThreadLeaveCS();
       
   956 			if(!pBuf)
       
   957 				return KErrNoMemory;
       
   958 			SIdleResourceInfo* pI = (SIdleResourceInfo*)pBuf->Ptr();
       
   959 			for(TUint c = 0; c < (TUint)a2; c++)
       
   960 				pI[c].iResourceId = c+1; //Update resource id
       
   961 			//Below function calls RegisterForResourceIdle resource controller virtual function, 
       
   962 			//This is for testing purposes only.
       
   963 
       
   964 			r =DSimulatedPowerResourceController::CaptureIdleResourcesInfo((TUint)a1, (TUint)a2, (TPtr*)pI);
       
   965 			if( r == KErrInUse)
       
   966 			   delete pBuf;
       
   967 			break;
       
   968 			}
       
   969 		case RTestResMan::EGetIdleResourcesInfo:
       
   970 			{
       
   971 			//Pass the buffer for comparision
       
   972 			if(!pBuf)
       
   973 				{
       
   974 				r = KErrNotFound;
       
   975 				break;
       
   976 				}
       
   977 			pBuf->SetLength(sizeof(SIdleResourceInfo) * (TUint)a1);
       
   978 			r = Kern::ThreadDesWrite(iClientThreadPtr, a2, (const TDesC8&)*pBuf, 0);
       
   979 			break;
       
   980 			}
       
   981 		case RTestResMan::ECheckPostBootLevelNotifications:
       
   982 			{
       
   983 				if(DTestResManLddFactory::iPostBootNotiCount != EXPECTED_POST_NOTI_COUNT)
       
   984 				{
       
   985 				r = KErrUnderflow;
       
   986 				break;
       
   987 				}
       
   988 			r = KErrNone;
       
   989 			break;
       
   990 			}
       
   991 		case RTestResMan::EGetControllerVersion:
       
   992 			{
       
   993 			TUint Version;
       
   994 			r = PowerResourceManager::GetResourceControllerVersion((TUint)a1, Version);
       
   995 			if(r == KErrNone)
       
   996 				{
       
   997 				r = Kern::ThreadRawWrite(iClientThreadPtr, a2, &Version, sizeof(TUint));
       
   998 				if(r != KErrNone)
       
   999 					Kern::Printf("RTestResMan::EGetControllerVersion ThreadRawWrite failed with %d", r);
       
  1000 				}
       
  1001 			break;
       
  1002 			}
       
  1003 #ifdef PRM_ENABLE_EXTENDED_VERSION
       
  1004 		case RTestResMan::ERegisterDynamicResource:
       
  1005 			{
       
  1006 			TUint resId;
       
  1007 			r = Kern::ThreadRawRead(iClientThreadPtr, a2, (TAny*)&resId, sizeof(TUint));
       
  1008 			if(r != KErrNone)
       
  1009 				{
       
  1010 				Kern::Printf("RTestResMan::RegisterDynamicResource ThreadRawRead failed with %d", r);
       
  1011 				break;
       
  1012 				}			
       
  1013 			DDynamicPowerResource* pDR = NULL;
       
  1014 			switch(resId) //Create the dynamic resource
       
  1015 				{
       
  1016 				case 1:
       
  1017 					pDR = new (DBIGISSPDynamicResource);
       
  1018 					break;
       
  1019 				case 2:
       
  1020 					pDR = new (DMLIGLSSHNDynamicResource);
       
  1021 					break;
       
  1022 				case 3: 
       
  1023 					pDR = new (DBLGLSSHNDynamicResource);
       
  1024 					break;
       
  1025 				case 4:
       
  1026 					pDR = new (DMLLGLSSHPDynamicResource);
       
  1027 					break;
       
  1028 				case 5:
       
  1029 					pDR = (DDynamicPowerResource*) new (DDynamicResourceD01);
       
  1030 					break;
       
  1031 				case 6:
       
  1032 					pDR = (DDynamicPowerResource*) new (DDynamicResourceD02);
       
  1033 					break;
       
  1034 				case 7:
       
  1035 					pDR = (DDynamicPowerResource*) new (DDynamicResourceD03);
       
  1036 					break;
       
  1037 				case 8:
       
  1038 					pDR = (DDynamicPowerResource*) new (DDynamicResourceD04);
       
  1039 					break;
       
  1040 				}
       
  1041 			if(!pDR)
       
  1042 				return KErrNoMemory;
       
  1043 			iDynamicArray.Append(pDR);
       
  1044 			r = PowerResourceManager::RegisterDynamicResource((TUint)a1, pDR, resId);
       
  1045 			if(r == KErrNone)
       
  1046 				{
       
  1047 				r = Kern::ThreadRawWrite(iClientThreadPtr, a2, (TAny*)&resId, sizeof(TUint));
       
  1048 				if(r != KErrNone)
       
  1049 					Kern::Printf("RTestResMan::RegisterDynamicResource ThreadRawWrite failed with %d", r);
       
  1050 				}
       
  1051 			break;
       
  1052 			}
       
  1053 		case RTestResMan::EDeRegisterDynamicResource: //Deregister dynamic resource
       
  1054 			{
       
  1055 			if(ptr.iPtr2)
       
  1056 				{
       
  1057 				TInt level;
       
  1058 				r = Kern::ThreadRawRead(iClientThreadPtr, ptr.iPtr2, &level, sizeof(TInt));
       
  1059 				if(r != KErrNone)
       
  1060 					{
       
  1061 					Kern::Printf("RTestResMan::DeRegisterDynamicResource ThreadRawRead failed with %d", r);
       
  1062 					break;
       
  1063 					}	
       
  1064 				r = PowerResourceManager::DeRegisterDynamicResource(ptr.iClientId, (TUint)ptr.iPtr1, &level);
       
  1065 				}
       
  1066 			else
       
  1067 				r = PowerResourceManager::DeRegisterDynamicResource(ptr.iClientId, (TUint)ptr.iPtr1, NULL);
       
  1068 			break;
       
  1069 			}
       
  1070 		case RTestResMan::ERegisterResourceDependency: //Register resource dependency
       
  1071 			{
       
  1072 			SResourceDependencyInfo info1, info2;
       
  1073 			r = Kern::ThreadRawRead(iClientThreadPtr, ptr.iPtr1, &info1, sizeof(SResourceDependencyInfo));
       
  1074 			if(r != KErrNone)
       
  1075 				{
       
  1076 				Kern::Printf("RTestResMan::RegisterResourceDependency ThreadRawRead failed with %d", r);
       
  1077 				break;
       
  1078 				}
       
  1079 			r = Kern::ThreadRawRead(iClientThreadPtr, ptr.iPtr2, &info2, sizeof(SResourceDependencyInfo));
       
  1080 			if(r != KErrNone)
       
  1081 				{
       
  1082 				Kern::Printf("RTestResMan::RegisterResourceDependency ThreadRawRead failed with %d", r);
       
  1083 				break;
       
  1084 				}
       
  1085 			r = PowerResourceManager::RegisterResourceDependency(ptr.iClientId, &info1, &info2);
       
  1086 			break;
       
  1087 			}
       
  1088 		case RTestResMan::EDeRegisterResourceDependency: //Deregister resource dependency
       
  1089 			{
       
  1090 			r = PowerResourceManager::DeRegisterResourceDependency(ptr.iClientId, (TUint)ptr.iPtr1, (TUint)ptr.iPtr2);
       
  1091 			break;
       
  1092 			}
       
  1093 		case RTestResMan::EGetNumDependentsForResource:
       
  1094 			{
       
  1095 			TUint numDepResources;
       
  1096 			r = PowerResourceManager::GetNumDependentsForResource(ptr.iClientId, (TUint)ptr.iPtr1, numDepResources);
       
  1097 			if(r == KErrNone)
       
  1098 				{
       
  1099 				r = Kern::ThreadRawWrite(iClientThreadPtr, ptr.iPtr2, (TAny*)&numDepResources, sizeof(TUint));
       
  1100 				if(r != KErrNone)
       
  1101 					Kern::Printf("RTestResMan::RegisterDynamicResource ThreadRawWrite failed with %d", r);
       
  1102 				}
       
  1103 			break;
       
  1104 			}
       
  1105 		case RTestResMan::EGetDependentsIdForResource:
       
  1106 			{
       
  1107 			TUint numDepResources;
       
  1108 			HBuf* sResDepInfo = NULL;
       
  1109 			r = Kern::ThreadRawRead(iClientThreadPtr, ptr.iPtr3, (TAny*)&numDepResources, sizeof(TUint));
       
  1110 			if(r != KErrNone)
       
  1111 				{
       
  1112 				Kern::Printf("RTestResMan::GetDependentsIdForResource ThreadRawRead failed with %d", r);
       
  1113 				break;
       
  1114 				}
       
  1115 			if(ptr.iPtr2 != NULL)
       
  1116 				{
       
  1117 				NKern::ThreadEnterCS();
       
  1118 				sResDepInfo = HBuf::New(numDepResources * sizeof(SResourceDependencyInfo));
       
  1119 				NKern::ThreadLeaveCS();
       
  1120 				if(!sResDepInfo)
       
  1121 					return KErrNoMemory;
       
  1122 				}
       
  1123 			
       
  1124 			r = PowerResourceManager::GetDependentsIdForResource(ptr.iClientId, (TUint)ptr.iPtr1, (TAny*)sResDepInfo, numDepResources);
       
  1125 			if(r == KErrNone)
       
  1126 				{
       
  1127 				if(ptr.iPtr2)
       
  1128 					r = Kern::ThreadDesWrite(iClientThreadPtr, ptr.iPtr2, (const TDesC8&)*sResDepInfo, 0);
       
  1129 				if(r != KErrNone)
       
  1130 					{
       
  1131 					Kern::Printf("RTestResMan::GetDepedentsIdForResource ThreadDesWrite failed with %d", r);
       
  1132 					Kern::Free(sResDepInfo);
       
  1133 					break;
       
  1134 					}
       
  1135 				r = Kern::ThreadRawWrite(iClientThreadPtr, ptr.iPtr3, (TAny*)&numDepResources, sizeof(TUint));
       
  1136 				if(r != KErrNone)
       
  1137 					{
       
  1138 					Kern::Printf("RTestResMan::GetDependentsIdForResource ThreadRawWrite failed with %d", r);
       
  1139 					Kern::Free(sResDepInfo);
       
  1140 					break;
       
  1141 					}
       
  1142 				}
       
  1143 			Kern::Free(sResDepInfo);
       
  1144 			break;
       
  1145 			}
       
  1146 
       
  1147 #endif
       
  1148 		default:
       
  1149 			r = KErrNotSupported;
       
  1150 			break;
       
  1151 		}
       
  1152 	return r;
       
  1153 	}
       
  1154 
       
  1155 TInt DTestResManLdd::DoRequest(TInt aReqNo, TRequestStatus* aStatus, TAny* a1, TAny* /*a2*/)
       
  1156 	{
       
  1157 	TInt r = KErrNone;
       
  1158 	TParameterListInfo ptr;
       
  1159 	r = Kern::ThreadRawRead(iClientThreadPtr, (TParameterListInfo*)a1, &ptr, sizeof(TParameterListInfo));
       
  1160 	if(r != KErrNone)
       
  1161 		Kern::RequestComplete(iClientThreadPtr, aStatus, r);
       
  1162 	switch(aReqNo)
       
  1163 		{
       
  1164 		case RTestResMan::EChangeResourceStateAsync:
       
  1165 #ifdef PRM_ENABLE_EXTENDED_VERSION
       
  1166 		case RTestResMan::EChangeResStateAndDeregisterDynamicRes:
       
  1167 		case RTestResMan::ECheckParallelExecutionForChangeResState:
       
  1168 #endif
       
  1169 			SNotificationList* pN; 
       
  1170 			//Zero the conditional notification count for the resource id.
       
  1171 			for(pN=iCondList; pN != NULL; pN=pN->iNext)
       
  1172 				{
       
  1173 				if(pN->iResourceId == (TUint)ptr.iPtr1)
       
  1174 					{
       
  1175 					pN->iCount = 0;
       
  1176 					break;
       
  1177 					}
       
  1178 				}
       
  1179 			//Zero the unconditional notification count for the resource id.
       
  1180 			for(pN=iUnCondList; pN != NULL; pN=pN->iNext)
       
  1181 				{
       
  1182 				if(pN->iResourceId == (TUint)ptr.iPtr1)
       
  1183 					{
       
  1184 					pN->iCount = 0;
       
  1185 					break;
       
  1186 					}
       
  1187 				}
       
  1188 			iLevelOwnerIdPtr = NULL;
       
  1189 			iStatePtr = (TInt*)ptr.iPtr2;
       
  1190 			TInt state;
       
  1191 			r = Kern::ThreadRawRead(iClientThreadPtr, iStatePtr, &state, sizeof(TInt)); 
       
  1192 			if(r != KErrNone)
       
  1193 				Kern::RequestComplete(iClientThreadPtr, aStatus, r);
       
  1194 			iStatus = aStatus;
       
  1195 			iClientId = ptr.iClientId;
       
  1196 			iResourceId = (TUint)ptr.iPtr1;
       
  1197 			iCallbackCancel = EFalse;
       
  1198 			r = PowerResourceManager::ChangeResourceState(ptr.iClientId, (TUint)ptr.iPtr1, state, &iAsyncResourceCallback);
       
  1199 #ifdef PRM_ENABLE_EXTENDED_VERSION
       
  1200 			if(aReqNo == RTestResMan::EChangeResStateAndDeregisterDynamicRes) //Try to delete the dynamic while resource change
       
  1201 				{
       
  1202 				r = PowerResourceManager::DeRegisterDynamicResource(ptr.iClientId, (TUint)ptr.iPtr1, NULL);
       
  1203 				if(r == KErrInUse) //Wait for the request to complete
       
  1204 					r = KErrNone; 
       
  1205 				break;
       
  1206 				}
       
  1207 			if(aReqNo == RTestResMan::ECheckParallelExecutionForChangeResState)
       
  1208 				{
       
  1209 				r = PowerResourceManager::ChangeResourceState(ptr.iClientId, (TUint)ptr.iPtr3, (TInt)ptr.iPtr4, &iAsyncTestParallelCallback);
       
  1210 				iTestParallelResourceId = (TUint)ptr.iPtr3;
       
  1211 				iValidateCallbackReceived = ETrue;
       
  1212 				break;
       
  1213 				}
       
  1214 #endif
       
  1215 			if(ptr.iPtr3) //Cancel the asynchronous operation if true.
       
  1216 				{
       
  1217 				r = PowerResourceManager::CancelAsyncRequestCallBack(ptr.iClientId, (TUint)ptr.iPtr1, iAsyncResourceCallback);
       
  1218 				if(r == KErrInUse) //Wait for the request to complete
       
  1219 					r = KErrNone;
       
  1220 				else
       
  1221 					iCallbackCancel = ETrue;
       
  1222 				}
       
  1223 			break;
       
  1224 		case RTestResMan::EGetResourceStateAsync:
       
  1225 			iStatus = aStatus;
       
  1226 			iCallbackCancel = EFalse;
       
  1227 			iClientId = ptr.iClientId;
       
  1228 			iResourceId = (TUint)ptr.iPtr1;
       
  1229 			iStatePtr = (TInt*)ptr.iPtr4;
       
  1230 			iLevelOwnerIdPtr = (TInt*)ptr.iPtr5;
       
  1231 			r = PowerResourceManager::GetResourceState(ptr.iClientId, (TUint)ptr.iPtr1, (TBool)ptr.iPtr2, iAsyncResourceCallback);
       
  1232 			if(ptr.iPtr3) //Cancel the asynchronous operation if true.
       
  1233 				{
       
  1234 				r = PowerResourceManager::CancelAsyncRequestCallBack(ptr.iClientId, (TUint)ptr.iPtr1, iAsyncResourceCallback);
       
  1235 				if(r == KErrInUse)
       
  1236 					r = KErrNone;
       
  1237 				else 
       
  1238 					iCallbackCancel = ETrue;
       
  1239 				}
       
  1240 			break;
       
  1241 		}
       
  1242 	return r;
       
  1243 	}
       
  1244 
       
  1245 //Function called on Asynchronous operation
       
  1246 void DTestResManLdd::CallbackFunc(TUint aClientId, TUint aResId, TInt aLevel, TInt aLevelOwnerId, TInt aResult, TAny* aParam)
       
  1247 	{
       
  1248 	TInt r;
       
  1249 	DTestResManLdd *pC = (DTestResManLdd*)aParam;
       
  1250 	//Check for correctnes of clientId and resourceId
       
  1251 	if((TUint)pC->iClientId != aClientId || pC->iResourceId != aResId)
       
  1252 		Kern::RequestComplete(pC->iClientThreadPtr, pC->iStatus, KErrCorrupt);
       
  1253 	if(!pC->iCallbackCancel)
       
  1254 		{
       
  1255 		if(pC->iStatePtr) 
       
  1256 			{
       
  1257 			r = Kern::ThreadRawWrite(pC->iClientThreadPtr, pC->iStatePtr, (TAny*)&aLevel, sizeof(TInt));
       
  1258 			if(r != KErrNone)
       
  1259 				Kern::Printf("RTestResManLdd::CallbackFunc ThreadRawWrite failed with %d", r);
       
  1260 			}
       
  1261 		if(pC->iLevelOwnerIdPtr)
       
  1262 			{
       
  1263 			r = Kern::ThreadRawWrite(pC->iClientThreadPtr, pC->iLevelOwnerIdPtr, (TAny*)&aLevelOwnerId, sizeof(TInt));
       
  1264 			if(r != KErrNone)
       
  1265 				Kern::Printf("RTestResManLdd::CallbackFunc ThreadRawWrite failed with %d", r);
       
  1266 			}
       
  1267 		#ifdef PRM_ENABLE_EXTENDED_VERSION
       
  1268 		if(pC->iValidateCallbackReceived)
       
  1269 			{
       
  1270 			if(!pC->iCallbackReceived)
       
  1271 				aResult = KErrCompletion;
       
  1272 			}
       
  1273 		#endif
       
  1274 		Kern::RequestComplete(pC->iClientThreadPtr, pC->iStatus, aResult);
       
  1275 		}
       
  1276 	pC->iCallbackCancel = EFalse;
       
  1277 	pC->iStatus = NULL;
       
  1278 	}
       
  1279 
       
  1280 #ifdef PRM_ENABLE_EXTENDED_VERSION
       
  1281 //Function called on completion of asynchronous long latency resource, used only to check parallel execution of DFC's
       
  1282 void DTestResManLdd::TestParallelExecutionCallback(TUint aClientId, TUint aResId, TInt /*aLevel*/, TInt /*aLevelOwnerId*/, TInt /*aResult*/, TAny* aParam)
       
  1283 	{
       
  1284 	Kern::Printf("DTestResManLdd::TestParallelExecutionCallback:: called");
       
  1285 	DTestResManLdd *pC = (DTestResManLdd*)aParam;
       
  1286 	//Check for correctness of clientId and resourceId
       
  1287 	if((TUint)pC->iClientId == aClientId && pC->iTestParallelResourceId == aResId)
       
  1288 		{
       
  1289 		pC->iCallbackReceived = ETrue;
       
  1290 		}
       
  1291 	}
       
  1292 #endif
       
  1293 
       
  1294 //Function called on Conditional notification
       
  1295 void DTestResManLdd::CondNotificationFunc(TUint /*aClientId*/, TUint aResId, TInt /*aLevel*/, TInt /*aLevelOwnerId*/, TInt /*aResult*/, TAny* aParam)
       
  1296 	{
       
  1297 	DTestResManLdd *pC = (DTestResManLdd*)aParam;
       
  1298 	for(SNotificationList *pN = pC->iCondList; pN!= NULL; pN=pN->iNext)
       
  1299 		{
       
  1300 		if((pN->iResourceId == aResId))
       
  1301 			{
       
  1302 			pN->iCount++; //Increment the count, as same callback function for all conditioanl notifications.
       
  1303 			break;
       
  1304 			}
       
  1305 		}
       
  1306 	}
       
  1307 
       
  1308 //Function called on UnConditional notification
       
  1309 void DTestResManLdd::UnCondNotificationFunc(TUint /*aClientId*/, TUint aResId, TInt /*aLevel*/, TInt /*aLevelOwnerId*/, TInt /*aResult*/, TAny* aParam)
       
  1310 	{
       
  1311 	DTestResManLdd *pC = (DTestResManLdd*)aParam;
       
  1312 	for(SNotificationList *pN = pC->iUnCondList; pN!= NULL; pN=pN->iNext)
       
  1313 		{
       
  1314 		if((pN->iResourceId == aResId) && (pC->iCallbackCancel == EFalse))
       
  1315 			{
       
  1316 			pN->iCount++; //Increment the count as same callback function for all unconditioanl notifications.
       
  1317 			break;
       
  1318 			}
       
  1319 		}
       
  1320 	}
       
  1321 
       
  1322 //Function called on postbootvalueset. 
       
  1323 void DTestResManLddFactory::PostBootNotificationFunc(TUint /*aClientId*/, TUint aResId, TInt /*aLevel*/, TInt /*aLevelOwnerId*/, TInt /*aResult*/, TAny* aParam)
       
  1324 	{
       
  1325 	iPostBootNotiCount++;
       
  1326 	DPowerResourceNotification *ptr = (DPowerResourceNotification*)aParam;
       
  1327 	TInt r = PowerResourceManager::CancelNotification(iClient.iClientId, aResId, *ptr);
       
  1328 	if(r == KErrNone)
       
  1329 		delete ptr;
       
  1330 	}