wlanutilities/wlansniffer/apwizard/src/wsfwlaniapcreator.cpp
branchRCL_3
changeset 25 f28ada11abbf
equal deleted inserted replaced
24:63be7eb3fc78 25:f28ada11abbf
       
     1 /*
       
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   Implementation of CWsfWlanIapCreator
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 //  EXTERNAL INCLUDES
       
    22 #include <wlancontainer.h>
       
    23 #include <EapType.h>
       
    24 #include <cmdestinationext.h>
       
    25 #include <utf.h>
       
    26 
       
    27 //  CLASS HEADER
       
    28 #include "wsfwlaniapcreator.h"
       
    29 
       
    30 //  INTERNAL INCLUDES
       
    31 #include "wsfactivewaiter.h"
       
    32 #include "wsflogger.h"
       
    33 
       
    34 
       
    35 using namespace CMManager;
       
    36 
       
    37 
       
    38 /**
       
    39 * Order of first WEP key
       
    40 */
       
    41 static const TInt KFirstWepKey = 0;
       
    42 
       
    43 /**
       
    44 * WEP authentication modes
       
    45 */
       
    46 static const TInt KWepAuthOpen = 0;        // Open authentication
       
    47 
       
    48 /**
       
    49 * Max length of WPA key
       
    50 */
       
    51 static const TUint KWpaKeyMaxLength = 64;
       
    52     
       
    53 /**
       
    54 * Max length of WEP key
       
    55 */
       
    56 static const TUint KMaxWepKeyLen = 26; 
       
    57 
       
    58 
       
    59 
       
    60   
       
    61 
       
    62 // ----------------------------------------------------------------------------
       
    63 // CWsfWlanIapCreator::NewL
       
    64 // ----------------------------------------------------------------------------
       
    65 //
       
    66 CWsfWlanIapCreator* CWsfWlanIapCreator::NewL()
       
    67     {
       
    68     CWsfWlanIapCreator* thisPtr = NewLC();
       
    69     CleanupStack::Pop( thisPtr );
       
    70     return thisPtr;         
       
    71     }
       
    72 
       
    73 
       
    74 // ----------------------------------------------------------------------------
       
    75 // CWsfWlanIapCreator::NewLC
       
    76 // ----------------------------------------------------------------------------
       
    77 //
       
    78 CWsfWlanIapCreator* CWsfWlanIapCreator::NewLC()
       
    79     {
       
    80     CWsfWlanIapCreator* thisPtr = new (ELeave) CWsfWlanIapCreator();
       
    81     CleanupStack::PushL( thisPtr );
       
    82     thisPtr->ConstructL();
       
    83     return thisPtr;    
       
    84     }
       
    85 
       
    86 
       
    87 // ----------------------------------------------------------------------------
       
    88 // CWsfWlanIapCreator::~CWsfWlanIapCreator
       
    89 // ----------------------------------------------------------------------------
       
    90 //
       
    91 CWsfWlanIapCreator::~CWsfWlanIapCreator()
       
    92     {
       
    93     delete iDbSession;
       
    94     iCmManagerExt.Close();
       
    95     }
       
    96     
       
    97 
       
    98 // ----------------------------------------------------------------------------
       
    99 // CWsfWlanIapCreator::CWsfWlanIapCreator
       
   100 // ----------------------------------------------------------------------------
       
   101 //
       
   102 CWsfWlanIapCreator::CWsfWlanIapCreator()     
       
   103     {
       
   104     }
       
   105 
       
   106 
       
   107 // ----------------------------------------------------------------------------
       
   108 // CWsfWlanIapCreator::ConstructL
       
   109 // ----------------------------------------------------------------------------
       
   110 //
       
   111 void CWsfWlanIapCreator::ConstructL()
       
   112     {
       
   113     iDbSession = CommsDat::CMDBSession::NewL( CMDBSession::LatestVersion() );
       
   114     iCmManagerExt.OpenL();
       
   115     iDestinationId = 0;
       
   116     }
       
   117 
       
   118 
       
   119 // ----------------------------------------------------------------------------
       
   120 // CWsfWlanIapCreator::StoreWPADataL
       
   121 // ----------------------------------------------------------------------------
       
   122 //
       
   123 void CWsfWlanIapCreator::StoreWPADataL( const TInt aIapId, 
       
   124                                      const TDesC& aPresharedKey, 
       
   125                                      const CMManager::TWlanSecMode aSecMode )
       
   126     {
       
   127     LOG_ENTERFN( "CWsfWlanIapCreator::StoreWPADataL" );
       
   128     
       
   129     CCDIAPRecord *iapRec = static_cast<CCDIAPRecord*>
       
   130                           ( CCDRecordBase::RecordFactoryL( KCDTIdIAPRecord ) );
       
   131     
       
   132     CleanupStack::PushL( iapRec );
       
   133     
       
   134     iapRec->SetRecordId( aIapId );
       
   135     iapRec->LoadL( *iDbSession );
       
   136         
       
   137     TUint32 serviceId = TUint32( iapRec->iService );
       
   138 
       
   139     LOG_WRITEF("IAP(%d).serviceID = %d", aIapId, serviceId );
       
   140     
       
   141     // we have the service id, now we can flush the iap record
       
   142     CleanupStack::PopAndDestroy( iapRec );
       
   143 
       
   144     // search for the record    
       
   145     CMDBGenericRecord* generic = static_cast<CMDBGenericRecord*>( 
       
   146                                           CCDRecordBase::RecordFactoryL( 0 ) );
       
   147     CleanupStack::PushL( generic );    
       
   148     generic->InitializeL( TPtrC( WLAN_SERVICE ), NULL );
       
   149     generic->LoadL( *iDbSession );
       
   150     
       
   151     CMDBField<TUint32>* sidField = static_cast<CMDBField<TUint32>*>
       
   152                              ( generic->GetFieldByIdL( KCDTIdWlanServiceId ) );
       
   153     
       
   154     // prime with service id                
       
   155     *sidField = serviceId;
       
   156     
       
   157     TBool found = generic->FindL( *iDbSession );
       
   158 
       
   159     LOG_WRITE( "saving common WPA settings..." );
       
   160     TBool usesPsk( aPresharedKey.Length() > 0 );
       
   161 
       
   162     // Save PreShared Key as 8 bit string
       
   163     TBuf8<KWpaKeyMaxLength> keyWPA;
       
   164     keyWPA.Copy( aPresharedKey );
       
   165     TInt len( keyWPA.Length() );
       
   166     LOG_WRITEF( "PSK:[%S] len = %d", &aPresharedKey, len );
       
   167 
       
   168 
       
   169     CMDBField<TUint>* enableWpaPskField = static_cast<CMDBField<TUint>*>
       
   170                           ( generic->GetFieldByIdL( KCDTIdWlanEnableWpaPsk ) );
       
   171     enableWpaPskField->SetL( usesPsk );
       
   172 
       
   173     CMDBField<TUint>* secModeField = static_cast<CMDBField<TUint>*>
       
   174                         ( generic->GetFieldByIdL( KCDTIdWlanSecMode ) );
       
   175     secModeField->SetL( aSecMode );
       
   176 
       
   177     CMDBField<TDesC8>* wpaPskField = static_cast<CMDBField<TDesC8>*>
       
   178                        ( generic->GetFieldByIdL( KCDTIdWlanWpaPreSharedKey ) );
       
   179     wpaPskField->SetL( keyWPA );
       
   180         
       
   181     // Save PreShared Key length
       
   182     CMDBField<TUint>* keyLengthField = static_cast<CMDBField<TUint>*>
       
   183                         ( generic->GetFieldByIdL( KCDTIdWlanWpaKeyLength ) );
       
   184     keyLengthField->SetL( keyWPA.Length() );
       
   185 
       
   186 
       
   187     if ( !usesPsk )
       
   188         {
       
   189         // not PSK -> must be EAP
       
   190         LOG_WRITE( "saving EAP info..." );
       
   191         RImplInfoPtrArray eapArray;
       
   192         REComSession::ListImplementationsL( KEapTypeInterfaceUid, eapArray );
       
   193         CleanupClosePushL( eapArray );
       
   194         
       
   195         for ( TInt i = 0; i < eapArray.Count(); ++i )
       
   196             {
       
   197             // IsDisallowedOutsidePEAP actually means IsDisallowedOutsideTunnel
       
   198             if ( !CEapType::IsDisallowedOutsidePEAP( *eapArray[i] ) )
       
   199                 {
       
   200                 CEapType* eapType = CEapType::NewL( eapArray[i]->DataType(), 
       
   201                                                     ELan, 
       
   202                                                     serviceId );
       
   203                 CleanupStack::PushL( eapType );
       
   204                 
       
   205                 eapType->SetIndexL( ELan, serviceId );
       
   206                 CleanupStack::PopAndDestroy( eapType );
       
   207                 }
       
   208             }
       
   209 
       
   210         eapArray.ResetAndDestroy();
       
   211         CleanupStack::PopAndDestroy( &eapArray );
       
   212         }
       
   213 
       
   214     if ( !found )
       
   215         {
       
   216         // there wasn't any wlan service record, we have to create it now
       
   217         LOG_WRITE("new service record");
       
   218         generic->SetRecordId( KCDNewRecordRequest );
       
   219         generic->StoreL( *iDbSession );
       
   220         }
       
   221     else
       
   222         {
       
   223         // modify existing record
       
   224         LOG_WRITE("existing service record");
       
   225         generic->ModifyL( *iDbSession );
       
   226         }
       
   227     
       
   228     CleanupStack::PopAndDestroy( generic );
       
   229     }
       
   230     
       
   231 
       
   232 // ----------------------------------------------------------------------------
       
   233 // CWsfWlanIapCreator::StoreWEPDataL
       
   234 // ----------------------------------------------------------------------------
       
   235 //
       
   236 void CWsfWlanIapCreator::StoreWEPDataL( const TInt aIapId, 
       
   237                                         const TDesC& aWepKey, 
       
   238                                         const TBool aIsHexWep )
       
   239     {
       
   240     LOG_ENTERFN( "CWsfWlanIapCreator::StoreWEPDataL" );
       
   241     
       
   242     CCDIAPRecord *iapRec = static_cast<CCDIAPRecord*>
       
   243                           ( CCDRecordBase::RecordFactoryL( KCDTIdIAPRecord ) );
       
   244     
       
   245     CleanupStack::PushL( iapRec );
       
   246     
       
   247     iapRec->SetRecordId( aIapId );
       
   248     iapRec->LoadL( *iDbSession );
       
   249 
       
   250     TUint32 serviceId = TUint32( iapRec->iService );
       
   251     LOG_WRITEF("IAP(%d).serviceID = %d", aIapId, serviceId );
       
   252     
       
   253     CleanupStack::PopAndDestroy( iapRec );
       
   254 
       
   255 
       
   256     CMDBGenericRecord* generic = static_cast<CMDBGenericRecord*>
       
   257         ( CCDRecordBase::RecordFactoryL( 0 ) );
       
   258     CleanupStack::PushL( generic );    
       
   259     generic->InitializeL( TPtrC( WLAN_SERVICE ), NULL );
       
   260     generic->LoadL( *iDbSession );
       
   261     
       
   262     CMDBField<TUint32>* sidField = static_cast<CMDBField<TUint32>*>
       
   263                              ( generic->GetFieldByIdL( KCDTIdWlanServiceId ) );
       
   264     
       
   265     // prime with service id                
       
   266     *sidField = serviceId;
       
   267     
       
   268     TBool found = generic->FindL( *iDbSession );
       
   269     
       
   270     LOG_WRITE( "saving WEP settings..." );
       
   271 
       
   272 
       
   273     CMDBField<TUint>* indexField = static_cast<CMDBField<TUint>*>
       
   274                             ( generic->GetFieldByIdL( KCDTIdWlanWepIndex ) );
       
   275     indexField->SetL( KFirstWepKey );
       
   276 
       
   277     // Save authentication mode    
       
   278     CMDBField<TUint>* authenticationField = static_cast<CMDBField<TUint>*>
       
   279                             ( generic->GetFieldByIdL( KCDTIdWlanAuthMode ) );
       
   280     authenticationField->SetL( KWepAuthOpen );
       
   281     
       
   282     
       
   283 
       
   284     // now we need to convert the key to 8bit and to hex
       
   285     // and again detect the required bits
       
   286     TBuf8<KMaxWepKeyLen> key;
       
   287     
       
   288     // convert to 8 bit
       
   289     key.Copy( aWepKey );
       
   290 
       
   291        
       
   292     if ( !aIsHexWep )
       
   293         {
       
   294         // Must be converted to hexa and stored as a hexa
       
   295         // Ascii key is half the length of Hex
       
   296         HBufC8* buf8Conv = HBufC8::NewLC( key.Length()*2 );
       
   297         TPtr8 bufptr( buf8Conv->Des() );
       
   298         ConvertAsciiToHex( key, bufptr );
       
   299         
       
   300         CMDBField<TDesC8>* wepKey1Field = static_cast<CMDBField<TDesC8>*>
       
   301                             ( generic->GetFieldByIdL( KCDTIdWlanWepKey1 ) );
       
   302         wepKey1Field->SetL( buf8Conv->Des() );
       
   303 
       
   304         CleanupStack::PopAndDestroy( buf8Conv );
       
   305         }
       
   306     else
       
   307         {
       
   308         // already in hexa format
       
   309         CMDBField<TDesC8>* wepKey1Field = static_cast<CMDBField<TDesC8>*>
       
   310                             ( generic->GetFieldByIdL( KCDTIdWlanWepKey1 ) );
       
   311         wepKey1Field->SetL( key );
       
   312         }
       
   313         
       
   314     
       
   315      // write default values to the rest of the columns
       
   316     key.Zero();
       
   317     
       
   318     CMDBField<TDesC8>* wepKey2Field = static_cast<CMDBField<TDesC8>*>
       
   319                         ( generic->GetFieldByIdL( KCDTIdWlanWepKey2 ) );
       
   320     wepKey2Field->SetL( key );
       
   321     CMDBField<TDesC8>* wepKey3Field = static_cast<CMDBField<TDesC8>*>
       
   322                         ( generic->GetFieldByIdL( KCDTIdWlanWepKey3 ) );
       
   323     wepKey3Field->SetL( key );
       
   324     CMDBField<TDesC8>* wepKey4Field = static_cast<CMDBField<TDesC8>*>
       
   325                         ( generic->GetFieldByIdL( KCDTIdWlanWepKey4 ) );
       
   326     wepKey4Field->SetL( key );
       
   327 
       
   328 
       
   329     // write format data
       
   330     TUint fmt( aIsHexWep );
       
   331     
       
   332     CMDBField<TUint>* formatKey1Field = static_cast<CMDBField<TUint>*>
       
   333                             ( generic->GetFieldByIdL( KCDTIdWlanFormatKey1 ) );
       
   334     formatKey1Field->SetL( fmt );
       
   335     CMDBField<TUint>* formatKey2Field = static_cast<CMDBField<TUint>*>
       
   336                             ( generic->GetFieldByIdL( KCDTIdWlanFormatKey2 ) );
       
   337     formatKey2Field->SetL( fmt );
       
   338     CMDBField<TUint>* formatKey3Field = static_cast<CMDBField<TUint>*>
       
   339                             ( generic->GetFieldByIdL( KCDTIdWlanFormatKey3 ) );
       
   340     formatKey3Field->SetL( fmt );
       
   341     CMDBField<TUint>* formatKey4Field = static_cast<CMDBField<TUint>*>
       
   342                             ( generic->GetFieldByIdL( KCDTIdWlanFormatKey4 ) );
       
   343     formatKey4Field->SetL( fmt );
       
   344     
       
   345     
       
   346     if ( !found )
       
   347         {
       
   348         // there wasn't any wlan service record, we have to create it now
       
   349         LOG_WRITE("new service record");
       
   350         generic->SetRecordId( KCDNewRecordRequest );
       
   351         generic->StoreL( *iDbSession );
       
   352         }
       
   353     else
       
   354         {
       
   355         // modify existing record
       
   356         LOG_WRITE("existing service record");
       
   357         generic->ModifyL( *iDbSession );
       
   358         }
       
   359         
       
   360     CleanupStack::PopAndDestroy( generic );    
       
   361     }
       
   362 
       
   363 
       
   364 // ----------------------------------------------------------------------------
       
   365 // CWsfWlanIapCreator::ConvertAsciiToHex
       
   366 // ----------------------------------------------------------------------------
       
   367 //
       
   368 void CWsfWlanIapCreator::ConvertAsciiToHex( const TDesC8& aSource, 
       
   369                                             TDes8& aDest )
       
   370     {
       
   371     LOG_ENTERFN( "CWsfWlanIapCreator::ConvertAsciiToHex" );
       
   372     _LIT8( hex, "0123456789ABCDEF" );
       
   373     TInt size = aSource.Size();
       
   374     aDest.Zero();
       
   375     
       
   376     for ( TInt ii = 0; ii < size; ii++ )
       
   377         {
       
   378         TText8 ch = aSource[ii];
       
   379         aDest.Append( hex()[(ch/16)&0x0f] );
       
   380         aDest.Append( hex()[ch&0x0f] );
       
   381         }
       
   382     }
       
   383     
       
   384 
       
   385 
       
   386 // ----------------------------------------------------------------------------
       
   387 // CWsfWlanIapCreator::SetDefaultDestination
       
   388 // ----------------------------------------------------------------------------
       
   389 //
       
   390 void CWsfWlanIapCreator::SetDefaultDestination( const TUint32 aDestinationId )
       
   391     {
       
   392     LOG_ENTERFN( "CWsfWlanIapCreator::SetDefaultDestination" );
       
   393     LOG_WRITEF( "destId = %d", aDestinationId );
       
   394     iDestinationId = aDestinationId;
       
   395     }
       
   396 
       
   397 
       
   398 // ----------------------------------------------------------------------------
       
   399 // CWsfWlanIapCreator::CreateAccessPointL
       
   400 // ----------------------------------------------------------------------------
       
   401 //
       
   402 void CWsfWlanIapCreator::CreateAccessPointL( TWsfWlanInfo& aWlanInfo, 
       
   403                                                 const TDesC& aPresharedKey, 
       
   404                                                 const TBool aIsHex )
       
   405     {
       
   406     LOG_ENTERFN( "CWsfWlanIapCreator::CreateAccessPointL" );
       
   407     TUint32 iapId( 0 );
       
   408 
       
   409     // create connection method
       
   410     iapId = CreateConnectionMethodL( aWlanInfo );
       
   411     
       
   412 
       
   413     // open a transaction
       
   414     const TUint KMaxOpenTransAttempts = 10;
       
   415     const TUint KRetryAfter = 100000;
       
   416 
       
   417     TInt err( KErrNone );
       
   418     TUint attempts( KMaxOpenTransAttempts );
       
   419     
       
   420     CWsfActiveWaiter* waiter = CWsfActiveWaiter::NewLC();
       
   421     RTimer timer;
       
   422     timer.CreateLocal();
       
   423     CleanupClosePushL( timer );
       
   424     
       
   425     do
       
   426         {
       
   427         TRAP( err, iDbSession->OpenTransactionL() );
       
   428         if ( err )
       
   429             {
       
   430             timer.After( waiter->iStatus, 
       
   431                          TTimeIntervalMicroSeconds32( KRetryAfter ) );
       
   432             waiter->WaitForRequest();
       
   433             }
       
   434         }
       
   435     while ( err && attempts-- );
       
   436 
       
   437     User::LeaveIfError( err );
       
   438 
       
   439     CleanupStack::PopAndDestroy( &timer );
       
   440     CleanupStack::PopAndDestroy( waiter );
       
   441     
       
   442     CleanupStack::PushL( TCleanupItem( RollbackCommsDat, iDbSession ) );
       
   443 
       
   444     
       
   445     // now we have a valid transaction, save security settings
       
   446     
       
   447     switch ( aWlanInfo.iSecurityMode )
       
   448         {
       
   449         case EWlanSecModeWep:
       
   450             {
       
   451             StoreWEPDataL( iapId, aPresharedKey, aIsHex );
       
   452             break;
       
   453             }
       
   454         case EWlanSecMode802_1x:
       
   455         case EWlanSecModeWpa:
       
   456         case EWlanSecModeWpa2:
       
   457             {
       
   458             StoreWPADataL( iapId, aPresharedKey, aWlanInfo.iSecurityMode );
       
   459             break;
       
   460             }
       
   461         case EWlanSecModeOpen: // fall through on purpose
       
   462         default:
       
   463             {
       
   464             }
       
   465         }
       
   466 
       
   467     CleanupStack::Pop( 1 ); // transaction rollback popped
       
   468     
       
   469     // commit changes
       
   470     if ( iDbSession->IsInTransaction() )
       
   471         {
       
   472         iDbSession->CommitTransactionL();
       
   473         }
       
   474         
       
   475     }
       
   476 
       
   477 
       
   478 // ----------------------------------------------------------------------------
       
   479 // CWsfWlanIapCreator::RollbackCommsDat
       
   480 // ----------------------------------------------------------------------------
       
   481 void CWsfWlanIapCreator::RollbackCommsDat( TAny* aDbSession )
       
   482     {
       
   483     CommsDat::CMDBSession* session = static_cast<CommsDat::CMDBSession*>( 
       
   484                                                                  aDbSession );
       
   485     TRAP_IGNORE( session->RollbackTransactionL() );
       
   486     }
       
   487 
       
   488 
       
   489 // ----------------------------------------------------------------------------
       
   490 // CWsfWlanIapCreator::CreateConnectionMethodL
       
   491 // ----------------------------------------------------------------------------
       
   492 //
       
   493 TUint32 CWsfWlanIapCreator::CreateConnectionMethodL( TWsfWlanInfo& aWlanInfo )
       
   494     {
       
   495     LOG_ENTERFN( "CWsfWlanIapCreator::CreateConnectionMethodL" );
       
   496     
       
   497     HBufC* name16 = NULL;
       
   498     HBufC* ssid16 = NULL;
       
   499     
       
   500     if ( aWlanInfo.iRawSsid.Length() )
       
   501         {
       
   502         // ssid is from scan result store it as it is 
       
   503         // since the encoding of ssid is unknown
       
   504         name16 = HBufC::NewLC( aWlanInfo.iRawSsid.Length() );
       
   505         name16->Des().Copy( aWlanInfo.iRawSsid );
       
   506         ssid16 = HBufC::NewLC( aWlanInfo.iRawSsid.Length() );
       
   507         ssid16->Des().Copy( aWlanInfo.iRawSsid );
       
   508         }
       
   509     else
       
   510         {
       
   511         // user has inputted ssid store it to name as unicode since it is utf-8 
       
   512         name16 = aWlanInfo.GetSsidAsUnicodeLC();
       
   513         ssid16 = HBufC::NewLC( aWlanInfo.iSsid.Length() );
       
   514         ssid16->Des().Copy( aWlanInfo.iSsid );
       
   515         }
       
   516         
       
   517     TUint32 iapId( 0 );
       
   518     
       
   519     if ( !iDestinationId )
       
   520         {
       
   521         // possibly temporary iap to uncategorized
       
   522         LOG_WRITE( "destId not set -> temporary into Uncategorised" );
       
   523         
       
   524         RCmConnectionMethodExt cm = iCmManagerExt.CreateConnectionMethodL( 
       
   525                                                          KUidWlanBearerType );
       
   526         CleanupClosePushL( cm );
       
   527         
       
   528         cm.SetStringAttributeL( ECmName, *name16 );
       
   529         cm.SetStringAttributeL( EWlanSSID, *ssid16 );
       
   530         cm.SetIntAttributeL( EWlanSecurityMode, aWlanInfo.iSecurityMode );
       
   531         cm.SetIntAttributeL( EWlanConnectionMode, aWlanInfo.iNetMode );
       
   532         cm.SetBoolAttributeL( EWlanScanSSID, !aWlanInfo.iVisibility );
       
   533         
       
   534         // commit changes
       
   535         cm.UpdateL();
       
   536 
       
   537         // get the iap id
       
   538         iapId = cm.GetIntAttributeL( ECmId );
       
   539         
       
   540         // now retrieve the name again to see if it has been changed 
       
   541         // (ExistingCmName -> ExistingCmName(01) and alike)
       
   542         HBufC* cmName = cm.GetStringAttributeL( ECmName );
       
   543         
       
   544         TInt error = CnvUtfConverter::ConvertFromUnicodeToUtf8( 
       
   545                                                         aWlanInfo.iNetworkName, 
       
   546                                                         *cmName  );
       
   547         if ( error )
       
   548             {
       
   549             LOG_WRITE( "ConvertFromUnicodeToUtf8 failed");
       
   550             aWlanInfo.iNetworkName.Copy( *cmName );
       
   551             }
       
   552 
       
   553         delete cmName;
       
   554         
       
   555         CleanupStack::PopAndDestroy( &cm );
       
   556         }
       
   557     else
       
   558         {
       
   559         // final iap to the default destination
       
   560         LOG_WRITEF( "destId = %d", iDestinationId );
       
   561         
       
   562         RCmDestinationExt destination = iCmManagerExt.DestinationL( 
       
   563                                                             iDestinationId );
       
   564         CleanupClosePushL( destination );
       
   565         
       
   566         RCmConnectionMethodExt cm = destination.CreateConnectionMethodL( 
       
   567                                                          KUidWlanBearerType );
       
   568         CleanupClosePushL( cm );
       
   569 
       
   570         cm.SetStringAttributeL( ECmName, *name16 );
       
   571         cm.SetStringAttributeL( EWlanSSID, *ssid16 );
       
   572         cm.SetIntAttributeL( EWlanSecurityMode, aWlanInfo.iSecurityMode );
       
   573         cm.SetIntAttributeL( EWlanConnectionMode, aWlanInfo.iNetMode );
       
   574         cm.SetBoolAttributeL( EWlanScanSSID, !aWlanInfo.iVisibility );
       
   575         
       
   576         // commit changes
       
   577         destination.UpdateL();
       
   578 
       
   579         // get the iap id
       
   580         iapId = cm.GetIntAttributeL( ECmId );
       
   581         
       
   582         // now retrieve the name again to see if it has been changed 
       
   583         // (ExistingCmName -> ExistingCmName(01) and alike)
       
   584         HBufC* cmName = cm.GetStringAttributeL( ECmName );
       
   585         TInt error = CnvUtfConverter::ConvertFromUnicodeToUtf8( 
       
   586                                                          aWlanInfo.iNetworkName, 
       
   587                                                          *cmName  );
       
   588         if ( error )
       
   589             {
       
   590             LOG_WRITE( "ConvertFromUnicodeToUtf8 failed");
       
   591             aWlanInfo.iNetworkName.Copy( *cmName );
       
   592             }
       
   593         
       
   594         delete cmName;
       
   595         
       
   596         CleanupStack::PopAndDestroy( &cm );
       
   597         CleanupStack::PopAndDestroy( &destination );
       
   598         }
       
   599 
       
   600     CleanupStack::PopAndDestroy( ssid16 );
       
   601     CleanupStack::PopAndDestroy( name16 );
       
   602 
       
   603     aWlanInfo.iIapId = iapId;
       
   604     LOG_WRITEF( "new iapId = %d", iapId );
       
   605 
       
   606     return iapId;
       
   607     }
       
   608         
       
   609