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