wlanutilities/wlansniffer/engine/server/src/wsfwlanbearerconnectionmonitor.cpp
changeset 19 10810c91db26
parent 3 ff3b37722600
child 22 498f36116140
equal deleted inserted replaced
3:ff3b37722600 19:10810c91db26
     1 /*
       
     2 * Copyright (c) 2007-2008 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:  Implementation of CWsfWlanBearerConnectionMonitor
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 //  EXTERNAL INCLUDES
       
    21 #include <utf.h>
       
    22 #include <cmpluginwlandef.h>
       
    23 #include <commdbconnpref.h>
       
    24 #include <wlanerrorcodes.h>
       
    25 #include <cmconnectionmethodext.h>
       
    26 #include <commdb.h>
       
    27 #include <WlanCdbCols.h>
       
    28 
       
    29 
       
    30 
       
    31 //  CLASS HEADER
       
    32 #include "wsfwlanbearerconnectionmonitor.h"
       
    33 
       
    34 //  INTERNAL INCLUDES
       
    35 #include "wsfwlanmonitorobserver.h"
       
    36 #include "wsflogger.h"
       
    37 #include "wsfactivewaiter.h"
       
    38 #include "wsfservercloseradapter.h"
       
    39 #include "wsfcommon.h"
       
    40 
       
    41 
       
    42 //  LOCAL DEFINITIONS
       
    43 using namespace CMManager;
       
    44 
       
    45 
       
    46 #ifdef _DEBUG
       
    47     _LIT( KWlanConnMonPanic, "wsfwlanconnmon" );
       
    48     #define __ASSERTD(x) __ASSERT_DEBUG( (x), \
       
    49                                 User::Panic( KWlanConnMonPanic, __LINE__ ) );
       
    50 #else
       
    51     #define __ASSERTD(x)
       
    52 #endif                                
       
    53 
       
    54 
       
    55 //  LOCAL CONSTANTS
       
    56 /**
       
    57 * Connection id referring to a non-existing connection
       
    58 */
       
    59 static const TInt KNoConnection = -1;
       
    60 
       
    61 /**
       
    62 * Max allowed connection inactivity time in seconds
       
    63 */
       
    64 static const TUint KMaxConnectionInactivityTime = 60;
       
    65 
       
    66 
       
    67 /**
       
    68 * Client polling interval in microseconds
       
    69 */
       
    70 static const TUint KClientPollInterval = 5 * 1000 * 1000;
       
    71 
       
    72 
       
    73 /**
       
    74 * Number of trivial clients of a connection (Sniffer, AI/CP plugin etc)
       
    75 */
       
    76 static const TUint KTrivialClientCount = 2;
       
    77 
       
    78 /**
       
    79 * List of UIDs of the trivial clients
       
    80 */
       
    81 static const TUid KTrivialClientUids[KTrivialClientCount] = 
       
    82     {
       
    83     { 0x10281CAB },         // Sniffer server (wsfserver.exe)
       
    84     { 0x101fD9C5 }          // DHCP server (dhcpserv.exe)
       
    85     };
       
    86 
       
    87 
       
    88 
       
    89 //  CONSTRUCTION AND DESTRUCTION
       
    90 
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // CWsfWlanBearerConnectionMonitor::NewL
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 CWsfWlanBearerConnectionMonitor* CWsfWlanBearerConnectionMonitor::NewL( 
       
    97                                   MWsfServerCloserAdapter& aServerCloser )
       
    98     {
       
    99     CWsfWlanBearerConnectionMonitor* thisPtr = NewLC( aServerCloser );
       
   100     CleanupStack::Pop( thisPtr );
       
   101     return thisPtr;
       
   102     }
       
   103     
       
   104 
       
   105 // ---------------------------------------------------------------------------
       
   106 // CWsfWlanBearerConnectionMonitor::NewLC
       
   107 // ---------------------------------------------------------------------------
       
   108 //
       
   109 CWsfWlanBearerConnectionMonitor* CWsfWlanBearerConnectionMonitor::NewLC( 
       
   110                                   MWsfServerCloserAdapter& aServerCloser )
       
   111     {
       
   112     CWsfWlanBearerConnectionMonitor* thisPtr = 
       
   113            new (ELeave) CWsfWlanBearerConnectionMonitor( aServerCloser );
       
   114     CleanupStack::PushL( thisPtr );
       
   115     thisPtr->ConstructL();
       
   116     return thisPtr;
       
   117     }
       
   118     
       
   119 
       
   120 // ---------------------------------------------------------------------------
       
   121 // CWsfWlanBearerConnectionMonitor::~CWsfWlanBearerConnectionMonitor
       
   122 // ---------------------------------------------------------------------------
       
   123 //
       
   124 CWsfWlanBearerConnectionMonitor::~CWsfWlanBearerConnectionMonitor()
       
   125     {
       
   126     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::"
       
   127                  L"~CWsfWlanBearerConnectionMonitor" );
       
   128     iMonitor.CancelNotifications();
       
   129     iMonitor.Close();
       
   130 
       
   131     Cancel();
       
   132 
       
   133     if ( iConnectionOwned )
       
   134         {
       
   135         // in case Cancel() hasn't done the job
       
   136         TRAP_IGNORE( ShutdownOwnedConnectionL() );
       
   137         }
       
   138     else if ( iMonitoredAp )
       
   139         {
       
   140         
       
   141         }
       
   142 
       
   143     delete iClientPoll;
       
   144 
       
   145     iCmMgr.Close();
       
   146     }
       
   147     
       
   148 
       
   149 // ---------------------------------------------------------------------------
       
   150 // CWsfWlanBearerConnectionMonitor::CWsfWlanBearerConnectionMonitor
       
   151 // ---------------------------------------------------------------------------
       
   152 //
       
   153 CWsfWlanBearerConnectionMonitor::CWsfWlanBearerConnectionMonitor( 
       
   154                                 MWsfServerCloserAdapter& aServerCloser ):
       
   155     CActive( CActive::EPriorityStandard ),
       
   156     iConnectionId( KNoConnection ),
       
   157     iMonitoring( EFalse ),
       
   158     iConnectingState( ECsIdle ),
       
   159     iServerCloser( aServerCloser )
       
   160     {
       
   161     CActiveScheduler::Add( this );
       
   162     }
       
   163     
       
   164 
       
   165 // ---------------------------------------------------------------------------
       
   166 // CWsfWlanBearerConnectionMonitor::ConstructL
       
   167 // ---------------------------------------------------------------------------
       
   168 //
       
   169 void CWsfWlanBearerConnectionMonitor::ConstructL()
       
   170     {
       
   171     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::ConstructL" );
       
   172     iCmMgr.OpenL();
       
   173     iMonitor.ConnectL();
       
   174     iClientPoll = CPeriodic::NewL( CActive::EPriorityLow );
       
   175     FindWlanBearerConnectedL();
       
   176     }
       
   177     
       
   178 
       
   179 // ---------------------------------------------------------------------------
       
   180 // CWsfWlanBearerConnectionMonitor::GetWlanBearerNameL
       
   181 // ---------------------------------------------------------------------------
       
   182 //
       
   183 const TDesC& CWsfWlanBearerConnectionMonitor::GetWlanBearerNameL()
       
   184     {
       
   185     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::GetWlanBearerNameL" );
       
   186     if ( iConnectionId == KNoConnection )
       
   187         {
       
   188         LOG_WRITE( "[no connection]" );
       
   189         return KNullDesC();
       
   190         }
       
   191         
       
   192     LOG_WRITEF( "connection name: %S", &iWlanNetworkName );
       
   193     return iWlanNetworkName;
       
   194     }
       
   195 
       
   196 
       
   197 // ---------------------------------------------------------------------------
       
   198 // CWsfWlanBearerConnectionMonitor::FindWlanBearerConnectedL
       
   199 // ---------------------------------------------------------------------------
       
   200 //
       
   201 void CWsfWlanBearerConnectionMonitor::FindWlanBearerConnectedL()
       
   202     {
       
   203     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::FindWlanBearerConnectedL" );
       
   204     CWsfActiveWaiter* waiter = CWsfActiveWaiter::NewLC();
       
   205     TUint connectionNumber( 0 );
       
   206 
       
   207     iMonitor.GetConnectionCount( connectionNumber, waiter->iStatus );
       
   208     waiter->WaitForRequest();
       
   209     
       
   210     if ( !waiter->iStatus.Int() )
       
   211         {
       
   212         TBool connectionIDFound = EFalse;
       
   213         for ( TUint i = 1; i <= connectionNumber; ++i )
       
   214             {
       
   215             TUint connectionId( 0 );
       
   216             TUint subConnectionCount( 0 );
       
   217             
       
   218             User::LeaveIfError( iMonitor.GetConnectionInfo( i, connectionId, 
       
   219                                                         subConnectionCount ) );
       
   220             if ( CheckConnectionDetailsL( connectionId ) )
       
   221                 {
       
   222                 LOG_WRITEF( "found connection %d", connectionId );
       
   223                 connectionIDFound = ETrue;
       
   224                 iConnectionId = connectionId;
       
   225                 break;
       
   226                 }
       
   227             }
       
   228         if ( !connectionIDFound )
       
   229             {
       
   230             LOG_WRITE( "Reset connection ID" );
       
   231             iConnectionId = KNoConnection;
       
   232             }
       
   233         }
       
   234     else
       
   235         {
       
   236         LOG_WRITEF( "GetConnectionCount failed error = %d", 
       
   237                                                     waiter->iStatus.Int() );
       
   238         }
       
   239         
       
   240     CleanupStack::PopAndDestroy( waiter );
       
   241     }
       
   242  
       
   243             
       
   244 // ---------------------------------------------------------------------------
       
   245 // CWsfWlanBearerConnectionMonitor::EventL
       
   246 // ---------------------------------------------------------------------------
       
   247 //
       
   248 void CWsfWlanBearerConnectionMonitor::EventL( 
       
   249                                        const CConnMonEventBase& aConnMonEvent )
       
   250     {
       
   251     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::EventL" );
       
   252 
       
   253     TConnMonEvent event = TConnMonEvent( aConnMonEvent.EventType() );
       
   254     TInt connectionId = aConnMonEvent.ConnectionId();
       
   255 
       
   256     LOG_WRITEF( "event = %d", (TInt)event );
       
   257     
       
   258     switch ( event )
       
   259         {
       
   260         case EConnMonCreateConnection:
       
   261             {
       
   262             LOG_WRITEF( "EConnMonCreateConnection id = %d", 
       
   263                         connectionId );
       
   264             break;
       
   265             }
       
   266 
       
   267         case EConnMonConnectionStatusChange:
       
   268             {
       
   269             LOG_WRITEF( "EConnMonConnectionStatusChange id = %d", 
       
   270                         connectionId );
       
   271                         
       
   272             const CConnMonConnectionStatusChange* statusChangeEvent = 
       
   273                         static_cast<const CConnMonConnectionStatusChange*>( 
       
   274                                                               &aConnMonEvent );
       
   275                         
       
   276             LOG_WRITEF( "EConnMonConnectionStatusChange status = %d", 
       
   277                         statusChangeEvent->ConnectionStatus() );
       
   278             
       
   279             if ( statusChangeEvent->ConnectionStatus() == KConnectionOpen &&
       
   280                  !iConnectionOwned )
       
   281                 {
       
   282                 LOG_WRITE( "connection status: open" );
       
   283                 TBool wlanEvent( EFalse );
       
   284                 
       
   285                 wlanEvent = CheckConnectionDetailsL( connectionId );
       
   286                 if ( wlanEvent )
       
   287                     {
       
   288                     LOG_WRITEF( "[%S] connected, id = %d", 
       
   289                                 &iWlanNetworkName, connectionId );
       
   290 
       
   291                     // notify observer ...
       
   292                     iConnectionId = connectionId;
       
   293                     iObserver->ConnectionEstablishedL( iWlanNetworkName );
       
   294                     }
       
   295                 }
       
   296             break;
       
   297             }                
       
   298             
       
   299         case EConnMonDeleteConnection:
       
   300             {
       
   301             LOG_WRITEF( "EConnMonDeleteConnection id = %d", connectionId );
       
   302 
       
   303             if ( connectionId == iConnectionId )
       
   304                 {
       
   305                 if ( iConnectionOwned )
       
   306                     {
       
   307                     LOG_WRITE( "connection was owned, manual closing" );
       
   308                     ShutdownOwnedConnectionL();
       
   309                     }
       
   310                 else
       
   311                     {
       
   312                     LOG_WRITE( "connection was not owned" );
       
   313                     iWlanNetworkName.Zero();
       
   314                     iConnectionId = KNoConnection;
       
   315                     
       
   316                     // notify observer
       
   317                     iObserver->ConnectionLostL();
       
   318                     }
       
   319                 }
       
   320             break;
       
   321             }
       
   322         
       
   323         default:
       
   324             {
       
   325             }
       
   326         }
       
   327     }
       
   328     
       
   329 
       
   330 // ---------------------------------------------------------------------------
       
   331 // CWsfWlanBearerConnectionMonitor::CheckConnectionDetailsL
       
   332 // ---------------------------------------------------------------------------
       
   333 //
       
   334 TBool CWsfWlanBearerConnectionMonitor::CheckConnectionDetailsL( 
       
   335                                                          TUint aConnectionId )
       
   336     {
       
   337     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::CheckConnectionDetailsL" );
       
   338     LOG_WRITEF( "aConnectionId: %d", aConnectionId );
       
   339     TConnMonBearerType bearerType( EBearerUnknown );
       
   340 
       
   341     TBool foundWlanBearer( EFalse );
       
   342 
       
   343     // Add only connections with valid id
       
   344     if ( aConnectionId > 0 )
       
   345         {
       
   346         CWsfActiveWaiter* waiter = CWsfActiveWaiter::NewLC();
       
   347         iMonitor.GetIntAttribute( aConnectionId, 0, KBearer, 
       
   348                                   (TInt&)bearerType, 
       
   349                                   waiter->iStatus );
       
   350         waiter->WaitForRequest();
       
   351         LOG_WRITEF( "found bearer: %d", bearerType );        
       
   352 
       
   353         if ( waiter->iStatus.Int() == KErrNone )
       
   354             {
       
   355             LOG_WRITE( "status: KErrNone" );
       
   356             if ( bearerType == EBearerWLAN )
       
   357                 {
       
   358                 
       
   359                 iMonitor.GetStringAttribute( aConnectionId, 0, KNetworkName, 
       
   360                                          iWlanNetworkName, waiter->iStatus );
       
   361                 waiter->WaitForRequest();        
       
   362                 if ( waiter->iStatus.Int() == KErrNone )
       
   363                     {
       
   364                     LOG_WRITEF( "WLAN network name: %S", &iWlanNetworkName );
       
   365                     foundWlanBearer = ETrue;
       
   366                     }
       
   367                 
       
   368                 }
       
   369             }
       
   370         else
       
   371             {
       
   372             LOG_WRITEF( "status: %d", waiter->iStatus.Int() );
       
   373             }
       
   374         CleanupStack::PopAndDestroy( waiter );
       
   375         }
       
   376 
       
   377     return foundWlanBearer;
       
   378     }
       
   379     
       
   380 
       
   381 // ---------------------------------------------------------------------------
       
   382 // CWsfWlanBearerConnectionMonitor::StartMonitoringL
       
   383 // ---------------------------------------------------------------------------
       
   384 //
       
   385 void CWsfWlanBearerConnectionMonitor::StartMonitoringL( 
       
   386                                            MWsfWlanMonitorObserver* aObserver )
       
   387     {
       
   388     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::StartMonitoringL" );        
       
   389     __ASSERTD( aObserver );
       
   390 
       
   391     iObserver = aObserver;
       
   392     if ( !iMonitoring )
       
   393         {
       
   394         iMonitoring = ETrue;
       
   395         iMonitor.NotifyEventL( *this );
       
   396         }
       
   397     }
       
   398     
       
   399 
       
   400 // ---------------------------------------------------------------------------
       
   401 // CWsfWlanBearerConnectionMonitor::StopMonitoring
       
   402 // ---------------------------------------------------------------------------
       
   403 //
       
   404 void CWsfWlanBearerConnectionMonitor::StopMonitoring()
       
   405     {
       
   406     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::StopMonitoring" );        
       
   407 
       
   408     if ( iMonitoring )
       
   409         {
       
   410         iMonitoredAp = 0;
       
   411         iMonitoring = EFalse;
       
   412         iMonitor.CancelNotifications();
       
   413         }
       
   414     }
       
   415     
       
   416 
       
   417 // ---------------------------------------------------------------------------
       
   418 // CWsfWlanBearerConnectionMonitor::ConnectBearer
       
   419 // ---------------------------------------------------------------------------
       
   420 //
       
   421 TInt CWsfWlanBearerConnectionMonitor::ConnectBearer( TUint32 aIapId )
       
   422     {
       
   423     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::ConnectBearer" );
       
   424     
       
   425     if ( iConnectionId != KNoConnection || iConnectingState != ECsIdle )
       
   426         {
       
   427         // there is already a connection
       
   428         LOG_WRITE( "there is already a WLAN connection" );
       
   429         return KErrWlanConnAlreadyActive;
       
   430         }
       
   431     
       
   432     // self-completion
       
   433     if ( iConnectingState == ECsIdle )
       
   434         {
       
   435         LOG_WRITE( "initiating connection process" );
       
   436         iConnIap = aIapId;
       
   437         iConnectingState = ECsNotConnected;
       
   438         iConnectionOwned = ETrue;
       
   439         iServerCloser.WaitForOwnedConnection( ETrue );
       
   440 
       
   441         SetActive();
       
   442         TRequestStatus* status = &iStatus;
       
   443         User::RequestComplete( status, KErrNone );
       
   444         }
       
   445     
       
   446     return KErrNone;
       
   447     }
       
   448 
       
   449 
       
   450 // ---------------------------------------------------------------------------
       
   451 // CWsfWlanBearerConnectionMonitor::DisconnectBearer
       
   452 // ---------------------------------------------------------------------------
       
   453 //
       
   454 TBool CWsfWlanBearerConnectionMonitor::DisconnectBearer()
       
   455     {
       
   456     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::DisconnectBearer" );
       
   457 
       
   458     if ( iConnectionId != KNoConnection )
       
   459         {
       
   460         LOG_WRITEF( "closing connection id = %d", iConnectionId );
       
   461         iMonitor.SetBoolAttribute( iConnectionId, 0, KConnectionStop, ETrue );
       
   462         }
       
   463 
       
   464     return ( iConnectionId != KNoConnection );
       
   465     }
       
   466 
       
   467 
       
   468 // ---------------------------------------------------------------------------
       
   469 // CWsfWlanBearerConnectionMonitor::AbortConnecting
       
   470 // ---------------------------------------------------------------------------
       
   471 //
       
   472 void CWsfWlanBearerConnectionMonitor::AbortConnecting()
       
   473     {
       
   474     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::AbortConnecting" );
       
   475     
       
   476     if ( iConnectingState != ECsIdle )
       
   477         {
       
   478         LOG_WRITE( "connection in progress, aborting");
       
   479         iAborting = ETrue;
       
   480         
       
   481         if ( IsActive() && iConnectingState == ECsSocketOpened )
       
   482             {
       
   483             LOG_WRITE( "forcing connection to stop" );
       
   484             iConnection.Stop();
       
   485             }
       
   486         }
       
   487     }
       
   488     
       
   489 
       
   490 // ---------------------------------------------------------------------------
       
   491 // CWsfWlanBearerConnectionMonitor::MonitorAccessPoint
       
   492 // ---------------------------------------------------------------------------
       
   493 //
       
   494 void CWsfWlanBearerConnectionMonitor::MonitorAccessPoint( TUint32 aIapId )    
       
   495     {
       
   496     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::MonitorAccessPoint" );
       
   497     iMonitoredAp = aIapId;
       
   498     }
       
   499 
       
   500 
       
   501 // ---------------------------------------------------------------------------
       
   502 // CWsfWlanBearerConnectionMonitor::ConnectedWlanConnectionDetailsL
       
   503 // ---------------------------------------------------------------------------
       
   504 //
       
   505 TBool CWsfWlanBearerConnectionMonitor::ConnectedWlanConnectionDetailsL( 
       
   506                                                      TWsfWlanInfo* aWlanInfo )
       
   507     {
       
   508     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::"
       
   509                  L"ConnectedWlanConnectionDetailsL" );
       
   510     
       
   511     LOG_WRITEF( "Monitor iConnectingState =%d and iConnectionId = %d", 
       
   512                          iConnectingState, iConnectionId );
       
   513     
       
   514     if ( iConnectingState == ECsIdle )
       
   515         {
       
   516         // Make sure that we have connection id
       
   517         FindWlanBearerConnectedL();
       
   518         }
       
   519     
       
   520     if ( iConnectionId == KNoConnection )
       
   521         {
       
   522         return EFalse;
       
   523         }
       
   524     
       
   525     // get the data's from the monitor add them to aWlanInfo..
       
   526     aWlanInfo->iConnectionState = EConnected;
       
   527     aWlanInfo->iVisibility = 1;
       
   528     aWlanInfo->iCoverage = 1;
       
   529     aWlanInfo->iNetworkName = KNullDesC8;
       
   530     
       
   531     CWsfActiveWaiter* waiter = CWsfActiveWaiter::NewLC();
       
   532     
       
   533     // security mode
       
   534     TInt securityAttribute( EWlanConnectionSecurityOpen );
       
   535     iMonitor.GetIntAttribute( iConnectionId, 0, KSecurityMode, 
       
   536                               securityAttribute, waiter->iStatus );
       
   537     waiter->WaitForRequest();
       
   538     
       
   539     if ( waiter->iStatus.Int() != KErrNone )
       
   540         {
       
   541         aWlanInfo->iConnectionState = ENotConnected;
       
   542         CleanupStack::PopAndDestroy( waiter );
       
   543         return EFalse;
       
   544         }
       
   545 
       
   546     switch ( securityAttribute )
       
   547         {
       
   548         case EConnMonSecurityWep:
       
   549             {
       
   550             aWlanInfo->iSecurityMode = EWlanSecModeWep;
       
   551             break;
       
   552             }
       
   553 
       
   554         case EConnMonSecurity802d1x:
       
   555             {
       
   556             aWlanInfo->iSecurityMode = EWlanSecMode802_1x;
       
   557             break;
       
   558             }
       
   559 
       
   560         case EConnMonSecurityWpa:       // fall-through
       
   561         case EConnMonSecurityWpaPsk:
       
   562             {
       
   563             aWlanInfo->iSecurityMode = EWlanSecModeWpa;
       
   564             break;
       
   565             }
       
   566 
       
   567         case EConnMonSecurityOpen:      // fall-through
       
   568         default:
       
   569             {
       
   570             aWlanInfo->iSecurityMode = EWlanSecModeOpen;
       
   571             }
       
   572         }    
       
   573     LOG_WRITEF( "conn.secmode = %d", aWlanInfo->iSecurityMode );
       
   574 
       
   575     // network mode    
       
   576     TInt networkModeAttribute( 0 );
       
   577     
       
   578     iMonitor.GetIntAttribute( iConnectionId, 0, KNetworkMode, 
       
   579                               networkModeAttribute, waiter->iStatus );
       
   580     waiter->WaitForRequest();  
       
   581     
       
   582     if ( waiter->iStatus.Int() != KErrNone )
       
   583         {
       
   584         aWlanInfo->iConnectionState = ENotConnected;
       
   585         CleanupStack::PopAndDestroy( waiter );
       
   586         return EFalse;
       
   587         }
       
   588 
       
   589     if ( networkModeAttribute == EConnMonAdHoc )
       
   590         {
       
   591         aWlanInfo->iNetMode = EAdhoc;
       
   592         }
       
   593     else
       
   594         {
       
   595         aWlanInfo->iNetMode = EInfra;
       
   596         }
       
   597         
       
   598     LOG_WRITEF( "conn.netmode = %d", aWlanInfo->iNetMode );
       
   599 
       
   600     // iap id
       
   601     TUint iapIdAttribute( 0 );
       
   602     iMonitor.GetUintAttribute( iConnectionId, 0, KIAPId, 
       
   603                               iapIdAttribute, waiter->iStatus );
       
   604     waiter->WaitForRequest();  
       
   605     
       
   606     if ( waiter->iStatus.Int() != KErrNone )
       
   607         {
       
   608         aWlanInfo->iConnectionState = ENotConnected;
       
   609         CleanupStack::PopAndDestroy( waiter );
       
   610         return EFalse;
       
   611         }
       
   612     
       
   613     aWlanInfo->iIapId = iapIdAttribute;
       
   614     
       
   615     LOG_WRITEF( "conn.iap = %d", aWlanInfo->iIapId );
       
   616 
       
   617     // signal strength
       
   618     TInt signStrengthAttribute( 0 );          
       
   619     iMonitor.GetIntAttribute( iConnectionId, 0, KSignalStrength, 
       
   620                               signStrengthAttribute, waiter->iStatus );
       
   621     waiter->WaitForRequest(); 
       
   622 	
       
   623 	if ( waiter->iStatus.Int() != KErrNone )
       
   624         {
       
   625         aWlanInfo->iConnectionState = ENotConnected;
       
   626         CleanupStack::PopAndDestroy( waiter );
       
   627         return EFalse;
       
   628         }
       
   629 	
       
   630     aWlanInfo->iStrengthLevel = signStrengthAttribute;
       
   631     LOG_WRITEF( "conn.signalstrength = %d", aWlanInfo->iStrengthLevel );
       
   632     
       
   633     if ( aWlanInfo->iIapId )
       
   634         {
       
   635         LOG_WRITE( "IAP id != 0" );
       
   636         
       
   637         RCmConnectionMethodExt cm = iCmMgr.ConnectionMethodL( aWlanInfo->iIapId );
       
   638         CleanupClosePushL( cm );
       
   639         HBufC* buf = NULL;
       
   640         
       
   641         if ( iCmMgr.EasyWlanIdL() != aWlanInfo->iIapId )
       
   642             {
       
   643             LOG_WRITE( "not EasyWLAN" );
       
   644             buf = cm.GetStringAttributeL( ECmName );
       
   645             }
       
   646         else
       
   647             {
       
   648             LOG_WRITE("Sniffer:EasyWLAN IAP ");
       
   649             aWlanInfo->iIapId = 0;
       
   650             buf = cm.GetStringAttributeL( EWlanUsedSSID );
       
   651             }
       
   652         
       
   653         if ( buf )
       
   654             {
       
   655             TInt error = CnvUtfConverter::ConvertFromUnicodeToUtf8( 
       
   656                                                         aWlanInfo->iSsid, 
       
   657                                                         *buf  );
       
   658             if ( error )
       
   659                 {
       
   660                 LOG_WRITE( "ConvertFromUnicodeToUtf8 failed");
       
   661                 aWlanInfo->iSsid.Copy( *buf );
       
   662                 }
       
   663             delete buf;
       
   664             }
       
   665         CleanupStack::PopAndDestroy( &cm ); //cm
       
   666         }
       
   667     else
       
   668         {
       
   669         LOG_WRITE( "IAP id = 0" );
       
   670         }
       
   671 
       
   672 #ifdef _DEBUG
       
   673     // Iap & Ssid logging   
       
   674     HBufC* nameUnicode = aWlanInfo->GetIapNameAsUnicodeLC();
       
   675     LOG_WRITEF( "aWlanInfo->iNetworkName: [%S]", nameUnicode );
       
   676     CleanupStack::PopAndDestroy( nameUnicode );
       
   677     
       
   678     HBufC* ssidUnicode = aWlanInfo->GetSsidAsUnicodeLC();
       
   679     LOG_WRITEF( "aWlanInfo->tmpSsid: [%S]", ssidUnicode );
       
   680     CleanupStack::PopAndDestroy( ssidUnicode );
       
   681 #endif
       
   682 
       
   683     CleanupStack::PopAndDestroy( waiter );
       
   684     
       
   685     return ETrue;    
       
   686     }
       
   687 
       
   688    
       
   689 // ---------------------------------------------------------------------------
       
   690 // CWsfWlanBearerConnectionMonitor::IsConnected
       
   691 // ---------------------------------------------------------------------------
       
   692 //
       
   693 TBool CWsfWlanBearerConnectionMonitor::IsConnected()
       
   694     {
       
   695     return ( iConnectionId != KNoConnection );
       
   696     }
       
   697 
       
   698 
       
   699 // ---------------------------------------------------------------------------
       
   700 // CWsfWlanBearerConnectionMonitor::CheckClientCount
       
   701 // ---------------------------------------------------------------------------
       
   702 //
       
   703 TInt CWsfWlanBearerConnectionMonitor::CheckClientCount( TAny* aPtr )
       
   704     {
       
   705     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::CheckClientCount" );
       
   706     CWsfWlanBearerConnectionMonitor* self = 
       
   707                          static_cast<CWsfWlanBearerConnectionMonitor*>( aPtr );
       
   708     TInt err( KErrNone );
       
   709     
       
   710     if ( !self->iClientCountMutex )
       
   711         {
       
   712         // in some rare cases this function is reentered because of the 
       
   713         // active scheduler waits, so we need a mutex
       
   714         self->iClientCountMutex = ETrue;
       
   715         
       
   716         TRAP( err, self->CheckClientCountL() );
       
   717         
       
   718         self->iClientCountMutex = EFalse;
       
   719         }    
       
   720 
       
   721     return err;
       
   722     }
       
   723 
       
   724 
       
   725 // ---------------------------------------------------------------------------
       
   726 // CWsfWlanBearerConnectionMonitor::CheckClientCountL
       
   727 // ---------------------------------------------------------------------------
       
   728 //
       
   729 void CWsfWlanBearerConnectionMonitor::CheckClientCountL()
       
   730     {
       
   731     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::CheckClientCountL" );
       
   732     
       
   733     CWsfActiveWaiter* waiter = CWsfActiveWaiter::NewL();
       
   734     
       
   735     TConnMonClientEnumBuf clientInfo;
       
   736 
       
   737     iMonitor.GetPckgAttribute( iConnectionId, 0, 
       
   738                                KClientInfo, 
       
   739                                clientInfo, 
       
   740                                waiter->iStatus );
       
   741     waiter->WaitForRequest();
       
   742     delete waiter;
       
   743     
       
   744     // get the client count
       
   745     iClientCount = clientInfo().iCount;
       
   746     
       
   747     // decrease count by each trivial client (Sniffer server, DHCP etc)
       
   748     for ( TInt i( 0 ); i < clientInfo().iCount; ++i )
       
   749         {
       
   750         for ( TInt j( 0 ); j < KTrivialClientCount; ++j )
       
   751             {
       
   752             if ( clientInfo().iUid[i] == KTrivialClientUids[j] )
       
   753                 {
       
   754                 --iClientCount;
       
   755                 LOG_WRITEF( "trivial client [0x%08X] discarded", 
       
   756                             clientInfo().iUid[i].iUid );                
       
   757                 break;
       
   758                 }
       
   759             }
       
   760         }
       
   761     
       
   762     
       
   763     LOG_WRITEF( "iClientCount = %d (trivial clients:%d)", 
       
   764                 iClientCount,
       
   765                 clientInfo().iCount - iClientCount );
       
   766 
       
   767     if ( iAutoDisconnect )
       
   768         {
       
   769         if ( iClientCount )
       
   770             {
       
   771             // there are more clients than the default ones ->
       
   772             // connection is considered active ->
       
   773             // reset the inactivity start time to current time
       
   774             iInactivityStart.UniversalTime();
       
   775             
       
   776             }
       
   777         else
       
   778             {
       
   779             // there are only trivial clients of the connection
       
   780             // this means inactivity, so check the time elapsed
       
   781             LOG_WRITEF( "Connection monitor state = %d", iConnectingState );
       
   782             TTime now;
       
   783             now.UniversalTime();
       
   784 
       
   785             if ( iInactivityStart + TTimeIntervalSeconds( 
       
   786                                        KMaxConnectionInactivityTime ) <= now )
       
   787                 {
       
   788                 // inactivity time limit elapsed, connection should be stopped
       
   789                 LOG_WRITE( "inactivity threshold reached, disconnecting..." );
       
   790                 DisconnectBearer();
       
   791                 }
       
   792             }
       
   793         
       
   794         }
       
   795 
       
   796     }
       
   797 
       
   798 
       
   799 // ---------------------------------------------------------------------------
       
   800 // CWsfWlanBearerConnectionMonitor::ControlDisconnectTimer
       
   801 // ---------------------------------------------------------------------------
       
   802 //
       
   803 TBool CWsfWlanBearerConnectionMonitor::ControlDisconnectTimer( 
       
   804                                                            TUint aAdcCommand )
       
   805     {
       
   806     LOG_ENTERFN("CWsfWlanBearerConnectionMonitor::ControlDisconnectTimer");    
       
   807     
       
   808     if ( iConnectionOwned )
       
   809         {
       
   810         if ( aAdcCommand & EAdcTimerReset )
       
   811             {
       
   812             // reset inactivity time
       
   813             LOG_WRITE( "timer reset" );
       
   814             iInactivityStart.UniversalTime();
       
   815             }
       
   816 
       
   817         if ( aAdcCommand & EAdcStartTimer ) 
       
   818             {
       
   819             if ( !iAutoDisconnect )
       
   820                 {
       
   821                 LOG_WRITE( "timer started" );
       
   822                 iAutoDisconnect = ETrue;
       
   823                 }
       
   824             else
       
   825                 {
       
   826                 LOG_WRITE( "timer had already been started!" );
       
   827                 }
       
   828             }
       
   829         else
       
   830             {
       
   831             iAutoDisconnect = EFalse;
       
   832             LOG_WRITE( "timer stopped" );
       
   833             }
       
   834 
       
   835         }
       
   836     else
       
   837         {
       
   838         LOG_WRITE( "connection is not owned!" );
       
   839         }
       
   840         
       
   841     return iAutoDisconnect;
       
   842     }
       
   843     
       
   844 
       
   845 // ---------------------------------------------------------------------------
       
   846 // CWsfWlanBearerConnectionMonitor::ReleaseShutdownMutex
       
   847 // ---------------------------------------------------------------------------
       
   848 //
       
   849 void CWsfWlanBearerConnectionMonitor::ReleaseShutdownMutex( TAny* aPtr )
       
   850     {
       
   851     CWsfWlanBearerConnectionMonitor* self = 
       
   852                         static_cast<CWsfWlanBearerConnectionMonitor*>( aPtr );
       
   853     self->iShutdownMutex = EFalse;
       
   854     }
       
   855 
       
   856 
       
   857 // ---------------------------------------------------------------------------
       
   858 // CWsfWlanBearerConnectionMonitor::ShutdownOwnedConnectionL
       
   859 // ---------------------------------------------------------------------------
       
   860 //
       
   861 void CWsfWlanBearerConnectionMonitor::ShutdownOwnedConnectionL()
       
   862     {
       
   863     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::ShutdownOwnedConnectionL" );
       
   864     
       
   865     // Make sure that the internal status is reset.
       
   866     iConnectionId = KNoConnection;
       
   867     
       
   868     if ( iShutdownMutex )
       
   869         {
       
   870         return;
       
   871         }
       
   872         
       
   873     // make sure we don't lock ourselves forever
       
   874     CleanupStack::PushL( TCleanupItem( ReleaseShutdownMutex, this ) );
       
   875     iShutdownMutex = ETrue;
       
   876     
       
   877     
       
   878     // roll-back the changes
       
   879     switch ( iConnectingState )
       
   880         {
       
   881         case ECsConnected:
       
   882             {
       
   883             // cancelling the periodic callback
       
   884             iClientPoll->Cancel();
       
   885             }
       
   886             
       
   887         case ECsConnectionCreated:          // fall-through
       
   888             {
       
   889 
       
   890             }
       
   891             
       
   892         case ECsSocketOpened:               // fall-through
       
   893             {
       
   894             // closing the connection handle
       
   895             LOG_WRITE( "closing the connection handle" );
       
   896             iConnection.Close();
       
   897             iSocketServ.Close();
       
   898             }
       
   899             
       
   900         case ECsNotConnected:               // fall-through
       
   901             {
       
   902             // nothing to put here
       
   903             }
       
   904             
       
   905         }
       
   906 
       
   907 
       
   908     // reset the state machine
       
   909     iConnectingState = ECsIdle;
       
   910     iConnectionOwned = EFalse;
       
   911 
       
   912     // tell the engine we have released the IAP 
       
   913     iObserver->ConnectedIapReleasedL();
       
   914 
       
   915     
       
   916     iServerCloser.WaitForOwnedConnection( EFalse );
       
   917     
       
   918     if ( !iAborting )
       
   919         {
       
   920         // notify observers only if connection creation 
       
   921         // was not aborted
       
   922         iObserver->ConnectionLostL();
       
   923         }
       
   924 
       
   925     iAborting = EFalse;
       
   926     
       
   927     CleanupStack::PopAndDestroy( 1 ); // ReleaseShutdownMutex()
       
   928     }
       
   929 
       
   930 
       
   931 // ---------------------------------------------------------------------------
       
   932 // CWsfWlanBearerConnectionMonitor::RunL
       
   933 // ---------------------------------------------------------------------------
       
   934 //
       
   935 void CWsfWlanBearerConnectionMonitor::RunL()
       
   936     {
       
   937     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::RunL" );
       
   938 
       
   939     if ( iAborting )
       
   940         {
       
   941         // if connection creation was aborted, do the cleanup
       
   942         ShutdownOwnedConnectionL();
       
   943         return;
       
   944         }
       
   945        
       
   946     switch ( iConnectingState )
       
   947         {
       
   948         case ECsNotConnected:
       
   949             {
       
   950 #ifdef __WINSCW__
       
   951             User::After(5000000);
       
   952 #endif
       
   953             LOG_WRITE( "<ENotConnected>" );
       
   954 
       
   955             TInt err( KErrNone );
       
   956             err = iSocketServ.Connect();
       
   957             
       
   958             if ( !err )
       
   959                 {
       
   960                 err = iConnection.Open( iSocketServ );
       
   961                 }
       
   962             
       
   963             if ( err )
       
   964                 {
       
   965                 // there was an error while connecting
       
   966                 LOG_WRITEF( "connection.Open error = %d", err );
       
   967                 ShutdownOwnedConnectionL();
       
   968         
       
   969                 iObserver->ConnectingFailedL( err );
       
   970                 break;
       
   971                 }            
       
   972 
       
   973             TCommDbConnPref connPref;
       
   974             connPref.SetIapId( iConnIap );
       
   975             connPref.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
       
   976 
       
   977             iConnectingState = ECsSocketOpened;
       
   978             SetActive();
       
   979             iConnection.Start( connPref, iStatus );
       
   980             break;
       
   981             }
       
   982             
       
   983         case ECsSocketOpened:
       
   984             {
       
   985             LOG_WRITE( "<ESocketOpened>" );
       
   986             if ( iStatus.Int() != KErrNone )
       
   987                 {
       
   988                 // there was an error while connecting
       
   989                 LOG_WRITE( "connection.Start error" );
       
   990                 ShutdownOwnedConnectionL();
       
   991         
       
   992                 iObserver->ConnectingFailedL( iStatus.Int() );
       
   993 
       
   994                 break;
       
   995                 }
       
   996 
       
   997             LOG_WRITE( "connection.Start OK" );
       
   998 
       
   999             // get the connection id
       
  1000             TRAPD( error, FindWlanBearerConnectedL(); );
       
  1001             if ( error || iConnectionId == KNoConnection )
       
  1002                 {
       
  1003                 // At this point we really need the connection id if it exists
       
  1004                 // without connection id server is in very unbalanced state
       
  1005                 LOG_WRITEF( "FindWlanBearerConnectedL error=%d", error );
       
  1006                 ShutdownOwnedConnectionL();
       
  1007                         
       
  1008                 iObserver->ConnectingFailedL( ( error )? error : KErrGeneral );
       
  1009 
       
  1010                 break;
       
  1011                 }
       
  1012 
       
  1013             iConnectingState = ECsConnectionCreated;
       
  1014             SetActive();
       
  1015             TRequestStatus* status = &iStatus;
       
  1016             User::RequestComplete( status, KErrNone );
       
  1017             break;
       
  1018             }
       
  1019             
       
  1020         case ECsConnectionCreated:
       
  1021             {
       
  1022             LOG_WRITE( "<EConnectionCreated>" );
       
  1023             
       
  1024             // start monitoring the iap
       
  1025             MonitorAccessPoint( iConnIap );
       
  1026             
       
  1027             // reset inactivity time
       
  1028             iInactivityStart.UniversalTime();
       
  1029             iAutoDisconnect = EFalse;
       
  1030             
       
  1031             iClientPoll->Start( 
       
  1032                            TTimeIntervalMicroSeconds32( KClientPollInterval ),
       
  1033                            TTimeIntervalMicroSeconds32( KClientPollInterval ),
       
  1034                            TCallBack( CheckClientCount, this ) );
       
  1035                                 
       
  1036             LOG_WRITE( "connection client polling started" );
       
  1037 
       
  1038             // notify observers of the connection name
       
  1039             iObserver->ConnectionEstablishedL( iWlanNetworkName );
       
  1040             
       
  1041             iConnectingState = ECsConnected;
       
  1042             break;
       
  1043             }
       
  1044             
       
  1045         default:
       
  1046             {
       
  1047             }
       
  1048         }
       
  1049     
       
  1050     }
       
  1051 
       
  1052 
       
  1053 // ---------------------------------------------------------------------------
       
  1054 // CWsfWlanBearerConnectionMonitor::DoCancel
       
  1055 // ---------------------------------------------------------------------------
       
  1056 //
       
  1057 void CWsfWlanBearerConnectionMonitor::DoCancel()
       
  1058     {
       
  1059     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::DoCancel" );
       
  1060 
       
  1061     // we were in the middle of a connection creation process    
       
  1062     iAborting = ETrue;
       
  1063     TRAP_IGNORE( ShutdownOwnedConnectionL() );
       
  1064     }
       
  1065 
       
  1066 
       
  1067 // ---------------------------------------------------------------------------
       
  1068 // CWsfWlanBearerConnectionMonitor::RunError
       
  1069 // ---------------------------------------------------------------------------
       
  1070 //
       
  1071 #ifdef _DEBUG
       
  1072 TInt CWsfWlanBearerConnectionMonitor::RunError( TInt aError )
       
  1073     {
       
  1074     LOG_ENTERFN( "CWsfWlanBearerConnectionMonitor::RunError" );    
       
  1075     LOG_WRITEF( "error = %d", aError );
       
  1076 #else
       
  1077 TInt CWsfWlanBearerConnectionMonitor::RunError( TInt /*aError*/ )
       
  1078     {
       
  1079 #endif
       
  1080     return KErrNone;
       
  1081     }
       
  1082 
       
  1083