nettools/conntest/Engine/SocketsEngine.cpp
changeset 0 857a3e953887
child 2 4cefe9af9cf4
equal deleted inserted replaced
-1:000000000000 0:857a3e953887
       
     1 /*
       
     2  * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description: CSocketsEngine implements an engine for connection
       
    15  * component checking: Interface opening/closing, connecting,
       
    16  * sending/receiving data, etc.
       
    17  * CSocketsEngine is an active object
       
    18  *
       
    19  */
       
    20 
       
    21 // INCLUDE FILES
       
    22 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    23 #include <es_sock_partner.h>
       
    24 #include <es_enum_internal.h>
       
    25 #endif
       
    26 #include <eikgted.h>
       
    27 #include <in_sock.h>
       
    28 #include <e32svr.h>
       
    29 #include <es_enum.h>
       
    30 #include <commdb.h>
       
    31 #include <in_iface.h>
       
    32 #include <apgcli.h>
       
    33 #include <apaid.h>
       
    34 
       
    35 #include "SocketsEngine.h"
       
    36 #include "TimeOutTimer.h"
       
    37 #include "SocketsRead.h"
       
    38 #include "SocketsWrite.h"
       
    39 #include "ProgressNotifier.h"
       
    40 #include "ConnTest.pan"
       
    41 #include "uinotify.h"
       
    42 #include "HttpHandler.h"
       
    43 #include "SettingData.h"
       
    44 #include "CustomPrefsData.h"
       
    45 #include "Utils.h"
       
    46 #include "SendTimer.h"
       
    47 #include "alractiveobject.h"
       
    48 
       
    49 #include <hal.h>
       
    50 #include <hal_data.h>
       
    51 
       
    52 #include <cmmanagerext.h>
       
    53 #include <cmdestinationext.h>
       
    54 #include <cmapplicationsettingsui.h>
       
    55 
       
    56 // CONSTANTS
       
    57 const TInt KTimeOut = 120000000; // 2 minutes time-out
       
    58 
       
    59 _LIT(KDefaultServerName, "127.0.0.1");
       
    60 const TInt KDefaultPortNumber = 25;
       
    61 
       
    62 // ================= MEMBER FUNCTIONS =======================
       
    63 
       
    64 // ---------------------------------------------------------
       
    65 // CSocketsEngine::NewL(MUINotify& aConsole)
       
    66 // EPOC two phased constructor
       
    67 // ---------------------------------------------------------
       
    68 //
       
    69 CSocketsEngine* CSocketsEngine::NewL( MUINotify& aConsole )
       
    70     {
       
    71     CSocketsEngine* self = CSocketsEngine::NewLC( aConsole );
       
    72     CleanupStack::Pop( self );
       
    73     return self;
       
    74     }
       
    75 
       
    76 // ---------------------------------------------------------
       
    77 // CSocketsEngine::NewLC(MUINotify& aConsole)
       
    78 // EPOC two phased constructor
       
    79 // ---------------------------------------------------------
       
    80 //
       
    81 CSocketsEngine* CSocketsEngine::NewLC( MUINotify& aConsole )
       
    82     {
       
    83     CSocketsEngine* self = new (ELeave) CSocketsEngine( aConsole );
       
    84     CleanupStack::PushL( self );
       
    85     self->ConstructL();
       
    86     return self;
       
    87     }
       
    88 
       
    89 // ---------------------------------------------------------
       
    90 // CSocketsEngine::CSocketsEngine(MUINotify& aConsole)
       
    91 // EPOC constructor
       
    92 // ---------------------------------------------------------
       
    93 //
       
    94 CSocketsEngine::CSocketsEngine( MUINotify& aConsole ) :
       
    95     CActive( EPriorityStandard ), iConsole( aConsole ), iPort(
       
    96             KDefaultPortNumber ), iServerName( KDefaultServerName )
       
    97     {
       
    98     iStartTime = 0;
       
    99     }
       
   100 
       
   101 // ---------------------------------------------------------
       
   102 // CSocketsEngine::~CSocketsEngine()
       
   103 // Destructor
       
   104 // ---------------------------------------------------------
       
   105 //
       
   106 CSocketsEngine::~CSocketsEngine()
       
   107     {
       
   108     Cancel();
       
   109     if (iProgressNotifier)
       
   110         {
       
   111         iProgressNotifier->Cancel();
       
   112         }
       
   113 
       
   114     delete iSocketsRead;
       
   115     iSocketsRead = NULL;
       
   116 
       
   117     delete iSocketsWrite;
       
   118     iSocketsWrite = NULL;
       
   119 
       
   120     delete iProgressNotifier;
       
   121     iProgressNotifier = NULL;
       
   122 
       
   123     delete iTimer;
       
   124     iTimer = NULL;
       
   125 
       
   126     delete iSendTimer;
       
   127 
       
   128     delete iHttpClient;
       
   129     iHttpClient = NULL;
       
   130 
       
   131     if (iData)
       
   132         {
       
   133         delete iData;
       
   134         iData = NULL;
       
   135         }
       
   136     //delete pointer to mobility API
       
   137     if (iMobility)
       
   138         {
       
   139         delete iMobility;
       
   140         iMobility = NULL;
       
   141         }
       
   142 
       
   143     delete iExtPrefs;
       
   144     delete iPrefsList;
       
   145 
       
   146     // Note! Closing order matters. Panic occurs if the
       
   147     // closing is made in different order. 
       
   148 
       
   149     iConnection.Close();
       
   150     iSocket.Close();
       
   151     iSocketServ.Close();
       
   152     }
       
   153 
       
   154 // ---------------------------------------------------------
       
   155 // CSocketsEngine::ConstructL()
       
   156 // EPOC two phased constructor
       
   157 // ---------------------------------------------------------
       
   158 //
       
   159 void CSocketsEngine::ConstructL()
       
   160     {
       
   161     ChangeStatus( EInterfaceDown );
       
   162 
       
   163     iData = HBufC8::NewL( KMaxSendBuffer);
       
   164     // Start a timer
       
   165     iTimer = CTimeOutTimer::NewL( EPriorityHigh, *this );
       
   166 
       
   167     CActiveScheduler::Add( this );
       
   168 
       
   169     // Open channel to Socket Server
       
   170     User::LeaveIfError( iSocketServ.Connect() );
       
   171 
       
   172     // Open connection
       
   173     User::LeaveIfError( iConnection.Open( iSocketServ ) );
       
   174 
       
   175     // Create socket read and write active objects
       
   176     iSocketsRead = CSocketsRead::NewL( iConsole, iSocket, *this );
       
   177     iSocketsWrite = CSocketsWrite::NewL( iConsole, iSocket, *this );
       
   178 
       
   179     // Create interface notification active object
       
   180     iProgressNotifier = CProgressNotifier::NewL( iConnection, *this );
       
   181     iProgressNotifier->StartNotify();
       
   182 
       
   183     //
       
   184     // HTTP framework initialization
       
   185     //
       
   186     iHttpClient = CHttpClient::NewL( iConsole );
       
   187     iHttpClient->SetHttpConnectionInfoL( ETrue, iConnection, iSocketServ );
       
   188 
       
   189     // Timer for sending multiple packets
       
   190     if (!iSendTimer)
       
   191         {
       
   192         iSendTimer = CSendTimer::NewL( EPriorityHigh, this );
       
   193         }
       
   194 
       
   195     iUseTTime = EFalse;
       
   196     TInt freq;
       
   197     TInt err = HAL::Get( HAL::EFastCounterFrequency, freq );
       
   198     if (err != KErrNone || freq == 0)
       
   199         {
       
   200         iUseTTime = ETrue;
       
   201         }
       
   202 
       
   203     //set mobility API flag to false
       
   204     iIsRegisteredToMobAPI = EFalse;
       
   205     SetSocketEngineConnType( ETypeUnknown );
       
   206 
       
   207     iExtPrefs = new (ELeave) TExtendedConnPref;
       
   208     iPrefsList = TConnPrefList::NewL();
       
   209     }
       
   210 
       
   211 // ---------------------------------------------------------
       
   212 // CSocketsEngine::GetSocketEngineState()
       
   213 // Return the current state of the socket engine.
       
   214 // ---------------------------------------------------------
       
   215 //
       
   216 CSocketsEngine::TSocketsEngineState CSocketsEngine::GetSocketEngineState()
       
   217     {
       
   218     return iEngineStatus;
       
   219     }
       
   220 
       
   221 // ---------------------------------------------------------
       
   222 // CSocketsEngine::GetSocketEngineRoamingState()
       
   223 // ---------------------------------------------------------
       
   224 // 
       
   225 CSocketsEngine::TRoamingUIState CSocketsEngine::GetSocketEngineRoamingState()
       
   226     {
       
   227     return iRoamingState;
       
   228     }
       
   229 
       
   230 // ---------------------------------------------------------
       
   231 // CSocketsEngine::GetMobilityAPI()
       
   232 // ---------------------------------------------------------
       
   233 // 
       
   234 CALRActiveObject* CSocketsEngine::GetMobilityAPI()
       
   235     {
       
   236     return iMobility;
       
   237     }
       
   238 
       
   239 // ---------------------------------------------------------
       
   240 // CSocketsEngine::GetSocketEngineConnType()
       
   241 // Return the current connection type of the socket engine.
       
   242 // ---------------------------------------------------------
       
   243 //
       
   244 CSocketsEngine::TSocketsEngineStartType CSocketsEngine::GetSocketEngineConnType()
       
   245     {
       
   246     return iSockEngineStartType;
       
   247     }
       
   248 // ---------------------------------------------------------
       
   249 // CSocketsEngine::SetSocketEngineConnType()
       
   250 // Set the current conn type of the socket engine.
       
   251 // ---------------------------------------------------------
       
   252 //
       
   253 void CSocketsEngine::SetSocketEngineConnType( const CSocketsEngine::TSocketsEngineStartType aConnStartType )
       
   254     {
       
   255     iSockEngineStartType = aConnStartType;
       
   256     }
       
   257 
       
   258 // ---------------------------------------------------------
       
   259 // CSocketsEngine::StartConnWithSnapL()
       
   260 // Start the connection with snap
       
   261 // ---------------------------------------------------------
       
   262 //
       
   263 void CSocketsEngine::StartConnWithSnapL( TBool aConnect )
       
   264     {
       
   265     if (iEngineStatus == EConnected || iEngineStatus == EInterfaceUp)
       
   266         {
       
   267         return;
       
   268         }
       
   269 
       
   270     //start the connection  dialog
       
   271     CCmApplicationSettingsUi* settings = CCmApplicationSettingsUi::NewL();
       
   272     CleanupStack::PushL( settings );
       
   273     TCmSettingSelection selection;
       
   274     settings->RunApplicationSettingsL( selection );
       
   275     CleanupStack::PopAndDestroy( settings );
       
   276 
       
   277     switch (selection.iResult)
       
   278         {
       
   279         case CMManager::EConnectionMethod:
       
   280             {
       
   281             // start connection with Iap Id
       
   282             iPrefs.SetIapId( selection.iId );
       
   283             iPrefs.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
       
   284 
       
   285             StartTickCount();
       
   286             iConnection.Start( iPrefs, iStatus );
       
   287             break;
       
   288             }
       
   289         case CMManager::EDestination:
       
   290             {
       
   291             TConnSnapPref pref;
       
   292             
       
   293             pref.SetSnap( selection.iId );
       
   294 
       
   295             StartTickCount();
       
   296             //start connection with snap Id
       
   297             iConnection.Start( pref, iStatus );
       
   298             //Set connection type as Snap	
       
   299             SetSocketEngineConnType( ESnap );
       
   300             
       
   301             break;
       
   302             }
       
   303         case CMManager::EAlwaysAsk:
       
   304             {
       
   305             // For testing start without preferences
       
   306             StartTickCount();
       
   307             iConnection.Start( iStatus );
       
   308             ChangeStatus( EStartingInterface );
       
   309             iConnectAfterStartup = ETrue;
       
   310             SetActive();
       
   311             return;
       
   312             }
       
   313         default:
       
   314             {
       
   315             RDebug::Print( _L("ConnTest: Invalid selection"));
       
   316             return;
       
   317             }
       
   318         }
       
   319 
       
   320     ChangeStatus( EStartingInterface );
       
   321     //specify if a connection is needed after interface startup(
       
   322     iConnectAfterStartup = aConnect;
       
   323     SetActive();
       
   324     
       
   325     }
       
   326 
       
   327 // ---------------------------------------------------------
       
   328 // CSocketsEngine::StartInterfaceL(TBool aConnect)
       
   329 // Start interface
       
   330 // ---------------------------------------------------------
       
   331 //
       
   332 void CSocketsEngine::StartInterfaceL( TSocketsEngineStartType aStartType,
       
   333         TBool aConnect )
       
   334     {
       
   335     RDebug::Print( _L("ConnTest: StartInterfaceL( Start type = %d )"), aStartType );
       
   336     if (iEngineStatus == EConnected || iEngineStatus == EInterfaceUp)
       
   337         {
       
   338         RDebug::Print( _L("ConnTest: Selection cancelled!"));
       
   339         return;
       
   340         }
       
   341 
       
   342     SetSocketEngineConnType( aStartType );
       
   343 
       
   344     if (aStartType == EAskIap)
       
   345         {
       
   346         // Get IAP id
       
   347         TUint32 iapId;
       
   348         TInt popupOk = Utils::AskIapIdL( iapId );
       
   349         
       
   350         if (!popupOk)
       
   351             {
       
   352             RDebug::Print( _L("ConnTest: Selection cancelled!"));
       
   353             return;
       
   354             }
       
   355         RDebug::Print( _L("ConnTest: Selected IAP id=%d"), iapId );
       
   356 
       
   357         // Create overrides
       
   358         iPrefs.SetIapId( iapId );
       
   359         iPrefs.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
       
   360         }
       
   361     else if (aStartType == ENetworkId)
       
   362         {
       
   363         // Get network id
       
   364         TUint32 nwkId;
       
   365         TInt popupOk = Utils::AskNetworkIdL( nwkId );
       
   366         
       
   367         if (!popupOk)
       
   368             {
       
   369             RDebug::Print( _L("ConnTest: Selection cancelled!"));
       
   370             return;
       
   371             }
       
   372         RDebug::Print( _L("ConnTest: Selected Network id=%d"), nwkId );
       
   373 
       
   374         // Create overrides
       
   375         iPrefs.SetNetId( nwkId );
       
   376         iPrefs.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
       
   377         }
       
   378 
       
   379     // Start the timer after the dialogs
       
   380     StartTickCount();
       
   381 
       
   382     // ...And now there are few different kinds of iConnection.Start() functions to call...
       
   383     if (aStartType == EExtPrefs)
       
   384         {
       
   385         iPrefsList->AppendL( iExtPrefs );
       
   386         iConnection.Start( *iPrefsList, iStatus );
       
   387         iPrefsList->Remove( 0 );
       
   388         }
       
   389     else if (aStartType == EExtPrefsInternet)
       
   390         {
       
   391         TExtendedConnPref* tmpExtPrefs = new (ELeave) TExtendedConnPref;
       
   392         tmpExtPrefs->SetSnapPurpose( CMManager::ESnapPurposeInternet );
       
   393         iPrefsList->AppendL( tmpExtPrefs );
       
   394         iConnection.Start( *iPrefsList, iStatus );
       
   395         iPrefsList->Remove( 0 );
       
   396         delete tmpExtPrefs;
       
   397         }
       
   398     else if (aStartType == ENoPrefs)
       
   399         {
       
   400         iConnection.Start( iStatus );
       
   401         }
       
   402     else // (aStartType == EAskIap || aStartType == ENetworkId) - Other values aren't used.
       
   403         {
       
   404         iConnection.Start( iPrefs, iStatus );
       
   405         }
       
   406 
       
   407     ChangeStatus( EStartingInterface );
       
   408     iConnectAfterStartup = aConnect;
       
   409     SetActive();
       
   410     }
       
   411 
       
   412 // ---------------------------------------------------------
       
   413 // CSocketsEngine::RegisterToMobilityAPI(TBool aConnect)
       
   414 // Register to mobility API
       
   415 // ---------------------------------------------------------
       
   416 //
       
   417 void CSocketsEngine::RegisterToMobilityAPIL()
       
   418     {
       
   419     if (iEngineStatus == EConnected || iEngineStatus == EInterfaceUp)
       
   420         {
       
   421         if (!iIsRegisteredToMobAPI)
       
   422             {
       
   423             iConsole.PrintNotify( _L("RegisterToMobilityAPI\n"));
       
   424             //create the listener active object
       
   425             iMobility = CALRActiveObject::NewL( iConsole,
       
   426                     (MMobilityProtocolResp*)this );
       
   427             iMobility->OpenL( iConnection );
       
   428             iIsRegisteredToMobAPI = ETrue;
       
   429             }
       
   430         }
       
   431     else
       
   432         {
       
   433         iConsole.PrintNotify( _L("Could not register\n"));
       
   434         }
       
   435     }
       
   436 
       
   437 // ---------------------------------------------------------
       
   438 // CSocketsEngine::UnRegisterToMobilityAPI(TBool aConnect)
       
   439 // UnRegister from mobility API
       
   440 // ---------------------------------------------------------
       
   441 //
       
   442 void CSocketsEngine::UnRegisterFromMobilityAPI()
       
   443     {
       
   444     if (iIsRegisteredToMobAPI && iMobility) //&& 
       
   445     //(iEngineStatus == EConnected || iEngineStatus == EInterfaceUp || iEngineStatus == EConnecting))
       
   446         {
       
   447         delete iMobility;
       
   448         iMobility = NULL;
       
   449         iConsole.PrintNotify( _L("Unregister MobilityAPI\n"));
       
   450         }
       
   451     iIsRegisteredToMobAPI = EFalse;
       
   452     }
       
   453 
       
   454 // ---------------------------------------------------------
       
   455 // CSocketsEngine::Error()
       
   456 // ---------------------------------------------------------
       
   457 // 
       
   458 void CSocketsEngine::Error( TInt aError )
       
   459     {
       
   460     TBuf8<64> text;
       
   461     text.AppendFormat( _L8("MobilityError: %d\n"), aError );
       
   462     iConsole.PrintNotify( text );
       
   463     UnRegisterFromMobilityAPI();
       
   464     iRoaming = ERoamingOff;
       
   465     }
       
   466 
       
   467 // ---------------------------------------------------------
       
   468 // CSocketsEngine::PreferredCarrierAvailable()
       
   469 // ---------------------------------------------------------
       
   470 // 
       
   471 void CSocketsEngine::PreferredCarrierAvailable( TAccessPointInfo aOldAP,
       
   472         TAccessPointInfo aNewAP,
       
   473         TBool aIsUpgrade,
       
   474         TBool aIsSeamless )
       
   475     {
       
   476     // silence warnings
       
   477     aOldAP = aOldAP;
       
   478     aIsUpgrade = aIsUpgrade;
       
   479     aIsSeamless = aIsSeamless;
       
   480 
       
   481     TBuf8<64> text;
       
   482     
       
   483     if (iRoaming == ERoamingAutomatic)
       
   484         {
       
   485         text.AppendFormat( _L8("Migrating to %i\n"), aNewAP.AccessPoint() );
       
   486         iMobility->MigrateToPreferredCarrier();
       
   487         iRoamingState = EIdle;
       
   488         }
       
   489     else if (iRoaming == ERoamingManual)
       
   490         {
       
   491         text.AppendFormat( _L8("Preferred IAP %i Available\n"), aNewAP.AccessPoint() );
       
   492         iRoamingState = EPendingPreferredCarrier;
       
   493         iConsole.PopupNotify( _L("Migrate or Ignore available"));
       
   494         }
       
   495     else
       
   496         {
       
   497         text.AppendFormat( _L8("Unexpected PreferrredCarrier %i\n"), aNewAP.AccessPoint() );
       
   498         iRoamingState = EIdle;
       
   499         iMobility->IgnorePreferredCarrier();
       
   500         }
       
   501     iConsole.PrintNotify( text );
       
   502     }
       
   503 // ---------------------------------------------------------
       
   504 // CSocketsEngine::NewCarrierActive()
       
   505 // ---------------------------------------------------------
       
   506 // 
       
   507 void CSocketsEngine::NewCarrierActive( TAccessPointInfo aNewAP,
       
   508         TBool aIsSeamless )
       
   509     {
       
   510     // silence warning
       
   511     aIsSeamless = aIsSeamless;
       
   512 
       
   513     TBuf8<64> text;
       
   514     
       
   515     if (iRoaming == ERoamingAutomatic)
       
   516         {
       
   517         iRoamingState = EIdle;
       
   518         iMobility->NewCarrierAccepted();
       
   519         }
       
   520     else if (iRoaming == ERoamingManual)
       
   521         {
       
   522         text.AppendFormat( _L8("IAP %i pending accept\n"), aNewAP.AccessPoint() );
       
   523         iRoamingState = EPendingNewCarrierActive;
       
   524         iConsole.PopupNotify( _L("Accept or Reject available"));
       
   525         }
       
   526     else
       
   527         {
       
   528         iRoamingState = EIdle;
       
   529         text.AppendFormat( _L8("Unexpected NewCarrierActive %i\n"), aNewAP.AccessPoint() );
       
   530         iMobility->NewCarrierRejected();
       
   531         }
       
   532     iConsole.PrintNotify( text );
       
   533     }
       
   534 
       
   535 // ---------------------------------------------------------
       
   536 // CSocketsEngine::
       
   537 // ---------------------------------------------------------
       
   538 //
       
   539 void CSocketsEngine::Migrate()
       
   540     {
       
   541     if (iRoaming != ERoamingManual)
       
   542         {
       
   543         User::Panic( KPanicSocketsEngine, EConnTestBadRoamingStatus );
       
   544         }
       
   545     iMobility->MigrateToPreferredCarrier();
       
   546     iConsole.PrintNotify( _L("Migration requested"));
       
   547     iRoamingState = EIdle;
       
   548     }
       
   549 
       
   550 // ---------------------------------------------------------
       
   551 // CSocketsEngine::
       
   552 // ---------------------------------------------------------
       
   553 //
       
   554 void CSocketsEngine::Ignore()
       
   555     {
       
   556     if (iRoaming != ERoamingManual)
       
   557         {
       
   558         User::Panic( KPanicSocketsEngine, EConnTestBadRoamingStatus );
       
   559         }
       
   560     iMobility->IgnorePreferredCarrier();
       
   561     iConsole.PrintNotify( _L("Ignored new carrier"));
       
   562     iRoamingState = EIdle;
       
   563     }
       
   564 
       
   565 // ---------------------------------------------------------
       
   566 // CSocketsEngine::
       
   567 // ---------------------------------------------------------
       
   568 //
       
   569 void CSocketsEngine::AcceptCarrier()
       
   570     {
       
   571     if (iRoaming != ERoamingManual)
       
   572         {
       
   573         User::Panic( KPanicSocketsEngine, EConnTestBadRoamingStatus );
       
   574         }
       
   575     iMobility->NewCarrierAccepted();
       
   576     iConsole.PrintNotify( _L("Accepted carrier"));
       
   577     iRoamingState = EIdle;
       
   578     }
       
   579 
       
   580 // ---------------------------------------------------------
       
   581 // CSocketsEngine::
       
   582 // ---------------------------------------------------------
       
   583 //
       
   584 void CSocketsEngine::RejectCarrier()
       
   585     {
       
   586     if (iRoaming != ERoamingManual)
       
   587         {
       
   588         User::Panic( KPanicSocketsEngine, EConnTestBadRoamingStatus );
       
   589         }
       
   590     iMobility->NewCarrierRejected();
       
   591     iConsole.PrintNotify( _L("Rejected carrier"));
       
   592     iRoamingState = EIdle;
       
   593     }
       
   594 
       
   595 // ---------------------------------------------------------
       
   596 // CSocketsEngine::StartCloseInterfaceL()
       
   597 // Start interface for testing RConnection::Close
       
   598 // ---------------------------------------------------------
       
   599 //
       
   600 void CSocketsEngine::StartCloseInterfaceL()
       
   601     {
       
   602     if (iEngineStatus == EConnected || iEngineStatus == EInterfaceUp)
       
   603         {
       
   604         // Already started, simply return
       
   605         return;
       
   606         }
       
   607 
       
   608     // Get IAP id
       
   609     TUint32 iapId;
       
   610     TInt popupOk = Utils::AskIapIdL( iapId );
       
   611     
       
   612     if (!popupOk)
       
   613         {
       
   614         // Selection cancelled
       
   615         return;
       
   616         }
       
   617     RDebug::Print( _L("ConnTest: Selected IAP id=%d"), iapId );
       
   618 
       
   619     iCloseConnection = new (ELeave) RConnection;
       
   620 
       
   621     User::LeaveIfError( iCloseConnection->Open( iSocketServ ) );
       
   622 
       
   623     // Create overrides
       
   624     iPrefs.SetIapId( iapId );
       
   625     iPrefs.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
       
   626 
       
   627     ChangeStatus( EStartingInterface );
       
   628 
       
   629     iCloseConnection->Start( iPrefs, iStatus );
       
   630     SetActive();
       
   631 
       
   632     iTempProgressNotifier = CProgressNotifier::NewL( *iCloseConnection, *this );
       
   633     iTempProgressNotifier->StartNotify();
       
   634     
       
   635     }
       
   636 
       
   637 // ---------------------------------------------------------
       
   638 // CSocketsEngine::StopInterfaceL()
       
   639 // Shutdown interface
       
   640 // ---------------------------------------------------------
       
   641 //
       
   642 void CSocketsEngine::StopInterfaceL()
       
   643     {
       
   644     // Note! Socket is not closed here, because in connected
       
   645     // state only Disconnect menu item is available. Therefore
       
   646     // interface cannot be stopped when connection is open.
       
   647     //
       
   648     // iProgressNotifier->Cancel() is called in ProgressNotifyReceived
       
   649     // method after KLinkLayerClosed has been received.
       
   650     //
       
   651 
       
   652     //unregister from mobility API if registered
       
   653     UnRegisterFromMobilityAPI();
       
   654     SetSocketEngineConnType( ETypeUnknown );
       
   655     
       
   656     if (iEngineStatus == EConnected || iEngineStatus == EInterfaceUp)
       
   657         {
       
   658         iConnection.Stop();
       
   659         ChangeStatus( EInterfaceDown );
       
   660         iHttpClient->SetHttpConnectionInfoL( ETrue, iConnection, iSocketServ );
       
   661         
       
   662         if (iCloseConnection)
       
   663             {
       
   664             // So if we are here, interface was started for testing RConnection::Close,
       
   665             // and the corresponding RConnection instance needs to be deleted.
       
   666             iCloseConnection->Close();
       
   667             delete iCloseConnection;
       
   668             iCloseConnection = NULL;
       
   669             }
       
   670         }
       
   671     }
       
   672 
       
   673 // ---------------------------------------------------------
       
   674 // CSocketsEngine::CloseInterface()
       
   675 // Close interface
       
   676 // ---------------------------------------------------------
       
   677 //
       
   678 void CSocketsEngine::CloseInterface()
       
   679     {
       
   680     if (iCloseConnection)
       
   681         {
       
   682         iCloseConnection->Close();
       
   683         delete iCloseConnection;
       
   684         iCloseConnection = NULL;
       
   685         }
       
   686     }
       
   687 
       
   688 // ---------------------------------------------------------
       
   689 // CSocketsEngine::ConnectL()
       
   690 // Check whether the address is valid IP address. If it's not
       
   691 // calls DNS to solve the name.
       
   692 // ---------------------------------------------------------
       
   693 //
       
   694 void CSocketsEngine::ConnectL()
       
   695     {
       
   696     // Just in case (does nothing if interface has already been started)
       
   697     StartInterfaceL( EAskIap, ETrue );
       
   698     
       
   699     // Initiate connection process
       
   700     if ((iEngineStatus == EInterfaceUp) || (iEngineStatus == ETimedOut))
       
   701         {
       
   702         TInetAddr addr;
       
   703         if (addr.Input( iServerName ) == KErrNone)
       
   704             {
       
   705             // server name is already a valid ip address
       
   706             ConnectL( addr );
       
   707             }
       
   708         else // need to look up name using dns
       
   709             {
       
   710             // Initiate DNS
       
   711             User::LeaveIfError( iResolver.Open( iSocketServ, KAfInet,
       
   712                     KProtocolInetUdp, iConnection ) );
       
   713             // DNS request for name resolution
       
   714             StartTickCount();
       
   715             iResolver.GetByName( iServerName, iNameEntry, iStatus );
       
   716 
       
   717             ChangeStatus( ELookingUp );
       
   718             // Request time out
       
   719             iTimer->After( KTimeOut );
       
   720             SetActive();
       
   721             }
       
   722         }
       
   723     }
       
   724 
       
   725 // ---------------------------------------------------------
       
   726 // CSocketsEngine::ConnectL()
       
   727 // Connect to socket
       
   728 // ---------------------------------------------------------
       
   729 //                          
       
   730 void CSocketsEngine::ConnectL( const TInetAddr& aAddr ) // <a name="ConnectL32">
       
   731     {
       
   732     
       
   733     // Initiate attempt to connect to a socket by IP address	
       
   734     if ((iEngineStatus == EInterfaceUp) || (iEngineStatus == ETimedOut))
       
   735         {
       
   736         // Open a TCP socket
       
   737         iSocket.Close(); // Just in case
       
   738         User::LeaveIfError( iSocket.Open( iSocketServ, KAfInet, iSocketType,
       
   739                 iProtocol, iConnection ) );
       
   740 
       
   741         // Set up address information
       
   742         iAddress = aAddr;
       
   743         iAddress.SetPort( iPort );
       
   744 
       
   745         // Initiate socket connection
       
   746         StartTickCount();
       
   747         iSocket.Connect( iAddress, iStatus );
       
   748         ChangeStatus( EConnecting );
       
   749         SetActive();
       
   750         
       
   751         // Start a timeout
       
   752         iTimer->After( KTimeOut );
       
   753         }
       
   754     }
       
   755 
       
   756 // ---------------------------------------------------------
       
   757 // CSocketsEngine::Disconnect()
       
   758 // Shutdown socket
       
   759 // ---------------------------------------------------------
       
   760 //
       
   761 void CSocketsEngine::Disconnect()
       
   762     {
       
   763     if (iEngineStatus == EListening && iProtocol == KProtocolInetTcp)
       
   764         {
       
   765         iListeningSocket.CancelAccept();
       
   766         iListeningSocket.Close();
       
   767         // Remember to close the socket on reader RunL()
       
   768         }
       
   769     if (iEngineStatus == EConnected || iEngineStatus == EListening)
       
   770         {
       
   771         iSocketsRead->Cancel();
       
   772         iSocketsWrite->Cancel();
       
   773 
       
   774         // have to do this.
       
   775         iTimer->Cancel();
       
   776 
       
   777         // Use Close() instead of Shutdown(). This way we
       
   778         // can handle the situation, where server initiates
       
   779         // the closing process, and sends RST signal instead
       
   780         // of FIN. If server sends RST and Shutdown() is used,
       
   781         // engine stays in EDisconnecting state forever.
       
   782         iSocket.Close();
       
   783         
       
   784         ChangeStatus( EInterfaceUp );
       
   785         }
       
   786     }
       
   787 
       
   788 // ---------------------------------------------------------
       
   789 // CSocketsEngine::ListenL()
       
   790 // Opens listening socket
       
   791 // ---------------------------------------------------------
       
   792 //
       
   793 void CSocketsEngine::ListenL()
       
   794     {
       
   795     // Initiate attempt to connect to a socket by IP address	
       
   796     if ((iEngineStatus == EInterfaceUp) || (iEngineStatus == ETimedOut))
       
   797         {
       
   798         // Open a TCP socket
       
   799         iSocket.Close(); // Just in case
       
   800         iListeningSocket.Close();
       
   801 
       
   802         // Set up address information
       
   803         iAddress.SetFamily( 0 );
       
   804         iAddress.SetPort( iPort );
       
   805         switch (iProtocol)
       
   806             {
       
   807             case KProtocolInetTcp:
       
   808                 User::LeaveIfError( iSocket.Open( iSocketServ ) );
       
   809                 User::LeaveIfError( iListeningSocket.Open( iSocketServ,
       
   810                         KAfInet, iSocketType, iProtocol, iConnection ) );
       
   811                 // Bind a port
       
   812                 iListeningSocket.Bind( iAddress );
       
   813                 // Listens a socket
       
   814                 iListeningSocket.Listen( 1 ); // Only one connection allowed
       
   815                 // Accepts one connection
       
   816                 iListeningSocket.Accept( iSocket, iStatus );
       
   817                 ChangeStatus( EListening );
       
   818                 SetActive();
       
   819                 break;
       
   820             case KProtocolInetUdp:
       
   821                 User::LeaveIfError( iSocket.Open( iSocketServ, KAfInet,
       
   822                         iSocketType, iProtocol, iConnection ) );
       
   823                 iSocket.Bind( iAddress );
       
   824                 ChangeStatus( EListening );
       
   825                 iStatus = KErrNone;
       
   826                 RunL(); // ugly.
       
   827                 break;
       
   828             default:
       
   829                 User::Leave( KErrNotSupported );
       
   830                 break;
       
   831             }
       
   832         }
       
   833     }
       
   834 
       
   835 // ---------------------------------------------------------
       
   836 // CSocketsEngine::Write()
       
   837 // Write data to socket
       
   838 // ---------------------------------------------------------
       
   839 //
       
   840 void CSocketsEngine::WriteL( const TDesC8& aData )
       
   841     {
       
   842     RDebug::Print( _L("ConnTest: Sending data, length = %d"),aData.Length() );
       
   843 
       
   844     iSocketsRead->SetPerformance( EFalse );
       
   845     
       
   846     // Write data to socket
       
   847     if (iEngineStatus == EConnected)
       
   848         {
       
   849         iSocketsWrite->IssueWriteL( aData, &iAddress, iProtocol );
       
   850         }
       
   851     }
       
   852 
       
   853 // ---------------------------------------------------------
       
   854 // CSocketsEngine::Write()
       
   855 // Write data to socket aCount times
       
   856 // ---------------------------------------------------------
       
   857 //
       
   858 void CSocketsEngine::WriteFloodL( const TDesC8& aData, TInt aCount )
       
   859     {
       
   860     // Write data to socket
       
   861     if (iEngineStatus == EConnected)
       
   862         {
       
   863         iThroughputStartTime.UniversalTime();
       
   864         iSocketsWrite->IssueWriteL( aData, &iAddress, iProtocol, aCount );
       
   865         }
       
   866     }
       
   867 
       
   868 // ---------------------------------------------------------
       
   869 // CSocketsEngine::SendHttpRequestOverSocketL(TBool aHasBody)
       
   870 // Write HTTP request to socket
       
   871 // ---------------------------------------------------------
       
   872 //
       
   873 void CSocketsEngine::SendHttpRequestOverSocketL( TBool aHasBody,
       
   874         TBool aDoPerformance )
       
   875     {
       
   876     iSocketsRead->SetPerformance( aDoPerformance );
       
   877 
       
   878     TBuf8<256> req;
       
   879     
       
   880     if (aHasBody)
       
   881         {
       
   882         // Send HTTP POST
       
   883         req.Copy( _L8("POST "));
       
   884 
       
   885         // Add '/' if it is not included in the given page name
       
   886         if (!((TChar)iSettingData->iHttpPage[0] == '/'))
       
   887             {
       
   888             req.Append( _L8("/"));
       
   889             }
       
   890 
       
   891         req.Append( iSettingData->iHttpPage );
       
   892         req.Append( _L8(" HTTP/1.1\r\n"));
       
   893         req.Append( _L8("Host: "));
       
   894         req.Append( iSettingData->iServerName );
       
   895         req.Append( _L8(":"));
       
   896         req.AppendNum( iSettingData->iPort );
       
   897         req.AppendFormat( _L8("\r\nContent-Length: %d"), iSettingData->iPacketSize
       
   898                 * iSettingData->iPackets );
       
   899         req.Append( _L8("\r\n\r\n"));
       
   900         
       
   901         iSocketsWrite->IssueWriteL( req, &iAddress, iProtocol,
       
   902                 iSettingData->iPacketSize, iSettingData->iPackets );
       
   903         }
       
   904     else
       
   905         {
       
   906         // Send HTTP GET
       
   907         req.Copy( _L8("GET "));
       
   908 
       
   909         // Add '/' if it is not included in the given page name
       
   910         if (!((TChar)iSettingData->iHttpPage[0] == '/'))
       
   911             {
       
   912             req.Append( _L8("/"));
       
   913             }
       
   914 
       
   915         req.Append( iSettingData->iHttpPage );
       
   916         req.Append( _L8(" HTTP/1.1\r\n"));
       
   917         req.Append( _L8("Host: "));
       
   918         req.Append( iSettingData->iServerName );
       
   919         req.Append( _L8(":"));
       
   920         req.AppendNum( iSettingData->iPort );
       
   921         req.Append( _L8("\r\n\r\n"));
       
   922 
       
   923         StartTickCount();
       
   924         iSocketsWrite->IssueWriteL( req, &iAddress, iProtocol );
       
   925         }
       
   926     }
       
   927 
       
   928 // ---------------------------------------------------------
       
   929 // CSocketsEngine::Read()
       
   930 // Read data from socket
       
   931 // ---------------------------------------------------------
       
   932 //
       
   933 void CSocketsEngine::Read()
       
   934     {
       
   935     // Initiate read of data from socket
       
   936     if ((iEngineStatus == EConnected) && (!iSocketsRead->IsActive()))
       
   937         {
       
   938         iSocketsRead->Start( &iAddress, iProtocol );
       
   939         }
       
   940     }
       
   941 
       
   942 // ---------------------------------------------------------
       
   943 // CSocketsEngine::RunL()
       
   944 // 
       
   945 // ---------------------------------------------------------
       
   946 //
       
   947 void CSocketsEngine::RunL()
       
   948     {
       
   949     // Active object request complete handler.
       
   950     // iEngineStatus flags what request was made, so its
       
   951     // completion can be handled appropriately
       
   952     iTimer->Cancel(); // Cancel TimeOut timer before completion
       
   953     TBuf<64> text( _L(""));
       
   954     switch (iEngineStatus)
       
   955         {
       
   956         case EInterfaceDown:
       
   957             // Just in case, if status is changed in ProgressNotifyReceived
       
   958             // method before this method is called
       
   959             break;
       
   960         case EStartingInterface:
       
   961             text.Format( _L("RConnection::Start()"));
       
   962             StopTickCount( text );
       
   963             // Interface startup request
       
   964             if (iStatus == KErrNone)
       
   965                 {
       
   966                 // Interface started successfully
       
   967                 ChangeStatus( EInterfaceUp );
       
   968 
       
   969                 if (iCloseConnection)
       
   970                     {
       
   971                     iHttpClient->SetHttpConnectionInfoL( EFalse,
       
   972                             *iCloseConnection, iSocketServ );
       
   973                     }
       
   974                 else
       
   975                     {
       
   976                     iHttpClient->SetHttpConnectionInfoL( EFalse, iConnection,
       
   977                             iSocketServ );
       
   978                     }
       
   979 
       
   980                 if ( ( iSockEngineStartType == ESnap ||
       
   981                        iSockEngineStartType == EExtPrefs ||
       
   982                        iSockEngineStartType == EExtPrefsInternet ) &&
       
   983                      ( iRoaming == ERoamingAutomatic || iRoaming == ERoamingManual ) )
       
   984                     {
       
   985                     RegisterToMobilityAPIL();
       
   986                     }
       
   987                 
       
   988                 if (iConnectAfterStartup)
       
   989                     {
       
   990                     ConnectL();
       
   991                     }
       
   992                 }
       
   993             else
       
   994                 {
       
   995                 iConsole.ErrorNotify( _L("<CSocketsEngine> Startup failed"), iStatus.Int() );
       
   996                 ChangeStatus( EInterfaceDown );
       
   997                 SetSocketEngineConnType( ETypeUnknown );
       
   998                 }
       
   999             break;
       
  1000             
       
  1001         case EConnecting:
       
  1002             // IP connection request
       
  1003             text.Format( _L("RSocket::Connect()"));
       
  1004             StopTickCount( text );
       
  1005             if (iStatus == KErrNone)
       
  1006             // Connection completed successfully
       
  1007                 {
       
  1008                 ChangeStatus( EConnected );
       
  1009                 Read(); //Start CSocketsRead Active object
       
  1010                 }
       
  1011             else
       
  1012                 {
       
  1013                 iConsole.ErrorNotify( _L("<CSocketsEngine> Conn. failed"), iStatus.Int() );
       
  1014                 ChangeStatus( EInterfaceUp );
       
  1015                 }
       
  1016             break;
       
  1017             
       
  1018         case ELookingUp:
       
  1019             text.Format( _L("RHostResolver::GetByName()"));
       
  1020             StopTickCount( text );
       
  1021             iResolver.Close();
       
  1022             if (iStatus == KErrNone)
       
  1023                 {
       
  1024                 // DNS look up successful
       
  1025                 iNameRecord = iNameEntry();
       
  1026                 // Extract domain name and IP address from name record
       
  1027                 PrintTextToConsole( _L("\r\nDomain name = "));
       
  1028                 PrintTextToConsole( iNameRecord.iName );
       
  1029                 TBuf<64> ipAddr;
       
  1030                 TInetAddr::Cast( iNameRecord.iAddr ).Output( ipAddr );
       
  1031                 iConsole.PrintNotify( _L("\r\nIP address = "));
       
  1032                 PrintTextToConsole( ipAddr );
       
  1033                 PrintTextToConsole( _L("\r\n"));
       
  1034                 // And connect to the IP address
       
  1035                 ChangeStatus( EInterfaceUp );
       
  1036                 ConnectL( TInetAddr::Cast( iNameRecord.iAddr ) );
       
  1037                 }
       
  1038             else
       
  1039                 {
       
  1040                 // DNS lookup failed
       
  1041                 iConsole.ErrorNotify( _L("<CSocketsEngine> DNS lookup failed"), iStatus.Int() );
       
  1042                 ChangeStatus( EInterfaceUp );
       
  1043                 }
       
  1044             break;
       
  1045         case EListening:
       
  1046             // Listening socket accept returned
       
  1047             if (iStatus == KErrNone && !iSocketsRead->IsActive())
       
  1048             // Connection established
       
  1049                 {
       
  1050                 //ChangeStatus(EConnected); Might as well be in listening state.
       
  1051                 // This how we can keep accepting several connections
       
  1052                 // Initiate read of data from socket
       
  1053                 iSocketsRead->StartRAWRead( &iAddress, iProtocol );
       
  1054                 }
       
  1055             else
       
  1056                 {
       
  1057                 iConsole.ErrorNotify( _L("<CSocketsEngine> Accept. failed"), iStatus.Int() );
       
  1058                 iListeningSocket.Close();
       
  1059                 iSocket.Close();
       
  1060                 ChangeStatus( EInterfaceUp );
       
  1061                 }
       
  1062             
       
  1063             break;
       
  1064         case EDisconnecting:
       
  1065             if (iStatus == KErrNone)
       
  1066                 {
       
  1067                 iSocket.Close();
       
  1068                 ChangeStatus( EInterfaceUp );
       
  1069                 }
       
  1070             break;
       
  1071             
       
  1072         default:
       
  1073             // Ignore the state check here, because it might happen that state
       
  1074             // has already been altered in ProgressNotifyReceived method.
       
  1075             break;
       
  1076         }
       
  1077     }
       
  1078 
       
  1079 // ---------------------------------------------------------
       
  1080 // CSocketsEngine::DoCancel()
       
  1081 // Cancel ongoing operations
       
  1082 // ---------------------------------------------------------
       
  1083 //
       
  1084 void CSocketsEngine::DoCancel()
       
  1085     {
       
  1086     iTimer->Cancel();
       
  1087     
       
  1088     // Cancel ongoing operation
       
  1089     switch (iEngineStatus)
       
  1090         {
       
  1091         case EStartingInterface:
       
  1092 
       
  1093             if (iCloseConnection)
       
  1094                 {
       
  1095                 iCloseConnection->Close();
       
  1096                 delete iCloseConnection;
       
  1097                 iCloseConnection = NULL;
       
  1098                 }
       
  1099             else
       
  1100                 {
       
  1101                 iConnection.Stop();
       
  1102                 }
       
  1103             
       
  1104             UnRegisterFromMobilityAPI();
       
  1105             SetSocketEngineConnType( ETypeUnknown );
       
  1106             ChangeStatus( EInterfaceDown );
       
  1107             
       
  1108             break;
       
  1109         case EConnecting:
       
  1110             iSocket.CancelConnect();
       
  1111             iSocket.Close();
       
  1112             ChangeStatus( EInterfaceUp );
       
  1113             break;
       
  1114         case ELookingUp:
       
  1115             // Cancel look up attempt
       
  1116             iResolver.Cancel();
       
  1117             iResolver.Close();
       
  1118             ChangeStatus( EInterfaceUp );
       
  1119             break;
       
  1120         case EDisconnecting:
       
  1121             ChangeStatus( EInterfaceUp );
       
  1122             iSocket.Close();
       
  1123             break;
       
  1124         case EListening:
       
  1125             ChangeStatus( EInterfaceUp );
       
  1126             iListeningSocket.CancelAccept();
       
  1127             iListeningSocket.Close();
       
  1128             iSocket.Close();
       
  1129             break;
       
  1130         default:
       
  1131             User::Panic( KPanicSocketsEngine, EConnTestBadStatus );
       
  1132             break;
       
  1133         }
       
  1134     }
       
  1135 
       
  1136 // ---------------------------------------------------------
       
  1137 // CSocketsEngine::ChangeStatus(TSocketsEngineState aNewStatus)
       
  1138 // Set new status for socket engine and update the status
       
  1139 // line in console window.
       
  1140 // ---------------------------------------------------------
       
  1141 //
       
  1142 void CSocketsEngine::ChangeStatus( TSocketsEngineState aNewStatus )
       
  1143     {
       
  1144     // Update the status (and the status display)
       
  1145     switch (aNewStatus)
       
  1146         {
       
  1147         case EInterfaceDown:
       
  1148             iConsole.SetStatus( _L("Interface stopped"));
       
  1149             break;
       
  1150         case EStartingInterface:
       
  1151             iConsole.SetStatus( _L("Starting interface..."));
       
  1152             break;
       
  1153         case EInterfaceUp:
       
  1154             iConsole.SetStatus( _L("Interface up"));
       
  1155             break;
       
  1156         case EConnecting:
       
  1157             iConsole.SetStatus( _L("Connecting..."));
       
  1158             break;
       
  1159         case EConnected:
       
  1160             iConsole.SetStatus( _L("Connected"));
       
  1161             break;
       
  1162         case EListening:
       
  1163             iConsole.SetStatus( _L("Listening"));
       
  1164             break;
       
  1165         case ETimedOut:
       
  1166             iConsole.SetStatus( _L("Timed out"));
       
  1167             break;
       
  1168         case ELookingUp:
       
  1169             iConsole.SetStatus( _L("Looking up..."));
       
  1170             break;
       
  1171         case ELookUpFailed:
       
  1172             iConsole.SetStatus( _L("Look up failed"));
       
  1173             break;
       
  1174         case EConnectFailed:
       
  1175             iConsole.SetStatus( _L("Failed"));
       
  1176             break;
       
  1177         case EDisconnecting:
       
  1178             iConsole.SetStatus( _L("Disconnecting..."));
       
  1179             break;
       
  1180         default:
       
  1181             User::Panic( KPanicSocketsEngine, EConnTestBadStatus );
       
  1182             break;
       
  1183         }
       
  1184     iEngineStatus = aNewStatus;
       
  1185     }
       
  1186 
       
  1187 // ---------------------------------------------------------
       
  1188 // CSocketsEngine::PrintTextToConsole(const TDesC& aDes)
       
  1189 // Helper function for printing notifications, text
       
  1190 // formatting added.
       
  1191 // ---------------------------------------------------------
       
  1192 //
       
  1193 void CSocketsEngine::PrintTextToConsole( const TDesC& aDes )
       
  1194     {
       
  1195     // Print some text on the console
       
  1196     iConsole.PrintNotify( aDes ); // Try if this works on real hw
       
  1197     }
       
  1198 
       
  1199 // ---------------------------------------------------------
       
  1200 // CSocketsEngine::SetConnectionData(const CSettingData* aData)
       
  1201 // Set connection information.
       
  1202 // ---------------------------------------------------------
       
  1203 //
       
  1204 void CSocketsEngine::SetConnectionData( const CSettingData* aData )
       
  1205     {
       
  1206     iServerName.Copy( aData->iServerName );
       
  1207     iPort = aData->iPort;
       
  1208 
       
  1209     switch (aData->iProtocol)
       
  1210         {
       
  1211         case 0:
       
  1212             iProtocol = KProtocolInetTcp;
       
  1213             iSocketType = KSockStream;
       
  1214             break;
       
  1215         case 1:
       
  1216             iProtocol = KProtocolInetUdp;
       
  1217             iSocketType = KSockDatagram;
       
  1218             break;
       
  1219         default:
       
  1220             iProtocol = KProtocolInetTcp;
       
  1221             iSocketType = KSockStream;
       
  1222             break;
       
  1223         }
       
  1224 
       
  1225     iRoaming = (TRoamingSetting)aData->iRoaming;
       
  1226 
       
  1227     iHttpPage = aData->iHttpPage;
       
  1228     
       
  1229     iSettingData = (CSettingData*)aData;
       
  1230     }
       
  1231 
       
  1232 // ---------------------------------------------------------
       
  1233 // CSocketsEngine::SetCustomPrefs(const CCustomPrefsData* aData)
       
  1234 // Set custom preferences information.
       
  1235 // ---------------------------------------------------------
       
  1236 //
       
  1237 void CSocketsEngine::SetCustomPrefsData( const CCustomPrefsData* aData )
       
  1238     {
       
  1239     iCustomPrefsData = (CCustomPrefsData*)aData;
       
  1240     iExtPrefs->SetSnapPurpose( (CMManager::TSnapPurpose)aData->iSnapPurpose );
       
  1241     iExtPrefs->SetSnapId( aData->iSnapId );
       
  1242     iExtPrefs->SetIapId( aData->iIapId );
       
  1243     iExtPrefs->SetBearerSet( aData->iBearerSet );
       
  1244     iExtPrefs->SetNoteBehaviour( aData->iNoteBehaviour );
       
  1245     iExtPrefs->SetDisconnectDialog( aData->iDisconnectDialog );
       
  1246     iExtPrefs->SetConnSelectionDialog( aData->iConnSelectionDialog );
       
  1247     iExtPrefs->SetForcedRoaming( aData->iForcedRoaming );
       
  1248     }
       
  1249 
       
  1250 // ---------------------------------------------------------
       
  1251 // CSocketsEngine::ConnectionInfo()
       
  1252 // Display information about current connection and sockets
       
  1253 // ---------------------------------------------------------
       
  1254 //
       
  1255 void CSocketsEngine::ConnectionInfoL()
       
  1256     {
       
  1257     TInt result;
       
  1258     TBuf<40> val;
       
  1259     TBuf<40> network;
       
  1260     TBuf<20> query;
       
  1261     TName name;
       
  1262     TUint connCount;
       
  1263     TUint i, j;
       
  1264     TConnectionEnumArg args;
       
  1265     TUint32 networkId;
       
  1266     RApaLsSession appSess;
       
  1267     TApaAppInfo appInfo;
       
  1268     
       
  1269     User::LeaveIfError( appSess.Connect() );
       
  1270     CleanupClosePushL( appSess );
       
  1271 
       
  1272     HBufC* buf = HBufC::NewLC( 2048 );
       
  1273     TPtr infoBuf = buf->Des();
       
  1274     
       
  1275     infoBuf.AppendFormat( _L("===============\n"));
       
  1276     infoBuf.AppendFormat( _L("Connection info:\n"));
       
  1277 
       
  1278     // Connection name.
       
  1279     result = iConnection.Name( name );
       
  1280     User::LeaveIfError( result );
       
  1281     RDebug::Print( _L("ConnTest: Connection name: %S"), &name );
       
  1282 
       
  1283     // Get connection info
       
  1284     result = iConnection.EnumerateConnections( connCount );
       
  1285     User::LeaveIfError( result );
       
  1286     RDebug::Print( _L("ConnTest: Number of connections: %d"), connCount );
       
  1287     infoBuf.AppendFormat( _L("Connections: %d\n"), connCount );
       
  1288 
       
  1289     if (connCount == 0)
       
  1290         {
       
  1291         infoBuf.AppendFormat( _L("---------------\n"));
       
  1292         PrintTextToConsole( infoBuf );
       
  1293         CleanupStack::PopAndDestroy( buf ); // buf
       
  1294         CleanupStack::PopAndDestroy( &appSess );
       
  1295         return;
       
  1296         }
       
  1297 
       
  1298     //
       
  1299     // Get active connection info
       
  1300     //
       
  1301     infoBuf.AppendFormat( _L("Active connection:\n"));
       
  1302 
       
  1303     // IAP name
       
  1304     query.Format( _L("%s\\%s"), IAP, COMMDB_NAME);
       
  1305     result = iConnection.GetDesSetting(query, val);
       
  1306     if(result == KErrNone)
       
  1307         {
       
  1308         RDebug::Print(_L("ConnTest: IAP/Name value: %S"), &val);
       
  1309         infoBuf.AppendFormat(_L("-IAP: %S\n"), &val);
       
  1310         }
       
  1311 
       
  1312     // Network id
       
  1313             query.Format(_L("%s\\%s"), IAP, IAP_NETWORK);
       
  1314             result = iConnection.GetIntSetting(query, networkId);
       
  1315             if(result == KErrNone)
       
  1316                 {
       
  1317                 // Get the corresponding network name for this id,
       
  1318             // it is user friendlier.
       
  1319             RDebug::Print(_L("ConnTest: NetworkId: %d"), networkId);
       
  1320 
       
  1321             // Network name
       
  1322             CCommsDatabase* TheDb;
       
  1323 
       
  1324             TheDb = CCommsDatabase::NewL();
       
  1325             CleanupStack::PushL(TheDb);
       
  1326 
       
  1327             CCommsDbTableView* view = TheDb->OpenViewMatchingUintLC( TPtrC(NETWORK),
       
  1328                     TPtrC(COMMDB_ID),
       
  1329                     networkId );
       
  1330             result = view->GotoFirstRecord();
       
  1331 
       
  1332             if(result == KErrNone)
       
  1333                 {
       
  1334                 view->ReadTextL(TPtrC(COMMDB_NAME), network);
       
  1335                 RDebug::Print(_L("ConnTest: Network name: %S"), &network);
       
  1336                 infoBuf.AppendFormat(_L("-net: %S\n"), &network);
       
  1337                 }
       
  1338             CleanupStack::PopAndDestroy(view);
       
  1339             CleanupStack::PopAndDestroy(TheDb);
       
  1340             }
       
  1341 
       
  1342         // Note! The following doesn't work, because NETWORK table
       
  1343         // is not among those tables where information can be
       
  1344         // fetched this way. (Perhaps in future.)
       
  1345         /*    
       
  1346          // Network name
       
  1347          query.Format(_L("%s\\%s"), NETWORK, COMMDB_NAME);
       
  1348          result = iConnection.GetDesSetting(query, network);
       
  1349          RDebug::Print(_L("IAP/Name value: %S"), &val);
       
  1350          infoBuf.AppendFormat(_L("Network name: %S\n"), &network);
       
  1351          
       
  1352          // Network id
       
  1353          query.Format(_L("%s\\%s"), NETWORK, COMMDB_ID);
       
  1354          result = iConnection.GetIntSetting(query, networkId);
       
  1355          RDebug::Print(_L("IAP/Name value: %d"), networkId);
       
  1356          infoBuf.AppendFormat(_L("Network id: %d\n"), networkId);
       
  1357          */
       
  1358 
       
  1359         // Print transferred data
       
  1360         TPckg<TUint> uplinkVolume(0);
       
  1361         TPckg<TUint> downlinkVolume(0);
       
  1362         TRequestStatus status;
       
  1363         iConnection.DataTransferredRequest(uplinkVolume, downlinkVolume, status);
       
  1364         User::WaitForRequest(status);
       
  1365         if(status.Int() == KErrNone)
       
  1366             {
       
  1367             infoBuf.AppendFormat(_L("-up=%d, down=%d\n"), uplinkVolume(), downlinkVolume());
       
  1368             }
       
  1369 
       
  1370         for(i = 1; i <= connCount; i++)
       
  1371             {
       
  1372             infoBuf.AppendFormat(_L("\nConnection %d:\n"), i);
       
  1373             TPckgBuf<TConnectionInfo> connInfo;
       
  1374             result = iConnection.GetConnectionInfo(i, connInfo);
       
  1375             User::LeaveIfError(result);
       
  1376             RDebug::Print(_L("ConnTest: IapId: %d, NetId: %d"), connInfo().iIapId, connInfo().iNetId);
       
  1377             infoBuf.AppendFormat(_L(" -IapId=%d, NetId=%d\n"), connInfo().iIapId, connInfo().iNetId);
       
  1378 
       
  1379             // Get info about clients using this connection
       
  1380             args.iIndex = i;
       
  1381             TConnEnumArgBuf enumBuf(args);
       
  1382 
       
  1383             TConnectionGetClientInfoArg clientInfoArg;
       
  1384             clientInfoArg.iIndex = 1;
       
  1385             TConnGetClientInfoArgBuf clientInfoBuf(clientInfoArg);
       
  1386 
       
  1387             TConnectionClientInfo clientInfo;
       
  1388 
       
  1389             result = iConnection.Control(KCOLConnection, KCoEnumerateConnectionClients, enumBuf);
       
  1390             User::LeaveIfError(result);
       
  1391             infoBuf.AppendFormat(_L(" -clients: %d\n"), enumBuf().iCount);
       
  1392 
       
  1393             for(j = 1; j <= enumBuf().iCount; j++)
       
  1394                 {
       
  1395                 clientInfoBuf().iIndex = j;
       
  1396                 result = iConnection.Control( KCOLConnection,
       
  1397                         KCoGetConnectionClientInfo,
       
  1398                         clientInfoBuf );
       
  1399                 User::LeaveIfError(result);
       
  1400                 clientInfo = clientInfoBuf().iClientInfo;
       
  1401 
       
  1402                 TInt id = clientInfo.iProcessId;
       
  1403                 RDebug::Print(_L("ConnTest: ProcessId: %d"), id);
       
  1404 
       
  1405                 // Get application  name
       
  1406 #ifndef __WINS__
       
  1407             result = appSess.GetAppInfo(appInfo, clientInfo.iUid);
       
  1408             if(result == KErrNone)
       
  1409                 {
       
  1410                 infoBuf.AppendFormat(_L("   client %d=%S\n"), j, &(appInfo.iCaption));
       
  1411                 }
       
  1412             else
       
  1413                 {
       
  1414                 // Servers don't have caption name (e.g. Connection Monitor server)
       
  1415                 infoBuf.AppendFormat(_L("   client %d=%x\n"), j, clientInfo.iUid);
       
  1416                 }
       
  1417 #else
       
  1418             infoBuf.AppendFormat(_L("   client %d=N/A in WINS\n"), j);
       
  1419 #endif        
       
  1420             }
       
  1421 
       
  1422         // Get info about sockets using this connection
       
  1423         TConnectionGetSocketInfoArg socketInfoArg;
       
  1424         socketInfoArg.iIndex = i;
       
  1425         TConnGetSocketInfoArgBuf socketInfoBuf(socketInfoArg);
       
  1426         TConnectionSocketInfo socketInfo;
       
  1427 
       
  1428         result = iConnection.Control( KCOLConnection,
       
  1429                 KCoEnumerateConnectionSockets,
       
  1430                 enumBuf);
       
  1431         User::LeaveIfError(result);
       
  1432         infoBuf.AppendFormat(_L(" -sockets: %d\n"), enumBuf().iCount);
       
  1433 
       
  1434         for(j = 1; j <= enumBuf().iCount; j++)
       
  1435             {
       
  1436             socketInfoBuf().iIndex = j;
       
  1437             result = iConnection.Control( KCOLConnection,
       
  1438                     KCoGetConnectionSocketInfo,
       
  1439                     socketInfoBuf );
       
  1440             User::LeaveIfError(result);
       
  1441             socketInfo = socketInfoBuf().iSocketInfo;
       
  1442 
       
  1443             TUint id = socketInfo.iAddressFamily;
       
  1444             TBuf<5> prot;
       
  1445             socketInfo.iProtocol == KProtocolInetTcp ? prot.Copy(_L("tcp")) : prot.Copy(_L("udp"));
       
  1446 
       
  1447             TSockAddr sa = socketInfo.iSourceAddress;
       
  1448             TSockAddr da = socketInfo.iDestinationAddress;
       
  1449 
       
  1450             TInetAddr& source = TInetAddr::Cast(sa);
       
  1451             TInetAddr& dest = TInetAddr::Cast(da);
       
  1452 
       
  1453             TBuf<20> a1;
       
  1454             source.Output(a1);
       
  1455 
       
  1456             TBuf<20> a2;
       
  1457             dest.Output(a2);
       
  1458 
       
  1459             RDebug::Print(_L("ConnTest: ProcessId: %d"), id);
       
  1460             infoBuf.AppendFormat(_L("  %d. socket: %S\n   %S->\n   %S\n"), j, &prot, &a1, &a2);
       
  1461             }
       
  1462         } // for (i...  
       
  1463     infoBuf.AppendFormat(_L("---------------\n"));
       
  1464     PrintTextToConsole(infoBuf);
       
  1465     CleanupStack::PopAndDestroy(buf);
       
  1466     CleanupStack::PopAndDestroy(&appSess);
       
  1467     }
       
  1468 
       
  1469     // ---------------------------------------------------------
       
  1470     // CSocketsEngine::SendHttpFrameworkRequestL()
       
  1471     // Send HTTP request
       
  1472     // ---------------------------------------------------------
       
  1473     //
       
  1474 void CSocketsEngine::SendHttpFrameworkRequestL( TBool aHasBody,
       
  1475         TBool aDoPerformance,
       
  1476         TBool aIsSecure )
       
  1477     {
       
  1478     iHttpClient->SetPerformance( aDoPerformance );
       
  1479     iHttpClient->InvokeHttpMethodL( iSettingData, aHasBody, aIsSecure );
       
  1480     }
       
  1481 
       
  1482 // ---------------------------------------------------------
       
  1483 // CSocketsEngine::ProgressNotifyReceivedL(TInt aStage)
       
  1484 // Progress notification has been received, display the
       
  1485 // stage in output window.
       
  1486 // ---------------------------------------------------------
       
  1487 //
       
  1488 void CSocketsEngine::ProgressNotifyReceivedL( TInt aStage, TInt aError )
       
  1489     {
       
  1490     
       
  1491     TBuf8<64> text;
       
  1492     text.AppendFormat( _L8("Progress: %d, %d"), aStage, aError );
       
  1493 
       
  1494     TBuf8<16> error;
       
  1495     error.Format( _L8(", %d"), aError );
       
  1496 
       
  1497     switch (aStage)
       
  1498         {
       
  1499         case KConnectionUninitialised: // 0
       
  1500             iConsole.PrintNotify( _L8("Connection uninit"));
       
  1501             iConsole.PrintNotify( error );
       
  1502             break;
       
  1503         case KStartingSelection: // 1000
       
  1504             iConsole.PrintNotify( _L8("Starting selection"));
       
  1505             iConsole.PrintNotify( error );
       
  1506             break;
       
  1507         case KFinishedSelection: // 2000
       
  1508             iConsole.PrintNotify( _L8("Finished selection"));
       
  1509             iConsole.PrintNotify( error );
       
  1510             
       
  1511             if (iCloseConnection)
       
  1512                 {
       
  1513                 // We are starting RConnection instance for testing RConnection::Close.
       
  1514                 // At this point we can attach the monitoring RConnection instance to
       
  1515                 // interface, and delete the temporary progress notifier.
       
  1516                 //
       
  1517                 // We may loose few progress notifications at start up, that's small
       
  1518                 // prize compared to advantages: we get correct notifications when
       
  1519                 // interface goes down, which is the essential part of this test.
       
  1520 
       
  1521                 TBuf<20> query;
       
  1522                 TUint32 networkId;
       
  1523                 TInt result;
       
  1524                 TInt err( KErrNone );
       
  1525                 TConnectionInfo info;
       
  1526                 TPckg<TConnectionInfo> pckgInfo( info );
       
  1527                 
       
  1528                 // Network id
       
  1529                 query.Format( _L("%s\\%s"), IAP, IAP_NETWORK);
       
  1530                 result = iCloseConnection->GetIntSetting(query, networkId);
       
  1531                 if(result == KErrNone)
       
  1532                     {
       
  1533                     info.iIapId = iPrefs.IapId();
       
  1534                     info.iNetId = networkId;
       
  1535                     }
       
  1536                 err = iConnection.Attach( pckgInfo, RConnection::EAttachTypeMonitor );
       
  1537                 if ( err != KErrNone )
       
  1538                     {
       
  1539                     iConsole.ErrorNotify(_L("Attaching failed"), err);
       
  1540                     }
       
  1541                 iTempProgressNotifier->Cancel();
       
  1542                 delete iTempProgressNotifier;
       
  1543                 iTempProgressNotifier = NULL;
       
  1544                 }
       
  1545             break;
       
  1546             case KConnectionFailure: // 2001
       
  1547                         iConsole.PrintNotify(_L8("Connection failure"));
       
  1548                         iConsole.PrintNotify(error);
       
  1549                         break;
       
  1550                         case KMinAgtProgress: // 2500
       
  1551                         iConsole.PrintNotify(_L8("Min agt progress"));
       
  1552                         iConsole.PrintNotify(error);
       
  1553                         break;
       
  1554                         case KConnectionOpen: // 3500
       
  1555                         iConsole.PrintNotify(_L8("Connection open"));
       
  1556                         iConsole.PrintNotify(error);
       
  1557                         break;
       
  1558                         case KConnectionClosed: // 4500
       
  1559                         iConsole.PrintNotify(_L8("Connection closed"));
       
  1560                         iConsole.PrintNotify(error);
       
  1561                         break;
       
  1562                         case KMaxAgtProgress: // 5500
       
  1563                         iConsole.PrintNotify(_L8("Max agent progress"));
       
  1564                         iConsole.PrintNotify(error);
       
  1565                         break;
       
  1566                         case KMinNifProgress: // 6000
       
  1567                         iConsole.PrintNotify(_L8("Min nif progress"));
       
  1568                         iConsole.PrintNotify(error);
       
  1569                         break;
       
  1570                         case KLinkLayerOpen: // 7000
       
  1571                         iConsole.PrintNotify(_L8("Link layer open"));
       
  1572                         iConsole.PrintNotify(error);
       
  1573                         break;
       
  1574                         case KLinkLayerClosed: // 8000
       
  1575                         iConsole.PrintNotify(_L8("Link layer closed"));
       
  1576                         iConsole.PrintNotify(error);
       
  1577                         iHttpClient->SetHttpConnectionInfoL(ETrue, iConnection, iSocketServ);
       
  1578 
       
  1579                         switch (iEngineStatus)
       
  1580                             {
       
  1581                             case EStartingInterface:
       
  1582                             // This case will occur, if username/password prompt dialog
       
  1583                             // is used and cancel is selected in dialog, and when error
       
  1584                             // occurs during interface startup.
       
  1585                             break;
       
  1586                             case EInterfaceDown:
       
  1587                             // EInterfaceDown must be handled also, because this
       
  1588                             // state has been set in StopInterface method.
       
  1589                             break;
       
  1590                             case EListening:
       
  1591                             iListeningSocket.CancelAccept();
       
  1592                             iListeningSocket.Close();
       
  1593                             // FALLTHROUGH
       
  1594                             case ELookingUp:
       
  1595                             // Cancel everything depending on state.
       
  1596                             iResolver.Cancel();
       
  1597                             iResolver.Close();
       
  1598                             // FALLTHROUGH
       
  1599                             case EConnected:
       
  1600                             iSocketsRead->Cancel();
       
  1601                             iSocketsWrite->Cancel();
       
  1602                             // FALLTHROUGH
       
  1603                             case EConnecting:
       
  1604                             // This looks awful, but is required because of the fall through
       
  1605                             // and buggy implementation of the CancelConnect.
       
  1606                             if( EConnecting == iEngineStatus )
       
  1607                                 {
       
  1608                                 iSocket.CancelConnect();
       
  1609                                 }
       
  1610                             // FALLTHROUGH
       
  1611                             case EDisconnecting:
       
  1612                             iSocket.Close();
       
  1613                             // FALLTHROUGH
       
  1614                             case EInterfaceUp:
       
  1615                             if (iRoaming == ERoamingOff)
       
  1616                                 {
       
  1617                                 UnRegisterFromMobilityAPI();
       
  1618                                 SetSocketEngineConnType(ETypeUnknown);
       
  1619                                 ChangeStatus(EInterfaceDown);
       
  1620                                 }
       
  1621                             else
       
  1622                                 {
       
  1623                                 iConsole.PrintNotify(_L8("MobilitySession lost!\n"));
       
  1624                                 UnRegisterFromMobilityAPI();
       
  1625                                 SetSocketEngineConnType(ETypeUnknown);
       
  1626                                 ChangeStatus(EInterfaceDown);
       
  1627                                 }
       
  1628                             break;
       
  1629                             default:
       
  1630                             User::Panic(KPanicSocketsEngine, EConnTestBadStatus);
       
  1631                             break;
       
  1632                             }
       
  1633 
       
  1634                         break;
       
  1635                         case KMaxNifProgress: // 9000
       
  1636                         iConsole.PrintNotify(_L8("Max nif progress\n"));
       
  1637                         break;
       
  1638                         default:
       
  1639                         iConsole.PrintNotify(text);
       
  1640                         }
       
  1641                     iConsole.PrintNotify(_L8("\f"));
       
  1642                     }
       
  1643 
       
  1644                 // ---------------------------------------------------------
       
  1645                 // CSocketsEngine::ProgressNotifyError(TInt aStatus)
       
  1646                 // Some error has occurred while receiving progress
       
  1647                 // notifications.
       
  1648                 // ---------------------------------------------------------
       
  1649                 //
       
  1650 void CSocketsEngine::ProgressNotifyError( TInt aStatus )
       
  1651     {
       
  1652     iConsole.ErrorNotify( _L("<CProgressNotifier> Notify failed"), aStatus );
       
  1653     }
       
  1654 
       
  1655 // ---------------------------------------------------------
       
  1656 // CSocketsEngine::TimerExpired()
       
  1657 // Cancel ongoing operations
       
  1658 // ---------------------------------------------------------
       
  1659 //
       
  1660 void CSocketsEngine::TimerExpired()
       
  1661     {
       
  1662     Cancel();
       
  1663     iConsole.ErrorNotify( _L("<CSocketsEngine> Timed out"), KErrTimedOut );
       
  1664     }
       
  1665 
       
  1666 // ---------------------------------------------------------
       
  1667 // CSocketsEngine::InterfaceInfoL()
       
  1668 // Print information about interfaces.
       
  1669 // ---------------------------------------------------------
       
  1670 //
       
  1671 void CSocketsEngine::InterfaceInfoL()
       
  1672     {
       
  1673     TBuf<128> t;
       
  1674     TAutoClose<RSocketServ> ss;
       
  1675     User::LeaveIfError( ss.iObj.Connect() );
       
  1676     ss.PushL();
       
  1677 
       
  1678     TAutoClose<RSocket> sock;
       
  1679     User::LeaveIfError( sock.iObj.Open( ss.iObj, _L("udp")) );
       
  1680     sock.PushL();
       
  1681 
       
  1682     User::LeaveIfError(
       
  1683             sock.iObj.SetOpt( KSoInetEnumInterfaces, KSolInetIfCtrl ) );
       
  1684 
       
  1685     TProtocolDesc in;
       
  1686     User::LeaveIfError( sock.iObj.Info( in ) );
       
  1687 
       
  1688     TPckgBuf<TSoInetInterfaceInfo> info, next;
       
  1689     
       
  1690     TInt res = sock.iObj.GetOpt( KSoInetNextInterface, KSolInetIfCtrl, info );
       
  1691     if (res != KErrNone)
       
  1692         {
       
  1693         User::Leave( res );
       
  1694         }
       
  1695     TInt count = 0;
       
  1696     while (res == KErrNone)
       
  1697         {
       
  1698         res = sock.iObj.GetOpt( KSoInetNextInterface, KSolInetIfCtrl, next );
       
  1699 
       
  1700         if (info().iName != _L("") && info().iName != _L("loop6") && info().iName != _L("loop4"))
       
  1701             {
       
  1702             t.Format( _L("Interface %d\n"),count++ );
       
  1703             PrintTextToConsole( t );
       
  1704             t.Format( _L("Name \"%S\"\n"), &info().iName );
       
  1705             PrintTextToConsole( t );
       
  1706 
       
  1707             t.Format( _L("State "));
       
  1708             switch (info().iState)
       
  1709                 {
       
  1710                 case EIfPending:
       
  1711                     t.AppendFormat( _L("pending\n"));
       
  1712                     break;
       
  1713                 case EIfUp:
       
  1714                     t.AppendFormat( _L("up\n"));
       
  1715                     break;
       
  1716                 case EIfBusy:
       
  1717                     t.AppendFormat( _L("busy\n"));
       
  1718                     break;
       
  1719                 default:
       
  1720                     t.AppendFormat( _L("down\n"));
       
  1721                     break;
       
  1722                 }
       
  1723 
       
  1724             t.AppendFormat( _L("Mtu %d\n"), info().iMtu );
       
  1725             t.AppendFormat( _L("Speed Metric %d\n"), info().iSpeedMetric );
       
  1726 
       
  1727             t.Format( _L("Features:"));
       
  1728             info().iFeatures & KIfIsLoopback ? t.AppendFormat( _L(" loopback")) : t.AppendFormat( _L(""));
       
  1729             info().iFeatures & KIfIsDialup ? t.AppendFormat( _L(" dialup")) : t.AppendFormat( _L(""));
       
  1730             info().iFeatures & KIfIsPointToPoint ? t.AppendFormat( _L(" pointtopoint")) : t.AppendFormat( _L(""));
       
  1731             info().iFeatures & KIfCanBroadcast ? t.AppendFormat( _L(" canbroadcast")) : t.AppendFormat( _L(""));
       
  1732             info().iFeatures & KIfCanMulticast ? t.AppendFormat( _L(" canmulticast")) : t.AppendFormat( _L(""));
       
  1733             info().iFeatures & KIfCanSetMTU ? t.AppendFormat( _L(" cansetmtu")) : t.AppendFormat( _L(""));
       
  1734             info().iFeatures & KIfHasHardwareAddr ? t.AppendFormat( _L(" hardwareaddr")) : t.AppendFormat( _L(""));
       
  1735             info().iFeatures & KIfCanSetHardwareAddr ? t.AppendFormat( _L(" cansethardwareaddr")) : t.AppendFormat( _L(""));
       
  1736             t.AppendFormat( _L("\n"));
       
  1737 
       
  1738             TName address;
       
  1739             info().iAddress.Output( address );
       
  1740             t.Format( _L("Addr: %S\n"), &address );
       
  1741 
       
  1742             if (info().iAddress.IsLinkLocal())
       
  1743                 {
       
  1744                 t.AppendFormat( _L("  -link local\n"));
       
  1745                 }
       
  1746             else if (info().iAddress.IsSiteLocal())
       
  1747                 {
       
  1748                 t.AppendFormat( _L("  -site local\n"));
       
  1749                 }
       
  1750             else
       
  1751                 {
       
  1752                 t.AppendFormat( _L("  -global\n"));
       
  1753                 }
       
  1754             PrintTextToConsole( t );
       
  1755 
       
  1756             info().iBrdAddr.Output( address );
       
  1757             info().iDefGate.Output( address );
       
  1758             t.Format( _L("Gatew: %S\n"), &address );
       
  1759             info().iNameSer1.Output( address );
       
  1760             t.AppendFormat( _L("DNS 1: %S\n"), &address );
       
  1761             info().iNameSer2.Output( address );
       
  1762             t.AppendFormat( _L("DNS 2: %S\n"), &address );
       
  1763             PrintTextToConsole( t );
       
  1764             
       
  1765             if (info().iHwAddr.Family() != KAFUnspec)
       
  1766                 {
       
  1767                 PrintTextToConsole( _L("Hardware address "));
       
  1768                 TUint j;
       
  1769                 for (j = sizeof(SSockAddr); j < sizeof(SSockAddr) + 6; ++j)
       
  1770                     {
       
  1771                     if (j < (TUint)info().iHwAddr.Length())
       
  1772                         {
       
  1773                         t.Format( _L("%02X"), info().iHwAddr[j] );
       
  1774                         PrintTextToConsole( t );
       
  1775                         }
       
  1776                     else
       
  1777                         {
       
  1778                         PrintTextToConsole( _L("??"));
       
  1779                         }
       
  1780                     if (j < sizeof(SSockAddr) + 5)
       
  1781                         {
       
  1782                         PrintTextToConsole( _L("-"));
       
  1783                         }
       
  1784                     else
       
  1785                         {
       
  1786                         PrintTextToConsole( _L("\n"));
       
  1787                         }
       
  1788                     }
       
  1789                 }
       
  1790             }
       
  1791         if (res == KErrNone)
       
  1792             {
       
  1793             info = next;
       
  1794             PrintTextToConsole( _L("\n"));
       
  1795             }
       
  1796         else
       
  1797             {
       
  1798             PrintTextToConsole( _L("\n"));
       
  1799             }
       
  1800         }
       
  1801     t.Format( _L("Total %d interfaces\n"),count );
       
  1802     PrintTextToConsole( t );
       
  1803 
       
  1804     sock.Pop();
       
  1805     ss.Pop();
       
  1806     }
       
  1807 
       
  1808 // ---------------------------------------------------------
       
  1809 // CSocketsEngine::SetWLANQoS()
       
  1810 // Set QoS for WLAN
       
  1811 // ---------------------------------------------------------
       
  1812 //
       
  1813 void CSocketsEngine::SetWLANQoS( TInt aClass )
       
  1814     {
       
  1815     TInt opt;
       
  1816     TInt err;
       
  1817     TBuf<64> t;
       
  1818     
       
  1819     if (iEngineStatus != EConnected)
       
  1820         {
       
  1821         t.Copy( _L("\nConnect first!\n"));
       
  1822         PrintTextToConsole( t );
       
  1823         return;
       
  1824         }
       
  1825 
       
  1826     err = iSocket.GetOpt( KSoIpTOS, KSolInetIp, opt );
       
  1827 
       
  1828     opt = opt >> 2;
       
  1829 
       
  1830     if (err == KErrNone)
       
  1831         {
       
  1832         t.Format( _L("Old TOS: %x\n"), opt );
       
  1833         }
       
  1834     else
       
  1835         {
       
  1836         t.Format( _L("Cannot get TOS opt\n"));
       
  1837         }
       
  1838 
       
  1839     PrintTextToConsole( t );
       
  1840 
       
  1841     switch (aClass)
       
  1842         {
       
  1843         case 7:
       
  1844             err = iSocket.SetOpt( KSoIpTOS, KSolInetIp, 0xE0 );
       
  1845             break;
       
  1846         case 5:
       
  1847             err = iSocket.SetOpt( KSoIpTOS, KSolInetIp, 0xA0 );
       
  1848             break;
       
  1849         case 3:
       
  1850             err = iSocket.SetOpt( KSoIpTOS, KSolInetIp, 0x60 );
       
  1851             break;
       
  1852         case 1:
       
  1853             err = iSocket.SetOpt( KSoIpTOS, KSolInetIp, 0x20 );
       
  1854             break;
       
  1855         case 0:
       
  1856             err = iSocket.SetOpt( KSoIpTOS, KSolInetIp, 0x0 );
       
  1857             break;
       
  1858         default:
       
  1859             break;
       
  1860         }
       
  1861 
       
  1862     err = iSocket.GetOpt( KSoIpTOS, KSolInetIp, opt );
       
  1863 
       
  1864     opt = opt >> 2;
       
  1865 
       
  1866     if (err == KErrNone)
       
  1867         {
       
  1868         t.Format( _L("New TOS: %x\n"), opt );
       
  1869         }
       
  1870     else
       
  1871         {
       
  1872         t.Format( _L("Cannot set TOS opt\n"));
       
  1873         }
       
  1874     
       
  1875     PrintTextToConsole( t );
       
  1876     
       
  1877     }
       
  1878 
       
  1879 // ---------------------------------------------------------
       
  1880 // CSocketsEngine::QoS1()
       
  1881 // 
       
  1882 // ---------------------------------------------------------
       
  1883 //
       
  1884 void CSocketsEngine::QoS1()
       
  1885     {
       
  1886     /*TQoSSelector selector;
       
  1887      selector.setAddr(iSocket);
       
  1888      
       
  1889      RQoSPolicy policy;
       
  1890      policy.Open(selector);
       
  1891      
       
  1892      CQosParameters* parameters = GetparametersL();
       
  1893      policy.NotifyEvent(iObserver);
       
  1894      policy.SetQoS(*parameters);
       
  1895      
       
  1896      policy.GetQoS();
       
  1897      policy.Close();
       
  1898      */
       
  1899     }
       
  1900 
       
  1901 // ---------------------------------------------------------
       
  1902 // CSocketsEngine::QoS2()
       
  1903 // 
       
  1904 // ---------------------------------------------------------
       
  1905 //
       
  1906 void CSocketsEngine::QoS2()
       
  1907     {
       
  1908     /*CQosParameters* parameters = GetparametersL();
       
  1909      
       
  1910      RQoSChannel channel;
       
  1911      
       
  1912      channel.Open(iSocket);
       
  1913      channel.NotifyEvent(iObserver);
       
  1914      channel.SetQoS(*parameters);
       
  1915      
       
  1916      channel.Join(iSocket2); 
       
  1917      
       
  1918      channel.Leave(ISocket);
       
  1919      
       
  1920      channel.Close();
       
  1921      */
       
  1922     }
       
  1923 // ----------------------------------------------------------------------------
       
  1924 // CSocketsEngine::SendDataL
       
  1925 // Start the sending of the data
       
  1926 // ----------------------------------------------------------------------------
       
  1927 //
       
  1928 void CSocketsEngine::SendDataL( const TBuf8<KSendDataSize> aData )
       
  1929     {
       
  1930     RDebug::Print( _L("ConnTest: SendDataL: size = %d"),aData.Length() );
       
  1931     iSendCount = 0;
       
  1932     iData->Des().Copy( aData );
       
  1933     iData->Des().ZeroTerminate();
       
  1934     iSocket.SetOpt( KSoUdpSynchronousSend, KSolInetUdp, 1 );
       
  1935 
       
  1936     iTroughputDataSize = 0;
       
  1937     iThroughputStartTime.UniversalTime();
       
  1938     
       
  1939     DoSendDataL();
       
  1940     }
       
  1941 
       
  1942 // ----------------------------------------------------------------------------
       
  1943 // CSocketsEngine::DoSendDataL
       
  1944 // Do the actual sending of the data
       
  1945 // ----------------------------------------------------------------------------
       
  1946 //
       
  1947 void CSocketsEngine::DoSendDataL()
       
  1948     {
       
  1949     if (iSendCount++ < (iSettingData->iPackets))
       
  1950         {
       
  1951         // Delay is given in milliseconds, timer uses microseconds.
       
  1952         if (iSettingData->iDelay != 0)
       
  1953             {
       
  1954             if (iSettingData->iProtocol == 1) //  1 == UDP here. Only way to send udp is raw data
       
  1955                 {
       
  1956                 RDebug::Print(
       
  1957                         _L("ConnTest: DoSendDataL: inserting seq number %d"),iSendCount );
       
  1958                 TUint32* seqNumberPointer = (TUint32*)(iData->Des().Ptr());
       
  1959                 *seqNumberPointer = ByteOrder::Swap32( iSendCount ); // put sequence number into to the packet
       
  1960                 }
       
  1961             WriteL( *iData );
       
  1962             iSendTimer->After( (iSettingData->iDelay) * 1000 );
       
  1963             }
       
  1964         else
       
  1965             {
       
  1966             WriteFloodL( *iData, iSettingData->iPackets );
       
  1967             }
       
  1968         }
       
  1969     }
       
  1970 
       
  1971 // ---------------------------------------------------------
       
  1972 // Notifys when a packet has been sent
       
  1973 // aAmount is th amount of data that has been sent
       
  1974 // ---------------------------------------------------------
       
  1975 //
       
  1976 void CSocketsEngine::NotifySend( TInt aAmount )
       
  1977     {
       
  1978     if (iThroughputStartTime != 0)
       
  1979         {
       
  1980         
       
  1981         iTroughputDataSize += aAmount;
       
  1982         TInt size = iSettingData->iPacketSize;
       
  1983         if (iSettingData->iPacketSize > KSendDataSize)
       
  1984             {
       
  1985             size = KSendDataSize;
       
  1986             }
       
  1987         // We add 1 for each packet since the create packet appends an extra \n
       
  1988         // after each packet
       
  1989         if (iTroughputDataSize == (iSettingData->iPackets * size))
       
  1990         // + iSettingData->iPackets) )
       
  1991             {
       
  1992             TBuf8<256> text;
       
  1993             Utils::CalculateThroughput( text, iThroughputStartTime,
       
  1994                     iTroughputDataSize );
       
  1995             iThroughputStartTime = 0;
       
  1996             iConsole.PrintNotify( text );
       
  1997             iTroughputDataSize = 0;
       
  1998             }
       
  1999         }
       
  2000     }
       
  2001 
       
  2002 // ---------------------------------------------------------
       
  2003 // Marks starting time into memory
       
  2004 //
       
  2005 // ---------------------------------------------------------
       
  2006 //
       
  2007 void inline CSocketsEngine::StartTickCount()
       
  2008     {
       
  2009     if (!iUseTTime)
       
  2010         {
       
  2011         iStartTime = User::FastCounter();
       
  2012         }
       
  2013     else
       
  2014         {
       
  2015         iStartTTime.UniversalTime();
       
  2016         iStartTime = 1; // Non null. iStartTime is also used as flag.
       
  2017         }
       
  2018     
       
  2019     }
       
  2020 
       
  2021 // ---------------------------------------------------------
       
  2022 // Calculates time interval using tick count and prints it
       
  2023 //
       
  2024 // ---------------------------------------------------------
       
  2025 //
       
  2026 void CSocketsEngine::StopTickCount( const TDesC& aComponentName )
       
  2027     {
       
  2028     TBuf<128> text;
       
  2029     if (!iUseTTime)
       
  2030         {
       
  2031         TUint32 currentTime = User::FastCounter();
       
  2032         currentTime = currentTime - iStartTime;
       
  2033 
       
  2034         if (iStartTime == 0)
       
  2035             {
       
  2036             return; //do not print if start time hasn't been initialized.
       
  2037             }
       
  2038         iStartTime = 0;
       
  2039 
       
  2040         TInt freq = 0;
       
  2041         TInt err = HAL::Get( HAL::EFastCounterFrequency, freq );
       
  2042         if (err != KErrNone || freq == 0)
       
  2043             {
       
  2044             text.Format( _L("FastCounter error: %d \n"),err );
       
  2045             }
       
  2046         else
       
  2047             {
       
  2048             currentTime = (currentTime * 1000.0) / freq;
       
  2049             text.Format( _L("%u ms by "), currentTime );
       
  2050             text.Append( aComponentName );
       
  2051             text.Append( _L("\n"));
       
  2052             }
       
  2053         }
       
  2054     else
       
  2055         {
       
  2056         TTime currentTime;
       
  2057         currentTime.UniversalTime();
       
  2058 
       
  2059         if (iStartTime == 0)
       
  2060             {
       
  2061             return; //do not print if start time hasn't been initialized.
       
  2062             }
       
  2063         TTimeIntervalMicroSeconds interval = currentTime.MicroSecondsFrom(
       
  2064                 iStartTTime );
       
  2065         iStartTime = 0;
       
  2066 
       
  2067         TBuf<32> app;
       
  2068         app.Format( _L("%u ms by "), interval.Int64() / 1000 );
       
  2069         text.Append( app );
       
  2070         text.Append( aComponentName );
       
  2071         }
       
  2072     
       
  2073     PrintTextToConsole( text );
       
  2074     }
       
  2075 
       
  2076 // end of file