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