diff -r 89d6a7a84779 -r 25a17d01db0c Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/console_8cpp-source.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/console_8cpp-source.html Fri Jan 22 18:26:19 2010 +0000 @@ -0,0 +1,522 @@ + +
+00001 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). +00002 // All rights reserved. +00003 // This component and the accompanying materials are made available +00004 // under the terms of "Eclipse Public License v1.0" +00005 // which accompanies this distribution, and is available +00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html". +00007 // +00008 // Initial Contributors: +00009 // Nokia Corporation - initial contribution. +00010 // +00011 // Contributors: +00012 // +00013 // Description: +00014 // +00015 +00016 +00017 +00018 #include <e32base.h> +00019 #include <e32cons.h> +00020 #include <bttypes.h> +00021 #include <btextnotifiers.h> +00022 #include <btsdp.h> +00023 +00024 #include "console.h" +00025 #include "panconnection.h" +00026 #include "dealer.h" +00027 #include "player.h" +00028 +00029 +00033 void Panic(TInt aPanicCode) +00034 { +00035 User::Panic(_L("PanConnConsole Panic: "), aPanicCode); +00036 } +00037 +00038 +00042 CActiveConsole::CActiveConsole(CConsoleBase* aConsole) +00043 : CActive(EPriorityNormal), +00044 iConsole(aConsole) +00045 { +00046 } +00047 +00051 CActiveConsole* CActiveConsole::NewLC(CConsoleBase* aConsole) +00052 { +00053 CActiveConsole* self = new (ELeave) CActiveConsole(aConsole); +00054 CleanupStack::PushL(self); +00055 self->ConstructL(aConsole); +00056 return self; +00057 } +00058 +00059 +00063 void CActiveConsole::ConstructL (CConsoleBase* aConsole) +00064 { +00065 CActiveScheduler::Add(this); +00066 iPanConnection = CPanConnections::NewL(*aConsole); +00067 } +00068 +00069 +00070 +00074 CActiveConsole::~CActiveConsole() +00075 { +00076 Cancel();// Cancel Active Object +00077 delete iPanConnection; +00078 } +00079 +00083 void CActiveConsole::RequestCharacterL() +00084 { +00085 +00086 iConsole->ClearScreen(); +00087 if (iPanConnection->iIapStarted && iPlayerRole == EPlayer) +00088 { +00089 iMode = EScabbyQueen; +00090 } +00091 +00092 if (iMode == EPan) +00093 { +00094 // Mode is PAN Profile, at this stage devices withh need to connect using Bluetooth +00095 iPanConnection->PrintConns(); +00096 MenuLine(); +00097 iConsole->Printf(_L("*****************PAN Profile menu*****************\n")); +00098 MenuLine(); +00099 iConsole->Printf(_L("* n - Invite a new player (Dealer Only) *\n")); +00100 // iConsole->Printf(_L("* N - Open new connection using PAN notifier *\n")); +00101 iConsole->Printf(_L("* c - Remove current active player *\n")); +00102 // iConsole->Printf(_L("* L - Start listening (for player or dealer) *\n")); +00103 iConsole->Printf(_L("* Y - Be a Player *\n")); +00104 iConsole->Printf(_L("* Z - Be a Dealer *\n")); +00105 iConsole->Printf(_L("* b - Start Game (Dealer Only) *\n")); +00106 iConsole->Printf(_L("* s - Stop Game *\n")); +00107 // iConsole->Printf(_L("* x - Switch to TCP/IP *\n")); +00108 iConsole->Printf(_L("* 1..7 - Select current active player *\n")); +00109 iConsole->Printf(_L("* Esc Escape *\n")); +00110 MenuLine(); +00111 } +00112 else if (iMode == EScabbyQueen) +00113 { +00114 if (iPlayerRole == EDealer) +00115 { +00116 // Dealer will already be created +00117 MenuLine(); +00118 iConsole->Printf(_L("******************* Dealer Menu ******************\n")); +00119 MenuLine(); +00120 iConsole->Printf(_L("* a - Deal *\n")); +00121 iConsole->Printf(_L("* b - Show Deck *\n")); +00122 iConsole->Printf(_L("* x - Exit Game *\n")); +00123 MenuLine(); +00124 } +00125 else +00126 { +00127 if (!iPlayer) +00128 { +00129 iPlayer = CScabbyQueenPlayer::NewL(*iConsole, iPanConnection->GetSocket()); +00130 } +00131 iConsole->Printf(_L("%dPlayer Number: %d\n\n"),iPlayer->iPlayerNum(),iPlayer->iPlayerNum()); +00132 +00133 MenuLine(); +00134 iConsole->Printf(_L("******************* Player Menu ******************\n")); +00135 MenuLine(); +00136 iConsole->Printf(_L("* a - Show Cards *\n")); +00137 if (iPlayer->iCurrentPlayer) +00138 { +00139 iConsole->Printf(_L("* b - Take card from right *\n")); +00140 } +00141 iConsole->Printf(_L("* x - Exit Game *\n")); +00142 MenuLine(); +00143 +00144 if (iPlayer->iCurrentPlayer) +00145 { +00146 iConsole->Printf(_L("\nYou need to select a card from the other player\n")); +00147 } +00148 } +00149 } +00150 +00151 iConsole->Read(iStatus);// Read the key press, RunL will be called when iStatus completes +00152 SetActive(); +00153 +00154 } +00155 +00156 +00157 +00158 +00162 void CActiveConsole::DoCancel() +00163 { +00164 iConsole->ReadCancel(); +00165 } +00166 +00167 +00168 +00172 void CActiveConsole::MenuLine() +00173 { +00174 iConsole->Printf(_L("**************************************************\n")); +00175 } +00176 +00181 void CActiveConsole::RunL() +00182 { +00183 TRAPD(err,ProcessKeyPressL(TChar(iConsole->KeyCode()))); +00184 if(err != KErrNone) +00185 { +00186 iConsole->Printf(_L("Failed. Error: %d\r\n"),err); +00187 RequestCharacterL(); +00188 } +00189 } +00190 +00191 +00197 void CActiveConsole::ProcessKeyPressL(TChar aChar) +00198 { +00199 TInt rerr = KErrNone; +00200 TBool redraw = ETrue; +00201 +00202 +00203 +00204 if (iMode == EPan) +00205 { +00206 +00207 TBTDevAddr devAddr; +00208 // TBTDevAddr devAddr(MAKE_TINT64(0x0002, 0x5b019a2c)); +00209 if (aChar == EKeyEscape) +00210 { +00211 CActiveScheduler::Stop(); +00212 return; +00213 } +00214 else +00215 { +00216 switch(aChar) +00217 { +00218 case 'n': +00219 { +00220 iMode = EPan; +00221 //Ask user which device address we should connect to... +00222 RNotifier notify; +00223 User::LeaveIfError(notify.Connect()); +00224 TBTDeviceSelectionParamsPckg pckg; +00225 TBTDeviceResponseParamsPckg resPckg; +00226 TRequestStatus stat; +00227 notify.StartNotifierAndGetResponse(stat, KDeviceSelectionNotifierUid, pckg, resPckg); +00228 User::WaitForRequest(stat); +00229 notify.CancelNotifier(KDeviceSelectionNotifierUid); +00230 notify.Close(); +00231 User::LeaveIfError(stat.Int()); +00232 { +00233 devAddr = resPckg().BDAddr(); +00234 } +00235 rerr = iPanConnection->StartConnection(&devAddr, EFalse, EFalse); +00236 } +00237 break; +00238 +00239 // case 'N': +00240 // { +00241 // // Need to open connection with pan profile here +00242 // iMode = EPan; +00243 // rerr = iPanConnection->StartConnection(&devAddr, ETrue, EFalse); +00244 // } +00245 // break; +00246 +00247 // case 'l': +00248 // case 'L': +00249 // rerr = StartListening(); +00250 // break; +00251 +00252 case 'y': +00253 case 'Y': +00254 // User will be a player, get them to enter IP address, +00255 // Local and peer role will be changed here, and the remote address +00256 // of the dealer will be set. +00257 iMode = EPan; +00258 iPlayerRole = EPlayer; +00259 iConsole->Printf(_L("\nEnter your IP address (11.11.11.XXX): ")); +00260 TRAP(rerr, PlayerProcessL()); +00261 iPanConnection->SetRemoteIpAddr(KDealerIpAddr); +00262 rerr = StartListening(); +00263 break; +00264 +00265 case 'z': +00266 case 'Z': +00267 // User will be a dealer, set the local and peer role, +00268 // and the local IP address. +00269 iMode = EPan; +00270 iPlayerRole = EDealer; +00271 iPanConnection->SetLocalIpAddrL(KDealerIpAddr); +00272 iPanConnection->SetFixedRoleL(ELocalRole, KPanGnUUID); +00273 iPanConnection->SetFixedRoleL(EPeerRole, KPanUUUID); +00274 rerr = KErrNone; +00275 break; +00276 +00277 // case 'h': +00278 // rerr = iPanConnection->UdpBind(); +00279 // break; +00280 +00281 case 'c': +00282 case 'C': +00283 iMode = EPan; +00284 // close current connection +00285 rerr = iPanConnection->StopConnection(); +00286 break; +00287 +00288 case 's': +00289 case 'S': +00290 iMode = EPan; +00291 //stop iap +00292 iPanConnection->StopIAP(); +00293 break; +00294 +00295 +00296 // case 'x': +00297 // case 'X': +00298 // Switch to the TCP, will allow for players to join game +00299 // and for the dealer to host. +00300 // iMode = ETcpIp; +00301 // rerr = StartTcpMenu(); +00302 // break; +00303 +00304 case 'b': +00305 case 'B': +00306 if (iPlayerRole == EDealer) +00307 { +00308 iMode = EScabbyQueen; +00309 iPanConnection->CancelRecvFrom(); +00310 if (!iDealer) +00311 { +00312 TRAP (rerr,iDealer = CScabbyQueenDealer::NewL(*iConsole, iPanConnection->RemoteNames(), iPanConnection->GetSocket())); +00313 } +00314 iDealer->SendPlayerNumbers(); +00315 } +00316 else +00317 { +00318 rerr = KErrNone; +00319 } +00320 break; +00321 +00322 case '0': +00323 case '1': +00324 case '2': +00325 case '3': +00326 case '4': +00327 case '5': +00328 case '6': +00329 case '7': +00330 if (iPanConnection->iIapStarted) +00331 rerr = iPanConnection->SetActivePanConn(aChar.GetNumericValue()); +00332 else +00333 rerr = KErrNone; +00334 break; +00335 +00336 default: +00337 break; +00338 +00339 } +00340 } +00341 } +00342 else if (iMode == EScabbyQueen) +00343 { +00344 if (iPlayerRole == EDealer) +00345 { +00346 switch(aChar) +00347 { +00348 case 'a': +00349 // Dealing the deck will initiate the game. +00350 +00351 TRAP(rerr, iDealer->DealDeckL()); +00352 if (rerr != KErrNone) +00353 { +00354 iConsole->Printf(_L("\nFailed to deal deck")); +00355 } +00356 else +00357 { +00358 iDealer->StartTheGame(); +00359 } +00360 break; +00361 +00362 case 'b': +00363 TRAP(rerr, iDealer->ShowDeckL()); +00364 break; +00365 +00366 case 'x': +00367 iMode = EPan; +00368 iDealer->BaseCancelAll(); +00369 break; +00370 default: +00371 break; +00372 } +00373 } +00374 else if (iPlayerRole == EPlayer) +00375 { +00376 switch(aChar) +00377 { +00378 case 'a': +00379 TRAP(rerr, iPlayer->ShowHandL()); +00380 break; +00381 +00382 case 'b': +00383 { +00384 iPlayer->GetRightHandPlayerCard(); +00385 iCardNum = GetCardNumber(); +00386 rerr = iPlayer->SendCardNum(iCardNum); +00387 } +00388 break; +00389 +00390 +00391 +00392 case 'x': +00393 iMode = EPan; +00394 iPlayer->BaseCancelAll(); +00395 break; +00396 +00397 default: +00398 break; +00399 } +00400 } +00401 +00402 } +00403 +00404 if(redraw && rerr == KErrNone) +00405 { +00406 RequestCharacterL(); +00407 } +00408 +00409 } +00410 +00411 +00415 CConsoleBase* CActiveConsole::Console() +00416 { +00417 return iConsole; +00418 } +00419 +00420 void CActiveConsole::PlayerProcessL() +00421 { +00422 iPanConnection->SetLocalIpAddrL(GetIpAddress()); +00423 iPanConnection->SetFixedRoleL(ELocalRole, KPanUUUID); +00424 iPanConnection->SetFixedRoleL(EPeerRole, KPanGnUUID); +00425 } +00426 +00431 TUint32 CActiveConsole::GetIpAddress() +00432 { +00433 TBuf<KMaxBufferSize> buf; +00434 TKeyCode key; +00435 +00436 while((key=iConsole->Getch())!=EKeyEnter) +00437 { +00438 buf.Append(key); +00439 iConsole->Printf(_L("%c"),key); +00440 } +00441 TInetAddr addr; +00442 addr.Input(buf); +00443 return addr.Address(); +00444 } +00445 +00449 TInt CActiveConsole::GetCardNumber() +00450 { +00451 TBuf<KMaxBufferSize> buf; +00452 TKeyCode key; +00453 while((key=iConsole->Getch())!=EKeyEnter) +00454 { +00455 TChar test = key; +00456 if (test.IsDigit()) +00457 { +00458 buf.Append(key); +00459 iConsole->Printf(_L("%c"),key); +00460 } +00461 } +00462 // Convert Desc into a TInt +00463 TLex16 lex; +00464 lex.Assign(buf); +00465 TInt val; +00466 lex.Val(val); +00467 val = 2*val; +00468 return val; +00469 } +00470 +00471 +00475 TInt CActiveConsole::StartListening() +00476 { +00477 TInt rerr = KErrNone; +00478 if(iPanConnection->iListening) +00479 { +00480 __ASSERT_ALWAYS(iPanConnection->iIapStarted || iPanConnection->iIapLoading, Panic(EListeningWithoutIAP)); +00481 iConsole->Printf(_L("Listening Connection already exists.")); +00482 rerr = KErrAlreadyExists; +00483 } +00484 else +00485 { +00486 if(iPanConnection->iIapStarted || iPanConnection->iIapLoading) +00487 { +00488 iConsole->Printf(_L("Non listening connection already exists.")); +00489 rerr = KErrAlreadyExists; +00490 } +00491 } +00492 iPanConnection->iListening = ETrue; +00493 rerr = iPanConnection->StartConnection(NULL, EFalse, EFalse);// Call to start the listen +00494 return rerr; +00495 } +00496 +00500 TInt CActiveConsole::CheckBtConnectionL() +00501 { +00502 _LIT(BtSearch, "Checking Bluetooth Connectivity\n"); +00503 iConsole->Printf(BtSearch); +00504 RSocketServ iSocketServ; +00505 User::LeaveIfError(iSocketServ.Connect()); +00506 // Create and initialise an RHostResolver +00507 RHostResolver hr; +00508 if (KErrNone != hr.Open(iSocketServ, KBTAddrFamily, KBTLinkManager)) +00509 { +00510 // if not success, this means bluetooth is not on +00511 _LIT(KBtOff, "Bluetooth is not on\n"); +00512 iConsole->Printf(KBtOff); +00513 +00514 _LIT(KPressAnyKey, "Press any key to exit the application"); +00515 iConsole->Printf(KPressAnyKey); +00516 iConsole->Getch(); +00517 iSocketServ.Close(); +00518 hr.Close(); +00519 return EFalse; +00520 } +00521 iSocketServ.Close(); +00522 hr.Close(); +00523 return ETrue; +00524 } +00525 +00526 +00527 +00528 +00529 +00534 void RunAppL(void) +00535 { +00536 CActiveScheduler *myScheduler = new (ELeave) CActiveScheduler(); +00537 CleanupStack::PushL(myScheduler); +00538 CActiveScheduler::Install(myScheduler); +00539 CConsoleBase* console = +00540 Console::NewL(_L("PAN Profile Example Application"),TSize(KConsFullScreen, KConsFullScreen)); +00541 CleanupStack::PushL(console); +00542 CActiveConsole* my_console = CActiveConsole::NewLC(console);// New active console +00543 if(my_console->CheckBtConnectionL()!= EFalse) +00544 { +00545 my_console->RequestCharacterL(); +00546 CActiveScheduler::Start(); +00547 } +00548 CleanupStack::PopAndDestroy(3); +00549 } +00550 +00551 +00557 TInt E32Main() +00558 { +00559 __UHEAP_MARK; +00560 CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack +00561 TRAPD(error,RunAppL()); // more initialization, then do example +00562 __ASSERT_ALWAYS(!error,User::Panic(_L("PAN Profile Example Application"),error)); +00563 delete cleanup; // destroy clean-up stack +00564 __UHEAP_MARKEND; +00565 +00566 return 0; // and return +00567 } +00568 +