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