00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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();
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
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
00101 iConsole->Printf(_L("* c - Remove current active player *\n"));
00102
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
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
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);
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
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
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
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 case 'y':
00253 case 'Y':
00254
00255
00256
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
00268
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
00278
00279
00280
00281 case 'c':
00282 case 'C':
00283 iMode = EPan;
00284
00285 rerr = iPanConnection->StopConnection();
00286 break;
00287
00288 case 's':
00289 case 'S':
00290 iMode = EPan;
00291
00292 iPanConnection->StopIAP();
00293 break;
00294
00295
00296
00297
00298
00299
00300
00301
00302
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
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
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);
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
00507 RHostResolver hr;
00508 if (KErrNone != hr.Open(iSocketServ, KBTAddrFamily, KBTLinkManager))
00509 {
00510
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);
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();
00561 TRAPD(error,RunAppL());
00562 __ASSERT_ALWAYS(!error,User::Panic(_L("PAN Profile Example Application"),error));
00563 delete cleanup;
00564 __UHEAP_MARKEND;
00565
00566 return 0;
00567 }
00568