wlan_bearer/wlanagent/src/wlanagtstates.cpp
changeset 0 c40eb8fe8501
child 50 5cdddd04119b
equal deleted inserted replaced
-1:000000000000 0:c40eb8fe8501
       
     1 /*
       
     2 * Copyright (c) 2002-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:  WLAN Agent state machine's state implementations
       
    15 *
       
    16 */
       
    17 
       
    18 /*
       
    19 * %version: 35 %
       
    20 */
       
    21 
       
    22 #include <connectprog.h>
       
    23 #include <comms-infras/dbaccess.h>
       
    24 #include <commdb.h>
       
    25 #include <wdbifwlansettings.h>
       
    26 #include <badesca.h>
       
    27 #include <e32ver.h>
       
    28 #include <utf.h>
       
    29 #include "am_debug.h"
       
    30 #include "genscanlist.h"
       
    31 #include "genscaninfo.h"
       
    32 #include "wlanagtstates.h"
       
    33 #include "wlanagtsm.h"
       
    34 #include "wlanagthotspotclient.inl"
       
    35 
       
    36 static const TUint KConnectionEndWaitTime = 2*1000000; // 2 seconds
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CWlanStateBase::CWlanStateBase
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CWlanStateBase::CWlanStateBase( CWlanSM* aWlanSM, RWLMServer& aWLMServer ) :
       
    43 	CAgentStateBase( *aWlanSM ),
       
    44 	iWlanSM( aWlanSM ),
       
    45 	iWLMServer( aWLMServer )
       
    46     {
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CWlanStateBase::StartState
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 void CWlanStateBase::StartState()
       
    54     {
       
    55 	JumpToRunl( KErrNone );
       
    56     }
       
    57 
       
    58 // Default implementations for the functions from MWLMNotify don't do anything.
       
    59 // If a state is interested in the notifications, it should 'activate' notifications
       
    60 // and override these.
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CWlanStateBase::ConnectionStateChanged
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 void CWlanStateBase::ConnectionStateChanged( TWlanConnectionState /* aNewState */ )
       
    67     {
       
    68 	DEBUG( "CWlanStateBase::ConnectionStateChanged()" );
       
    69     }
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // CWlanStartState::CWlanStartState
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 CWlanStartState::CWlanStartState( CWlanSM* aWlanSM, RWLMServer& aWLMServer ) :
       
    76 	CWlanStateBase( aWlanSM, aWLMServer ),
       
    77 	iNextState( EWlanAgtNextStateUnknown )
       
    78     {
       
    79 	DEBUG( "CWlanStartState constructor" );
       
    80     }
       
    81 
       
    82 // -----------------------------------------------------------------------------
       
    83 // CWlanStartState::RunL
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 void CWlanStartState::RunL()
       
    87     {
       
    88 	if( iWlanSM->WlanSettings()->SSID.Length() == 0 )
       
    89         {
       
    90         PrepareEasyWlanParamsL();
       
    91         }
       
    92 		
       
    93 	DEBUG( "CWlanStartState: next state: EWlanAgtNextStateJoin" );
       
    94 	iNextState = EWlanAgtNextStateJoin;
       
    95 	
       
    96 	iWlanSM->CompleteState( KErrNone );
       
    97     }
       
    98 
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CWlanStartState::PrepareEasyWlanParamsL
       
   102 // -----------------------------------------------------------------------------
       
   103 //
       
   104 void CWlanStartState::PrepareEasyWlanParamsL()
       
   105     {
       
   106 	DEBUG( "CWlanStartState::PrepareEasyWlanParamsL()" );
       
   107     TPckgBuf<TWep> iWep;
       
   108     TPtr8 wepPtr( &iWep().KeyMaterial[0], KWLMMaxWEPKeyLength );
       
   109 	TPckgBuf<TWpaPsk> iPsk;
       
   110 	TPtr8 pskPtr( &iPsk().KeyMaterial[0], KWLMMaxWpaPskLength );
       
   111 	TWLMOverrideSettings overrides;
       
   112 	Mem::FillZ(&overrides, sizeof(overrides));
       
   113   
       
   114 	TBuf8<KMaxSSIDLength> ssid8;
       
   115 	CnvUtfConverter::ConvertFromUnicodeToUtf8( ssid8, iWlanSM->WlanSettings()->UsedSSID );
       
   116 	
       
   117 	overrides.settingsMask = EOverrideSsidMask;
       
   118 	overrides.ssid.ssidLength = ssid8.Length(); 
       
   119 	Mem::Copy(overrides.ssid.ssid, ssid8.Ptr(), overrides.ssid.ssidLength);
       
   120 
       
   121     if ( (iWlanSM->WlanSettings())->ConnectionMode == Adhoc )
       
   122         {
       
   123     	DEBUG( "CWlanStartState::PrepareEasyWlanParamsL(), conn mode: Adhoc" );
       
   124         overrides.settingsMask |= EOverrideIbssMask;
       
   125         }
       
   126     
       
   127     // Set security mode and parameters
       
   128 	switch( iWlanSM->WlanSettings()->SecurityMode )
       
   129     	{
       
   130         case AllowUnsecure:
       
   131         	DEBUG( "CWlanStartState::PrepareEasyWlanParamsL(), security mode: Open" );
       
   132             break;
       
   133         case Wep:
       
   134         	DEBUG( "CWlanStartState::PrepareEasyWlanParamsL(), security mode: Wep" );
       
   135             overrides.settingsMask |= EOverrideWepMask;                
       
   136             
       
   137             Mem::Copy(&iWep().KeyMaterial[0], &((iWlanSM->WlanSettings()->WepKey1).KeyMaterial[0]), (iWlanSM->WlanSettings()->WepKey1).KeyLength);
       
   138             iWep().KeyIndex = (iWlanSM->WlanSettings()->WepKey1).KeyIndex;
       
   139             iWep().KeyLength = (iWlanSM->WlanSettings()->WepKey1).KeyLength;
       
   140             
       
   141             overrides.wep = iWep();
       
   142             break;
       
   143         case Wlan8021x:
       
   144         case Wpa:
       
   145         case Wpa2Only:
       
   146         	if ( iWlanSM->WlanSettings()->EnableWpaPsk )
       
   147         		{
       
   148         		DEBUG1( "CWlanStartState::PrepareEasyWlanParamsL(), security mode: Wpa-psk,len:%d", 
       
   149         				iWlanSM->WlanSettings()->WPAKeyLength );
       
   150         		
       
   151         		overrides.settingsMask |= EOverrideWpaPskMask;
       
   152         		pskPtr.Copy( iWlanSM->WlanSettings()->WPAPreSharedKey );
       
   153         	    iPsk().KeyLength = iWlanSM->WlanSettings()->WPAKeyLength;
       
   154         	    
       
   155         	    overrides.wpaPsk = iPsk();        		
       
   156         		}
       
   157         	else
       
   158         		{
       
   159         		DEBUG( "CWlanStartState::PrepareEasyWlanParamsL(), security mode: Wpa" );
       
   160         		overrides.settingsMask |= EOverrideWpaMask;
       
   161         		}
       
   162             break;
       
   163         default:
       
   164         	DEBUG1( "CWlanStartState::PrepareEasyWlanParamsL(), wrong security mode value in commsdat:%d",
       
   165         							iWlanSM->WlanSettings()->SecurityMode );
       
   166         	User::LeaveIfError(KErrGeneral);
       
   167 		    return;
       
   168     	}
       
   169 	
       
   170 	iWlanSM->SetOverrideSettingsL( overrides );
       
   171     }
       
   172 
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 // CWlanStartState::NextStateL
       
   176 // -----------------------------------------------------------------------------
       
   177 //
       
   178 CAgentStateBase* CWlanStartState::NextStateL(TBool aContinue)
       
   179     {
       
   180 	DEBUG( "CWlanStartState::NextStateL()" );
       
   181 	
       
   182 	if( aContinue )
       
   183 	    {
       
   184 		switch(iNextState)
       
   185 		    {
       
   186     		case EWlanAgtNextStateJoin:
       
   187     			return new (ELeave) CWlanJoinState(iWlanSM, iWLMServer);
       
   188     		case EWlanAgtNextStateOpen:
       
   189     			return new (ELeave) CWlanOpenState(iWlanSM, iWLMServer);
       
   190     		default:
       
   191 	    		return new(ELeave) CWlanErrorState( iWlanSM, KErrCouldNotConnect );
       
   192 		    }
       
   193 	    }
       
   194 	else
       
   195 	{
       
   196 	    return new(ELeave) CWlanErrorState( iWlanSM, KErrCouldNotConnect );
       
   197 	}
       
   198     }
       
   199 
       
   200 // -----------------------------------------------------------------------------
       
   201 // CWlanStartState::DoCancel
       
   202 // -----------------------------------------------------------------------------
       
   203 //
       
   204 void CWlanStartState::DoCancel()
       
   205     {
       
   206     }
       
   207     
       
   208 
       
   209 // -----------------------------------------------------------------------------
       
   210 // CWlanJoinState::CWlanJoinState
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 CWlanJoinState::CWlanJoinState( CWlanSM* aWlanSM, RWLMServer& aWLMServer ) :
       
   214 	CWlanStateBase( aWlanSM, aWLMServer )
       
   215     {
       
   216     DEBUG( "CWlanJoinState constructor" );
       
   217     }
       
   218 
       
   219 // -----------------------------------------------------------------------------
       
   220 // CWlanJoinState::~CWlanJoinState
       
   221 // -----------------------------------------------------------------------------
       
   222 //
       
   223 CWlanJoinState::~CWlanJoinState()
       
   224     {
       
   225 	DEBUG( "CWlanJoinState destructor" );
       
   226 	Cancel();
       
   227 	if ( iAgtHotSpotClient )
       
   228 	    {
       
   229 	    delete iAgtHotSpotClient;
       
   230 	    }
       
   231     }
       
   232 
       
   233 // -----------------------------------------------------------------------------
       
   234 // CWlanJoinState::StartState
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 void CWlanJoinState::StartState()
       
   238     {
       
   239     DEBUG( "CWlanJoinState::StartState()" );
       
   240     TRAPD( ret, DoStartStateL() );
       
   241     if( ret != KErrNone )
       
   242         {
       
   243         iContext = EHotSpotFailure;
       
   244         JumpToRunl( ret );
       
   245         }
       
   246     }
       
   247 
       
   248 // -----------------------------------------------------------------------------
       
   249 // CWlanJoinState::DoStartStateL
       
   250 // -----------------------------------------------------------------------------
       
   251 //
       
   252 void CWlanJoinState::DoStartStateL()
       
   253     {
       
   254     DEBUG( "CWlanJoinState::DoStartStateL()" );
       
   255 
       
   256     // check if plug-in is available
       
   257     TRAPD( ret, iAgtHotSpotClient = CWlanAgtHotSpotClient::NewL() );
       
   258 
       
   259     if ( ret == KErrNone )
       
   260         {
       
   261         DEBUG( "CWlanJoinState::DoStartStateL(), AgtHotSpot plugin available" );
       
   262 
       
   263         //Assume that AP is HotSpot AP. Later if HS Server says its not HS AP, then it will be set as normal AP
       
   264         iWlanSM->SetHotSpotAP( ETrue );
       
   265 
       
   266         // talk to hotspot server	
       
   267         DEBUG( "CWlanJoinState::DoStartStateL(), Sending Start message to HotSpot Server" );
       
   268         iAgtHotSpotClient->HotSpotStart( iWlanSM->IapId(), iStatus );
       
   269         iContext = EHotSpotInProgress;
       
   270         SetActive();
       
   271         }
       
   272     else
       
   273         {
       
   274         iAgtHotSpotClient = NULL;
       
   275         DEBUG(  "CWlanJoinState::DoStartStateL(), Couldn't find AgtHotSpot plug-in,defaulting to normal procedure" );
       
   276 
       
   277         iWLMServer.Join( iStatus, iWlanSM->SettingsId(), iWlanSM->OverrideSettings() );
       
   278         SetActive();
       
   279         }
       
   280     }
       
   281 
       
   282 // -----------------------------------------------------------------------------
       
   283 // CWlanJoinState::NextStateL
       
   284 // -----------------------------------------------------------------------------
       
   285 //
       
   286 CAgentStateBase* CWlanJoinState::NextStateL( TBool aContinue )
       
   287     {
       
   288 	DEBUG( "CWlanJoinState::NextStateL()" );
       
   289 
       
   290 	if( aContinue )
       
   291 	    {
       
   292 	    return new (ELeave) CWlanOpenState( iWlanSM, iWLMServer );
       
   293 	    }
       
   294    	else if( iStatus.Int() == KErrWlanConnAlreadyActive )
       
   295         {
       
   296         return new(ELeave) CWlanErrorState( iWlanSM, KErrWlanConnAlreadyActive );
       
   297         }
       
   298    	else
       
   299         {
       
   300         return new(ELeave) CWlanErrorState( iWlanSM, KErrCouldNotConnect );
       
   301         }
       
   302     }
       
   303 
       
   304 // -----------------------------------------------------------------------------
       
   305 // CWlanJoinState::RunL
       
   306 // -----------------------------------------------------------------------------
       
   307 //
       
   308 void CWlanJoinState::RunL()
       
   309     {
       
   310     DEBUG1( "CWlanJoinState::RunL(), iStatus = %i", iStatus.Int() );
       
   311 
       
   312     if( iAgtHotSpotClient )
       
   313 	{
       
   314 	switch( iContext )
       
   315 	    {
       
   316 	    case EHotSpotInProgress:
       
   317 		if( iStatus != KErrNone )
       
   318 		    {
       
   319 		    if( iStatus == KErrNotSupported )
       
   320 		        {
       
   321                 DEBUG( "CWlanJoinState::RunL(), HotSpot Server returned KErrNotSupported, treating it as normal AP" );
       
   322                 iWlanSM->SetHotSpotAP( EFalse );
       
   323                 iContext = EHotSpotNormalAP;
       
   324                 }
       
   325 		    else
       
   326 		        {
       
   327 		        DEBUG( "CWlanJoinState::RunL(), Error returned by HotSpot Server" );
       
   328 		        iContext = EHotSpotFailure;
       
   329 		        }
       
   330 		    iAgtHotSpotClient->HotSpotCloseConnection( iWlanSM->IapId(), iStatus );
       
   331 		    SetActive();
       
   332 		    }
       
   333 		else
       
   334 		    {
       
   335 		    // send Join request to WLM server
       
   336 		    DEBUG( "CWlanJoinState::RunL(), Sending Join request to Engine" );
       
   337 		    iWLMServer.Join( iStatus, iWlanSM->SettingsId(), iWlanSM->OverrideSettings() );
       
   338 		    iContext = EHotSpotWLMServerHS;
       
   339 		    SetActive();
       
   340 		    }
       
   341 		break;
       
   342     
       
   343 	    case EHotSpotWLMServerHS:
       
   344 		if(iStatus != KErrNone)
       
   345 		    {
       
   346 		    DEBUG( "CWlanJoinState::RunL(), Join failed, Continue trying with HOTSPOT Server" );
       
   347 		    iAgtHotSpotClient->HotSpotStartAgain( iWlanSM->IapId(), iStatus );
       
   348 		    iContext = EHotSpotInProgress;
       
   349 		    SetActive();
       
   350 		    }
       
   351 		else
       
   352 		    {
       
   353 		    DEBUG( "CWlanJoinState::RunL(), Successfully joined to network,  CloseConnection will be called from Disconnect State");
       
   354 		    DEBUG( "CWlanJoinState::RunL(), Sending ServiceStarted indication to NIFMAN" );
       
   355 		    iSMObserver->ServiceStarted();
       
   356 		    iWlanSM->CompleteState( KErrNone );
       
   357 		    }
       
   358 		break;
       
   359 
       
   360 	    case EHotSpotNormalAP:
       
   361 		// send Join request to WLM server
       
   362 		DEBUG( "CWlanJoinState::RunL(), Sending Join request to Engine" );
       
   363 		iWLMServer.Join( iStatus, iWlanSM->SettingsId(), iWlanSM->OverrideSettings() );
       
   364 		iContext = EHotSpotWLMServerNormal;
       
   365 		SetActive();
       
   366 		break;
       
   367 
       
   368 	    case EHotSpotWLMServerNormal:
       
   369 		if(iStatus != KErrNone)
       
   370 		    {   
       
   371 		    DEBUG1( "CWlanJoinState::RunL(), Join failed(%d), not retrying with HotSpot Server, as this is normal AP", iStatus.Int() );
       
   372 		    iSMObserver->ConnectionComplete( EAttemptingToJoin, iStatus.Int() );
       
   373 		    }
       
   374 		else
       
   375 		    {
       
   376 		    DEBUG( "CWlanJoinState::RunL(), Successfully joined to network,Sending ServiceStarted indication to NIFMAN" );
       
   377 		    iSMObserver->ServiceStarted();
       
   378 		    iWlanSM->CompleteState( KErrNone );
       
   379 		    }
       
   380 		break;
       
   381 
       
   382 	    case EHotSpotFailure:
       
   383 		DEBUG( "CWlanJoinState::RunL(), Cancelling the Join operation" );
       
   384 		iSMObserver->ConnectionComplete( EAttemptingToJoin, KErrCouldNotConnect );
       
   385 		break;
       
   386 
       
   387 	    default:
       
   388 		DEBUG( "CWlanJoinState::RunL(), Unknown Active Object context, Panicing.." );
       
   389     		User::Panic( _L("wlanagt"), KErrGeneral );
       
   390     		break;
       
   391 	    }
       
   392 	}
       
   393 
       
   394     else
       
   395 	{
       
   396 	//Normal AP
       
   397 	if(iStatus != KErrNone)
       
   398 	    {
       
   399 	    iSMObserver->ConnectionComplete( EAttemptingToJoin, iStatus.Int() );
       
   400 	    return;
       
   401 	    }
       
   402 	else
       
   403 	    {
       
   404 	    DEBUG( "CWlanJoinState::RunL(), Successfully joined to network" );
       
   405 
       
   406 	    // Nifman loads the NIF after a ServiceStarted() call
       
   407 	    iSMObserver->ServiceStarted();
       
   408 	    iWlanSM->CompleteState( KErrNone );
       
   409 	    }
       
   410 	}
       
   411 
       
   412     }
       
   413 
       
   414 // CWlanJoinState::DoCancel
       
   415 // -----------------------------------------------------------------------------
       
   416 //
       
   417 void CWlanJoinState::DoCancel()
       
   418     {
       
   419     DEBUG( "CWlanJoinState::DoCancel()" );
       
   420     if ( iAgtHotSpotClient )
       
   421         {
       
   422         //Cancel is sent to HSS even if there is no pending request, so that CloseConnection is not 
       
   423         //required to be called seperately
       
   424         DEBUG( "CWlanJoinState::DoCancel(), Sending Cancel to HotSpot Server" );
       
   425         iAgtHotSpotClient->HotSpotCancel( iWlanSM->IapId() );
       
   426         }
       
   427 
       
   428     // If Join request is sent to Engine, cancel it by sending Release request
       
   429     if ( !(iAgtHotSpotClient) || iContext == EHotSpotWLMServerHS || 
       
   430 		iContext == EHotSpotWLMServerNormal )
       
   431         {
       
   432         DEBUG( "CWlanJoinState::DoCancel(), Sending Release to WLM Server" );
       
   433         iWLMServer.Release();
       
   434         }
       
   435     }
       
   436 
       
   437 // -----------------------------------------------------------------------------
       
   438 // CWlanOpenState::CWlanOpenState
       
   439 // -----------------------------------------------------------------------------
       
   440 //
       
   441 CWlanOpenState::CWlanOpenState( CWlanSM* aWlanSM, RWLMServer& aWLMServer ) :
       
   442 	CWlanStateBase( aWlanSM, aWLMServer ),
       
   443 	iSubState( EWlanOpenSubStateOpening )
       
   444     {
       
   445 	DEBUG( "CWlanOpenState constructor" );
       
   446     }
       
   447 
       
   448 // -----------------------------------------------------------------------------
       
   449 // CWlanOpenState::StartState
       
   450 // -----------------------------------------------------------------------------
       
   451 //
       
   452 void CWlanOpenState::StartState()
       
   453     {
       
   454 	DEBUG( "CWlanOpenState::StartState()" );
       
   455 	TRAPD( ret, DoStartStateL() );
       
   456 	JumpToRunl( ret );
       
   457     }
       
   458 
       
   459 // -----------------------------------------------------------------------------
       
   460 // CWlanOpenState::DoStartStateL
       
   461 // -----------------------------------------------------------------------------
       
   462 //
       
   463 void CWlanOpenState::DoStartStateL()
       
   464     {
       
   465 	DEBUG( "CWlanOpenState::DoStartStateL()" );
       
   466 
       
   467     // @to do: whether this callback system is needed anymore or not,
       
   468     // should be checked.
       
   469     
       
   470 	// Create a callback for handling the "disconnect" notification from 
       
   471 	// WLM. We must use async callback to avoid deleting iNotify of RWLMServer 
       
   472 	// "too early" (ConnectionStateChanged() notification call from WLM must 
       
   473 	// not result to calling CancelNotifies() of RWLMServer synchronously).
       
   474 	TCallBack callback( DisconnectCb, this );
       
   475 	iDisconnectCb = new (ELeave) CAsyncCallBack( callback, CActive::EPriorityStandard );
       
   476 
       
   477     }
       
   478 
       
   479 // -----------------------------------------------------------------------------
       
   480 // CWlanOpenState::NextStateL
       
   481 // -----------------------------------------------------------------------------
       
   482 //
       
   483 CAgentStateBase* CWlanOpenState::NextStateL( TBool /*aContinue*/ )
       
   484     {
       
   485 	DEBUG( "CWlanOpenState::NextStateL(), calling RWLMServer::CancelNotifies()" );
       
   486 	iWLMServer.CancelNotifies();
       
   487 	return new (ELeave) CWlanDisconnectState( iWlanSM, iWLMServer );
       
   488     }
       
   489 
       
   490 // -----------------------------------------------------------------------------
       
   491 // CWlanOpenState::RunL
       
   492 // -----------------------------------------------------------------------------
       
   493 //
       
   494 void CWlanOpenState::RunL()
       
   495     {
       
   496 	DEBUG( "CWlanOpenState::RunL()" );
       
   497 	
       
   498 	switch( iSubState )
       
   499 	    {
       
   500 	    case EWlanOpenSubStateOpening:
       
   501 		    if( iStatus != KErrNone )
       
   502 		        {
       
   503 			    iSMObserver->ConnectionComplete( EOpeningConnection, iStatus.Int() );
       
   504 			    return;
       
   505 		        }
       
   506 		    DEBUG( "CWlanOpenState::RunL(), Connection Complete with KErrNone" );
       
   507     		iSMObserver->ConnectionComplete( KConnectionOpen, KErrNone );
       
   508     		iSubState = EWlanOpenSubStateOpened;
       
   509 
       
   510     		DEBUG( "CWlanOpenState::RunL(), calling RWLMServer::ActivateNotifiesL()" );
       
   511     		iWLMServer.ActivateNotifiesL( *this, EWlmNotifyConnectionStateChanged );
       
   512     		break;
       
   513     	case EWlanOpenSubStateOpened:
       
   514     		User::Panic( _L("wlanagt"), KErrGeneral );
       
   515     		break;
       
   516     	default:
       
   517     		User::Panic( _L("wlanagt"), KErrGeneral );
       
   518     		break;
       
   519 	    }
       
   520     }
       
   521 
       
   522 // -----------------------------------------------------------------------------
       
   523 // CWlanOpenState::DoCancel
       
   524 // -----------------------------------------------------------------------------
       
   525 //
       
   526 void CWlanOpenState::DoCancel()
       
   527     {
       
   528 	DEBUG( "CWlanOpenState::DoCancel()" );
       
   529     }
       
   530 
       
   531 // -----------------------------------------------------------------------------
       
   532 // CWlanOpenState::DisconnectCb
       
   533 // -----------------------------------------------------------------------------
       
   534 //
       
   535 TInt CWlanOpenState::DisconnectCb( TAny* aThisPtr )
       
   536     {
       
   537 	DEBUG( "CCWlanOpenState::DisconnectCb()" );
       
   538 
       
   539 	CWlanOpenState* self = static_cast<CWlanOpenState*>( aThisPtr );
       
   540 
       
   541 	// Send a "vendor specific" notification to wlannif. This is the only 
       
   542 	// agent-to-nif notification we use, and always means that interface should 
       
   543 	// be stopped.
       
   544 	self->iSMObserver->Notification( EAgentToNifEventVendorSpecific, NULL );
       
   545 	return KErrNone;
       
   546     }
       
   547 
       
   548 // -----------------------------------------------------------------------------
       
   549 // CWlanOpenState::ConnectionStateChanged
       
   550 // -----------------------------------------------------------------------------
       
   551 //
       
   552 void CWlanOpenState::ConnectionStateChanged( TWlanConnectionState aNewState )
       
   553     {
       
   554 	DEBUG1( "CWlanOpenState::ConnectionStateChanged(), new state %i", aNewState );
       
   555 
       
   556 	// EWlanStateNotConnected is the only connection state we are interested in.
       
   557 	if( aNewState == EWlanStateNotConnected )
       
   558     	{
       
   559 		DEBUG( "CWlanOpenState::ConnectionStateChanged(), calling iDisconnectCb->CallBack()" );
       
   560 	    	    
       
   561 	    iDisconnectCb->CallBack();
       
   562         }
       
   563     }
       
   564 
       
   565 // -----------------------------------------------------------------------------
       
   566 // CWlanOpenState::~CWlanOpenState
       
   567 // -----------------------------------------------------------------------------
       
   568 //
       
   569 CWlanOpenState::~CWlanOpenState()
       
   570     {
       
   571 	DEBUG( "CWlanOpenState destructor" );
       
   572 	delete iDisconnectCb;
       
   573     }
       
   574 
       
   575 // -----------------------------------------------------------------------------
       
   576 // CWlanDisconnectState::CWlanDisconnectState
       
   577 // -----------------------------------------------------------------------------
       
   578 //
       
   579 CWlanDisconnectState::CWlanDisconnectState(CWlanSM* aWlanSM, RWLMServer& aWLMServer) :
       
   580 	CWlanStateBase(aWlanSM, aWLMServer)
       
   581     {
       
   582     DEBUG( "CWlanDisconnectState constructor" );
       
   583     }
       
   584 
       
   585 // -----------------------------------------------------------------------------
       
   586 // CWlanDisconnectState::~CWlanDisconnectState
       
   587 // -----------------------------------------------------------------------------
       
   588 //
       
   589 CWlanDisconnectState::~CWlanDisconnectState()
       
   590     {
       
   591 	DEBUG( "CWlanDisconnectState destructor" );
       
   592 	Cancel();
       
   593 	if ( iAgtHotSpotClient )
       
   594 	    {
       
   595 	    delete iAgtHotSpotClient;
       
   596 	    }
       
   597     }
       
   598 
       
   599 // -----------------------------------------------------------------------------
       
   600 // CWlanDisconnectState::StartState
       
   601 // -----------------------------------------------------------------------------
       
   602 //
       
   603 void CWlanDisconnectState::StartState()
       
   604     {
       
   605     DEBUG( "CWlanDisconnectState::StartState()" );
       
   606     // check if plug-in is available
       
   607    TRAPD( ret, iAgtHotSpotClient = CWlanAgtHotSpotClient::NewL() );
       
   608 
       
   609     if( ret == KErrNone )
       
   610 	{
       
   611 	DEBUG( "CWlanDisconnectState::StartState(), AgtHotSpot plugin is available" );
       
   612 
       
   613 	if ( iWlanSM->IsHotSpotAP() )
       
   614 	    {
       
   615 	    // talk to hospot server	
       
   616 	    DEBUG( "CWlanDisconnectState::StartState(), Sending CloseConnection message to HotSpot Server" );
       
   617 	    iAgtHotSpotClient->HotSpotCloseConnection( iWlanSM->IapId(), iStatus );
       
   618 	    SetActive();
       
   619 	    return;
       
   620 	    }
       
   621 	}
       
   622     else 
       
   623 	{
       
   624 	iAgtHotSpotClient = NULL;
       
   625 	}
       
   626     JumpToRunl( KErrNone );
       
   627     }
       
   628 
       
   629 
       
   630 // -----------------------------------------------------------------------------
       
   631 // CWlanDisconnectState::NextStateL
       
   632 // -----------------------------------------------------------------------------
       
   633 //
       
   634 CAgentStateBase* CWlanDisconnectState::NextStateL( TBool /*aContinue*/ )
       
   635     {
       
   636 	DEBUG( "CWlanDisconnectState::NextStateL()" );
       
   637 	return new (ELeave) CWlanDisconnectState( iWlanSM, iWLMServer );
       
   638     }
       
   639 
       
   640 // -----------------------------------------------------------------------------
       
   641 // CWlanDisconnectState::RunL
       
   642 // -----------------------------------------------------------------------------
       
   643 //
       
   644 void CWlanDisconnectState::RunL()
       
   645     {
       
   646 	DEBUG( "CWlanDisconnectState::RunL()" );
       
   647 	iSMObserver->UpdateProgress( EDisconnecting, KErrNone );
       
   648 	DEBUG( "CWlanDisconnectState::RunL(), calling RWLMServer::Release()" );
       
   649 
       
   650 	// Only call iWLMServer.Release() and assume that disconnect is complete.
       
   651 	iWLMServer.Release();
       
   652 	iSMObserver->DisconnectComplete();
       
   653     }
       
   654 
       
   655 // -----------------------------------------------------------------------------
       
   656 // CWlanDisconnectState::DoCancel
       
   657 // -----------------------------------------------------------------------------
       
   658 //
       
   659 void CWlanDisconnectState::DoCancel()
       
   660     {
       
   661     DEBUG( "CWlanDisconnectState::DoCancel()" );
       
   662     if( iAgtHotSpotClient )
       
   663 	{
       
   664 	iAgtHotSpotClient->HotSpotCancel( iWlanSM->IapId() );
       
   665 	}
       
   666     }
       
   667 
       
   668 // -----------------------------------------------------------------------------
       
   669 // CWlanErrorState::CWlanErrorState
       
   670 // -----------------------------------------------------------------------------
       
   671 //
       
   672 // CWlanErrorState
       
   673 //
       
   674 // "Dummy state" that only reports an error.
       
   675 // Currently only used when the CWlanSM cannot be constructed properly.
       
   676 //
       
   677 CWlanErrorState::CWlanErrorState( CWlanSM* aWlanSM, TInt aError ) :
       
   678 	CAgentStateBase( *aWlanSM ),
       
   679 	iWlanSM( aWlanSM ),
       
   680 	iError( aError )
       
   681     {
       
   682 	DEBUG( "CWlanErrorState constructor" );
       
   683     }
       
   684 
       
   685 // -----------------------------------------------------------------------------
       
   686 // CWlanErrorState::StartState
       
   687 // -----------------------------------------------------------------------------
       
   688 //
       
   689 void CWlanErrorState::StartState()
       
   690     {
       
   691 	DEBUG( "CWlanErrorState::StartState()" );
       
   692 	JumpToRunl( KErrNone );
       
   693     }
       
   694 
       
   695 // -----------------------------------------------------------------------------
       
   696 // CWlanErrorState::NextStateL
       
   697 // -----------------------------------------------------------------------------
       
   698 //
       
   699 CAgentStateBase* CWlanErrorState::NextStateL( TBool /*aContinue*/ )
       
   700     {
       
   701 	DEBUG( "CWlanErrorState::NextStateL()" );
       
   702 	return new (ELeave) CWlanErrorState( iWlanSM, iError );
       
   703     }
       
   704 
       
   705 // -----------------------------------------------------------------------------
       
   706 // CWlanErrorState::RunL
       
   707 // -----------------------------------------------------------------------------
       
   708 //
       
   709 void CWlanErrorState::RunL()
       
   710     {
       
   711 	DEBUG( "CWlanErrorState::RunL()" );
       
   712 	iWlanSM->CompleteState( iError );
       
   713     }
       
   714 
       
   715 // -----------------------------------------------------------------------------
       
   716 // CWlanErrorState::DoCancel
       
   717 // -----------------------------------------------------------------------------
       
   718 //
       
   719 void CWlanErrorState::DoCancel()
       
   720     {
       
   721 	DEBUG( "CWlanErrorState::DoCancel" );
       
   722     }
       
   723 
       
   724 // -----------------------------------------------------------------------------
       
   725 // CWlanWaitConnectionState::CWlanWaitConnectionState
       
   726 // -----------------------------------------------------------------------------
       
   727 //
       
   728 CWlanWaitConnectionState::CWlanWaitConnectionState(
       
   729     CWlanSM* aWlanSM, 
       
   730     RWLMServer& aWLMServer ) :
       
   731     CWlanStateBase( aWlanSM, aWLMServer )
       
   732     {
       
   733     DEBUG(  "CWlanWaitConnectionState::CWlanWaitConnectionState()"  );
       
   734     }
       
   735 
       
   736 // -----------------------------------------------------------------------------
       
   737 // CWlanWaitConnectionState::~CWlanWaitConnectionState
       
   738 // -----------------------------------------------------------------------------
       
   739 //
       
   740 CWlanWaitConnectionState::~CWlanWaitConnectionState()
       
   741     {
       
   742     DEBUG( "CWlanWaitConnectionState::~CWlanWaitConnectionState()" );
       
   743     iTimer.Close();
       
   744     }
       
   745 
       
   746 // -----------------------------------------------------------------------------
       
   747 // CWlanWaitConnectionState::StartState
       
   748 // -----------------------------------------------------------------------------
       
   749 //
       
   750 void CWlanWaitConnectionState::StartState()
       
   751     {
       
   752     DEBUG( "CWlanWaitConnectionState::StartState()" );
       
   753     
       
   754     DEBUG1( "CWlanWaitConnectionState::StartState() - setting the timer to %u microseconds",
       
   755         KConnectionEndWaitTime );
       
   756     
       
   757     TInt ret = iTimer.CreateLocal();
       
   758     if ( ret != KErrNone )
       
   759         {
       
   760         DEBUG1( "CWlanWaitConnectionState::StartState() - unable to create the timer (%d)",
       
   761             ret );
       
   762             
       
   763         JumpToRunl( ret );
       
   764         }
       
   765 
       
   766     iTimer.After( iStatus, KConnectionEndWaitTime );
       
   767     SetActive();
       
   768     }
       
   769 
       
   770 // -----------------------------------------------------------------------------
       
   771 // CWlanWaitConnectionState::NextStateL
       
   772 // -----------------------------------------------------------------------------
       
   773 //
       
   774 CAgentStateBase* CWlanWaitConnectionState::NextStateL(TBool aContinue)
       
   775     {
       
   776     DEBUG( "CWlanWaitConnectionState::NextStateL()"  );
       
   777     DEBUG1( "CWlanWaitConnectionState::NextStateL() - aContinue %u",
       
   778         aContinue );
       
   779 
       
   780 	if( aContinue )
       
   781 	{ 
       
   782 		return new (ELeave) CWlanStartState( iWlanSM, iWLMServer );   
       
   783 	}
       
   784 
       
   785 	return new (ELeave) CWlanErrorState( iWlanSM, KErrWlanConnAlreadyActive );
       
   786     }
       
   787 
       
   788 // -----------------------------------------------------------------------------
       
   789 // CWlanWaitConnectionState::RunL
       
   790 // -----------------------------------------------------------------------------
       
   791 //  
       
   792 void CWlanWaitConnectionState::RunL()
       
   793     {
       
   794     DEBUG( "CWlanWaitConnectionState::RunL()"  );
       
   795     DEBUG1( "CWlanWaitConnectionState::RunL() - iStatus %d" , iStatus.Int() );
       
   796 
       
   797 	if ( iStatus.Int()!= KErrNone )
       
   798 	    {
       
   799 		iSMObserver->ConnectionComplete( EAttemptingToJoin, iStatus.Int() );
       
   800 	    }
       
   801 	else
       
   802 	    {
       
   803 	    TWlanConnectionState state = iWLMServer.GetConnectionState();
       
   804 
       
   805 	    DEBUG1(  "CWlanWaitConnectionState::RunL() - WLAN engine state is %u",
       
   806             state );
       
   807 	    
       
   808 	    if ( state != EWlanStateNotConnected )
       
   809 	        {
       
   810 	        DEBUG( "CWlanWaitConnectionState::RunL() - connection is still open" );
       
   811 	        DEBUG( "CWlanWaitConnectionState::RunL() - completing request with KErrWlanConnAlreadyActive" );
       
   812 
       
   813 		      iSMObserver->ConnectionComplete( EAttemptingToJoin, KErrWlanConnAlreadyActive );		
       
   814 	        }
       
   815             else
       
   816             	{
       
   817             	DEBUG( "CWlanWaitConnectionState::RunL() - connection has closed, continuing with request" );
       
   818             	iWlanSM->CompleteState( KErrNone );
       
   819             	}
       
   820 	    }
       
   821     }
       
   822 
       
   823 // -----------------------------------------------------------------------------
       
   824 // CWlanWaitConnectionState::DoCancel
       
   825 // -----------------------------------------------------------------------------
       
   826 //     
       
   827 void CWlanWaitConnectionState::DoCancel()
       
   828     {
       
   829     DEBUG( "CWlanWaitConnectionState::DoCancel()" );
       
   830     iTimer.Cancel();
       
   831     }