wlansecuritysettings/wepsecuritysettingsui/src/WEPSecuritySettingsImpl.cpp
branchRCL_3
changeset 46 c74b3d9f6b9e
equal deleted inserted replaced
45:bad0cc58d154 46:c74b3d9f6b9e
       
     1 /*
       
     2 * Copyright (c) 2001-2010 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 CWEPSecuritySettingsImpl.     
       
    15 *
       
    16 */
       
    17 
       
    18 /*
       
    19 * %version: tr1cfwln#27 %
       
    20 */
       
    21 
       
    22 // INCLUDE FILES
       
    23 
       
    24 #include "WEPSecuritySettingsImpl.h"
       
    25 #include "WEPSecuritySettingsUiPanic.h"
       
    26 
       
    27 #include <WEPSecuritySettingsUI.h>
       
    28 #include <commdb.h>
       
    29 #include <featmgr.h>
       
    30 #include <WlanCdbCols.h>
       
    31 
       
    32 #include <commsdattypesv1_1.h>
       
    33 #include <cmmanagertablefields.h>
       
    34 #include <wlancontainer.h>
       
    35 
       
    36 // CONSTANT DECLARATIONS
       
    37 
       
    38 // Index of first key
       
    39 LOCAL_D const TInt KFirstKey = 0;
       
    40 
       
    41 // Index of second key
       
    42 LOCAL_D const TInt KSecondKey = 1;
       
    43 
       
    44 // Index of third key
       
    45 LOCAL_D const TInt KThirdKey = 2;
       
    46 
       
    47 // Index of fourth key
       
    48 LOCAL_D const TInt KFourthKey = 3;
       
    49 
       
    50 // Ratio of ascii and hex key sizes
       
    51 LOCAL_D const TInt KAsciiHexRatio = 2;
       
    52 
       
    53 
       
    54 // ================= MEMBER FUNCTIONS =======================
       
    55 
       
    56 // ---------------------------------------------------------
       
    57 // CWEPSecuritySettingsImpl::NewL
       
    58 // ---------------------------------------------------------
       
    59 //
       
    60 CWEPSecuritySettingsImpl* CWEPSecuritySettingsImpl::NewL()
       
    61     {
       
    62     CWEPSecuritySettingsImpl* settings = 
       
    63                                     new ( ELeave ) CWEPSecuritySettingsImpl();
       
    64     CleanupStack::PushL( settings );
       
    65     settings->ConstructL();
       
    66     CleanupStack::Pop( settings ); 
       
    67     return settings;    
       
    68     }
       
    69 
       
    70 
       
    71 // ---------------------------------------------------------
       
    72 // CWEPSecuritySettingsImpl::CWEPSecuritySettingsImpl
       
    73 // ---------------------------------------------------------
       
    74 //
       
    75 CWEPSecuritySettingsImpl::CWEPSecuritySettingsImpl()
       
    76 : iIsWEP256Enabled( ETrue )
       
    77     {
       
    78     iKeyInUse = CWEPSecuritySettings::EKeyNumber1;
       
    79     iAuthentication = CWEPSecuritySettings::EAuthOpen;
       
    80     for ( TUint i = 0; i < KMaxNumberofKeys; i++)
       
    81         {
       
    82         iKeyLength[i] = CWEPSecuritySettings::E40Bits;
       
    83         iKeyFormat[i] = CWEPSecuritySettings::EAscii;
       
    84         iKeyData[i].Zero();
       
    85         }
       
    86     }
       
    87 
       
    88 
       
    89 // ---------------------------------------------------------
       
    90 // CWEPSecuritySettingsImpl::ConstructL
       
    91 // ---------------------------------------------------------
       
    92 //
       
    93 void CWEPSecuritySettingsImpl::ConstructL()
       
    94     {
       
    95     // WEP256 is deprecated.
       
    96     iIsWEP256Enabled = EFalse;
       
    97     }
       
    98 
       
    99 
       
   100 // ---------------------------------------------------------
       
   101 // CWEPSecuritySettingsImpl::~CWEPSecuritySettingsImpl
       
   102 // ---------------------------------------------------------
       
   103 //
       
   104 CWEPSecuritySettingsImpl::~CWEPSecuritySettingsImpl()
       
   105     {
       
   106     }
       
   107 
       
   108 
       
   109 // ---------------------------------------------------------
       
   110 // CWEPSecuritySettingsImpl::LoadL
       
   111 // ---------------------------------------------------------
       
   112 //
       
   113 void CWEPSecuritySettingsImpl::LoadL( TUint32 aIapId, 
       
   114                                       CCommsDatabase& aCommsDb )
       
   115     {
       
   116     if ( aIapId == KUidNone )
       
   117         {
       
   118         return;
       
   119         }
       
   120 
       
   121     CCommsDbTableView* wLanServiceTable;
       
   122         
       
   123     wLanServiceTable = aCommsDb.OpenViewMatchingUintLC(
       
   124                     TPtrC( WLAN_SERVICE ), TPtrC( WLAN_SERVICE_ID ), aIapId );
       
   125 
       
   126     TInt errorCode = wLanServiceTable->GotoFirstRecord();
       
   127     if ( errorCode == KErrNone )
       
   128         {
       
   129         // Get index of key in use
       
   130         TRAPD( err, wLanServiceTable->ReadUintL( TPtrC( WLAN_WEP_INDEX ),
       
   131                                     ( TUint32& ) iKeyInUse ) );
       
   132 
       
   133         // Get index of key in use
       
   134         TRAP( err, wLanServiceTable->ReadUintL( 
       
   135                                         TPtrC( NU_WLAN_AUTHENTICATION_MODE ),
       
   136                                         ( TUint32& ) iAuthentication ) );
       
   137 
       
   138         // Get first WEP key
       
   139         wLanServiceTable->ReadTextL( TPtrC( NU_WLAN_WEP_KEY1 ), 
       
   140                                      iKeyData[KFirstKey] );
       
   141         SetLenKeyDataFromText( KFirstKey );
       
   142 
       
   143         // Get second WEP key
       
   144         wLanServiceTable->ReadTextL( TPtrC( NU_WLAN_WEP_KEY2 ), 
       
   145                                      iKeyData[KSecondKey] );
       
   146         SetLenKeyDataFromText( KSecondKey );
       
   147 
       
   148         // Get third WEP key
       
   149         wLanServiceTable->ReadTextL( TPtrC( NU_WLAN_WEP_KEY3 ), 
       
   150                                      iKeyData[KThirdKey] );
       
   151         SetLenKeyDataFromText( KThirdKey );
       
   152 
       
   153         // Get fourth WEP key
       
   154         wLanServiceTable->ReadTextL( TPtrC( NU_WLAN_WEP_KEY4 ), 
       
   155                                      iKeyData[KFourthKey] );
       
   156         SetLenKeyDataFromText( KFourthKey );
       
   157 
       
   158 
       
   159         // Get the format of the keys
       
   160         TRAP( err, wLanServiceTable->ReadUintL( TPtrC( WLAN_WEP_KEY1_FORMAT ),
       
   161                                     ( TUint32& ) iKeyFormat[KFirstKey] ) );
       
   162 
       
   163         TRAP( err, wLanServiceTable->ReadUintL( TPtrC( WLAN_WEP_KEY2_FORMAT ),
       
   164                                     ( TUint32& ) iKeyFormat[KSecondKey] ) );
       
   165 
       
   166         TRAP( err, wLanServiceTable->ReadUintL( TPtrC( WLAN_WEP_KEY3_FORMAT ),
       
   167                                     ( TUint32& ) iKeyFormat[KThirdKey] ) );
       
   168 
       
   169         TRAP( err, wLanServiceTable->ReadUintL( TPtrC( WLAN_WEP_KEY4_FORMAT ),
       
   170                                     ( TUint32& ) iKeyFormat[KFourthKey] ) );
       
   171         }
       
   172     else
       
   173         {
       
   174         // silently ignore KErrNotFound. It is caused by incorrect DB,
       
   175         // we are 'repairing it' this way.
       
   176         if ( errorCode != KErrNotFound )
       
   177             {
       
   178             User::Leave( errorCode );
       
   179             }
       
   180         }
       
   181 
       
   182     CleanupStack::PopAndDestroy( wLanServiceTable );  // wLanServiceTable
       
   183     }
       
   184 
       
   185 
       
   186 // ---------------------------------------------------------
       
   187 // CWEPSecuritySettingsImpl::SaveL
       
   188 // ---------------------------------------------------------
       
   189 //
       
   190 void CWEPSecuritySettingsImpl::SaveL( TUint32 aIapId, 
       
   191                                       CCommsDatabase& aCommsDb ) const
       
   192     {
       
   193     CCommsDbTableView* wLanServiceTable;
       
   194 
       
   195     // Caller MUST initiate a transaction, WE WILL NOT.
       
   196 
       
   197     wLanServiceTable = aCommsDb.OpenViewMatchingUintLC( 
       
   198                     TPtrC( WLAN_SERVICE ), TPtrC( WLAN_SERVICE_ID ), aIapId );
       
   199     TInt errorCode = wLanServiceTable->GotoFirstRecord();
       
   200 
       
   201     if ( errorCode == KErrNone )
       
   202         {
       
   203         wLanServiceTable->UpdateRecord();
       
   204         }
       
   205     else
       
   206         {
       
   207         TUint32 dummyUid( KUidNone );
       
   208         User::LeaveIfError( wLanServiceTable->InsertRecord( dummyUid ) );
       
   209 
       
   210         // Save link to LAN service
       
   211         wLanServiceTable->WriteUintL( TPtrC( WLAN_SERVICE_ID ), aIapId );
       
   212         }
       
   213 
       
   214     // Save index of key in use
       
   215     wLanServiceTable->WriteUintL( TPtrC( WLAN_WEP_INDEX ), 
       
   216                                  ( TUint32& ) iKeyInUse );
       
   217 
       
   218     // Save index of key in use
       
   219     wLanServiceTable->WriteUintL( TPtrC( NU_WLAN_AUTHENTICATION_MODE ), 
       
   220                                  ( TUint32& ) iAuthentication );
       
   221 
       
   222     // Save first WEP key
       
   223     wLanServiceTable->WriteTextL( TPtrC( NU_WLAN_WEP_KEY1 ), 
       
   224                                   iKeyData[KFirstKey] );
       
   225 
       
   226     // Save second WEP key
       
   227     wLanServiceTable->WriteTextL( TPtrC( NU_WLAN_WEP_KEY2 ), 
       
   228                                   iKeyData[KSecondKey] );
       
   229 
       
   230     // Save third WEP key
       
   231     wLanServiceTable->WriteTextL( TPtrC( NU_WLAN_WEP_KEY3 ), 
       
   232                                   iKeyData[KThirdKey] );
       
   233 
       
   234     // Save fourth WEP key
       
   235     wLanServiceTable->WriteTextL( TPtrC( NU_WLAN_WEP_KEY4 ), 
       
   236                                   iKeyData[KFourthKey] );
       
   237 
       
   238     // Save the format of the keys
       
   239     wLanServiceTable->WriteUintL( TPtrC( WLAN_WEP_KEY1_FORMAT ), 
       
   240                                  ( TUint32& ) iKeyFormat[KFirstKey] );
       
   241 
       
   242     wLanServiceTable->WriteUintL( TPtrC( WLAN_WEP_KEY2_FORMAT ), 
       
   243                                  ( TUint32& ) iKeyFormat[KSecondKey] );
       
   244 
       
   245     wLanServiceTable->WriteUintL( TPtrC( WLAN_WEP_KEY3_FORMAT ), 
       
   246                                  ( TUint32& ) iKeyFormat[KThirdKey] );
       
   247 
       
   248     wLanServiceTable->WriteUintL( TPtrC( WLAN_WEP_KEY4_FORMAT ), 
       
   249                                  ( TUint32& ) iKeyFormat[KFourthKey] );
       
   250 
       
   251     wLanServiceTable->PutRecordChanges();
       
   252 
       
   253     CleanupStack::PopAndDestroy( wLanServiceTable );  // wLanServiceTable
       
   254     }
       
   255     
       
   256 
       
   257 // ---------------------------------------------------------
       
   258 // CWEPSecuritySettingsImpl::SetLenKeyDataFromText
       
   259 // ---------------------------------------------------------
       
   260 //
       
   261 void CWEPSecuritySettingsImpl::SetLenKeyDataFromText( const TInt aIndex )
       
   262     {
       
   263     const TUint keyDataLength = iKeyData[aIndex].Length();
       
   264 
       
   265     if ( keyDataLength == KKeyDataLength104Bits )
       
   266         {
       
   267         iKeyLength[aIndex] = CWEPSecuritySettings::E104Bits;
       
   268         }
       
   269     else if ( keyDataLength == KKeyDataLength232Bits && iIsWEP256Enabled )
       
   270         {
       
   271         iKeyLength[aIndex] = CWEPSecuritySettings::E232Bits;
       
   272         }
       
   273     else            // if ( aKeyDataLength == KKeyDataLength40Bits ) or any
       
   274         {           //  other case, by default
       
   275         iKeyLength[aIndex] = CWEPSecuritySettings::E40Bits;
       
   276         }
       
   277     }
       
   278 
       
   279 
       
   280 // ---------------------------------------------------------
       
   281 // CWEPSecuritySettingsImpl::IsValid
       
   282 // ---------------------------------------------------------
       
   283 //
       
   284 TBool CWEPSecuritySettingsImpl::IsValid()
       
   285     {
       
   286     return ( KeyData( KeyInUse() )->Length() == 
       
   287                         ExpectedLengthOfKeyData( KeyLength( KeyInUse() ) ) );
       
   288     }
       
   289 
       
   290 
       
   291 //----------------------------------------------------------
       
   292 // CWEPSecuritySettingsImpl::ExpectedLengthOfKeyData
       
   293 //----------------------------------------------------------
       
   294 //
       
   295 TInt CWEPSecuritySettingsImpl::ExpectedLengthOfKeyData( 
       
   296                                CWEPSecuritySettings::TWEPKeyLength aKeyLength )
       
   297     {
       
   298     TInt retVal;
       
   299 
       
   300     switch ( aKeyLength )
       
   301         {
       
   302         case CWEPSecuritySettings::E40Bits:
       
   303             {
       
   304             retVal = KKeyDataLength40Bits;
       
   305             break;
       
   306             }
       
   307 
       
   308         case CWEPSecuritySettings::E104Bits:
       
   309             {
       
   310             retVal = KKeyDataLength104Bits;
       
   311             break;
       
   312             }
       
   313 
       
   314         case CWEPSecuritySettings::E232Bits:
       
   315             {
       
   316             retVal = WEP256Enabled() ? KKeyDataLength232Bits : 0;
       
   317             break;
       
   318             }
       
   319 
       
   320         default:
       
   321             {
       
   322             retVal = 0;
       
   323             break;
       
   324             }
       
   325         }
       
   326 
       
   327     return retVal;
       
   328     }
       
   329  
       
   330 
       
   331 
       
   332 // ---------------------------------------------------------
       
   333 // CWEPSecuritySettingsImpl::SetKeyDataL
       
   334 // ---------------------------------------------------------
       
   335 //
       
   336 TInt CWEPSecuritySettingsImpl::SetKeyDataL( const TInt aElement, 
       
   337                                             const TDesC& aKeyData,
       
   338                                             const TBool aHex )
       
   339     {
       
   340     CWEPSecuritySettings::TWEPKeyFormat keyFormat = aHex ? 
       
   341                                     CWEPSecuritySettings::EHexadecimal : 
       
   342                                     CWEPSecuritySettings::EAscii;
       
   343     SetKeyFormat( aElement, keyFormat );
       
   344 
       
   345     TInt dataLength = aKeyData.Length();
       
   346     if ( dataLength == KKeyDataLength40Bits ||
       
   347          dataLength == KKeyDataLength40Bits / KAsciiHexRatio )
       
   348         {
       
   349         SetKeyLength( aElement, CWEPSecuritySettings::E40Bits );
       
   350         }
       
   351     else if ( dataLength == KKeyDataLength104Bits ||
       
   352          dataLength == KKeyDataLength104Bits / KAsciiHexRatio )
       
   353         {
       
   354         SetKeyLength( aElement, CWEPSecuritySettings::E104Bits );
       
   355         }
       
   356     else if ( dataLength == KKeyDataLength232Bits ||
       
   357          dataLength == KKeyDataLength232Bits / KAsciiHexRatio )
       
   358         {
       
   359         SetKeyLength( aElement, CWEPSecuritySettings::E232Bits );
       
   360         }
       
   361     else
       
   362         {
       
   363         return KErrInvalidLength;
       
   364         }
       
   365 
       
   366     TInt expectedLength = ExpectedLengthOfKeyData( KeyLength( aElement ) );
       
   367 
       
   368     if ( keyFormat == CWEPSecuritySettings::EAscii )
       
   369         {
       
   370         expectedLength /= KAsciiHexRatio; //Ascii key is half the length of Hex
       
   371         }
       
   372 
       
   373     HBufC8* buf8 = HBufC8::NewL( dataLength );
       
   374     CleanupStack::PushL( buf8 );
       
   375     buf8->Des().Copy( aKeyData ); 
       
   376 
       
   377     TInt errData = VerifyKeyData( *buf8, expectedLength, 
       
   378                                   KeyFormat( aElement ) );
       
   379     if ( errData == KErrNone )
       
   380         {
       
   381         if ( aHex )
       
   382             {
       
   383             SetKeyData( aElement, buf8->Des() );
       
   384             }
       
   385         else
       
   386             {
       
   387             HBufC8* buf8Conv = HBufC8::NewL( dataLength * KAsciiHexRatio );
       
   388                                 // Ascii key is half the length of Hex
       
   389             ConvertAsciiToHex( buf8->Des(), buf8Conv );
       
   390             SetKeyData( aElement, buf8Conv->Des() );
       
   391             delete buf8Conv;
       
   392             }
       
   393         }
       
   394 
       
   395     CleanupStack::PopAndDestroy( buf8 ); // buf8
       
   396 
       
   397     return errData;
       
   398     }
       
   399 
       
   400 
       
   401 //----------------------------------------------------------
       
   402 // CWEPSecuritySettingsImpl::VerifyKeyData
       
   403 //----------------------------------------------------------
       
   404 //
       
   405 TInt CWEPSecuritySettingsImpl::VerifyKeyData( const TDesC8& aTextToTest,
       
   406                                              TInt aLengthOfKeyData,
       
   407                             CWEPSecuritySettings::TWEPKeyFormat aWEPKeyFormat )
       
   408     {
       
   409     TInt err = KErrNone;
       
   410     TInt lengthOfText = aTextToTest.Length();
       
   411 
       
   412     if ( aTextToTest.Length() != aLengthOfKeyData )
       
   413         {
       
   414         err = KErrInvalidLength;
       
   415         }
       
   416     else if ( aWEPKeyFormat == CWEPSecuritySettings::EHexadecimal )
       
   417         {
       
   418         for ( TInt i = 0; i < lengthOfText; i++ )
       
   419             {
       
   420             TChar c ( aTextToTest[i] );
       
   421 
       
   422             if ( !c.IsHexDigit() ) 
       
   423                 {
       
   424                 err = KErrInvalidChar;
       
   425                 break;
       
   426                 }
       
   427             }
       
   428         }
       
   429 
       
   430     return err;
       
   431     }
       
   432 
       
   433 
       
   434 //----------------------------------------------------------
       
   435 // CWEPSecuritySettingsImpl::ConvertAsciiToHex
       
   436 //----------------------------------------------------------
       
   437 //
       
   438 void CWEPSecuritySettingsImpl::ConvertAsciiToHex( const TDesC8& aSource, 
       
   439                                                   HBufC8*& aDest )
       
   440 	{
       
   441 	_LIT( hex, "0123456789ABCDEF" );
       
   442 	TInt size = aSource.Size();
       
   443 	TPtr8 ptr = aDest->Des();
       
   444 	for ( TInt ii = 0; ii < size; ii++ )
       
   445 		{
       
   446 		TText8 ch = aSource[ii];
       
   447 		ptr.Append( hex()[(ch/16)&0x0f] );
       
   448 		ptr.Append( hex()[ch&0x0f] );
       
   449 		}
       
   450 	}
       
   451 
       
   452 
       
   453 // ---------------------------------------------------------
       
   454 // CWEPSecuritySettingsImpl::LoadL
       
   455 // ---------------------------------------------------------
       
   456 //
       
   457 void CWEPSecuritySettingsImpl::LoadL( TUint32 aIapId, 
       
   458                                       CMDBSession& aSession )
       
   459     {
       
   460     
       
   461     if ( aIapId == KUidNone )
       
   462         {
       
   463         return;
       
   464         }
       
   465     
       
   466     // Load WLAN service table
       
   467     // first get WLAN table id
       
   468     CMDBGenericRecord* generic = static_cast<CMDBGenericRecord*>
       
   469         ( CCDRecordBase::RecordFactoryL( 0 ) );
       
   470     CleanupStack::PushL( generic );    
       
   471     generic->InitializeL( TPtrC( WLAN_SERVICE ), NULL );
       
   472     generic->LoadL( aSession );
       
   473     TMDBElementId wlanTableId = generic->TableId();
       
   474     
       
   475     CMDBField<TUint32>* sidField = static_cast<CMDBField<TUint32>*>
       
   476                                     (generic->GetFieldByIdL( KCDTIdWlanServiceId));
       
   477     
       
   478     // prime with service id                
       
   479     *sidField = aIapId;
       
   480 
       
   481     if( generic->FindL( aSession) )
       
   482         {
       
   483         // get the values
       
   484         CMDBField<TUint>* keyInUseField = static_cast<CMDBField<TUint>*>
       
   485                             ( generic->GetFieldByIdL( KCDTIdWlanWepIndex ) );
       
   486         ( TUint32& )iKeyInUse = *keyInUseField;
       
   487         CMDBField<TUint>* authenticationField = static_cast<CMDBField<TUint>*>
       
   488                             ( generic->GetFieldByIdL( KCDTIdWlanAuthMode ) );
       
   489         ( TUint32& )iAuthentication = *authenticationField;
       
   490         
       
   491         CMDBField<TDesC8>* wepKey1Field = static_cast<CMDBField<TDesC8>*>
       
   492                         ( generic->GetFieldByIdL( KCDTIdWlanWepKey1 ) );
       
   493         iKeyData[ KFirstKey ] = *wepKey1Field;
       
   494         SetLenKeyDataFromText( KFirstKey );
       
   495         
       
   496         CMDBField<TDesC8>* wepKey2Field = static_cast<CMDBField<TDesC8>*>
       
   497                         ( generic->GetFieldByIdL( KCDTIdWlanWepKey2 ) );
       
   498         iKeyData[ KSecondKey ] = *wepKey2Field;
       
   499         SetLenKeyDataFromText( KSecondKey );
       
   500         
       
   501         CMDBField<TDesC8>* wepKey3Field = static_cast<CMDBField<TDesC8>*>
       
   502                         ( generic->GetFieldByIdL( KCDTIdWlanWepKey3 ) );
       
   503         iKeyData[ KThirdKey ] = *wepKey3Field;
       
   504         SetLenKeyDataFromText( KThirdKey );
       
   505         
       
   506         CMDBField<TDesC8>* wepKey4Field = static_cast<CMDBField<TDesC8>*>
       
   507                         ( generic->GetFieldByIdL( KCDTIdWlanWepKey4 ) );
       
   508         iKeyData[ KFourthKey ] = *wepKey4Field;
       
   509         SetLenKeyDataFromText( KFourthKey );
       
   510         
       
   511         CMDBField<TUint>* formatKey1Field = static_cast<CMDBField<TUint>*>
       
   512                             ( generic->GetFieldByIdL( KCDTIdWlanFormatKey1 ) );
       
   513         ( TUint32& )iKeyFormat[ KFirstKey ] = *formatKey1Field;
       
   514         CMDBField<TUint>* formatKey2Field = static_cast<CMDBField<TUint>*>
       
   515                             ( generic->GetFieldByIdL( KCDTIdWlanFormatKey2 ) );
       
   516         ( TUint32& )iKeyFormat[ KSecondKey ] = *formatKey2Field;
       
   517         CMDBField<TUint>* formatKey3Field = static_cast<CMDBField<TUint>*>
       
   518                             ( generic->GetFieldByIdL( KCDTIdWlanFormatKey3 ) );
       
   519         ( TUint32& )iKeyFormat[ KThirdKey ] = *formatKey3Field;
       
   520         CMDBField<TUint>* formatKey4Field = static_cast<CMDBField<TUint>*>
       
   521                             ( generic->GetFieldByIdL( KCDTIdWlanFormatKey4 ) );
       
   522         ( TUint32& )iKeyFormat[ KFourthKey ] = *formatKey4Field;
       
   523         }
       
   524 
       
   525     CleanupStack::PopAndDestroy( generic );
       
   526     
       
   527     }
       
   528     
       
   529 
       
   530 // ---------------------------------------------------------
       
   531 // CWEPSecuritySettingsImpl::SaveL
       
   532 // ---------------------------------------------------------
       
   533 //
       
   534 void CWEPSecuritySettingsImpl::SaveL( TUint32 aIapId, 
       
   535                                       CMDBSession& aSession ) const
       
   536     {
       
   537     const TInt KRetryWait = 100000;    // Wait time between retries in TTimeIntervalMicroSeconds32
       
   538     const TInt KRetryCount = 50;       // Max retry count
       
   539 
       
   540     // Load WLAN service table
       
   541     // first get WLAN table id
       
   542     CMDBGenericRecord* generic = static_cast<CMDBGenericRecord*>
       
   543         ( CCDRecordBase::RecordFactoryL( 0 ) );
       
   544     CleanupStack::PushL( generic );    
       
   545     generic->InitializeL( TPtrC( WLAN_SERVICE ), NULL );
       
   546     generic->LoadL( aSession );
       
   547     TMDBElementId wlanTableId = generic->TableId();
       
   548     
       
   549     CMDBField<TUint32>* sidField = static_cast<CMDBField<TUint32>*>
       
   550                              ( generic->GetFieldByIdL( KCDTIdWlanServiceId ) );
       
   551     
       
   552     // prime with service id                
       
   553     *sidField = aIapId;
       
   554     
       
   555     TBool found = generic->FindL( aSession);
       
   556    
       
   557     // If loading failed, WLAN service record will be 
       
   558     // created and StoreL()-d, otherwise, ModifyL()
       
   559     
       
   560     CMDBField<TUint>* keyInUseField = static_cast<CMDBField<TUint>*>
       
   561                             ( generic->GetFieldByIdL( KCDTIdWlanWepIndex ) );
       
   562     keyInUseField->SetL( iKeyInUse );
       
   563 
       
   564     CMDBField<TUint>* authenticationField = static_cast<CMDBField<TUint>*>
       
   565                             ( generic->GetFieldByIdL( KCDTIdWlanAuthMode ) );
       
   566     authenticationField->SetL( iAuthentication );
       
   567     
       
   568     CMDBField<TDesC8>* wepKey1Field = static_cast<CMDBField<TDesC8>*>
       
   569                         ( generic->GetFieldByIdL( KCDTIdWlanWepKey1 ) );
       
   570     wepKey1Field->SetL( iKeyData[ KFirstKey ] );
       
   571     CMDBField<TDesC8>* wepKey2Field = static_cast<CMDBField<TDesC8>*>
       
   572                         ( generic->GetFieldByIdL( KCDTIdWlanWepKey2 ) );
       
   573     wepKey2Field->SetL( iKeyData[ KSecondKey ] );
       
   574     CMDBField<TDesC8>* wepKey3Field = static_cast<CMDBField<TDesC8>*>
       
   575                         ( generic->GetFieldByIdL( KCDTIdWlanWepKey3 ) );
       
   576     wepKey3Field->SetL( iKeyData[ KThirdKey ] );
       
   577     CMDBField<TDesC8>* wepKey4Field = static_cast<CMDBField<TDesC8>*>
       
   578                         ( generic->GetFieldByIdL( KCDTIdWlanWepKey4 ) );
       
   579     wepKey4Field->SetL( iKeyData[ KFourthKey ] );
       
   580     
       
   581     CMDBField<TUint>* formatKey1Field = static_cast<CMDBField<TUint>*>
       
   582                             ( generic->GetFieldByIdL( KCDTIdWlanFormatKey1 ) );
       
   583     formatKey1Field->SetL( iKeyFormat[ KFirstKey ] );
       
   584     CMDBField<TUint>* formatKey2Field = static_cast<CMDBField<TUint>*>
       
   585                             ( generic->GetFieldByIdL( KCDTIdWlanFormatKey2 ) );
       
   586     formatKey2Field->SetL( iKeyFormat[ KSecondKey ] );
       
   587     CMDBField<TUint>* formatKey3Field = static_cast<CMDBField<TUint>*>
       
   588                             ( generic->GetFieldByIdL( KCDTIdWlanFormatKey3 ) );
       
   589     formatKey3Field->SetL( iKeyFormat[ KThirdKey ] );
       
   590     CMDBField<TUint>* formatKey4Field = static_cast<CMDBField<TUint>*>
       
   591                             ( generic->GetFieldByIdL( KCDTIdWlanFormatKey4 ) );
       
   592     formatKey4Field->SetL( iKeyFormat[ KFourthKey ] );
       
   593     
       
   594     TInt error( KErrNone );
       
   595     
       
   596     // Saving changes
       
   597     for ( TInt i( 0 ); i < KRetryCount; i++ )
       
   598         {
       
   599         
       
   600         // If table existed modify it
       
   601         if( found )
       
   602             {
       
   603             TRAP( error, generic->ModifyL( aSession ) );
       
   604             }
       
   605                    
       
   606         // Otherwise store a new record
       
   607         else
       
   608             {
       
   609             generic->SetRecordId( KCDNewRecordRequest );
       
   610             TRAP( error, generic->StoreL( aSession ) );
       
   611             }
       
   612                   
       
   613         // If operation failed with KErrLocked, we'll retry.
       
   614         if ( KErrLocked == error )
       
   615             {
       
   616             User::After( KRetryWait );
       
   617             }
       
   618         
       
   619         // Otherwise break the retry loop.
       
   620         else 
       
   621             {
       
   622             break;        
       
   623             }
       
   624         }
       
   625     
       
   626     // If the save operation failed, leave now. 
       
   627     User::LeaveIfError( error );
       
   628 
       
   629     CleanupStack::PopAndDestroy( generic );
       
   630     }
       
   631 
       
   632 
       
   633 // End of File