|
1 // Copyright (c) 2005-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 // THostRes.CPP |
|
15 // BT Host Resolver test code |
|
16 // |
|
17 // |
|
18 |
|
19 #include "tcsy.h" |
|
20 #include <in_sock.h> |
|
21 #include <e32std.h> |
|
22 #include <bluetooth/logger.h> |
|
23 |
|
24 #if defined (__WINS__) |
|
25 #define PDD_NAME _L("ECDRV") |
|
26 #define LDD_NAME _L("ECOMM") |
|
27 #else // __GCC32__ |
|
28 #define PDD_NAME _L("EUART1") |
|
29 #define LDD_NAME _L("ECOMM") |
|
30 #endif |
|
31 |
|
32 #ifdef __FLOG_ACTIVE |
|
33 _LIT8(KLogComponent, LOG_COMPONENT_BT_COMM_TCSY); |
|
34 #endif |
|
35 |
|
36 #ifdef _DEBUG |
|
37 PANICCATEGORY("btcommtcsy"); |
|
38 #endif |
|
39 |
|
40 //GLDEF_D RTest test(_L("CSY open/close tests")); |
|
41 |
|
42 GLDEF_D TSize gSmallConsole(30,10); |
|
43 GLDEF_D TInt gPanics=0; |
|
44 GLDEF_D TInt gServerReboots=0; |
|
45 |
|
46 // define/declare statics |
|
47 TInt CCSY::iCommInstance = 0; |
|
48 |
|
49 void LoadLDD_PDD() |
|
50 { |
|
51 RTest test(_L("LoadLDD_PDD")); |
|
52 TInt r; |
|
53 |
|
54 r=StartC32(); |
|
55 if (r!=KErrNone && r!=KErrAlreadyExists) |
|
56 { |
|
57 test.Printf(_L("Failed %d!\n\r"),r); |
|
58 test(r==KErrNone); |
|
59 } |
|
60 else |
|
61 test.Printf(_L("Started C32\n")); |
|
62 |
|
63 test.Printf(_L("Loading PDD\n")); |
|
64 r=User::LoadPhysicalDevice(PDD_NAME); |
|
65 if (r!=KErrNone && r!=KErrAlreadyExists) |
|
66 { |
|
67 test.Printf(_L("Failed %d!\n\r"),r); |
|
68 test(r==KErrNone); |
|
69 } |
|
70 else |
|
71 test.Printf(_L("Loaded LDD\n")); |
|
72 test.Printf(_L("Loading LDD\n")); |
|
73 r=User::LoadLogicalDevice(LDD_NAME); |
|
74 if (r!=KErrNone && r!=KErrAlreadyExists) |
|
75 { |
|
76 test.Printf(_L("Failed %d!\n\r"),r); |
|
77 test(r==KErrNone); |
|
78 } |
|
79 else |
|
80 test.Printf(_L("Loaded PDD\n")); |
|
81 test.Close(); |
|
82 } |
|
83 |
|
84 // ---------------------------------------------------------------------------- |
|
85 // CSY App |
|
86 // ---------------------------------------------------------------------------- |
|
87 |
|
88 CCSYApp::CCSYApp(CConsoleBase& aConsole) |
|
89 : iConsole(aConsole) |
|
90 { |
|
91 } |
|
92 |
|
93 CCSYApp::~CCSYApp() |
|
94 { |
|
95 DeleteAllConnections(); |
|
96 iConnections.Close(); |
|
97 iCommServer.UnloadCommModule(_L("BTCOMM")); |
|
98 iCommServer.Close(); |
|
99 delete iJimmier; |
|
100 delete iActiveRootConsole; |
|
101 } |
|
102 |
|
103 CCSYApp* CCSYApp::NewL(CConsoleBase& aConsole, TBool aAutoAuto) |
|
104 { |
|
105 CCSYApp* thisapp = new (ELeave) CCSYApp(aConsole); |
|
106 CleanupStack::PushL(thisapp); |
|
107 thisapp->ConstructL(aConsole, aAutoAuto); |
|
108 CleanupStack::Pop(); |
|
109 return thisapp; |
|
110 } |
|
111 |
|
112 void CCSYApp::ConstructL(CConsoleBase& aConsole, TBool aAutoAuto) |
|
113 { |
|
114 iActiveRootConsole = CActiveRootConsole::NewL(aConsole, *this); |
|
115 iJimmier = CAutoJimmier::NewL(aConsole, *this); |
|
116 User::LeaveIfError(iCommServer.Connect()); |
|
117 |
|
118 if (!aAutoAuto) |
|
119 AutoMode(); |
|
120 } |
|
121 |
|
122 void CCSYApp::StartL() |
|
123 { |
|
124 // go .... |
|
125 CActiveScheduler::Start(); |
|
126 } |
|
127 |
|
128 void CCSYApp::Stop() |
|
129 { |
|
130 CActiveScheduler::Stop(); |
|
131 } |
|
132 |
|
133 void CCSYApp::NewCSYL(TBool aStartReading) |
|
134 { |
|
135 CCSY* csy=NULL; |
|
136 |
|
137 do |
|
138 { |
|
139 TRAPD(err, csy = CCSY::NewL(iConsole, *this, iCommServer)); |
|
140 |
|
141 if (err == KErrServerTerminated) |
|
142 { |
|
143 //restart c32...not sure why it terminated tho... |
|
144 iConsole.Printf(_L("Restarting c32...\n")); |
|
145 ++gServerReboots; |
|
146 iCommServer.Close(); |
|
147 iCommServer.Connect(); |
|
148 } |
|
149 else if (err != KErrNone) |
|
150 { |
|
151 User::Leave(err); |
|
152 } |
|
153 } while (csy == NULL); |
|
154 |
|
155 iConnections.Append(csy); |
|
156 if (aStartReading) |
|
157 csy->Read(); |
|
158 } |
|
159 |
|
160 void CCSYApp::SingleCSYL() |
|
161 { |
|
162 if (iConnections.Count() >= 1) |
|
163 { |
|
164 iConsole.Printf(_L("RComm instance already open (on session %d)\n"), gServerReboots); |
|
165 return; |
|
166 } |
|
167 NewCSYL(ETrue); |
|
168 } |
|
169 |
|
170 void CCSYApp::DeleteAllConnections() |
|
171 { |
|
172 iConnections.ResetAndDestroy(); |
|
173 } |
|
174 |
|
175 void CCSYApp::DeleteConnection(const CCSY* aComm) |
|
176 { |
|
177 TInt index = iConnections.Find(aComm); |
|
178 ASSERT_DEBUG(index != KErrNotFound); |
|
179 |
|
180 delete iConnections[index]; |
|
181 iConnections.Remove(index); |
|
182 } |
|
183 |
|
184 void CCSYApp::AutoMode() |
|
185 { |
|
186 iJimmier->StartTimer(); |
|
187 } |
|
188 |
|
189 void CCSYApp::ReStartAutoMode() |
|
190 { |
|
191 DeleteAllConnections(); |
|
192 AutoMode(); |
|
193 } |
|
194 |
|
195 // Playpen... |
|
196 |
|
197 void CCSYApp::FetchWebPageL() |
|
198 { |
|
199 typedef TBuf8<4096> TBuffer; |
|
200 |
|
201 RSocketServ socketServer; |
|
202 User::LeaveIfError(socketServer.Connect()); |
|
203 |
|
204 RSocket tcpSocket; |
|
205 User::LeaveIfError(tcpSocket.Open(socketServer, _L("TCPIP6"))); |
|
206 |
|
207 // address |
|
208 TInetAddr remoteServer; |
|
209 |
|
210 const TUint32 KRemoteServer = INET_ADDR(194,129,2,54); |
|
211 remoteServer.SetAddress(KRemoteServer); |
|
212 |
|
213 // const TUint KHttpPort = 80; |
|
214 |
|
215 TRequestStatus status; |
|
216 |
|
217 iConsole.Printf(_L("Playpen = making TCP connection...")); |
|
218 tcpSocket.Connect(remoteServer, status); |
|
219 |
|
220 |
|
221 User::WaitForRequest(status); |
|
222 User::LeaveIfError(status.Int()); |
|
223 // |
|
224 |
|
225 iConsole.Printf(_L("Connect OK, Send HTML request\r\n")); |
|
226 |
|
227 TBuffer* wtemp=new TBuffer; |
|
228 TBuffer& wbuf=*wtemp; |
|
229 |
|
230 TBuffer* rtemp=new TBuffer; |
|
231 TBuffer& rbuf=*rtemp; |
|
232 |
|
233 wtemp->Zero(); |
|
234 |
|
235 // Build an HTML request |
|
236 wtemp->Copy(_L("GET ")); |
|
237 const TBufC8<40> url(_L8("index.html")); |
|
238 wtemp->Append(url); |
|
239 wtemp->Append(_L(" \r\n\r\n")); |
|
240 //Send the request |
|
241 tcpSocket.Write(wbuf, status); |
|
242 User::WaitForRequest(status); |
|
243 if(status!=KErrNone) |
|
244 { |
|
245 iConsole.Printf(_L("Send Failed %d\n"), status.Int()); |
|
246 tcpSocket.Close(); |
|
247 delete wtemp; |
|
248 delete rtemp; |
|
249 return; |
|
250 } |
|
251 |
|
252 iConsole.Printf(_L("Send OK, Recv HTML page \r\n")); |
|
253 |
|
254 iConsole.Printf(_L("\r\nUrl: ")); |
|
255 iConsole.Printf(_L("%S"), &url); |
|
256 iConsole.Printf(_L("\r\n")); |
|
257 |
|
258 // Send Okay, Recv HTML page |
|
259 // Get IP Address |
|
260 |
|
261 // Wait for page |
|
262 TSockXfrLength HTTPHeaderLen; |
|
263 TInt rcount = 0; |
|
264 FOREVER |
|
265 { |
|
266 rtemp->Zero(); |
|
267 tcpSocket.RecvOneOrMore(rbuf,0,status, HTTPHeaderLen); |
|
268 User::WaitForRequest(status); |
|
269 if(status!=KErrNone) |
|
270 break; |
|
271 rcount += rbuf.Length(); |
|
272 iConsole.SetPos(0); |
|
273 iConsole.Printf(_L("Recved:%5dBytes"), rcount); |
|
274 } |
|
275 tcpSocket.Close(); |
|
276 if(status!=KErrEof) |
|
277 { |
|
278 // Read Failed |
|
279 // Read Error Code |
|
280 // Error Message Convert TInt to TBuf |
|
281 iConsole.Printf(_L("Read Failed %d\n"), status.Int()); |
|
282 } |
|
283 else |
|
284 { |
|
285 // Read Okay |
|
286 iConsole.Printf(_L(" - Recv HTML page, OK - Test Finished\r\n")); |
|
287 } |
|
288 |
|
289 delete wtemp; |
|
290 delete rtemp; |
|
291 } |
|
292 |
|
293 void CCSYApp::C32UHeapMark() |
|
294 { |
|
295 iCommServer.__DbgMarkHeap(); |
|
296 } |
|
297 |
|
298 void CCSYApp::C32UHeapMarkEnd() |
|
299 { |
|
300 iCommServer.__DbgMarkEnd(0); |
|
301 } |
|
302 |
|
303 void CCSYApp::StartExclusiveSessionL() |
|
304 { |
|
305 User::LeaveIfError(iExclusiveSession.Connect()); |
|
306 TRequestStatus status; |
|
307 iExclusiveSession.SetExclusiveMode(status); |
|
308 User::WaitForRequest(status); |
|
309 } |
|
310 |
|
311 void CCSYApp::StopExclusiveSession() |
|
312 { |
|
313 iExclusiveSession.ClearExclusiveMode(); |
|
314 iExclusiveSession.Close(); |
|
315 } |
|
316 |
|
317 // ---------------------------------------------------------------------------- |
|
318 // Auto Jimmier |
|
319 // ---------------------------------------------------------------------------- |
|
320 |
|
321 CAutoJimmier* CAutoJimmier::NewL(CConsoleBase& aConsole, CCSYApp& aApp) |
|
322 { |
|
323 CAutoJimmier* c = new (ELeave) CAutoJimmier(aConsole, aApp); |
|
324 CleanupStack::PushL(c); |
|
325 c->ConstructL(); |
|
326 CleanupStack::Pop(c); |
|
327 return c; |
|
328 } |
|
329 |
|
330 void CAutoJimmier::ConstructL() |
|
331 { |
|
332 CTimer::ConstructL(); |
|
333 CActiveScheduler::Add(this); |
|
334 } |
|
335 |
|
336 CAutoJimmier::CAutoJimmier(CConsoleBase& aConsole, CCSYApp& aApp) |
|
337 : CTimer(EPriorityStandard), iConsole(aConsole), iApp(aApp) |
|
338 { |
|
339 } |
|
340 |
|
341 void CAutoJimmier::StartTimer() |
|
342 { |
|
343 if (IsActive()) |
|
344 return; |
|
345 |
|
346 TInt time; |
|
347 if (iRemove) |
|
348 time = KAutoRemoveTime; |
|
349 else |
|
350 time = KAutoStartTime; |
|
351 |
|
352 iConsole.Printf(_L("Queing timer of %d musec\n"), time); |
|
353 After(time); |
|
354 } |
|
355 |
|
356 void CAutoJimmier::RunL() |
|
357 { |
|
358 // if (Math::Rand(iSeed) < KMaxTInt/10) //random version |
|
359 if (iRemove) |
|
360 { |
|
361 iConsole.Printf(_L("Auto removing all RComm...\n")); |
|
362 iApp.C32UHeapMarkEnd(); |
|
363 iApp.DeleteAllConnections(); |
|
364 } |
|
365 else |
|
366 { |
|
367 iConsole.Printf(_L("Auto starting RComm...\n")); |
|
368 iApp.C32UHeapMark(); |
|
369 // iApp.NewCSYL(ETrue); // for multiple service/device testing |
|
370 iApp.SingleCSYL(); |
|
371 } |
|
372 |
|
373 iRemove=!iRemove; |
|
374 StartTimer(); |
|
375 } |
|
376 |
|
377 TInt CAutoJimmier::RunError(TInt aError) |
|
378 { |
|
379 if (aError == KErrNoMemory) |
|
380 { |
|
381 iConsole.Printf(_L("****Hey! Out of memory!***\nAuto-restarting...\n")); |
|
382 iApp.ReStartAutoMode(); |
|
383 // CActiveScheduler::Stop(); |
|
384 } |
|
385 |
|
386 return KErrNone; |
|
387 } |
|
388 |
|
389 // ---------------------------------------------------------------------------- |
|
390 // CSY Connection |
|
391 // ---------------------------------------------------------------------------- |
|
392 |
|
393 CCSY::CCSY(CConsoleBase& aConsole, CCSYApp& aApp, RCommServ& aCommServer) |
|
394 : CActive(EPriorityStandard), iRootConsole(aConsole), iApp(aApp), iCommServer(aCommServer) |
|
395 { |
|
396 CActiveScheduler::Add(this); |
|
397 ++iCommInstance; |
|
398 } |
|
399 |
|
400 CCSY::~CCSY() |
|
401 { |
|
402 Cancel(); |
|
403 |
|
404 if (iComm.SubSessionHandle()) |
|
405 iComm.Close(); |
|
406 |
|
407 delete iActiveCommConsole; |
|
408 delete iCommConsole; |
|
409 |
|
410 --iCommInstance; //funny, but static! |
|
411 } |
|
412 |
|
413 CCSY* CCSY::NewL(CConsoleBase& aConsole, CCSYApp& aApp, RCommServ& aCommServer) |
|
414 { |
|
415 CCSY* thisconnection = new (ELeave) CCSY(aConsole, aApp, aCommServer); |
|
416 CleanupStack::PushL(thisconnection); |
|
417 thisconnection->ConstructL(); |
|
418 CleanupStack::Pop(); |
|
419 return thisconnection; |
|
420 } |
|
421 |
|
422 void CCSY::ConstructL() |
|
423 { |
|
424 TBuf<8> title(_L("RComm")); |
|
425 TInt err; |
|
426 title.AppendNum(iCommInstance); |
|
427 iCommConsole = AutoSizeNewL(title, gSmallConsole); |
|
428 iActiveCommConsole = CActiveCommConsole::NewL(*iCommConsole, *this); |
|
429 iRootConsole.Printf(_L("Loading BTCOMM module\n")); |
|
430 err = iCommServer.LoadCommModule(_L("BTCOMM")); |
|
431 if (err != KErrNone) |
|
432 { |
|
433 iRootConsole.Printf(_L("ERROR %d opening BTCOMM...restarting c32\n"), err); |
|
434 //not sure why this happens yet! |
|
435 // User::After(10000000); |
|
436 User::Leave(err); |
|
437 } |
|
438 |
|
439 iRootConsole.Printf(_L("Open BTCOMM serial port in exclusive mode\r\n")); |
|
440 |
|
441 TBuf<10> commname(_L("BTCOMM::")); |
|
442 // commname.AppendNum(iCommInstance-1); |
|
443 commname.AppendNum(0); |
|
444 |
|
445 User::LeaveIfError(iComm.Open(iCommServer,commname,ECommExclusive)); |
|
446 } |
|
447 |
|
448 CConsoleBase* CCSY::AutoSizeNewL(const TDesC& aTitle, TSize aSize) |
|
449 { |
|
450 // Try to create a console of our preferred size, otherwise we |
|
451 // guess the screen is too small and use its entirety. |
|
452 CConsoleBase* console = NULL; |
|
453 TRAPD(err, console = Console::NewL(aTitle, aSize)); |
|
454 if (err != KErrNone) |
|
455 { |
|
456 // If we leave now it is not because of offscreen drawing. |
|
457 console = Console::NewL(aTitle, TSize(KConsFullScreen, KConsFullScreen)); |
|
458 } |
|
459 return console; |
|
460 } |
|
461 |
|
462 void CCSY::Read() |
|
463 { |
|
464 TInt err = KErrNone; |
|
465 if(!iComm.SubSessionHandle()) |
|
466 { |
|
467 TBuf<10> commname(_L("BTCOMM::")); |
|
468 commname.AppendNum(0); |
|
469 |
|
470 err = iComm.Open(iCommServer,commname,ECommExclusive); |
|
471 } |
|
472 if(!err) |
|
473 { |
|
474 iCommConsole->Printf(_L("Read request issued\n")); |
|
475 iComm.ReadOneOrMore(iStatus, iBuffer); |
|
476 SetActive(); |
|
477 } |
|
478 } |
|
479 |
|
480 void CCSY::Close() |
|
481 { |
|
482 iCommConsole->Printf(_L("Close\n")); |
|
483 iComm.Close(); |
|
484 } |
|
485 |
|
486 void CCSY::RunL() |
|
487 { |
|
488 switch (iCommState) |
|
489 { |
|
490 case EReading: |
|
491 { |
|
492 iCommConsole->Printf(_L("Read complete with %d\n"), iStatus.Int()); |
|
493 TBuf<40> readable; |
|
494 readable.Copy(iBuffer); |
|
495 iCommConsole->Printf(_L("Read = %S\n"), &readable); |
|
496 break; |
|
497 } |
|
498 case EWriting: |
|
499 iCommConsole->Printf(_L("Write complete with %d\n"), iStatus.Int()); |
|
500 break; |
|
501 default: |
|
502 __DEBUGGER(); |
|
503 } |
|
504 } |
|
505 |
|
506 TInt CCSY::RunError(TInt /*aError*/) |
|
507 { |
|
508 __DEBUGGER(); |
|
509 return KErrNone; |
|
510 } |
|
511 |
|
512 void CCSY::DoCancel() |
|
513 { |
|
514 iCommConsole->Printf(_L("Cancelled.\n")); |
|
515 iComm.ReadCancel(); |
|
516 iComm.WriteCancel(); |
|
517 } |
|
518 |
|
519 void CCSY::Destroy() |
|
520 { |
|
521 iApp.DeleteConnection(this); |
|
522 } |
|
523 |
|
524 |
|
525 |
|
526 TInt StartCCSYApp() |
|
527 { |
|
528 RTest test(_L("CSY open/close tests")); |
|
529 test.SetLogged(EFalse); |
|
530 test.Title(); |
|
531 |
|
532 CCSYApp* app = CCSYApp::NewL(*test.Console()); |
|
533 app->StartL(); |
|
534 |
|
535 delete app; |
|
536 return KErrNone; |
|
537 } |
|
538 |
|
539 TInt StartTestThread(TAny*) |
|
540 { |
|
541 CTrapCleanup* cleanupStack=CTrapCleanup::New(); // Get CleanupStack |
|
542 CActiveScheduler* activescheduler=new CActiveScheduler; |
|
543 CActiveScheduler::Install(activescheduler); |
|
544 |
|
545 TRAPD(err, StartCCSYApp()); |
|
546 delete cleanupStack; |
|
547 return err; |
|
548 } |
|
549 |
|
550 void StartL() |
|
551 { |
|
552 TBool again = ETrue; |
|
553 RTest test(_L("Test thread")); |
|
554 do |
|
555 { |
|
556 RThread testThread; |
|
557 TThreadFunction testThreadFunction(StartTestThread); |
|
558 TInt err=KErrNone; |
|
559 TRequestStatus stat; |
|
560 _LIT(KTestThread, "TCSY Test Thread"); |
|
561 err=testThread.Create(KTestThread,testThreadFunction,KDefaultStackSize,KMinHeapSize,1024*1000,NULL, EOwnerThread); |
|
562 test.Printf(_L("Test thread create %d\n"), err); |
|
563 |
|
564 (void)(err != KErrNone); // keep the compiler happy by taking err as an r-value in urel |
|
565 |
|
566 testThread.Logon(stat); |
|
567 |
|
568 //User::SetJustInTime(EFalse); |
|
569 testThread.Resume(); |
|
570 User::WaitForRequest(stat); |
|
571 User::SetJustInTime(ETrue); |
|
572 |
|
573 // the test thread gets Killed on exit, but might panic due to BTCOMM ;-) |
|
574 if (testThread.ExitType()==EExitKill) |
|
575 { |
|
576 again = EFalse; |
|
577 } |
|
578 else |
|
579 { |
|
580 // we don't mind about the panics at present, but we do want to get an Access Violation |
|
581 TInt reason = testThread.ExitReason(); |
|
582 TExitCategoryName category = testThread.ExitCategory(); |
|
583 |
|
584 test.Printf(_L("Test thread Panic %d: StopCategory %S, StopReason 0x%04x\n"), ++gPanics, &category, reason); |
|
585 // test.Getch(); |
|
586 User::LeaveIfError(reason); // if EPOC error, lets give up; otherwise restart |
|
587 } |
|
588 |
|
589 testThread.Close(); |
|
590 } while (again); |
|
591 test.Close(); |
|
592 } |
|
593 |
|
594 |
|
595 TInt E32Main() |
|
596 { |
|
597 // TInt ret; |
|
598 |
|
599 // For some reason, you have to do the following to |
|
600 // ensure that the file server behaves properly. |
|
601 RFs fs; |
|
602 (void)fs.Connect(); |
|
603 if(fs.Handle()) |
|
604 { |
|
605 fs.Close(); |
|
606 } |
|
607 |
|
608 LoadLDD_PDD(); |
|
609 |
|
610 __UHEAP_MARK; |
|
611 CTrapCleanup* cleanupStack=CTrapCleanup::New(); // Get CleanupStack |
|
612 |
|
613 CActiveScheduler* activescheduler=new CActiveScheduler; |
|
614 CActiveScheduler::Install(activescheduler); |
|
615 |
|
616 TRAPD(err, StartL()); |
|
617 |
|
618 delete activescheduler; |
|
619 delete cleanupStack; |
|
620 __UHEAP_MARKEND; |
|
621 |
|
622 return err; |
|
623 } |