satengine/SatServer/Engine/src/CSatApnHandler.cpp
changeset 46 2fa1fa551b0b
parent 42 35488577e233
child 48 78df25012fda
equal deleted inserted replaced
42:35488577e233 46:2fa1fa551b0b
     1 /*
       
     2 * Copyright (c) 2002-2010 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "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 APN operations
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <cmmanager.h>
       
    20 #include <cmmanagerext.h>
       
    21 #include <cmconnectionmethod.h>
       
    22 #include <cmconnectionmethodext.h>
       
    23 #include <cmconnectionmethoddef.h>
       
    24 #include <cmpluginpacketdatadef.h>
       
    25 #include <cmdestination.h>
       
    26 #include <cmmanagerdef.h>
       
    27 
       
    28 #include    "MSatUtils.h"
       
    29 #include    "CSatApnHandler.h"
       
    30 #include    "SatLog.h"
       
    31 
       
    32 const TUint32 KSatCMGranularity( 5 );
       
    33 
       
    34 // ============================ MEMBER FUNCTIONS ===============================
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CSatApnHandler::CSatApnHandler
       
    38 // C++ default constructor can NOT contain any code, that
       
    39 // might leave.
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CSatApnHandler::CSatApnHandler( MSatUtils& aUtils ) :
       
    43     iUtils( aUtils )
       
    44     {
       
    45     LOG( SIMPLE,
       
    46         "SATENGINE: CSatApnHandler::CSatApnHandler calling-exiting" )
       
    47     }
       
    48 
       
    49 // -----------------------------------------------------------------------------
       
    50 // CSatApnHandler::NewL
       
    51 // Two-phased constructor.
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 CSatApnHandler* CSatApnHandler::NewL( MSatUtils& aUtils )
       
    55     {
       
    56     LOG( SIMPLE, "SATENGINE: CSatApnHandler::NewL calling" )
       
    57 
       
    58     CSatApnHandler* self = new( ELeave )CSatApnHandler( aUtils );
       
    59     LOG( SIMPLE, "SATENGINE: CSatApnHandler::NewL exiting" )
       
    60     return self;
       
    61     }
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // CSatApnHandler::Destructor
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 CSatApnHandler::~CSatApnHandler()
       
    68     {
       
    69     LOG( SIMPLE,
       
    70         "SATENGINE: CSatApnHandler::~CSatApnHandler calling" )
       
    71     LOG( SIMPLE,
       
    72         "SATENGINE: CSatApnHandler::~CSatApnHandler exiting" )
       
    73     }
       
    74 
       
    75 // -----------------------------------------------------------------------------
       
    76 // CSatApnHandler::GetApnInfoL
       
    77 // -----------------------------------------------------------------------------
       
    78 //
       
    79 void CSatApnHandler::GetApnInfoL(
       
    80     const RSat::TAccessName& aName,
       
    81     const RSat::TTextString& aUserLogin,
       
    82     const RSat::TTextString& aUserPwd,
       
    83     TUint32& aIapId,
       
    84     TUint32& aNwId,
       
    85     TBool& aApnCreated,
       
    86     const RPacketContext::TProtocolType& aPdpType,
       
    87     const TDesC8& aLocalAddress )
       
    88     {
       
    89     LOG( SIMPLE, "SATENGINE: CSatApnHandler::GetApnInfoL calling" )
       
    90 
       
    91 
       
    92     // First check the length of the APN
       
    93     if ( !aName.Length() )
       
    94         {
       
    95         LOG( SIMPLE, "SATENGINE: CSatApnHandler::GetApnInfoL \
       
    96         aName.Length() == 0" )
       
    97         User::Leave( KErrNotFound );
       
    98         }
       
    99 
       
   100     // Format APN
       
   101     HBufC* formatName = FormatAPN( aName );
       
   102 
       
   103     if ( !formatName )
       
   104         {
       
   105         LOG( SIMPLE, "SATENGINE: CSatApnHandler::GetApnInfoL wrong APN name \
       
   106             format" )
       
   107         // APN was in false format, leave
       
   108         User::Leave( KErrArgument );
       
   109         }
       
   110     CleanupStack::PushL( formatName );
       
   111 
       
   112     // Reference params
       
   113     TUint32 iapId( 0 );
       
   114     TUint32 networkId( 0 );
       
   115 
       
   116     // Find if the connection method already exists 
       
   117     // Initialize the flags
       
   118     TBool found = EFalse;
       
   119     aApnCreated = EFalse;
       
   120 
       
   121     // Create CMManager Session
       
   122     RCmManager cmManager;
       
   123     cmManager.OpenLC();
       
   124 
       
   125     // Get the Connection Method list from the open CMManager session
       
   126     RArray<TUint32> array = RArray<TUint32>( KSatCMGranularity );
       
   127     CleanupClosePushL( array );
       
   128 
       
   129     cmManager.ConnectionMethodL( array );
       
   130     
       
   131     // Go through the Connection Method list to find if there is matched one
       
   132     HBufC* apnCM;
       
   133 
       
   134     LOG2( SIMPLE, "SATENGINE: CSatApnHandler::GetApnInfoL, \
       
   135                    required pdp type is %d", aPdpType )
       
   136 
       
   137     for( TInt i = 0; ( i < array.Count() ) && !found; ++i )
       
   138         {
       
   139         RCmConnectionMethod cm = cmManager.ConnectionMethodL( array[i] );
       
   140         
       
   141         CleanupClosePushL( cm );
       
   142         // query the APN of the Connection Method
       
   143         apnCM = cm.GetStringAttributeL( CMManager::EPacketDataAPName );
       
   144         CleanupStack::PushL( apnCM );
       
   145         // query the pdpType of the Connection Method
       
   146         TInt pdpType = cm.GetIntAttributeL( CMManager::EPacketDataPDPType );
       
   147 
       
   148         
       
   149         if ( ( pdpType == aPdpType ) && ( *apnCM == *formatName ) )
       
   150             {
       
   151             // Found the Connection Method, query the IapId and NwId
       
   152             LOG( SIMPLE, "SATENGINE: CSatApnHandler::GetApnInfoL, \
       
   153                           Record found" )
       
   154 
       
   155             // Get IAP Id, Network Id
       
   156             networkId = cm.GetIntAttributeL( CMManager::ECmNetworkId );
       
   157             LOG2( SIMPLE, "SATENGINE: CSatApnHandler::GetApnInfoL, \
       
   158                            networkId %d", networkId ) 
       
   159             
       
   160             iapId = cm.GetIntAttributeL( CMManager::ECmIapId );
       
   161             LOG2( SIMPLE, "SATENGINE: CSatApnHandler::GetApnInfoL, \
       
   162                            iapId %d", iapId )
       
   163     
       
   164             found = ETrue;
       
   165             }
       
   166         CleanupStack::PopAndDestroy( apnCM );
       
   167         CleanupStack::PopAndDestroy( &cm );    
       
   168         }        
       
   169  
       
   170     CleanupStack::PopAndDestroy( &array );
       
   171 
       
   172     if ( !found )
       
   173         {
       
   174         // No Connection Method found, insert a new one
       
   175     
       
   176         LOG( SIMPLE, "SATENGINE: CSatApnHandler::GetApnInfoL, \
       
   177                       Record Not found, insert a new one" )
       
   178         
       
   179         TRAPD( insertError, InsertRecordL( *formatName, aUserLogin, 
       
   180                aUserPwd, iapId, networkId, aPdpType, aLocalAddress ) )
       
   181 
       
   182         if ( KErrNone == insertError )
       
   183             {
       
   184             // Insert OK
       
   185             LOG( NORMAL, "SATENGINE: CSatApnHandler::GetApnInfoL, \
       
   186                           new record inserted" )
       
   187             aApnCreated = ETrue;
       
   188             }
       
   189         }
       
   190     
       
   191     CleanupStack::PopAndDestroy( &cmManager );
       
   192     CleanupStack::PopAndDestroy( formatName );
       
   193 
       
   194     // Set return values
       
   195     aIapId = iapId;
       
   196     aNwId = networkId;
       
   197 
       
   198     LOG( SIMPLE, "SATENGINE: CSatApnHandler::GetApnInfoL exiting" )
       
   199     }
       
   200 
       
   201 // -----------------------------------------------------------------------------
       
   202 // CSatApnHandler::DeleteApnL
       
   203 // -----------------------------------------------------------------------------
       
   204 //
       
   205 void CSatApnHandler::DeleteApnL( TUint32 aApnId )
       
   206     {
       
   207     LOG( SIMPLE, "SATENGINE: CSatApnHandler::DeleteApnL calling" )
       
   208 
       
   209     if ( aApnId )
       
   210         { 
       
   211         // Create CMManager Session
       
   212         RCmManagerExt cmManager;
       
   213         cmManager.OpenLC();
       
   214         
       
   215         // Get the connection method
       
   216         RCmConnectionMethodExt cm;
       
   217         cm = cmManager.ConnectionMethodL( aApnId );
       
   218         CleanupClosePushL( cm );
       
   219 
       
   220         cm.DeleteL();
       
   221         LOG2( SIMPLE, "SATENGINE: CSatApnHandler::DeleteApnL, \
       
   222                        delete the connection method %d", aApnId )
       
   223 
       
   224         CleanupStack::PopAndDestroy( &cm );
       
   225         CleanupStack::PopAndDestroy( &cmManager );
       
   226         }
       
   227 
       
   228     LOG( SIMPLE, "SATENGINE: CSatApnHandler::DeleteApnL exiting" )
       
   229     }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // CSatApnHandler::InsertRecordL
       
   233 // -----------------------------------------------------------------------------
       
   234 //
       
   235 void CSatApnHandler::InsertRecordL(
       
   236     const TDesC& aReqApn,
       
   237     const RSat::TTextString& aUserLogin,
       
   238     const RSat::TTextString& aUserPwd,
       
   239     TUint32& aIapId,
       
   240     TUint32& aNwId,
       
   241     const RPacketContext::TProtocolType aPdpType,
       
   242     const TDesC8& aLocalAddress )
       
   243     {
       
   244     LOG( SIMPLE, "SATENGINE: CSatApnHandler::InsertRecordL calling" )
       
   245 
       
   246     LOG2( NORMAL, "SATENGINE: CSatApnHandler::InsertRecordL, apn is %S", 
       
   247         &aReqApn )
       
   248     
       
   249     // Create CMManager Session
       
   250     RCmManagerExt cmManager;
       
   251     cmManager.OpenLC();
       
   252 
       
   253     // Create a connection method without destination. So when we can find 
       
   254     // the connection method by going through the connection method list.
       
   255     RCmConnectionMethodExt cm;
       
   256     cm = cmManager.CreateConnectionMethodL( KUidPacketDataBearerType );
       
   257     CleanupClosePushL( cm );
       
   258     
       
   259     // This is shown in GS in Access Point list
       
   260     cm.SetStringAttributeL( CMManager::ECmName, iUtils.BipApnName() );    
       
   261     // GPRS connection name
       
   262     cm.SetStringAttributeL( CMManager::EPacketDataAPName, aReqApn );   
       
   263     // IPv4 or IPv6
       
   264     cm.SetIntAttributeL( CMManager::EPacketDataPDPType, aPdpType );
       
   265 
       
   266     // Set the local IP address (if any)
       
   267     if ( aLocalAddress.Length() )
       
   268         {
       
   269         LOG2( SIMPLE, "SATENGINE: CSatApnHandler::InsertRecordL, Local addr \
       
   270             length: %i", aLocalAddress.Length() )
       
   271         cm.SetString8AttributeL( CMManager::EPacketDataPDPAddress, 
       
   272             aLocalAddress );
       
   273         }
       
   274 
       
   275     cm.SetBoolAttributeL( CMManager::ECmWapIPSecurity, EFalse );
       
   276     cm.SetIntAttributeL( CMManager::ECmWapIPWSPOption, 
       
   277                          CMManager::ECmWapWspOptionConnectionOriented );
       
   278     cm.SetBoolAttributeL( CMManager::EPacketDataDisablePlainTextAuth, EFalse );
       
   279     cm.SetBoolAttributeL( CMManager::ECmIFPromptForAuth, EFalse );
       
   280 
       
   281     if ( aUserLogin != KNullDesC )
       
   282         {
       
   283         LOG2( NORMAL, "SATENGINE: CSatApnHandler::InsertRecordL, \
       
   284             username is %S", &aUserLogin ) 
       
   285         cm.SetStringAttributeL( CMManager::ECmIFAuthName, aUserLogin );
       
   286 
       
   287         if ( aUserPwd != KNullDesC )
       
   288             {
       
   289             LOG2( NORMAL, "SATENGINE: CSatApnHandler::InsertRecordL, \
       
   290                 pwd is %S", &aUserPwd ) 
       
   291             cm.SetStringAttributeL( CMManager::ECmIFAuthPass, aUserPwd );
       
   292             }
       
   293         }
       
   294     
       
   295     // Update the access point ID for use later.
       
   296     cm.UpdateL();
       
   297 
       
   298     // Get IAP Id, Network Id
       
   299     aNwId = cm.GetIntAttributeL( CMManager::ECmNetworkId );
       
   300     LOG2( SIMPLE, "SATENGINE: CSatApnHandler::InsertRecordL, \
       
   301                    Nwid is %d", aNwId ) 
       
   302     
       
   303     aIapId = cm.GetIntAttributeL( CMManager::ECmIapId );
       
   304     LOG2( SIMPLE, "SATENGINE: CSatApnHandler::InsertRecordL, \
       
   305                    aIpId is %d", aIapId )
       
   306 
       
   307     CleanupStack::PopAndDestroy( &cm );
       
   308     CleanupStack::PopAndDestroy( &cmManager );
       
   309 
       
   310     LOG( SIMPLE, "SATENGINE: CSatApnHandler::InsertRecordL exiting" )
       
   311     }
       
   312 
       
   313 // -----------------------------------------------------------------------------
       
   314 // CSatApnHandler::FormatAPN
       
   315 // -----------------------------------------------------------------------------
       
   316 //
       
   317 HBufC* CSatApnHandler::FormatAPN( const RSat::TAccessName& 
       
   318     aReqApn ) const
       
   319     {
       
   320     LOG( SIMPLE, "SATENGINE: CSatApnHandler::FormatAPN calling" )
       
   321 
       
   322     // Length of the access point name
       
   323     const TInt accessNameLength( aReqApn.Length() );
       
   324     TInt index( 0 );
       
   325 
       
   326     // Format APN coming from SIM. Remove length infos and add dots
       
   327     // from the requested APN. The APN format is, for example,
       
   328     // '4Some9accesspnt2fi' and after the format the formatted APN
       
   329     // should be 'Some.accesspnt.fi'
       
   330     HBufC* formatApnName = HBufC::New( aReqApn.MaxLength() );
       
   331 
       
   332     if ( formatApnName )
       
   333         {
       
   334         TPtr formApn( formatApnName->Des() );
       
   335 
       
   336         // Indicates is the APN format correct. Can be detected only in loops.
       
   337         TBool correctFormat( ETrue );
       
   338 
       
   339         // Loop requested APN. The first byte is always the length
       
   340         for ( index = 0; index < accessNameLength && correctFormat; index++ )
       
   341             {
       
   342             // Next byte is the length of the next label
       
   343             const TInt length( aReqApn[index] );
       
   344 
       
   345             // Label length cannot be bigger than the length of the APN
       
   346             if ( ( index + length ) > accessNameLength )
       
   347                 {
       
   348                 LOG( SIMPLE, "SATENGINE: CSatApnHandler::FormatAPN \
       
   349                 ( index + length ) > accessNameLength" )
       
   350                 correctFormat = EFalse;
       
   351                 }
       
   352 
       
   353             // Append the label
       
   354             for ( TInt j = 0; j < length && correctFormat; j++ )
       
   355                 {
       
   356                 index++; // move to next index
       
   357                 TChar next = aReqApn[index];
       
   358                 formApn.Append( next );
       
   359                 }
       
   360 
       
   361             // Add dot to APN if not end of APN
       
   362             if ( accessNameLength > index + 1 )
       
   363                 {
       
   364                 LOG( SIMPLE, "SATENGINE: CSatApnHandler::FormatAPN \
       
   365                 accessNameLength > index + 1" )
       
   366                 formApn.Append( '.' );
       
   367                 }
       
   368             }
       
   369         LOG2( SIMPLE, "SATENGINE: CSatApnHandler::FormatAPN index: %i", index )
       
   370 
       
   371         // Check did we read all characters
       
   372         if ( !correctFormat || ( index != accessNameLength ) )
       
   373             {
       
   374             // Format was not correct, return zero
       
   375             formApn.Zero();
       
   376             formApn.SetLength( 0 );
       
   377             }
       
   378         else
       
   379             {
       
   380             LOG( SIMPLE, "SATENGINE: CSatApnHandler::FormatAPN lower case" )
       
   381             // Put characters to lower case
       
   382             formApn.LowerCase();
       
   383             }
       
   384 
       
   385         LOG( SIMPLE, "SATENGINE: CSatApnHandler::FormatAPN exiting" )
       
   386         }
       
   387 
       
   388     return formatApnName;
       
   389     }
       
   390 
       
   391 // -----------------------------------------------------------------------------
       
   392 // CSatApnHandler::FindDefaultApL
       
   393 // -----------------------------------------------------------------------------
       
   394 //
       
   395 TUint32 CSatApnHandler::FindDefaultApL(
       
   396         const RPacketContext::TProtocolType& aPdpType )
       
   397     {
       
   398     LOG( SIMPLE, "SATENGINE: CSatApnHandler::FindDefaultApL calling" );
       
   399     TUint32 defaultIap( 0 );
       
   400     TInt pdpType;
       
   401     TBool isFound( EFalse );
       
   402 
       
   403     // create a network destination
       
   404     RCmDestination des;
       
   405     RCmConnectionMethod cm;
       
   406 
       
   407     // Create CMManager Session
       
   408     RCmManager cmManager;
       
   409     cmManager.OpenL();
       
   410     CleanupClosePushL( cmManager );
       
   411 
       
   412     // Get the Connection Method list from the open CMManager session
       
   413     RArray<TUint32> array( KSatCMGranularity );
       
   414     CleanupClosePushL( array );
       
   415 
       
   416     // list all available destinations' id
       
   417     cmManager.AllDestinationsL( array );
       
   418     for( TInt i = 0; ( i < array.Count() ) && !isFound; ++i )
       
   419         {
       
   420         des = cmManager.DestinationL( array[i] );
       
   421         CleanupClosePushL( des );
       
   422 
       
   423         if ( CMManager::ESnapPurposeInternet ==
       
   424         des.MetadataL( CMManager::ESnapMetadataPurpose ) )
       
   425             {
       
   426             LOG( SIMPLE, "SATENGINE: CSatApnHandler::FindDefaultApL \
       
   427             the fixed destination is identified as 'Internet'" );
       
   428             for( TInt j = 0; ( j < des.ConnectionMethodCount() ) &&
       
   429             !isFound; ++j )
       
   430                 {
       
   431                 cm = des.ConnectionMethodL( j );
       
   432                 CleanupClosePushL( cm );
       
   433                 pdpType = cm.GetIntAttributeL(
       
   434                         CMManager::EPacketDataPDPType );
       
   435                 LOG2( SIMPLE, "SATENGINE: CSatApnHandler::FindDefaultApL \
       
   436                         current protocol type is %d", pdpType )
       
   437                 if ( pdpType == aPdpType )
       
   438                     {
       
   439                     defaultIap = cm.GetIntAttributeL( CMManager::ECmIapId );
       
   440                     isFound  = ETrue;
       
   441                     LOG2( SIMPLE, "SATENGINE: CSatApnHandler::FindDefaultApL \
       
   442                             default iap had been found %d", defaultIap )
       
   443                     }
       
   444                 CleanupStack::PopAndDestroy( &cm );
       
   445                 }
       
   446             }
       
   447         CleanupStack::PopAndDestroy( &des );
       
   448         }
       
   449     CleanupStack::PopAndDestroy( &array );
       
   450     CleanupStack::PopAndDestroy( &cmManager );
       
   451 
       
   452     if ( !isFound )
       
   453         {
       
   454         LOG( SIMPLE, "SATENGINE: CSatApnHandler: default AP not found" );
       
   455         User::Leave( KErrNotFound );
       
   456         }
       
   457 
       
   458     LOG( SIMPLE, "SATENGINE: CSatApnHandler::FindDefaultApL exit" )
       
   459     return defaultIap;
       
   460     }
       
   461 //  End of File