wlanutilities/wlansniffer/model/src/wsfmodel.cpp
changeset 0 56b72877c1cb
child 2 6e4b6261703d
equal deleted inserted replaced
-1:000000000000 0:56b72877c1cb
       
     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     return iRefreshing;
       
   899     }
       
   900     
       
   901 
       
   902 // ----------------------------------------------------------------------------
       
   903 // CWsfModel::LaunchHelperApplicationL
       
   904 // ----------------------------------------------------------------------------
       
   905 //
       
   906 EXPORT_C void CWsfModel::LaunchHelperApplicationL( TWsfWlanInfo &aWlanInfo,
       
   907                                                    TBool aConnecting, 
       
   908                                                    TBool aConnectOnly )
       
   909     {
       
   910     LOG_ENTERFN( "CWsfModel::LaunchHelperApplicationL" );
       
   911     TPckgC<TWsfWlanInfo> param( aWlanInfo );
       
   912     TPckgC<TBool> param2( aConnecting );
       
   913     TPckgC<TBool> param3( aConnectOnly );
       
   914     
       
   915     TBuf8<sizeof( TWsfWlanInfo ) + sizeof( TBool ) + sizeof( TBool )> temp;
       
   916     temp.Copy( param );
       
   917     temp.Append( param2 );
       
   918     temp.Append( param3 );
       
   919     
       
   920     TFileName fileName;
       
   921     fileName.Copy( temp );
       
   922     
       
   923     RApaLsSession appArcSession;
       
   924     
       
   925     User::LeaveIfError( appArcSession.Connect() ); // connect to AppArc server
       
   926     CleanupClosePushL( appArcSession );
       
   927 
       
   928     // check if the app is already running ... and kill it.
       
   929     TUid id( TUid::Uid( KHelperApUid.iUid ) );
       
   930     TApaTaskList taskList( CEikonEnv::Static()->WsSession() );
       
   931     TApaTask task = taskList.FindApp( id );
       
   932 
       
   933     if ( task.Exists() )
       
   934         {
       
   935         task.EndTask();
       
   936         }    
       
   937 
       
   938     TThreadId threadId;
       
   939     User::LeaveIfError( appArcSession.StartDocument( fileName, TUid::Uid( 
       
   940                                              KHelperApUid.iUid ), threadId ) );
       
   941 
       
   942     CleanupStack::PopAndDestroy( &appArcSession );
       
   943     }
       
   944 
       
   945 
       
   946 // ----------------------------------------------------------------------------
       
   947 // CWsfModel::CancelNotifyEvents
       
   948 // ----------------------------------------------------------------------------
       
   949 //
       
   950 EXPORT_C void CWsfModel::CancelNotifyEvents()
       
   951     {
       
   952     LOG_ENTERFN( "CWsfModel::CancelNotifyEvents" );
       
   953     iSession.CancelNotifyEvent();
       
   954     }
       
   955 
       
   956 
       
   957 // ----------------------------------------------------------------------------
       
   958 // CWsfModel::RequestNotifyEventsL
       
   959 // ----------------------------------------------------------------------------
       
   960 //
       
   961 EXPORT_C void CWsfModel::RequestNotifyEventsL( 
       
   962                                            MWsfStateChangeObserver& aObserver )
       
   963     {
       
   964     LOG_ENTERFN( "CWsfModel::RequestNotifyEventsL" );
       
   965     iSession.NotifyEventL( aObserver );
       
   966     }
       
   967 
       
   968 
       
   969 // ------------------------- From MWsfBrowserLaunchObserver -------------------
       
   970     
       
   971 // ----------------------------------------------------------------------------
       
   972 // CWsfModel::BrowserLaunchFailed
       
   973 // ----------------------------------------------------------------------------
       
   974 //
       
   975 void CWsfModel::BrowserLaunchFailed( TInt aError )
       
   976     {
       
   977     LOG_ENTERFN( "CWsfModel::BrowserLaunchFailed" );
       
   978     LOG_WRITEF( "error = %d", aError );
       
   979     
       
   980     // do the cleanup if necessary
       
   981     TRAP_IGNORE( 
       
   982         iSession.SetIapPersistenceL( EIapForcedExpiry );
       
   983         iSession.ControlDisconnectTimerL( EAdcStartTimer | EAdcTimerReset );
       
   984     );
       
   985     
       
   986     if ( iObserver )
       
   987         {
       
   988         iObserver->BrowserLaunchFailed( aError );    
       
   989         }
       
   990     }
       
   991 
       
   992 
       
   993 // ----------------------------------------------------------------------------
       
   994 // CWsfModel::BrowserLaunchCompleteL
       
   995 // ----------------------------------------------------------------------------
       
   996 //
       
   997 void CWsfModel::BrowserLaunchCompleteL()
       
   998     {
       
   999     LOG_ENTERFN( "CWsfModel::BrowserLaunchCompleteL" );
       
  1000 
       
  1001     if ( iObserver )
       
  1002         {
       
  1003         iObserver->BrowserLaunchCompleteL();    
       
  1004         }
       
  1005     }
       
  1006 
       
  1007 
       
  1008 // ----------------------------------------------------------------------------
       
  1009 // CWsfModel::BrowserExitL
       
  1010 // ----------------------------------------------------------------------------
       
  1011 //
       
  1012 void CWsfModel::BrowserExitL()
       
  1013     {
       
  1014     LOG_ENTERFN( "CWsfModel::BrowserExitL" );
       
  1015     
       
  1016     // browser has been terminated, do the cleanup if necessary
       
  1017     iSession.SetIapPersistenceL( EIapForcedExpiry );
       
  1018     iSession.ControlDisconnectTimerL( EAdcStartTimer | EAdcTimerReset );
       
  1019     
       
  1020     if ( iObserver )
       
  1021         {
       
  1022         iObserver->BrowserExitL();    
       
  1023         }
       
  1024     }
       
  1025     
       
  1026 // ------------------------- From MWsfScreenSaverStateObserver ----------------
       
  1027  
       
  1028 // ----------------------------------------------------------------------------
       
  1029 // CWsfModel::ScreenSaverStatusChangedL
       
  1030 // ----------------------------------------------------------------------------
       
  1031 //
       
  1032 void CWsfModel::ScreenSaverStatusChangedL( const TBool aScreenSaverActive )
       
  1033     {
       
  1034     LOG_ENTERFN( "CWsfModel::ScreenSaverStatusChangedL" );
       
  1035     LOG_WRITEF( "status = %d", aScreenSaverActive );
       
  1036     if ( aScreenSaverActive )
       
  1037         {
       
  1038         iSession.DisableScanL();
       
  1039         }
       
  1040     else 
       
  1041         {
       
  1042         iSession.EnableScanL();
       
  1043         }
       
  1044     }
       
  1045     
       
  1046 
       
  1047 // ----------------------------------------------------------------------------
       
  1048 // CWsfModel::IsConnectedL
       
  1049 // ----------------------------------------------------------------------------
       
  1050 //
       
  1051 EXPORT_C TBool CWsfModel::IsConnectedL()
       
  1052     {
       
  1053     return iSession.IsConnectedL();
       
  1054     }
       
  1055     
       
  1056 
       
  1057 // ----------------------------------------------------------------------------
       
  1058 // CWsfModel::GetConnectedWlanDetailsL
       
  1059 // ----------------------------------------------------------------------------
       
  1060 //
       
  1061 EXPORT_C TBool CWsfModel::GetConnectedWlanDetailsL( TWsfWlanInfo& aWlanInfo )
       
  1062     {
       
  1063     return iSession.GetConnectedWlanDetailsL( aWlanInfo );
       
  1064     }
       
  1065 
       
  1066 
       
  1067 // ----------------------------------------------------------------------------
       
  1068 // CWsfModel::AbortConnectingL
       
  1069 // ----------------------------------------------------------------------------
       
  1070 //
       
  1071 EXPORT_C void CWsfModel::AbortConnectingL()
       
  1072     {
       
  1073     LOG_ENTERFN( "CWsfModel::AbortConnectingL" );
       
  1074     if ( iConnecting )
       
  1075         {
       
  1076         iSession.AbortConnectingL();
       
  1077         }
       
  1078     }
       
  1079 
       
  1080 
       
  1081 // ----------------------------------------------------------------------------
       
  1082 // CWsfModel::AbortScanningL
       
  1083 // ----------------------------------------------------------------------------
       
  1084 //
       
  1085 EXPORT_C void CWsfModel::AbortScanningL()
       
  1086     {
       
  1087     LOG_ENTERFN( "CWsfModel::AbortScanningL" );
       
  1088     if ( iRefreshing )
       
  1089         {
       
  1090         iSession.AbortScanningL();
       
  1091         }
       
  1092     }
       
  1093 
       
  1094 
       
  1095 // ----------------------------------------------------------------------------
       
  1096 // CWsfModel::CheckSpaceBelowCriticalLevelL
       
  1097 // ----------------------------------------------------------------------------
       
  1098 //
       
  1099 void CWsfModel::CheckSpaceBelowCriticalLevelL() const
       
  1100     {
       
  1101     // OOD handling. If disk space is low user is notified.
       
  1102     RFs fs;
       
  1103     User::LeaveIfError( fs.Connect() );
       
  1104     CleanupClosePushL<RFs>( fs );
       
  1105     
       
  1106     // Checks the FFS space "after" addition
       
  1107     TBool belowCL = SysUtil::FFSSpaceBelowCriticalLevelL
       
  1108             ( &fs, KEstimatedOverhead );
       
  1109     
       
  1110     CleanupStack::PopAndDestroy(); // fs
       
  1111     
       
  1112     if( belowCL )
       
  1113         {
       
  1114         User::Leave( KErrDiskFull );
       
  1115         }
       
  1116     }
       
  1117 
       
  1118 // ----------------------------------------------------------------------------
       
  1119 // CWsfModel::CheckUnknownWapiL
       
  1120 // ----------------------------------------------------------------------------
       
  1121 //
       
  1122 void CWsfModel::CheckUnknownWapiL( TWsfWlanInfo& aWlan ) const
       
  1123     {    
       
  1124     if( !aWlan.Known() && aWlan.SecurityMode() == EWlanSecModeWAPI )
       
  1125         {
       
  1126         User::Leave( KErrWlanProtectedSetupSetupLocked );
       
  1127         }
       
  1128     }
       
  1129     
       
  1130 // End of file