|
1 // Copyright (c) 2008-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 // Remote Control bulk server implementation. |
|
15 // |
|
16 |
|
17 |
|
18 |
|
19 /** |
|
20 @file |
|
21 @internalComponent |
|
22 */ |
|
23 #include <bluetooth/logger.h> |
|
24 |
|
25 #include "bulkbearerinterface.h" |
|
26 #include "bulkserver.h" |
|
27 #include "bulkserversecuritypolicy.h" |
|
28 #include "bulksession.h" |
|
29 #include "messagequeue.h" |
|
30 #include "remconmessage.h" |
|
31 #include "utils.h" |
|
32 |
|
33 #include "server.h" |
|
34 |
|
35 #ifdef __FLOG_ACTIVE |
|
36 _LIT8(KLogComponent, LOG_COMPONENT_REMCON_SERVER); |
|
37 #endif |
|
38 |
|
39 #ifdef _DEBUG |
|
40 PANICCATEGORY("bulkserver"); |
|
41 #endif // _DEBUG |
|
42 |
|
43 #ifdef __FLOG_ACTIVE |
|
44 #define LOGSESSIONS LogSessions() |
|
45 #define LOGINCOMINGPENDINGDELIVERY LogIncomingPendingDelivery() |
|
46 #define LOGINCOMINGDELIVERED LogIncomingDelivered() |
|
47 #else |
|
48 #define LOGSESSIONS |
|
49 #define LOGINCOMINGPENDINGDELIVERY |
|
50 #define LOGINCOMINGDELIVERED |
|
51 #endif // __FLOG_ACTIVE |
|
52 |
|
53 CRemConBulkServer* CRemConBulkServer::NewLC(RMsgQueue<TBulkServerMsg>& aMsgQueue) |
|
54 { |
|
55 LOG_STATIC_FUNC; |
|
56 CRemConBulkServer* self = new(ELeave) CRemConBulkServer(aMsgQueue); |
|
57 CleanupStack::PushL(self); |
|
58 // StartL is where the kernel checks that there isn't already an instance |
|
59 // of the same server running, so do it before ConstructL. |
|
60 self->StartL(KRemConBulkServerName); |
|
61 self->ConstructL(); |
|
62 return self; |
|
63 } |
|
64 |
|
65 CRemConBulkServer::~CRemConBulkServer() |
|
66 { |
|
67 LOG_FUNC; |
|
68 |
|
69 delete iShutdownTimer; |
|
70 |
|
71 iSessions.Close(); |
|
72 |
|
73 delete iIncomingPendingDelivery; |
|
74 delete iIncomingDelivered; |
|
75 |
|
76 delete iBulkBearerInterface; |
|
77 } |
|
78 |
|
79 CRemConBulkServer::CRemConBulkServer(RMsgQueue<TBulkServerMsg>& aMsgQueue) |
|
80 : CPolicyServer(CActive::EPriorityStandard, KRemConBulkServerPolicy, ESharableSessions), |
|
81 iRemConMsgQueue(aMsgQueue) |
|
82 { |
|
83 LOG_FUNC; |
|
84 } |
|
85 |
|
86 void CRemConBulkServer::ConstructL() |
|
87 { |
|
88 LOG_FUNC; |
|
89 iShutdownTimer = CPeriodic::NewL(CActive::EPriorityStandard); |
|
90 |
|
91 iIncomingPendingDelivery = CMessageQueue::NewL(); |
|
92 iIncomingDelivered = CMessageQueue::NewL(); |
|
93 |
|
94 // First message is the control server for client ID binding |
|
95 TBulkServerMsg ctrlMsg; |
|
96 iRemConMsgQueue.ReceiveBlocking(ctrlMsg); |
|
97 ASSERT_DEBUG(ctrlMsg.iType == EControlServer); |
|
98 ASSERT_DEBUG(ctrlMsg.iData); |
|
99 iControlServer = reinterpret_cast<CRemConServer*>(ctrlMsg.iData); |
|
100 |
|
101 // Second message is bearer manager information for interface loading. |
|
102 TBulkServerMsg msg; |
|
103 iRemConMsgQueue.ReceiveBlocking(msg); |
|
104 ASSERT_DEBUG(msg.iType == EBearerManager); |
|
105 ASSERT_DEBUG(msg.iData); |
|
106 CBearerManager* bearerManager = reinterpret_cast<CBearerManager*>(msg.iData); |
|
107 iBulkBearerInterface = CBulkBearerInterface::NewL(*this, *bearerManager); |
|
108 } |
|
109 |
|
110 CSession2* CRemConBulkServer::NewSessionL(const TVersion& aVersion, |
|
111 const RMessage2& aMessage) const |
|
112 { |
|
113 LOG_FUNC; |
|
114 LOG3(_L8("\taVersion = (%d,%d,%d)"), aVersion.iMajor, aVersion.iMinor, aVersion.iBuild); |
|
115 |
|
116 // Version number check... |
|
117 TVersion v(KRemConBulkSrvMajorVersionNumber, |
|
118 KRemConBulkSrvMinorVersionNumber, |
|
119 KRemConBulkSrvBuildNumber); |
|
120 |
|
121 if ( !User::QueryVersionSupported(v, aVersion) ) |
|
122 { |
|
123 LEAVEIFERRORL(KErrNotSupported); |
|
124 } |
|
125 |
|
126 CRemConBulkServer* ncThis = const_cast<CRemConBulkServer*>(this); |
|
127 |
|
128 CRemConBulkSession* sess = NULL; |
|
129 TRAPD(err, sess = CRemConBulkSession::NewL(*ncThis, |
|
130 aMessage) |
|
131 ); |
|
132 if ( err != KErrNone ) |
|
133 { |
|
134 // Session creation might have failed- if it has we need to check if |
|
135 // we need to shut down again. |
|
136 const_cast<CRemConBulkServer*>(this)->StartShutdownTimerIfNoSessions(); |
|
137 LEAVEIFERRORL(err); |
|
138 } |
|
139 |
|
140 LOG1(_L8("\tsess = 0x%08x"), sess); |
|
141 return sess; |
|
142 } |
|
143 |
|
144 void CRemConBulkServer::StartShutdownTimerIfNoSessions() |
|
145 { |
|
146 LOG_FUNC; |
|
147 |
|
148 if ( iSessions.Count() == 0 ) |
|
149 { |
|
150 LOG(_L8("\tno remaining sessions- starting shutdown timer")); |
|
151 // Should have been created during our construction. |
|
152 ASSERT_DEBUG(iShutdownTimer); |
|
153 // Start the shutdown timer. It's actually a CPeriodic- the first |
|
154 // event will be in KShutdownDelay microseconds' time. |
|
155 // NB The shutdown timer might already be active, in the following |
|
156 // case: this function is being called by NewSessionL because there |
|
157 // was a failure creating a new session, BUT this function had already |
|
158 // been called by the session's destructor (i.e. the failure was in |
|
159 // the session's ConstructL, NOT its new(ELeave)). To protect against |
|
160 // KERN-EXEC 15 just check for if the timer is already active. |
|
161 if ( !iShutdownTimer->IsActive() ) |
|
162 { |
|
163 iShutdownTimer->Start(KShutdownDelay, |
|
164 // Delay of subsequent firings (will not happen because we kill |
|
165 // ourselves after the first). |
|
166 0, |
|
167 TCallBack(CRemConBulkServer::TimerFired, this) |
|
168 ); |
|
169 } |
|
170 else |
|
171 { |
|
172 LOG(_L8("\tshutdown timer was already active")); |
|
173 } |
|
174 } |
|
175 } |
|
176 |
|
177 TInt CRemConBulkServer::TimerFired(TAny* aThis) |
|
178 { |
|
179 LOG_STATIC_FUNC |
|
180 static_cast<void>(aThis); |
|
181 |
|
182 #if defined(__FLOG_ACTIVE) || defined(_DEBUG) |
|
183 CRemConBulkServer* self = static_cast<CRemConBulkServer*>(aThis); |
|
184 // We should have sent 'this' to this callback. |
|
185 ASSERT_DEBUG(self); |
|
186 LOG1(_L8("\tauto shutdown- terminating the bulk server [0x%08x]"), self); |
|
187 #endif // __FLOG_ACTIVE || _DEBUG |
|
188 |
|
189 // Stop our Active Scheduler. This returns the flow of execution to after |
|
190 // the CActiveScheduler::Start call in the server startup code, and |
|
191 // terminates the server. |
|
192 CActiveScheduler::Stop(); |
|
193 |
|
194 return KErrNone; |
|
195 } |
|
196 |
|
197 TInt CRemConBulkServer::ClientOpened(CRemConBulkSession& aSession, TProcessId aProcessId) |
|
198 { |
|
199 LOG_FUNC; |
|
200 LOG1(_L8("\t&aSession = 0x%08x"), &aSession); |
|
201 LOGSESSIONS; |
|
202 |
|
203 ASSERT_DEBUG(iControlServer); |
|
204 TRemConClientId id = iControlServer->ClientIdByProcessId(aProcessId); |
|
205 TInt ret = KErrNotFound; // If the control session has already been destroyed then fail... |
|
206 if(id != KNullClientId) |
|
207 { |
|
208 TRAP(ret, iControlServer->BulkInterfacesForClientL(id, aSession.InterestedAPIs())); |
|
209 if(ret == KErrNone) |
|
210 { |
|
211 aSession.SetId(id); |
|
212 |
|
213 // Register the session by appending it to our array, and also making an |
|
214 // item for it in the record of which points in the connection history |
|
215 // sessions are interested in. |
|
216 ret = iSessions.Append(&aSession); |
|
217 |
|
218 if ( ret == KErrNone ) |
|
219 { |
|
220 ASSERT_DEBUG(iBulkBearerInterface); |
|
221 iBulkBearerInterface->BulkClientAvailable(id); |
|
222 |
|
223 // Should have been created during our construction. |
|
224 ASSERT_DEBUG(iShutdownTimer); |
|
225 iShutdownTimer->Cancel(); |
|
226 } |
|
227 } |
|
228 } |
|
229 |
|
230 LOGSESSIONS; |
|
231 LOG1(_L8("\tret = %d"), ret); |
|
232 return ret; |
|
233 } |
|
234 |
|
235 // called by session when session is closed. |
|
236 void CRemConBulkServer::ClientClosed(CRemConBulkSession& aSession) |
|
237 { |
|
238 LOG_FUNC; |
|
239 LOG1(_L8("\t&aSession = 0x%08x"), &aSession); |
|
240 LOGSESSIONS; |
|
241 |
|
242 // Find this session in the array and remove it (if it's there). |
|
243 const TUint sessCount = iSessions.Count(); |
|
244 for(TUint ix = 0; ix < sessCount; ++ix) |
|
245 { |
|
246 if(iSessions[ix] == &aSession) |
|
247 { |
|
248 // We've found the session in our array. |
|
249 ASSERT_DEBUG(aSession.Id() != KNullClientId); |
|
250 ASSERT_DEBUG(iBulkBearerInterface); |
|
251 iBulkBearerInterface->BulkClientRemoved(aSession.Id()); |
|
252 // Remove the session from our array. |
|
253 iSessions.Remove(ix); |
|
254 break; |
|
255 } |
|
256 } |
|
257 |
|
258 StartShutdownTimerIfNoSessions(); |
|
259 |
|
260 LOGSESSIONS; |
|
261 } |
|
262 |
|
263 #ifdef __FLOG_ACTIVE |
|
264 void CRemConBulkServer::LogSessions() const |
|
265 { |
|
266 const TUint count = iSessions.Count(); |
|
267 LOG1(_L8("\tNumber of sessions = %d"), count); |
|
268 for ( TUint ii = 0 ; ii < count ; ++ii ) |
|
269 { |
|
270 CRemConBulkSession* const session = iSessions[ii]; |
|
271 ASSERT_DEBUG(session); |
|
272 LOG3(_L8("\t\tsession %d [0x%08x], Id = %d"), |
|
273 ii, |
|
274 session, |
|
275 session->Id() |
|
276 ); |
|
277 } |
|
278 } |
|
279 |
|
280 void CRemConBulkServer::LogIncomingPendingDelivery() const |
|
281 { |
|
282 LOG(_L8("Logging incoming pending delivery messages")); |
|
283 __FLOG_STMT(const_cast<CRemConBulkServer*>(this)->IncomingPendingDelivery().LogQueue();) |
|
284 } |
|
285 |
|
286 void CRemConBulkServer::LogIncomingDelivered() const |
|
287 { |
|
288 LOG(_L8("Logging incoming delivered commands")); |
|
289 __FLOG_STMT(const_cast<CRemConBulkServer*>(this)->IncomingDelivered().LogQueue();) |
|
290 } |
|
291 #endif // __FLOG_ACTIVE |
|
292 |
|
293 |
|
294 void CRemConBulkServer::ReceiveRequest(CRemConBulkSession& aSession) |
|
295 { |
|
296 LOG_FUNC; |
|
297 LOGINCOMINGPENDINGDELIVERY; |
|
298 LOGINCOMINGDELIVERED; |
|
299 |
|
300 // Find the first message in IncomingPendingDelivery for this session. |
|
301 TSglQueIter<CRemConMessage>& iter = IncomingPendingDelivery().SetToFirst(); |
|
302 CRemConMessage* msg; |
|
303 while ( ( msg = iter++ ) != NULL ) |
|
304 { |
|
305 if ( msg->SessionId() == aSession.Id() ) |
|
306 { |
|
307 TInt err = aSession.WriteMessageToClient(*msg); |
|
308 IncomingPendingDelivery().Remove(*msg); |
|
309 |
|
310 if (err == KErrNone ) |
|
311 { |
|
312 // We'll need to remember it for the response coming back. |
|
313 IncomingDelivered().Append(*msg); |
|
314 } |
|
315 else |
|
316 { |
|
317 // Tell bearer it won't be getting a response |
|
318 SendReject(msg->Addr(), msg->InterfaceUid(), msg->OperationId(), msg->TransactionId()); |
|
319 |
|
320 // 'Take ownership' of it by destroying it- it's finished with. |
|
321 delete msg; |
|
322 } |
|
323 |
|
324 break; |
|
325 } |
|
326 } |
|
327 |
|
328 LOGINCOMINGPENDINGDELIVERY; |
|
329 LOGINCOMINGDELIVERED; |
|
330 } |
|
331 |
|
332 void CRemConBulkServer::NewCommand(CRemConMessage& aMsg) |
|
333 { |
|
334 LOG_FUNC; |
|
335 |
|
336 CRemConBulkSession* const sess = Session(aMsg.Client()); |
|
337 if(!sess) |
|
338 { |
|
339 // Tell bearer it won't be getting a response |
|
340 SendReject(aMsg.Addr(), aMsg.InterfaceUid(), aMsg.OperationId(), aMsg.TransactionId()); |
|
341 |
|
342 // 'Take ownership' of it by destroying it- it's finished with. |
|
343 delete &aMsg; |
|
344 } |
|
345 else |
|
346 { |
|
347 DeliverCmdToClient(aMsg, *sess); |
|
348 } |
|
349 //now wait for the actual response to come from the client (success!) |
|
350 } |
|
351 |
|
352 void CRemConBulkServer::DeliverCmdToClient(const CRemConMessage& aMsg, CRemConBulkSession& aSess) |
|
353 { |
|
354 LOG_FUNC; |
|
355 |
|
356 ASSERT_DEBUG(aMsg.MsgType() == ERemConCommand); |
|
357 // Take a reference of the message and set the right session ID (important |
|
358 // to set the selected session's ID because this is how we match up the |
|
359 // client's response). |
|
360 CRemConMessage& msg = const_cast<CRemConMessage&>(aMsg); |
|
361 msg.SessionId() = aSess.Id(); |
|
362 DeliverMessageToClient(msg, aSess); |
|
363 } |
|
364 |
|
365 void CRemConBulkServer::DeliverMessageToClient(CRemConMessage& aMsg, CRemConBulkSession& aSess) |
|
366 { |
|
367 LOG_FUNC; |
|
368 |
|
369 TInt err = KErrNone; |
|
370 // See if the client can take the message now and put it on the right |
|
371 // queue. |
|
372 if ( aSess.CurrentReceiveMessage().Handle() ) |
|
373 { |
|
374 err = aSess.WriteMessageToClient(aMsg); |
|
375 // If the message was a command, and it was delivered with no error, |
|
376 // then put it in the 'incoming delivered' log. Otherwise, delete it |
|
377 // because it's finished with. |
|
378 if (err == KErrNone ) |
|
379 { |
|
380 // We'll need to remember it for the response coming back. |
|
381 IncomingDelivered().Append(aMsg); |
|
382 } |
|
383 else |
|
384 { |
|
385 // Tell bearer it won't be getting a response |
|
386 SendReject(aMsg.Addr(), aMsg.InterfaceUid(), aMsg.OperationId(), aMsg.TransactionId()); |
|
387 |
|
388 // 'Take ownership' of it by destroying it- it's finished with. |
|
389 delete &aMsg; |
|
390 } |
|
391 } |
|
392 else |
|
393 { |
|
394 IncomingPendingDelivery().Append(aMsg); |
|
395 } |
|
396 |
|
397 LOGINCOMINGPENDINGDELIVERY; |
|
398 LOGINCOMINGDELIVERED; |
|
399 } |
|
400 |
|
401 TInt CRemConBulkServer::SendResponse(CRemConMessage& aMsg, CRemConBulkSession& aSess) |
|
402 { |
|
403 LOG_FUNC; |
|
404 LOGINCOMINGDELIVERED; |
|
405 TInt ret = KErrNone; |
|
406 |
|
407 CRemConMessage* response = &aMsg; |
|
408 |
|
409 // Find the first command ('space badger') in the iIncomingDelivered queue with |
|
410 // the same session ID, interface UID and operation ID as the response |
|
411 // we're sending, and send the response to the same address that space badger came |
|
412 // from. |
|
413 TSglQueIter<CRemConMessage>& iter = IncomingDelivered().SetToFirst(); |
|
414 CRemConMessage* msg; |
|
415 TBool found = EFalse; |
|
416 while ( ( msg = iter++ ) != NULL ) |
|
417 { |
|
418 if ( msg->SessionId() == aSess.Id() |
|
419 && msg->InterfaceUid() == response->InterfaceUid() |
|
420 && msg->OperationId() == response->OperationId()) |
|
421 { |
|
422 LOG1(_L8("\tfound a matching item in the incoming delivered commands log: [0x%08x]"), msg); |
|
423 found = ETrue; |
|
424 |
|
425 // Set the right address and transaction id in the outgoing message |
|
426 response->Addr() = msg->Addr(); |
|
427 response->TransactionId() = msg->TransactionId(); |
|
428 |
|
429 // Remove the item from the 'incoming delivered' queue now we've |
|
430 // addressed a response using it. |
|
431 IncomingDelivered().RemoveAndDestroy(*msg); |
|
432 |
|
433 ASSERT_DEBUG(iBulkBearerInterface); |
|
434 ret = iBulkBearerInterface->Send(aMsg); |
|
435 |
|
436 break; |
|
437 } |
|
438 } |
|
439 |
|
440 // If the command was not found, then the app has sent a response to a |
|
441 // non-existant command. It may do this in good intention if the server |
|
442 // has transparently died and been restarted between the app's reception |
|
443 // of the command and it sending its response, so we can't panic it. |
|
444 // Just drop the message. |
|
445 if ( !found ) |
|
446 { |
|
447 // Complete the message with KErrNone. We have done all we can with |
|
448 // it. Any other error may encourage retries from the application, |
|
449 // which would be useless in this situation. |
|
450 ret = KErrNone; |
|
451 } |
|
452 |
|
453 // We've now finished with the response. |
|
454 delete response; |
|
455 |
|
456 LOGINCOMINGDELIVERED; |
|
457 return ret; |
|
458 } |
|
459 |
|
460 void CRemConBulkServer::CommandExpired(TUint aTransactionId) |
|
461 { |
|
462 LOG_FUNC; |
|
463 |
|
464 CRemConMessage* msg; |
|
465 |
|
466 TSglQueIter<CRemConMessage> pendingDeliveryIter = IncomingPendingDelivery().SetToFirst(); |
|
467 |
|
468 while ((msg = pendingDeliveryIter++) != NULL) |
|
469 { |
|
470 if (msg->TransactionId() == aTransactionId) |
|
471 { |
|
472 IncomingPendingDelivery().RemoveAndDestroy(*msg); |
|
473 } |
|
474 } |
|
475 |
|
476 TSglQueIter<CRemConMessage> deliveredIter = IncomingDelivered().SetToFirst(); |
|
477 |
|
478 while ((msg = deliveredIter++) != NULL) |
|
479 { |
|
480 if (msg->TransactionId() == aTransactionId) |
|
481 { |
|
482 IncomingDelivered().RemoveAndDestroy(*msg); |
|
483 } |
|
484 } |
|
485 } |
|
486 |
|
487 void CRemConBulkServer::SendReject(TRemConAddress aAddr, TUid aInterfaceUid, TUint aOperationId, TUint aTransactionId) |
|
488 { |
|
489 LOG_FUNC; |
|
490 |
|
491 CRemConMessage* rejectMsg = NULL; |
|
492 RBuf8 data; |
|
493 data = KNullDesC8; |
|
494 TRAPD(err, rejectMsg = CRemConMessage::NewL(aAddr, KNullClientId, ERemConReject, ERemConMessageDefault, aInterfaceUid, aOperationId, data, 0, aTransactionId)); |
|
495 if ( err == KErrNone) |
|
496 { |
|
497 static_cast<void>(iBulkBearerInterface->Send(*rejectMsg)); |
|
498 // We've now finished with the response. |
|
499 delete rejectMsg; |
|
500 } |
|
501 } |
|
502 |
|
503 CMessageQueue& CRemConBulkServer::IncomingPendingDelivery() |
|
504 { |
|
505 ASSERT_DEBUG(iIncomingPendingDelivery); |
|
506 return *iIncomingPendingDelivery; |
|
507 } |
|
508 |
|
509 CMessageQueue& CRemConBulkServer::IncomingDelivered() |
|
510 { |
|
511 ASSERT_DEBUG(iIncomingDelivered); |
|
512 return *iIncomingDelivered; |
|
513 } |
|
514 |
|
515 CRemConBulkSession* CRemConBulkServer::Session(TUint aSessionId) const |
|
516 { |
|
517 CRemConBulkSession* sess = NULL; |
|
518 |
|
519 const TUint count = iSessions.Count(); |
|
520 for ( TUint ii = 0 ; ii < count ; ++ii ) |
|
521 { |
|
522 CRemConBulkSession* const temp = iSessions[ii]; |
|
523 ASSERT_DEBUG(temp); |
|
524 if ( temp->Id() == aSessionId ) |
|
525 { |
|
526 sess = temp; |
|
527 break; |
|
528 } |
|
529 } |
|
530 |
|
531 return sess; |
|
532 } |
|
533 |