|
1 // Copyright (c) 1999-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 #include <kernel/kern_priv.h> |
|
17 |
|
18 #ifdef SYMBIAN_OLD_EXPORT_LOCATION |
|
19 #include <comms-infras/legacy_loopback_driver.h> |
|
20 #else |
|
21 //this header is not exported, needs to be a user include |
|
22 #include "legacy_loopback_driver.h" |
|
23 #endif |
|
24 |
|
25 #include "device.h" |
|
26 |
|
27 _LIT(KESockLoopbackPanicCategory,"ESockLoopback"); |
|
28 |
|
29 |
|
30 |
|
31 /** |
|
32 Standard export function for LDDs. This creates a DLogicalDevice derived object, |
|
33 in this case, our DESockLoopbackFactory |
|
34 */ |
|
35 DECLARE_STANDARD_LDD() |
|
36 { |
|
37 return new DESockLoopbackFactory; |
|
38 } |
|
39 |
|
40 /** |
|
41 Constructor |
|
42 */ |
|
43 DESockLoopbackFactory::DESockLoopbackFactory() |
|
44 { |
|
45 // Set version number for this device |
|
46 iVersion=RLegacyLoopbackDriver::VersionRequired(); |
|
47 // Indicate that we work with a PDD |
|
48 iParseMask=KDeviceAllowPhysicalDevice; |
|
49 } |
|
50 |
|
51 |
|
52 /** |
|
53 Second stage constructor for DESockLoopbackFactory. |
|
54 This must at least set a name for the driver object. |
|
55 |
|
56 @return KErrNone if successful, otherwise one of the other system wide error codes. |
|
57 */ |
|
58 TInt DESockLoopbackFactory::Install() |
|
59 { |
|
60 return SetName(&RLegacyLoopbackDriver::Name()); |
|
61 } |
|
62 |
|
63 DESockLoopbackFactory::~DESockLoopbackFactory() |
|
64 { |
|
65 } |
|
66 |
|
67 /** |
|
68 Return the drivers capabilities. |
|
69 Called in the response to an RDevice::GetCaps() request. |
|
70 |
|
71 @param aDes User-side descriptor to write capabilities information into |
|
72 */ |
|
73 void DESockLoopbackFactory::GetCaps(TDes8& aDes) const |
|
74 { |
|
75 // Create a capabilities object |
|
76 RLegacyLoopbackDriver::TCaps caps; |
|
77 caps.iVersion = iVersion; |
|
78 // Write it back to user memory |
|
79 Kern::InfoCopy(aDes,(TUint8*)&caps,sizeof(caps)); |
|
80 } |
|
81 |
|
82 /** |
|
83 Called by the kernel's device driver framework to create a Logical Channel. |
|
84 This is called in the context of the user thread (client) which requested the creation of a Logical Channel |
|
85 (E.g. through a call to RBusLogicalChannel::DoCreate) |
|
86 The thread is in a critical section. |
|
87 |
|
88 @param aChannel Set to point to the created Logical Channel |
|
89 |
|
90 @return KErrNone if successful, otherwise one of the other system wide error codes. |
|
91 */ |
|
92 TInt DESockLoopbackFactory::Create(DLogicalChannelBase*& aChannel) |
|
93 { |
|
94 aChannel=new DESockLoopbackChannel; |
|
95 if(!aChannel) |
|
96 return KErrNoMemory; |
|
97 |
|
98 return KErrNone; |
|
99 } |
|
100 |
|
101 DESockLoopbackChannel::DESockLoopbackChannel() |
|
102 : iSendDataDfc(SendDataDfc, this, 1), // DFC is priority '1' |
|
103 iReceiveDataDfc(ReceiveDataDfc, this, 1) // DFC is priority '1' |
|
104 { |
|
105 // Get pointer to client threads DThread object |
|
106 iClient=&Kern::CurrentThread(); |
|
107 |
|
108 // Open a reference on client thread so it's control block can't dissapear until |
|
109 // this driver has finished with it. |
|
110 // Note, this call to Open can't fail since its the thread we are currently running in |
|
111 iClient->Open(); |
|
112 memclr(&iDebugProbes, sizeof(iDebugProbes)); |
|
113 Kern::Printf("clearing probe points\n"); |
|
114 } |
|
115 |
|
116 /** |
|
117 Second stage constructor called by the kernel's device driver framework. |
|
118 This is called in the context of the user thread (client) which requested the creation of a Logical Channel |
|
119 (E.g. through a call to RBusLogicalChannel::DoCreate) |
|
120 The thread is in a critical section. |
|
121 |
|
122 @param aUnit The unit argument supplied by the client to RBusLogicalChannel::DoCreate |
|
123 @param aInfo The info argument supplied by the client to RBusLogicalChannel::DoCreate |
|
124 @param aVer The version argument supplied by the client to RBusLogicalChannel::DoCreate |
|
125 |
|
126 @return KErrNone if successful, otherwise one of the other system wide error codes. |
|
127 */ |
|
128 TInt DESockLoopbackChannel::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& aVer) |
|
129 { |
|
130 // Check Platform Security capabilities of client thread (if required). |
|
131 // |
|
132 // Here we handle the simple case where: |
|
133 // 1. The device driver can only have one client thread |
|
134 // 2. The security policy is the binary all-or-nothing policy. |
|
135 // E.g. "If you have the right capability you can do anything with the driver |
|
136 // and if you don't have the capability you can't do anything" |
|
137 // |
|
138 // If only some functionality of the driver is restricted, then the security check should |
|
139 // go elsewhere. E.g. in DoRequest/DoControl. In that case Kern::CurrentThreadHasCapability |
|
140 // shouldn't be used because the 'current thread' isn't the client. |
|
141 // |
|
142 // In this example we do a check here for ECapability_None (which always passes)... |
|
143 if(!Kern::CurrentThreadHasCapability(ECapability_None,__PLATSEC_DIAGNOSTIC_STRING("Checked by ESockLoopback"))) |
|
144 return KErrPermissionDenied; |
|
145 |
|
146 // Check version |
|
147 if (!Kern::QueryVersionSupported(RLegacyLoopbackDriver::VersionRequired(),aVer)) |
|
148 return KErrNotSupported; |
|
149 |
|
150 // Setup LDD for receiving client messages |
|
151 SetDfcQ(((DESockLoopbackPddFactory*)iPhysicalDevice)->iDfcQ); |
|
152 iMsgQ.Receive(); |
|
153 |
|
154 // Associate DFCs with the same queue we set above to receive client messages on |
|
155 iSendDataDfc.SetDfcQ(iDfcQ); |
|
156 iReceiveDataDfc.SetDfcQ(iDfcQ); |
|
157 |
|
158 // Give PDD a pointer to this channel |
|
159 Pdd()->iLdd=this; |
|
160 |
|
161 // Done |
|
162 return KErrNone; |
|
163 } |
|
164 |
|
165 #define PRINT_PROBE_POINTS(name) \ |
|
166 {\ |
|
167 Kern::Printf("probe list:%s\n\t", name); \ |
|
168 for(TInt i = 0; i < (sizeof(iDebugProbes) / sizeof(TInt)); i++)\ |
|
169 { \ |
|
170 Kern::Printf("%d:%d, ", i, iDebugProbes[i]); \ |
|
171 } \ |
|
172 Kern::Printf("\n\n"); \ |
|
173 } |
|
174 |
|
175 #define PROBE_LDD(index) (iDebugProbes[index]++) |
|
176 #define PROBE_LDD_STATIC(index, obj) (((obj)->iDebugProbes[index])++) |
|
177 |
|
178 DESockLoopbackChannel::~DESockLoopbackChannel() |
|
179 { |
|
180 // Cancel all processing that we may be doing |
|
181 DoCancel(RLegacyLoopbackDriver::EAllRequests); |
|
182 // Close our reference on the client thread |
|
183 Kern::SafeClose((DObject*&)iClient,NULL); |
|
184 } |
|
185 |
|
186 /** |
|
187 Called when a user thread requests a handle to this channel. |
|
188 */ |
|
189 TInt DESockLoopbackChannel::RequestUserHandle(DThread* aThread, TOwnerType aType) |
|
190 { |
|
191 // Make sure that only our client can get a handle |
|
192 if (aType!=EOwnerThread || aThread!=iClient) |
|
193 return KErrAccessDenied; |
|
194 return KErrNone; |
|
195 } |
|
196 |
|
197 /** |
|
198 Process a message for this logical channel. |
|
199 This function is called in the context of a DFC thread. |
|
200 |
|
201 @param aMessage The message to process. |
|
202 The iValue member of this distinguishes the message type: |
|
203 iValue==ECloseMsg, channel close message |
|
204 iValue==KMaxTInt, a 'DoCancel' message |
|
205 iValue>=0, a 'DoControl' message with function number equal to iValue |
|
206 iValue<0, a 'DoRequest' message with function number equal to ~iValue |
|
207 */ |
|
208 void DESockLoopbackChannel::HandleMsg(TMessageBase* aMsg) |
|
209 { |
|
210 TThreadMessage& m=*(TThreadMessage*)aMsg; |
|
211 |
|
212 // Get message type |
|
213 TInt id=m.iValue; |
|
214 |
|
215 // Decode the message type and dispatch it to the relevent handler function... |
|
216 |
|
217 if (id==(TInt)ECloseMsg) |
|
218 { |
|
219 // Channel Close |
|
220 PRINT_PROBE_POINTS("ldd"); |
|
221 DoCancel(RLegacyLoopbackDriver::EAllRequests); |
|
222 m.Complete(KErrNone, EFalse); |
|
223 return; |
|
224 } |
|
225 |
|
226 if (id==KMaxTInt) |
|
227 { |
|
228 // DoCancel |
|
229 DoCancel(m.Int0()); |
|
230 m.Complete(KErrNone,ETrue); |
|
231 return; |
|
232 } |
|
233 |
|
234 if (id<0) |
|
235 { |
|
236 // DoRequest |
|
237 TRequestStatus* pS=(TRequestStatus*)m.Ptr0(); |
|
238 TInt r=DoRequest(~id,pS,m.Ptr1(),m.Ptr2()); |
|
239 if (r!=KErrNone) |
|
240 { |
|
241 Kern::RequestComplete(iClient,pS,r); |
|
242 } |
|
243 m.Complete(KErrNone,ETrue); |
|
244 } |
|
245 else |
|
246 { |
|
247 // DoControl |
|
248 TInt r=DoControl(id,m.Ptr0(),m.Ptr1()); |
|
249 m.Complete(r,ETrue); |
|
250 } |
|
251 } |
|
252 |
|
253 /** |
|
254 Process synchronous 'control' requests |
|
255 */ |
|
256 TInt DESockLoopbackChannel::DoControl(TInt aFunction, TAny* a1, TAny* a2) |
|
257 { |
|
258 (void)a2; // a2 not used in this example |
|
259 |
|
260 TInt r; |
|
261 |
|
262 switch (aFunction) |
|
263 { |
|
264 case RLegacyLoopbackDriver::EGetConfig: |
|
265 r = GetConfig((TDes8*)a1); |
|
266 break; |
|
267 |
|
268 case RLegacyLoopbackDriver::ESetConfig: |
|
269 r = SetConfig((const TDesC8*)a1); |
|
270 break; |
|
271 |
|
272 default: |
|
273 r = KErrNotSupported; |
|
274 break; |
|
275 } |
|
276 |
|
277 return r; |
|
278 } |
|
279 |
|
280 /** |
|
281 Process asynchronous requests. |
|
282 */ |
|
283 TInt DESockLoopbackChannel::DoRequest(TInt aReqNo, TRequestStatus* aStatus, TAny* a1, TAny* a2) |
|
284 { |
|
285 (void)a2; // a2 not used in this example |
|
286 |
|
287 TInt r; |
|
288 |
|
289 switch(aReqNo) |
|
290 { |
|
291 case RLegacyLoopbackDriver::ESendData: |
|
292 r=SendData(aStatus,(const TDesC8*)a1); |
|
293 break; |
|
294 |
|
295 case RLegacyLoopbackDriver::EReceiveData: |
|
296 // Example Platform Security capability check which tests the |
|
297 // client for ECapability_None (which always passes)... |
|
298 if(iClient->HasCapability(ECapability_None,__PLATSEC_DIAGNOSTIC_STRING("Checked by ESockLoopback"))) |
|
299 r=ReceiveData(aStatus,(TDes8*)a1); |
|
300 else |
|
301 r=KErrPermissionDenied; |
|
302 break; |
|
303 |
|
304 default: |
|
305 r = KErrNotSupported; |
|
306 break; |
|
307 } |
|
308 |
|
309 return r; |
|
310 } |
|
311 |
|
312 /** |
|
313 Process cancelling of asynchronous requests. |
|
314 */ |
|
315 void DESockLoopbackChannel::DoCancel(TUint aMask) |
|
316 { |
|
317 if(aMask&(1<<RLegacyLoopbackDriver::ESendData)) |
|
318 SendDataCancel(); |
|
319 if(aMask&(1<<RLegacyLoopbackDriver::EReceiveData)) |
|
320 ReceiveDataCancel(); |
|
321 } |
|
322 |
|
323 /** |
|
324 Process a GetConfig control message. This writes the current driver configuration to a |
|
325 RLegacyLoopbackDriver::TConfigBuf supplied by the client. |
|
326 */ |
|
327 TInt DESockLoopbackChannel::GetConfig(TDes8* aConfigBuf) |
|
328 { |
|
329 // Create a structure giving the current configuration |
|
330 RLegacyLoopbackDriver::TConfig config; |
|
331 CurrentConfig(config); |
|
332 |
|
333 // Write the config to the client |
|
334 TPtrC8 ptr((const TUint8*)&config,sizeof(config)); |
|
335 return Kern::ThreadDesWrite(iClient,aConfigBuf,ptr,0,KTruncateToMaxLength,NULL); |
|
336 } |
|
337 |
|
338 /** |
|
339 Process a SetConfig control message. This sets the driver configuration using a |
|
340 RLegacyLoopbackDriver::TConfigBuf supplied by the client. |
|
341 */ |
|
342 TInt DESockLoopbackChannel::SetConfig(const TDesC8* aConfigBuf) |
|
343 { |
|
344 // Don't allow configuration changes whilst we're busy |
|
345 if(iSendDataStatus || iReceiveDataStatus) |
|
346 return KErrInUse; |
|
347 |
|
348 // Create a config structure. |
|
349 RLegacyLoopbackDriver::TConfig config; |
|
350 CurrentConfig(config); |
|
351 |
|
352 // Note: We have filled config with the current settings, this is to allow |
|
353 // backwards compatibility when a client gives us an old (and shorter) version |
|
354 // of the config structure. |
|
355 |
|
356 // Read the config structure from client |
|
357 TPtr8 ptr((TUint8*)&config,sizeof(config)); |
|
358 TInt r=Kern::ThreadDesRead(iClient,aConfigBuf,ptr,0); |
|
359 if(r!=KErrNone) |
|
360 return r; |
|
361 |
|
362 // Use config data to setup the driver. Checking that parameters which aren't settable |
|
363 // either contain the correct values or are zero (meaning 'default') |
|
364 if(config.iPddBufferSize && config.iPddBufferSize!=Pdd()->BufferSize()) |
|
365 return KErrArgument; |
|
366 |
|
367 if(config.iMaxSendDataSize && config.iMaxSendDataSize!=KLoopbackMTU) |
|
368 return KErrArgument; |
|
369 |
|
370 if(config.iMaxReceiveDataSize && config.iMaxReceiveDataSize!=KLoopbackMTU) |
|
371 return KErrArgument; |
|
372 |
|
373 r=Pdd()->SetSpeed(config.iSpeed); |
|
374 if(r!=KErrNone) |
|
375 return r; |
|
376 |
|
377 return r; |
|
378 } |
|
379 |
|
380 /** |
|
381 Fill a TConfig with the drivers current configuration. |
|
382 */ |
|
383 void DESockLoopbackChannel::CurrentConfig(RLegacyLoopbackDriver::TConfig& aConfig) |
|
384 { |
|
385 aConfig.iSpeed = Pdd()->Speed(); |
|
386 aConfig.iPddBufferSize = Pdd()->BufferSize(); |
|
387 aConfig.iMaxSendDataSize = KLoopbackMTU; |
|
388 aConfig.iMaxReceiveDataSize = KLoopbackMTU; |
|
389 } |
|
390 |
|
391 /** |
|
392 Start processing a SendData request. |
|
393 */ |
|
394 TInt DESockLoopbackChannel::SendData(TRequestStatus* aStatus,const TDesC8* aData) |
|
395 { |
|
396 // Check that a 'SendData' isn't already in progress |
|
397 PROBE_LDD(0); |
|
398 if(iSendDataStatus) |
|
399 { |
|
400 Kern::ThreadKill(iClient,EExitPanic,ERequestAlreadyPending,KESockLoopbackPanicCategory); |
|
401 return KErrInUse; |
|
402 } |
|
403 |
|
404 // Read data from client into our buffer |
|
405 TInt r=Kern::ThreadDesRead(iClient,aData,Pdd()->SendBuffer(),0); |
|
406 if(r!=KErrNone) |
|
407 return r; |
|
408 |
|
409 // Give data to PDD so that it can do the work |
|
410 r=Pdd()->RequestDataSend(); |
|
411 if(r!=KErrNone) |
|
412 return r; |
|
413 |
|
414 // Save the client request status and return |
|
415 PROBE_LDD(1); |
|
416 iSendDataStatus = aStatus; |
|
417 return KErrNone; |
|
418 } |
|
419 |
|
420 /** |
|
421 Cancel a SendData request. |
|
422 */ |
|
423 void DESockLoopbackChannel::SendDataCancel() |
|
424 { |
|
425 if(iSendDataStatus) |
|
426 { |
|
427 // Tell PDD to stop processing the request |
|
428 Pdd()->SendDataCancel(); |
|
429 |
|
430 // Cancel DFC |
|
431 iSendDataDfc.Cancel(); |
|
432 |
|
433 // Complete clients request |
|
434 Kern::RequestComplete(iClient,iSendDataStatus,KErrCancel); |
|
435 } |
|
436 } |
|
437 |
|
438 /** |
|
439 Called by PDD from ISR to indicate that a SendData operation has completed. |
|
440 */ |
|
441 void DESockLoopbackChannel::SendDataComplete(TInt aResult) |
|
442 { |
|
443 PROBE_LDD(2); |
|
444 // Save result code |
|
445 iSendDataResult = aResult; |
|
446 |
|
447 // Queue DFC |
|
448 iSendDataDfc.Add(); |
|
449 } |
|
450 |
|
451 /** |
|
452 DFC callback which gets triggered after the PDD has signalled that SendData completed. |
|
453 This just casts aPtr and calls DoSendDataComplete(). |
|
454 */ |
|
455 void DESockLoopbackChannel::SendDataDfc(TAny* aPtr) |
|
456 { |
|
457 PROBE_LDD_STATIC(3, ((DESockLoopbackChannel*)aPtr)); |
|
458 ((DESockLoopbackChannel*)aPtr)->DoSendDataComplete(); |
|
459 } |
|
460 |
|
461 /** |
|
462 Called from a DFC after the PDD has signalled that SendData completed. |
|
463 */ |
|
464 void DESockLoopbackChannel::DoSendDataComplete() |
|
465 { |
|
466 TInt result = iSendDataResult; |
|
467 |
|
468 // Complete clients request |
|
469 Kern::RequestComplete(iClient,iSendDataStatus,result); |
|
470 } |
|
471 |
|
472 /** |
|
473 Start processing a ReceiveData request. |
|
474 */ |
|
475 TInt DESockLoopbackChannel::ReceiveData(TRequestStatus* aStatus,TDes8* aPtr) |
|
476 { |
|
477 PROBE_LDD(4); |
|
478 // Check that a 'ReceiveData' isn't already in progress |
|
479 if(iReceiveDataStatus) |
|
480 { |
|
481 Kern::ThreadKill(iClient,EExitPanic,ERequestAlreadyPending,KESockLoopbackPanicCategory); |
|
482 return KErrInUse; |
|
483 } |
|
484 |
|
485 // Save the client request status and descriptor for later completion |
|
486 iReceiveDataStatus = aStatus; |
|
487 iReceiveDataDescriptor = aPtr; |
|
488 |
|
489 if(Pdd()->ReceivedQueueLen() > 0) |
|
490 { |
|
491 // complete with waiting data |
|
492 DoReceiveDataComplete(); |
|
493 } |
|
494 |
|
495 // Ask PDD for data |
|
496 NKern::Lock(); |
|
497 TInt r = Pdd()->RequestDataReceipt(); |
|
498 NKern::Unlock(); |
|
499 |
|
500 if(r != KErrNone) |
|
501 { |
|
502 iReceiveDataDescriptor = NULL; |
|
503 return r; |
|
504 } |
|
505 |
|
506 PROBE_LDD(5); |
|
507 return KErrNone; |
|
508 } |
|
509 |
|
510 /** |
|
511 Cancel a ReceiveData request. |
|
512 */ |
|
513 void DESockLoopbackChannel::ReceiveDataCancel() |
|
514 { |
|
515 if(iReceiveDataStatus) |
|
516 { |
|
517 // Tell PDD to stop processing the request |
|
518 Pdd()->ReceiveDataCancel(); |
|
519 |
|
520 // Cancel DFC |
|
521 iReceiveDataDfc.Cancel(); |
|
522 |
|
523 // Finished with client descriptor, so NULL it to help detect coding errors |
|
524 iReceiveDataDescriptor = NULL; |
|
525 |
|
526 // Complete clients request |
|
527 Kern::RequestComplete(iClient,iReceiveDataStatus,KErrCancel); |
|
528 } |
|
529 } |
|
530 |
|
531 /** |
|
532 Called by PDD from ISR to indicate that a ReceiveData operation has completed. |
|
533 */ |
|
534 void DESockLoopbackChannel::ReceiveDataComplete(TInt aResult) |
|
535 { |
|
536 PROBE_LDD(6); |
|
537 // Save result code |
|
538 iReceiveDataResult = aResult; |
|
539 |
|
540 // Queue DFC |
|
541 NKern::Lock(); |
|
542 iReceiveDataDfc.Add(); |
|
543 NKern::Unlock(); |
|
544 } |
|
545 |
|
546 /** |
|
547 DFC Callback which gets triggered after the PDD has signalled that ReceiveData completed. |
|
548 This just casts aPtr and calls DoReceiveDataComplete(). |
|
549 */ |
|
550 void DESockLoopbackChannel::ReceiveDataDfc(TAny* aPtr) |
|
551 { |
|
552 PROBE_LDD_STATIC(7, ((DESockLoopbackChannel*)aPtr)); |
|
553 ((DESockLoopbackChannel*)aPtr)->DoReceiveDataComplete(); |
|
554 } |
|
555 |
|
556 /** |
|
557 Called from a DFC after the PDD has signalled that ReceiveData completed. |
|
558 */ |
|
559 void DESockLoopbackChannel::DoReceiveDataComplete() |
|
560 { |
|
561 if(iReceiveDataStatus) |
|
562 { |
|
563 PROBE_LDD(8); |
|
564 |
|
565 // Write data to client from our buffer |
|
566 TInt result = Kern::ThreadDesWrite(iClient, iReceiveDataDescriptor, Pdd()->ReceiveBuffer(), 0); |
|
567 |
|
568 // Finished with client descriptor, so NULL it to help detect coding errors |
|
569 iReceiveDataDescriptor = NULL; |
|
570 |
|
571 // Step the receive queue |
|
572 Pdd()->AdvanceReceiveQueue(); |
|
573 |
|
574 // Use result code from PDD if it was an error |
|
575 if(iReceiveDataResult != KErrNone) |
|
576 result = iReceiveDataResult; |
|
577 |
|
578 // Complete client's request |
|
579 Kern::RequestComplete(iClient, iReceiveDataStatus, result); |
|
580 } |
|
581 } |
|
582 |
|
583 |