wlanutilities/wlansniffer/model/src/wsfmodel.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 CWsfModel
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  EXTERNAL INCLUDES
       
    20 #include <eikenv.h>
       
    21 #include <apgcli.h>
       
    22 #include <apgtask.h>
       
    23 #include <ictsclientinterface.h>
       
    24 #include <cmmanagerext.h>
       
    25 #include <cmdestinationext.h>
       
    26 #include <cmpluginwlandef.h>
       
    27 #include <centralrepository.h>
       
    28 #include <internetconnectivitycrkeys.h>
       
    29 #include <featmgr.h>
       
    30 #include <sysutil.h>
       
    31 #include <wlanerrorcodes.h>
       
    32 
       
    33 //  CLASS HEADER
       
    34 #include "wsfmodel.h"
       
    35 
       
    36 //  INTERNAL INCLUDES
       
    37 #include "wsfwlaninfoarray.h"
       
    38 #include "wsfwlaniapwizard.h"
       
    39 #include "wsfapplauncher.h"
       
    40 #include "wsfmodelobserver.h"
       
    41 #include "wsfstatechangeobserver.h"
       
    42 #include "wsfscreensaverwatcher.h"
       
    43 #include "wsflogger.h"
       
    44 
       
    45 using namespace CMManager;
       
    46 
       
    47 
       
    48 /**
       
    49 * UID of helper application 
       
    50 * used when model is instantiated in the active idle
       
    51 */
       
    52 static const TUid KHelperApUid = { 0x10281CEB };
       
    53 
       
    54 /**
       
    55 * UID of Wlan Login application (hsbrowser)
       
    56 * used when launching WLAN Login application
       
    57 */
       
    58 static const TInt KBrowserUid = { 0x2000AFCC };
       
    59 
       
    60 /**
       
    61 * Estimated overhead for access point creation 
       
    62 */
       
    63 const TInt KEstimatedOverhead = 8192;
       
    64 
       
    65 
       
    66 // ----------------------------------------------------------------------------
       
    67 // CWsfModel::NewL
       
    68 // ----------------------------------------------------------------------------
       
    69 //
       
    70 EXPORT_C CWsfModel* CWsfModel::NewL( MWsfStateChangeObserver& aObserver,
       
    71                                      const TBool aScreenSaverAware )
       
    72     {
       
    73     CWsfModel* self = CWsfModel::NewLC( aObserver, aScreenSaverAware );
       
    74     CleanupStack::Pop( self );
       
    75     return self;
       
    76     }
       
    77 
       
    78 
       
    79 // ----------------------------------------------------------------------------
       
    80 // CWsfModel::NewLC
       
    81 // ----------------------------------------------------------------------------
       
    82 //
       
    83 EXPORT_C CWsfModel* CWsfModel::NewLC( MWsfStateChangeObserver& aObserver,
       
    84                                       const TBool aScreenSaverAware )
       
    85     {
       
    86     CWsfModel* self = new( ELeave ) CWsfModel;
       
    87     CleanupStack::PushL( self );
       
    88     self->ConstructL( aObserver, aScreenSaverAware );
       
    89     return self;
       
    90     }
       
    91 
       
    92 
       
    93 // ----------------------------------------------------------------------------
       
    94 // CWsfModel::~CWsfModel
       
    95 // ----------------------------------------------------------------------------
       
    96 //
       
    97 EXPORT_C CWsfModel::~CWsfModel()
       
    98     {
       
    99     iSession.CancelNotifyEvent();
       
   100     iSession.Close();
       
   101     delete iScreenSaverWatcher;
       
   102     delete iArray;   
       
   103     delete iObservedWlan;
       
   104     delete iBrowserLauncher;
       
   105     }
       
   106 
       
   107 
       
   108 // ----------------------------------------------------------------------------
       
   109 // CWsfModel::CWsfModel
       
   110 // ----------------------------------------------------------------------------
       
   111 //
       
   112 CWsfModel::CWsfModel(): 
       
   113     iEikEnv( CEikonEnv::Static() ),
       
   114     iRefreshing( EFalse ),
       
   115     iIctEnded( EFalse ),
       
   116     iKeepConnection( EFalse ),
       
   117     iConnectOnly( EFalse )
       
   118     {
       
   119     }
       
   120 
       
   121 
       
   122 // ----------------------------------------------------------------------------
       
   123 // CWsfModel::ConstructL
       
   124 // ----------------------------------------------------------------------------
       
   125 //
       
   126 void CWsfModel::ConstructL( MWsfStateChangeObserver& aObserver, 
       
   127                             const TBool aScreenSaverAware )
       
   128     {
       
   129     iArray = CWsfWlanInfoArray::NewL(); 
       
   130     iBrowserLauncher = CWsfAppLauncher::NewL();
       
   131     User::LeaveIfError( iSession.Connect() ); 
       
   132     iSession.NotifyEventL( aObserver );
       
   133     if ( aScreenSaverAware )
       
   134         {
       
   135         iScreenSaverWatcher = CWsfScreenSaverWatcher::NewL( *this );
       
   136         }
       
   137     }
       
   138 
       
   139 
       
   140 // ----------------------------------------------------------------------------
       
   141 // CWsfModel::SetEngineObserver
       
   142 // ----------------------------------------------------------------------------
       
   143 //
       
   144 EXPORT_C void CWsfModel::SetEngineObserver( MWsfModelObserver* aObserver )
       
   145     {
       
   146     iObserver = aObserver;
       
   147     }
       
   148 
       
   149 
       
   150 // ----------------------------------------------------------------------------
       
   151 // CWsfModel::GetWlanListL
       
   152 // ----------------------------------------------------------------------------
       
   153 //
       
   154 EXPORT_C CWsfWlanInfoArray* CWsfModel::GetWlanListL()
       
   155     {
       
   156     LOG_ENTERFN( "CWsfModel::GetWlanListL" );
       
   157     iArray->Reset();
       
   158     iSession.UpdateWlanListL( iArray );
       
   159     return iArray;
       
   160     }
       
   161 
       
   162 
       
   163 // ----------------------------------------------------------------------------
       
   164 // CWsfModel::SetActiveViewL
       
   165 // ----------------------------------------------------------------------------
       
   166 //
       
   167 EXPORT_C void CWsfModel::SetActiveViewL( TInt aViewId )
       
   168     {
       
   169     TUid id;
       
   170     id.iUid = aViewId;
       
   171     iActiveView = id;
       
   172     }
       
   173         
       
   174 
       
   175 // ----------------------------------------------------------------------------
       
   176 // CWsfModel::ActiveViewL
       
   177 // ----------------------------------------------------------------------------
       
   178 //
       
   179 EXPORT_C TUid CWsfModel::ActiveViewL()
       
   180     {
       
   181     return iActiveView;
       
   182     }
       
   183 
       
   184     
       
   185 // ----------------------------------------------------------------------------
       
   186 // CWsfModel::SetObservedWlanL
       
   187 // ----------------------------------------------------------------------------
       
   188 //
       
   189 EXPORT_C void CWsfModel::SetObservedWlanL( const TDesC8& aSsid )
       
   190     {
       
   191     HBufC8* ssid = aSsid.AllocL();
       
   192     delete iObservedWlan;
       
   193     iObservedWlan = ssid;
       
   194     }
       
   195     
       
   196 
       
   197 // ----------------------------------------------------------------------------
       
   198 // CWsfModel::ObservedWlan
       
   199 // ----------------------------------------------------------------------------
       
   200 //
       
   201 EXPORT_C const TDesC8& CWsfModel::ObservedWlan()
       
   202     {
       
   203     return *iObservedWlan;
       
   204     }
       
   205 
       
   206 
       
   207 // ----------------------------------------------------------------------------
       
   208 // CWsfModel::ConnectL
       
   209 // ----------------------------------------------------------------------------
       
   210 //
       
   211 EXPORT_C int CWsfModel::ConnectL( TUint32 aIapId )
       
   212     {
       
   213     LOG_ENTERFN( "CWsfModel::ConnectL" );
       
   214     
       
   215     if ( iObserver )
       
   216         {
       
   217         iObserver->ConnectingL( aIapId );
       
   218         }
       
   219     
       
   220     TInt err = iSession.ConnectWlanBearerL( aIapId, EIapPersistent );
       
   221     if ( err == KErrNone )
       
   222         {
       
   223         iConnectedIapId = aIapId;
       
   224         }
       
   225     else
       
   226         {
       
   227         if ( iObserver )
       
   228             {
       
   229             iObserver->BrowserLaunchFailed( err );            
       
   230             }
       
   231         }
       
   232 
       
   233     if ( iObserver )
       
   234         {
       
   235         iObserver->ConnectingFinishedL( err );
       
   236         }
       
   237 
       
   238     TBool timerStarted( EFalse );
       
   239     timerStarted = iSession.ControlDisconnectTimerL( 
       
   240                                          EAdcStartTimer | EAdcTimerReset );
       
   241     
       
   242     if ( !timerStarted )
       
   243         {
       
   244         LOG_WRITE( "auto-disconnect timer couldn't be started!" );
       
   245         }
       
   246     
       
   247     
       
   248     iRefreshing = iSession.RequestScanL();   
       
   249     
       
   250     return err;
       
   251     }
       
   252 
       
   253 
       
   254 // ----------------------------------------------------------------------------
       
   255 // CWsfModel::ConnectWithoutConnWaiterL
       
   256 // ----------------------------------------------------------------------------
       
   257 //
       
   258 EXPORT_C int CWsfModel::ConnectWithoutConnWaiterL( TUint32 aIapId, 
       
   259                                                    TBool aTestedAccessPoint )
       
   260     {
       
   261     LOG_ENTERFN( "CWsfModel::ConnectWithoutConnWaiterL" );
       
   262     
       
   263     if ( iObserver )
       
   264         {
       
   265         iObserver->ConnectingL( aIapId );
       
   266         }
       
   267     if ( aTestedAccessPoint )
       
   268         {
       
   269         return iSession.ConnectWlanBearerWithoutConnWaiterL( aIapId, 
       
   270                                                        EIapPersistent );
       
   271         }
       
   272     else
       
   273         {
       
   274         return iSession.ConnectWlanBearerWithoutConnWaiterL( aIapId, 
       
   275                                                        EIapExpireOnDisconnect );
       
   276         }
       
   277     }
       
   278 
       
   279 
       
   280 // ----------------------------------------------------------------------------
       
   281 // CWsfModel::FinalizeConnectL
       
   282 // ----------------------------------------------------------------------------
       
   283 //
       
   284 EXPORT_C void CWsfModel::FinalizeConnectL()
       
   285     {
       
   286     LOG_ENTERFN( "CWsfModel::FinalizeConnectL" );
       
   287     
       
   288     TBool timerStarted( EFalse );
       
   289     timerStarted = iSession.ControlDisconnectTimerL( 
       
   290                                               EAdcStartTimer | EAdcTimerReset );
       
   291     
       
   292     if ( !timerStarted )
       
   293         {
       
   294         LOG_WRITE( "auto-disconnect timer couldn't be started!" );
       
   295         }
       
   296     }
       
   297 
       
   298 
       
   299 // ----------------------------------------------------------------------------
       
   300 // CWsfModel::DisconnectL
       
   301 // ----------------------------------------------------------------------------
       
   302 //
       
   303 EXPORT_C void CWsfModel::DisconnectL()
       
   304     {
       
   305     LOG_ENTERFN( "CWsfModel::DisconnectL" );
       
   306     iSession.DisconnectWlanBearerL();
       
   307     iConnectedIapId = 0;
       
   308     iConnectedNetId = 0;
       
   309     iConnectOnly = EFalse;
       
   310     iRefreshing = iSession.RequestScanL();    
       
   311     }
       
   312 
       
   313 
       
   314 // ----------------------------------------------------------------------------
       
   315 // CWsfModel::Refreshing
       
   316 // ----------------------------------------------------------------------------
       
   317 //
       
   318 EXPORT_C TBool CWsfModel::Refreshing()
       
   319     {
       
   320     return iRefreshing;
       
   321     }
       
   322 
       
   323 
       
   324 // ----------------------------------------------------------------------------
       
   325 // CWsfModel::SetRefreshState
       
   326 // ----------------------------------------------------------------------------
       
   327 //
       
   328 EXPORT_C void CWsfModel::SetRefreshState( TBool aRefreshing )
       
   329     {
       
   330     iRefreshing = aRefreshing;
       
   331     }
       
   332 
       
   333 
       
   334 // ----------------------------------------------------------------------------
       
   335 // CWsfModel::EnableScanL
       
   336 // ----------------------------------------------------------------------------
       
   337 //
       
   338 EXPORT_C TBool CWsfModel::EnableScanL()
       
   339     {
       
   340     LOG_ENTERFN( "CWsfModel::EnableScanL" );
       
   341     TBool enableScan( iSession.EnableScanL() );
       
   342     if ( iScreenSaverWatcher )
       
   343         {
       
   344         iScreenSaverWatcher->Cancel();
       
   345         iScreenSaverWatcher->StartStatusScanning();
       
   346         }
       
   347     return enableScan;
       
   348     }
       
   349 
       
   350 
       
   351 // ----------------------------------------------------------------------------
       
   352 // CWsfModel::DisableScanL
       
   353 // ----------------------------------------------------------------------------
       
   354 //
       
   355 EXPORT_C TBool CWsfModel::DisableScanL()
       
   356     {
       
   357     LOG_ENTERFN( "CWsfModel::DisableScanL" );
       
   358     TBool disableScan( iSession.DisableScanL() );   
       
   359     if ( iScreenSaverWatcher )
       
   360         {
       
   361         iScreenSaverWatcher->Cancel();
       
   362         }
       
   363     return disableScan; 
       
   364     }
       
   365 
       
   366 
       
   367 // ----------------------------------------------------------------------------
       
   368 // CWsfModel::IsScanEnabledL
       
   369 // ----------------------------------------------------------------------------
       
   370 //
       
   371 EXPORT_C TBool CWsfModel::IsScanEnabledL()
       
   372     {
       
   373     return iSession.IsScanEnabledL(); 
       
   374     }
       
   375 
       
   376 
       
   377 // ----------------------------------------------------------------------------
       
   378 // CWsfModel::LaunchBrowserL
       
   379 // ----------------------------------------------------------------------------
       
   380 //
       
   381 EXPORT_C void CWsfModel::LaunchBrowserL( TUint32 aIapId )
       
   382     {
       
   383     LOG_ENTERFN( "CWsfModel::LaunchBrowserL" );
       
   384     
       
   385     if ( !iBrowserLauncher->Launching() ||
       
   386          iBrowserLauncher->BrowserIap() != aIapId ) 
       
   387         {
       
   388         LOG_WRITE( "launching browser..." );
       
   389         iConnectedIapId = aIapId;        
       
   390         iSession.MonitorAccessPointL( aIapId );
       
   391         iSession.ControlDisconnectTimerL( EAdcStopTimer );
       
   392         iBrowserLauncher->LaunchBrowserL( *this, aIapId );
       
   393         }
       
   394     else if ( iBrowserLauncher->BrowserIap() == aIapId ) 
       
   395         {
       
   396         LOG_WRITE( "bringing browser to foreground..." );
       
   397         ContinueBrowsingL();
       
   398         }
       
   399         
       
   400     }
       
   401 
       
   402 
       
   403 // ----------------------------------------------------------------------------
       
   404 // CWsfModel::CleanUpCancelledLaunchL
       
   405 // ----------------------------------------------------------------------------
       
   406 //
       
   407 EXPORT_C void CWsfModel::CleanUpCancelledLaunchL()
       
   408     {
       
   409     LOG_ENTERFN( "CWsfModel::CleanUpCancelledLaunchL" );
       
   410     iSession.SetIapPersistenceL( EIapForcedExpiry );
       
   411     }
       
   412     
       
   413 
       
   414 // ----------------------------------------------------------------------------
       
   415 // CWsfModel::ContinueBrowsingL
       
   416 // ----------------------------------------------------------------------------
       
   417 //
       
   418 EXPORT_C void CWsfModel::ContinueBrowsingL()
       
   419     {
       
   420     LOG_ENTERFN( "CWsfModel::ContinueBrowsingL(void)" );
       
   421     iBrowserLauncher->ContinueBrowsingL();
       
   422     }
       
   423     
       
   424 
       
   425 // ----------------------------------------------------------------------------
       
   426 // CWsfModel::ContinueBrowsingL
       
   427 // ----------------------------------------------------------------------------
       
   428 //
       
   429 EXPORT_C void CWsfModel::ContinueBrowsingL( TUint32 aIapId )
       
   430     {
       
   431     LOG_ENTERFN( "CWsfModel::ContinueBrowsingL(TUint32)" );
       
   432     iBrowserLauncher->ContinueBrowsingL( *this, aIapId );
       
   433     }    
       
   434 
       
   435 
       
   436 // ----------------------------------------------------------------------------
       
   437 // CWsfModel::ConnectivityObserver
       
   438 // ----------------------------------------------------------------------------
       
   439 //
       
   440 void CWsfModel::ConnectivityObserver( TIctsTestResult aResult, 
       
   441                                       const TDesC& aString )
       
   442     {
       
   443     LOG_ENTERFN( "CWsfModel::ConnectivityObserver" );
       
   444     LOG_WRITEF( "ICTS result: %d", aResult );
       
   445     
       
   446     TBool makePersistent( EFalse );
       
   447     // check the result
       
   448     switch ( aResult )
       
   449         {
       
   450         case EConnectionOk:
       
   451             {
       
   452             // test succeeded
       
   453             TRAP_IGNORE( MoveToInternetSnapL( iConnectedIapId ) );
       
   454             makePersistent = ETrue;
       
   455             LOG_WRITE( "ICT: EConnectionOk" );
       
   456             break;            
       
   457             }
       
   458             
       
   459         case EConnectionNotOk:
       
   460             {
       
   461             // test was run but it failed
       
   462             LOG_WRITE( "ICT: EConnectionNotOk" );
       
   463             break;
       
   464             }
       
   465         case EHttpAuthenticationNeeded:
       
   466             {
       
   467             // test was run but HTTP authentication is required
       
   468             LOG_WRITE( "ICT: EHttpAuthenticationNeeded" );
       
   469             if ( iConnectOnly )
       
   470                 {
       
   471                 // Connect selected. WLAN Login needed.
       
   472                 TRAP_IGNORE( LaunchWlanLoginL(aString) );
       
   473                 }    
       
   474             break;
       
   475             }    
       
   476         case ETimeout:
       
   477             {
       
   478             LOG_WRITE( "ICT: ETimeout" );
       
   479             break;
       
   480             }
       
   481             
       
   482         default:
       
   483             {
       
   484             _LIT( KIctPanic, "ICT result" );
       
   485             User::Panic( KIctPanic, aResult );
       
   486             }
       
   487         }
       
   488 
       
   489     if ( makePersistent )
       
   490         {
       
   491         TWsfIapPersistence pt = ( iConnectedIapId )? 
       
   492                                      EIapPersistent: 
       
   493                                      EIapExpireOnShutdown;
       
   494                                      
       
   495         TRAPD( err, MakeIctIapPersistentL( pt ) );
       
   496         if ( err )
       
   497             {
       
   498             LOG_WRITEF( "MakeIctIapPersistentL leaved with error = %d", err );
       
   499             }
       
   500         }
       
   501 
       
   502     if ( iKeepConnection )
       
   503         {
       
   504         // trigger the auto-disconnect timer as well
       
   505         TBool timerStarted( EFalse );
       
   506         TRAP_IGNORE( timerStarted = iSession.ControlDisconnectTimerL( 
       
   507                                           EAdcStartTimer | EAdcTimerReset ) );
       
   508         
       
   509         if ( !timerStarted )
       
   510             {
       
   511             LOG_WRITE( "auto-disconnect timer couldn't be started!" );
       
   512             }
       
   513         }
       
   514         
       
   515     LOG_WRITE( "before AsyncStop" );
       
   516     // finally stop blocking the caller
       
   517     iIctEnded = ETrue; 
       
   518     if ( iIctWait.IsStarted() )
       
   519         {
       
   520         LOG_WRITE( "ICT: AsyncStop" );
       
   521         iIctWait.AsyncStop();
       
   522         } 
       
   523      
       
   524 
       
   525     }
       
   526 
       
   527 // -----------------------------------------------------------------------------
       
   528 // CWsfModel::LaunchWlanLoginL()
       
   529 // -----------------------------------------------------------------------------
       
   530 //    
       
   531 void CWsfModel::LaunchWlanLoginL( const TDesC& aString )
       
   532     {   
       
   533     LOG_ENTERFN( "WsfModel::LaunchWlanLoginL" );
       
   534     HBufC* param = HBufC::NewLC( KMaxFileName );
       
   535     _LIT(tmpString, "%d, %d, %S");
       
   536     param->Des().Format( tmpString, 
       
   537                          iConnectedIapId, 
       
   538                          iConnectedNetId, 
       
   539                          &aString );
       
   540     TUid uid( TUid::Uid( KBrowserUid ) );
       
   541     TThreadId id;
       
   542     
       
   543     RApaLsSession appArcSession;
       
   544     User::LeaveIfError( appArcSession.Connect() ); 
       
   545     CleanupClosePushL( appArcSession );
       
   546         
       
   547     TInt err = appArcSession.StartDocument( *param, TUid::Uid( KBrowserUid ), id );
       
   548     if ( err != KErrNone )
       
   549         {
       
   550         LOG_ENTERFN( "WsfModel::LaunchWlanLoginL failed" );
       
   551         }
       
   552     CleanupStack::PopAndDestroy( &appArcSession );
       
   553     CleanupStack::PopAndDestroy( param );
       
   554     }
       
   555 
       
   556 // ----------------------------------------------------------------------------
       
   557 // CWsfModel::MakeIctIapPersistentL
       
   558 // ----------------------------------------------------------------------------
       
   559 //
       
   560 void CWsfModel::MakeIctIapPersistentL( TWsfIapPersistence aPersistence )
       
   561     {
       
   562     LOG_ENTERFN( "CWsfModel::MakeIctIapPersistentL" );
       
   563     LOG_WRITEF( "temp ICT IAP id = %d", iIctWlanInfo.iIapId );
       
   564     
       
   565     if ( !iSession.SetIapPersistenceL( aPersistence ) )
       
   566         {
       
   567         LOG_WRITE( "setting temporary flag FAILED" );
       
   568         }
       
   569     
       
   570     }
       
   571 
       
   572 // ----------------------------------------------------------------------------
       
   573 // CWsfModel::MoveToInternetSnapL
       
   574 // ----------------------------------------------------------------------------
       
   575 //
       
   576 void CWsfModel::MoveToInternetSnapL( const TUint32 aIapId )
       
   577     {
       
   578 	LOG_ENTERFN( "CWsfModel::MoveToInternetSnapL" );
       
   579     // Read all destination(SNAP) settings into an array
       
   580     RArray<TUint32> destinations;
       
   581     CleanupClosePushL(destinations);
       
   582     RCmManagerExt cmManager;
       
   583     cmManager.OpenL();
       
   584     CleanupClosePushL( cmManager );      
       
   585     cmManager.AllDestinationsL(destinations);
       
   586     RCmDestinationExt destination;
       
   587     // Loop through each destination
       
   588     for(TInt i = 0; i < destinations.Count(); i++)
       
   589         {
       
   590         destination = cmManager.DestinationL(destinations[i]);
       
   591         CleanupClosePushL(destination); 
       
   592         // Internet destination will always exist in the system.
       
   593         // Internet destination will have ESnapPurposeInternet set in its metadata.
       
   594         if (destination.MetadataL(CMManager::ESnapMetadataPurpose) == CMManager::ESnapPurposeInternet)
       
   595             {
       
   596             RCmConnectionMethodExt iap = cmManager.ConnectionMethodL( aIapId );
       
   597             CleanupClosePushL( iap );     
       
   598             LOG_WRITE( "Move Iap to internet destination" );
       
   599             destination.AddConnectionMethodL( iap );
       
   600             destination.UpdateL();
       
   601             CleanupStack::PopAndDestroy( &iap ); 
       
   602             }
       
   603         CleanupStack::PopAndDestroy( &destination ); 
       
   604         }
       
   605     CleanupStack::PopAndDestroy( &cmManager ); 
       
   606     CleanupStack::PopAndDestroy( &destinations ); 
       
   607     }
       
   608 
       
   609 // ----------------------------------------------------------------------------
       
   610 // CWsfModel::CreateAccessPointL
       
   611 // ----------------------------------------------------------------------------
       
   612 //
       
   613 EXPORT_C TBool CWsfModel::CreateAccessPointL( TWsfWlanInfo& aWlan, 
       
   614                                               TBool aExplicitDefine )
       
   615     {
       
   616     LOG_ENTERFN( "CWsfModel::CreateAccessPointL" );
       
   617     if ( aExplicitDefine )
       
   618         {
       
   619         LOG_WRITE( "called from 'Define access point'" );
       
   620         }
       
   621     
       
   622     CheckSpaceBelowCriticalLevelL();
       
   623     CheckUnknownWapiL( aWlan );
       
   624 
       
   625 #pragma message("TODO: oursource UI to client interfaces!")        
       
   626     CWsfWlanIapWizard* iapWizard = CWsfWlanIapWizard::NewLC();
       
   627     
       
   628     // the wlaninfo must be persistent to avoid nullpointer crashes due to
       
   629     // background refreshing 
       
   630     TBool ret( ETrue );
       
   631     
       
   632     // query necessary data
       
   633     if ( !iapWizard->LaunchWizardL( aWlan, aExplicitDefine ) )
       
   634         {
       
   635         LOG_WRITE( "iapWizard.LaunchWizardL failed" );
       
   636         ret = EFalse;
       
   637         }
       
   638 
       
   639     // then create accesspoint
       
   640     if ( ret )
       
   641         {
       
   642         if ( iapWizard->CreateAccessPointL() )
       
   643             {
       
   644             // copy back the IAP id
       
   645             LOG_WRITEF( "IAP id = %d", aWlan.iIapId );
       
   646             }
       
   647         else
       
   648             {
       
   649             LOG_WRITE( "iapWizard.CreateAccessPointL failed" );
       
   650             ret = EFalse;
       
   651             }
       
   652         }
       
   653 
       
   654     CleanupStack::PopAndDestroy( iapWizard );
       
   655     
       
   656     return ret;
       
   657     }
       
   658 
       
   659 
       
   660 // ----------------------------------------------------------------------------
       
   661 // CWsfModel::IctsTestPermission
       
   662 // ----------------------------------------------------------------------------
       
   663 //
       
   664 EXPORT_C TInt CWsfModel::IctsTestPermission()
       
   665     {
       
   666     LOG_ENTERFN( "CWsfModel::IctsTestPermission" );
       
   667     TInt ictTestPermission( 0 );
       
   668     CRepository* repository( NULL );
       
   669     
       
   670     TRAPD( err, repository = CRepository::NewL( 
       
   671                                         KCRUidInternetConnectivitySettings ) );
       
   672     if ( err == KErrNone )
       
   673         {
       
   674         repository->Get( KIctsTestPermission, ictTestPermission );
       
   675         delete repository;
       
   676         LOG_WRITEF( "ICT is set to %d", ictTestPermission );
       
   677         }
       
   678     return ictTestPermission;
       
   679     }
       
   680 
       
   681 
       
   682 // ----------------------------------------------------------------------------
       
   683 // CWsfModel::TestAccessPointL
       
   684 // ----------------------------------------------------------------------------
       
   685 //
       
   686 EXPORT_C TInt CWsfModel::TestAccessPointL( TWsfWlanInfo& aWlan,
       
   687                                            TBool aKeepConnection, 
       
   688                                            TBool aConnectOnly )
       
   689     {
       
   690     LOG_ENTERFN( "CWsfModel::TestAccessPointL" );    
       
   691     TInt err( KErrNone );
       
   692     iKeepConnection = aKeepConnection;
       
   693     iConnectOnly = aConnectOnly;
       
   694     if ( !aWlan.iIapId )
       
   695         {
       
   696         // the wlaninfo must already contain a valid IAP id
       
   697         LOG_WRITE( "invalid IAP id" );
       
   698         return KErrCorrupt;
       
   699         }
       
   700     
       
   701     // the wlaninfo must be persistent to avoid nullpointer crashes due to
       
   702     // background refreshing 
       
   703     iIctWlanInfo = aWlan;
       
   704 
       
   705     // create connection and test connectivity if needed
       
   706 
       
   707     // check ICT settings
       
   708     TInt ictTestPermission( IctsTestPermission() );
       
   709     
       
   710     
       
   711     if ( aKeepConnection || ictTestPermission != EIctsNeverRun )
       
   712         {
       
   713         // make connection if Connect was selected or if ICT needs it
       
   714         LOG_WRITE( "creating connection..." );
       
   715         if ( iObserver )
       
   716             {
       
   717             iObserver->ConnectingL( iIctWlanInfo.iIapId );
       
   718             }
       
   719 
       
   720         // create the connection with temporary IAP by default
       
   721         err = iSession.ConnectWlanBearerL( iIctWlanInfo.iIapId, 
       
   722                                            EIapExpireOnDisconnect );
       
   723         
       
   724         if ( err == KErrNone )
       
   725             {
       
   726             LOG_WRITE( "connection OK." )
       
   727             }
       
   728         else
       
   729             {
       
   730             LOG_WRITEF( "connection creation failed with error = %d", err );
       
   731             // either the connection creation failed or was aborted, 
       
   732             // the server already cleaned up the mess, so nothing to do
       
   733             }
       
   734             
       
   735         if ( iObserver )
       
   736             {
       
   737             iObserver->ConnectingFinishedL( err );
       
   738             }
       
   739             
       
   740         }
       
   741 
       
   742     if ( err == KErrNone && ictTestPermission != EIctsNeverRun )
       
   743         {
       
   744         // do the connectivity test
       
   745         iConnectedIapId = iIctWlanInfo.iIapId;
       
   746         
       
   747         RCmManagerExt cmManager;
       
   748         cmManager.OpenL();
       
   749         CleanupClosePushL( cmManager );        
       
   750 
       
   751         RCmConnectionMethodExt cm = cmManager.ConnectionMethodL( 
       
   752                                                             iConnectedIapId );
       
   753         CleanupClosePushL( cm );
       
   754         
       
   755         iConnectedNetId = cm.GetIntAttributeL( CMManager::ECmNetworkId ); 
       
   756 
       
   757         CleanupStack::PopAndDestroy( &cm );
       
   758         CleanupStack::PopAndDestroy( &cmManager );
       
   759 
       
   760         LOG_WRITE( "starting ICT test..." );
       
   761         CIctsClientInterface* ict = CIctsClientInterface::NewL( 
       
   762                                                     iConnectedIapId, 
       
   763                                                     iConnectedNetId,
       
   764                                                     *this );
       
   765         LOG_WRITE( "ICT created" );
       
   766         CleanupStack::PushL( ict );
       
   767         ict->StartL();
       
   768         LOG_WRITE( "ICT: started" );
       
   769         
       
   770         // enter a waitloop since ICT is a kind of asynchronous service
       
   771         if ( !iIctEnded )
       
   772             {
       
   773             LOG_WRITE( "ICT: iIctWait started" );
       
   774             iIctWait.Start();
       
   775             }
       
   776             
       
   777         iIctEnded = EFalse;
       
   778         CleanupStack::PopAndDestroy( ict );
       
   779         LOG_WRITE( "ICT test done." );
       
   780         }
       
   781     
       
   782         
       
   783     if ( ( err == KErrNone && !aKeepConnection && 
       
   784                                      ictTestPermission != EIctsNeverRun ) ||
       
   785          ( err != KErrNone && err != KErrCancel ) )
       
   786         {
       
   787         // drop the connection in case of Start web browsing, and if an error
       
   788         // different from KErrCancel occured (on cancel the connection is
       
   789         // closed automatically)
       
   790         LOG_WRITE( "disconnecting..." );
       
   791         iSession.DisconnectWlanBearerL();
       
   792         LOG_WRITE( "Disconnected." );
       
   793         }
       
   794         
       
   795     
       
   796         
       
   797     if ( err == KErrNone && ictTestPermission == EIctsNeverRun )
       
   798         {
       
   799         LOG_WRITE( "ICT is set to never run, IAP remains temporary" );
       
   800         
       
   801         if ( !iKeepConnection )
       
   802             {
       
   803             //get the engine monitor the IAP
       
   804             iSession.MonitorAccessPointL( iIctWlanInfo.iIapId );
       
   805             iSession.SetIapPersistenceL( EIapExpireOnShutdown );
       
   806             iSession.MonitorAccessPointL( iIctWlanInfo.iIapId );
       
   807             }
       
   808 
       
   809         ConnectivityObserver( EConnectionNotOk, KNullDesC );
       
   810         }
       
   811 
       
   812     return err;
       
   813     }
       
   814 
       
   815 
       
   816 // ----------------------------------------------------------------------------
       
   817 // CWsfModel::TestConnectedAccessPointL
       
   818 // ----------------------------------------------------------------------------
       
   819 //
       
   820 EXPORT_C TInt CWsfModel::TestConnectedAccessPointL( TWsfWlanInfo& aWlan,
       
   821                                                     TBool aConnectOnly )
       
   822     {
       
   823     LOG_ENTERFN( "CWsfModel::TestConnectedAccessPointL" );    
       
   824     TInt err( KErrNone );
       
   825     iConnectOnly = aConnectOnly;
       
   826     if ( !aWlan.iIapId )
       
   827         {
       
   828         // the wlaninfo must already contain a valid IAP id
       
   829         LOG_WRITE( "invalid IAP id" );
       
   830         return KErrCorrupt;
       
   831         }
       
   832     
       
   833     // the wlaninfo must be persistent to avoid nullpointer crashes due to
       
   834     // background refreshing 
       
   835     iIctWlanInfo = aWlan;
       
   836 
       
   837     // check ICT settings
       
   838     TInt ictTestPermission( IctsTestPermission() );
       
   839 
       
   840     if ( ictTestPermission != EIctsNeverRun )
       
   841         {
       
   842         // do the connectivity test
       
   843         iConnectedIapId = iIctWlanInfo.iIapId;
       
   844         
       
   845         RCmManagerExt cmManager;
       
   846         cmManager.OpenL();
       
   847         CleanupClosePushL( cmManager );        
       
   848 
       
   849         RCmConnectionMethodExt cm = cmManager.ConnectionMethodL( 
       
   850                                                             iConnectedIapId );
       
   851         CleanupClosePushL( cm );
       
   852         
       
   853         iConnectedNetId = cm.GetIntAttributeL( CMManager::ECmNetworkId ); 
       
   854 
       
   855         CleanupStack::PopAndDestroy( &cm );
       
   856         CleanupStack::PopAndDestroy( &cmManager );
       
   857 
       
   858         LOG_WRITE( "starting ICT test..." );
       
   859         CIctsClientInterface* ict = CIctsClientInterface::NewL( 
       
   860                                                     iConnectedIapId, 
       
   861                                                     iConnectedNetId,
       
   862                                                     *this );
       
   863         LOG_WRITE( "ICT created" );
       
   864         CleanupStack::PushL( ict );
       
   865         ict->StartL();
       
   866         LOG_WRITE( "ICT: started" );
       
   867         
       
   868         // enter a waitloop since ICT is a kind of asynchronous service
       
   869         if ( !iIctEnded )
       
   870             {
       
   871             LOG_WRITE( "ICT: iIctWait started" );
       
   872             iIctWait.Start();
       
   873             }
       
   874             
       
   875         iIctEnded = EFalse;
       
   876         CleanupStack::PopAndDestroy( ict );
       
   877         LOG_WRITE( "ICT test done." );
       
   878         }
       
   879 
       
   880     if ( ictTestPermission == EIctsNeverRun )
       
   881         {
       
   882         LOG_WRITE( "ICT is set to never run, IAP remains temporary" );
       
   883         ConnectivityObserver( EConnectionNotOk, KNullDesC );
       
   884         }
       
   885 
       
   886     return err;
       
   887     }
       
   888 
       
   889 
       
   890 // ----------------------------------------------------------------------------
       
   891 // CWsfModel::RefreshScanL
       
   892 // ----------------------------------------------------------------------------
       
   893 //
       
   894 EXPORT_C TBool CWsfModel::RefreshScanL()
       
   895     {
       
   896     LOG_ENTERFN( "CWsfModel::RefreshScanL" );
       
   897     iRefreshing = iSession.RequestScanL();
       
   898     LOG_WRITEF( "iRefreshing = %d", iRefreshing );
       
   899     return iRefreshing;
       
   900     }
       
   901     
       
   902 
       
   903 // ----------------------------------------------------------------------------
       
   904 // CWsfModel::LaunchHelperApplicationL
       
   905 // ----------------------------------------------------------------------------
       
   906 //
       
   907 EXPORT_C void CWsfModel::LaunchHelperApplicationL( TWsfWlanInfo &aWlanInfo,
       
   908                                                    TBool aConnecting, 
       
   909                                                    TBool aConnectOnly )
       
   910     {
       
   911     LOG_ENTERFN( "CWsfModel::LaunchHelperApplicationL" );
       
   912     TPckgC<TWsfWlanInfo> param( aWlanInfo );
       
   913     TPckgC<TBool> param2( aConnecting );
       
   914     TPckgC<TBool> param3( aConnectOnly );
       
   915     
       
   916     TBuf8<sizeof( TWsfWlanInfo ) + sizeof( TBool ) + sizeof( TBool )> temp;
       
   917     temp.Copy( param );
       
   918     temp.Append( param2 );
       
   919     temp.Append( param3 );
       
   920     
       
   921     TFileName fileName;
       
   922     fileName.Copy( temp );
       
   923     
       
   924     RApaLsSession appArcSession;
       
   925     
       
   926     User::LeaveIfError( appArcSession.Connect() ); // connect to AppArc server
       
   927     CleanupClosePushL( appArcSession );
       
   928 
       
   929     // check if the app is already running ... and kill it.
       
   930     TUid id( TUid::Uid( KHelperApUid.iUid ) );
       
   931     TApaTaskList taskList( CEikonEnv::Static()->WsSession() );
       
   932     TApaTask task = taskList.FindApp( id );
       
   933 
       
   934     if ( task.Exists() )
       
   935         {
       
   936         task.EndTask();
       
   937         }    
       
   938 
       
   939     TThreadId threadId;
       
   940     User::LeaveIfError( appArcSession.StartDocument( fileName, TUid::Uid( 
       
   941                                              KHelperApUid.iUid ), threadId ) );
       
   942 
       
   943     CleanupStack::PopAndDestroy( &appArcSession );
       
   944     }
       
   945 
       
   946 
       
   947 // ----------------------------------------------------------------------------
       
   948 // CWsfModel::CancelNotifyEvents
       
   949 // ----------------------------------------------------------------------------
       
   950 //
       
   951 EXPORT_C void CWsfModel::CancelNotifyEvents()
       
   952     {
       
   953     LOG_ENTERFN( "CWsfModel::CancelNotifyEvents" );
       
   954     iSession.CancelNotifyEvent();
       
   955     }
       
   956 
       
   957 
       
   958 // ----------------------------------------------------------------------------
       
   959 // CWsfModel::RequestNotifyEventsL
       
   960 // ----------------------------------------------------------------------------
       
   961 //
       
   962 EXPORT_C void CWsfModel::RequestNotifyEventsL( 
       
   963                                            MWsfStateChangeObserver& aObserver )
       
   964     {
       
   965     LOG_ENTERFN( "CWsfModel::RequestNotifyEventsL" );
       
   966     iSession.NotifyEventL( aObserver );
       
   967     }
       
   968 
       
   969 
       
   970 // ------------------------- From MWsfBrowserLaunchObserver -------------------
       
   971     
       
   972 // ----------------------------------------------------------------------------
       
   973 // CWsfModel::BrowserLaunchFailed
       
   974 // ----------------------------------------------------------------------------
       
   975 //
       
   976 void CWsfModel::BrowserLaunchFailed( TInt aError )
       
   977     {
       
   978     LOG_ENTERFN( "CWsfModel::BrowserLaunchFailed" );
       
   979     LOG_WRITEF( "error = %d", aError );
       
   980     
       
   981     // do the cleanup if necessary
       
   982     TRAP_IGNORE( 
       
   983         iSession.SetIapPersistenceL( EIapForcedExpiry );
       
   984         iSession.ControlDisconnectTimerL( EAdcStartTimer | EAdcTimerReset );
       
   985     );
       
   986     
       
   987     if ( iObserver )
       
   988         {
       
   989         iObserver->BrowserLaunchFailed( aError );    
       
   990         }
       
   991     }
       
   992 
       
   993 
       
   994 // ----------------------------------------------------------------------------
       
   995 // CWsfModel::BrowserLaunchCompleteL
       
   996 // ----------------------------------------------------------------------------
       
   997 //
       
   998 void CWsfModel::BrowserLaunchCompleteL()
       
   999     {
       
  1000     LOG_ENTERFN( "CWsfModel::BrowserLaunchCompleteL" );
       
  1001 
       
  1002     if ( iObserver )
       
  1003         {
       
  1004         iObserver->BrowserLaunchCompleteL();    
       
  1005         }
       
  1006     }
       
  1007 
       
  1008 
       
  1009 // ----------------------------------------------------------------------------
       
  1010 // CWsfModel::BrowserExitL
       
  1011 // ----------------------------------------------------------------------------
       
  1012 //
       
  1013 void CWsfModel::BrowserExitL()
       
  1014     {
       
  1015     LOG_ENTERFN( "CWsfModel::BrowserExitL" );
       
  1016     
       
  1017     // browser has been terminated, do the cleanup if necessary
       
  1018     iSession.SetIapPersistenceL( EIapForcedExpiry );
       
  1019     iSession.ControlDisconnectTimerL( EAdcStartTimer | EAdcTimerReset );
       
  1020     
       
  1021     if ( iObserver )
       
  1022         {
       
  1023         iObserver->BrowserExitL();    
       
  1024         }
       
  1025     }
       
  1026     
       
  1027 // ------------------------- From MWsfScreenSaverStateObserver ----------------
       
  1028  
       
  1029 // ----------------------------------------------------------------------------
       
  1030 // CWsfModel::ScreenSaverStatusChangedL
       
  1031 // ----------------------------------------------------------------------------
       
  1032 //
       
  1033 void CWsfModel::ScreenSaverStatusChangedL( const TBool aScreenSaverActive )
       
  1034     {
       
  1035     LOG_ENTERFN( "CWsfModel::ScreenSaverStatusChangedL" );
       
  1036     LOG_WRITEF( "status = %d", aScreenSaverActive );
       
  1037     if ( aScreenSaverActive )
       
  1038         {
       
  1039         iSession.DisableScanL();
       
  1040         }
       
  1041     else 
       
  1042         {
       
  1043         iSession.EnableScanL();
       
  1044         }
       
  1045     }
       
  1046     
       
  1047 
       
  1048 // ----------------------------------------------------------------------------
       
  1049 // CWsfModel::IsConnectedL
       
  1050 // ----------------------------------------------------------------------------
       
  1051 //
       
  1052 EXPORT_C TBool CWsfModel::IsConnectedL()
       
  1053     {
       
  1054     return iSession.IsConnectedL();
       
  1055     }
       
  1056     
       
  1057 
       
  1058 // ----------------------------------------------------------------------------
       
  1059 // CWsfModel::GetConnectedWlanDetailsL
       
  1060 // ----------------------------------------------------------------------------
       
  1061 //
       
  1062 EXPORT_C TBool CWsfModel::GetConnectedWlanDetailsL( TWsfWlanInfo& aWlanInfo )
       
  1063     {
       
  1064     return iSession.GetConnectedWlanDetailsL( aWlanInfo );
       
  1065     }
       
  1066 
       
  1067 
       
  1068 // ----------------------------------------------------------------------------
       
  1069 // CWsfModel::AbortConnectingL
       
  1070 // ----------------------------------------------------------------------------
       
  1071 //
       
  1072 EXPORT_C void CWsfModel::AbortConnectingL()
       
  1073     {
       
  1074     LOG_ENTERFN( "CWsfModel::AbortConnectingL" );
       
  1075     if ( iConnecting )
       
  1076         {
       
  1077         iSession.AbortConnectingL();
       
  1078         }
       
  1079     }
       
  1080 
       
  1081 
       
  1082 // ----------------------------------------------------------------------------
       
  1083 // CWsfModel::AbortScanningL
       
  1084 // ----------------------------------------------------------------------------
       
  1085 //
       
  1086 EXPORT_C void CWsfModel::AbortScanningL()
       
  1087     {
       
  1088     LOG_ENTERFN( "CWsfModel::AbortScanningL" );
       
  1089     if ( iRefreshing )
       
  1090         {
       
  1091         iSession.AbortScanningL();
       
  1092         }
       
  1093     }
       
  1094 
       
  1095 
       
  1096 // ----------------------------------------------------------------------------
       
  1097 // CWsfModel::CheckSpaceBelowCriticalLevelL
       
  1098 // ----------------------------------------------------------------------------
       
  1099 //
       
  1100 void CWsfModel::CheckSpaceBelowCriticalLevelL() const
       
  1101     {
       
  1102     // OOD handling. If disk space is low user is notified.
       
  1103     RFs fs;
       
  1104     User::LeaveIfError( fs.Connect() );
       
  1105     CleanupClosePushL<RFs>( fs );
       
  1106     
       
  1107     // Checks the FFS space "after" addition
       
  1108     TBool belowCL = SysUtil::FFSSpaceBelowCriticalLevelL
       
  1109             ( &fs, KEstimatedOverhead );
       
  1110     
       
  1111     CleanupStack::PopAndDestroy(); // fs
       
  1112     
       
  1113     if( belowCL )
       
  1114         {
       
  1115         User::Leave( KErrDiskFull );
       
  1116         }
       
  1117     }
       
  1118 
       
  1119 // ----------------------------------------------------------------------------
       
  1120 // CWsfModel::CheckUnknownWapiL
       
  1121 // ----------------------------------------------------------------------------
       
  1122 //
       
  1123 void CWsfModel::CheckUnknownWapiL( TWsfWlanInfo& aWlan ) const
       
  1124     {    
       
  1125     if( !aWlan.Known() && aWlan.SecurityMode() == EWlanSecModeWAPI )
       
  1126         {
       
  1127         User::Leave( KErrWlanProtectedSetupSetupLocked );
       
  1128         }
       
  1129     }
       
  1130 
       
  1131 // ----------------------------------------------------------------------------
       
  1132 // CWsfModel::CheckIsIapIdValidL
       
  1133 // ----------------------------------------------------------------------------
       
  1134 //
       
  1135 EXPORT_C void CWsfModel::CheckIsIapIdValidL( TUint aIapId ) const
       
  1136     {    
       
  1137     LOG_ENTERFN( "CWsfModel::CheckIsIapIdValidL" );
       
  1138     LOG_WRITEF( "Checking iapId= %d", aIapId );
       
  1139     if( aIapId )
       
  1140         {
       
  1141         RCmManagerExt cmManager;
       
  1142         cmManager.OpenL();
       
  1143         CleanupClosePushL( cmManager );        
       
  1144 
       
  1145         RCmConnectionMethodExt cm = cmManager.ConnectionMethodL( aIapId );
       
  1146         cm.Close();
       
  1147         CleanupStack::PopAndDestroy( &cmManager );
       
  1148         }
       
  1149     else
       
  1150         {
       
  1151         User::Leave( KErrArgument );
       
  1152         }
       
  1153     }
       
  1154     
       
  1155 // End of file