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