|
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 * |
|
22 * System Includes |
|
23 * |
|
24 ********************************************************************************/ |
|
25 #include <e32base.h> |
|
26 #include <e32cons.h> |
|
27 #include <hal.h> |
|
28 #include <iniparser.h> |
|
29 |
|
30 |
|
31 /******************************************************************************** |
|
32 * |
|
33 * Local Includes |
|
34 * |
|
35 ********************************************************************************/ |
|
36 #include "stat.h" |
|
37 #include "stat_controller.h" |
|
38 #include "stat_console.h" |
|
39 #include "assert.h" |
|
40 #include "activeconsole.h" |
|
41 #include "filedump.h" |
|
42 |
|
43 #ifndef LIGHT_MODE |
|
44 #include "activeconnection.h" |
|
45 #endif // ifndef LIGHT_MODE |
|
46 |
|
47 /******************************************************************************** |
|
48 * |
|
49 * Macro functions |
|
50 * |
|
51 ********************************************************************************/ |
|
52 |
|
53 /******************************************************************************** |
|
54 * |
|
55 * Definitions |
|
56 * |
|
57 ********************************************************************************/ |
|
58 _LIT( KTxtEPOC32EX, "EPOC32EX" ); |
|
59 _LIT( KFormatFailed, "failed: leave code=%d" ); |
|
60 |
|
61 _LIT( KSerialBaudRate, "115200" ); |
|
62 |
|
63 _LIT( KIniFile, "stat.ini" ); |
|
64 _LIT( KOff, "off" ); |
|
65 _LIT( KOn, "on" ); |
|
66 |
|
67 // Labels used in the ini file. |
|
68 _LIT( KIniLogging, "logging" ); |
|
69 _LIT( KIniTransport, "transport" ); |
|
70 _LIT( KIniComPort, "comport" ); |
|
71 _LIT( KIniDebugPort, "debugport" ); |
|
72 #ifndef LIGHT_MODE |
|
73 _LIT( KIniTransIAP, "iap" ); |
|
74 _LIT( KIniTransSnap, "snap" ); |
|
75 #endif // ifndef LIGHT_MODE |
|
76 |
|
77 // Default parameters we use if there is no ini file. |
|
78 static const TInt defaultLogging = 0; |
|
79 static const TStatConnectType defaultTransport = ESerial; |
|
80 #ifdef __WINS__ |
|
81 _LIT(KDefaultPort,"1"); |
|
82 #else // def __WINS__ |
|
83 _LIT(KDefaultPort,"2"); |
|
84 #endif // def __WINS__ |
|
85 |
|
86 // General value for unset or incorrect data. |
|
87 static const TInt invalidValue = -1; |
|
88 |
|
89 // Test string descriptions of the transport types. |
|
90 // The choice specified in the ini file should match one |
|
91 // of these. |
|
92 static const TPtrC transportText[ENumConnectTypes] = { |
|
93 _L("tcpip"), |
|
94 _L("serial"), |
|
95 _L("ir"), |
|
96 _L("bt"), |
|
97 _L("usb") |
|
98 }; |
|
99 |
|
100 LOCAL_D MNotifyLogMessage *iMsg = NULL; |
|
101 LOCAL_D RFs iFsSession; |
|
102 |
|
103 /******************************************************************************** |
|
104 * |
|
105 * Prototypes |
|
106 * |
|
107 ********************************************************************************/ |
|
108 LOCAL_D CConsoleBase* console; |
|
109 void callExampleL(); |
|
110 void statmainL( void ); |
|
111 |
|
112 /******************************************************************************** |
|
113 * |
|
114 * Implementation |
|
115 * |
|
116 ********************************************************************************/ |
|
117 GLDEF_C TInt E32Main() |
|
118 { |
|
119 __UHEAP_MARK; |
|
120 CTrapCleanup* cleanup=CTrapCleanup::New(); |
|
121 TRAPD(error,callExampleL()); |
|
122 __ASSERT_ALWAYS(!error,User::Panic(KTxtEPOC32EX,error)); |
|
123 delete cleanup; |
|
124 __UHEAP_MARKEND; |
|
125 return 0; |
|
126 } |
|
127 |
|
128 |
|
129 void callExampleL() |
|
130 { |
|
131 console=Console::NewL( KTxtExampleCode, TSize(KConsFullScreen,KConsFullScreen) ); |
|
132 CleanupStack::PushL(console); |
|
133 TRAPD(error,statmainL()); |
|
134 if (error) |
|
135 console->Printf( KFormatFailed, error); |
|
136 CleanupStack::PopAndDestroy(); |
|
137 } |
|
138 |
|
139 TInt SetDebugPort(TInt aPort) |
|
140 { |
|
141 TInt lError=KErrNone; |
|
142 |
|
143 #ifndef SYMBIAN_DIST_SERIES60 |
|
144 lError=HAL::Set(HALData::EDebugPort, aPort); |
|
145 #endif |
|
146 |
|
147 return lError; |
|
148 } |
|
149 |
|
150 |
|
151 TInt getLogging(CIniData* aIniFile,TInt *aLogging) |
|
152 { |
|
153 TPtrC result; |
|
154 TBool ret=TRUE; |
|
155 |
|
156 if(ret==TRUE) |
|
157 { |
|
158 //read logging |
|
159 ret=aIniFile->FindVar(KIniLogging,result); |
|
160 } |
|
161 |
|
162 if(ret==TRUE) |
|
163 { |
|
164 TLex lLex(result); |
|
165 TInt lLogging; |
|
166 lLex.Val(lLogging); |
|
167 |
|
168 *aLogging = (lLogging)?1:0; |
|
169 } |
|
170 |
|
171 return (ret==TRUE)?0:1; |
|
172 } |
|
173 |
|
174 TInt getTransport(CIniData* aIniFile,TInt *aTransport) |
|
175 { |
|
176 TPtrC result; |
|
177 TBool ret=TRUE; |
|
178 |
|
179 if(ret==TRUE) |
|
180 { |
|
181 //read transport that will be used |
|
182 ret=aIniFile->FindVar(KIniTransport,result); |
|
183 } |
|
184 |
|
185 if(ret==TRUE) |
|
186 { |
|
187 *aTransport = invalidValue; |
|
188 TInt count; |
|
189 for( count = 0; ( *aTransport == invalidValue ) && ( count < ENumConnectTypes ); count++ ) |
|
190 { |
|
191 if( 0 == ( result.Compare( transportText[count] ) ) ) |
|
192 { |
|
193 *aTransport = count; |
|
194 } |
|
195 } |
|
196 |
|
197 ret = (*aTransport != invalidValue); |
|
198 } |
|
199 |
|
200 return (ret==TRUE)?0:1; |
|
201 } |
|
202 |
|
203 #ifndef LIGHT_MODE |
|
204 |
|
205 TInt getIAP(CIniData* aIniFile, TDes *aIAP, CActiveConnection::TActiveConnectionMode *mode, TInt *index) |
|
206 { |
|
207 TPtrC result; |
|
208 TBool ret=TRUE; |
|
209 |
|
210 //read IAP |
|
211 ret=aIniFile->FindVar(KIniTransIAP,result); |
|
212 if(ret==FALSE) |
|
213 return KErrNotFound; |
|
214 |
|
215 TLex lNum = TLex(result); |
|
216 TInt lInt; |
|
217 TInt err = lNum.Val(lInt); |
|
218 if (err != KErrNone) |
|
219 return err; |
|
220 |
|
221 *mode = CActiveConnection::EModeIAP; |
|
222 *index = lInt; |
|
223 |
|
224 aIAP->Copy(KIniTransIAP); |
|
225 aIAP->Append(_L("|")); |
|
226 aIAP->Append(result); |
|
227 return KErrNone; |
|
228 } |
|
229 |
|
230 TInt getSnap(CIniData* aIniFile, TDes *aSnap, CActiveConnection::TActiveConnectionMode *mode, TInt *index) |
|
231 { |
|
232 TPtrC result; |
|
233 TBool ret=TRUE; |
|
234 |
|
235 //read snap |
|
236 ret=aIniFile->FindVar(KIniTransSnap,result); |
|
237 if(ret==FALSE) |
|
238 return KErrNotFound; |
|
239 |
|
240 TLex lNum = TLex(result); |
|
241 TInt lInt; |
|
242 TInt err = lNum.Val(lInt); |
|
243 if (err != KErrNone) |
|
244 return err; |
|
245 |
|
246 #ifdef SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY |
|
247 *mode = CActiveConnection::EModeSnap; |
|
248 *index = lInt; |
|
249 |
|
250 aSnap->Copy(KIniTransSnap); |
|
251 aSnap->Append(_L("|")); |
|
252 aSnap->Append(result); |
|
253 return KErrNone; |
|
254 #else |
|
255 _LIT(KError,"INFO: Access Point selection is not supported..\n"); |
|
256 console->Printf(KError); |
|
257 |
|
258 // use default IAP connection |
|
259 aSnap->Zero(); |
|
260 *mode = CActiveConnection::EModeIAP; |
|
261 *index = 0; |
|
262 |
|
263 return KErrNotSupported; |
|
264 #endif // SYMBIAN_NON_SEAMLESS_NETWORK_BEARER_MOBILITY |
|
265 } |
|
266 |
|
267 #endif // ifndef LIGHT_MODE |
|
268 |
|
269 TInt getPort(CIniData* aIniFile,TDes *AtbAddress) |
|
270 { |
|
271 TPtrC result; |
|
272 TBool ret=TRUE; |
|
273 TInt lError=KErrNone; |
|
274 |
|
275 if(ret==TRUE) |
|
276 { |
|
277 //read comport setting |
|
278 ret=aIniFile->FindVar(KIniComPort,result); |
|
279 } |
|
280 |
|
281 if(ret==TRUE) |
|
282 { |
|
283 |
|
284 if(lError==KErrNone) |
|
285 { |
|
286 AtbAddress->Append( result ); |
|
287 } |
|
288 else |
|
289 { |
|
290 ret = FALSE; |
|
291 } |
|
292 } |
|
293 |
|
294 return (ret==TRUE)?0:1; |
|
295 } |
|
296 |
|
297 TInt getDebugPort(CIniData* aIniFile,TInt *aDebugPort) |
|
298 { |
|
299 TPtrC result; |
|
300 TBool ret=TRUE; |
|
301 TInt lError=KErrNone; |
|
302 |
|
303 if(ret==TRUE) |
|
304 { |
|
305 //read debug port setting |
|
306 |
|
307 // If we have found an entry for the debug |
|
308 // port then extract it. |
|
309 // If there is no debug port specified then there |
|
310 // is no error (and no 'else'). |
|
311 if(aIniFile->FindVar(KIniDebugPort,result)) |
|
312 { |
|
313 TLex lLexPort(result); //retrive integer value for comport |
|
314 TInt lPort; |
|
315 lError=lLexPort.Val(lPort); |
|
316 |
|
317 if(lError==KErrNone) |
|
318 { |
|
319 *aDebugPort = lPort; |
|
320 } |
|
321 else |
|
322 { |
|
323 ret = FALSE; |
|
324 } |
|
325 } |
|
326 } |
|
327 |
|
328 return (ret==TRUE)?0:1; |
|
329 } |
|
330 |
|
331 #ifdef LIGHT_MODE |
|
332 TInt getparams( TInt *logging, TInt *transport, TDes *tbAddress ) |
|
333 #else |
|
334 TInt getparams( TInt *logging, TInt *transport, TDes *tbAddress, CActiveConnection::TActiveConnectionMode *mode, TInt *index ) |
|
335 #endif // ifndef LIGHT_MODE |
|
336 { |
|
337 // validate params |
|
338 asserte( logging != NULL ); |
|
339 asserte( transport != NULL ); |
|
340 asserte( tbAddress != NULL ); |
|
341 #ifndef LIGHT_MODE |
|
342 asserte( mode != NULL ); |
|
343 asserte( index != NULL ); |
|
344 #endif // ifndef LIGHT_MODE |
|
345 TInt lError=KErrNone; |
|
346 TInt debugPort = invalidValue; |
|
347 |
|
348 // open cinidata object |
|
349 CIniData* lIniFile=NULL; |
|
350 |
|
351 TRAPD(r,lIniFile=CIniData::NewL(KIniFile)); |
|
352 if(r!=KErrNone) //ini file not found, use defaults |
|
353 { |
|
354 _LIT(KMsg,"stat.ini file not found, using defaults...\n"); |
|
355 console->Printf(KMsg); |
|
356 |
|
357 *logging = defaultLogging; |
|
358 *transport = defaultTransport; |
|
359 (*tbAddress) = KDefaultPort; |
|
360 } |
|
361 else |
|
362 { |
|
363 if(lError==KErrNone) |
|
364 { |
|
365 lError=getLogging(lIniFile,logging); |
|
366 |
|
367 if(lError!=KErrNone) |
|
368 { |
|
369 _LIT(KError,"ERROR: Failed to read logging setting in stat.ini file.\n"); |
|
370 console->Printf(KError); |
|
371 } |
|
372 } |
|
373 |
|
374 if(lError==KErrNone) |
|
375 { |
|
376 lError=getTransport(lIniFile,transport); |
|
377 |
|
378 if(lError!=KErrNone) |
|
379 { |
|
380 _LIT(KError,"ERROR: Failed to read transport setting in stat.ini file.\n"); |
|
381 console->Printf(KError); |
|
382 } |
|
383 } |
|
384 |
|
385 if(lError==KErrNone) |
|
386 { |
|
387 lError=getPort(lIniFile,tbAddress); |
|
388 |
|
389 if(lError!=KErrNone) |
|
390 { |
|
391 _LIT(KError,"ERROR: Failed to read COM port address setting in stat.ini file.\n"); |
|
392 console->Printf(KError); |
|
393 } |
|
394 } |
|
395 |
|
396 if(lError==KErrNone) |
|
397 { |
|
398 lError=getDebugPort(lIniFile,&debugPort); |
|
399 |
|
400 if(lError!=KErrNone) |
|
401 { |
|
402 _LIT(KError,"ERROR: Failed to read debug port address setting in stat.ini file.\n"); |
|
403 console->Printf(KError); |
|
404 } |
|
405 } |
|
406 } |
|
407 |
|
408 // Handle any special cases with data. |
|
409 if(lError==KErrNone) |
|
410 { |
|
411 if((*transport==ESerial)||(*transport==EInfrared)) |
|
412 { |
|
413 // Append the baud rate to the COM port text string. |
|
414 tbAddress->Append( _L("|") ); |
|
415 tbAddress->Append( KSerialBaudRate ); |
|
416 } |
|
417 #ifndef LIGHT_MODE |
|
418 else if (*transport == ETCPIP) |
|
419 { |
|
420 // Load TCPIP specific parameters |
|
421 lError = getIAP(lIniFile, tbAddress, mode, index); |
|
422 if (lError!=KErrNone) |
|
423 getSnap(lIniFile, tbAddress, mode, index); |
|
424 |
|
425 // reset error |
|
426 lError = KErrNone; |
|
427 } |
|
428 #endif // ifndef LIGHT_MODE |
|
429 } |
|
430 |
|
431 delete lIniFile; |
|
432 lIniFile = NULL; |
|
433 |
|
434 // Set the debug port to the value from the ini file |
|
435 // of the default value. |
|
436 if(lError==KErrNone) |
|
437 { |
|
438 // Only set the debug port if the user |
|
439 // specified a value. |
|
440 if(debugPort!=invalidValue) |
|
441 { |
|
442 lError = SetDebugPort(debugPort); |
|
443 |
|
444 if(lError!=KErrNone) |
|
445 { |
|
446 _LIT(KError,"ERROR: Failed to set the debug port.\n"); |
|
447 console->Printf(KError); |
|
448 } |
|
449 } |
|
450 } |
|
451 |
|
452 // Display data to the user. |
|
453 if(lError==KErrNone) |
|
454 { |
|
455 // Logging. |
|
456 console->Printf(_L("logging is %S.\n\r"), |
|
457 (0==(*logging))?&KOff:&KOn); |
|
458 |
|
459 // Transport. |
|
460 console->Printf(_L("transport = %S.\n\r"), &(transportText[*transport])); |
|
461 |
|
462 // Port or address. |
|
463 console->Printf(_L("port = %S.\n\r"), tbAddress); |
|
464 |
|
465 // Debug port. |
|
466 if(debugPort!=invalidValue) |
|
467 { |
|
468 console->Printf(_L("debugport = %d.\n\r"), debugPort); |
|
469 } |
|
470 else |
|
471 { |
|
472 console->Printf(_L("debugport is unchanged.\n\r")); |
|
473 } |
|
474 } |
|
475 |
|
476 return lError; |
|
477 } |
|
478 |
|
479 void NotifyStarted() |
|
480 { |
|
481 RNotifier notifier; |
|
482 notifier.Connect(); |
|
483 notifier.InfoPrint(_L("Starting STAT...")); |
|
484 notifier.Close(); |
|
485 } |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 void statmainL( void ) |
|
491 { |
|
492 CActiveScheduler *iActiveScheduler = NULL; |
|
493 CStatController *iController = NULL; |
|
494 TInt sessionid = 0; |
|
495 TInt err = 0; |
|
496 TInt logging = 0; |
|
497 TInt transport = 0; |
|
498 CStatConsole *iConsole = NULL; |
|
499 |
|
500 #ifndef LIGHT_MODE |
|
501 CActiveConnection::TActiveConnectionMode mode = CActiveConnection::EModeIAP; |
|
502 TInt index = 0; |
|
503 #endif // ifndef LIGHT_MODE |
|
504 |
|
505 TBuf<KAddressTextLimit> tbAddress; |
|
506 |
|
507 User::LeaveIfError( iFsSession.Connect() ); |
|
508 iMsg = new FileDump(); |
|
509 |
|
510 NotifyStarted(); //print a notification to the screen |
|
511 |
|
512 #ifdef LIGHT_MODE //lite version can't be hide to background |
|
513 console->Printf( _L("** STAT Lite Version %u.%u.%u **\n\n"),KVersionMajor,KVersionMinor,KVersionPatch); |
|
514 console->Printf( _L("Press ESC to exit\n") ); |
|
515 #else |
|
516 console->Printf( _L("** STAT Version %u.%u.%u **\n\n"),KVersionMajor,KVersionMinor,KVersionPatch); |
|
517 console->Printf( _L("Press ESC to exit, or 'b' to switch to the background\n") ); |
|
518 #endif |
|
519 |
|
520 // get the params for STAT |
|
521 #ifndef LIGHT_MODE |
|
522 err = getparams( &logging, &transport, &tbAddress, &mode, &index ); |
|
523 #else |
|
524 err = getparams( &logging, &transport, &tbAddress ); |
|
525 #endif // ifndef LIGHT_MODE |
|
526 if( err != KErrNone ) { |
|
527 console->Printf( _L("ERROR: Couldn't retrieve parameters.\n") ); |
|
528 console->Printf( _L("Please check stat.ini file.\n") ); |
|
529 console->Printf( _L("Press any key to exit.\n\r") ); |
|
530 console->Getch(); |
|
531 return; |
|
532 } |
|
533 |
|
534 #ifdef LIGHT_MODE //lite version can't be hide to background |
|
535 if (transport != ESerial){ |
|
536 console->Printf( _L("\nERROR: STAT lite only support serial connection.\n") ); |
|
537 console->Printf( _L("Press any key to exit.\n\r") ); |
|
538 console->Getch(); |
|
539 return; |
|
540 } |
|
541 #endif |
|
542 TBuf<KMaxFileName> statLogFile; |
|
543 |
|
544 TDriveNumber defaultSysDrive(EDriveC); |
|
545 RLibrary pluginLibrary; |
|
546 TInt pluginErr = pluginLibrary.Load(KFileSrvDll); |
|
547 |
|
548 if (pluginErr == KErrNone) |
|
549 { |
|
550 typedef TDriveNumber(*fun1)(); |
|
551 fun1 sysdrive; |
|
552 |
|
553 #ifdef __EABI__ |
|
554 sysdrive = (fun1)pluginLibrary.Lookup(336); |
|
555 #else |
|
556 sysdrive = (fun1)pluginLibrary.Lookup(304); |
|
557 #endif |
|
558 |
|
559 if(sysdrive!=NULL) |
|
560 { |
|
561 defaultSysDrive = sysdrive(); |
|
562 } |
|
563 } |
|
564 pluginLibrary.Close(); |
|
565 |
|
566 statLogFile.Append(TDriveUnit(defaultSysDrive).Name()); |
|
567 statLogFile.Append(KFileSeparator); |
|
568 statLogFile.Append(KStatLogFile); |
|
569 |
|
570 if( logging ) |
|
571 { |
|
572 iMsg->Init( iFsSession, statLogFile, console ); |
|
573 } |
|
574 |
|
575 // create an active scheduler |
|
576 iActiveScheduler= new(ELeave) CActiveScheduler; |
|
577 CActiveScheduler::Install( iActiveScheduler ); |
|
578 |
|
579 #ifndef LIGHT_MODE |
|
580 // active object used to start ppp connection, only started if using tcpip |
|
581 CActiveConnection* lAOConnection=CActiveConnection::NewL(_L("81.89.143.203"),80); |
|
582 // if we are using tcpip |
|
583 if(transport == ETCPIP) |
|
584 { |
|
585 lAOConnection->Start(mode, index); |
|
586 } |
|
587 #else |
|
588 if( transport != ESerial ) |
|
589 { |
|
590 console->Printf( _L("ERROR: statlite supports only serial connection.\n") ); |
|
591 console->Printf( _L("Please check stat.ini file.\n") ); |
|
592 console->Printf( _L("Press any key to exit.\n\r") ); |
|
593 console->Getch(); |
|
594 return; |
|
595 } |
|
596 #endif // ifndef LIGHT_MODE |
|
597 |
|
598 // create the console object to receive notifications from the controller |
|
599 iConsole = CStatConsole::NewL(console, iMsg); |
|
600 |
|
601 // create and run the controller |
|
602 iController = CStatController::NewL(); |
|
603 sessionid = iController->StartSession( (enum TStatConnectType)transport, &tbAddress, iConsole, &iFsSession, iMsg ); |
|
604 |
|
605 if(-1 == sessionid) |
|
606 { |
|
607 console->Printf( _L("Initialisation failed. Press ESC to exit.\n") ); |
|
608 } |
|
609 |
|
610 // create active object to monitor console for escape(exit) and b(push to background) |
|
611 CActiveConsole* lAOConsole=CActiveConsole::NewL(console,iController,iConsole,sessionid,iMsg); |
|
612 lAOConsole->Start(); |
|
613 |
|
614 // now wait for events -- there MUST be a read outstanding here |
|
615 // Issue the first request |
|
616 iActiveScheduler->Start(); |
|
617 |
|
618 delete lAOConsole; |
|
619 |
|
620 #ifndef LIGHT_MODE |
|
621 //cancel and destroy activeconsole |
|
622 if(transport == ETCPIP) |
|
623 { |
|
624 lAOConnection->CloseSocket(); |
|
625 } |
|
626 delete lAOConnection; |
|
627 #endif // ifndef LIGHT_MODE |
|
628 |
|
629 // cleanup the controller |
|
630 delete iController; |
|
631 |
|
632 // cleanup the console thing |
|
633 delete iConsole; |
|
634 |
|
635 // cleanup the active scheduler |
|
636 delete iActiveScheduler; |
|
637 |
|
638 if( iMsg->IsInitialised() ) |
|
639 { |
|
640 iMsg->CloseFile(); |
|
641 } |
|
642 |
|
643 iFsSession.Close(); |
|
644 |
|
645 delete iMsg; |
|
646 iMsg = NULL; |
|
647 } |
|
648 |
|
649 /******************************************************************************** |
|
650 * |
|
651 * Notification object |
|
652 * |
|
653 ********************************************************************************/ |
|
654 |
|
655 |