|
1 /* |
|
2 * Copyright (c) 2006-2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Sensor server proxy for accessing specific Ssy plugin. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #ifndef SENSRVPLUGINPROXY_H |
|
20 #define SENSRVPLUGINPROXY_H |
|
21 |
|
22 #include <e32base.h> |
|
23 #include <sensrvchannelinfo.h> |
|
24 #include <ecom/implementationinformation.h> |
|
25 #include "sensrvtypes.h" |
|
26 #include "sensrvtimer.h" |
|
27 #include "sensrvresourcechannelinfo.h" |
|
28 |
|
29 class CSensrvProxyManager; |
|
30 class CSensrvTransaction; |
|
31 class CSensrvServerMediator; |
|
32 class CSensrvSsyMediator; |
|
33 class CSensrvThreadMonitor; |
|
34 class CSensrvMessage; |
|
35 class CSensrvTransactionQueue; |
|
36 class CSensrvChannel; |
|
37 class CSensrvSession; |
|
38 class CSensrvTransactionMonitor; |
|
39 |
|
40 /** |
|
41 * Manages single plugin and transactions related to it |
|
42 * |
|
43 * @since S60 5.0 |
|
44 */ |
|
45 class CSensrvPluginProxy : public CBase, |
|
46 public MSensrvTimerCallback |
|
47 { |
|
48 public: |
|
49 |
|
50 |
|
51 /** |
|
52 * Enumeration from plugin states. |
|
53 */ |
|
54 enum TPluginState |
|
55 { |
|
56 EPluginStateUninitialized, ///< SSY thread not yet initalized for the first time. |
|
57 EPluginStateThreadInitializing, ///< SSY thread started |
|
58 EPluginStateThreadInitialized, ///< SSY thread scheduler loop active |
|
59 EPluginStateLoaded, ///< SSY loaded and ready to serve |
|
60 EPluginStateUnloading, ///< SSY unloading |
|
61 EPluginStateUnloaded ///< SSY unloaded. Thread and SSY mediator are destroyed. |
|
62 }; |
|
63 |
|
64 /** |
|
65 * Two phase constructor |
|
66 * |
|
67 * @since S60 5.0 |
|
68 * @param aParent Reference to proxy manager. |
|
69 * @param aImplementationUid Implementation UID required to load plugin. |
|
70 * @return New CSensrvPluginProxy instance |
|
71 * @exception KErrNoMemory Out of memory |
|
72 */ |
|
73 static CSensrvPluginProxy* NewL(CSensrvProxyManager& aParent, |
|
74 TUid aImplementationUid); |
|
75 |
|
76 /** |
|
77 * Destructor. |
|
78 */ |
|
79 virtual ~CSensrvPluginProxy(); |
|
80 |
|
81 |
|
82 public: |
|
83 |
|
84 // public accessors |
|
85 |
|
86 /** |
|
87 * Gets pointer to SSY mediator. |
|
88 * Called from both threads. |
|
89 * |
|
90 * @since S60 5.0 |
|
91 * @return Pointer to SSY mediator. Can be NULL if plugin thread is not started. |
|
92 */ |
|
93 inline CSensrvSsyMediator* SsyMediator() { return iSsyMediator; }; |
|
94 |
|
95 /** |
|
96 * Gets pointer to server mediator. |
|
97 * Called from both threads. |
|
98 * |
|
99 * @since S60 5.0 |
|
100 * @return Pointer to server mediator. Can be NULL if plugin thread is not started. |
|
101 */ |
|
102 inline CSensrvServerMediator* ServerMediator() { return iServerMediator; }; |
|
103 |
|
104 /** |
|
105 * Gets reference to the mutex controlling access to shared resources |
|
106 * between server and plugin threads. |
|
107 * |
|
108 * @since S60 5.0 |
|
109 * @return Reference to Mutex. |
|
110 */ |
|
111 inline RMutex& Mutex() { return iMutex; }; |
|
112 |
|
113 /** |
|
114 * Gets reference to the proxy manager |
|
115 * Called from both threads. |
|
116 * |
|
117 * @since S60 5.0 |
|
118 * @return Reference to proxy manager. |
|
119 */ |
|
120 inline CSensrvProxyManager& ProxyManager() { return iProxyManager; }; |
|
121 |
|
122 /** |
|
123 * Gets plugin state. |
|
124 * Called from both threads. |
|
125 * |
|
126 * @since S60 5.0 |
|
127 * @return Current plugin state according to proxy. |
|
128 */ |
|
129 inline TPluginState PluginState() const { return iPluginState; }; |
|
130 |
|
131 /** |
|
132 * Gets proxy initialization status |
|
133 * Called from server thread |
|
134 * |
|
135 * @since S60 5.0 |
|
136 * @return Current proxy initialization status. |
|
137 */ |
|
138 inline TBool IsInitialized() const { return iIsInitialized; }; |
|
139 |
|
140 /** |
|
141 * Gets plugin implementation UID |
|
142 * Called from SSY thread. |
|
143 * |
|
144 * @since S60 5.0 |
|
145 * @return Plugin implementation UID |
|
146 */ |
|
147 inline const TUid ImplementationUid() const { return iImplementationUid; }; |
|
148 |
|
149 /** |
|
150 * Gets a reference for plugin channel info array. |
|
151 * Called from both threads. |
|
152 * |
|
153 * @since S60 5.0 |
|
154 * @return Channel info array |
|
155 */ |
|
156 inline RSensrvResourceChannelInfoList& ChannelInfoList() { return iChannelInfoList; }; |
|
157 |
|
158 /** |
|
159 * Gets a reference for plugin removed channels info array. |
|
160 * Called from both threads. |
|
161 * |
|
162 * @since S60 5.0 |
|
163 * @return Removed channels info array |
|
164 */ |
|
165 inline RSensrvResourceChannelInfoList& RemovedChannelsInfoList() { return iRemovedChannelsInfoList; }; |
|
166 |
|
167 /** |
|
168 * Gets a reference to transaction monitor. |
|
169 * Called from both threads. |
|
170 * |
|
171 * @since S60 5.0 |
|
172 * @return Transaction monitor |
|
173 */ |
|
174 inline CSensrvTransactionMonitor* TransactionMonitor() { return iTransactionMonitor; }; |
|
175 |
|
176 /** |
|
177 * Checks if proxy is marked for deletion |
|
178 * Called from server thread |
|
179 * |
|
180 * @since S60 5.0 |
|
181 * @return Current proxy deletion mark status |
|
182 */ |
|
183 inline TBool IsMarkedForDeletion() const { return iDeletionFlag; }; |
|
184 |
|
185 /** |
|
186 * Marks proxy for deletion |
|
187 * Called from server thread |
|
188 * |
|
189 * @since S60 5.0 |
|
190 */ |
|
191 inline void SetDeletionFlag() { iDeletionFlag = ETrue; }; |
|
192 |
|
193 /** |
|
194 * Notifies proxy that a transaction has been handled by plugin. |
|
195 * Called only from server thread. |
|
196 * |
|
197 * @since S60 5.0 |
|
198 * @param aTransaction The transaction that has been handled. |
|
199 */ |
|
200 void TransactionHandledAtSsy(CSensrvTransaction* aTransaction); |
|
201 |
|
202 /** |
|
203 * Initializes plugin thread and mediators. |
|
204 * Also queues load transaction. |
|
205 * Called only within server thread. |
|
206 * |
|
207 * @since S60 5.0 |
|
208 * @exception Normal Symbian error code |
|
209 */ |
|
210 void InitializePluginL(); |
|
211 |
|
212 /** |
|
213 * Handles client message. |
|
214 * Message completion responsibility transfers. |
|
215 * Called only within server thread. |
|
216 * |
|
217 * @since S60 5.0 |
|
218 * @param aMessage The message containing the client request. |
|
219 * @param aChannelId The channel id of related channel. |
|
220 */ |
|
221 void DispatchMessage( CSensrvMessage& aMessage, TSensrvChannelId aChannelId ); |
|
222 |
|
223 /** |
|
224 * Check if this proxy supports specified channel |
|
225 * Called only within server thread. |
|
226 * |
|
227 * @since S60 5.0 |
|
228 * @param aChannelId Channel id to check |
|
229 * @return ETrue if proxy does provide specified channel |
|
230 */ |
|
231 TBool IsChannelSupported(TSensrvChannelId aChannelId); |
|
232 |
|
233 |
|
234 /** |
|
235 * Gets the channel object that handles the specified channel. |
|
236 * |
|
237 * @since S60 5.0 |
|
238 * @param aChannelId The channel id of desired channel. |
|
239 * @return Pointer to the channel object handling the specified channel |
|
240 * or NULL if channel is not yet initialized. |
|
241 */ |
|
242 CSensrvChannel* GetChannelForId(TSensrvChannelId aChannelId) const; |
|
243 |
|
244 /** |
|
245 * Cleans up session related data. |
|
246 * |
|
247 * @since S60 5.0 |
|
248 * @param aSession The session that was deleted |
|
249 */ |
|
250 void SessionTerminated( CSensrvSession* aSession ); |
|
251 |
|
252 /** |
|
253 * Handles notify failure at SSY |
|
254 * |
|
255 * @since S60 5.0 |
|
256 */ |
|
257 void HandleSsyNotifyFailure(); |
|
258 |
|
259 /** |
|
260 * Transaction timeout handling. |
|
261 * |
|
262 * @since S60 5.0 |
|
263 */ |
|
264 void HandleTransactionTimeout(); |
|
265 |
|
266 /** |
|
267 * Called by thread monitor when it detects that SSY thread |
|
268 * is no longer running or has explicitly killed it. |
|
269 * |
|
270 * @since S60 5.0 |
|
271 */ |
|
272 void SsyThreadTerminated(); |
|
273 |
|
274 /** |
|
275 * Check if plugin has no clients, and start unload timer if so. |
|
276 * |
|
277 * @since S60 5.0 |
|
278 */ |
|
279 void StartUnloadTimerIfNeeded(); |
|
280 |
|
281 /** |
|
282 * Stop unload timer if it is active |
|
283 * |
|
284 * @since S60 5.0 |
|
285 */ |
|
286 inline void StopUnloadTimer() { iUnloadTimer->Cancel(); }; |
|
287 |
|
288 /** |
|
289 * Initializes plugin channel data. |
|
290 * |
|
291 * @since S60 5.0 |
|
292 * @param aImplInfo Implementation information of the channel |
|
293 * @return Normal Symbian error code |
|
294 */ |
|
295 TInt InitializeChannelData( const CImplementationInformation& aImplInfo ); |
|
296 |
|
297 /** |
|
298 * Gets a reference for plugin dynamic channel info array. |
|
299 * Called from both threads. |
|
300 * |
|
301 * @since S60 5.0 |
|
302 * @return Dynamic channel info array |
|
303 */ |
|
304 inline RSensrvResourceChannelInfoList& DynamicChannelInfoList() { return iDynamicChannelInfoList; } |
|
305 |
|
306 /** |
|
307 * Finds channel from info list that matches to given channel info |
|
308 * |
|
309 * @since S60 5.0 |
|
310 * @param aChannelInfo Channel info to match from |
|
311 * @param aChannelInfoList Channel info list to match with |
|
312 * @return Index to given list. KErrNotFound if match is not found. |
|
313 */ |
|
314 static TInt FindChannel( const TSensrvChannelInfo& aChannelInfo, |
|
315 const RSensrvResourceChannelInfoList& aChannelInfoList ); |
|
316 |
|
317 /** |
|
318 * Finds channel from info list that matches to given channel info |
|
319 * |
|
320 * @since S60 5.0 |
|
321 * @param aChannelInfoList Channel info list to match from |
|
322 * @param aChannelInfo Channel info to match with |
|
323 * @return Index to given list. KErrNotFound if match is not found. |
|
324 */ |
|
325 static TInt FindChannel( const RSensrvChannelInfoList& aChannelInfoList, |
|
326 const TSensrvChannelInfo& aChannelInfo ); |
|
327 |
|
328 /** |
|
329 * Increments dynamic channel change listener count |
|
330 * |
|
331 * @since S60 5.0 |
|
332 */ |
|
333 void AddChannelChangeListener(); |
|
334 |
|
335 /** |
|
336 * Decrements dynamic channel change listener count |
|
337 * |
|
338 * @since S60 5.0 |
|
339 */ |
|
340 void RemoveChannelChangeListener(); |
|
341 |
|
342 /** |
|
343 * Checks SSY load fail indicator |
|
344 * |
|
345 * @since S60 5.0 |
|
346 * @return ETrue if loading of previous SSY failed. Otherwise EFalse. |
|
347 */ |
|
348 inline TBool PreviousSsyLoadFailed() const { return iPreviousSsyLoadFailed; } |
|
349 |
|
350 // From base classes |
|
351 |
|
352 /** |
|
353 * Called when the unload timer fires. |
|
354 * Cleanus up the SSY |
|
355 * |
|
356 * @since S60 5.0 |
|
357 * @param aTimerId Timer identifier to id which timer fired. |
|
358 */ |
|
359 void TimerFired(TInt aTimerId); |
|
360 |
|
361 /** |
|
362 * Cleans up the plugin, its thread and SSY mediator. |
|
363 * Only called from server thread. |
|
364 * |
|
365 * @since S60 5.0 |
|
366 */ |
|
367 void CleanupPlugin(); |
|
368 |
|
369 |
|
370 private: // Member functions |
|
371 |
|
372 /** |
|
373 * C++ constructor |
|
374 * |
|
375 * @param aParent Reference to proxy manager. |
|
376 * @param aImplementationUid Implementation UID required to load plugin. |
|
377 */ |
|
378 CSensrvPluginProxy(CSensrvProxyManager& aParent, |
|
379 TUid aImplementationUid); |
|
380 |
|
381 /** |
|
382 * Second phase of construction. |
|
383 */ |
|
384 void ConstructL(); |
|
385 |
|
386 /** |
|
387 * Queues new transaction to proxy's queue. |
|
388 * Ownership of the transaction transfers to queue. |
|
389 * If queue is empty, also calls HandleNextTransaction. |
|
390 * Only called from server thread. |
|
391 * |
|
392 * @since S60 5.0 |
|
393 * @param aTransaction The transaction to queue |
|
394 * @param aLast If ETrue, transaction is added to the end of the queue |
|
395 * @return KErrNoMemory Out of memory |
|
396 */ |
|
397 TInt QueueTransaction(CSensrvTransaction* aTransaction, TBool aLast); |
|
398 |
|
399 /** |
|
400 * Handle next queued transaction in specified queue. |
|
401 * If there was an error, transaction is completed with error. |
|
402 * Only called from server thread. |
|
403 * |
|
404 * @since S60 5.0 |
|
405 */ |
|
406 void HandleNextTransaction(); |
|
407 |
|
408 /** |
|
409 * Completes transaction after it has been handled by SSY. |
|
410 * Only called from server thread. |
|
411 * |
|
412 * @since S60 5.0 |
|
413 * @param aTransaction The transaction to complete |
|
414 */ |
|
415 void CompleteTransaction(CSensrvTransaction* aTransaction); |
|
416 |
|
417 /** |
|
418 * Gets channel info matching specified channel id |
|
419 * |
|
420 * @since S60 5.0 |
|
421 * @param aChannelId Channel id |
|
422 * @return Channel info. This is valid until plugin is reloaded. |
|
423 * NULL if not found |
|
424 */ |
|
425 TSensrvResourceChannelInfo* GetChannelInfoForId( TSensrvChannelId aChannelId ); |
|
426 |
|
427 /** |
|
428 * Sets plugin state. |
|
429 * Called only from server thread. |
|
430 * |
|
431 * @since S60 5.0 |
|
432 * @param aState New state. |
|
433 */ |
|
434 void SetPluginState(TPluginState aState); |
|
435 |
|
436 /** |
|
437 * Gets listener count of all channels |
|
438 * Called only from server thread. |
|
439 * |
|
440 * @since S60 5.0 |
|
441 * @return the total listener count. |
|
442 */ |
|
443 TInt GetTotalListenerCount(); |
|
444 |
|
445 /** |
|
446 * Grabs raw channel info data from SSY mediator and |
|
447 * Updates channel info and removed channel info lists. |
|
448 * |
|
449 * @since S60 5.0 |
|
450 */ |
|
451 TInt UpdateChannelInfoLists(); |
|
452 |
|
453 /** |
|
454 * SSY thread main method. |
|
455 * Only called from SSY thread. |
|
456 * |
|
457 * @since S60 5.0 |
|
458 * @param aParameter CSensrvPluginProxy pointer. |
|
459 */ |
|
460 static TInt SsyThreadFunction(TAny* aParameter); |
|
461 |
|
462 /** |
|
463 * Cleanupitem callback for plugin initialization |
|
464 * |
|
465 * @since S60 5.0 |
|
466 * @param aAny The proxy pointer |
|
467 */ |
|
468 static void CleanupInitializePlugin( TAny* aAny ); |
|
469 |
|
470 /** |
|
471 * Enqueues delayd initialization if load queue is empty |
|
472 * Only called from server thread. |
|
473 * |
|
474 * @since S60 5.0 |
|
475 * @return KErrNoMemory Out of memory |
|
476 */ |
|
477 TInt EnqueueDelayedInitializeTransaction(); |
|
478 |
|
479 private: // Member variables |
|
480 |
|
481 /** |
|
482 * Stores the state of plugin controlled by this proxy. |
|
483 */ |
|
484 TPluginState iPluginState; |
|
485 |
|
486 /** |
|
487 * Reference to proxy manager |
|
488 * Own. |
|
489 */ |
|
490 CSensrvProxyManager& iProxyManager; |
|
491 |
|
492 /** |
|
493 * Implementation UID needed to load plugin. |
|
494 */ |
|
495 TUid iImplementationUid; |
|
496 |
|
497 /** |
|
498 * Active channels provided by the plugin |
|
499 */ |
|
500 RSensrvResourceChannelInfoList iChannelInfoList; |
|
501 |
|
502 /** |
|
503 * Channels that have been provided by the plugin at one time, |
|
504 * but have subsequently been removed. Removed channels |
|
505 * are stored so that original channel ids can be recovered should they |
|
506 * be re-registered. |
|
507 */ |
|
508 RSensrvResourceChannelInfoList iRemovedChannelsInfoList; |
|
509 |
|
510 /** |
|
511 * Holds load transaction and channel related transactions until |
|
512 * plugin has finished loading. |
|
513 * Queued transactions are owned by the queue. |
|
514 * Own. |
|
515 */ |
|
516 CSensrvTransactionQueue* iLoadWaitQueue; |
|
517 |
|
518 /** |
|
519 * List of channels opened at least for one client. |
|
520 * Own. (the channels in list) |
|
521 */ |
|
522 RPointerArray<CSensrvChannel> iChannelList; |
|
523 |
|
524 /** |
|
525 * Mediator used to pass transactions from main thread to plugin thread |
|
526 * Own technically, but actual deletion is done in static SsyThreadFunction, |
|
527 * after SSY active scheduler loop terminates. |
|
528 */ |
|
529 CSensrvSsyMediator* iSsyMediator; |
|
530 |
|
531 /** |
|
532 * Mediator used to pass transactions from plugin thread to main thread |
|
533 * Own. |
|
534 */ |
|
535 CSensrvServerMediator* iServerMediator; |
|
536 |
|
537 /** |
|
538 * Used to control access to data shared by server and plugin threads. |
|
539 */ |
|
540 RMutex iMutex; |
|
541 |
|
542 /** |
|
543 * Indicates that this proxy has finished its initialization. |
|
544 * Note that this doesn't mean channels are actually available or |
|
545 * that plugins are loaded successfully. |
|
546 */ |
|
547 TBool iIsInitialized; |
|
548 |
|
549 /** |
|
550 * SSY thread handle |
|
551 */ |
|
552 RThread iSsyThread; |
|
553 |
|
554 /** |
|
555 * Monitors transactions |
|
556 * Own. |
|
557 */ |
|
558 CSensrvTransactionMonitor* iTransactionMonitor; |
|
559 |
|
560 /** |
|
561 * Monitors plugin threads existence. |
|
562 * Own. |
|
563 */ |
|
564 CSensrvThreadMonitor* iThreadMonitor; |
|
565 |
|
566 /** |
|
567 * Timer used to unload plugin if it has no clients |
|
568 * for a certain period of time. |
|
569 * Own. |
|
570 */ |
|
571 CSensrvTimer* iUnloadTimer; |
|
572 |
|
573 /** |
|
574 * If true, proxy is marked for deletion and will be deleted soon. |
|
575 */ |
|
576 TBool iDeletionFlag; |
|
577 |
|
578 /** |
|
579 * Dynamic channels provided by the plugin |
|
580 */ |
|
581 RSensrvResourceChannelInfoList iDynamicChannelInfoList; |
|
582 |
|
583 /** |
|
584 * Channel change listener counter |
|
585 */ |
|
586 TInt iChannelChangeListenerCount; |
|
587 |
|
588 /** |
|
589 * Indicator of previous SSY load fail. If ETrue, the load attempt was failed. |
|
590 */ |
|
591 TBool iPreviousSsyLoadFailed; |
|
592 }; |
|
593 |
|
594 |
|
595 |
|
596 #endif // SENSRVPLUGINPROXY_H |