|
1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include "HWRMReservationHandler.h" |
|
21 #include "HWRMtrace.h" |
|
22 #include "HWRMClientServer.h" // panic ref |
|
23 #include "HWRMService.h" |
|
24 |
|
25 |
|
26 // EXTERNAL DATA STRUCTURES |
|
27 // None |
|
28 |
|
29 // EXTERNAL FUNCTION PROTOTYPES |
|
30 // None |
|
31 |
|
32 // CONSTANTS |
|
33 // None |
|
34 |
|
35 // MACROS |
|
36 // None |
|
37 |
|
38 // LOCAL CONSTANTS AND MACROS |
|
39 _LIT( KPanicCategory, "HWRMReservationHandler" ); |
|
40 |
|
41 // MODULE DATA STRUCTURES |
|
42 // None |
|
43 |
|
44 // LOCAL FUNCTION PROTOTYPES |
|
45 // None |
|
46 |
|
47 // FORWARD DECLARATIONS |
|
48 // None |
|
49 |
|
50 // ============================= LOCAL FUNCTIONS =============================== |
|
51 |
|
52 // ============================ MEMBER FUNCTIONS =============================== |
|
53 |
|
54 // ----------------------------------------------------------------------------- |
|
55 // CHWRMReservationHandler::CHWRMReservationHandler |
|
56 // C++ constructor |
|
57 // ----------------------------------------------------------------------------- |
|
58 // |
|
59 CHWRMReservationHandler::CHWRMReservationHandler(TInt aSubResourceCount) |
|
60 : iSubResourceCount(aSubResourceCount) |
|
61 { |
|
62 COMPONENT_TRACE1(_L( "HWRM Server - CHWRMReservationHandler::CHWRMReservationHandler()" ) ); |
|
63 COMPONENT_TRACE1(_L( "HWRM Server - CHWRMReservationHandler::CHWRMReservationHandler - return" )); |
|
64 } |
|
65 |
|
66 // ----------------------------------------------------------------------------- |
|
67 // CHWRMReservationHandler::NewL |
|
68 // Two-phased constructor. |
|
69 // ----------------------------------------------------------------------------- |
|
70 // |
|
71 CHWRMReservationHandler* CHWRMReservationHandler::NewL(TInt aSubResourceCount, const TDesC& aPolicyFilename) |
|
72 { |
|
73 COMPONENT_TRACE2(_L( "HWRM Server - CHWRMReservationHandler::NewL(%d)" ), aSubResourceCount ); |
|
74 |
|
75 CHWRMReservationHandler* self = new( ELeave ) CHWRMReservationHandler(aSubResourceCount); |
|
76 |
|
77 CleanupStack::PushL( self ); |
|
78 |
|
79 self->ConstructL(aPolicyFilename); |
|
80 |
|
81 CleanupStack::Pop(); |
|
82 |
|
83 COMPONENT_TRACE2(_L( "HWRM Server - CHWRMReservationHandler::NewL - return 0x%x" ), self ); |
|
84 |
|
85 return self; |
|
86 } |
|
87 |
|
88 // ----------------------------------------------------------------------------- |
|
89 // CHWRMServer::ConstructL |
|
90 // Symbian 2nd phase constructor can leave. |
|
91 // ----------------------------------------------------------------------------- |
|
92 // |
|
93 void CHWRMReservationHandler::ConstructL(const TDesC& aPolicyFilename) |
|
94 { |
|
95 COMPONENT_TRACE1(_L( "HWRM Server - CHWRMReservationHandler::ConstructL()" ) ); |
|
96 |
|
97 // Initialize subtarget array with NULLS and create iAllMask |
|
98 iAllMask = 0x0; |
|
99 for ( TInt i=0; i < iSubResourceCount; i++ ) |
|
100 { |
|
101 iReservations.AppendL(NULL); |
|
102 |
|
103 iAllMask <<= 1; // initial shift is ignored as mask is zero on first loop |
|
104 iAllMask |= 0x1; |
|
105 } |
|
106 |
|
107 // Initialize owned pointers |
|
108 iPolicy = CHWRMPolicy::NewL(aPolicyFilename); |
|
109 |
|
110 // Consider all targets active by default |
|
111 iActiveMask = iAllMask; |
|
112 |
|
113 COMPONENT_TRACE1(_L( "HWRM Server - CHWRMReservationHandler::ConstructL - return" ) ); |
|
114 } |
|
115 |
|
116 // --------------------------------------------------------- |
|
117 // Destructor |
|
118 // --------------------------------------------------------- |
|
119 // |
|
120 CHWRMReservationHandler::~CHWRMReservationHandler() |
|
121 { |
|
122 COMPONENT_TRACE1(_L( "HWRM Server - CHWRMReservationHandler::~CHWRMReservationHandler()" ) ); |
|
123 |
|
124 delete iPolicy; |
|
125 iPolicy = NULL; |
|
126 |
|
127 // destroy any reservations |
|
128 for ( TInt i=0; i < iReservations.Count(); i++ ) |
|
129 { |
|
130 while ( iReservations[i] ) |
|
131 { |
|
132 TReservationData* deleteData = iReservations[i]; |
|
133 iReservations[i] = iReservations[i]->iSuspendedData; |
|
134 delete deleteData; |
|
135 } |
|
136 } |
|
137 iReservations.ResetAndDestroy(); |
|
138 |
|
139 COMPONENT_TRACE1(_L( "HWRM Server - CHWRMReservationHandler::~CHWRMReservationHandler - return" ) ); |
|
140 } |
|
141 |
|
142 |
|
143 // ----------------------------------------------------------------------------- |
|
144 // CHWRMReservationHandler::ReserveL |
|
145 // Reserves resource |
|
146 // ----------------------------------------------------------------------------- |
|
147 // |
|
148 TInt CHWRMReservationHandler::ReserveL(const RMessagePtr2& aMessage, TBool aForceNoCCoeEnv, CHWRMService* aServiceCallback, TInt aSubResourceMask) |
|
149 { |
|
150 COMPONENT_TRACE5(_L( "HWRM Server - CHWRMReservationHandler::ReserveL(0x%x, 0x%x, 0x%x, 0x%x)" ), aMessage.Handle(), aForceNoCCoeEnv, aServiceCallback, aSubResourceMask ); |
|
151 |
|
152 __ASSERT_ALWAYS(aServiceCallback, User::Panic(KPanicCategory, EPanicBadHandle)); |
|
153 |
|
154 TBool trusted(EFalse); |
|
155 TInt subMask(aSubResourceMask); // Mask of subresources that caller wants to reserve |
|
156 TInt reservedSuspendedMask(0x0); // Mask of subresources reserved in suspended mode |
|
157 TInt alreadyReservedMask(0x0); // Mask of subresources already reserved by this caller |
|
158 |
|
159 // Get priority |
|
160 TInt priority = iPolicy->GetPriority(aMessage.SecureId(), trusted); |
|
161 |
|
162 // If no CCoeEnv is forced, check that client is trusted |
|
163 if ( aForceNoCCoeEnv && !trusted ) |
|
164 { |
|
165 User::Leave(KErrAccessDenied); |
|
166 } |
|
167 |
|
168 // New reservation data(s) might be required for suspended reservations |
|
169 // If multiple targets are suspended, datas create linked list |
|
170 TReservationData* newDatas = NULL; |
|
171 TInt newDataCount(0); |
|
172 |
|
173 // If whole resource specified, use iAllMask |
|
174 if ( aSubResourceMask == KHWRMAllSubResources) |
|
175 { |
|
176 subMask = iAllMask; |
|
177 } |
|
178 |
|
179 // Check reservations of all desired subtargets |
|
180 TInt compareMask(0x1); |
|
181 for ( TInt i=0; i < iSubResourceCount; i++ ) |
|
182 { |
|
183 if ( (compareMask & subMask) ) |
|
184 { |
|
185 TReservationData* reservationCheckData = iReservations[i]; |
|
186 |
|
187 // Check if caller is already reserving this subresource |
|
188 while( reservationCheckData && !(alreadyReservedMask & compareMask) ) |
|
189 { |
|
190 if ( reservationCheckData->iCallback == aServiceCallback ) |
|
191 { |
|
192 alreadyReservedMask |= compareMask; |
|
193 COMPONENT_TRACE2(_L( "HWRM Server - CHWRMReservationHandler::ReserveL - Duplicate reservation for subresource 0x%x - Ignoring" ), compareMask ); |
|
194 } |
|
195 reservationCheckData = reservationCheckData->iSuspendedData; |
|
196 } |
|
197 |
|
198 // If caller is not yet reserving this subresource, reserve it. |
|
199 if ( !(alreadyReservedMask & compareMask) ) |
|
200 { |
|
201 if ( iReservations[i] && iReservations[i]->iPriority <= priority ) |
|
202 { |
|
203 // Add to reserve suspended mask |
|
204 reservedSuspendedMask |= compareMask; |
|
205 } |
|
206 |
|
207 // Create data objects at this point so that any OOM leaves |
|
208 // will not cause reservation state to be uncertain |
|
209 TReservationData* newOne = new (ELeave) TReservationData; |
|
210 CleanupStack::PushL(newOne); |
|
211 ++newDataCount; |
|
212 |
|
213 if ( !newDatas ) |
|
214 { |
|
215 newDatas = newOne; |
|
216 } |
|
217 else |
|
218 { |
|
219 // There is already at least one newDatas, so link the new one up with old ones |
|
220 TReservationData* compareData = newDatas; |
|
221 while ( compareData->iSuspendedData ) |
|
222 { |
|
223 compareData = compareData->iSuspendedData; |
|
224 } |
|
225 compareData->iSuspendedData = newOne; |
|
226 } |
|
227 } |
|
228 } |
|
229 |
|
230 compareMask <<= 1; |
|
231 } |
|
232 |
|
233 // Pop the TReservationData structures from the cleanup stack. This can be done here |
|
234 // because no leaving functions are called in the following loop. |
|
235 CleanupStack::Pop(newDataCount); |
|
236 |
|
237 // Now we have done all memory allocations we need to, |
|
238 // so we can reserve specified subresources with no risk of OOM errors |
|
239 // and therefore be sure that reservations array is never left in undetermined state. |
|
240 compareMask = 0x1; |
|
241 for ( TInt j=0; j < iSubResourceCount; j++ ) |
|
242 { |
|
243 if ( (compareMask & subMask) && !(alreadyReservedMask & compareMask) ) |
|
244 { |
|
245 // if things work right, this assert will never trigger |
|
246 __ASSERT_ALWAYS(newDatas, User::Panic(KPanicCategory, EPanicBadHandle)); |
|
247 |
|
248 TReservationData* nextData = newDatas->iSuspendedData; |
|
249 newDatas->iCallback = aServiceCallback; |
|
250 newDatas->iPriority = priority; |
|
251 |
|
252 if ( compareMask & reservedSuspendedMask ) |
|
253 { |
|
254 // Reserve suspended, i.e. go through list until next one has lower priority |
|
255 TReservationData* priorityCheckData = iReservations[j]->iSuspendedData; |
|
256 TReservationData* priorityPreviousData = iReservations[j]; |
|
257 |
|
258 while ( priorityCheckData |
|
259 && priorityCheckData->iPriority <= priority ) |
|
260 { |
|
261 priorityPreviousData = priorityCheckData; |
|
262 priorityCheckData = priorityCheckData->iSuspendedData; |
|
263 } |
|
264 |
|
265 // insert new data |
|
266 newDatas->iSuspendedData = priorityCheckData; |
|
267 priorityPreviousData->iSuspendedData = newDatas; |
|
268 } |
|
269 else |
|
270 { |
|
271 // Reserve as active, i.e. first in list |
|
272 newDatas->iSuspendedData = iReservations[j]; |
|
273 iReservations[j] = newDatas; |
|
274 |
|
275 // Notify callback of current reserver, if any |
|
276 if ( newDatas->iSuspendedData ) |
|
277 { |
|
278 (newDatas->iSuspendedData->iCallback)->SuspendSubResource(j); |
|
279 } |
|
280 } |
|
281 |
|
282 newDatas = nextData; |
|
283 } |
|
284 |
|
285 compareMask <<= 1; |
|
286 } |
|
287 |
|
288 COMPONENT_TRACE2(_L( "HWRM Server - CHWRMReservationHandler::ReserveL - return: 0x%x" ), reservedSuspendedMask ); |
|
289 |
|
290 return reservedSuspendedMask; |
|
291 } |
|
292 |
|
293 |
|
294 // ----------------------------------------------------------------------------- |
|
295 // CHWRMReservationHandler::Release |
|
296 // Releases resource |
|
297 // ----------------------------------------------------------------------------- |
|
298 // |
|
299 TInt CHWRMReservationHandler::Release(CHWRMService* aServiceCallback, TInt aSubResourceMask) |
|
300 { |
|
301 COMPONENT_TRACE3(_L( "HWRM Server - CHWRMReservationHandler::Release(0x%x, 0x%x)" ), aServiceCallback, aSubResourceMask ); |
|
302 |
|
303 __ASSERT_ALWAYS(aServiceCallback, User::Panic(KPanicCategory, EPanicBadHandle)); |
|
304 |
|
305 TInt unreservedMask(0x0); // Return value, mask of all subresources that are unreserved. |
|
306 TInt subMask(aSubResourceMask); |
|
307 |
|
308 // If whole resource specified, use iAllMask |
|
309 if ( aSubResourceMask == KHWRMAllSubResources) |
|
310 { |
|
311 subMask = iAllMask; |
|
312 } |
|
313 |
|
314 // Check reservations of all desired subtargets |
|
315 TInt compareMask(0x1); |
|
316 for ( TInt i=0; i < iSubResourceCount; i++ ) |
|
317 { |
|
318 if ( compareMask & subMask ) |
|
319 { |
|
320 TReservationData* releasedService = NULL; |
|
321 // Check if released client is active or suspended (or not reserved at all) |
|
322 if ( iReservations[i] && iReservations[i]->iCallback == aServiceCallback ) |
|
323 { |
|
324 // Released service was active, remove it |
|
325 COMPONENT_TRACE3(_L( "HWRM Server - CHWRMReservationHandler::Release - Releasing: 0x%x, suspendedData = 0x%x" ), iReservations[i], iReservations[i]->iSuspendedData ); |
|
326 releasedService = iReservations[i]; |
|
327 iReservations[i] = iReservations[i]->iSuspendedData; |
|
328 delete releasedService; |
|
329 releasedService = NULL; |
|
330 |
|
331 // Reactivate first service on list if there still is one |
|
332 if ( iReservations[i] && iReservations[i]->iCallback ) |
|
333 { |
|
334 (iReservations[i]->iCallback)->ResumeSubResource(i); |
|
335 } |
|
336 } |
|
337 else if ( iReservations[i] ) |
|
338 { |
|
339 // Released service is not active so it is either suspended or not reserved at all. |
|
340 // Find the service from suspended services |
|
341 TReservationData* checkData = iReservations[i]->iSuspendedData; |
|
342 TReservationData* previousData = iReservations[i]; |
|
343 |
|
344 while ( checkData && checkData->iCallback != aServiceCallback ) |
|
345 { |
|
346 previousData = checkData; |
|
347 checkData = checkData->iSuspendedData; |
|
348 } |
|
349 |
|
350 // Reset links and delete released service. |
|
351 // checkData is either NULL or the calling service at this point. |
|
352 // If it is NULL, no need to delete anything, the service was not reserved. |
|
353 if ( checkData ) |
|
354 { |
|
355 COMPONENT_TRACE3(_L( "HWRM Server - CHWRMReservationHandler::Release - Releasing: 0x%x, suspendedData = 0x%x" ), checkData, checkData->iSuspendedData ); |
|
356 previousData->iSuspendedData = checkData->iSuspendedData; |
|
357 delete checkData; |
|
358 checkData = NULL; |
|
359 } |
|
360 else |
|
361 { |
|
362 COMPONENT_TRACE1(_L( "HWRM Server - CHWRMReservationHandler::Release - Reservation not found, releasing nothing" ) ); |
|
363 } |
|
364 } |
|
365 else |
|
366 { |
|
367 // Nothing reserved |
|
368 } |
|
369 |
|
370 } |
|
371 |
|
372 // update return value if no more reservations for this subresource |
|
373 if ( !iReservations[i] ) |
|
374 { |
|
375 unreservedMask |= compareMask; |
|
376 } |
|
377 |
|
378 compareMask <<= 1; |
|
379 } |
|
380 |
|
381 COMPONENT_TRACE2(_L( "HWRM Server - CHWRMReservationHandler::Release - return: 0x%x" ), unreservedMask ); |
|
382 |
|
383 return unreservedMask; |
|
384 } |
|
385 |
|
386 // ----------------------------------------------------------------------------- |
|
387 // CHWRMReservationHandler::IsReserved |
|
388 // ----------------------------------------------------------------------------- |
|
389 // |
|
390 TBool CHWRMReservationHandler::IsReserved(const CHWRMService* aServiceCallback, TInt aSubResourceMask) const |
|
391 { |
|
392 COMPONENT_TRACE3(_L( "HWRM Server - CHWRMReservationHandler::IsReserved(0x%x, 0x%x)" ), aServiceCallback, aSubResourceMask ); |
|
393 |
|
394 __ASSERT_ALWAYS(aServiceCallback != NULL, User::Panic(KPanicCategory, EPanicBadHandle)); |
|
395 |
|
396 TBool retval(EFalse); |
|
397 TInt subMask(aSubResourceMask); |
|
398 |
|
399 // If whole resource specified, use iAllMask |
|
400 if ( aSubResourceMask == KHWRMAllSubResources) |
|
401 { |
|
402 subMask = iAllMask; |
|
403 } |
|
404 |
|
405 // Check if any of the specified subresources is reserved to somebody other than caller |
|
406 TInt compareMask(0x1); |
|
407 for ( TInt i=0; i < iSubResourceCount && !retval; i++ ) |
|
408 { |
|
409 if ( (compareMask & subMask) && iReservations[i] && iReservations[i]->iCallback != aServiceCallback ) |
|
410 { |
|
411 retval = ETrue; |
|
412 } |
|
413 |
|
414 compareMask <<= 1; |
|
415 } |
|
416 |
|
417 |
|
418 COMPONENT_TRACE2(_L( "HWRM Server - CHWRMReservationHandler::IsReserved - return: 0x%x" ), retval ); |
|
419 |
|
420 return retval; |
|
421 } |
|
422 |
|
423 |
|
424 // ----------------------------------------------------------------------------- |
|
425 // CHWRMReservationHandler::GetUnreservedTargets |
|
426 // Returns target mask containing unreserved targets. |
|
427 // ----------------------------------------------------------------------------- |
|
428 // |
|
429 TInt CHWRMReservationHandler::GetUnreservedTargets() |
|
430 { |
|
431 TInt compareMask(0x1); |
|
432 TInt returnMask(0x0); |
|
433 for ( TInt i=0; i < iSubResourceCount; i++ ) |
|
434 { |
|
435 if ( !iReservations[i] ) |
|
436 { |
|
437 returnMask |= compareMask; |
|
438 } |
|
439 |
|
440 compareMask <<= 1; |
|
441 } |
|
442 |
|
443 return returnMask; |
|
444 } |
|
445 |
|
446 |
|
447 // ----------------------------------------------------------------------------- |
|
448 // CHWRMReservationHandler::UpdateActiveMask |
|
449 // Active targets mask indicates targets that are active (i.e. are not stripped |
|
450 // from target by target modifier plugin). |
|
451 // This is obviously not realtime information, but we are only interested in |
|
452 // targets that someone wants to modify anyway. I.e. if nobody wants to |
|
453 // adjust the state of some target after device state modification, we do not need |
|
454 // to know about it anyway. |
|
455 // ----------------------------------------------------------------------------- |
|
456 // |
|
457 void CHWRMReservationHandler::UpdateActiveMask(TInt aOriginalTarget, TInt aModifiedTarget, CHWRMService* aUpdater) |
|
458 { |
|
459 // All modified targets are activated |
|
460 // All targets that were dropped from original target are deactivated. |
|
461 TInt newActiveMask( (iActiveMask | aModifiedTarget) & ~(aOriginalTarget & ~aModifiedTarget) ); |
|
462 |
|
463 COMPONENT_TRACE3(_L( "HWRM Server - CHWRMReservationHandler::UpdateActiveMask - Updated mask: 0x%x, old mask: 0x%x" ), newActiveMask, iActiveMask ); |
|
464 |
|
465 // Get bits that are different in old and new mask |
|
466 TInt differenceMask( iActiveMask ^ newActiveMask ); |
|
467 |
|
468 iActiveMask = newActiveMask; |
|
469 |
|
470 // Notify sessions with active reservation on modified targets |
|
471 if ( differenceMask ) |
|
472 { |
|
473 COMPONENT_TRACE1(_L( "HWRM Server - CHWRMReservationHandler::UpdateActiveMask - Notifying active sessions" ) ); |
|
474 TInt compareMask(0x1); |
|
475 for ( TInt i=0; i < iSubResourceCount; i++ ) |
|
476 { |
|
477 if ( compareMask & differenceMask ) |
|
478 { |
|
479 TBool activate(compareMask & newActiveMask); |
|
480 |
|
481 // Skip activation notifications to whoever called this method as they are redundant. |
|
482 // Deactivations are notified in any case. |
|
483 if ( iReservations[i] && iReservations[i]->iCallback && (iReservations[i]->iCallback != aUpdater || !activate) ) |
|
484 { |
|
485 (iReservations[i]->iCallback)->ActivateSubResource(i, activate); |
|
486 } |
|
487 } |
|
488 |
|
489 compareMask <<= 1; |
|
490 } |
|
491 } |
|
492 |
|
493 } |
|
494 |
|
495 // ----------------------------------------------------------------------------- |
|
496 // CHWRMReservationHandler::GetActiveReserver |
|
497 // Returns active reserver for the specified target |
|
498 // ----------------------------------------------------------------------------- |
|
499 // |
|
500 CHWRMService* CHWRMReservationHandler::GetActiveReserver(TInt aTarget) |
|
501 { |
|
502 CHWRMService* retval = NULL; |
|
503 |
|
504 if ( iReservations[aTarget] ) |
|
505 { |
|
506 retval = iReservations[aTarget]->iCallback; |
|
507 } |
|
508 |
|
509 COMPONENT_TRACE2(_L( "HWRM Server - CHWRMReservationHandler::GetActiveReserver - return: 0x%x" ), retval ); |
|
510 return retval; |
|
511 } |
|
512 |
|
513 // ----------------------------------------------------------------------------- |
|
514 // CHWRMReservationHandler::GetPolicy |
|
515 // Returns policy instance |
|
516 // ----------------------------------------------------------------------------- |
|
517 // |
|
518 CHWRMPolicy* CHWRMReservationHandler::GetPolicy() |
|
519 { |
|
520 return iPolicy; |
|
521 } |
|
522 |
|
523 |
|
524 // ========================== OTHER EXPORTED FUNCTIONS ========================= |
|
525 |
|
526 // End of File |