199
|
1 |
// Copyright (c) 2010 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_rescontrolclisync.cpp
|
|
15 |
//
|
|
16 |
//
|
|
17 |
|
|
18 |
#include <kernel/kern_priv.h>
|
|
19 |
#include <drivers/resource_extend.h>
|
|
20 |
#include <drivers/resourceman.h>
|
|
21 |
#include "d_rescontrolclisync.h"
|
|
22 |
|
|
23 |
#include "resourceman_psl/rescontrol_psl.h"
|
|
24 |
|
|
25 |
_LIT(KTestPowerRCName, "D_RESCONTROLCLISYNC.LDD");
|
|
26 |
|
|
27 |
|
|
28 |
const TInt KTestResManLddThreadPriority = 0x5;
|
|
29 |
const TUint KResourceId = 16; // DMLSHLGLSPResource
|
|
30 |
const TInt KResourceMax = 65;
|
|
31 |
const TInt KResourceMin = 10;
|
|
32 |
|
|
33 |
_LIT(KTestResManLddThread, "TestResManLddThread");
|
|
34 |
_LIT(KTestResManLddHelperThread, "TestResManLddHelperThread");
|
|
35 |
_LIT(KTestResManLddCallbackThread, "TestResManLddCallbackThread");
|
|
36 |
_LIT(KResClientName1, "ResTestClient1");
|
|
37 |
_LIT(KResClientName2, "ResTestClient2");
|
|
38 |
|
|
39 |
class DTestResManLdd;
|
|
40 |
/**
|
|
41 |
The logical device (factory class) for the resource manager client side test.
|
|
42 |
*/
|
|
43 |
class DTestResManLddFactory : public DLogicalDevice
|
|
44 |
{
|
|
45 |
public:
|
|
46 |
enum {
|
|
47 |
ESignallerCallback,
|
|
48 |
EWaiterCallback
|
|
49 |
};
|
|
50 |
DTestResManLddFactory();
|
|
51 |
~DTestResManLddFactory();
|
|
52 |
virtual TInt Install();
|
|
53 |
virtual void GetCaps(TDes8 &aDes) const;
|
|
54 |
virtual TInt Create(DLogicalChannelBase*& aChannel);
|
|
55 |
|
|
56 |
NFastSemaphore iSemaphore1; // fast semaphore for helper queue
|
|
57 |
NFastSemaphore iSemaphore2; // fast semaphore for call back thread
|
|
58 |
|
|
59 |
TInt iResourceClientRegisterCount;
|
|
60 |
TDynamicDfcQue* iLddQue; // dfc que for logical channel (also running the primary ChangeResourceState
|
|
61 |
TDynamicDfcQue* iLddHelperQue; // helper dfc que to execute the secondary ChangeResourceState
|
|
62 |
TDynamicDfcQue* iCallbackQue; // dfc que for call back
|
|
63 |
|
|
64 |
DTestResManLdd* iChannel1;
|
|
65 |
DTestResManLdd* iChannel2;
|
|
66 |
TInt iCallbackState;
|
|
67 |
TPowerResourceCb* iAsyncResourceCallback;
|
|
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 |
|
|
80 |
DThread* iClientThreadPtr;
|
|
81 |
TRequestStatus* iStatus;
|
|
82 |
TUint iResClientId;
|
|
83 |
TDfc* iWaitAndChangeResourceDfc;
|
|
84 |
|
|
85 |
private:
|
|
86 |
TInt DoControl(TInt aFunction, TAny* a1, TAny* a2);
|
|
87 |
TInt DoRequest(TInt aFunction, TRequestStatus* aStatus, TAny* a1, TAny* a2);
|
|
88 |
TInt DoCancel(TUint aMask);
|
|
89 |
};
|
|
90 |
|
|
91 |
void AsyncResourceCallbackFn(TUint /*aClientId*/, TUint /*aResourceId*/, TInt /*aLevel*/, TInt /*aLevelOwnerId*/, TInt aResult, TAny* aParam)
|
|
92 |
{
|
|
93 |
// call back function, always run on call back dfc queue
|
|
94 |
Kern::Printf(">AsyncResourceCallbackFn 0x%x", &(Kern::CurrentThread().iNThread));
|
|
95 |
DTestResManLddFactory* pLddFactory = (DTestResManLddFactory*)aParam;
|
|
96 |
DTestResManLdd* pLdd = NULL;
|
|
97 |
|
|
98 |
if(pLddFactory->iCallbackState == DTestResManLddFactory::ESignallerCallback)
|
|
99 |
{
|
|
100 |
pLdd = pLddFactory->iChannel1;
|
|
101 |
Kern::Printf("AsyncResourceCallbackFn cond 1 signal [#3.1]");
|
|
102 |
NKern::FSSignal(&(pLddFactory->iSemaphore1));
|
|
103 |
|
|
104 |
Kern::Printf("AsyncResourceCallbackFn cond 1 wait [#3.2]");
|
|
105 |
NKern::FSWait(&(pLddFactory->iSemaphore2));
|
|
106 |
|
|
107 |
Kern::Printf("AsyncResourceCallbackFn cond 1 wake up [#3.3]");
|
|
108 |
}
|
|
109 |
else if(pLddFactory->iCallbackState == DTestResManLddFactory::EWaiterCallback)
|
|
110 |
{
|
|
111 |
pLdd = pLddFactory->iChannel2;
|
|
112 |
// aResult should be equal to KErrNone (not KErrCompletion)
|
|
113 |
Kern::Printf("AsyncResourceCallbackFn cond 2 r = %d [#2.5]", aResult);
|
|
114 |
}
|
|
115 |
else
|
|
116 |
{
|
|
117 |
Kern::Fault("AsyncResourceCallbackFn", __LINE__);
|
|
118 |
}
|
|
119 |
|
|
120 |
Kern::RequestComplete(pLdd->iClientThreadPtr, pLdd->iStatus, aResult);
|
|
121 |
pLdd->iStatus = NULL;
|
|
122 |
Kern::Printf("<AsyncResourceCallbackFn");
|
|
123 |
}
|
|
124 |
|
|
125 |
void WaitAndChangeResourceDfcFn(TAny* aLdd)
|
|
126 |
{
|
|
127 |
// helper function to call ChangeResourceState, always run on ldd helper dfc queue
|
|
128 |
Kern::Printf(">WaitAndChangeResourceDfcFn 0x%x [#2.1]", &(Kern::CurrentThread().iNThread));
|
|
129 |
DTestResManLdd* pLdd = (DTestResManLdd*)aLdd;
|
|
130 |
DTestResManLddFactory* pLddFactory = (DTestResManLddFactory*)(pLdd->iDevice);
|
|
131 |
|
|
132 |
Kern::Printf(" WaitAndChangeResourceDfcFn - Wait for Semaphore [#2.2]");
|
|
133 |
NKern::FSWait(&(pLddFactory->iSemaphore1));
|
|
134 |
|
|
135 |
Kern::Printf(" WaitAndChangeResourceDfcFn - ChangeResourceState [#2.3]");
|
|
136 |
pLddFactory->iCallbackState = DTestResManLddFactory::EWaiterCallback;
|
|
137 |
PowerResourceManager::ChangeResourceState(pLdd->iResClientId, KResourceId, KResourceMax, pLddFactory->iAsyncResourceCallback);
|
|
138 |
|
|
139 |
Kern::Printf(" WaitAndChangeResourceDfcFn - signal [#2.4]");
|
|
140 |
NKern::FSSignal(&(pLddFactory->iSemaphore2));
|
|
141 |
|
|
142 |
delete pLdd->iWaitAndChangeResourceDfc;
|
|
143 |
Kern::Printf("<WaitAndChangeResourceDfcFn");
|
|
144 |
}
|
|
145 |
|
|
146 |
DTestResManLddFactory::DTestResManLddFactory()
|
|
147 |
{
|
|
148 |
iParseMask=0; // Allow info and pdd, but not units
|
|
149 |
iUnitsMask=0;
|
|
150 |
// Set version number for this device
|
|
151 |
iVersion=RTestResMan::VersionRequired();
|
|
152 |
}
|
|
153 |
|
|
154 |
DTestResManLddFactory::~DTestResManLddFactory()
|
|
155 |
{
|
|
156 |
if(iLddQue)
|
|
157 |
iLddQue->Destroy();
|
|
158 |
if(iLddHelperQue)
|
|
159 |
iLddHelperQue->Destroy();
|
|
160 |
if(iCallbackQue)
|
|
161 |
iCallbackQue->Destroy();
|
|
162 |
|
|
163 |
if(iAsyncResourceCallback)
|
|
164 |
delete iAsyncResourceCallback;
|
|
165 |
}
|
|
166 |
|
|
167 |
/** Entry point for this driver */
|
|
168 |
DECLARE_STANDARD_LDD()
|
|
169 |
{
|
|
170 |
DTestResManLddFactory* p = new DTestResManLddFactory;
|
|
171 |
if(!p)
|
|
172 |
return NULL;
|
|
173 |
|
|
174 |
TInt r = KErrNone;
|
|
175 |
|
|
176 |
r = Kern::DynamicDfcQCreate(p->iLddQue, KTestResManLddThreadPriority, KTestResManLddThread);
|
|
177 |
if(r != KErrNone)
|
|
178 |
{
|
|
179 |
return NULL;
|
|
180 |
}
|
|
181 |
Kern::Printf("iLddQue 0x%x", p->iLddQue->iThread);
|
|
182 |
|
|
183 |
r = Kern::DynamicDfcQCreate(p->iLddHelperQue, KTestResManLddThreadPriority, KTestResManLddHelperThread);
|
|
184 |
if(r != KErrNone)
|
|
185 |
{
|
|
186 |
p->iLddQue->Destroy();
|
|
187 |
return NULL;
|
|
188 |
}
|
|
189 |
p->iSemaphore1.iOwningThread = (NThreadBase*)(p->iLddHelperQue->iThread);
|
|
190 |
Kern::Printf("iSemaphore1 owning thread 0x%x", p->iSemaphore1.iOwningThread);
|
|
191 |
|
|
192 |
r = Kern::DynamicDfcQCreate(p->iCallbackQue, KTestResManLddThreadPriority, KTestResManLddCallbackThread);
|
|
193 |
if(r != KErrNone)
|
|
194 |
{
|
|
195 |
p->iLddQue->Destroy();
|
|
196 |
p->iLddHelperQue->Destroy();
|
|
197 |
return NULL;
|
|
198 |
}
|
|
199 |
p->iSemaphore2.iOwningThread = (NThreadBase*)(p->iCallbackQue->iThread);
|
|
200 |
Kern::Printf("iSemaphore2 owning thread 0x%x", p->iSemaphore2.iOwningThread);
|
|
201 |
|
|
202 |
p->iAsyncResourceCallback = new TPowerResourceCb(AsyncResourceCallbackFn, p,
|
|
203 |
p->iCallbackQue, 5);
|
|
204 |
|
|
205 |
#ifdef __SMP__
|
|
206 |
NKern::ThreadSetCpuAffinity((NThread*)(p->iLddQue->iThread), 0);
|
|
207 |
NKern::ThreadSetCpuAffinity((NThread*)(p->iLddHelperQue->iThread), 0);
|
|
208 |
NKern::ThreadSetCpuAffinity((NThread*)(p->iCallbackQue->iThread), 1);
|
|
209 |
#endif
|
|
210 |
|
|
211 |
r = DSimulatedPowerResourceController::CompleteResourceControllerInitialisation();
|
|
212 |
if(r != KErrNone)
|
|
213 |
Kern::Fault("PRM INIT FAILED", __LINE__);
|
|
214 |
return p;
|
|
215 |
}
|
|
216 |
|
|
217 |
/** Second stage constuctor */
|
|
218 |
TInt DTestResManLddFactory::Install()
|
|
219 |
{
|
|
220 |
return(SetName(&KTestPowerRCName));
|
|
221 |
}
|
|
222 |
|
|
223 |
/** Device capabilities */
|
|
224 |
void DTestResManLddFactory::GetCaps(TDes8& aDes)const
|
|
225 |
{
|
|
226 |
// Create a capabilities object
|
|
227 |
RTestResMan::TCaps caps;
|
|
228 |
caps.iVersion = iVersion;
|
|
229 |
// Write it back to user memory
|
|
230 |
Kern::InfoCopy(aDes,(TUint8*)&caps,sizeof(caps));
|
|
231 |
}
|
|
232 |
|
|
233 |
|
|
234 |
TInt DTestResManLddFactory::Create(DLogicalChannelBase*& aChannel)
|
|
235 |
{
|
|
236 |
aChannel = new DTestResManLdd();
|
|
237 |
if(!aChannel)
|
|
238 |
return KErrNoMemory;
|
|
239 |
if(!iChannel1)
|
|
240 |
iChannel1 = (DTestResManLdd*)aChannel;
|
|
241 |
else if(!iChannel2)
|
|
242 |
iChannel2 = (DTestResManLdd*)aChannel;
|
|
243 |
else
|
|
244 |
{
|
|
245 |
delete aChannel;
|
|
246 |
return KErrInUse;
|
|
247 |
}
|
|
248 |
return KErrNone;
|
|
249 |
}
|
|
250 |
|
|
251 |
/** Constructor */
|
|
252 |
DTestResManLdd::DTestResManLdd()
|
|
253 |
{
|
|
254 |
iClientThreadPtr=&Kern::CurrentThread();
|
|
255 |
iResClientId = 0;
|
|
256 |
|
|
257 |
// Increase the DThread's ref count so that it does not close without us
|
|
258 |
((DObject*)iClientThreadPtr)->Open();
|
|
259 |
}
|
|
260 |
|
|
261 |
/** Destructor */
|
|
262 |
DTestResManLdd::~DTestResManLdd()
|
|
263 |
{
|
|
264 |
Kern::SafeClose((DObject*&)iClientThreadPtr,NULL);
|
|
265 |
}
|
|
266 |
|
|
267 |
/** Second stage constructor. */
|
|
268 |
TInt DTestResManLdd::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& aVer)
|
|
269 |
{
|
|
270 |
// Check version
|
|
271 |
if (!Kern::QueryVersionSupported(RTestResMan::VersionRequired(),aVer))
|
|
272 |
return KErrNotSupported;
|
|
273 |
|
|
274 |
SetDfcQ(((DTestResManLddFactory*)iDevice)->iLddQue);
|
|
275 |
iMsgQ.Receive();
|
|
276 |
return KErrNone;
|
|
277 |
}
|
|
278 |
|
|
279 |
/** Process a message for this logical channel */
|
|
280 |
void DTestResManLdd::HandleMsg(TMessageBase* aMsg)
|
|
281 |
{
|
|
282 |
TThreadMessage& m=*(TThreadMessage*)aMsg;
|
|
283 |
TInt id=m.iValue;
|
|
284 |
|
|
285 |
if (id==(TInt)ECloseMsg)
|
|
286 |
{
|
|
287 |
// Channel close.
|
|
288 |
m.Complete(KErrNone,EFalse);
|
|
289 |
return;
|
|
290 |
}
|
|
291 |
else if (id==KMaxTInt)
|
|
292 |
{
|
|
293 |
// DoCancel
|
|
294 |
m.Complete(KErrNone,ETrue);
|
|
295 |
return;
|
|
296 |
}
|
|
297 |
else if (id<0)
|
|
298 |
{
|
|
299 |
// DoRequest
|
|
300 |
TRequestStatus* pS=(TRequestStatus*)m.Ptr0();
|
|
301 |
TInt r=DoRequest(~id,pS,m.Ptr1(),m.Ptr2());
|
|
302 |
if (r!=KErrNone)
|
|
303 |
Kern::RequestComplete(iClientThreadPtr,pS,r);
|
|
304 |
m.Complete(KErrNone,ETrue);
|
|
305 |
}
|
|
306 |
else
|
|
307 |
{
|
|
308 |
// DoControl
|
|
309 |
TInt r=DoControl(id,m.Ptr0(),m.Ptr1());
|
|
310 |
m.Complete(r,ETrue);
|
|
311 |
}
|
|
312 |
}
|
|
313 |
|
|
314 |
/**
|
|
315 |
Process synchronous 'control' requests
|
|
316 |
*/
|
|
317 |
TInt DTestResManLdd::DoControl(TInt aFunction, TAny* /*a1*/, TAny* /*a2*/)
|
|
318 |
{
|
|
319 |
TInt r = KErrNone;
|
|
320 |
|
|
321 |
switch(aFunction)
|
|
322 |
{
|
|
323 |
case RTestResMan::ERegisterClient:
|
|
324 |
{
|
|
325 |
Kern::Printf("RTestResMan::ERegisterClient");
|
|
326 |
if(iResClientId!=0)
|
|
327 |
{
|
|
328 |
r = KErrInUse;
|
|
329 |
break;
|
|
330 |
}
|
|
331 |
if(((DTestResManLddFactory*)iDevice)->iResourceClientRegisterCount==0)
|
|
332 |
{
|
|
333 |
r = PowerResourceManager::RegisterClient(iResClientId, KResClientName1);
|
|
334 |
}
|
|
335 |
else if(((DTestResManLddFactory*)iDevice)->iResourceClientRegisterCount==1)
|
|
336 |
{
|
|
337 |
r = PowerResourceManager::RegisterClient(iResClientId, KResClientName2);
|
|
338 |
}
|
|
339 |
else
|
|
340 |
r = KErrInUse;
|
|
341 |
|
|
342 |
(((DTestResManLddFactory*)iDevice)->iResourceClientRegisterCount)++;
|
|
343 |
|
|
344 |
break;
|
|
345 |
}
|
|
346 |
case RTestResMan::EDeRegisterClient:
|
|
347 |
{
|
|
348 |
Kern::Printf("RTestResMan::EDeRegisterClient");
|
|
349 |
if(iResClientId==0)
|
|
350 |
{
|
|
351 |
r = KErrArgument;
|
|
352 |
break;
|
|
353 |
}
|
|
354 |
r = PowerResourceManager::DeRegisterClient(iResClientId);
|
|
355 |
break;
|
|
356 |
}
|
|
357 |
case RTestResMan::EPrintResourceInfo:
|
|
358 |
{
|
|
359 |
Kern::Printf("RTestResMan::EPrintResourceInfo");
|
|
360 |
TPowerResourceInfoBuf01 info;
|
|
361 |
info.SetLength(0);
|
|
362 |
Kern::Printf("EPrintResourceInfo");
|
|
363 |
r = PowerResourceManager::GetResourceInfo(iResClientId,KResourceId,&info);
|
|
364 |
Kern::Printf("EPrintResourceInfo:%S", info().iResourceName);
|
|
365 |
break;
|
|
366 |
}
|
|
367 |
default:
|
|
368 |
{
|
|
369 |
r = KErrNotSupported;
|
|
370 |
break;
|
|
371 |
}
|
|
372 |
}
|
|
373 |
return r;
|
|
374 |
|
|
375 |
}
|
|
376 |
|
|
377 |
|
|
378 |
TInt DTestResManLdd::DoRequest(TInt aReqNo, TRequestStatus* aStatus, TAny* /*a1*/, TAny* /*a2*/)
|
|
379 |
{
|
|
380 |
TInt r = KErrNone;
|
|
381 |
|
|
382 |
if(r != KErrNone)
|
|
383 |
Kern::RequestComplete(iClientThreadPtr, aStatus, r);
|
|
384 |
switch(aReqNo)
|
|
385 |
{
|
|
386 |
case RTestResMan::EWaitAndChangeResource:
|
|
387 |
// Queue a dfc which wait for the semaphore and then call ChangeResourceState
|
|
388 |
Kern::Printf("RTestResMan::EWaitAndChangeResource 0x%x [#1.1]", &(Kern::CurrentThread().iNThread));
|
|
389 |
if(iStatus)
|
|
390 |
{
|
|
391 |
r = KErrInUse;
|
|
392 |
break;
|
|
393 |
}
|
|
394 |
iStatus = aStatus;
|
|
395 |
|
|
396 |
iWaitAndChangeResourceDfc = new TDfc(WaitAndChangeResourceDfcFn, this,
|
|
397 |
((DTestResManLddFactory*)iDevice)->iLddHelperQue, 5);
|
|
398 |
iWaitAndChangeResourceDfc->Enque();
|
|
399 |
|
|
400 |
break;
|
|
401 |
case RTestResMan::EChangeResourceAndSignal:
|
|
402 |
// call ChangeResourceState and signal the semaphore
|
|
403 |
Kern::Printf("RTestResMan::EChangeResourceAndSignal 0x%x [#1.2]", &(Kern::CurrentThread().iNThread));
|
|
404 |
if(iStatus)
|
|
405 |
{
|
|
406 |
r = KErrInUse;
|
|
407 |
break;
|
|
408 |
}
|
|
409 |
iStatus = aStatus;
|
|
410 |
PowerResourceManager::ChangeResourceState(iResClientId, KResourceId, KResourceMin,
|
|
411 |
((DTestResManLddFactory*)iDevice)->iAsyncResourceCallback);
|
|
412 |
((DTestResManLddFactory*)iDevice)->iCallbackState = DTestResManLddFactory::ESignallerCallback;
|
|
413 |
|
|
414 |
break;
|
|
415 |
default:
|
|
416 |
break;
|
|
417 |
}
|
|
418 |
return r;
|
|
419 |
}
|