|
1 /* |
|
2 * Copyright (c) 2002-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 /** @file |
|
19 @internalComponent |
|
20 @test |
|
21 */ |
|
22 |
|
23 |
|
24 #include "ncmtestconsole.h" |
|
25 #include "ncmtestdefines.hrh" |
|
26 #include "commandengine.h" |
|
27 #include "devicewatcher.h" |
|
28 #include "servicewatcher.h" |
|
29 #include "ncmwatcher.h" |
|
30 #include "exitcommand.h" |
|
31 #include "setncmiapcommand.h" |
|
32 #include "simpancommand.h" |
|
33 #include "tcpcommand.h" |
|
34 |
|
35 #include <commdb.h> |
|
36 #include <commdbconnpref.h> |
|
37 #include <nifman.h> |
|
38 #include <es_enum.h> |
|
39 |
|
40 //#include <ncm/ncmuiinterfaces.h> |
|
41 using namespace UsbNcm; |
|
42 |
|
43 //The app parameter |
|
44 _LIT(KArgDemo, "DEMO"); |
|
45 |
|
46 //Set the display item length |
|
47 //every display item has fixed length. It will be displayed screen permanently. |
|
48 //The details about how to use display item is in CUsbNcmConsole::DrawL() |
|
49 TInt gDisplayItemLength[] = { |
|
50 DISPLAY_USB_SERVICE_STATE_LEN, //EUsbServiceStateItem |
|
51 DISPLAY_USB_DEVICE_STATE_LEN, //EUsbDeviceStateItem |
|
52 DISPLAY_NCM_CONN_STATE_LEN, //ENcmConnStatusItem |
|
53 DISPLAY_NCM_CONN_IP_LEN, //ENcmConnIpItem |
|
54 DISPLAY_UPLINK_CONN_STATE_LEN, //EUplinkConnStatusItem |
|
55 DISPLAY_UPLINK_CONN_IP_LEN, //EUplinkConnIpItem |
|
56 DISPLAY_NCM_BTPAN_LEN, //ENcmBtPanItem |
|
57 DISPLAY_AGENT_STATE_LEN, //ENcmAgentStateItem |
|
58 DISPLAY_AUTH_SETTING_LEN, //EAuthorisationSettingItem |
|
59 DISPLAY_NCM_CONNECTION_STATE |
|
60 }; |
|
61 |
|
62 void Panic(TInt aPanic) |
|
63 { |
|
64 User::Panic(_L("NcmTestConsole"), aPanic); |
|
65 } |
|
66 |
|
67 |
|
68 void RunConsoleL() |
|
69 { |
|
70 TInt cmdLineLength(User::CommandLineLength()); |
|
71 HBufC* cmdLine = HBufC::NewMaxLC(cmdLineLength); |
|
72 TPtr cmdLinePtr = cmdLine->Des(); |
|
73 User::CommandLine(cmdLinePtr); |
|
74 |
|
75 TLex args(*cmdLine); |
|
76 // args are separated by spaces |
|
77 args.SkipSpace(); |
|
78 |
|
79 TPtrC cmdToken = args.NextToken(); |
|
80 HBufC* tc = HBufC::NewLC(80); |
|
81 *tc = cmdToken; |
|
82 |
|
83 TBool isDemo = EFalse; |
|
84 |
|
85 //Find if there is 'DEMO' or not. |
|
86 while (tc->Length()) |
|
87 { |
|
88 TInt pos = tc->FindF(KArgDemo); |
|
89 if ( pos != KErrNotFound ) |
|
90 { |
|
91 isDemo = ETrue; |
|
92 break; |
|
93 } |
|
94 |
|
95 // next parameter |
|
96 *tc = args.NextToken(); |
|
97 } |
|
98 CleanupStack::PopAndDestroy(tc); |
|
99 CleanupStack::PopAndDestroy(cmdLine); |
|
100 |
|
101 CUsbNcmConsole* console = CUsbNcmConsole::NewLC(isDemo); |
|
102 console->StartL(); |
|
103 CleanupStack::PopAndDestroy(console); |
|
104 } |
|
105 |
|
106 TInt E32Main() |
|
107 { |
|
108 __UHEAP_MARK; |
|
109 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
110 CActiveScheduler* activeScheduler = new CActiveScheduler; |
|
111 TInt err = KErrNoMemory; |
|
112 if(cleanup && activeScheduler) |
|
113 { |
|
114 CActiveScheduler::Install(activeScheduler); |
|
115 TRAP(err,RunConsoleL()); |
|
116 } |
|
117 delete activeScheduler; |
|
118 delete cleanup; |
|
119 __UHEAP_MARKEND; |
|
120 return err; |
|
121 } |
|
122 |
|
123 CUsbNcmConsoleEvent* CUsbNcmConsoleEvent::NewL() |
|
124 { |
|
125 CUsbNcmConsoleEvent* self = new(ELeave) CUsbNcmConsoleEvent(); |
|
126 CleanupStack::PushL(self); |
|
127 self->ConstructL(); |
|
128 CleanupStack::Pop(self); |
|
129 return self; |
|
130 |
|
131 } |
|
132 |
|
133 CUsbNcmConsoleEvent::CUsbNcmConsoleEvent() |
|
134 { |
|
135 } |
|
136 |
|
137 CUsbNcmConsoleEvent::~CUsbNcmConsoleEvent() |
|
138 { |
|
139 iLink.Deque(); |
|
140 iEvent.Close(); |
|
141 } |
|
142 |
|
143 void CUsbNcmConsoleEvent::ConstructL() |
|
144 { |
|
145 User::LeaveIfError(iEvent.Create(NUM_CHARACTERS_ON_LINE-1)); |
|
146 } |
|
147 |
|
148 |
|
149 CUsbNcmConsole* CUsbNcmConsole::NewLC(TBool aDemo) |
|
150 /** |
|
151 Constructs a CUsbNcmConsole object. |
|
152 @param aDemo Indicate start NCM control app and NCM automaticly or manually |
|
153 |
|
154 When iDemo is ETrue, means ncmtestconsole starts as a demo. It will ask user to set ncm iap and |
|
155 uplink iap.Then user can experience the NCM function by plug/unplug usb cable. |
|
156 */ |
|
157 { |
|
158 LOG_STATIC_FUNC_ENTRY |
|
159 |
|
160 CUsbNcmConsole* self = new(ELeave) CUsbNcmConsole(aDemo); |
|
161 CleanupStack::PushL(self); |
|
162 self->ConstructL(); |
|
163 return self; |
|
164 } |
|
165 |
|
166 CUsbNcmConsole::~CUsbNcmConsole() |
|
167 { |
|
168 LOG_FUNC |
|
169 |
|
170 //If Ncm control app is not stopped, stop it. |
|
171 //destroy all commands |
|
172 delete iKeys; |
|
173 |
|
174 //destroy event list |
|
175 TDblQueIter<CUsbNcmConsoleEvent> iter(iEventList); |
|
176 CUsbNcmConsoleEvent* event = NULL; |
|
177 while((event = iter++) != NULL) |
|
178 { |
|
179 delete event; |
|
180 } |
|
181 |
|
182 //delete iStartWatcherCallback; |
|
183 |
|
184 //destroy all watches |
|
185 delete iSharedStateWatcher; |
|
186 delete iDeviceWatcher; |
|
187 delete iServiceWatcher; |
|
188 |
|
189 iUsb.Close(); |
|
190 |
|
191 delete iConsole; |
|
192 |
|
193 //Destroy all display items |
|
194 for(TInt i=0; i<ELastItem; i++) |
|
195 { |
|
196 iDisplayArray[i].Close(); |
|
197 } |
|
198 iDisplayArray.Close(); |
|
199 |
|
200 Cancel(); |
|
201 |
|
202 } |
|
203 |
|
204 CUsbNcmConsole::CUsbNcmConsole(TBool aDemo) |
|
205 : CActive(EPriorityLow) // Low so all notifications that want to be serviced will be done first |
|
206 , iHelp(EFalse) |
|
207 , iDemo(aDemo) |
|
208 , iEventList(_FOFF(CUsbNcmConsoleEvent, iLink)) |
|
209 { |
|
210 CActiveScheduler::Add(this); |
|
211 } |
|
212 |
|
213 void CUsbNcmConsole::ConstructL() |
|
214 { |
|
215 LOG_FUNC |
|
216 |
|
217 //Init Display Item |
|
218 iDisplayArray.ReserveL(ELastItem); |
|
219 CreateDisplayItemL(); |
|
220 |
|
221 iConsole = Console::NewL(KUsbNcmConsoleTitle, TSize(-1,-1)); |
|
222 User::LeaveIfError(iUsb.Connect()); |
|
223 __FLOG_STATIC0(KSubSys, KLogComponent , _L8("iUsb.Connect() successful")); |
|
224 |
|
225 //Init event list |
|
226 for(TInt i=0; i< NUM_EVENT_ON_SCREEN; ++i) |
|
227 { |
|
228 CUsbNcmConsoleEvent* nullEvent = CUsbNcmConsoleEvent::NewL(); |
|
229 iEventList.AddFirst(*nullEvent); |
|
230 } |
|
231 __FLOG_STATIC0(KSubSys, KLogComponent , _L8("Create event list successful")); |
|
232 |
|
233 |
|
234 //Start watcher |
|
235 //iDeviceWatcher = CDeviceWatcher::NewL(*this); |
|
236 //iServiceWatcher = CServiceWatcher::NewL(*this); |
|
237 // iUplinkConnectionWatcher = CUplinkWatcher::NewL(*this); |
|
238 |
|
239 __FLOG_STATIC0(KSubSys, KLogComponent , _L8("Create watchers successful")); |
|
240 |
|
241 //create command engine |
|
242 iKeys = CNcmCommandEngine::NewL(*this); |
|
243 |
|
244 //add commands |
|
245 CNcmCommandBase* command = NULL; |
|
246 |
|
247 //the command to set ncm iap |
|
248 command = CSetNcmIapCommand::NewL(*this, 'n'); |
|
249 iKeys->RegisterCommand(command); |
|
250 //Get the default iap used by ncm |
|
251 TInt defaultIap = 0; |
|
252 ((CSetNcmIapCommand*)command)->GetDefaultIapL(defaultIap); |
|
253 SetLocalIapId(defaultIap); |
|
254 |
|
255 //the command to simulate PAN actvie/inactive |
|
256 command = CSimPANCommand::NewL(*this, 'p'); |
|
257 iKeys->RegisterCommand(command); |
|
258 |
|
259 //the command to create a connection from PC to H4 and make data transfer |
|
260 command = CTcpCommand::NewL(*this, 'v', ETrue, ETrue); |
|
261 iKeys->RegisterCommand(command); |
|
262 //the command to create a connection from H4 to PC and make data transfer |
|
263 command = CTcpCommand::NewL(*this, 'i', ETrue, EFalse); |
|
264 iKeys->RegisterCommand(command); |
|
265 |
|
266 command = CTcpCommand::NewL(*this , 'r', EFalse, ETrue); |
|
267 iKeys->RegisterCommand(command); |
|
268 command = CTcpCommand::NewL(*this , 's', EFalse, EFalse); |
|
269 iKeys->RegisterCommand(command); |
|
270 |
|
271 //here to add new command |
|
272 |
|
273 //the command to quit |
|
274 command = CExitCommand::NewL(*this, 'q'); |
|
275 iKeys->RegisterCommand(command); |
|
276 |
|
277 //Monitor the status published by NCM agent |
|
278 iSharedStateWatcher = CSharedStateWatcher::NewL(*this); |
|
279 } |
|
280 |
|
281 void CUsbNcmConsole::StartL() |
|
282 { |
|
283 //Output the init state to screen |
|
284 DrawL(); |
|
285 //Do StartWatcherCommand if 'demo' is the command parameter |
|
286 if(iDemo) |
|
287 { |
|
288 // iStartWatcherCallback->CallBack(); |
|
289 } |
|
290 CActiveScheduler::Start(); |
|
291 } |
|
292 |
|
293 void CUsbNcmConsole::Stop() const |
|
294 { |
|
295 CActiveScheduler::Stop(); |
|
296 } |
|
297 |
|
298 void CUsbNcmConsole::DoCancel() |
|
299 { |
|
300 } |
|
301 |
|
302 void CUsbNcmConsole::RunL() |
|
303 { |
|
304 DrawL(); |
|
305 } |
|
306 |
|
307 TInt CUsbNcmConsole::RunError(TInt aError) |
|
308 { |
|
309 User::Panic(_L("CUsbNcmConsole"), aError); |
|
310 return aError; |
|
311 } |
|
312 |
|
313 void CUsbNcmConsole::ScheduleDraw(TUint aKey) |
|
314 /** |
|
315 Called by commands and monitors.To notify main console there's new infomation to show. |
|
316 @param aKey Display type. '1'-'9' - display the command help |
|
317 'E' - display the events. |
|
318 '0' - back from command help |
|
319 */ |
|
320 { |
|
321 iHelp = aKey; |
|
322 if(!IsActive()) |
|
323 { |
|
324 SetActive(); |
|
325 TRequestStatus* status = &iStatus; |
|
326 User::RequestComplete(status, KErrNone); |
|
327 } |
|
328 } |
|
329 |
|
330 RUsb& CUsbNcmConsole::Usb() |
|
331 { |
|
332 return iUsb; |
|
333 } |
|
334 |
|
335 void CUsbNcmConsole::SetLocalIapId(TInt aId) |
|
336 /** |
|
337 Set the Ncm Iap which will be monitored and displayed on screen |
|
338 @param aId The Ncm Iap Id |
|
339 */ |
|
340 { |
|
341 iLocalIapId = aId; |
|
342 ScheduleDraw('E'); |
|
343 } |
|
344 |
|
345 void CUsbNcmConsole::NotifyEvent(CUsbNcmConsoleEvent* aEvent) |
|
346 /** |
|
347 Called by commands and monitors. To tell main console to display the new event. |
|
348 @param aEvent Event reported by commands and monitors |
|
349 */ |
|
350 { |
|
351 //Console can't display a NULL event |
|
352 __ASSERT_ALWAYS(aEvent, Panic(ENcmEventIsNull)); |
|
353 //Console can't display a string more than one line |
|
354 __ASSERT_ALWAYS(aEvent->iEvent.Length() <= NUM_CHARACTERS_ON_LINE, Panic(ENcmEventLengthValid)); |
|
355 |
|
356 iEventList.AddFirst(*aEvent); |
|
357 CUsbNcmConsoleEvent* theLastEvent = iEventList.Last(); |
|
358 delete theLastEvent; |
|
359 |
|
360 ScheduleDraw('E'); |
|
361 } |
|
362 |
|
363 void CUsbNcmConsole::DrawL() |
|
364 /** |
|
365 Display all infomation on screen. |
|
366 There are two parts on the screen. One is permanent info. It will display all kinds of states such as USB device state, |
|
367 USB service state, NCM state, and so on. The other is event info show. It will display the events reported by monitors |
|
368 and commands. It also used to show command help when user need command help. |
|
369 */ |
|
370 { |
|
371 iConsole->ClearScreen(); |
|
372 // First line is the server version number |
|
373 TVersionName versionName = iVersion.Name(); |
|
374 iConsole->Printf(_L( |
|
375 // 1 2 3 4 5 |
|
376 // 12345678901234567890123456789012345678901234567890123 |
|
377 "Server Version: %S %S\n" |
|
378 ), |
|
379 &versionName, &iDisplayArray[EAuthorisationSettingItem] |
|
380 ); |
|
381 |
|
382 |
|
383 iConsole->Printf(_L("Service: %S Device: %S\n"), |
|
384 &iDisplayArray[EUsbServiceStateItem], &iDisplayArray[EUsbDeviceStateItem]); |
|
385 iConsole->Printf(_L("NCM Iap:%2d state:%S IP:%S\n"), |
|
386 iLocalIapId, &iDisplayArray[ENcmConnStatusItem], &iDisplayArray[ENcmConnIpItem]); |
|
387 iConsole->Printf(_L("Active:%S NcmConnection:%S\n"), |
|
388 &iDisplayArray[ENcmBtPanItem], &iDisplayArray[ENcmAgentStateItem]); |
|
389 iConsole->Printf(_L("Ncm connection:%S\n"), &iDisplayArray[ENcmConnectionStateItem]); |
|
390 |
|
391 if ((iHelp > 0) && (iHelp <=9)) |
|
392 { |
|
393 iConsole->Printf(_L("Press 0 to quit help\n\n")); |
|
394 //Display the command help |
|
395 iKeys->PrintHelp(iHelp); |
|
396 } |
|
397 else |
|
398 { |
|
399 iConsole->Printf(_L("Press 1 to 9 to get menu help\n\n")); |
|
400 //Display the event list |
|
401 TDblQueIter<CUsbNcmConsoleEvent> iter(iEventList); |
|
402 CUsbNcmConsoleEvent* event = NULL; |
|
403 while((event = iter++) != NULL) |
|
404 { |
|
405 iConsole->Printf(event->iEvent.Left(NUM_CHARACTERS_ON_LINE-1)); |
|
406 iConsole->Printf(_L("\n")); |
|
407 } |
|
408 } |
|
409 } |
|
410 |
|
411 |
|
412 void CUsbNcmConsole::CreateDisplayItemL() |
|
413 /** |
|
414 Init the display item. |
|
415 */ |
|
416 { |
|
417 for(TInt i = 0; i < ELastItem; i++) |
|
418 { |
|
419 RBuf buf; |
|
420 iDisplayArray.AppendL(buf); |
|
421 iDisplayArray[i].CreateL(gDisplayItemLength[i]); |
|
422 } |
|
423 } |
|
424 |
|
425 void CUsbNcmConsole::SetDisplayItem(TInt aIndex, TDes& aInfo) |
|
426 /** |
|
427 Called by commands and monitors to show the changed info in screen |
|
428 */ |
|
429 { |
|
430 //Prevent array is out of bound |
|
431 __ASSERT_ALWAYS(((aIndex < ELastItem) && (aIndex >= 0)), Panic(ENcmDisplayItemVaild)); |
|
432 |
|
433 iDisplayArray[aIndex].SetLength(0); |
|
434 iDisplayArray[aIndex].Copy(aInfo); |
|
435 |
|
436 ScheduleDraw('E'); |
|
437 } |
|
438 |
|
439 TInt CUsbNcmConsole::LocalIapId() const |
|
440 { |
|
441 return iLocalIapId; |
|
442 } |
|
443 |
|
444 const TDes& CUsbNcmConsole::GetDisplayItem(TInt aIndex) |
|
445 { |
|
446 //Prevent array is out of bound |
|
447 __ASSERT_ALWAYS(((aIndex < ELastItem) && (aIndex >= 0)), Panic(ENcmDisplayItemVaild)); |
|
448 |
|
449 return iDisplayArray[aIndex]; |
|
450 } |
|
451 |
|
452 |
|
453 TInt CUsbNcmConsole::DoCommandCallback(TAny *aThisPtr) |
|
454 { |
|
455 CNcmCommandBase* command = static_cast<CNcmCommandBase*>(aThisPtr); |
|
456 TRAPD(err,command->DoCommandL()); |
|
457 if(err != KErrNone) |
|
458 { |
|
459 User::Panic(_L("DoCommandCallback"), err); |
|
460 } |
|
461 return err; |
|
462 } |
|
463 |
|
464 |
|
465 TInt CUsbNcmConsole::DrawAvailableIapsL() |
|
466 /** |
|
467 Display commsdb's iaps on console |
|
468 @return the count of iaps |
|
469 */ |
|
470 { |
|
471 LOG_FUNC |
|
472 __FLOG_STATIC0(KSubSys, KLogComponent , _L8("CNcmTestUtil::DrawAvailableIapsL")); |
|
473 |
|
474 iConsole->Printf(_L("CommDB IAPs:\n")); |
|
475 iConsole->Printf(_L("ID: Name\n")); |
|
476 |
|
477 //Get commsdb handle |
|
478 CCommsDatabase* db = CCommsDatabase::NewL(); |
|
479 __FLOG_STATIC0(KSubSys, KLogComponent , _L8("Get CommsDatabase ok")); |
|
480 CleanupStack::PushL(db); |
|
481 |
|
482 //Get Iap table infomation |
|
483 CCommsDbTableView* iapTableView = db->OpenTableLC(TPtrC(IAP)); |
|
484 __FLOG_STATIC0(KSubSys, KLogComponent , _L8("OpenTableLC ok")); |
|
485 |
|
486 |
|
487 TUint32 iapId; |
|
488 TBuf<128> iapName; |
|
489 TInt iapCount = 0; |
|
490 |
|
491 for (TInt iapTableErr = iapTableView->GotoFirstRecord(); iapTableErr == KErrNone; iapTableErr = iapTableView->GotoNextRecord()) |
|
492 { |
|
493 //Get every IAP |
|
494 iapTableView->ReadTextL(TPtrC(COMMDB_NAME), iapName); |
|
495 iapTableView->ReadUintL(TPtrC(COMMDB_ID), iapId); |
|
496 |
|
497 __FLOG_STATIC2(KSubSys, KLogComponent , _L8("Read: %d: %S"), iapId, &iapName); |
|
498 |
|
499 iConsole->Printf(_L("%d: %S\n"), iapId, &iapName); |
|
500 iapCount++; |
|
501 } |
|
502 |
|
503 if (0 == iapCount) |
|
504 { |
|
505 iConsole->Printf(_L("No available IAP!\n")); |
|
506 } |
|
507 |
|
508 CleanupStack::PopAndDestroy(2); // iapTableView, db |
|
509 return iapCount; |
|
510 |
|
511 } |
|
512 |
|
513 TBool CUsbNcmConsole::AttachConnectionL(TInt aIap, RConnection& aConnection) |
|
514 /** |
|
515 Try to attach the connection specified by iIapId. If attach failed or no specified connection,return false |
|
516 */ |
|
517 { |
|
518 LOG_FUNC |
|
519 __FLOG_STATIC1(KSubSys, KLogComponent , _L8("AttachConnectionL aIap=%d"), aIap); |
|
520 |
|
521 TBool result = ETrue; |
|
522 TUint count = 0; |
|
523 //Get active connections |
|
524 User::LeaveIfError(aConnection.EnumerateConnections(count)); |
|
525 __FLOG_STATIC2(KSubSys, KLogComponent , _L8("active connection count = %d, watch iap = %d"), count, aIap); |
|
526 |
|
527 TBool hasConnection = EFalse; |
|
528 for(TInt i = 1; i <= count; i++) |
|
529 { |
|
530 TPckgBuf<TConnectionInfo> info; |
|
531 User::LeaveIfError(aConnection.GetConnectionInfo(i,info)); |
|
532 TUint32 id = info().iIapId; |
|
533 __FLOG_STATIC2(KSubSys, KLogComponent , _L8("aIap=%d, id=%d"), aIap, id); |
|
534 |
|
535 if(id == aIap) |
|
536 { |
|
537 hasConnection = ETrue; |
|
538 //The connection is the connection specified by aIap |
|
539 __FLOG_STATIC0(KSubSys, KLogComponent , _L8("Got it!")); |
|
540 |
|
541 //Attach the connection |
|
542 TInt err = aConnection.Attach(info, RConnection::EAttachTypeMonitor); |
|
543 if(err != KErrNone) |
|
544 { |
|
545 result = EFalse; |
|
546 __FLOG_STATIC1(KSubSys, KLogComponent , _L8("Attach error[%d]"), err); |
|
547 } |
|
548 else |
|
549 { |
|
550 __FLOG_STATIC0(KSubSys, KLogComponent , _L8("Attach it!")); |
|
551 } |
|
552 break; |
|
553 } |
|
554 } |
|
555 |
|
556 if(!hasConnection) |
|
557 { |
|
558 //connection not found; |
|
559 __FLOG_STATIC0(KSubSys, KLogComponent , _L8("No connection found!")); |
|
560 result = EFalse; |
|
561 } |
|
562 |
|
563 return result; |
|
564 |
|
565 } |