|
1 /* |
|
2 * Copyright (c) 2005-2009 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 |
|
19 |
|
20 /** |
|
21 @file |
|
22 @internalComponent |
|
23 */ |
|
24 |
|
25 // System includes.. |
|
26 #include <e32base.h> |
|
27 |
|
28 #include "SerialDriverExt.h" |
|
29 #include "SerialDriver.h" // our header |
|
30 |
|
31 // Some useful logical/physical driver names. |
|
32 #if defined (__WINS__) |
|
33 #define PDD_NAME _L("ECDRV.PDD") |
|
34 #define LDD_NAME _L("ECOMM.LDD") |
|
35 #define DEFAULT_PORT 1 |
|
36 #else |
|
37 #define PDD_NAME _L("EUART") |
|
38 #define LDD_NAME _L("ECOMM") |
|
39 #define DEFAULT_PORT 0 |
|
40 #endif |
|
41 |
|
42 // How many varaiants of the physical driver should we attempt to load before failing. |
|
43 #ifdef __WINS__ |
|
44 const TInt KMaxPdds=0; |
|
45 #else |
|
46 const TInt KMaxPdds=10; |
|
47 #endif |
|
48 |
|
49 #define IGNORE_VALUE(val) ((val)=(val)) |
|
50 |
|
51 class RSerialPort : public RBusDevComm |
|
52 { |
|
53 public: |
|
54 |
|
55 TBool TimedWrite(TRequestStatus & aStatus, |
|
56 TTimeIntervalMicroSeconds32 aTimeout, |
|
57 const TDesC8 &aDes, |
|
58 TInt aLength); |
|
59 |
|
60 TBool TimedRead(TRequestStatus & aStatus, |
|
61 TTimeIntervalMicroSeconds32 aTimeout, |
|
62 TDes8 &aDes, |
|
63 TInt aLength); |
|
64 |
|
65 }; |
|
66 |
|
67 TBool RSerialPort::TimedRead(TRequestStatus & aStatus, |
|
68 TTimeIntervalMicroSeconds32 aTimeout, |
|
69 TDes8 &aDes, |
|
70 TInt aLength) |
|
71 { |
|
72 IGNORE_VALUE(aTimeout); |
|
73 |
|
74 |
|
75 // Do the actual read using the base class function... |
|
76 |
|
77 Read(aStatus, aDes, aLength); // asynchronous |
|
78 |
|
79 |
|
80 // if the timer was still running when we cancelled it here, then all ok. |
|
81 return (ETrue); |
|
82 |
|
83 } |
|
84 |
|
85 TBool RSerialPort::TimedWrite(TRequestStatus & aStatus, |
|
86 TTimeIntervalMicroSeconds32 aTimeout, |
|
87 const TDesC8 &aDes, |
|
88 TInt aLength) |
|
89 { |
|
90 |
|
91 IGNORE_VALUE(aTimeout); |
|
92 |
|
93 // Do the actual write using the base class function... |
|
94 Write(aStatus, aDes, aLength); // asynchronous |
|
95 |
|
96 return (ETrue); |
|
97 |
|
98 } |
|
99 |
|
100 |
|
101 ///////////////////// |
|
102 /// CSerialServer /// |
|
103 ///////////////////// |
|
104 |
|
105 CSerialServer::CSerialServer() |
|
106 { |
|
107 |
|
108 m_errLogicalLoad = KErrNone; |
|
109 m_errPhysicalLoad = KErrNone; |
|
110 |
|
111 for (TUint index = 0; index < MAX_PORTS; index++) |
|
112 portsInUse[index] = NULL; |
|
113 |
|
114 } |
|
115 |
|
116 CSerialServer::~CSerialServer() |
|
117 { |
|
118 // Delete any ports that are still in use... |
|
119 for (TUint index = 0; index < MAX_PORTS; index++) |
|
120 { |
|
121 if (portsInUse[index] != NULL) |
|
122 { |
|
123 delete portsInUse[index]; |
|
124 portsInUse[index]= NULL; // just tidying up |
|
125 } |
|
126 } |
|
127 } |
|
128 |
|
129 TBool CSerialServer::Open(void) |
|
130 { |
|
131 |
|
132 TBool bSuccess = EFalse; |
|
133 |
|
134 // Load the logical driver. |
|
135 m_errLogicalLoad = User::LoadLogicalDevice(LDD_NAME); |
|
136 |
|
137 if (m_errLogicalLoad == KErrNone || m_errLogicalLoad == KErrAlreadyExists) |
|
138 { |
|
139 |
|
140 // Load the physical driver. |
|
141 TInt loadErr; |
|
142 TBuf<10> pddName=PDD_NAME; |
|
143 |
|
144 // Try EUART as a driver name first, then EUART0, EUART1, EUART2...EUARTn. |
|
145 TInt driver; |
|
146 for (driver=-1; driver<KMaxPdds && bSuccess == EFalse; ++driver) |
|
147 { |
|
148 if (driver==0) |
|
149 pddName.Append(TChar('0')); |
|
150 else if (driver>0) |
|
151 pddName[pddName.Length()-1] = (TText)('0'+driver); |
|
152 loadErr = User::LoadPhysicalDevice(pddName); |
|
153 if (loadErr == KErrNone || loadErr==KErrAlreadyExists) |
|
154 bSuccess = ETrue; |
|
155 } |
|
156 |
|
157 // If failed to load physical driver, unload the logical if this wasn't originally present |
|
158 if (bSuccess == EFalse) |
|
159 { |
|
160 if (m_errLogicalLoad == KErrNone) |
|
161 User::FreeLogicalDevice(LDD_NAME); |
|
162 } |
|
163 } |
|
164 |
|
165 return(bSuccess); |
|
166 } |
|
167 |
|
168 TBool CSerialServer::Close(void) |
|
169 { |
|
170 TInt logUnloadError = KErrNone; |
|
171 TInt phyUnloadError = KErrNone; |
|
172 TBool bSuccess = EFalse; |
|
173 |
|
174 // If not originally loaded, then unload the physical device. |
|
175 if (m_errPhysicalLoad == KErrNone) |
|
176 phyUnloadError = User::FreePhysicalDevice(PDD_NAME); |
|
177 |
|
178 // If not originally loaded, then unload the logical device. |
|
179 if (m_errLogicalLoad == KErrNone) |
|
180 logUnloadError = User::FreeLogicalDevice(LDD_NAME); |
|
181 |
|
182 if (phyUnloadError == KErrNone && logUnloadError == KErrNone) |
|
183 bSuccess = ETrue; |
|
184 |
|
185 return (bSuccess); |
|
186 |
|
187 } |
|
188 |
|
189 |
|
190 RSerialPort * CSerialServer::GetPort(TUint aPortNum) |
|
191 { |
|
192 RSerialPort * pRPort = (aPortNum < MAX_PORTS ? portsInUse[aPortNum] : NULL); |
|
193 return (pRPort); |
|
194 } |
|
195 |
|
196 |
|
197 RSerialPort * CSerialServer::InitialisePort(TUint aPortNum) |
|
198 { |
|
199 RSerialPort * newPort = NULL; |
|
200 |
|
201 if (aPortNum < MAX_PORTS && portsInUse[aPortNum] == NULL) |
|
202 { |
|
203 |
|
204 // Create a new port... |
|
205 newPort = new RSerialPort; //RClass - so can't leave. |
|
206 |
|
207 // Open the real port... |
|
208 TInt error = newPort->Open(aPortNum); |
|
209 |
|
210 if (error != KErrNone ) |
|
211 { |
|
212 // There was a problem, tidy up... |
|
213 delete newPort; |
|
214 newPort = NULL; |
|
215 } |
|
216 else |
|
217 { |
|
218 // Record a pointer to our port for later use. |
|
219 portsInUse[aPortNum] = newPort; |
|
220 } |
|
221 } |
|
222 |
|
223 return (newPort); |
|
224 } |
|
225 |
|
226 TBool CSerialServer::ClosePort (TUint aPortNum) |
|
227 { |
|
228 TBool closed = EFalse; |
|
229 |
|
230 // if port is valid... |
|
231 if (aPortNum < MAX_PORTS && portsInUse[aPortNum]) |
|
232 { |
|
233 // Close the physical port! |
|
234 portsInUse[aPortNum]->Close(); |
|
235 |
|
236 // Delete the memory and update the pointer. |
|
237 delete portsInUse[aPortNum]; |
|
238 portsInUse[aPortNum] = NULL; |
|
239 |
|
240 // Update flag to indicate all closed ok. |
|
241 closed = ETrue; |
|
242 |
|
243 } |
|
244 return (closed); |
|
245 |
|
246 } |
|
247 |
|
248 /////////////////// |
|
249 /// CSerialPort /// |
|
250 /////////////////// |
|
251 |
|
252 |
|
253 CSerialPort::CSerialPort() |
|
254 { |
|
255 m_pOurServer = NULL; |
|
256 } |
|
257 |
|
258 CSerialPort::~CSerialPort() |
|
259 { |
|
260 |
|
261 } |
|
262 |
|
263 TBool CSerialPort::Open(CSerialServer & aServer, TUint aPort) |
|
264 { |
|
265 TBool bOpenedOk = EFalse; |
|
266 |
|
267 // The server holds the real port details, so call the server to do the business... |
|
268 if (aServer.InitialisePort(aPort) != NULL) |
|
269 { |
|
270 m_pOurServer = &aServer; |
|
271 |
|
272 m_portId = aPort; |
|
273 |
|
274 bOpenedOk = ETrue; |
|
275 } |
|
276 |
|
277 return (bOpenedOk); |
|
278 } |
|
279 |
|
280 |
|
281 TBool CSerialPort::Close(void) |
|
282 { |
|
283 TBool bClosed = EFalse; |
|
284 |
|
285 // The server object holds all the real details, so call the appropriate |
|
286 // server function. |
|
287 if (m_pOurServer) |
|
288 bClosed = m_pOurServer->ClosePort(m_portId); |
|
289 |
|
290 return (bClosed); |
|
291 } |
|
292 |
|
293 TBool CSerialPort::Read(TRequestStatus & aStatus, |
|
294 TTimeIntervalMicroSeconds32 aTimeout, |
|
295 TDes8 &aDes, |
|
296 TInt aLength) |
|
297 { |
|
298 TBool success = EFalse; |
|
299 |
|
300 RSerialPort * pPort = GivePortDetails(); |
|
301 |
|
302 if (pPort) |
|
303 { |
|
304 if (pPort->TimedRead(aStatus, aTimeout, aDes, aLength)) |
|
305 success = ETrue; |
|
306 } |
|
307 |
|
308 // return success or otherwise! |
|
309 return (success); |
|
310 } |
|
311 |
|
312 |
|
313 TBool CSerialPort::Write(TRequestStatus & aStatus, |
|
314 TTimeIntervalMicroSeconds32 aTimeout, |
|
315 const TDesC8 &aDes, |
|
316 TInt aLength) |
|
317 { |
|
318 |
|
319 TBool success = EFalse; |
|
320 |
|
321 RSerialPort * pPort = GivePortDetails(); |
|
322 |
|
323 if (pPort) |
|
324 { |
|
325 if (pPort->TimedWrite(aStatus, aTimeout, aDes, aLength)) |
|
326 success = ETrue; |
|
327 } |
|
328 |
|
329 // return success or otherwise! |
|
330 return (success); |
|
331 } |
|
332 |
|
333 |
|
334 TInt CSerialPort::SetConfig(const TCommConfig & aRequiredConfig) |
|
335 { |
|
336 TInt errConfig = KErrUnknown; |
|
337 |
|
338 RSerialPort * pPort = GivePortDetails(); |
|
339 |
|
340 if (pPort) |
|
341 { |
|
342 errConfig = pPort->SetConfig(aRequiredConfig); |
|
343 } |
|
344 |
|
345 return (errConfig); |
|
346 } |
|
347 |
|
348 TBool CSerialPort::GetPortConfig(TCommConfig & aPortSettings) |
|
349 { |
|
350 TBool validAction = EFalse; |
|
351 |
|
352 RSerialPort * pPort = GivePortDetails(); |
|
353 |
|
354 if (pPort) |
|
355 { |
|
356 pPort->Config( aPortSettings ); |
|
357 validAction = ETrue; |
|
358 } |
|
359 |
|
360 return (validAction); |
|
361 } |
|
362 |
|
363 TBool CSerialPort::ReadCancel(void) |
|
364 { |
|
365 TBool validAction = EFalse; |
|
366 |
|
367 RSerialPort * pPort = GivePortDetails(); |
|
368 |
|
369 if (pPort) |
|
370 { |
|
371 pPort->ReadCancel(); |
|
372 validAction = ETrue; |
|
373 } |
|
374 |
|
375 return (validAction); |
|
376 } |
|
377 |
|
378 |
|
379 TBool CSerialPort::WriteCancel(void) |
|
380 { |
|
381 TBool validAction = EFalse; |
|
382 |
|
383 RSerialPort * pPort = GivePortDetails(); |
|
384 |
|
385 if (pPort) |
|
386 { |
|
387 pPort->WriteCancel(); |
|
388 validAction = ETrue; |
|
389 } |
|
390 return (validAction); |
|
391 |
|
392 } |
|
393 |
|
394 TBool CSerialPort::SetReceiveBufferLength(TInt aSize) |
|
395 { |
|
396 TBool validAction = EFalse; |
|
397 |
|
398 RSerialPort * pPort = GivePortDetails(); |
|
399 |
|
400 if (pPort) |
|
401 { |
|
402 pPort->SetReceiveBufferLength(aSize); |
|
403 validAction = ETrue; |
|
404 } |
|
405 |
|
406 return (validAction); |
|
407 |
|
408 } |
|
409 |
|
410 TBool CSerialPort::ReceiveBufferLength(TInt & aSize) |
|
411 { |
|
412 TBool validAction = EFalse; |
|
413 |
|
414 RSerialPort * pPort = GivePortDetails(); |
|
415 |
|
416 if (pPort) |
|
417 { |
|
418 aSize = pPort->ReceiveBufferLength(); |
|
419 validAction = ETrue; |
|
420 } |
|
421 |
|
422 return (validAction); |
|
423 } |
|
424 |
|
425 |
|
426 |
|
427 RSerialPort * CSerialPort::GivePortDetails(void) |
|
428 { |
|
429 RSerialPort * pPort = NULL; |
|
430 |
|
431 if (m_pOurServer) |
|
432 pPort = m_pOurServer->GetPort(m_portId); |
|
433 |
|
434 return (pPort); |
|
435 } |
|
436 |
|
437 ////////////////////////////// |
|
438 // The external interface |
|
439 // for users of the library |
|
440 ////////////////////////////// |
|
441 |
|
442 EXPORT_C SerialServer OpenSerialServerL(void) |
|
443 { |
|
444 CSerialServer * pServer; |
|
445 pServer = new(ELeave) CSerialServer; |
|
446 |
|
447 // Declare a server and open it. |
|
448 if ( pServer->Open() == EFalse) |
|
449 { |
|
450 delete pServer; |
|
451 pServer = NULL; |
|
452 } |
|
453 |
|
454 return (static_cast<SerialServer>(pServer)); |
|
455 } |
|
456 |
|
457 EXPORT_C void CloseSerialServer(SerialServer aServer) |
|
458 { |
|
459 if (aServer) |
|
460 { |
|
461 CSerialServer * pServer = static_cast<CSerialServer *>(aServer); |
|
462 delete pServer; |
|
463 } |
|
464 } |
|
465 |
|
466 EXPORT_C TAny * OpenSerialPortL(SerialServer aServer, TUint aPort) |
|
467 { |
|
468 CSerialServer * pServer = static_cast<CSerialServer *>(aServer); |
|
469 |
|
470 CSerialPort * pPort = new(ELeave) CSerialPort; |
|
471 |
|
472 if (pPort->Open(*pServer, aPort) == EFalse) |
|
473 { |
|
474 delete pPort; |
|
475 pPort = NULL; |
|
476 } |
|
477 |
|
478 return (static_cast<TAny *>(pPort)); |
|
479 } |
|
480 |
|
481 EXPORT_C TBool CloseSerialPort(SerialPort aPort) |
|
482 { |
|
483 CSerialPort * pPort = static_cast<CSerialPort *>(aPort); |
|
484 |
|
485 TBool returnVal = pPort->Close(); |
|
486 |
|
487 delete pPort; |
|
488 |
|
489 return (returnVal); |
|
490 } |
|
491 |
|
492 EXPORT_C TBool Read(SerialPort aPort, |
|
493 TRequestStatus & aStatus, |
|
494 TTimeIntervalMicroSeconds32 aTimeout, |
|
495 TDes8 &aDes, |
|
496 TInt aLength) |
|
497 { |
|
498 TBool ok = EFalse; |
|
499 if (aPort) |
|
500 { |
|
501 CSerialPort * pPort = static_cast<CSerialPort *>(aPort); |
|
502 ok = pPort->Read(aStatus, aTimeout, aDes, aLength); |
|
503 } |
|
504 |
|
505 return ok; |
|
506 } |
|
507 |
|
508 EXPORT_C TBool Write(SerialPort aPort, |
|
509 TRequestStatus & aStatus, |
|
510 TTimeIntervalMicroSeconds32 aTimeout, |
|
511 const TDesC8 &aDes, |
|
512 TInt aLength) |
|
513 { |
|
514 TBool ok = EFalse; |
|
515 |
|
516 if (aPort) |
|
517 { |
|
518 CSerialPort * pPort = static_cast<CSerialPort *>(aPort); |
|
519 ok = pPort->Write(aStatus, aTimeout, aDes, aLength); |
|
520 } |
|
521 return (ok); |
|
522 } |
|
523 |
|
524 EXPORT_C TInt SetConfig(SerialPort aPort, const TCommConfig & aRequiredConfig ) |
|
525 { |
|
526 TInt error = KErrUnknown; |
|
527 |
|
528 if (aPort) |
|
529 { |
|
530 CSerialPort * pPort = static_cast<CSerialPort *>(aPort); |
|
531 error = pPort->SetConfig(aRequiredConfig); |
|
532 } |
|
533 return error; |
|
534 } |
|
535 |
|
536 EXPORT_C TBool ReadCancel(SerialPort aPort) |
|
537 { |
|
538 TBool ok = EFalse; |
|
539 |
|
540 if (aPort) |
|
541 { |
|
542 CSerialPort * pPort = static_cast<CSerialPort *>(aPort); |
|
543 ok = pPort->ReadCancel(); |
|
544 } |
|
545 return (ok); |
|
546 } |
|
547 |
|
548 |
|
549 EXPORT_C TBool WriteCancel(SerialPort aPort) |
|
550 { |
|
551 TBool ok = EFalse; |
|
552 |
|
553 if (aPort) |
|
554 { |
|
555 CSerialPort * pPort = static_cast<CSerialPort *>(aPort); |
|
556 ok = pPort->WriteCancel(); |
|
557 } |
|
558 return (ok); |
|
559 } |
|
560 |
|
561 EXPORT_C TBool GetPortConfig (SerialPort aPort, TCommConfig & config) |
|
562 { |
|
563 TBool ok = EFalse; |
|
564 |
|
565 if (aPort) |
|
566 { |
|
567 CSerialPort * pPort = static_cast<CSerialPort *>(aPort); |
|
568 ok = pPort->GetPortConfig(config); |
|
569 } |
|
570 return (ok); |
|
571 } |
|
572 |
|
573 |
|
574 EXPORT_C TBool SetReceiveBufferLength(SerialPort aPort, TInt aSize) |
|
575 { |
|
576 TBool ok = EFalse; |
|
577 |
|
578 if (aPort) |
|
579 { |
|
580 CSerialPort * pPort = static_cast<CSerialPort *>(aPort); |
|
581 ok = pPort->SetReceiveBufferLength(aSize); |
|
582 } |
|
583 return (ok); |
|
584 } |
|
585 |
|
586 EXPORT_C TBool ReceiveBufferLength(SerialPort aPort, TInt & aSize) |
|
587 { |
|
588 TBool ok = EFalse; |
|
589 |
|
590 if (aPort) |
|
591 { |
|
592 CSerialPort * pPort = static_cast<CSerialPort *>(aPort); |
|
593 ok = pPort->ReceiveBufferLength(aSize); |
|
594 } |
|
595 return (ok); |
|
596 } |