omaprovisioning/provisioning/ProvisioningEngine/Src/CWPBootstrap.cpp
changeset 0 b497e44ab2fc
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2002 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 persistance of Bootstrap settings
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 //  INCLUDE FILES
       
    20 #include "CWPBootstrap.h"
       
    21 #include <badesca.h>
       
    22 #include <uri16.h>
       
    23 #include <CWPCharacteristic.h>
       
    24 #include <CWPParameter.h>
       
    25 #include <CWPEngine.h>
       
    26 #include "CWPPushMessage.h"
       
    27 #include "MWPContextManager.h"
       
    28 
       
    29 // CONSTANTS
       
    30 const TInt KProxiesGranularity = 3;
       
    31 const TInt KInitialHBufCSize = 1;
       
    32 const TInt KProvURLMaxLength = 50;
       
    33 _LIT( KHttp, "http://" );
       
    34 
       
    35 // ============================ MEMBER FUNCTIONS ===============================
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CWPBootstrap::CWPBootstrap
       
    39 // C++ default constructor can NOT contain any code, that
       
    40 // might leave.
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 CWPBootstrap::CWPBootstrap( const TDesC& aIMSI )
       
    44                         : iIMSI( aIMSI )
       
    45     {
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CWPBootstrap::ConstructL
       
    50 // Symbian 2nd phase constructor can leave.
       
    51 // -----------------------------------------------------------------------------
       
    52 //
       
    53 void CWPBootstrap::ConstructL()
       
    54     {
       
    55     iProxies = new(ELeave) CDesC16ArrayFlat( KProxiesGranularity );
       
    56     iTPS = HBufC::NewL( KInitialHBufCSize );
       
    57     iName = HBufC::NewL( KInitialHBufCSize );
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CWPCharacteristic::NewL
       
    62 // Two-phased constructor.
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 EXPORT_C CWPBootstrap* CWPBootstrap::NewL( const TDesC& aIMSI )
       
    66     {
       
    67     CWPBootstrap* self = NewLC( aIMSI );
       
    68     CleanupStack::Pop();
       
    69     
       
    70     return self;
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CWPCharacteristic::NewLC
       
    75 // Two-phased constructor.
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 CWPBootstrap* CWPBootstrap::NewLC( const TDesC& aIMSI )
       
    79     {
       
    80     CWPBootstrap* self = new( ELeave ) CWPBootstrap(aIMSI);
       
    81     
       
    82     CleanupStack::PushL( self );
       
    83     self->ConstructL();
       
    84     
       
    85     return self;
       
    86     }
       
    87 
       
    88 // -----------------------------------------------------------------------------
       
    89 // Destructor
       
    90 // -----------------------------------------------------------------------------
       
    91 CWPBootstrap::~CWPBootstrap()
       
    92     {
       
    93     delete iProxies;
       
    94     delete iTPS;
       
    95     delete iName;
       
    96     }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // CWPBootstrap::SaveL
       
   100 // -----------------------------------------------------------------------------
       
   101 //
       
   102 void CWPBootstrap::SaveL( CWPEngine& aEngine )
       
   103     {
       
   104     aEngine.CreateContextL( *iName, *iTPS, *iProxies );
       
   105     }
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // CWPBootstrap::LoadL
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 void CWPBootstrap::LoadL( CWPEngine& aEngine )
       
   112     {
       
   113     // Check if the saved settings apply to the current home network
       
   114     if( !aEngine.ContextExistsL( *iTPS ) )
       
   115         {
       
   116         iLoaded = EFalse;
       
   117         }
       
   118     else
       
   119         {
       
   120         // Find TPS
       
   121         TUint32 tps( aEngine.ContextL( *iTPS ) );
       
   122         CDesCArray* proxies = aEngine.ContextProxiesL( tps );
       
   123         CleanupStack::PushL( proxies );
       
   124         HBufC* name = aEngine.ContextNameL( tps );
       
   125         CleanupStack::Pop(); // proxies
       
   126 
       
   127         delete iProxies;
       
   128         iProxies = proxies;
       
   129 
       
   130         delete iName;
       
   131         iName = name;
       
   132 
       
   133         iLoaded = ETrue;
       
   134         }
       
   135     }
       
   136 
       
   137 // -----------------------------------------------------------------------------
       
   138 // CWPBootstrap::BootstrapL
       
   139 // -----------------------------------------------------------------------------
       
   140 //
       
   141 EXPORT_C CWPBootstrap::TBootstrapResult CWPBootstrap::BootstrapL( 
       
   142                                                     CWPPushMessage& aMessage,
       
   143                                                     CWPEngine& aEngine,
       
   144                                                     const TDesC& aPIN )
       
   145     {
       
   146     aEngine.AcceptL( *this );
       
   147 
       
   148     TBootstrapResult result( ENoBootstrap );
       
   149 
       
   150     if( !aMessage.Authenticated() )
       
   151         {
       
   152         result = DoAuthenticateL( aMessage, aPIN, aEngine );
       
   153         }
       
   154     else  if( iTPS->Length() > 0 )
       
   155         {
       
   156         result = DoBootstrapL( aEngine );
       
   157         }
       
   158 
       
   159     // Set TPS as the sender to later locate the correct configuration context
       
   160     if( result == ESucceeded )
       
   161         {
       
   162         HBufC8* orig = HBufC8::NewLC( iTPS->Length() );
       
   163         orig->Des().Copy( *iTPS );
       
   164         aMessage.SetOriginatorL( *orig );
       
   165         CleanupStack::PopAndDestroy(); // orig
       
   166         }
       
   167     
       
   168     return result;
       
   169     }
       
   170 
       
   171 // -----------------------------------------------------------------------------
       
   172 // CWPBootstrap::TPS
       
   173 // -----------------------------------------------------------------------------
       
   174 //
       
   175 EXPORT_C const TDesC& CWPBootstrap::TPS() const
       
   176     {
       
   177     return *iTPS;
       
   178     }
       
   179 
       
   180 // -----------------------------------------------------------------------------
       
   181 // CWPBootstrap::DoBootstrapL
       
   182 // -----------------------------------------------------------------------------
       
   183 //
       
   184 CWPBootstrap::TBootstrapResult CWPBootstrap::DoAuthenticateL( 
       
   185                                                     CWPPushMessage& aMessage,
       
   186                                                     const TDesC& aPIN,
       
   187                                                     CWPEngine& aEngine )
       
   188     {
       
   189     TBootstrapResult result( ENoBootstrap );
       
   190     TInt authResult( aMessage.AuthenticateL( iIMSI, aPIN ) );
       
   191     
       
   192     switch( authResult )
       
   193         {
       
   194         case KWPAuthResultAuthenticated:
       
   195             {
       
   196             if( iTPS->Length() > 0 )
       
   197                 {
       
   198                 result = DoBootstrapL( aEngine );
       
   199                 }
       
   200 
       
   201             aMessage.SetAuthenticated( ETrue );
       
   202             break;
       
   203             }
       
   204             
       
   205         case KWPAuthResultPinRequired:
       
   206             {
       
   207             result = EPinRequired;
       
   208             break;
       
   209             }
       
   210             
       
   211         case KWPAuthResultAuthenticationFailed:
       
   212             {
       
   213             result = EAuthenticationFailed;
       
   214             break;
       
   215             }
       
   216             
       
   217         case KWPAuthResultNoAuthentication:
       
   218             {
       
   219             if( iTPS->Length() > 0 )
       
   220                 {
       
   221                 result = ENotAuthenticated;
       
   222                 }
       
   223             else
       
   224                 {
       
   225                 result = ENoBootstrap;
       
   226                 }
       
   227             break;
       
   228             }
       
   229             
       
   230         default:
       
   231             {
       
   232             break;
       
   233             }
       
   234         }
       
   235 
       
   236     return result;
       
   237     }
       
   238 
       
   239 // -----------------------------------------------------------------------------
       
   240 // CWPBootstrap::DoBootstrapL
       
   241 // -----------------------------------------------------------------------------
       
   242 //
       
   243 CWPBootstrap::TBootstrapResult CWPBootstrap::DoBootstrapL( CWPEngine& aEngine )
       
   244     {
       
   245     TBootstrapResult result( ESucceeded );
       
   246 
       
   247     LoadL( aEngine );
       
   248     if( !iLoaded )
       
   249         {
       
   250         TRAPD( err, SaveL( aEngine ) );
       
   251 
       
   252         if( err == KErrNone )
       
   253             {
       
   254             result = ESucceeded;
       
   255             }
       
   256         else if( err == KErrAlreadyExists )
       
   257             {
       
   258             result = EBootstrapExists;
       
   259             }
       
   260         else
       
   261             {
       
   262             User::LeaveIfError( err );
       
   263             }
       
   264         }
       
   265     else
       
   266         {
       
   267         result = EBootstrapExists;
       
   268         }
       
   269 
       
   270     return result;
       
   271     }
       
   272 
       
   273 // -----------------------------------------------------------------------------
       
   274 // CWPBootstrap::VisitL
       
   275 // -----------------------------------------------------------------------------
       
   276 //
       
   277 void CWPBootstrap::VisitL( CWPCharacteristic& aCharacteristic )
       
   278     {
       
   279     TInt prevChar = iCurrentChar;
       
   280     iCurrentChar = aCharacteristic.Type();
       
   281 
       
   282     switch( aCharacteristic.Type() )
       
   283         {
       
   284         case KWPBootstrap:
       
   285             /* FALLTHROUGH */
       
   286         case KWPPxLogical:
       
   287             /* FALLTHROUGH */
       
   288         case KWPPxPhysical:
       
   289             {
       
   290             aCharacteristic.AcceptL( *this );
       
   291             break;
       
   292             }
       
   293         default:
       
   294             {
       
   295             break;
       
   296             }
       
   297         }
       
   298     iCurrentChar = prevChar;
       
   299     }
       
   300 
       
   301 // -----------------------------------------------------------------------------
       
   302 // CWPBootstrap::VisitL
       
   303 // -----------------------------------------------------------------------------
       
   304 //
       
   305 void CWPBootstrap::VisitL( CWPParameter& aParameter )
       
   306     {
       
   307     TPtrC value( aParameter.Value() );
       
   308     TPtrC http( KHttp );
       
   309 
       
   310     switch( aParameter.ID() )
       
   311         {
       
   312         case EWPParameterProvURL:
       
   313             {
       
   314             TInt ProvURLLength = value.Length();
       
   315             if( ProvURLLength <= KProvURLMaxLength && iCurrentChar == KWPBootstrap && iTPS->Length() == 0 )
       
   316                 {
       
   317                 HBufC* buf = HBufC::NewLC( ProvURLLength + http.Length() );
       
   318                 TPtr ptr( buf->Des() );
       
   319                 ptr.Copy( value );
       
   320 
       
   321                 if( ptr.Left( http.Length() ).CompareF( http ) != 0 )
       
   322                     {
       
   323                     ptr.Insert( 0, http );
       
   324                     }
       
   325 
       
   326                 TUriParser16 uri;
       
   327                 if( uri.Parse( ptr ) == KErrNone && uri.IsPresent(EUriHost) )
       
   328                     {
       
   329                     HBufC* provUrl = uri.Extract(EUriHost).AllocL();
       
   330                     delete iTPS;
       
   331                     iTPS = provUrl;
       
   332                     }
       
   333 
       
   334                 CleanupStack::PopAndDestroy(); // buf
       
   335                 }
       
   336             break;
       
   337             }
       
   338 
       
   339         case EWPParameterName:
       
   340             {
       
   341             if( iCurrentChar == KWPBootstrap && iName->Length() == 0 )
       
   342                 {
       
   343                 delete iName;
       
   344                 iName = NULL;
       
   345                 TInt length = aParameter.Value().Length();
       
   346                 if( length >= 50 ) 
       
   347                 {
       
   348                 TPtrC ptr = aParameter.Value().Left( 50 );
       
   349                 iName = ptr.AllocL();
       
   350                 }
       
   351 				else
       
   352 				{
       
   353 					iName = aParameter.Value().AllocL();
       
   354 				}
       
   355                 }
       
   356             break;
       
   357             }
       
   358 
       
   359         case EWPParameterPxAddr:
       
   360             {
       
   361             TInt pos;
       
   362             if( iCurrentChar == KWPPxPhysical 
       
   363                 && iProxies->Find( aParameter.Value(), pos ) != 0)
       
   364                 {
       
   365                 iProxies->AppendL( aParameter.Value() );
       
   366                 }
       
   367             break;
       
   368             }
       
   369 
       
   370         default:
       
   371             break;
       
   372         }
       
   373     }
       
   374 
       
   375 // -----------------------------------------------------------------------------
       
   376 // CWPBootstrap::VisitL
       
   377 // -----------------------------------------------------------------------------
       
   378 //
       
   379 void CWPBootstrap::VisitLinkL( CWPCharacteristic& aLink )
       
   380     {
       
   381     if( aLink.Type() == KWPPxLogical )
       
   382         {
       
   383         TInt prevChar = iCurrentChar;
       
   384         iCurrentChar = KWPPxLogical;
       
   385         aLink.AcceptL( *this );
       
   386         iCurrentChar = prevChar;
       
   387         }
       
   388     }
       
   389 
       
   390 //  End of File