00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "rconnection.h"
00034
00035 _LIT(KContinueNote,"Press any key to continue\n");
00036 _LIT(KOpenErr,"Open() unsuccessful\n");
00037 _LIT(KStartErr,"RConnection::Start() unsuccessful\n");
00038 _LIT(KInterfaceUp,"Connection has started and interface is up\n");
00039 _LIT(KInterfaceDown,"Connection has closed and interface is down\n");
00040 _LIT(KConnectionUpNote,"Connection is up....\n");
00041 _LIT(KConnectionStartNote,"Start() was called on the connection\n");
00042 _LIT(KConnectionClosingNote,"The connection is about to be closed\n");
00043 _LIT(KWaitNote,"Waiting for the connection to close.....\n");
00044 _LIT(KUpLinkData,"Data volume on uplink=%d bytes\n");
00045 _LIT(KDownLinkData,"Data volume on downlink=%d bytes\n");
00046 _LIT(KDataTransferNote,"Calling DataSendAndReceive()\n");
00047 _LIT(KSocketErrNote,"Socket preparation failed\n");
00048 _LIT(KSocketSetNote,"Socket successfully established\n");
00049
00050 _LIT(KRow1,"**********************************************************\n");
00051 _LIT(KRow2," Section Demonstrating : \n");
00052 _LIT(KRow3,"**********************************************************\n");
00053 _LIT(KTab,"\t");
00054 _LIT(KNewLine,"\n");
00055
00056 #define KEchoPort 0xAB //Port value for client socket.
00057 #define KMaxBufferLength 2048 //Maximum length of buffer used to send and receive data.
00058 #define KDelay 1000000 //Time interval in microseconds to wait before carrying out the next task.
00059
00063 CRConnection::CRConnection(CConsoleBase* aConsole)
00064 :iConsole(aConsole)
00065 {
00066
00067
00068
00069
00070
00071
00072
00073 _LIT(KDestAddr,"127.0.0.1");
00074 const TInt KSockPort= 7;
00075
00076 iDestAddr.Input(KDestAddr);
00077 iDestAddr.SetPort(KSockPort);
00078
00079 StartESOCK();
00080 }
00081
00082
00086 CRConnection::~CRConnection()
00087 {
00088
00089 iMonitor.Close();
00090 iSocketServ.Close();
00091 }
00092
00098 void CRConnection::StartESOCK()
00099 {
00100
00101
00102 TInt err=iSocketServ.Connect();
00103 if(err!=KErrNone)
00104 {
00105 _LIT(KConnectErr,"Connect failed\n");
00106 User::Panic(KConnectErr,err);
00107 }
00108
00109
00110
00111 err=iMonitor.Open(iSocketServ);
00112 if(err!=KErrNone)
00113 {
00114 iConsole->Printf(KOpenErr);
00115 return;
00116 }
00117 }
00118
00126 TInt CRConnection::ConnectWithoutDbOverrideL()
00127 {
00128
00129
00130
00131 TInt err=iConnection.Open(iSocketServ);
00132 if(err!=KErrNone)
00133 {
00134 iConsole->Printf(KOpenErr);
00135 return err;
00136 }
00137
00138
00139 err=iConnection.Start();
00140 if(err!=KErrNone)
00141 {
00142 iConsole->Printf(KStartErr);
00143 return err;
00144 }
00145 else
00146 {
00147 iConsole->Printf(KConnectionStartNote);
00148 return err;
00149 }
00150 }
00151
00155 TInt CRConnection::ConnectWithDbOverrideL()
00156 {
00157
00158
00159 TInt err=iConnection.Open(iSocketServ);
00160 if(err!=KErrNone)
00161 {
00162 iConsole->Printf(KOpenErr);
00163 return err;
00164 }
00165
00166
00167 const TInt KIapId=9;
00168 TCommDbConnPref prefs;
00169 prefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
00170 prefs.SetIapId(KIapId);
00171
00172
00173 err=iConnection.Start(prefs);
00174
00175 if(err==KErrNone)
00176 {
00177 iConsole->Printf(KConnectionStartNote);
00178 return err;
00179 }
00180 else
00181 {
00182 iConsole->Printf(KStartErr);
00183 return err;
00184 }
00185 }
00191 void CRConnection::GetProgressNotification()
00192 {
00193 TRequestStatus status;
00194 TNifProgressBuf buffer;
00195 while((TUint)(buffer().iStage)!=KConnectionUp)
00196 {
00197 iConnection.ProgressNotification(buffer,status);
00198 User::WaitForRequest(status);
00199 if(status==KErrNone&&(buffer().iError)==KErrNone)
00200 {
00201 DisplayProgressinfo(buffer());
00202 }
00203 User::After(KDelay);
00204 }
00205 iConsole->Printf(KConnectionUpNote);
00206
00207
00208 }
00209
00214 void CRConnection::DisplayProgressinfo(const TNifProgress& aProgress)
00215 {
00216 switch(aProgress.iStage)
00217 {
00218 case KStartingSelection:
00219 {
00220 _LIT(KStartSelectionNote,"Starting Selection...........\n");
00221 iConsole->Printf(KStartSelectionNote);
00222 break;
00223 }
00224
00225 case KFinishedSelection:
00226 {
00227 _LIT(KFinishedSelectionNote,"Finished Selection...........\n");
00228 iConsole->Printf(KFinishedSelectionNote);
00229 break;
00230 }
00231
00232 case KConnectionOpen:
00233 {
00234 _LIT(KWlanAgtConnected,"Wlan agent connected....\n");
00235 iConsole->Printf(KWlanAgtConnected);
00236 break;
00237 }
00238
00239
00240 case KConfigDaemonLoading:
00241 {
00242 _LIT(KConfigDaemonLoadingNote,"Daemon loading....\n");
00243 iConsole->Printf(KConfigDaemonLoadingNote);
00244 break;
00245 }
00246
00247 case KConfigDaemonLoaded:
00248 {
00249 _LIT(KConfigDaemonLoadedNote,"Daemon loaded....\n");
00250 iConsole->Printf(KConfigDaemonLoadedNote);
00251 break;
00252 }
00253
00254 case KConfigDaemonStartingRegistration:
00255 {
00256 _LIT(KConfigDaemonStartingRegistrationNote,"Daemon starting registration....\n");
00257 iConsole->Printf(KConfigDaemonStartingRegistrationNote);
00258 break;
00259 }
00260
00261 case KConfigDaemonFinishedRegistration:
00262 {
00263 _LIT(KConfigDaemonFinishedRegistrationNote,"Daemon registration finished....\n");
00264 iConsole->Printf(KConfigDaemonFinishedRegistrationNote);
00265 break;
00266 }
00267
00268 default:
00269 break;
00270 }
00271 }
00272
00278 TInt CRConnection::PrepareSocket()
00279 {
00280 TInt err = iSocket.Open(iSocketServ, KAfInet, KSockDatagram, KProtocolInetUdp, iConnection);
00281 if (err != KErrNone)
00282 {
00283 return err;
00284 }
00285
00286
00287
00288
00289
00290
00291
00292 err = iSocket.SetOpt(KSoReuseAddr, KSolInetIp, 1);
00293 if (err != KErrNone)
00294 {
00295 return err;
00296 }
00297
00298
00299 err = iSocket.SetLocalPort(KEchoPort);
00300 if (err != KErrNone)
00301 {
00302 return err;
00303 }
00304 return err;
00305 }
00311 TInt CRConnection::SendUdpData(TUint aPayloadSize)
00312 {
00313
00314 TBuf8<KMaxBufferLength> buffer;
00315
00316 buffer.SetMax();
00317 buffer.FillZ();
00318 buffer[0] = (TUint8) 0x08;
00319 buffer[1] = (TUint8) 0x00;
00320 buffer[2] = (TUint8) 0xF7;
00321 buffer[3] = (TUint8) 0xFF;
00322
00323
00324
00325
00326
00327
00328 buffer.SetLength(aPayloadSize);
00329
00330 TRequestStatus status;
00331
00332
00333 iSocket.SendTo(buffer, iDestAddr, 0, status);
00334 User::WaitForRequest(status);
00335
00336 return status.Int();
00337 }
00338
00344 TInt CRConnection::RecvUdpData(TUint aPayloadSize)
00345 {
00346
00347 TInt timeoutInSecs = 30;
00348 RTimer timer;
00349
00350
00351
00352
00353
00354
00355
00356
00357 TInt ret;
00358 if ((ret = timer.CreateLocal()) != KErrNone)
00359 {
00360 timer.Close();
00361 return ret;
00362 }
00363 TRequestStatus timerStatus;
00364 timer.After(timerStatus, timeoutInSecs * 1000000);
00365
00366 TBuf8<KMaxBufferLength> buffer;
00367 buffer.Zero();
00368 buffer.SetLength(aPayloadSize);
00369
00370 TRequestStatus status;
00371 iSocket.RecvFrom(buffer, iDestAddr, 0, status);
00372 User::WaitForRequest(status, timerStatus);
00373
00374
00375 if(timerStatus != KRequestPending)
00376 {
00377 iSocket.CancelAll();
00378 User::WaitForRequest(status);
00379 timer.Close();
00380 return KErrTimedOut;
00381 }
00382
00383 timer.Cancel();
00384 User::WaitForRequest(timerStatus);
00385 timer.Close();
00386
00387
00388
00389 if (status != KErrNone)
00390 return status.Int();
00391 else
00392 {
00393 if(buffer[0] == 0x08)
00394 {
00395 if(buffer[1] == 0x00)
00396 {
00397 if(buffer[2] == 0xF7)
00398 {
00399 if(buffer[3] == 0xFF)
00400 {
00401 ;
00402 }
00403 else
00404 {
00405 _LIT(KFourthDataSet,"Fourth set of data not received\n");
00406 iConsole->Printf(KFourthDataSet);
00407 }
00408 }
00409 else
00410 {
00411 _LIT(KThirdDataSet,"Third set of data not received\n");
00412 iConsole->Printf(KThirdDataSet);
00413 }
00414
00415 }
00416 else
00417 {
00418 _LIT(KSecondDataSet,"Second set of data not received\n");
00419 iConsole->Printf(KSecondDataSet);
00420 }
00421 }
00422 else
00423 {
00424 _LIT(KFirstDataSet,"First set of data not received\n");
00425 iConsole->Printf(KFirstDataSet);
00426 }
00427
00428 }
00429 return KErrNone;
00430 }
00431
00435 void CRConnection::DataSendAndReceive(TUint aPayloadSize)
00436 {
00437
00438 TInt err = SendUdpData(aPayloadSize);
00439 if (err != KErrNone)
00440 {
00441 _LIT(KDataSentErrNote,"DataSend Failed\n");
00442 iConsole->Printf(KDataSentErrNote);
00443 return ;
00444 }
00445 else
00446 {
00447 _LIT(KDataSentNote,"DataSend Successful\n");
00448 iConsole->Printf(KDataSentNote);
00449 }
00450
00451
00452 err = RecvUdpData(aPayloadSize);
00453 if(err!=KErrNone)
00454 {
00455 _LIT(KDataReceiveErrNote,"DataReceive Failed\n");
00456 iConsole->Printf(KDataReceiveErrNote);
00457 return ;
00458 }
00459 else
00460 {
00461 _LIT(KDataReceiveNote,"DataReceive Successful\n");
00462 iConsole->Printf(KDataReceiveNote);
00463 }
00464
00465 }
00466
00470 void CRConnection::DataTransferredRequest()
00471 {
00472 TPckg<TUint> uplinkVolumeDes(0);
00473 TPckg<TUint> downlinkVolumeDes(0);
00474
00475 TRequestStatus datastatus;
00476
00477 iConnection.DataTransferredRequest(uplinkVolumeDes, downlinkVolumeDes, datastatus);
00478 User::WaitForRequest(datastatus);
00479
00480 if(datastatus==KErrNone)
00481 {
00482 iConsole->Printf(KUpLinkData,uplinkVolumeDes());
00483 iConsole->Printf(KDownLinkData,downlinkVolumeDes());
00484 }
00485
00486 }
00491 void CRConnection::DataTransferNotificationRequest()
00492 {
00493 const TUint KHundredBytes = 100;
00494 const TUint KThousandBytes = 1000;
00495 TPckg< TUint > uplinkPkg(0);
00496 TPckg< TUint > downlinkPkg(0);
00497 TRequestStatus dataSentStatus,dataReceivedStatus;
00498
00499
00500 iConnection.DataSentNotificationRequest(KHundredBytes, uplinkPkg, dataSentStatus );
00501
00502
00503 iConnection.DataReceivedNotificationRequest(KHundredBytes, downlinkPkg, dataReceivedStatus );
00504 iConsole->Printf(KDataTransferNote);
00505
00506
00507 DataSendAndReceive(KThousandBytes);
00508
00509
00510
00511
00512
00513
00514
00515 User::WaitForRequest(dataSentStatus);
00516 User::WaitForRequest(dataReceivedStatus);
00517 if ((dataSentStatus.Int())== KErrNone)
00518 {
00519 _LIT(KSentSuccessNote,"DataSentNotificationRequest is successful and\n ");
00520 iConsole->Printf(KSentSuccessNote);
00521 }
00522 else
00523 {
00524 _LIT(KSentFailureNote,"DataSentNotificationRequest has failed and \n");
00525 iConsole->Printf(KSentFailureNote);
00526 }
00527 iConsole->Printf(KUpLinkData,uplinkPkg());
00528
00529 if (dataReceivedStatus.Int()==KErrNone)
00530 {
00531 _LIT(KReceivedSuccessNote,"DataReceivedNotificationRequest is successful and \n");
00532 iConsole->Printf(KReceivedSuccessNote);
00533 }
00534 else
00535 {
00536 _LIT(KReceivedFailureNote,"DataReceivedNotificationRequest has failed and \n");
00537 iConsole->Printf(KReceivedFailureNote);
00538 }
00539 iConsole->Printf(KDownLinkData,downlinkPkg());
00540
00541 }
00542
00543 void CRConnection::DisplaySectionToDemo(const TDesC& aText)
00544 {
00545 TBuf<120> newtext;
00546 newtext.Append(KTab);
00547 newtext.Append(aText);
00548 newtext.Append(KTab);
00549 newtext.Append(KNewLine);
00550 iConsole->Printf(KRow1);
00551 iConsole->Printf(KRow2);
00552 iConsole->Printf(newtext);
00553 iConsole->Printf(KRow3);
00554 iConsole->Printf(KNewLine);
00555 }
00556
00567 void CRConnection::DemoApiWithoutDbOverrideL()
00568 {
00569 _LIT(KDemoApiWithoutDbOverride,"RConnection API without CommDb override\n");
00570 iConsole->ClearScreen();
00571 DisplaySectionToDemo(KDemoApiWithoutDbOverride);
00572
00573 TRequestStatus status;
00574 TPckgBuf<TInterfaceNotification> info;
00575
00576 iMonitor.AllInterfaceNotification(info,status);
00577
00578
00579 TInt err=ConnectWithoutDbOverrideL();
00580 if(err!=KErrNone)
00581 return;
00582
00583
00584 User::WaitForRequest(status);
00585 if(info().iState==EInterfaceUp)
00586 {
00587 iConsole->Printf(KInterfaceUp);
00588 }
00589
00590
00591 GetProgressNotification();
00592 User::After(KDelay);
00593
00594 err=PrepareSocket();
00595 if(err!=KErrNone)
00596 {
00597 iConsole->Printf(KSocketErrNote);
00598 return;
00599 }
00600 else
00601 {
00602 iConsole->Printf(KSocketSetNote);
00603 }
00604
00605 iConsole->Printf(KDataTransferNote);
00606
00607 const TUint KPayLoadSize=512;
00608
00609 DataSendAndReceive(KPayLoadSize);
00610
00611 DataTransferredRequest();
00612
00613 User::After(KDelay);
00614
00615
00616 iMonitor.AllInterfaceNotification(info,status);
00617
00618
00619 iSocket.Close();
00620 iConsole->Printf(KConnectionClosingNote);
00621 iConnection.Close();
00622 iConsole->Printf(KWaitNote);
00623
00624
00625 User::WaitForRequest(status);
00626 if(info().iState==EInterfaceDown)
00627 {
00628 iConsole->Printf(KInterfaceDown);
00629 }
00630
00631 iConsole->Printf(KContinueNote);
00632 iConsole->Getch();
00633
00634 }
00635
00646 void CRConnection::DemoApiWithDbOverrideL()
00647 {
00648
00649 _LIT(KDemoApiWithDbOverride,"RConnection API with CommDb override\n");
00650 iConsole->ClearScreen();
00651 DisplaySectionToDemo(KDemoApiWithDbOverride);
00652
00653 TRequestStatus status;
00654 TPckgBuf<TInterfaceNotification> info;
00655
00656
00657
00658 iMonitor.AllInterfaceNotification(info,status);
00659
00660
00661 TInt err=ConnectWithDbOverrideL();
00662 if(err!=KErrNone)
00663 return;
00664
00665
00666 User::WaitForRequest(status);
00667 if(info().iState==EInterfaceUp)
00668 {
00669 iConsole->Printf(KInterfaceUp);
00670 }
00671
00672
00673 GetProgressNotification();
00674
00675
00676 User::After(KDelay);
00677
00678 err=PrepareSocket();
00679 if(err!=KErrNone)
00680 {
00681 iConsole->Printf(KSocketErrNote);
00682 return;
00683 }
00684 else
00685 {
00686 iConsole->Printf(KSocketSetNote);
00687 }
00688
00689 DataTransferNotificationRequest();
00690 User::After(KDelay);
00691
00692
00693 iMonitor.AllInterfaceNotification(info,status);
00694
00695
00696 iSocket.Close();
00697 iConsole->Printf(KConnectionClosingNote);
00698
00699
00700 iConnection.Close();
00701 iConsole->Printf(KWaitNote);
00702
00703
00704 User::WaitForRequest(status);
00705 if(info().iState==EInterfaceDown)
00706 {
00707 iConsole->Printf(KInterfaceDown);
00708 }
00709
00710 iConsole->Printf(KContinueNote);
00711 iConsole->Getch();
00712
00713 }
00714
00724 void CRConnection::AttachToExistingInterfaceL()
00725 {
00726
00727 _LIT(KAttachToConnection,"Attaching to a connection\n");
00728 iConsole->ClearScreen();
00729 DisplaySectionToDemo(KAttachToConnection);
00730
00731
00732
00733
00734
00735
00736
00737 RConnection conn;
00738
00739
00740 TInt err = conn.Open(iSocketServ);
00741 if(err==KErrNone)
00742 {
00743 CleanupClosePushL(conn);
00744 }
00745 else
00746 {
00747 iConsole->Printf(KOpenErr);
00748 return;
00749 }
00750
00751
00752 err = iConnection.Open(iSocketServ);
00753 if(err!=KErrNone)
00754 {
00755 iConsole->Printf(KOpenErr);
00756 return;
00757 }
00758
00759
00760 err=conn.Start();
00761 if(err!=KErrNone)
00762 {
00763 iConsole->Printf(KStartErr);
00764 return;
00765 }
00766
00767
00768
00769 TUint connectionCount;
00770 err = conn.EnumerateConnections(connectionCount);
00771 if((err != KErrNone) || (connectionCount < 1))
00772 {
00773 return;
00774 }
00775
00776
00777
00778
00779 TConnectionInfoBuf connectionInfo;
00780 err = conn.GetConnectionInfo(connectionCount, connectionInfo);
00781
00782 if(err != KErrNone)
00783 {
00784 return;
00785 }
00786
00787
00788
00789
00790 err = iConnection.Attach(connectionInfo,RConnection::EAttachTypeNormal);
00791 if(err==KErrNone)
00792 {
00793 _LIT(KAttachNote,"Attached to interface\n");
00794 iConsole->Printf(KAttachNote);
00795 }
00796 else
00797 return;
00798
00799
00800 TNifProgress progress;
00801 err = iConnection.Progress(progress);
00802 if(err==KErrNone&&progress.iError==KErrNone)
00803 {
00804 if((TUint)(progress.iStage)==KConnectionUp)
00805 iConsole->Printf(KConnectionUpNote);
00806 }
00807
00808 User::After(KDelay);
00809
00810 iConsole->Printf(KConnectionClosingNote);
00811
00812
00813 iConnection.Close();
00814 CleanupStack::PopAndDestroy(&conn);
00815
00816 _LIT(KConnectionClosedNote,"Connection Closed\n");
00817 iConsole->Printf(KConnectionClosedNote);
00818
00819 }
00820