|
1 /* |
|
2 * Copyright (c) 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: |
|
15 * |
|
16 */ |
|
17 |
|
18 // System include files |
|
19 #include <s32strm.h> |
|
20 #include <s32mem.h> |
|
21 |
|
22 // User include files |
|
23 #include "ccsrvsession.h" |
|
24 #include "ccsrv.h" |
|
25 #include "ccresource.h" |
|
26 #include "ccsrvapi.h" |
|
27 |
|
28 // Local constants |
|
29 |
|
30 |
|
31 // ======== MEMBER FUNCTIONS ======== |
|
32 |
|
33 // ----------------------------------------------------------------------- |
|
34 // CCcSrvSession::CCcSrvSession() |
|
35 // ----------------------------------------------------------------------- |
|
36 // |
|
37 CCcSrvSession::CCcSrvSession(): |
|
38 iLastTrId( 0 ) |
|
39 { |
|
40 } |
|
41 |
|
42 // ----------------------------------------------------------------------- |
|
43 // CCcSrvSession::~CCcSrvSession() |
|
44 // ----------------------------------------------------------------------- |
|
45 // |
|
46 CCcSrvSession::~CCcSrvSession() |
|
47 { |
|
48 iRequests.ResetAndDestroy(); |
|
49 iObservers.Close(); |
|
50 Server().DropSession( this ); |
|
51 } |
|
52 |
|
53 // ----------------------------------------------------------------------- |
|
54 // CCcSrvSession::CreateL() |
|
55 // ----------------------------------------------------------------------- |
|
56 // |
|
57 void CCcSrvSession::CreateL() |
|
58 { |
|
59 Server().AddSessionL( this ); |
|
60 } |
|
61 |
|
62 // ----------------------------------------------------------------------- |
|
63 // CCcSrvSession::Id() |
|
64 // ----------------------------------------------------------------------- |
|
65 // |
|
66 TUint32 CCcSrvSession::Id() |
|
67 { |
|
68 return iId; |
|
69 } |
|
70 |
|
71 // ----------------------------------------------------------------------- |
|
72 // CCcSrvSession::SetId() |
|
73 // ----------------------------------------------------------------------- |
|
74 // |
|
75 void CCcSrvSession::SetId( |
|
76 TUint32 aId ) |
|
77 { |
|
78 iId = aId; |
|
79 } |
|
80 |
|
81 // ----------------------------------------------------------------------- |
|
82 // CCcSrvSession::ServiceL() |
|
83 // ----------------------------------------------------------------------- |
|
84 // |
|
85 void CCcSrvSession::ServiceL( |
|
86 const RMessage2& aMessage ) |
|
87 { |
|
88 RMessage2 msg = aMessage; |
|
89 TRAPD( err, DoServiceL( msg ) ); |
|
90 if ( err ) |
|
91 { |
|
92 aMessage.Complete( err ); |
|
93 } |
|
94 } |
|
95 |
|
96 // ----------------------------------------------------------------------- |
|
97 // CCcSrvSession::DoServiceL() |
|
98 // ----------------------------------------------------------------------- |
|
99 // |
|
100 void CCcSrvSession::DoServiceL( |
|
101 RMessage2& aMessage ) |
|
102 { |
|
103 switch ( aMessage.Function() ) |
|
104 { |
|
105 case ECcReqisterProvider: |
|
106 HandleRegisterProviderL( aMessage ); |
|
107 break; |
|
108 case ECcRegisterObserver: |
|
109 HandleRegisterObserverL( aMessage ); |
|
110 break; |
|
111 case ECcWaitForApiReq: |
|
112 HandleWaitForApiReqL( aMessage ); |
|
113 break; |
|
114 case ECcWaitForApiNtf: |
|
115 HandleWaitForApiNtfL( aMessage ); |
|
116 break; |
|
117 case ECcCancel: |
|
118 HandleCancelReqL( aMessage ); |
|
119 break; |
|
120 case ECcApiReq: |
|
121 HandleApiReqL( aMessage ); |
|
122 break; |
|
123 case ECcApiResp: |
|
124 HandleApiRespL( aMessage ); |
|
125 break; |
|
126 case ECcGetMsgData: |
|
127 HandleGetMsgDataL( aMessage ); |
|
128 break; |
|
129 case ECcApiNtf: |
|
130 HandleApiNtfL( aMessage ); |
|
131 break; |
|
132 default: |
|
133 User::Leave( KErrNotSupported ); |
|
134 break; |
|
135 } |
|
136 } |
|
137 |
|
138 // ----------------------------------------------------------------------- |
|
139 // CCcSrvSession::HandleRegisterProviderL() |
|
140 // ----------------------------------------------------------------------- |
|
141 // |
|
142 void CCcSrvSession::HandleRegisterProviderL( |
|
143 RMessage2& aMessage ) |
|
144 { |
|
145 TUint32 provider; |
|
146 TPckgBuf<TUint32> packagedProvider; |
|
147 aMessage.ReadL(0, packagedProvider); |
|
148 provider = packagedProvider(); |
|
149 |
|
150 Server().RegisterProviderL( |
|
151 provider, |
|
152 this ); |
|
153 |
|
154 TPckgBuf<TUint32> packagedSessionId( iId ); |
|
155 aMessage.WriteL( 1, packagedSessionId ); |
|
156 aMessage.Complete( KErrNone ); |
|
157 } |
|
158 |
|
159 // ----------------------------------------------------------------------- |
|
160 // CCcSrvSession::HandleRegisterObserverL() |
|
161 // ----------------------------------------------------------------------- |
|
162 // |
|
163 void CCcSrvSession::HandleRegisterObserverL( |
|
164 RMessage2& aMessage ) |
|
165 { |
|
166 TUint32 provider; |
|
167 TPckgBuf<TUint32> packagedProvider; |
|
168 aMessage.ReadL(0, packagedProvider); |
|
169 provider = packagedProvider(); |
|
170 |
|
171 Server().RegisterObserverL( |
|
172 provider, |
|
173 this ); |
|
174 |
|
175 TPckgBuf<TUint32> packagedSessionId( iId ); |
|
176 aMessage.WriteL( 1, packagedSessionId ); |
|
177 aMessage.Complete( KErrNone ); |
|
178 } |
|
179 |
|
180 // ----------------------------------------------------------------------- |
|
181 // CCcSrvSession::RegisterObserverSessionL() |
|
182 // ----------------------------------------------------------------------- |
|
183 // |
|
184 void CCcSrvSession::RegisterObserverSessionL( |
|
185 TUint32 aObserver ) |
|
186 { |
|
187 // Notify provider of registered observer |
|
188 SendObserverNtfL( aObserver, ECcRegisterObserverNtf ); |
|
189 iObservers.AppendL( aObserver ); |
|
190 } |
|
191 |
|
192 // ----------------------------------------------------------------------- |
|
193 // CCcSrvSession::UnregisterObserverSessionL() |
|
194 // ----------------------------------------------------------------------- |
|
195 // |
|
196 void CCcSrvSession::UnregisterObserverSessionL( |
|
197 TUint32 aObserver ) |
|
198 { |
|
199 for ( TInt i = 0; i < iObservers.Count(); i++ ) |
|
200 { |
|
201 if ( iObservers[ i ] == aObserver ) |
|
202 { |
|
203 // Notify provider of unregistered observer |
|
204 SendObserverNtfL( aObserver, ECcUnregisterObserverNtf ); |
|
205 iObservers.Remove( i ); |
|
206 break; |
|
207 } |
|
208 } |
|
209 } |
|
210 |
|
211 // ----------------------------------------------------------------------- |
|
212 // CCcSrvSession::HandleWaitForApiReqL() |
|
213 // ----------------------------------------------------------------------- |
|
214 // |
|
215 void CCcSrvSession::HandleWaitForApiReqL( |
|
216 RMessage2& aMessage ) |
|
217 { |
|
218 CCcSrvMsg* tr = CCcSrvMsg::NewL(); |
|
219 CleanupStack::PushL( tr ); |
|
220 tr->SetMessage( aMessage ); |
|
221 iRequests.AppendL( tr ); |
|
222 CleanupStack::Pop( tr ); |
|
223 } |
|
224 |
|
225 // ----------------------------------------------------------------------- |
|
226 // CCcSrvSession::HandleWaitForApiNtfL() |
|
227 // ----------------------------------------------------------------------- |
|
228 // |
|
229 void CCcSrvSession::HandleWaitForApiNtfL( |
|
230 RMessage2& aMessage ) |
|
231 { |
|
232 CCcSrvMsg* tr = CCcSrvMsg::NewL(); |
|
233 CleanupStack::PushL( tr ); |
|
234 tr->SetMessage( aMessage ); |
|
235 iRequests.AppendL( tr ); |
|
236 CleanupStack::Pop( tr ); |
|
237 } |
|
238 |
|
239 // ----------------------------------------------------------------------- |
|
240 // CCcSrvSession::HandleCancelReqL() |
|
241 // ----------------------------------------------------------------------- |
|
242 // |
|
243 void CCcSrvSession::HandleCancelReqL( |
|
244 RMessage2& aMessage ) |
|
245 { |
|
246 TInt function; |
|
247 TPckgBuf<TInt> packagedFunction; |
|
248 aMessage.ReadL( 0, packagedFunction ); |
|
249 function = packagedFunction(); |
|
250 |
|
251 for ( TInt i = 0; i < iRequests.Count(); i++ ) |
|
252 { |
|
253 CCcSrvMsg* req = iRequests[ i ]; |
|
254 if ( req->Message().Function() == function && |
|
255 !req->Message().IsNull() ) |
|
256 { |
|
257 // Complete canceled request |
|
258 req->Message().Complete( KErrCancel ); |
|
259 iRequests.Remove( i ); |
|
260 delete req; |
|
261 } |
|
262 } |
|
263 aMessage.Complete( KErrNone ); |
|
264 } |
|
265 |
|
266 // ----------------------------------------------------------------------- |
|
267 // CCcSrvSession::HandleApiReqL() |
|
268 // ----------------------------------------------------------------------- |
|
269 // |
|
270 void CCcSrvSession::HandleApiReqL( |
|
271 RMessage2& aMessage ) |
|
272 { |
|
273 TUint32 provider; |
|
274 TPckgBuf<TUint32> packagedProvider; |
|
275 aMessage.ReadL( 0, packagedProvider ); |
|
276 provider = packagedProvider(); |
|
277 TUint32 providerAddress; |
|
278 Server().ResolveProviderAddressL( provider, providerAddress ); |
|
279 |
|
280 HBufC8* msgBuf = HBufC8::NewL( aMessage.GetDesLengthL( 3 ) ); |
|
281 CleanupStack::PushL( msgBuf ); |
|
282 TPtr8 msgBufPtr = msgBuf->Des(); |
|
283 aMessage.ReadL( 3, msgBufPtr, 0 ); |
|
284 |
|
285 RDesReadStream stream( msgBufPtr ); |
|
286 |
|
287 CCcSrvMsg* message = CCcSrvMsg::NewL(); |
|
288 CleanupStack::PushL( message ); |
|
289 message->InternalizeL( stream ); |
|
290 message->SetTrId( Server().GetTrId() ); |
|
291 message->SetMessage( aMessage ); |
|
292 |
|
293 Server().SendMsgL( iId, providerAddress, *message ); |
|
294 |
|
295 iRequests.AppendL( message ); |
|
296 |
|
297 CleanupStack::Pop( message ); |
|
298 CleanupStack::PopAndDestroy( msgBuf ); |
|
299 |
|
300 } |
|
301 |
|
302 // ----------------------------------------------------------------------- |
|
303 // CCcSrvSession::HandleApiReqL() |
|
304 // ----------------------------------------------------------------------- |
|
305 // |
|
306 void CCcSrvSession::HandleApiRespL( |
|
307 RMessage2& aMessage ) |
|
308 { |
|
309 // Read sender and receiver of a response |
|
310 TPckgBuf<TUint32> pckg; |
|
311 aMessage.ReadL( 1, pckg ); |
|
312 TUint32 sender = pckg(); |
|
313 aMessage.ReadL( 2, pckg ); |
|
314 TUint32 receiver = pckg(); |
|
315 |
|
316 // Read message data |
|
317 HBufC8* msgBuf = HBufC8::NewL( aMessage.GetDesLengthL( 3 ) ); |
|
318 CleanupStack::PushL( msgBuf ); |
|
319 TPtr8 msgBufPtr = msgBuf->Des(); |
|
320 aMessage.ReadL( 3, msgBufPtr, 0 ); |
|
321 |
|
322 // Internalize message |
|
323 RDesReadStream stream( msgBufPtr ); |
|
324 CCcSrvMsg* message = CCcSrvMsg::NewL(); |
|
325 CleanupStack::PushL( message ); |
|
326 message->InternalizeL( stream ); |
|
327 message->SetMessage( aMessage ); |
|
328 |
|
329 // Forward message to receiver |
|
330 Server().SendMsgL( sender, receiver, *message ); |
|
331 |
|
332 CleanupStack::PopAndDestroy( message ); |
|
333 CleanupStack::PopAndDestroy( msgBuf ); |
|
334 |
|
335 aMessage.Complete( KErrNone ); |
|
336 |
|
337 } |
|
338 |
|
339 // ----------------------------------------------------------------------- |
|
340 // CCcSrvSession::HandleGetMsgDataL() |
|
341 // ----------------------------------------------------------------------- |
|
342 // |
|
343 void CCcSrvSession::HandleGetMsgDataL( |
|
344 RMessage2& aMessage ) |
|
345 { |
|
346 // Read sender and receiver of a response |
|
347 TPckgBuf<TUint32> pckg; |
|
348 aMessage.ReadL( 0, pckg ); |
|
349 TUint32 trId = pckg(); |
|
350 |
|
351 CCcSrvMsg* req( NULL ); |
|
352 TInt index( 0 ); |
|
353 for( TInt i = 0; i < iRequests.Count() && !req; i++ ) |
|
354 { |
|
355 if ( iRequests[ i ]->TrId() == trId ) |
|
356 { |
|
357 req = iRequests[ i ]; |
|
358 index = i; |
|
359 } |
|
360 } |
|
361 |
|
362 TInt err( KErrNone ); |
|
363 if ( req ) |
|
364 { |
|
365 // Write message data |
|
366 aMessage.WriteL( 1, req->Data(), 0); |
|
367 // Remove request |
|
368 iRequests.Remove( index ); |
|
369 delete req; |
|
370 } |
|
371 else |
|
372 { |
|
373 err = KErrNotFound; |
|
374 } |
|
375 |
|
376 aMessage.Complete( err ); |
|
377 |
|
378 } |
|
379 |
|
380 // ----------------------------------------------------------------------- |
|
381 // CCcSrvSession::HandleApiNtfL() |
|
382 // ----------------------------------------------------------------------- |
|
383 // |
|
384 void CCcSrvSession::HandleApiNtfL( |
|
385 RMessage2& aMessage ) |
|
386 { |
|
387 // Read notification data |
|
388 HBufC8* msgBuf = HBufC8::NewL( aMessage.GetDesLengthL( 3 ) ); |
|
389 CleanupStack::PushL( msgBuf ); |
|
390 TPtr8 msgBufPtr = msgBuf->Des(); |
|
391 aMessage.ReadL( 3, msgBufPtr, 0 ); |
|
392 |
|
393 // Internalize notification |
|
394 RDesReadStream stream( msgBufPtr ); |
|
395 CCcSrvMsg* message = CCcSrvMsg::NewL(); |
|
396 CleanupStack::PushL( message ); |
|
397 message->InternalizeL( stream ); |
|
398 message->SetMessage( aMessage ); |
|
399 |
|
400 // Forward notification to observers |
|
401 for ( TInt i = 0; i < iObservers.Count(); i++ ) |
|
402 { |
|
403 Server().SendMsgL( iId, iObservers[ i ], *message ); |
|
404 } |
|
405 |
|
406 message->Message().Complete( KErrNone ); |
|
407 |
|
408 CleanupStack::PopAndDestroy( message ); |
|
409 CleanupStack::PopAndDestroy( msgBuf ); |
|
410 } |
|
411 |
|
412 // ----------------------------------------------------------------------- |
|
413 // CCcSrvSession::ReceiveMsgL() |
|
414 // ----------------------------------------------------------------------- |
|
415 // |
|
416 void CCcSrvSession::ReceiveMsgL( |
|
417 TUint32 aSender, |
|
418 TUint32 aReceiver, |
|
419 CCcSrvMsg& aMessage ) |
|
420 { |
|
421 TBool found( EFalse ); |
|
422 CCcSrvMsg* req( NULL ); |
|
423 TInt index( 0 ); |
|
424 for ( TInt i = 0; i < iRequests.Count() && !found; i++ ) |
|
425 { |
|
426 req = iRequests[ i ]; |
|
427 if ( aMessage.Message().Function() == ECcApiReq && |
|
428 req->Message().Function() == ECcWaitForApiReq && |
|
429 !req->Message().IsNull() ) |
|
430 { |
|
431 // Pending WaitForApiReq transaction found |
|
432 index = i; |
|
433 found = ETrue; |
|
434 } |
|
435 else if ( aMessage.Message().Function() == ECcApiResp && |
|
436 req->TrId() == aMessage.TrId() ) |
|
437 { |
|
438 // Pending ApiReq transaction found |
|
439 index = i; |
|
440 found = ETrue; |
|
441 } |
|
442 else if ( aMessage.Message().Function() == ECcApiNtf && |
|
443 req->Message().Function() == ECcWaitForApiNtf && |
|
444 !req->Message().IsNull() ) |
|
445 { |
|
446 // Pending WaitForApiNtf transaction found |
|
447 index = i; |
|
448 found = ETrue; |
|
449 } |
|
450 } |
|
451 |
|
452 if ( found ) |
|
453 { |
|
454 // Write sender of message |
|
455 TPckgBuf<TUint32> packagedSender( aSender ); |
|
456 req->Message().WriteL( 1, packagedSender, 0 ); |
|
457 |
|
458 // Write receiver of message |
|
459 TPckgBuf<TUint32> packagedReceiver( aReceiver ); |
|
460 req->Message().WriteL( 2, packagedReceiver, 0 ); |
|
461 |
|
462 // Externalize message header |
|
463 CBufFlat* buf = CBufFlat::NewL( KCcHeaderSize ); |
|
464 CleanupStack::PushL( buf ); |
|
465 RBufWriteStream stream( *buf ); |
|
466 CleanupClosePushL( stream ); |
|
467 aMessage.ExternalizeHeaderL( stream ); |
|
468 CleanupStack::PopAndDestroy( &stream ); |
|
469 |
|
470 //Create a heap descriptor from the buffer |
|
471 HBufC8* des = HBufC8::NewL( buf->Size() ); |
|
472 CleanupStack::PushL( des ); |
|
473 TPtr8 ptr( des->Des() ); |
|
474 buf->Read( 0, ptr, buf->Size() ); |
|
475 |
|
476 // Write request header |
|
477 req->Message().WriteL( 3, ptr, 0); |
|
478 CleanupStack::PopAndDestroy( des ); |
|
479 CleanupStack::PopAndDestroy( buf ); |
|
480 |
|
481 // Complete request |
|
482 req->Message().Complete( KErrNone ); |
|
483 if ( aMessage.DataSize() ) |
|
484 { |
|
485 // Store request data to be read later |
|
486 // with GetMsgData() |
|
487 req->SetTrId( aMessage.TrId() ); |
|
488 req->SetData( aMessage.Data() ); |
|
489 } |
|
490 else |
|
491 { |
|
492 // Received request does not contain any data |
|
493 // -> remove it from request array |
|
494 iRequests.Remove( index ); |
|
495 delete req; |
|
496 } |
|
497 } |
|
498 else |
|
499 { |
|
500 User::Leave( KErrNotFound ); |
|
501 } |
|
502 } |
|
503 |
|
504 // ----------------------------------------------------------------------- |
|
505 // CCcSrvSession::SendObserverNtfL() |
|
506 // ----------------------------------------------------------------------- |
|
507 // |
|
508 void CCcSrvSession::SendObserverNtfL( |
|
509 TUint32 aSender, |
|
510 TUint32 aNtf ) |
|
511 { |
|
512 // Notify provider of registered observer |
|
513 TBool found( EFalse ); |
|
514 CCcSrvMsg* req( NULL ); |
|
515 TInt index( 0 ); |
|
516 for ( TInt i = 0; i < iRequests.Count() && !found; i++ ) |
|
517 { |
|
518 req = iRequests[ i ]; |
|
519 if ( req->Message().Function() == ECcWaitForApiReq && |
|
520 !req->Message().IsNull() ) |
|
521 { |
|
522 // Pending WaitForApiReq transaction found |
|
523 index = i; |
|
524 found = ETrue; |
|
525 } |
|
526 } |
|
527 if ( found ) |
|
528 { |
|
529 // Write sender of message |
|
530 TPckgBuf<TUint32> packagedSender( aSender ); |
|
531 req->Message().WriteL( 1, packagedSender, 0 ); |
|
532 |
|
533 // Write receiver of message |
|
534 TPckgBuf<TUint32> packagedReceiver( iId ); |
|
535 req->Message().WriteL( 2, packagedReceiver, 0 ); |
|
536 |
|
537 // Externalize notification |
|
538 CCcSrvMsg* ntf = CCcSrvMsg::NewL(); |
|
539 CleanupStack::PushL( ntf ); |
|
540 ntf->SetMsgId( aNtf ); |
|
541 HBufC8* ntfBuf = ntf->MarshalL(); |
|
542 CleanupStack::PushL( ntfBuf ); |
|
543 TPtr8 ntfPtr( NULL, 0); |
|
544 ntfPtr.Set( ntfBuf->Des() ); |
|
545 |
|
546 // Write request header |
|
547 req->Message().WriteL( 3, ntfPtr, 0); |
|
548 CleanupStack::PopAndDestroy( ntfBuf ); |
|
549 CleanupStack::PopAndDestroy( ntf ); |
|
550 |
|
551 // Complete request |
|
552 req->Message().Complete( KErrNone ); |
|
553 iRequests.Remove( index ); |
|
554 delete req; |
|
555 |
|
556 } |
|
557 else |
|
558 { |
|
559 User::Leave( KErrNotFound ); |
|
560 } |
|
561 } |
|
562 |
|
563 // End of file |