hotspotfw/hsserver/src/hssiapsettingshandler.cpp
changeset 0 56b72877c1cb
child 3 ff3b37722600
equal deleted inserted replaced
-1:000000000000 0:56b72877c1cb
       
     1 /*
       
     2 * Copyright (c) 2002-2006 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 iapsettings
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "hssiapsettingshandler.h"
       
    22 #include "hotspotclientserver.h"
       
    23 #include "am_debug.h"
       
    24 #include <es_enum.h>
       
    25 
       
    26 #include <cmconnectionmethodext.h>
       
    27 #include <cmconnectionmethoddef.h>
       
    28 #include <cmdestinationext.h>
       
    29 #include <cmmanagerext.h>
       
    30 
       
    31 using namespace CMManager;
       
    32 
       
    33 // ============================ MEMBER FUNCTIONS ===============================
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // CHssIapSettingsHandler
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 CHssIapSettingsHandler::CHssIapSettingsHandler( )
       
    40 	{
       
    41     DEBUG("CHssIapSettingsHandler::CHssIapSettingsHandler");
       
    42 	}
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // ConstructL
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 void CHssIapSettingsHandler::ConstructL()
       
    49     {
       
    50     DEBUG("CHssIapSettingsHandler::ConstructL()");
       
    51     }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // NewL
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 CHssIapSettingsHandler* CHssIapSettingsHandler::NewL()
       
    58     {
       
    59     CHssIapSettingsHandler* self = new( ELeave ) CHssIapSettingsHandler();
       
    60     CleanupStack::PushL( self );
       
    61     self->ConstructL();
       
    62     CleanupStack::Pop( self );
       
    63     return self;
       
    64     }
       
    65     
       
    66 // -----------------------------------------------------------------------------
       
    67 // ~CHssIapSettingsHandler
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 CHssIapSettingsHandler::~CHssIapSettingsHandler()
       
    71 	{
       
    72 	DEBUG("CHssIapSettingsHandler::~CHssIapSettingsHandler()");
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CreateClientIapL
       
    77 // -----------------------------------------------------------------------------
       
    78 //    
       
    79 void CHssIapSettingsHandler::CreateClientIapL( const TDesC& aIapName, 
       
    80                                          TUint32& aIapId, 
       
    81                                          const TUid aUid )
       
    82     {
       
    83     DEBUG("CHssIapSettingsHandler::CreateClientIapL");
       
    84 
       
    85     RCmManagerExt cmManager;
       
    86     cmManager.OpenL();
       
    87     CleanupClosePushL( cmManager );
       
    88     
       
    89     // Read all destination(SNAP) settings into an array
       
    90     RArray<TUint32> destinations;
       
    91     CleanupClosePushL( destinations );
       
    92     
       
    93     cmManager.AllDestinationsL( destinations );
       
    94     RCmDestinationExt destination;
       
    95     // Loop through each destination
       
    96     for( TInt i = 0; i < destinations.Count(); i++ )
       
    97         {
       
    98         destination = cmManager.DestinationL(destinations[i]);
       
    99         CleanupClosePushL(destination); 
       
   100         // Internet destination will always exist in the system.
       
   101         // Internet destination will have ESnapPurposeInternet set in its metadata.
       
   102         if (destination.MetadataL(CMManager::ESnapMetadataPurpose) == CMManager::ESnapPurposeInternet)
       
   103             {
       
   104             RCmConnectionMethodExt plugin = cmManager.CreateConnectionMethodL( KUidWlanBearerType );
       
   105             CleanupClosePushL( plugin );
       
   106                 
       
   107             plugin.SetStringAttributeL( ECmName, aIapName );
       
   108             plugin.SetStringAttributeL( EWlanSSID, aIapName );
       
   109             plugin.SetStringAttributeL( EWlanServiceExtensionTableName, aUid.Name() );
       
   110             plugin.SetStringAttributeL( ECmConfigDaemonManagerName, KHotSpotPlugin );
       
   111             plugin.SetBoolAttributeL( ECmProtected, ETrue );
       
   112             
       
   113             destination.AddConnectionMethodL( plugin );
       
   114             destination.UpdateL();
       
   115                 
       
   116             aIapId = plugin.GetIntAttributeL( ECmIapId  );
       
   117             CleanupStack::PopAndDestroy( &plugin );
       
   118             }
       
   119         CleanupStack::PopAndDestroy(); // destination
       
   120         }
       
   121     CleanupStack::PopAndDestroy(); // destinations
       
   122     CleanupStack::PopAndDestroy( &cmManager );
       
   123 
       
   124     DEBUG("CHssIapSettingsHandler::CreateClientIapL Done");
       
   125     }
       
   126 
       
   127 // -----------------------------------------------------------------------------
       
   128 // CreateIapL
       
   129 // -----------------------------------------------------------------------------
       
   130 //    
       
   131 void CHssIapSettingsHandler::CreateIapL()
       
   132     {
       
   133     DEBUG("CHssIapSettingsHandler::CreateIapL");
       
   134 
       
   135     RCmManagerExt cmManager;
       
   136     cmManager.OpenL();
       
   137     CleanupClosePushL( cmManager );
       
   138     
       
   139     // Read all destination(SNAP) settings into an array
       
   140     RArray<TUint32> destinations;
       
   141     CleanupClosePushL( destinations );
       
   142     
       
   143     cmManager.AllDestinationsL( destinations );
       
   144     RCmDestinationExt destination;
       
   145     // Loop through each destination
       
   146     for( TInt i = 0; i < destinations.Count(); i++ )
       
   147         {
       
   148         destination = cmManager.DestinationL( destinations[i] );
       
   149         CleanupClosePushL( destination ); 
       
   150         // Internet destination will always exist in the system.
       
   151         // Internet destination will have ESnapPurposeInternet set in its metadata.
       
   152         if (destination.MetadataL( CMManager::ESnapMetadataPurpose ) == 
       
   153 		                                CMManager::ESnapPurposeInternet )
       
   154             {
       
   155             CWlanMgmtClient* wlanMgmtClient = CWlanMgmtClient::NewL();
       
   156             CleanupStack::PushL( wlanMgmtClient );
       
   157             
       
   158             TWlanConnectionMode connMode( EWlanConnectionModeNotConnected );
       
   159             wlanMgmtClient->GetConnectionMode( connMode );
       
   160 
       
   161             TWlanConnectionSecurityMode secMode( EWlanConnectionSecurityOpen );
       
   162             wlanMgmtClient->GetConnectionSecurityMode( secMode );
       
   163                         
       
   164             HBufC* ssid( NULL );
       
   165             TWlanSsid ssidConn;
       
   166             
       
   167             wlanMgmtClient->GetConnectionSsid( ssidConn );
       
   168             
       
   169             ssid = HBufC::NewLC( ssidConn.Length() );
       
   170             ssid->Des().Copy( ssidConn ); 
       
   171             
       
   172             TUint32 serviceId(0);
       
   173             TUint32 easyWlanIapId(0);
       
   174             easyWlanIapId = cmManager.EasyWlanIdL();
       
   175             DEBUG1("CHssIapSettingsHandler::SaveDestinationL easyWlanIapId: %d", 
       
   176                     easyWlanIapId);            
       
   177             
       
   178             RCmConnectionMethodExt easyWlanPlugin;
       
   179             easyWlanPlugin = cmManager.ConnectionMethodL( easyWlanIapId );
       
   180             CleanupClosePushL( easyWlanPlugin );
       
   181             TBool scanSsid = easyWlanPlugin.GetBoolAttributeL( EWlanScanSSID );
       
   182             DEBUG1("CHssIapSettingsHandler::SaveDestinationL scanSsid: %d", scanSsid);
       
   183             TUint32 easyWlanServiceId = easyWlanPlugin.GetIntAttributeL( EWlanServiceId );
       
   184             DEBUG1("CHssIapSettingsHandler::SaveDestinationL easyWlanServiceId: %d", easyWlanServiceId);
       
   185             CleanupStack::PopAndDestroy(); // easyWlanPlugin;
       
   186             
       
   187             RCmConnectionMethodExt plugin =
       
   188                 cmManager.CreateConnectionMethodL( KUidWlanBearerType );
       
   189             CleanupClosePushL( plugin );        
       
   190             plugin.SetStringAttributeL( ECmName, *ssid );
       
   191             plugin.SetStringAttributeL( EWlanSSID, *ssid );
       
   192             plugin.SetIntAttributeL( CMManager::EWlanSecurityMode,
       
   193                     ConvertConnectionSecurityModeToSecModeL( secMode ));
       
   194             plugin.SetIntAttributeL( EWlanConnectionMode, 
       
   195                     ConvertConnectionModeToNetModeL( connMode ) );
       
   196             plugin.SetBoolAttributeL( EWlanScanSSID, scanSsid );
       
   197             destination.AddConnectionMethodL( plugin );
       
   198             destination.UpdateL();
       
   199             serviceId = plugin.GetIntAttributeL( EWlanServiceId );                
       
   200            
       
   201             CleanupStack::PopAndDestroy( &plugin ); // Close() called
       
   202             if ( secMode == EWlanConnectionSecurityWep )
       
   203                 {
       
   204                 SaveWEPKeyL( easyWlanServiceId, serviceId );
       
   205                 }
       
   206                     
       
   207             if ( secMode == EWlanConnectionSecurityWpaPsk )
       
   208                 {
       
   209                 SaveWPAKeyL( easyWlanServiceId, serviceId );
       
   210                 }
       
   211             CleanupStack::PopAndDestroy( ssid ); // ssid
       
   212             CleanupStack::PopAndDestroy( wlanMgmtClient );
       
   213             }
       
   214         CleanupStack::PopAndDestroy(); // destination
       
   215         }
       
   216     CleanupStack::PopAndDestroy(); // destinations
       
   217     CleanupStack::PopAndDestroy( &cmManager );
       
   218 
       
   219     DEBUG("CHssIapSettingsHandler::CreateIapL Done");
       
   220     }
       
   221 
       
   222 // -----------------------------------------------------------------------------
       
   223 // DeleteIapL
       
   224 // -----------------------------------------------------------------------------
       
   225 //
       
   226 void CHssIapSettingsHandler::DeleteIapL( const TUint aIapId )
       
   227     {
       
   228     DEBUG1("CHssIapSettingsHandler:::DeleteIapL aIapId=%d ", aIapId);
       
   229 
       
   230     RCmManagerExt cmManager;
       
   231     cmManager.OpenL();
       
   232     CleanupClosePushL( cmManager );
       
   233 
       
   234     RCmConnectionMethodExt plugin = cmManager.ConnectionMethodL( aIapId );
       
   235     CleanupClosePushL( plugin );
       
   236 
       
   237     cmManager.RemoveAllReferencesL( plugin );
       
   238     CleanupStack::Pop( &plugin );
       
   239 
       
   240     TBool result = plugin.DeleteL();
       
   241     DEBUG1("CHssIapSettingsHandler:::DeleteIapL result = %d ", result);
       
   242 
       
   243     CleanupStack::PopAndDestroy( &cmManager );
       
   244     }
       
   245    
       
   246 // -----------------------------------------------------------------------------
       
   247 // FindClientL
       
   248 // -----------------------------------------------------------------------------
       
   249 //
       
   250 TInt CHssIapSettingsHandler::FindClientL( const TUint aIapId, TUid& aUid, TDes8& aUidText )
       
   251     {   
       
   252     DEBUG("CHssIapSettingsHandler::FindClientL");
       
   253     TInt ret( KErrNone );
       
   254     aUid = TUid::Null();
       
   255     RCmManagerExt cmManager;
       
   256     cmManager.OpenL();
       
   257     CleanupClosePushL( cmManager );
       
   258     
       
   259 	RCmConnectionMethodExt plugin = cmManager.ConnectionMethodL( aIapId );
       
   260     CleanupClosePushL( plugin );
       
   261     
       
   262     HBufC* uid = plugin.GetStringAttributeL( EWlanServiceExtensionTableName );
       
   263     
       
   264     TBuf8<KExtensionAPILength> buffer;
       
   265     buffer.Copy( *uid );
       
   266     delete uid;
       
   267     // Find and remove [ and ]. 
       
   268     // If found [ it's known that buffer contains UID
       
   269     TInt indx = buffer.Find( KMark1 );
       
   270     	 if ( KErrNotFound != indx )
       
   271          {
       
   272          DEBUG("CHssIapSettingsHandler::FindClientL Client is found");
       
   273          buffer.Delete( indx, 1 );
       
   274          indx = buffer.Find( KMark2 );
       
   275          if ( KErrNotFound != indx )
       
   276              {
       
   277              buffer.Delete( indx, 1 );
       
   278              }
       
   279          // Convert TBuf to TUid
       
   280          TLex8 lex( buffer );
       
   281          TUint value( 0 );
       
   282          User::LeaveIfError( lex.Val( value, EHex ) );
       
   283          aUid.iUid = value;
       
   284          aUidText = buffer;
       
   285          }
       
   286     
       
   287     CleanupStack::PopAndDestroy( &plugin ); // Close() called on "plugin"
       
   288     CleanupStack::PopAndDestroy( &cmManager );
       
   289     DEBUG("CHssIapSettingsHandler::FindClientL");
       
   290 
       
   291     return ret;
       
   292     }
       
   293 
       
   294 // ---------------------------------------------------------
       
   295 // SaveWEPKeyL()
       
   296 // ---------------------------------------------------------
       
   297 //
       
   298 void CHssIapSettingsHandler::SaveWEPKeyL( const TUint32 aFrom, 
       
   299         const TUint32 aTo )
       
   300     {
       
   301     DEBUG("CHssIapSettingsHandler::SaveWEPKey");
       
   302     TInt err( KErrNone );
       
   303     TUint attempts( KHssMaxTransAttempts );
       
   304 
       
   305     CMDBSession* dbSession = 
       
   306     CMDBSession::NewLC( CMDBSession::LatestVersion() );
       
   307     
       
   308     // open a transaction to make sure key is saved
       
   309     do
       
   310         {
       
   311         TRAP( err, dbSession->OpenTransactionL() );
       
   312         if ( err )
       
   313             {
       
   314             DEBUG1("CHssIapSettingsHandler::SaveWEPKey err: %d", err);
       
   315             User::After( KHssRetryAfter );
       
   316             }
       
   317         }
       
   318     while ( err && attempts-- );
       
   319     
       
   320     if ( err != KErrNone )
       
   321         {
       
   322         dbSession->Close();
       
   323         CleanupStack::PopAndDestroy( dbSession );
       
   324         User::LeaveIfError( err );
       
   325         }
       
   326     
       
   327     CleanupStack::PushL( TCleanupItem( RollbackCommsDat, dbSession ) );
       
   328     
       
   329     CMDBGenericRecord* wlanRecord = static_cast<CMDBGenericRecord*>
       
   330             ( CCDRecordBase::RecordFactoryL( 0));
       
   331     CleanupStack::PushL( wlanRecord );
       
   332     wlanRecord->InitializeL( KGenericTable(),NULL );
       
   333     wlanRecord->SetRecordId( aFrom );
       
   334     wlanRecord->LoadL( *dbSession );
       
   335         
       
   336     TInt format = 
       
   337         *((CMDBField<TUint32>*)wlanRecord->GetFieldByIdL( KCDTIdWlanFormatKey1 ));
       
   338     CMDBField<TDesC8>* wepKey = 
       
   339             static_cast<CMDBField<TDesC8>*>(wlanRecord->GetFieldByIdL(KCDTIdWlanWepKey1));
       
   340         
       
   341     TBuf8<KWLMMaxWEPKeyLength> buf8;
       
   342     buf8.Copy( *wepKey );
       
   343     CleanupStack::PopAndDestroy( wlanRecord );
       
   344         
       
   345     CMDBGenericRecord* wlanRecordNew = static_cast<CMDBGenericRecord*>
       
   346         ( CCDRecordBase::RecordFactoryL( 0));
       
   347     CleanupStack::PushL( wlanRecordNew );
       
   348     wlanRecordNew->InitializeL( KGenericTable(),NULL );
       
   349     wlanRecordNew->SetRecordId( aTo );
       
   350     wlanRecordNew->LoadL( *dbSession );
       
   351         
       
   352     CMDBField<TDesC8>* wepKeyNew = 
       
   353         static_cast<CMDBField<TDesC8>*>(wlanRecordNew->GetFieldByIdL(KCDTIdWlanWepKey1));
       
   354     *((CMDBField<TUint32>*)wlanRecordNew->GetFieldByIdL( KCDTIdWlanFormatKey1 )) = format;
       
   355     
       
   356     wepKeyNew->SetL(buf8); 
       
   357     
       
   358     wlanRecordNew->ModifyL( *dbSession );        
       
   359     CleanupStack::PopAndDestroy( wlanRecordNew );
       
   360     
       
   361     dbSession->CommitTransactionL();
       
   362     
       
   363     dbSession->Close();
       
   364     CleanupStack::PopAndDestroy( dbSession );
       
   365     
       
   366     CleanupStack::Pop( 1 ); // transaction rollback popped
       
   367         
       
   368     dbSession->CommitTransactionL();
       
   369    
       
   370     
       
   371     DEBUG("CHssIapSettingsHandler::SaveWEPKey Done");
       
   372     }
       
   373 
       
   374 // ---------------------------------------------------------
       
   375 // SaveWPAKeyL()
       
   376 // ---------------------------------------------------------
       
   377 //
       
   378 void CHssIapSettingsHandler::SaveWPAKeyL( const TUint32 aFrom, 
       
   379         const TUint32 aTo )
       
   380     {
       
   381     DEBUG("CHssIapSettingsHandler::SaveWPAKey");
       
   382     TInt err( KErrNone );
       
   383     TUint attempts( KHssMaxTransAttempts );
       
   384     
       
   385     CMDBSession* dbSession = 
       
   386     CMDBSession::NewLC( CMDBSession::LatestVersion() );
       
   387     
       
   388     // open a transaction to make sure key is saved
       
   389     do
       
   390         {
       
   391         TRAP( err, dbSession->OpenTransactionL() );
       
   392         if ( err )
       
   393             {
       
   394             DEBUG1("CHssIapSettingsHandler::SaveWPAKey err: %d", err);
       
   395             User::After( KHssRetryAfter );
       
   396             }
       
   397         }
       
   398     while ( err && attempts-- );
       
   399     
       
   400     if ( err != KErrNone )
       
   401         {
       
   402         dbSession->Close();
       
   403         CleanupStack::PopAndDestroy( dbSession );
       
   404         User::LeaveIfError( err );
       
   405         }
       
   406     
       
   407     CleanupStack::PushL( TCleanupItem( RollbackCommsDat, dbSession ) );
       
   408    
       
   409     CMDBGenericRecord* wlanRecord = static_cast<CMDBGenericRecord*>
       
   410            ( CCDRecordBase::RecordFactoryL( 0));
       
   411     CleanupStack::PushL( wlanRecord );
       
   412     wlanRecord->InitializeL( KGenericTable(), NULL );
       
   413     wlanRecord->SetRecordId( aFrom );
       
   414     wlanRecord->LoadL( *dbSession );
       
   415        
       
   416     CMDBField<TDesC8>* binField = 
       
   417             (CMDBField<TDesC8>*)wlanRecord->GetFieldByIdL(KCDTIdWlanWpaPreSharedKey);
       
   418     TBuf8<KWLMMaxWpaPskLength> buf8;
       
   419     buf8.Copy( *binField );
       
   420     CleanupStack::PopAndDestroy( wlanRecord );
       
   421        
       
   422     CMDBGenericRecord* wlanRecordNew = static_cast<CMDBGenericRecord*>
       
   423            ( CCDRecordBase::RecordFactoryL( 0));
       
   424     CleanupStack::PushL( wlanRecordNew );
       
   425     wlanRecordNew->InitializeL( KGenericTable(),NULL );
       
   426     wlanRecordNew->SetRecordId( aTo );
       
   427     wlanRecordNew->LoadL( *dbSession );
       
   428        
       
   429     CMDBField<TDesC8>* binFieldNew = 
       
   430            (CMDBField<TDesC8>*)wlanRecordNew->GetFieldByIdL(KCDTIdWlanWpaPreSharedKey);
       
   431     binFieldNew->SetMaxLengthL( KWLMMaxWpaPskLength );
       
   432     binFieldNew->SetL( buf8 );
       
   433     *((CMDBField<TUint32>*)wlanRecordNew->GetFieldByIdL(KCDTIdWlanEnableWpaPsk)) = ETrue;
       
   434     wlanRecordNew->ModifyL( *dbSession );
       
   435     CleanupStack::PopAndDestroy( wlanRecordNew );
       
   436        
       
   437     CleanupStack::Pop( 1 ); // transaction rollback popped
       
   438           
       
   439     dbSession->CommitTransactionL();
       
   440        
       
   441     dbSession->Close();
       
   442     CleanupStack::PopAndDestroy( dbSession );
       
   443     DEBUG("CHssIapSettingsHandler::SaveWPAKey Done");
       
   444 }
       
   445 
       
   446 // ----------------------------------------------------------------------------
       
   447 // RollbackCommsDat
       
   448 // ----------------------------------------------------------------------------
       
   449 void CHssIapSettingsHandler::RollbackCommsDat( TAny* aDbSession )
       
   450     {
       
   451     DEBUG("CHssIapSettingsHandler::RollbackCommsDat");
       
   452     CommsDat::CMDBSession* session = static_cast<CommsDat::CMDBSession*>( aDbSession );
       
   453     TRAP_IGNORE( session->RollbackTransactionL() );
       
   454     }
       
   455 
       
   456 // ---------------------------------------------------------
       
   457 // ConvertConnectionSecurityModeToSecModeL
       
   458 // ---------------------------------------------------------
       
   459 //
       
   460 TWlanSecMode CHssIapSettingsHandler::ConvertConnectionSecurityModeToSecModeL(
       
   461                                      TWlanConnectionSecurityMode aSecurityMode )
       
   462      {
       
   463      DEBUG( "CHssIapSettingsHandler::ConvertConnectionSecurityModeToSecModeL" );
       
   464      switch( aSecurityMode )
       
   465          {
       
   466          case EWlanConnectionSecurityOpen:
       
   467              {
       
   468              return EWlanSecModeOpen;
       
   469              }
       
   470          
       
   471          case EWlanConnectionSecurityWep:
       
   472              {
       
   473              return EWlanSecModeWep;
       
   474              }
       
   475          
       
   476          case EWlanConnectionSecurity802d1x:
       
   477              {
       
   478              return EWlanSecMode802_1x;
       
   479              }
       
   480          
       
   481          case EWlanConnectionSecurityWpa:
       
   482          case EWlanConnectionSecurityWpaPsk:
       
   483              {
       
   484              return EWlanSecModeWpa;
       
   485              }
       
   486          
       
   487          default:
       
   488              {
       
   489              User::Leave( KErrNotSupported );
       
   490              }
       
   491          }
       
   492  
       
   493      return EWlanSecModeOpen;
       
   494      } 
       
   495 
       
   496 // ---------------------------------------------------------
       
   497 // ConvertConnectionModeToNetModeL
       
   498 // ---------------------------------------------------------
       
   499 //
       
   500 TWlanNetMode CHssIapSettingsHandler::ConvertConnectionModeToNetModeL(
       
   501         TWlanConnectionMode aConnMode )
       
   502     {
       
   503     DEBUG( "CHssIapSettingsHandler::ConvertConnectionModeToNetModeL" );
       
   504     if ( EWlanConnectionModeAdhoc == aConnMode )
       
   505         {
       
   506         return EAdhoc;
       
   507         }
       
   508     else
       
   509         {
       
   510         return EInfra;
       
   511         }
       
   512     }
       
   513 
       
   514 // End of File