wlansecuritysettings/wpasecuritysettingsui/src/WPASecuritySettingsImpl.cpp
changeset 0 c8830336c852
child 2 1c7bc153c08e
equal deleted inserted replaced
-1:000000000000 0:c8830336c852
       
     1 /*
       
     2 * Copyright (c) 2001-2009 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 the License "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 class CWPASecuritySettingsImpl.     
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include "WPASecuritySettingsUiPanic.h"
       
    22 
       
    23 #include "WPASecuritySettingsUI.hrh"
       
    24 
       
    25 #include <WPASecuritySettingsUI.h>
       
    26 #include <commdb.h>
       
    27 #include <EAPPluginConfigurationIf.h>
       
    28 #include "WPASecuritySettingsImpl.h"
       
    29 
       
    30 #include <commsdattypesv1_1.h>
       
    31 #include <cmmanagertablefields.h>
       
    32 #include <wlancontainer.h>
       
    33 
       
    34 
       
    35 // CONSTANTS
       
    36 LOCAL_D const TUint32 KUidNone = 0;     // Invalid id
       
    37 LOCAL_D const TUint32 E8021X = 4;       // 802.1X security mode
       
    38 LOCAL_D const TUint32 EWpa = 8;         // Wpa security mode
       
    39 LOCAL_D const TUint32 EWpa2 = 16;       // Wpa2 only security mode
       
    40 
       
    41 LOCAL_D const TUint32 KExpEapTypeLength = 8;    // expanded EAP type length
       
    42 
       
    43 // ================= MEMBER FUNCTIONS =======================
       
    44 
       
    45 // ---------------------------------------------------------
       
    46 // CWPASecuritySettingsImpl::NewL
       
    47 // ---------------------------------------------------------
       
    48 //
       
    49 CWPASecuritySettingsImpl* CWPASecuritySettingsImpl::NewL( 
       
    50                                                 TSecurityMode aSecurityMode )
       
    51     {
       
    52     CWPASecuritySettingsImpl* settings = new ( ELeave ) 
       
    53                                     CWPASecuritySettingsImpl( aSecurityMode );
       
    54     CleanupStack::PushL( settings );
       
    55     settings->ConstructL();
       
    56     CleanupStack::Pop( settings ); 
       
    57     return settings;    
       
    58     }
       
    59 
       
    60 
       
    61 // ---------------------------------------------------------
       
    62 // CWPASecuritySettingsImpl::CWPASecuritySettingsImpl
       
    63 // ---------------------------------------------------------
       
    64 //
       
    65 CWPASecuritySettingsImpl::CWPASecuritySettingsImpl( 
       
    66                                                 TSecurityMode aSecurityMode )
       
    67 : iSecurityMode( aSecurityMode ),
       
    68   iWPAMode( EFalse ),
       
    69   iWpa2Only( EFalse )
       
    70     {
       
    71     iWPAEAPPlugin.Zero();
       
    72     iWPAPreSharedKey.Zero();
       
    73     }
       
    74 
       
    75 
       
    76 // ---------------------------------------------------------
       
    77 // CWPASecuritySettingsImpl::ConstructL
       
    78 // ---------------------------------------------------------
       
    79 //
       
    80 void CWPASecuritySettingsImpl::ConstructL()
       
    81     {
       
    82     _LIT8( KMatchString, "EAPPConfig" );
       
    83 	TRAPD( err, iPlugin = CEAPPluginConfigurationIf::NewL( KMatchString ) );
       
    84     if ( err != KErrNone && err != KEComErrNoInterfaceIdentified )
       
    85         {
       
    86         User::Leave( err );
       
    87         }
       
    88     }
       
    89 
       
    90 
       
    91 // ---------------------------------------------------------
       
    92 // CWPASecuritySettingsImpl::~CWPASecuritySettingsImpl
       
    93 // ---------------------------------------------------------
       
    94 //
       
    95 CWPASecuritySettingsImpl::~CWPASecuritySettingsImpl()
       
    96     {
       
    97     delete iWPAEnabledEAPPlugin;
       
    98     delete iWPADisabledEAPPlugin;
       
    99     delete iPlugin;
       
   100     }
       
   101 
       
   102 
       
   103 // ---------------------------------------------------------
       
   104 // CWPASecuritySettingsImpl::LoadL
       
   105 // ---------------------------------------------------------
       
   106 //
       
   107 void CWPASecuritySettingsImpl::LoadL( TUint32 aIapId, 
       
   108                                       CCommsDatabase& aCommsDb )
       
   109     {
       
   110     if ( aIapId == KUidNone )
       
   111         {
       
   112         return;
       
   113         }
       
   114 
       
   115     CCommsDbTableView* wLanServiceTable;
       
   116         
       
   117     wLanServiceTable = aCommsDb.OpenViewMatchingUintLC(
       
   118                     TPtrC( WLAN_SERVICE ), TPtrC( WLAN_SERVICE_ID ), aIapId );
       
   119 
       
   120     TInt errorCode = wLanServiceTable->GotoFirstRecord();
       
   121     if ( errorCode == KErrNone )
       
   122         {
       
   123         // Get WPA Mode
       
   124         TRAPD( err, wLanServiceTable->ReadUintL( TPtrC( WLAN_ENABLE_WPA_PSK ),
       
   125                                                 ( TUint32& ) iWPAMode ) );
       
   126         if ( err != KErrNone )
       
   127             { // do not leave if value is not present in table...
       
   128             if ( err != KErrUnknown )
       
   129                 User::Leave( err );
       
   130             }
       
   131 
       
   132         TUint32 secMode = 0;
       
   133         // Get WPA2 Only Mode
       
   134         TRAP( err, wLanServiceTable->ReadUintL( TPtrC( WLAN_SECURITY_MODE ),
       
   135                                                  secMode ) );
       
   136         if ( err != KErrNone )
       
   137             { // do not leave if value is not present in table...
       
   138             if ( err != KErrUnknown )
       
   139                 User::Leave( err );
       
   140             }
       
   141 
       
   142         iWpa2Only = secMode == EWpa2;
       
   143 
       
   144         // Get EAP list
       
   145     	iWPAEAPPlugin.Copy( *wLanServiceTable->ReadLongTextLC( 
       
   146                                                         TPtrC( WLAN_EAPS ) ) );
       
   147 	    CleanupStack::PopAndDestroy();
       
   148 	    
       
   149 	    if ( !iWPAEAPPlugin.Length() )
       
   150 	        {
       
   151 	        // no data found in the old column, use the new ones
       
   152 
       
   153 	        // enabled EAP types
       
   154 	        HBufC *data = wLanServiceTable->ReadLongTextLC( 
       
   155 	                                              TPtrC( WLAN_ENABLED_EAPS ) );
       
   156 
       
   157             TPtrC8 reint( reinterpret_cast<const TUint8*>( data->Ptr() ), 
       
   158 	                                                       data->Size() );
       
   159             
       
   160         	delete iWPAEnabledEAPPlugin; iWPAEnabledEAPPlugin = NULL;
       
   161         	iWPAEnabledEAPPlugin = reint.AllocL();
       
   162 
       
   163             CleanupStack::PopAndDestroy( data );
       
   164             
       
   165             
       
   166             // 2. disabled EAP types
       
   167             data = wLanServiceTable->ReadLongTextLC( 
       
   168 	                                             TPtrC( WLAN_DISABLED_EAPS ) );
       
   169             
       
   170             reint.Set( reinterpret_cast<const TUint8*>( data->Ptr() ), 
       
   171 	                                                    data->Size() );
       
   172 	                                                    
       
   173         	delete iWPADisabledEAPPlugin; iWPADisabledEAPPlugin = NULL;
       
   174         	iWPADisabledEAPPlugin = reint.AllocL();
       
   175 
       
   176 	        CleanupStack::PopAndDestroy( data );
       
   177 	        }
       
   178 	    else
       
   179 	        {
       
   180 	        // generate appropriate entries in the new enabled and disabled list,
       
   181 	        // overwriting those values 
       
   182 	        
       
   183 	        // count the + and - signs to determine the size of enabled and 
       
   184 	        // disabled descriptors
       
   185 	        TLex lex( iWPAEAPPlugin );
       
   186             
       
   187 	        TInt numPlus = 0;
       
   188 	        TInt numMinus = 0;
       
   189 	        TChar ch;
       
   190 	        while ( !lex.Eos() )
       
   191 	            {
       
   192 	            ch = lex.Get();
       
   193 	            if ( ch == '+' ) ++numPlus;
       
   194 	            else if ( ch == '-' ) ++numMinus;
       
   195 	            }
       
   196 	            
       
   197             // each entry consumes 8 bytes in binary format
       
   198             delete iWPAEnabledEAPPlugin; iWPAEnabledEAPPlugin = NULL;
       
   199             iWPAEnabledEAPPlugin = HBufC8::NewL( 8 * numPlus );
       
   200             
       
   201             delete iWPADisabledEAPPlugin; iWPADisabledEAPPlugin = NULL;
       
   202             iWPADisabledEAPPlugin = HBufC8::NewL( 8 * numMinus );
       
   203 
       
   204             lex.Assign( iWPAEAPPlugin );
       
   205             
       
   206             while ( !lex.Eos() )
       
   207                 {
       
   208                 // beginning of implementation UID
       
   209                 TInt16 implUid = 0;
       
   210                 
       
   211                 if ( lex.Val( implUid ) != KErrNone || !implUid )
       
   212                     {
       
   213                     // if the old string is corrupted, null out both lists
       
   214                     iWPAEnabledEAPPlugin->Des().Zero();
       
   215                     iWPADisabledEAPPlugin->Des().Zero();
       
   216                     break;
       
   217                     }
       
   218 
       
   219                 // append it to the appropriate list ('+' enabled, '-' disabled)
       
   220                 _LIT8( KPadding, "\xFE\0\0\0\0\0\0" );
       
   221                 _LIT8( KMsChapV2Padding, "\xFE\xFF\xFF\xFF\0\0\0");
       
   222                 const TInt KPlainMsChapV2ImplUid = 99;
       
   223                 
       
   224                 if ( implUid > 0 )
       
   225                     {
       
   226                     iWPAEnabledEAPPlugin->Des().Append( 
       
   227                                         Abs( implUid ) == KPlainMsChapV2ImplUid? 
       
   228                                                     KMsChapV2Padding: KPadding );
       
   229                     iWPAEnabledEAPPlugin->Des().Append( Abs( implUid ) );
       
   230                     }
       
   231                 else if (implUid < 0 )
       
   232                     {
       
   233                     iWPADisabledEAPPlugin->Des().Append( 
       
   234                                         Abs( implUid ) == KPlainMsChapV2ImplUid? 
       
   235                                                     KMsChapV2Padding: KPadding );
       
   236                     iWPADisabledEAPPlugin->Des().Append( Abs( implUid ) );
       
   237                     }
       
   238                 
       
   239                 // swallow the delimiter (',')
       
   240                 lex.Get();
       
   241                 }
       
   242                 
       
   243             // finally, wipe old column data
       
   244             iWPAEAPPlugin.Zero();
       
   245 	        }
       
   246 	        
       
   247         // Get PreShared Key
       
   248         wLanServiceTable->ReadTextL( TPtrC( WLAN_WPA_PRE_SHARED_KEY ), 
       
   249                                     iWPAPreSharedKey );
       
   250 
       
   251         if ( !IsValidPsk( iWPAPreSharedKey ) )
       
   252             {       
       
   253             // invalid key format
       
   254             iWPAPreSharedKey.Zero();
       
   255             }
       
   256         }
       
   257 
       
   258     CleanupStack::PopAndDestroy( wLanServiceTable );  // wLanServiceTable
       
   259     }
       
   260 
       
   261 
       
   262 // ---------------------------------------------------------
       
   263 // CWPASecuritySettingsImpl::SaveL
       
   264 // ---------------------------------------------------------
       
   265 //
       
   266 void CWPASecuritySettingsImpl::SaveL( TUint32 aIapId, 
       
   267                                       CCommsDatabase& aCommsDb,
       
   268                                       TTypeOfSaving aTypeOfSaving,
       
   269                                       TUint32 aOldIapId )
       
   270     {
       
   271     CCommsDbTableView* wLanServiceTable;
       
   272 
       
   273     // Caller MUST initiate a transaction, WE WILL NOT.
       
   274 
       
   275     wLanServiceTable = aCommsDb.OpenViewMatchingUintLC( 
       
   276                     TPtrC( WLAN_SERVICE ), TPtrC( WLAN_SERVICE_ID ), aIapId );
       
   277     TInt errorCode = wLanServiceTable->GotoFirstRecord();
       
   278 
       
   279     if ( errorCode == KErrNone )
       
   280         {
       
   281         wLanServiceTable->UpdateRecord();
       
   282         }
       
   283     else
       
   284         {
       
   285         TUint32 dummyUid( KUidNone );
       
   286         User::LeaveIfError( wLanServiceTable->InsertRecord( dummyUid ) );
       
   287 
       
   288         // Save link to LAN service
       
   289         wLanServiceTable->WriteUintL( TPtrC( WLAN_SERVICE_ID ), aIapId );
       
   290         }
       
   291 
       
   292     // Save WPA Mode
       
   293     wLanServiceTable->WriteUintL( TPtrC( WLAN_ENABLE_WPA_PSK ), 
       
   294                                   ( TUint32& ) iWPAMode );
       
   295 
       
   296     TUint32 secMode;
       
   297 
       
   298     if ( iSecurityMode == ESecurityMode8021x )
       
   299         {
       
   300         secMode = E8021X;
       
   301         }
       
   302     else if ( iWpa2Only )
       
   303         {
       
   304         secMode = EWpa2;
       
   305         }
       
   306     else
       
   307         {
       
   308         secMode = EWpa;
       
   309         }
       
   310 
       
   311     // Save security mode
       
   312     wLanServiceTable->WriteUintL( TPtrC( WLAN_SECURITY_MODE ), secMode );
       
   313 
       
   314     // With expanded EAP types allowed we no longer need the old column
       
   315     iWPAEAPPlugin.Zero(); 
       
   316 
       
   317     // Save EAP list
       
   318     wLanServiceTable->WriteLongTextL( TPtrC( WLAN_EAPS ), iWPAEAPPlugin );
       
   319 
       
   320 
       
   321     // Save the expanded EAPs
       
   322     wLanServiceTable->WriteTextL( TPtrC( WLAN_ENABLED_EAPS ), 
       
   323                                   iWPAEnabledEAPPlugin? 
       
   324                                         (const TDesC8&)*iWPAEnabledEAPPlugin: 
       
   325                                         (const TDesC8&)KNullDesC8 );
       
   326 
       
   327     wLanServiceTable->WriteTextL( TPtrC( WLAN_DISABLED_EAPS ), 
       
   328                                   iWPADisabledEAPPlugin? 
       
   329                                         (const TDesC8&)*iWPADisabledEAPPlugin: 
       
   330                                         (const TDesC8&)KNullDesC8 );
       
   331     // Save PreShared Key
       
   332     wLanServiceTable->WriteTextL( TPtrC( WLAN_WPA_PRE_SHARED_KEY ), 
       
   333                                   iWPAPreSharedKey );
       
   334 
       
   335     // Save PreShared Key Length
       
   336     wLanServiceTable->WriteUintL( TPtrC( WLAN_WPA_KEY_LENGTH ), 
       
   337                                   iWPAPreSharedKey.Length() );
       
   338 
       
   339     wLanServiceTable->PutRecordChanges();
       
   340 
       
   341     if ( iPlugin )
       
   342         {
       
   343         if ( aTypeOfSaving == ESavingBrandNewAP )
       
   344             {
       
   345             iPlugin->ChangeIapIDL( aOldIapId, aIapId );
       
   346             }
       
   347         else if ( aTypeOfSaving == ESavingNewAPAsACopy )
       
   348             {
       
   349             iPlugin->CopySettingsL( aOldIapId, aIapId );
       
   350             }
       
   351         }
       
   352 
       
   353     CleanupStack::PopAndDestroy( wLanServiceTable );  // wLanServiceTable
       
   354     }
       
   355 
       
   356 
       
   357 // ---------------------------------------------------------
       
   358 // CWPASecuritySettingsImpl::DeleteL
       
   359 // ---------------------------------------------------------
       
   360 //
       
   361 void CWPASecuritySettingsImpl::DeleteL( TUint32 aIapId )
       
   362     {
       
   363     if ( iPlugin )
       
   364         {
       
   365         iPlugin->DeleteSettingsL( aIapId );
       
   366         }
       
   367     }
       
   368 
       
   369 
       
   370 // ---------------------------------------------------------
       
   371 // CWPASecuritySettingsImpl::IsValid
       
   372 // ---------------------------------------------------------
       
   373 //
       
   374 TBool CWPASecuritySettingsImpl::IsValid()
       
   375     {
       
   376     TBool retval = ETrue;
       
   377     if ( iWPAMode || !iPlugin )
       
   378         {
       
   379         retval = IsValidPsk( iWPAPreSharedKey );
       
   380         }
       
   381 
       
   382     return retval;
       
   383     }
       
   384 
       
   385 
       
   386 // ---------------------------------------------------------
       
   387 // CWPASecuritySettingsImpl::IsValidPsk
       
   388 // ---------------------------------------------------------
       
   389 //
       
   390 TBool CWPASecuritySettingsImpl::IsValidPsk( const TDesC8& aPsk )
       
   391     {
       
   392     TBool ret( EFalse );
       
   393 
       
   394     TInt len = aPsk.Length();
       
   395     
       
   396     ret = ( len >= EMinLengthOfPreSharedKeyAscii && 
       
   397             len <= EMaxLengthOfPreSharedKeyAscii );
       
   398                
       
   399     if ( !ret && len == ELengthOfPreSharedKeyHex )
       
   400         {
       
   401         // perhaps it is hex
       
   402         ret = ETrue;
       
   403         
       
   404         for ( TInt i = 0; i < len; ++i )
       
   405             {
       
   406             TChar ch( aPsk[i] );
       
   407             if ( !ch.IsHexDigit() )
       
   408                 {
       
   409                 // got a bad character
       
   410                 ret = EFalse;
       
   411                 break;
       
   412                 }
       
   413             }
       
   414         }
       
   415     
       
   416     return ret;
       
   417     }
       
   418 
       
   419 
       
   420 // ---------------------------------------------------------
       
   421 // CWPASecuritySettingsImpl::SetWPAPreSharedKey
       
   422 // ---------------------------------------------------------
       
   423 //
       
   424 TInt CWPASecuritySettingsImpl::SetWPAPreSharedKey( 
       
   425                                                 const TDesC& aPreSharedKey )
       
   426     {
       
   427     TInt ret( KErrNone );
       
   428     
       
   429     HBufC8* buf8 = HBufC8::New( aPreSharedKey.Length() );
       
   430     
       
   431     if ( buf8 )
       
   432         {
       
   433         TPtr8 pskPtr( buf8->Des() );
       
   434         pskPtr.Copy( aPreSharedKey ); 
       
   435 
       
   436         if ( IsValidPsk( pskPtr ) )
       
   437             {
       
   438             SetWPAPreSharedKey( pskPtr );
       
   439             SetWPAMode( ETrue );
       
   440             }
       
   441         else
       
   442             {
       
   443             ret = KErrArgument;
       
   444             }
       
   445 
       
   446         delete buf8;
       
   447         }
       
   448     else
       
   449         {
       
   450         ret = KErrNoMemory;
       
   451         }
       
   452     
       
   453     return ret;
       
   454     }
       
   455 
       
   456 
       
   457 // ---------------------------------------------------------
       
   458 // CWPASecuritySettingsImpl::SetWPAEnabledEAPPlugin
       
   459 // ---------------------------------------------------------
       
   460 //
       
   461 TInt CWPASecuritySettingsImpl::SetWPAEnabledEAPPlugin( 
       
   462                                              const TDesC8& aEnabledPluginList )
       
   463 	{
       
   464 	delete iWPAEnabledEAPPlugin; iWPAEnabledEAPPlugin = NULL;
       
   465 
       
   466     if ( aEnabledPluginList.Length() % KExpEapTypeLength )
       
   467         {
       
   468         // valid expanded EAP types occupy 8 bytes each
       
   469         return KErrArgument;
       
   470         }
       
   471 
       
   472 	if ( aEnabledPluginList.Length() )
       
   473 	    {
       
   474 	    iWPAEnabledEAPPlugin = aEnabledPluginList.Alloc();
       
   475 	    if ( !iWPAEnabledEAPPlugin )
       
   476 	        {
       
   477 	        return KErrNoMemory;
       
   478 	        }
       
   479 	    }
       
   480 	
       
   481 	return KErrNone;
       
   482 	}
       
   483 
       
   484 
       
   485 // ---------------------------------------------------------
       
   486 // CWPASecuritySettingsImpl::SetWPADisabledEAPPlugin
       
   487 // ---------------------------------------------------------
       
   488 //
       
   489 TInt CWPASecuritySettingsImpl::SetWPADisabledEAPPlugin( 
       
   490                                             const TDesC8& aDisabledPluginList )
       
   491 	{
       
   492 	delete iWPADisabledEAPPlugin; iWPADisabledEAPPlugin = NULL;
       
   493 
       
   494     if ( aDisabledPluginList.Length() % KExpEapTypeLength )
       
   495         {
       
   496         // valid expanded EAP types occupy 8 bytes each
       
   497         return KErrArgument;
       
   498         }
       
   499         
       
   500 	if ( aDisabledPluginList.Length() )
       
   501 	    {
       
   502 	    iWPADisabledEAPPlugin = aDisabledPluginList.Alloc();
       
   503 	    if ( !iWPADisabledEAPPlugin )
       
   504 	        {
       
   505 	        return KErrNoMemory;
       
   506 	        }
       
   507 	    }
       
   508 	
       
   509 	return KErrNone;
       
   510 	}
       
   511 	
       
   512 
       
   513 // ---------------------------------------------------------
       
   514 // CWPASecuritySettingsImpl::LoadL
       
   515 // ---------------------------------------------------------
       
   516 //
       
   517 void CWPASecuritySettingsImpl::LoadL( TUint32 aIapId, 
       
   518                                       CMDBSession& aSession )
       
   519     {
       
   520     if ( aIapId == KUidNone )
       
   521         {
       
   522         return;
       
   523         }
       
   524         
       
   525     // Load WLAN service table        
       
   526     // first get WLAN table id
       
   527     CMDBGenericRecord* generic = static_cast<CMDBGenericRecord*>
       
   528         ( CCDRecordBase::RecordFactoryL( 0 ) );
       
   529     CleanupStack::PushL( generic );    
       
   530     generic->InitializeL( TPtrC( WLAN_SERVICE ), NULL );
       
   531     generic->LoadL( aSession );
       
   532     TMDBElementId wlanTableId = generic->TableId();
       
   533     
       
   534     CMDBField<TUint32>* sidField = static_cast<CMDBField<TUint32>*>
       
   535                              ( generic->GetFieldByIdL( KCDTIdWlanServiceId ) );
       
   536                                     
       
   537     // prime with service id                
       
   538     *sidField = aIapId;
       
   539 
       
   540     if( generic->FindL( aSession) )
       
   541         {
       
   542         // Get WPA mode
       
   543         CMDBField<TUint>* enableWpaPskField = static_cast<CMDBField<TUint>*>
       
   544                           ( generic->GetFieldByIdL( KCDTIdWlanEnableWpaPsk ) );
       
   545         iWPAMode = *enableWpaPskField;
       
   546         
       
   547         // Get WPA2 Only Mode
       
   548         CMDBField<TUint>* secModeField = static_cast<CMDBField<TUint>*>
       
   549                                ( generic->GetFieldByIdL( KCDTIdWlanSecMode ) );
       
   550         TUint32 secMode = *secModeField;
       
   551         iWpa2Only = secMode == EWpa2;
       
   552         
       
   553         // Get EAP plugins
       
   554         CMDBField<TDesC>* wlanEapsField = static_cast<CMDBField<TDesC>*>
       
   555                                   ( generic->GetFieldByIdL( KCDTIdWlanEaps ) );
       
   556         iWPAEAPPlugin = *wlanEapsField;
       
   557         
       
   558 	    if ( !iWPAEAPPlugin.Length() )
       
   559 	        {
       
   560 	        // no data found in the old column, use the new ones
       
   561 
       
   562 	        // enabled EAP types
       
   563             CMDBField<TDesC8>* wlanEnabledEapsField = 
       
   564                           static_cast<CMDBField<TDesC8>*>
       
   565                           ( generic->GetFieldByIdL( KCDTIdWlanEnabledEaps ) );
       
   566 
       
   567         	delete iWPAEnabledEAPPlugin; iWPAEnabledEAPPlugin = NULL;
       
   568         	iWPAEnabledEAPPlugin = 
       
   569         	             ( ( const TDesC8& ) *wlanEnabledEapsField ).AllocL();
       
   570 
       
   571             
       
   572             // disabled EAP types
       
   573             CMDBField<TDesC8>* wlanDisabledEapsField = 
       
   574                           static_cast<CMDBField<TDesC8>*>
       
   575                           ( generic->GetFieldByIdL( KCDTIdWlanDisabledEaps ) );
       
   576             
       
   577         	delete iWPADisabledEAPPlugin; iWPADisabledEAPPlugin = NULL;
       
   578         	iWPADisabledEAPPlugin = 
       
   579         	             ( ( const TDesC8& ) *wlanDisabledEapsField ).AllocL();
       
   580 
       
   581 	        }
       
   582 	    else
       
   583 	        {
       
   584 	        // generate appropriate entries in the new enabled and disabled list,
       
   585 	        // overwriting those values 
       
   586 	        
       
   587 	        // count the + and - signs to determine the size of enabled and 
       
   588 	        // disabled descriptors
       
   589 	        TLex lex( iWPAEAPPlugin );
       
   590             
       
   591 	        TInt numPlus = 0;
       
   592 	        TInt numMinus = 0;
       
   593 	        TChar ch;
       
   594 	        while ( !lex.Eos() )
       
   595 	            {
       
   596 	            ch = lex.Get();
       
   597 	            if ( ch == '+' ) ++numPlus;
       
   598 	            else if ( ch == '-' ) ++numMinus;
       
   599 	            }
       
   600 	            
       
   601             // each entry consumes 8 bytes in binary format
       
   602             delete iWPAEnabledEAPPlugin; iWPAEnabledEAPPlugin = NULL;
       
   603             iWPAEnabledEAPPlugin = HBufC8::NewL( 8 * numPlus );
       
   604             
       
   605             delete iWPADisabledEAPPlugin; iWPADisabledEAPPlugin = NULL;
       
   606             iWPADisabledEAPPlugin = HBufC8::NewL( 8 * numMinus );
       
   607 
       
   608             lex.Assign( iWPAEAPPlugin );
       
   609             
       
   610             while ( !lex.Eos() )
       
   611                 {
       
   612                 // beginning of implementation UID
       
   613                 TInt16 implUid = 0;
       
   614                 
       
   615                 if ( lex.Val( implUid ) != KErrNone || !implUid )
       
   616                     {
       
   617                     // if the old string is corrupted, null out both lists
       
   618                     iWPAEnabledEAPPlugin->Des().Zero();
       
   619                     iWPADisabledEAPPlugin->Des().Zero();
       
   620                     break;
       
   621                     }
       
   622 
       
   623                 // append it to the appropriate list ('+' enabled, '-' disabled)
       
   624                 _LIT8( KPadding, "\xFE\0\0\0\0\0\0" );
       
   625                 _LIT8( KMsChapV2Padding, "\xFE\xFF\xFF\xFF\0\0\0");
       
   626                 const TInt KPlainMsChapV2ImplUid = 99;
       
   627                 
       
   628                 if ( implUid > 0 )
       
   629                     {
       
   630                     iWPAEnabledEAPPlugin->Des().Append( 
       
   631                                         Abs( implUid ) == KPlainMsChapV2ImplUid? 
       
   632                                                     KMsChapV2Padding: KPadding );
       
   633                     iWPAEnabledEAPPlugin->Des().Append( Abs( implUid ) );
       
   634                     }
       
   635                 else if (implUid < 0 )
       
   636                     {
       
   637                     iWPADisabledEAPPlugin->Des().Append( 
       
   638                                         Abs( implUid ) == KPlainMsChapV2ImplUid? 
       
   639                                                     KMsChapV2Padding: KPadding );
       
   640                     iWPADisabledEAPPlugin->Des().Append( Abs( implUid ) );
       
   641                     }
       
   642                 
       
   643                 // swallow the delimiter (',')
       
   644                 lex.Get();
       
   645                 }
       
   646                 
       
   647             // finally, wipe old column data
       
   648             iWPAEAPPlugin.Zero();
       
   649 	        }
       
   650 
       
   651         // GetWPA preshared key
       
   652         CMDBField<TDesC8>* wpaPskField = static_cast<CMDBField<TDesC8>*>
       
   653                        ( generic->GetFieldByIdL( KCDTIdWlanWpaPreSharedKey ) );
       
   654         iWPAPreSharedKey = *wpaPskField;
       
   655         
       
   656         if ( !IsValidPsk( iWPAPreSharedKey ) )
       
   657             {       
       
   658             // invalid key format
       
   659             iWPAPreSharedKey.Zero();
       
   660             }
       
   661         }
       
   662     
       
   663     CleanupStack::PopAndDestroy( generic );
       
   664     }
       
   665     
       
   666     
       
   667 // ---------------------------------------------------------
       
   668 // CWPASecuritySettingsImpl::SaveL
       
   669 // ---------------------------------------------------------
       
   670 //
       
   671 void CWPASecuritySettingsImpl::SaveL( TUint32 aIapId, 
       
   672                                       CMDBSession& aSession,
       
   673                                       TTypeOfSaving aTypeOfSaving,
       
   674                                       TUint32 aOldIapId )
       
   675     {
       
   676     const TInt KRetryWait = 100000;    // Wait time between retries in TTimeIntervalMicroSeconds32
       
   677     const TInt KRetryCount = 50;       // Max retry count
       
   678 
       
   679     // Load WLAN service table
       
   680     // first get WLAN table id
       
   681     CMDBGenericRecord* generic = static_cast<CMDBGenericRecord*>
       
   682         ( CCDRecordBase::RecordFactoryL( 0 ) );
       
   683     CleanupStack::PushL( generic );    
       
   684     generic->InitializeL( TPtrC( WLAN_SERVICE ), NULL );
       
   685     generic->LoadL( aSession );
       
   686     TMDBElementId wlanTableId = generic->TableId();
       
   687     
       
   688     CMDBField<TUint32>* sidField = static_cast<CMDBField<TUint32>*>
       
   689                              ( generic->GetFieldByIdL( KCDTIdWlanServiceId ) );
       
   690     
       
   691     // prime with service id                
       
   692     *sidField = aIapId;
       
   693     
       
   694     TBool found = generic->FindL( aSession);
       
   695    
       
   696     // If loading failed, WLAN service record will be 
       
   697     // created and StoreL()-d, otherwise, ModifyL()
       
   698     
       
   699     // Set WPA mode
       
   700     CMDBField<TUint>* enableWpaPskField = static_cast<CMDBField<TUint>*>
       
   701                           ( generic->GetFieldByIdL( KCDTIdWlanEnableWpaPsk ) );
       
   702     enableWpaPskField->SetL( iWPAMode );
       
   703     
       
   704     // Set security mode
       
   705     TUint32 secMode;
       
   706     if ( iSecurityMode == ESecurityMode8021x )
       
   707         {
       
   708         secMode = E8021X;
       
   709         }
       
   710     else if ( iWpa2Only )
       
   711         {
       
   712         secMode = EWpa2;
       
   713         }
       
   714     else
       
   715         {
       
   716         secMode = EWpa;
       
   717         }
       
   718     CMDBField<TUint>* secModeField = static_cast<CMDBField<TUint>*>
       
   719                         ( generic->GetFieldByIdL( KCDTIdWlanSecMode ) );
       
   720     secModeField->SetL( secMode );
       
   721     
       
   722     // Save EAP list
       
   723     CMDBField<TDesC>* wlanEapsField = static_cast<CMDBField<TDesC>*>
       
   724                                 ( generic->GetFieldByIdL( KCDTIdWlanEaps ) );
       
   725 
       
   726     // when using the expanded EAP types, wipe out data in the old column
       
   727     iWPAEAPPlugin.Zero();
       
   728 
       
   729     wlanEapsField->SetL( iWPAEAPPlugin );
       
   730 
       
   731     // Save the expanded EAPs
       
   732     CMDBField<TDesC8>* wlanEnabledEapsField = static_cast<CMDBField<TDesC8>*>
       
   733                            ( generic->GetFieldByIdL( KCDTIdWlanEnabledEaps ) );
       
   734     wlanEnabledEapsField->SetL( iWPAEnabledEAPPlugin? 
       
   735                                     (const TDesC8&)*iWPAEnabledEAPPlugin: 
       
   736                                     (const TDesC8&)KNullDesC8 );
       
   737 
       
   738 
       
   739     CMDBField<TDesC8>* wlanDisabledEapsField = static_cast<CMDBField<TDesC8>*>
       
   740                           ( generic->GetFieldByIdL( KCDTIdWlanDisabledEaps ) );
       
   741     wlanDisabledEapsField->SetL( iWPADisabledEAPPlugin? 
       
   742                                     (const TDesC8&)*iWPADisabledEAPPlugin: 
       
   743                                     (const TDesC8&)KNullDesC8 );
       
   744 
       
   745     // Save PreShared Key
       
   746     CMDBField<TDesC8>* wpaPskField = static_cast<CMDBField<TDesC8>*>
       
   747                        ( generic->GetFieldByIdL( KCDTIdWlanWpaPreSharedKey ) );
       
   748     wpaPskField->SetL( iWPAPreSharedKey );
       
   749 
       
   750     // Save PreShared Key length
       
   751     CMDBField<TUint>* keyLengthField = static_cast<CMDBField<TUint>*>
       
   752                         ( generic->GetFieldByIdL( KCDTIdWlanWpaKeyLength ) );
       
   753     keyLengthField->SetL( iWPAPreSharedKey.Length() );
       
   754     
       
   755     TInt error( KErrNone );
       
   756     
       
   757     // Saving changes
       
   758     for ( TInt i( 0 ); i < KRetryCount; i++ )
       
   759         {
       
   760         
       
   761         // If table existed modify it
       
   762         if( found )
       
   763             {
       
   764             TRAP( error, generic->ModifyL( aSession ) );
       
   765             }
       
   766                    
       
   767         // Otherwise store a new record
       
   768         else
       
   769             {
       
   770             generic->SetRecordId( KCDNewRecordRequest );
       
   771             TRAP( error, generic->StoreL( aSession ) );
       
   772             }
       
   773                   
       
   774         // If operation failed with KErrLocked, we'll retry.
       
   775         if ( KErrLocked == error )
       
   776             {
       
   777             User::After( KRetryWait );
       
   778             }
       
   779         
       
   780         // Otherwise break the retry loop.
       
   781         else 
       
   782             {
       
   783             break;        
       
   784             }
       
   785         }
       
   786     
       
   787     // If the save operation failed, leave now. 
       
   788     User::LeaveIfError( error );
       
   789 
       
   790     CleanupStack::PopAndDestroy( generic );
       
   791             
       
   792     if ( iPlugin )
       
   793         {
       
   794         if ( aTypeOfSaving == ESavingBrandNewAP )
       
   795             {
       
   796             iPlugin->ChangeIapIDL( aOldIapId, aIapId );
       
   797             }
       
   798         else if ( aTypeOfSaving == ESavingNewAPAsACopy )
       
   799             {
       
   800             iPlugin->CopySettingsL( aOldIapId, aIapId );
       
   801             }
       
   802         }
       
   803     }
       
   804 
       
   805 // End of File