wlan_bearer/wlandbif_cd/src/wdbifwlansettings.cpp
changeset 0 c40eb8fe8501
child 15 191c8407e577
equal deleted inserted replaced
-1:000000000000 0:c40eb8fe8501
       
     1 /*
       
     2 * Copyright (c) 2002-2004 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Handles the custom WLANServiceTable  in CommsDat
       
    15 *
       
    16 */
       
    17 
       
    18 /*
       
    19 * %version: 12 %
       
    20 */
       
    21 
       
    22 //  INCLUDE FILES
       
    23 
       
    24 #include <e32svr.h>
       
    25 #include <commdb.h>
       
    26 #include <commsdattypeinfov1_1.h>
       
    27 #include "wdbifwlansettings.h"
       
    28 #include "wdbifwlandefs.h"
       
    29 #include "gendebug.h"
       
    30 
       
    31 // LOCAL CONSTANTS
       
    32 
       
    33 _LIT( KGenericTable, "WLANServiceTable");
       
    34 
       
    35 const int KMaxEapDataBufLength = 128;
       
    36 const int KOldStyleMaxEapTypeValue = 100;
       
    37 
       
    38 // ---------------------------------------------------------
       
    39 // Class CWlanSettings
       
    40 // ---------------------------------------------------------
       
    41 
       
    42 // MEMBER FUNCTIONS
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CWLanSettings::CWLanSettings()
       
    46 // C++ default constructor can NOT contain any code, that
       
    47 // might leave.
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 EXPORT_C CWLanSettings::CWLanSettings() : iCurrentRecord( KErrNotFound),
       
    51                                            iWLANRecord( 0),
       
    52                                            iDBSession( 0)
       
    53     {
       
    54     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::CWlanSettings()")));    
       
    55     }
       
    56 
       
    57 // Destructor
       
    58 //
       
    59 EXPORT_C CWLanSettings::~CWLanSettings() 
       
    60     {
       
    61     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::~CWlanSettings()")));    
       
    62     delete iDBSession;
       
    63     delete iWLANRecord;
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------
       
    67 // TInt CWLanSettings::Connect()
       
    68 // Connects to the CommsDat storage server
       
    69 // Wraps around leavable function DoConnectL
       
    70 // returns KErrNone on success
       
    71 // ---------------------------------------------------------
       
    72 //
       
    73 EXPORT_C TInt CWLanSettings::Connect()
       
    74     {
       
    75     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::Connect")));
       
    76 
       
    77     TRAPD(err,DoConnectL());
       
    78     return err;
       
    79     }
       
    80 
       
    81 // ---------------------------------------------------------
       
    82 // void CWLanSettings::Disconnect()
       
    83 // Disconnects from the CommsDat storage server
       
    84 // ---------------------------------------------------------
       
    85 //
       
    86 EXPORT_C void CWLanSettings::Disconnect()
       
    87     {
       
    88     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::Disconnect")));
       
    89     
       
    90     delete iDBSession;
       
    91     iDBSession = NULL;
       
    92     
       
    93     delete iWLANRecord;
       
    94     iWLANRecord = NULL;
       
    95     
       
    96     iCurrentRecord = KErrNotFound;
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------
       
   100 // void CWLanSettings::GetDataL( SWLANSettings& aSettings)
       
   101 // Reads data from a WLANServiceTable record which has previously
       
   102 // been fetched (= current record) from the CommsDat.
       
   103 // returns settings in aSettings
       
   104 // ---------------------------------------------------------
       
   105 //
       
   106 void CWLanSettings::GetDataL( SWLANSettings& aSettings)
       
   107     {
       
   108     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::GetDataL start")));
       
   109     
       
   110     ClearWepKeys( aSettings);
       
   111     
       
   112     aSettings.Id = iWLANRecord->RecordId();
       
   113 
       
   114     aSettings.Name = *((CMDBField<TDesC>*)iWLANRecord->GetFieldByIdL(KCDTIdRecordName));
       
   115 
       
   116     aSettings.ServiceID = *((CMDBField<TUint32>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanServiceId));
       
   117     
       
   118     aSettings.ConnectionMode = *((CMDBField<TUint32>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanConnMode));
       
   119     
       
   120     aSettings.SSID = *((CMDBField<TDesC>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanSSID));
       
   121     
       
   122     // new
       
   123     aSettings.UsedSSID = *((CMDBField<TDesC>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanUsedSSID));
       
   124     
       
   125     aSettings.ScanSSID = *((CMDBField<TBool>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanScanSSID));
       
   126     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::GetDataL ScanSSID = %d"),aSettings.ScanSSID));
       
   127     
       
   128     aSettings.ChannelID = *((CMDBField<TUint32>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanChannelID));
       
   129     // end new
       
   130 
       
   131     aSettings.AuthenticationMode = EWepAuthModeOpen; // defaults to open (in case of wep, the value will be read from db later)
       
   132     aSettings.EnableWpaPsk = EFalse;
       
   133     aSettings.WPAKeyLength = 0;
       
   134 
       
   135     aSettings.SecurityMode = *((CMDBField<TUint32>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanSecMode));
       
   136     
       
   137     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::GetDataL, SecurityMode = %d"),aSettings.SecurityMode));
       
   138 
       
   139     if( aSettings.SecurityMode == Wep)
       
   140         {    
       
   141         // authentication mode can be != open only when WEP in use
       
   142         aSettings.AuthenticationMode = *((CMDBField<TUint32>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanAuthMode));
       
   143 
       
   144         aSettings.WepIndex = *((CMDBField<TUint32>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanWepIndex));
       
   145         ReadWepKeysL(aSettings);
       
   146         TraceDump(WARNING_LEVEL,(_L("CWlanSettings::GetDataL, Wep keys succesfully read")));
       
   147         }
       
   148     else if( aSettings.SecurityMode > Wep)
       
   149         {
       
   150         aSettings.WPAKeyLength = *((CMDBField<TUint32>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanWpaKeyLength));
       
   151         aSettings.EnableWpaPsk = *((CMDBField<TUint32>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanEnableWpaPsk));
       
   152         aSettings.PresharedKeyFormat = EWlanPresharedKeyFormatAscii;
       
   153         aSettings.WPAPreSharedKey.Zero();
       
   154         CMDBField<TDesC8>* wpaKey = static_cast<CMDBField<TDesC8>*>(iWLANRecord->GetFieldByIdL(KCDTIdWlanWpaPreSharedKey));
       
   155          
       
   156         aSettings.WPAPreSharedKey.Append( *wpaKey);
       
   157         
       
   158         // In WPA-PSK case the preshared key can be either 8 - 63 characters in ASCII
       
   159         // or 64 characters in hex.
       
   160         //
       
   161         // In WAPI case the preshared key format is stored in WEP key #1 format field.
       
   162         if( aSettings.EnableWpaPsk )
       
   163             {
       
   164             if( aSettings.SecurityMode == Wapi )
       
   165                 {
       
   166                 TRAP_IGNORE(
       
   167                     aSettings.PresharedKeyFormat = *(CMDBField<TUint32>*)iWLANRecord->GetFieldByIdL( KCDTIdWlanFormatKey1 ) ); 
       
   168                 }
       
   169             else if( aSettings.WPAKeyLength == KWLMMaxWpaPskLength )
       
   170                 {
       
   171                 aSettings.PresharedKeyFormat = EWlanPresharedKeyFormatHex;
       
   172                 }
       
   173             }            
       
   174         }
       
   175     
       
   176     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::GetDataL end")));
       
   177     }
       
   178     
       
   179 
       
   180 
       
   181 void CWLanSettings::ClearWepKeys( SWLANSettings &aSettings)
       
   182     {
       
   183     // it is enough to set the keylengths to zero, the key is not attempted to read if length == 0
       
   184     aSettings.WepKey1.KeyLength = 0;    
       
   185     aSettings.WepKey2.KeyLength = 0;
       
   186     aSettings.WepKey3.KeyLength = 0;
       
   187     aSettings.WepKey4.KeyLength = 0;
       
   188     }
       
   189 
       
   190 // ---------------------------------------------------------
       
   191 // void CWLanSettings::WriteDataL( SWLANSettings& aSettings)
       
   192 // Writes data from aSettings to a WLANServiceTable record which has 
       
   193 // previously been fetched (or newly created) from the CommsDat.
       
   194 // ---------------------------------------------------------
       
   195 //
       
   196 void CWLanSettings::WriteDataL(SWLANSettings& aSettings)
       
   197     {
       
   198     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::WriteDataL start")));
       
   199 
       
   200     CMDBField<TDesC>* textField = (CMDBField<TDesC>*)iWLANRecord->GetFieldByIdL(KCDTIdRecordName);
       
   201     textField->SetMaxLengthL(aSettings.Name.Length());
       
   202     textField->SetL( aSettings.Name);
       
   203     
       
   204     *((CMDBField<TUint32>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanServiceId)) = aSettings.ServiceID;
       
   205     
       
   206     *((CMDBField<TUint32>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanConnMode)) = aSettings.ConnectionMode;
       
   207 
       
   208     textField = (CMDBField<TDesC>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanSSID);
       
   209     textField->SetMaxLengthL( aSettings.SSID.Length());
       
   210     textField->SetL( aSettings.SSID);
       
   211         
       
   212     *((CMDBField<TUint32>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanAuthMode)) = aSettings.AuthenticationMode;
       
   213        
       
   214     *((CMDBField<TUint32>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanSecMode)) = aSettings.SecurityMode;
       
   215 
       
   216     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::WriteDataL, SecurityMode = %d"),aSettings.SecurityMode));
       
   217 
       
   218     if( aSettings.SecurityMode == Wep)
       
   219         {
       
   220         *((CMDBField<TUint32>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanWepIndex)) = aSettings.WepIndex;
       
   221         
       
   222         //
       
   223         // never write wep keys
       
   224         //
       
   225         //WriteWepKeysL( aSettings);
       
   226         }
       
   227     else if( aSettings.SecurityMode > Wep)
       
   228         {
       
   229         //
       
   230         // never write wpa psk key
       
   231         //
       
   232         /*
       
   233         TBuf8<KWlanWpaPskLength> buf;
       
   234         
       
   235         TInt len = aSettings.WPAPreSharedKey.Length();
       
   236         
       
   237         for( TInt i=0; i<len; i++)
       
   238             {
       
   239             buf.Append( aSettings.WPAPreSharedKey[i]);
       
   240             }
       
   241         
       
   242         CMDBField<TDesC8>* binField = (CMDBField<TDesC8>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanWpaPreSharedKey);
       
   243         binField->SetMaxLengthL( len);
       
   244         binField->SetL( buf);
       
   245         */
       
   246         *((CMDBField<TUint32>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanWpaKeyLength)) = aSettings.WPAKeyLength;
       
   247         *((CMDBField<TUint32>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanEnableWpaPsk)) = aSettings.EnableWpaPsk;
       
   248         }
       
   249     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::WriteDataL end")));
       
   250     }
       
   251 
       
   252 
       
   253 // ---------------------------------------------------------
       
   254 // TInt CWLanSettings::GetWlanSettings(TUint32 aId,SWLANSettings &aSettings)
       
   255 // Gets a WLANServiceTable record from CommsDat with record ID aId
       
   256 // writes record data to aSettings; the record ID of the fetched record is set as the 
       
   257 // current record
       
   258 // returns KErrNone on success
       
   259 // ---------------------------------------------------------
       
   260 //
       
   261 EXPORT_C TInt CWLanSettings::GetWlanSettings(TUint32 aId,SWLANSettings &aSettings)
       
   262     {
       
   263     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::GetWlanSettings %d"),aId));
       
   264     
       
   265     TRAPD( err, CreateWLANServiceRecordL( aId, EWlanRecordId));
       
   266     
       
   267     if( !err)
       
   268         {
       
   269         TRAP( err, GetDataL( aSettings));
       
   270         if( !err)
       
   271             {
       
   272             iCurrentRecord = aId;    
       
   273             }
       
   274         }
       
   275         
       
   276     return err;
       
   277     }
       
   278 
       
   279 // ---------------------------------------------------------
       
   280 // TInt CWLanSettings::GetWlanSettingsForService(TUint32 aId,SWLANSettings &aSettings)
       
   281 // Gets a WLANServiceTable record from CommsDat with service ID aId
       
   282 // writes record data to aSettings; the record ID of the fetched record is set as the 
       
   283 // current record
       
   284 // returns KErrNone on success
       
   285 // ---------------------------------------------------------
       
   286 //
       
   287 EXPORT_C TInt CWLanSettings::GetWlanSettingsForService(TUint32 aId,SWLANSettings &aSettings)
       
   288     {
       
   289     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::GetWlanSettingsForService %d"),aId));
       
   290     
       
   291     TRAPD( err, CreateWLANServiceRecordL( aId, EWlanServiceId));
       
   292     
       
   293     if( !err)
       
   294         {
       
   295         TRAP( err, GetDataL( aSettings));
       
   296         if( !err)
       
   297             {
       
   298             iCurrentRecord = aSettings.Id;        
       
   299             }
       
   300         }
       
   301         
       
   302     return err;
       
   303     }
       
   304 
       
   305 // ---------------------------------------------------------
       
   306 // TInt CWLanSettings::GetWlanSettingsForIap( const TUint32 aId, SWLANSettings &aSettings )
       
   307 // Gets a WLANServiceTable record from CommsDat with IAP ID aId
       
   308 // writes record data to aSettings; the record ID of the fetched record is set as the 
       
   309 // current record
       
   310 // returns KErrNone on success
       
   311 // ---------------------------------------------------------
       
   312 //
       
   313 EXPORT_C TInt CWLanSettings::GetWlanSettingsForIap(TUint32 aId,SWLANSettings &aSettings)
       
   314     {
       
   315     TraceDump( WARNING_LEVEL, ( _L("CWlanSettings::GetWlanSettingsForIap %d"), aId ) );
       
   316 
       
   317     TRAPD( err, CreateWLANServiceRecordL( aId, EWlanIapId ) );
       
   318     if ( !err )
       
   319         {
       
   320         TRAP( err, GetDataL( aSettings ) );
       
   321         if ( !err )
       
   322             {
       
   323             iCurrentRecord = aSettings.Id;
       
   324             }
       
   325         }
       
   326 
       
   327     return err;
       
   328     }
       
   329 
       
   330 // ---------------------------------------------------------
       
   331 // TInt CWLanSettings::GetFirstSettings( SWLANSettings &aSettings )
       
   332 // 
       
   333 // Not implemented (never used)
       
   334 //
       
   335 // returns KErrNotSupported 
       
   336 // ---------------------------------------------------------
       
   337 //
       
   338 EXPORT_C TInt CWLanSettings::GetFirstSettings(SWLANSettings& /*aSettings*/) // implemented if needed
       
   339     {
       
   340     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::GetFirstSettings")));
       
   341 
       
   342     return KErrNotSupported;
       
   343     }
       
   344 
       
   345 // ---------------------------------------------------------
       
   346 // TInt CWLanSettings::GetNextSettings( SWLANSettings &aSettings)
       
   347 // 
       
   348 // Not implemented (never used)
       
   349 //
       
   350 // returns KErrNotSupported 
       
   351 // ---------------------------------------------------------
       
   352 //
       
   353 EXPORT_C TInt CWLanSettings::GetNextSettings(SWLANSettings& /*aSettings*/) // implemented if needed
       
   354     {
       
   355     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::GetNextSettings")));
       
   356 
       
   357     return KErrNotSupported;
       
   358     }
       
   359 
       
   360 // ---------------------------------------------------------
       
   361 // TInt CWLanSettings::AddWlanTable( )
       
   362 // 
       
   363 // Not implemented (WLAN table is created by UI)
       
   364 //
       
   365 // leaves with KErrNotSupported 
       
   366 // ---------------------------------------------------------
       
   367 //
       
   368 EXPORT_C void CWLanSettings::AddWlanTableL()
       
   369     {
       
   370     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::AddWlanTableL")));
       
   371     
       
   372     User::Leave( KErrNotSupported);    
       
   373     }
       
   374 
       
   375 
       
   376 // ---------------------------------------------------------
       
   377 // TInt CWLanSettings::WriteWlanSettings( SWLANSettings &aSettings)
       
   378 // Writes settings from aSettings to a WLANServiceTable record in CommsDat
       
   379 // If aSettings.Id == 0, creates a new record, given new record ID 
       
   380 // is returned in aSettings.Id; otherwise modifies an existing record
       
   381 // returns KErrNone on success
       
   382 // ---------------------------------------------------------
       
   383 //
       
   384 EXPORT_C TInt CWLanSettings::WriteWlanSettings( SWLANSettings& aSettings)
       
   385     {
       
   386     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::WriteWlanSettings start")));
       
   387 
       
   388     TInt id = ( aSettings.Id == 0 ? KCDNewRecordRequest : aSettings.Id);
       
   389                                             
       
   390     TRAPD( err, CreateWLANServiceRecordL( id, EWlanRecordId));
       
   391     
       
   392     if( !err)
       
   393         {
       
   394         if( aSettings.Id == 0) // create new
       
   395             {  // store first (placeholder for a new generic wlan record)
       
   396             TraceDump(WARNING_LEVEL,(_L("CWlanSettings::WriteWlanSettings() creating new record")));
       
   397             TRAP( err, iWLANRecord->StoreL( *iDBSession)); 
       
   398             if( err)
       
   399                 {
       
   400                 return err;
       
   401                 }
       
   402             aSettings.Id = iWLANRecord->RecordId(); // return new id to caller    
       
   403             TraceDump(WARNING_LEVEL,(_L("CWlanSettings::WriteWlanSettings() new record id %d"),aSettings.Id));
       
   404             }
       
   405             
       
   406         TRAP( err, WriteDataL( aSettings)); // write from SWLANSettings to iWLANRecord
       
   407         if( err)
       
   408             {
       
   409             TRAP_IGNORE( iWLANRecord->DeleteL( *iDBSession)); // get rid of the placeholder
       
   410             TraceDump(WARNING_LEVEL,(_L("CWlanSettings::WriteWlanSettings(), error writing to record; nothing stored")));
       
   411             return err;
       
   412             }
       
   413                 // modify the placeholder or an existing record
       
   414         TRAP( err, iWLANRecord->ModifyL( *iDBSession));
       
   415         }
       
   416     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::WriteWlanSettings end")));
       
   417     return err;
       
   418     }
       
   419 
       
   420 
       
   421 // ---------------------------------------------------------
       
   422 // void CWLanSettings::ReadWepKeysL( SWLANSettings &aSettings)
       
   423 // Reads WEP keys from current WLANServiceTable record; the
       
   424 // record from which data is read must be fetched from CommsDat
       
   425 // before calling ReadWepKeysL()
       
   426 // ---------------------------------------------------------
       
   427 //
       
   428 
       
   429 
       
   430 void CWLanSettings::ReadWepKeysL( SWLANSettings &aSettings)
       
   431     {
       
   432     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::ReadWepKeysL start")));
       
   433 
       
   434     // 1    
       
   435     CMDBField<TDesC8>* wepKey = static_cast<CMDBField<TDesC8>*>(iWLANRecord->GetFieldByIdL(KCDTIdWlanWepKey1));
       
   436     UnPackWepKey( wepKey->GetL(), aSettings.WepKey1, 0);
       
   437 
       
   438     // 2
       
   439     wepKey = static_cast<CMDBField<TDesC8>*>(iWLANRecord->GetFieldByIdL(KCDTIdWlanWepKey2));
       
   440     UnPackWepKey( wepKey->GetL(), aSettings.WepKey2, 1);
       
   441 
       
   442     // 3
       
   443     wepKey = static_cast<CMDBField<TDesC8>*>(iWLANRecord->GetFieldByIdL(KCDTIdWlanWepKey3));
       
   444     UnPackWepKey( wepKey->GetL(), aSettings.WepKey3, 2);
       
   445 
       
   446     // 4
       
   447     wepKey = static_cast<CMDBField<TDesC8>*>(iWLANRecord->GetFieldByIdL(KCDTIdWlanWepKey4));
       
   448     UnPackWepKey( wepKey->GetL(), aSettings.WepKey4, 3);
       
   449     
       
   450     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::ReadWepKeysL end")));
       
   451     }
       
   452 
       
   453 
       
   454 // ---------------------------------------------------------
       
   455 // void CWLanSettings::WriteWepKeysL( SWLANSettings &aSettings)
       
   456 // Writes WEP keys from aSettings to current WLANServiceTable record
       
   457 // The record which the data is written to must be fetched from CommsDat
       
   458 // before calling WriteWepKeysL()
       
   459 // ---------------------------------------------------------
       
   460 //
       
   461 void CWLanSettings::WriteWepKeysL( SWLANSettings &aSettings)
       
   462     {
       
   463     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::WriteWepKeys start")));
       
   464 
       
   465     // 1
       
   466     CMDBField<TDesC8>* wepKey = static_cast<CMDBField<TDesC8>*>(iWLANRecord->GetFieldByIdL(KCDTIdWlanWepKey1));
       
   467 
       
   468     ASSERT( aSettings.WepKey1.KeyLength <= KWLMMaxWEPKeyLength);
       
   469     PackWepKeyL( aSettings.WepKey1, wepKey);
       
   470     
       
   471     // 2
       
   472     wepKey = static_cast<CMDBField<TDesC8>*>(iWLANRecord->GetFieldByIdL(KCDTIdWlanWepKey2));
       
   473 
       
   474     ASSERT( aSettings.WepKey2.KeyLength <= KWLMMaxWEPKeyLength);
       
   475     PackWepKeyL( aSettings.WepKey2, wepKey);
       
   476     
       
   477     // 3
       
   478     wepKey = static_cast<CMDBField<TDesC8>*>(iWLANRecord->GetFieldByIdL(KCDTIdWlanWepKey3));
       
   479 
       
   480     ASSERT( aSettings.WepKey3.KeyLength <= KWLMMaxWEPKeyLength);
       
   481     PackWepKeyL( aSettings.WepKey3, wepKey);
       
   482 
       
   483     // 4
       
   484     wepKey = static_cast<CMDBField<TDesC8>*>(iWLANRecord->GetFieldByIdL(KCDTIdWlanWepKey4));
       
   485 
       
   486     ASSERT( aSettings.WepKey4.KeyLength <= KWLMMaxWEPKeyLength);
       
   487     PackWepKeyL( aSettings.WepKey4, wepKey);
       
   488     
       
   489     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::WriteWepKeys end")));
       
   490     }
       
   491 
       
   492 
       
   493 // ---------------------------------------------------------
       
   494 // void CWLanSettings::PackWepKeyL( const TWep& aInputKey, CMDBField<TDesC>& aPackedKey)
       
   495 // Copies wepkey string from wepkey structs TUint8 buffer to a WLANServiceTable
       
   496 // record's WEP key field descriptor
       
   497 // ---------------------------------------------------------
       
   498 //
       
   499 void CWLanSettings::PackWepKeyL( const TWep& aInputKey, CMDBField<TDesC8>* aPackedKey)
       
   500     {
       
   501     TraceDump(ERROR_LEVEL,(_L("CWlanSettings::PackWepKeyL %d"),aInputKey.KeyIndex));
       
   502 
       
   503     if( aInputKey.KeyLength > KWLMMaxWEPKeyLength)
       
   504         {
       
   505         User::Leave( KErrArgument);
       
   506         }
       
   507     
       
   508     TBuf8<KWLMMaxWEPKeyLength> wepBuf;
       
   509     
       
   510     for (TUint i = 0 ; i<aInputKey.KeyLength; i++)
       
   511         {
       
   512         wepBuf.Append(aInputKey.KeyMaterial[i]);
       
   513         }
       
   514     
       
   515     aPackedKey->SetL( wepBuf);
       
   516     }
       
   517 
       
   518 
       
   519 void CWLanSettings::AsciiToHexValue( TUint8& aValue)
       
   520     {
       
   521     // 0-9 = [48(0x30) - 57(0x39)], A-F = [65(0x41) - 70(0x46)], a-f = [97(0x61)-102(0x66)]
       
   522     
       
   523     if( aValue >= '0' && aValue <= '9') // 0..9
       
   524         {
       
   525         aValue -= '0';
       
   526         }
       
   527     else if( aValue >= 'A' && aValue <= 'F')  // A..F
       
   528         {
       
   529         aValue -= ('A' - 10);
       
   530         }
       
   531     else if( aValue >= 'a' && aValue <= 'f') // a..f
       
   532         {
       
   533         aValue -= ('a' - 10);    
       
   534         }
       
   535     }
       
   536 
       
   537 // ---------------------------------------------------------
       
   538 // void CWLanSettings::UnPackWepKeyL( const TDesC& aInputKey, TWep& aUnpackedKey, TInt aKeyIndex )
       
   539 // Copies 8 bit wepkey string to TWep structs TUint8 buffer from a WLANServiceTable
       
   540 // record's WEP key field descriptor
       
   541 // ---------------------------------------------------------
       
   542 //
       
   543 void CWLanSettings::UnPackWepKey( TDesC8& aInputKey, TWep& aUnpackedKey, TInt aKeyIndex )
       
   544     {
       
   545     TraceDump(ERROR_LEVEL,(_L("CWlanSettings::UnPackWepKey %d"),aKeyIndex));
       
   546     
       
   547     TUint length = aInputKey.Length() / 2; // in aInputKey, 0xXY numbers are represented as ascii(X)ascii(Y)
       
   548                                            // -> actual length is half of the length of aInputKey
       
   549     
       
   550     if( length == 0 || length > KWLMMaxWEPKeyLength)
       
   551         {
       
   552         aUnpackedKey.KeyLength = 0;
       
   553         return;
       
   554         }
       
   555     
       
   556     for( TUint i=0, j=0; i<length ; i++)
       
   557         {
       
   558         TUint8 hexMSB = aInputKey[j++];
       
   559         TUint8 hexLSB = aInputKey[j++];
       
   560     
       
   561         AsciiToHexValue( hexMSB);
       
   562         AsciiToHexValue( hexLSB);
       
   563         
       
   564         TUint8 keyMaterial = hexMSB * 0x10 + hexLSB;
       
   565         
       
   566         aUnpackedKey.KeyMaterial[i] = keyMaterial;
       
   567         }
       
   568     aUnpackedKey.KeyLength = length;
       
   569     aUnpackedKey.KeyIndex = aKeyIndex;
       
   570     }
       
   571 
       
   572 
       
   573 // ---------------------------------------------------------
       
   574 // TInt CWLanSettings::DeleteWlanSettings( TUint32 aId)
       
   575 // Deletes a WLANServiceTable record with record ID aId
       
   576 // returns KErrNone on success
       
   577 // ---------------------------------------------------------
       
   578 //
       
   579 EXPORT_C TInt CWLanSettings::DeleteWlanSettings( TUint32 aId)
       
   580     {
       
   581     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::DeleteWlanSettings")));
       
   582     
       
   583     TRAPD( err, CreateWLANServiceRecordL( aId, EWlanRecordId));
       
   584     
       
   585     if( !err)
       
   586         {
       
   587         TRAP( err, iWLANRecord->DeleteL( *iDBSession));
       
   588         }
       
   589     return err;    
       
   590     }
       
   591 
       
   592 
       
   593 // ---------------------------------------------------------
       
   594 // TInt CWLanSettings::DeleteWlanSettingsForService( TUint32 aId)
       
   595 // Deletes a WLANServiceTable record with service ID aId
       
   596 // returns KErrNone on success
       
   597 // ---------------------------------------------------------
       
   598 //
       
   599 EXPORT_C TInt CWLanSettings::DeleteWlanSettingsForService( TUint32 aId)
       
   600     {
       
   601     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::DeleteWlanSettingsForService")));
       
   602     
       
   603     TRAPD( err, CreateWLANServiceRecordL( aId, EWlanServiceId));
       
   604     
       
   605     if( !err)
       
   606         {
       
   607         TRAP( err, iWLANRecord->DeleteL( *iDBSession));
       
   608         }
       
   609 
       
   610     return err;    
       
   611     }
       
   612 
       
   613 
       
   614 // ---------------------------------------------------------
       
   615 // void CWLanSettings::SetEapDataL( const TEapArray &aEaps)
       
   616 // Sets EAP data to current WLANServiceTable record
       
   617 // Format "+123,- 34", + means enabled, - disabled, then id, 
       
   618 // id is always 3 characters for easy parsing
       
   619 //
       
   620 // GetWlanSettings() or GetWlanSettingsForService() must be called prior to
       
   621 // calling SetEapDataL() so that current record is set
       
   622 // ---------------------------------------------------------
       
   623 //
       
   624 EXPORT_C void CWLanSettings::SetEapDataL( const TEapArray &aEaps)
       
   625     {
       
   626     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::SetEapDataL start")));
       
   627 
       
   628     // the behaviour has been that first user calls GetWlanSettingsForService()
       
   629     // and then writes EAP data..
       
   630  
       
   631     if( iCurrentRecord == KErrNotFound)
       
   632         {
       
   633         User::Leave( KErrNotReady);
       
   634         }
       
   635 
       
   636     TInt num( aEaps.Count());
       
   637     
       
   638     if( num == 0)
       
   639         {
       
   640         return;
       
   641         }
       
   642    
       
   643     HBufC16* DbText = HBufC16::NewLC( 5*num); // 5 chars per eap entry + ','
       
   644     TPtr16 ptr( DbText->Des());
       
   645     TBuf16<3+1> UidTmp;   // 4 chars per eap entry
       
   646 
       
   647     ptr = _L("");
       
   648     for( TInt i=0 ; i<num; i++)
       
   649         {
       
   650         UidTmp.Copy( aEaps[i]->UID);
       
   651         if( i > 0)
       
   652             {
       
   653             ptr.Append(',');
       
   654             }
       
   655         ptr.AppendFormat( _L("%c%3s"), aEaps[i]->Enabled ? '+' : '-', UidTmp.PtrZ());
       
   656         }
       
   657     
       
   658     DoSetEapDataL( ptr);
       
   659     
       
   660     CleanupStack::PopAndDestroy(DbText);
       
   661     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::SetEapDataL end")));
       
   662     }
       
   663 
       
   664 
       
   665 EXPORT_C void CWLanSettings::SetEapDataL( RExpandedEapTypeArray &aEapsEnabled, RExpandedEapTypeArray &aEapsDisabled )
       
   666     {
       
   667     TraceDump( WARNING_LEVEL, ( _L("CWlanSettings::SetEapDataL start") ) );
       
   668 
       
   669     // the behaviour has been that first user calls GetWlanSettingsForService()
       
   670     // and then writes EAP data..
       
   671  
       
   672     if ( iCurrentRecord == KErrNotFound )
       
   673         {
       
   674         User::Leave( KErrNotReady );
       
   675         }
       
   676     
       
   677     HBufC8* const DbDataEnabled = HBufC8::NewLC( aEapsEnabled.Count() * KExpandedEapTypeSize ); // 8 bytes per eap entry
       
   678     TPtr8 enabledPtr( DbDataEnabled->Des() );
       
   679     for ( TInt i = 0 ; i < aEapsEnabled.Count(); i++ )
       
   680         {
       
   681 		enabledPtr.Append( aEapsEnabled[i]->EapExpandedType );
       
   682         }
       
   683         
       
   684     HBufC8* const DbDataDisabled = HBufC8::NewLC( aEapsDisabled.Count() * KExpandedEapTypeSize ); // 8 bytes per eap entry
       
   685     TPtr8 disabledPtr( DbDataDisabled->Des() );
       
   686     for ( TInt i = 0 ; i < aEapsDisabled.Count(); i++ )
       
   687         {
       
   688 		disabledPtr.Append( aEapsDisabled[i]->EapExpandedType );
       
   689         }
       
   690 
       
   691     DoSetEapDataL( enabledPtr, disabledPtr );
       
   692     
       
   693     CleanupStack::PopAndDestroy( DbDataDisabled );
       
   694     CleanupStack::PopAndDestroy( DbDataEnabled );
       
   695     
       
   696     TraceDump( WARNING_LEVEL, ( _L("CWlanSettings::SetEapDataL end") ) );
       
   697     }
       
   698 
       
   699 // ---------------------------------------------------------
       
   700 // void CWLanSettings::DoSetEapDataL( TDesC& aData)
       
   701 // Sets EAP data to current WLANServiceTable record
       
   702 // ---------------------------------------------------------
       
   703 //
       
   704 void CWLanSettings::DoSetEapDataL( TDesC& aData)
       
   705     {
       
   706     
       
   707     ASSERT( iCurrentRecord > 0);
       
   708     
       
   709     CMDBField<TDesC>* eapList = (CMDBField<TDesC>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanEaps);
       
   710     eapList->SetMaxLengthL( aData.Length());
       
   711     eapList->SetL( aData);
       
   712     iWLANRecord->ModifyL( *iDBSession);
       
   713 
       
   714     }
       
   715 
       
   716 // ---------------------------------------------------------
       
   717 // void CWLanSettings::DoSetEapDataL( TDesC8& aEnabledData, TDesC8& aDisabledData )
       
   718 // Sets EAP data to current WLANServiceTable record
       
   719 // ---------------------------------------------------------
       
   720 //
       
   721 void CWLanSettings::DoSetEapDataL( TDesC8& aEnabledData, TDesC8& aDisabledData )
       
   722     {
       
   723     ASSERT( iCurrentRecord > 0 );
       
   724     
       
   725     CMDBField<TDesC8>* const eapEnabledList = (CMDBField<TDesC8>*) iWLANRecord->GetFieldByIdL( KCDTIdWlanEapsExtEnabled );
       
   726     eapEnabledList->SetMaxLengthL( aEnabledData.Length() );
       
   727     eapEnabledList->SetL( aEnabledData );
       
   728     
       
   729     CMDBField<TDesC8>* const eapDisabledList = (CMDBField<TDesC8>*) iWLANRecord->GetFieldByIdL( KCDTIdWlanEapsExtDisabled );
       
   730     eapDisabledList->SetMaxLengthL( aDisabledData.Length() );
       
   731     eapDisabledList->SetL( aDisabledData );
       
   732     
       
   733     iWLANRecord->ModifyL( *iDBSession );
       
   734 
       
   735     }
       
   736 
       
   737 // ---------------------------------------------------------
       
   738 // HBufC* CWLanSettings::DoGetEapDataLC()
       
   739 // Gets eap data from current WLANServiceTable record
       
   740 // returns buffer which contains the data
       
   741 // ---------------------------------------------------------
       
   742 //
       
   743 HBufC* CWLanSettings::DoGetEapDataLC()
       
   744     {
       
   745     
       
   746     ASSERT( iCurrentRecord > 0);
       
   747     
       
   748     HBufC* temp = HBufC::NewLC( KMaxEapDataBufLength);
       
   749     
       
   750     TPtr ptr( temp->Des());
       
   751         
       
   752     ptr = *((CMDBField<TDesC>*)iWLANRecord->GetFieldByIdL(KCDTIdWlanEaps));
       
   753     
       
   754     return temp;
       
   755     }
       
   756 
       
   757 // ---------------------------------------------------------
       
   758 // HBufC* CWLanSettings::DoGetEapDataLC( HBufC8*& enabledEapsData, HBufC8*& disabledEapsData )
       
   759 // Gets eap data from current WLANServiceTable record
       
   760 // returns buffer which contains the data
       
   761 // ---------------------------------------------------------
       
   762 //
       
   763 void CWLanSettings::DoGetEapDataLC( HBufC8*& enabledEapsData, HBufC8*& disabledEapsData )
       
   764 	{
       
   765     ASSERT( iCurrentRecord > 0 );
       
   766     
       
   767     enabledEapsData = HBufC8::NewLC( KMaxEapDataBufLength );
       
   768     disabledEapsData = HBufC8::NewLC( KMaxEapDataBufLength );
       
   769 
       
   770     TPtr8 ptr( enabledEapsData->Des() );
       
   771     TPtr8 ptr2( disabledEapsData->Des() );
       
   772     ptr = *( (CMDBField<TDesC8>*) iWLANRecord->GetFieldByIdL( KCDTIdWlanEapsExtEnabled) );
       
   773    // ptr2 = disabledEapsData->Des();
       
   774     ptr2 = *( (CMDBField<TDesC8>*) iWLANRecord->GetFieldByIdL( KCDTIdWlanEapsExtDisabled) );
       
   775 	}
       
   776 
       
   777 // ---------------------------------------------------------
       
   778 // void CWLanSettings::GetEapDataL( TEapArray &aEaps)
       
   779 // Gets EAP data from current WLANServiceTable record
       
   780 // see format in SetEapDataL()
       
   781 //
       
   782 // GetWlanSettings() or GetWlanSettingsForService() must be called prior to
       
   783 // calling GetEapDataL() so that current record is set
       
   784 // ---------------------------------------------------------
       
   785 //
       
   786 EXPORT_C void CWLanSettings::GetEapDataL( TEapArray &aEaps)
       
   787     {
       
   788     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::GetEapDataL start")));
       
   789  
       
   790     if( iCurrentRecord == KErrNotFound)
       
   791         {
       
   792         User::Leave( KErrNotReady);
       
   793         }
       
   794     
       
   795     HBufC* eapData = 0;
       
   796 
       
   797     eapData = DoGetEapDataLC();
       
   798    
       
   799     aEaps.Reset();
       
   800     
       
   801     TUint Length = eapData->Length();
       
   802    
       
   803     if( Length == 0)
       
   804         {
       
   805         CleanupStack::PopAndDestroy( eapData);
       
   806         return;
       
   807         }
       
   808         
       
   809     TPtr16 ptr( eapData->Des());
       
   810     TEap* eapTmp;
       
   811     TUint x = 0;
       
   812     
       
   813     while( x < Length)
       
   814         {
       
   815         eapTmp = new (ELeave) TEap; 
       
   816         if( (ptr[x++]) == '+')
       
   817             {
       
   818             eapTmp->Enabled = ETrue;
       
   819             }
       
   820             
       
   821         else 
       
   822             {
       
   823             eapTmp->Enabled=EFalse;
       
   824             }
       
   825            
       
   826         eapTmp->UID.Zero();
       
   827         eapTmp->UID.Append( ptr[x++]);
       
   828         eapTmp->UID.Append( ptr[x++]);
       
   829         eapTmp->UID.Append( ptr[x++]);
       
   830         eapTmp->UID.Trim();
       
   831         x++; // skip ',' if there is one
       
   832         aEaps.Append( eapTmp); 
       
   833         }
       
   834     CleanupStack::PopAndDestroy( eapData);
       
   835     }
       
   836 // ---------------------------------------------------------
       
   837 // void CWLanSettings::GetEapDataL( RExpandedEapTypeArray &aEapsEnabled, RExpandedEapTypeArray &aEapsDisabled )
       
   838 // Gets EAP data from current WLANServiceTable record
       
   839 // see format in SetEapDataL()
       
   840 //
       
   841 // GetWlanSettings() or GetWlanSettingsForService() must be called prior to
       
   842 // calling GetEapDataL() so that current record is set
       
   843 // ---------------------------------------------------------
       
   844 //
       
   845 EXPORT_C void CWLanSettings::GetEapDataL( RExpandedEapTypeArray &aEapsEnabled, RExpandedEapTypeArray &aEapsDisabled )
       
   846     {
       
   847     TraceDump( WARNING_LEVEL, ( _L("CWlanSettings::GetEapDataL start") ) );
       
   848  
       
   849     if ( iCurrentRecord == KErrNotFound )
       
   850         {
       
   851         User::Leave( KErrNotReady );
       
   852         }
       
   853    
       
   854     aEapsEnabled.Reset();
       
   855     aEapsDisabled.Reset();
       
   856    
       
   857     HBufC* const eapData = DoGetEapDataLC();
       
   858 
       
   859     const TUint Length = eapData->Length();
       
   860    
       
   861     if ( Length == 0 )
       
   862         {
       
   863     	TraceDump(WARNING_LEVEL,(_L("CWlanSettings::start getting data")));
       
   864 
       
   865         // No old-style EAP data
       
   866 		CleanupStack::PopAndDestroy( eapData );
       
   867 		        
       
   868         // Get EAP data as in new format
       
   869 		HBufC8* enabledEapsData;
       
   870 		HBufC8* disabledEapsData;
       
   871 		DoGetEapDataLC( enabledEapsData, disabledEapsData );
       
   872     	TraceDump(WARNING_LEVEL,(_L("CWlanSettings::data taken")));
       
   873 		
       
   874 	    // Array pointer
       
   875 	    TPtr8 ptr( enabledEapsData->Des() );
       
   876     
       
   877 		TInt i = 0;
       
   878 		for ( i = 0; i<(enabledEapsData->Length() / KExpandedEapTypeSize); i++ )
       
   879 			{
       
   880 	        SEapExpandedType* const expandedEap = new (ELeave) SEapExpandedType();
       
   881 	        CleanupStack::PushL(expandedEap);
       
   882 	        expandedEap->EapExpandedType = ptr.Mid( i * KExpandedEapTypeSize, KExpandedEapTypeSize );
       
   883 	        aEapsEnabled.AppendL( expandedEap );
       
   884 			CleanupStack::Pop( expandedEap );
       
   885     		TraceDump(WARNING_LEVEL,(_L("CWlanSettings::inloop1")));
       
   886  			}
       
   887         TraceDump(WARNING_LEVEL,(_L("CWlanSettings::first loop ok")));
       
   888 			
       
   889 		ptr = disabledEapsData->Des();
       
   890 		for ( i = 0; i<(disabledEapsData->Length() / KExpandedEapTypeSize); i++ )
       
   891 			{
       
   892 	        SEapExpandedType* const expandedEap = new (ELeave) SEapExpandedType();
       
   893 	        CleanupStack::PushL(expandedEap);
       
   894 	        expandedEap->EapExpandedType = ptr.Mid( i * KExpandedEapTypeSize, KExpandedEapTypeSize );
       
   895 	        aEapsDisabled.AppendL( expandedEap );
       
   896 			CleanupStack::Pop( expandedEap );
       
   897     		TraceDump(WARNING_LEVEL,(_L("CWlanSettings::inloop2")));
       
   898 			}
       
   899         TraceDump(WARNING_LEVEL,(_L("CWlanSettings::second loop ok")));
       
   900 			    
       
   901 		CleanupStack::PopAndDestroy( disabledEapsData );
       
   902 		CleanupStack::PopAndDestroy( enabledEapsData );
       
   903 		
       
   904 	    TraceDump( WARNING_LEVEL,( _L("CWlanSettings::GetEapDataL end") ) );
       
   905 		return;
       
   906         }
       
   907     
       
   908     else
       
   909     	{
       
   910     	// Convert to new format. This part should be removed after old-atyle EAP list support is not needed anymore
       
   911     	
       
   912     	TraceDump(WARNING_LEVEL,(_L("CWlanSettings::GetEapDataL - old style data")));
       
   913 
       
   914 	    TUint16 x = 0;
       
   915 	    TUint16 newEapValue;
       
   916 	    TBool enableEapType;
       
   917 
       
   918 	    // Array pointer
       
   919 	    const TPtrC16 ptr( eapData->Des() );
       
   920 	    
       
   921 	    while ( (x+3) < Length )
       
   922 	        {
       
   923 	        TBool addToList = ETrue;
       
   924 	        if ( ptr[x] == '+' )
       
   925 	            {
       
   926 	            // Append into enabled list
       
   927 	            enableEapType = ETrue;
       
   928 	            }
       
   929 	        else if ( ptr[x] == '-' )
       
   930 	            {
       
   931 	            // Append into disabled list
       
   932 	        	enableEapType = EFalse;
       
   933 	            }
       
   934 	        else 
       
   935 	            {
       
   936 	            // Unknown value
       
   937 	            TraceDump(WARNING_LEVEL,(_L("CWlanSettings::GetEapDataL - unknown value")));
       
   938 	            enableEapType = EFalse;
       
   939 	            addToList = EFalse;
       
   940 	            }
       
   941 
       
   942             ++x;
       
   943             TChar tens( ptr[x+1] );
       
   944             TChar ones( ptr[x+2] );
       
   945 
       
   946             newEapValue = KOldStyleMaxEapTypeValue;
       
   947 	        
       
   948             if( tens.IsDigit() && ones.IsDigit() )
       
   949                 {
       
   950                 newEapValue = tens.GetNumericValue() * 10 + ones.GetNumericValue();	
       
   951                 }
       
   952 
       
   953             if( addToList && ptr[x] == '0' && newEapValue < KOldStyleMaxEapTypeValue )
       
   954                 {
       
   955                 SEapExpandedType* const expandedEap = new (ELeave) SEapExpandedType();
       
   956                 CleanupStack::PushL( expandedEap );
       
   957 	        
       
   958                 expandedEap->EapExpandedType.Zero();
       
   959 
       
   960                 expandedEap->EapExpandedType.Append( 0xFE );
       
   961 
       
   962                 if ( newEapValue == 99 )
       
   963                     {
       
   964                     // Plain MSCHAPv2
       
   965                     expandedEap->EapExpandedType.Append( 0xFF );
       
   966                     expandedEap->EapExpandedType.Append( 0xFF );
       
   967                     expandedEap->EapExpandedType.Append( 0xFF );
       
   968                     expandedEap->EapExpandedType.Append( 0x00 );
       
   969                     expandedEap->EapExpandedType.Append( 0x00 );
       
   970                     expandedEap->EapExpandedType.Append( 0x00 );
       
   971                     expandedEap->EapExpandedType.Append( 0x1A );
       
   972                     }
       
   973                 else
       
   974                     {
       
   975                     expandedEap->EapExpandedType.Append( 0x00 );
       
   976                     expandedEap->EapExpandedType.Append( 0x00 );
       
   977                     expandedEap->EapExpandedType.Append( 0x00 );
       
   978                     expandedEap->EapExpandedType.Append( 0x00 );
       
   979                     expandedEap->EapExpandedType.Append( 0x00 );
       
   980                     expandedEap->EapExpandedType.Append( 0x00 );
       
   981                     expandedEap->EapExpandedType.Append( newEapValue );
       
   982                     }
       
   983 	        
       
   984                 if ( enableEapType )
       
   985                     {
       
   986                     aEapsEnabled.AppendL( expandedEap );
       
   987                     }
       
   988                 else
       
   989                     {
       
   990                     aEapsDisabled.AppendL( expandedEap );
       
   991                     }
       
   992 	        
       
   993                 CleanupStack::Pop( expandedEap );
       
   994                 }
       
   995                 
       
   996             x += 4;
       
   997             }	        
       
   998 	    
       
   999 	    // Store new-style EAP list
       
  1000 	    HBufC8* const DbDataEnabled = HBufC8::NewLC( aEapsEnabled.Count() * KExpandedEapTypeSize ); // 8 bytes per eap entry
       
  1001 	    TPtr8 enabledPtr( DbDataEnabled->Des() );
       
  1002 	    for ( TInt i = 0 ; i < aEapsEnabled.Count(); i++ )
       
  1003 	        {
       
  1004 			enabledPtr.Append( aEapsEnabled[i]->EapExpandedType );
       
  1005 	        }
       
  1006 	        
       
  1007 	    HBufC8* const DbDataDisabled = HBufC8::NewLC( aEapsDisabled.Count() * KExpandedEapTypeSize ); // 8 bytes per eap entry
       
  1008 	    TPtr8 disabledPtr( DbDataDisabled->Des() );
       
  1009 	    for ( TInt i = 0 ; i < aEapsDisabled.Count(); i++ )
       
  1010 	        {
       
  1011 			disabledPtr.Append( aEapsDisabled[i]->EapExpandedType );
       
  1012 	        }
       
  1013 	    
       
  1014 	    CMDBField<TDesC8>* const eapEnabledList = (CMDBField<TDesC8>*) iWLANRecord->GetFieldByIdL( KCDTIdWlanEapsExtEnabled );
       
  1015 	    eapEnabledList->SetMaxLengthL( enabledPtr.Length() );
       
  1016 	    eapEnabledList->SetL( enabledPtr );
       
  1017 	    
       
  1018 	    CMDBField<TDesC8>* const eapDisabledList = (CMDBField<TDesC8>*) iWLANRecord->GetFieldByIdL( KCDTIdWlanEapsExtDisabled );
       
  1019 	    eapDisabledList->SetMaxLengthL( disabledPtr.Length() );
       
  1020 	    eapDisabledList->SetL( disabledPtr );
       
  1021 	    
       
  1022 	    // Empty old-style EAP list
       
  1023 	    CMDBField<TDesC>* const eapList = (CMDBField<TDesC>*) iWLANRecord->GetFieldByIdL( KCDTIdWlanEaps );
       
  1024 	    eapList->SetMaxLengthL( 0 );
       
  1025 	    
       
  1026 	    iWLANRecord->ModifyL( *iDBSession );
       
  1027 	    
       
  1028 		CleanupStack::PopAndDestroy( DbDataDisabled );
       
  1029 		CleanupStack::PopAndDestroy( DbDataEnabled );
       
  1030 	    
       
  1031 		CleanupStack::PopAndDestroy( eapData );
       
  1032     	}
       
  1033 	
       
  1034     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::GetEapDataL end")));
       
  1035     }
       
  1036 
       
  1037 
       
  1038 // ---------------------------------------------------------
       
  1039 // TInt CWLanSettings::IsEasyWlan( TUint aServiceId, TBool &aEasyWlan)
       
  1040 // Checks if wlan settings with given ServiceId is easywlan iap
       
  1041 // returns KErrNone on success
       
  1042 // ---------------------------------------------------------
       
  1043 //
       
  1044 EXPORT_C TInt CWLanSettings::IsEasyWlan( TUint aServiceId, TBool &aEasyWlan)
       
  1045     {
       
  1046     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::IsEasyWlan %d"),aServiceId));
       
  1047     
       
  1048     aEasyWlan = EFalse;
       
  1049 
       
  1050     TRAPD( err, CreateWLANServiceRecordL( aServiceId, EWlanServiceId));
       
  1051 
       
  1052     if( !err)
       
  1053         {
       
  1054             CMDBField<TDesC>* ssid = NULL;
       
  1055             TRAP(err, ssid = static_cast<CMDBField<TDesC>*>(iWLANRecord->GetFieldByIdL(KCDTIdWlanSSID)));
       
  1056             if (err)
       
  1057                 {
       
  1058             	return err;
       
  1059                 }
       
  1060             
       
  1061             TPtrC pSsid;
       
  1062             TRAP(err, pSsid.Set(ssid->GetL()));
       
  1063             if (err)
       
  1064                 {
       
  1065             	return err;
       
  1066                 }
       
  1067             
       
  1068             if( pSsid.Length() == 0)
       
  1069                 {
       
  1070                 aEasyWlan = ETrue;
       
  1071                 }
       
  1072                 
       
  1073                 
       
  1074         }
       
  1075     return err;
       
  1076     }
       
  1077 
       
  1078 // ----------------------------------------------------------
       
  1079 // void CWLanSettings::GetIAPWlanServicesL( RArray<SWlanIAPId>& aServices)
       
  1080 // Find all WLANServiceTable records which correspond to wlan IAPs 
       
  1081 // returns ID's to both WLANServiceTable records and IAP records in aServices
       
  1082 // ----------------------------------------------------------
       
  1083 //
       
  1084 
       
  1085 EXPORT_C void CWLanSettings::GetIAPWlanServicesL( RArray<SWlanIAPId>& aServices)
       
  1086     {
       
  1087     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::GetIAPWlanServicesL start")));
       
  1088     
       
  1089     CMDBRecordSet<CCDIAPRecord>* iapRecSet = new (ELeave) CMDBRecordSet<CCDIAPRecord>(KCDTIdIAPRecord);
       
  1090     CleanupStack::PushL( iapRecSet);
       
  1091     
       
  1092     CCDIAPRecord* iapPrimer = static_cast<CCDIAPRecord*>
       
  1093                 ( CCDRecordBase::RecordFactoryL( KCDTIdIAPRecord));
       
  1094     CleanupStack::PushL( iapPrimer);
       
  1095 
       
  1096     // prime record set for service type "LANService"
       
  1097     TPtrC servType( KCDTypeNameLANService);
       
  1098     iapPrimer->iServiceType.SetL( servType);
       
  1099     iapRecSet->iRecords.AppendL( iapPrimer);
       
  1100     
       
  1101     CleanupStack::Pop( ); // iapPrimer
       
  1102     iapPrimer = 0;
       
  1103     
       
  1104     TBool found( EFalse);
       
  1105 
       
  1106     // get all iap records with service type "LANService"    
       
  1107     found = iapRecSet->FindL( *iDBSession);
       
  1108     
       
  1109     // find all such WLANServiceTable records that have a ServiceID that match some lanId
       
  1110     if( found)
       
  1111         {
       
  1112         TraceDump(WARNING_LEVEL, (_L("\tfound %d IAPs with LANService type"),iapRecSet->iRecords.Count()));
       
  1113         for( TInt i=0; i<iapRecSet->iRecords.Count(); i++)
       
  1114             {
       
  1115             TRAPD( error, CreateWLANServiceRecordL( (*iapRecSet)[i]->iService, EWlanServiceId));
       
  1116             
       
  1117             if( error == KErrNone)
       
  1118                 {    // found a WLANServiceTable record with ServiceID that matches a LANService record id
       
  1119                 SWlanIAPId id;
       
  1120                 id.iWLANRecordId = iWLANRecord->RecordId();  // WLANServiceTable record ID (not the value of ServiceID of the found record)
       
  1121                 id.iIAPId = (*iapRecSet)[i]->RecordId();    // IAP record id
       
  1122                 User::LeaveIfError( aServices.Append( id)); 
       
  1123                 }
       
  1124             }
       
  1125         }
       
  1126     CleanupStack::PopAndDestroy( ); // iapRecSet
       
  1127     TraceDump(WARNING_LEVEL,(_L("CWlanSettings::GetIAPWlanServicesL end")));
       
  1128     }
       
  1129 
       
  1130 // -----------------------------------------------------------------------------
       
  1131 // CWLanSettings::GetSecondarySsidsForService
       
  1132 // -----------------------------------------------------------------------------
       
  1133 //
       
  1134 EXPORT_C TInt CWLanSettings::GetSecondarySsidsForService(
       
  1135 	TUint32 aId,
       
  1136 	RArray<TWlanSecondarySsid>& aSsids )
       
  1137 	{
       
  1138 	TraceDump( INFO_LEVEL, ( _L( "CWlanSettings::GetSecondarySsidsForService()" ) ) );
       
  1139 
       
  1140 	aSsids.Reset();
       
  1141 
       
  1142     /**
       
  1143     * This is done using the old commdb interface because Symbian
       
  1144     * doesn't have a class definition for WLAN Secondary SSID table.
       
  1145     */
       
  1146     CCommsDatabase* db = NULL;
       
  1147     TRAPD( ret, db = CCommsDatabase::NewL() );
       
  1148     if ( ret != KErrNone )
       
  1149         {
       
  1150         TraceDump( ERROR_LEVEL,
       
  1151             ( _L( "CWlanSettings::GetSecondarySsidsForService() - unable to create CCommsDatabase" ) ) );
       
  1152 
       
  1153         return ret;
       
  1154         }
       
  1155     
       
  1156     TraceDump(INFO_LEVEL,
       
  1157         ( _L( "CWlanSettings::GetSecondarySsidsForService() - searching for WLAN ID %u" ), aId ) );
       
  1158 
       
  1159     CCommsDbTableView* table = NULL;
       
  1160     TRAP( ret, table = db->OpenViewMatchingUintLC(
       
  1161         TPtrC( WLAN_SECONDARY_SSID ),
       
  1162         TPtrC( WLAN_SEC_SSID_SERVICE_ID ),
       
  1163         aId ); CleanupStack::Pop( table ) );
       
  1164     if ( ret != KErrNone )
       
  1165         {
       
  1166         TraceDump( ERROR_LEVEL,
       
  1167             ( _L( "CWlanSettings::GetSecondarySsidsForService() - unable to create CCommsDbTableView" ) ) );
       
  1168         delete db;
       
  1169         
       
  1170         return ret;
       
  1171         }
       
  1172 
       
  1173     ret = table->GotoFirstRecord();
       
  1174     while ( ret == KErrNone )
       
  1175         {
       
  1176         TWlanSecondarySsid entry;
       
  1177         TRAPD( err, table->ReadUintL( TPtrC( WLAN_SEC_SSID_ID ), entry.id ) );
       
  1178 
       
  1179         if ( err == KErrNone )
       
  1180             {
       
  1181             TraceDump( INFO_LEVEL,
       
  1182                 ( _L( "CWlanSettings::GetSecondarySsidsForService() - reading ID %u" ), entry.id ) );
       
  1183             
       
  1184             TRAP( err, table->ReadTextL( TPtrC( WLAN_SEC_SSID_SCANNED_SSID ), entry.ssid ) );
       
  1185             }
       
  1186 
       
  1187         if ( err == KErrNone )
       
  1188             {
       
  1189             TRAP( err, table->ReadTextL( TPtrC( WLAN_SEC_SSID_USED_SSID ), entry.usedSsid ) );
       
  1190             }
       
  1191 
       
  1192         if ( err == KErrNone )
       
  1193             {
       
  1194             TraceDump( ERROR_LEVEL,
       
  1195                 ( _L( "CWlanSettings::GetSecondarySsidsForService() - appending entry" ) ) );
       
  1196             aSsids.Append( entry );
       
  1197             }
       
  1198         else
       
  1199             {
       
  1200             TraceDump( ERROR_LEVEL,
       
  1201                 ( _L( "CWlanSettings::GetSecondarySsidsForService() - unable to read, discarding" ) ) );            
       
  1202             }
       
  1203         ret = table->GotoNextRecord();    
       
  1204         }
       
  1205 
       
  1206     delete table;
       
  1207     delete db;
       
  1208 	
       
  1209 	return KErrNone;
       
  1210 	}
       
  1211     
       
  1212 // ---------------------------------------------------------
       
  1213 // void CWLanSettings::CreateWLANServiceRecordL( TUint32 aId, TWlanRecIdType aType)
       
  1214 // Fetches a WLANServiceTable record from CommsDat or creates a new record if
       
  1215 // aId == KCDNewRecordRequest
       
  1216 // if aType == EWlanRecordId, fetches record using record ID as criteria (can be LoadL'd)
       
  1217 // if aType == EWlanServiceId, fetches record using service ID as criteria (must be FindL'd)
       
  1218 // on success, iWLANRecord points to the fetched or created record
       
  1219 // on failure, iWLANRecord remains NULL
       
  1220 // ---------------------------------------------------------
       
  1221 //
       
  1222 void CWLanSettings::CreateWLANServiceRecordL( TUint32 aId, TWlanRecIdType aType)
       
  1223     {
       
  1224     if( iWLANRecord)
       
  1225         {
       
  1226         delete iWLANRecord;
       
  1227         iWLANRecord = NULL;
       
  1228         }
       
  1229 
       
  1230     if ( aType == EWlanIapId )
       
  1231         {
       
  1232         /**
       
  1233          * Load the IAP record and get the service ID that should point
       
  1234          * to the WLAN service table.
       
  1235          */        
       
  1236         CCDIAPRecord *iapRecord = static_cast<CCDIAPRecord *>
       
  1237             ( CCDRecordBase::RecordFactoryL( KCDTIdIAPRecord ) );
       
  1238         CleanupStack::PushL( iapRecord );
       
  1239 
       
  1240         TraceDump( INFO_LEVEL,
       
  1241             ( _L( "CWlanSettings::CreateWLANServiceRecordL() - searching for IAP ID %u"  ), aId ) );
       
  1242 
       
  1243         iapRecord->SetRecordId( aId );
       
  1244         TRAPD( ret, iapRecord->LoadL( *iDBSession ) );
       
  1245         if ( ret !=  KErrNone )
       
  1246             {
       
  1247             TraceDump( ERROR_LEVEL,
       
  1248                 ( _L( "CWlanSettings::CreateWLANServiceRecordL() - LoadL leaved with %d"  ), ret ) );
       
  1249 
       
  1250             User::Leave( ret );
       
  1251             }
       
  1252 
       
  1253         if ( TPtrC( iapRecord->iServiceType ) != TPtrC( KCDTypeNameLANService ) ||
       
  1254              TPtrC( iapRecord->iBearerType ) != TPtrC( KCDTypeNameLANBearer ) )
       
  1255             {
       
  1256             TraceDump( ERROR_LEVEL,
       
  1257                 ( _L( "CWlanSettings::CreateWLANServiceRecordL() - IAP doesn't point to LANService table" ) ) );
       
  1258 
       
  1259             User::Leave( KErrNotFound );
       
  1260             }
       
  1261         aId = iapRecord->iService;
       
  1262 
       
  1263         TraceDump( INFO_LEVEL,
       
  1264             ( _L( "CWlanSettings::CreateWLANServiceRecordL() - WLAN ServiceID is %u"  ), aId ) );
       
  1265 
       
  1266         CleanupStack::PopAndDestroy( iapRecord );
       
  1267         }
       
  1268 
       
  1269     CMDBGenericRecord* const genRec = static_cast<CMDBGenericRecord*> ( CCDRecordBase::RecordFactoryL( 0 ) );
       
  1270     CleanupStack::PushL( genRec );
       
  1271 
       
  1272     genRec->InitializeL( KGenericTable(),NULL);
       
  1273     
       
  1274     if( aType == EWlanRecordId)
       
  1275         {
       
  1276         genRec->SetRecordId( aId);
       
  1277         genRec->LoadL( *iDBSession);
       
  1278         }
       
  1279     else // EWlanServiceId
       
  1280         {
       
  1281         genRec->LoadL( *iDBSession);
       
  1282         
       
  1283         CMDBField<TUint32>* sidField = static_cast<CMDBField<TUint32>*>
       
  1284                                         (genRec->GetFieldByIdL( KCDTIdWlanServiceId));
       
  1285             // prime with service id                
       
  1286         *sidField = aId;
       
  1287         
       
  1288         TBool found = genRec->FindL( *iDBSession);
       
  1289         
       
  1290         if( !found)
       
  1291             {
       
  1292             User::Leave( KErrNotFound);
       
  1293             }
       
  1294 
       
  1295         }
       
  1296 
       
  1297     CleanupStack::Pop(); //genRec
       
  1298     iWLANRecord = genRec;
       
  1299     }
       
  1300 
       
  1301 //
       
  1302 // L-methods called and trapped by the public non-leaving methods
       
  1303 //
       
  1304 
       
  1305 // ---------------------------------------------------------
       
  1306 // void CWLanSettings::DoConnectL()
       
  1307 // Connects to the CommsDat storage server
       
  1308 // ---------------------------------------------------------
       
  1309 //
       
  1310 void CWLanSettings::DoConnectL()
       
  1311     {
       
  1312     TraceDump(ERROR_LEVEL,(_L("CWlanSettings::DoConnectL")));    
       
  1313     
       
  1314     if( !iDBSession)
       
  1315         {
       
  1316         iDBSession = CMDBSession::NewL( KCDCurrentVersion);
       
  1317         }
       
  1318     
       
  1319     }
       
  1320