voipplugins/sipconnectionprovider/src/scpprofilehandler.cpp
branchRCL_3
changeset 28 d38647835c2e
equal deleted inserted replaced
27:f742655b05bf 28:d38647835c2e
       
     1 /*
       
     2 * Copyright (c) 2007-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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <sip.h>
       
    20 #include <sipprofileregistry.h>
       
    21 #include <sipprofilealrcontroller.h>
       
    22 #include <sipprofile.h>
       
    23 #include <sipmanagedprofile.h>
       
    24 #include <sipmanagedprofileregistry.h>
       
    25 #include <ctsydomainpskeys.h>
       
    26 #include <bamdesca.h>
       
    27 #include <sipprofileregistryobserver.h> 
       
    28 #include "ipvmbxinterface.h"
       
    29 #include "scpprofilehandler.h"
       
    30 #include "scplogger.h"
       
    31 #include "scpsipconnection.h"
       
    32 #include "cipappphoneutils.h"
       
    33 #include "cipapputilsaddressresolver.h"
       
    34 
       
    35 #include "scputility.h"
       
    36 
       
    37 const TInt KTempStringlength = 200;
       
    38 const TUint KSCPUserAgentStringLength = 12;
       
    39 const TUint KSCPWlanMacAddressLength = 50;
       
    40 const TUint KSCPMaxTerminalTypeLength = 64;
       
    41 _LIT( KScpUserAgentString, "User-Agent: " );
       
    42 _LIT8 ( KSCPWlanMacAddressFrmt, "-" );
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CScpProfileHandler::CScpProfileHandler
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 CScpProfileHandler::CScpProfileHandler()
       
    49     {
       
    50     }
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // CScpProfileHandler::ConstructL
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 void CScpProfileHandler::ConstructL()
       
    57     {
       
    58     SCPLOGSTRING( "CScpProfileHandler::ConstructL" );
       
    59 
       
    60     iSip = CSIP::NewL( TUid::Null(), *this );
       
    61     iProfileRegistry = CSIPProfileRegistry::NewL( *iSip, *this );
       
    62     iManagedProfileRegistry = CSIPManagedProfileRegistry::NewL( *this );
       
    63     iAlrController = CSipProfileAlrController::NewL( *iProfileRegistry,
       
    64                                                      *this );
       
    65     }
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // CScpProfileHandler::NewL
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 CScpProfileHandler* CScpProfileHandler::NewL()
       
    72     {
       
    73     CScpProfileHandler* self =  new ( ELeave ) CScpProfileHandler();
       
    74     CleanupStack::PushL( self );
       
    75     self->ConstructL();
       
    76     CleanupStack::Pop( self );
       
    77     return self;
       
    78     }
       
    79  
       
    80 // -----------------------------------------------------------------------------
       
    81 // CScpProfileHandler::~CScpProfileHandler
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 CScpProfileHandler::~CScpProfileHandler()
       
    85     {  
       
    86     SCPLOGSTRING( "CScpProfileHandler::~CScpProfileHandler" );
       
    87 
       
    88     iObservers.Close();
       
    89     iSipConnections.ResetAndDestroy();
       
    90     iSipConnections.Close();
       
    91 
       
    92     delete iVmbxInterface;
       
    93     delete iAlrController;
       
    94     delete iManagedProfileRegistry;
       
    95     delete iProfileRegistry;    
       
    96     delete iSip;
       
    97     }
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 // CScpProfileHandler::AddObserverL
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 void CScpProfileHandler::AddObserverL( MScpSipConnectionObserver& aObserver )
       
   104     {
       
   105     SCPLOGSTRING2( "CScpProfileHandler::AddObserverL: 0x%x", &aObserver );
       
   106 
       
   107     TInt result = iObservers.Find( &aObserver );
       
   108 
       
   109     if ( result == KErrNotFound )
       
   110         {
       
   111         iObservers.AppendL( &aObserver );
       
   112         }
       
   113     else
       
   114         {
       
   115         User::Leave( KErrAlreadyExists );
       
   116         }
       
   117     }
       
   118     
       
   119 // -----------------------------------------------------------------------------
       
   120 // CScpProfileHandler::RemoveObserver
       
   121 // -----------------------------------------------------------------------------
       
   122 //
       
   123 TInt CScpProfileHandler::RemoveObserver( MScpSipConnectionObserver& aObserver )
       
   124     {
       
   125     SCPLOGSTRING2( "CScpProfileHandler::RemoveObserver: 0x%x", &aObserver );
       
   126 
       
   127     TInt result = iObservers.Find( &aObserver );
       
   128     
       
   129     if ( result != KErrNotFound )
       
   130         {
       
   131         iObservers.Remove( result );
       
   132         return KErrNone;
       
   133         }
       
   134 
       
   135     return KErrNotFound;
       
   136     }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 // CScpProfileHandler::GetCurrentState
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 TInt CScpProfileHandler::GetCurrentState( TUint32 aProfileId, 
       
   143     CScpSipConnection::TConnectionState& aState,
       
   144     TInt& aError ) const
       
   145     {
       
   146     SCPLOGSTRING( "CScpProfileHandler::GetCurrentState" );
       
   147 
       
   148     TInt result( KErrNone );
       
   149 
       
   150     // Go through own sip profiles and check state
       
   151     // If not found, create SipConnection and check state
       
   152     CScpSipConnection* sipConnection = GetSipConnection( aProfileId );
       
   153 
       
   154     TBool sipConnectionCreated = EFalse;
       
   155 
       
   156     if( !sipConnection )
       
   157         {
       
   158         sipConnectionCreated = ETrue;
       
   159         TRAP( result, sipConnection = CreateSipConnectionL( aProfileId ) );
       
   160         }
       
   161 
       
   162     if( result == KErrNone && sipConnection )
       
   163         {
       
   164         sipConnection->GetState( aState, aError );
       
   165         }
       
   166 
       
   167     if( sipConnectionCreated )
       
   168         {
       
   169         delete sipConnection;
       
   170         }
       
   171        
       
   172     return result;
       
   173     }
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CScpProfileHandler::RegisterProfileL
       
   177 // -----------------------------------------------------------------------------
       
   178 //
       
   179 void CScpProfileHandler::RegisterProfileL( TUint32 aProfileId )
       
   180     {
       
   181     SCPLOGSTRING2("CScpProfileHandler::RegisterProfileL profile id: %d", aProfileId );
       
   182 
       
   183     // Check if sip connection already exists
       
   184     TBool found( EFalse );
       
   185     CScpSipConnection* sipConnection( NULL );
       
   186     
       
   187     for( TInt i=0; i<iSipConnections.Count(); i++ )
       
   188         {
       
   189         sipConnection = iSipConnections[ i ];
       
   190 
       
   191         if( sipConnection->ProfileId() == aProfileId )
       
   192             {
       
   193             found = ETrue;
       
   194             sipConnection->EnableL();
       
   195             break;
       
   196             }    
       
   197         }
       
   198 
       
   199     // Otherwise create it
       
   200     if( !found )
       
   201         {
       
   202         sipConnection = CreateSipConnectionL( aProfileId );
       
   203         CleanupStack::PushL( sipConnection );        
       
   204         // try enabling
       
   205         sipConnection->AddObserver( *this );
       
   206         sipConnection->EnableL();
       
   207         // add to array
       
   208         iSipConnections.AppendL( sipConnection );
       
   209         CleanupStack::Pop( sipConnection );
       
   210         }
       
   211     }
       
   212 
       
   213 // -----------------------------------------------------------------------------
       
   214 // CScpProfileHandler::SipConnectionExists
       
   215 // -----------------------------------------------------------------------------
       
   216 //
       
   217 TBool CScpProfileHandler::SipConnectionExists( TUint32 aProfileId ) const
       
   218     {
       
   219     SCPLOGSTRING2("CScpProfileHandler::SipConnectionExists profile id: %d", aProfileId );
       
   220 
       
   221     return ( GetSipConnection( aProfileId ) != NULL );
       
   222     }
       
   223 
       
   224 // -----------------------------------------------------------------------------
       
   225 // CScpProfileHandler::GetSipConnection
       
   226 // -----------------------------------------------------------------------------
       
   227 //
       
   228 CScpSipConnection* CScpProfileHandler::GetSipConnection( TUint32 aProfileId ) const
       
   229     {
       
   230     SCPLOGSTRING2("CScpProfileHandler::GetSipConnection profile id: %d", aProfileId );
       
   231 
       
   232     CScpSipConnection* sipConnection( NULL );
       
   233     
       
   234     for( TInt i=0; i<iSipConnections.Count(); i++ )
       
   235         {
       
   236         sipConnection = iSipConnections[ i ];
       
   237 
       
   238         if( sipConnection->ProfileId() == aProfileId )
       
   239             {
       
   240             return sipConnection;
       
   241             }    
       
   242         }
       
   243 
       
   244     return NULL;
       
   245     }
       
   246 
       
   247 // -----------------------------------------------------------------------------
       
   248 // CScpProfileHandler::CreateSipConnectionL
       
   249 // -----------------------------------------------------------------------------
       
   250 //
       
   251 CScpSipConnection* CScpProfileHandler::CreateSipConnectionL( TUint32 aProfileId ) const
       
   252     {
       
   253     SCPLOGSTRING2("CScpProfileHandler::CreateSipConnectionL profile: %d", aProfileId);
       
   254 
       
   255     return CScpSipConnection::NewL( aProfileId,
       
   256                                     *iProfileRegistry,
       
   257                                     *iManagedProfileRegistry,
       
   258                                     *iSip );
       
   259     }
       
   260 
       
   261 // -----------------------------------------------------------------------------
       
   262 // CScpProfileHandler::UnRegisterProfile
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 TInt CScpProfileHandler::UnregisterProfile( TUint32 aProfileId )
       
   266     {
       
   267     SCPLOGSTRING2("CScpProfileHandler::UnregisterProfile id: %d", aProfileId );
       
   268 
       
   269     CScpSipConnection* sipConnection( NULL );    
       
   270 
       
   271     for( TInt i=0; i<iSipConnections.Count(); i++ )
       
   272         {
       
   273         sipConnection = iSipConnections[ i ];
       
   274 
       
   275         if( sipConnection->ProfileId() == aProfileId )
       
   276             {
       
   277             return sipConnection->Disable();
       
   278             }
       
   279         }
       
   280 
       
   281     return KErrNotFound;
       
   282     }
       
   283 
       
   284 // -----------------------------------------------------------------------------
       
   285 // CScpProfileHandler::CancelRegistration
       
   286 // -----------------------------------------------------------------------------
       
   287 //
       
   288 TInt CScpProfileHandler::CancelRegistration( TUint32 aProfileId )
       
   289     {
       
   290     SCPLOGSTRING2("CScpProfileHandler::UnregisterProfile id: %d", aProfileId );
       
   291 
       
   292     TInt result = UnregisterProfile( aProfileId );
       
   293 
       
   294     if( result == KErrNone ||
       
   295         result == KErrNotFound || 
       
   296         result == KErrArgument )
       
   297         {
       
   298         result = RemoveSipConnection( aProfileId );
       
   299         }
       
   300 
       
   301     return result;
       
   302     }
       
   303 
       
   304 // -----------------------------------------------------------------------------
       
   305 // CScpProfileHandler::IncomingRequest
       
   306 // -----------------------------------------------------------------------------
       
   307 //
       
   308 void CScpProfileHandler::IncomingRequest( TUint32 /*aIapId*/,
       
   309                                           CSIPServerTransaction* /*aTransaction*/ )
       
   310     {
       
   311     SCPLOGSTRING( "CScpProfileHandler::IncomingRequest" );
       
   312     }
       
   313 
       
   314 // -----------------------------------------------------------------------------
       
   315 // CScpProfileHandler::TimedOut
       
   316 // -----------------------------------------------------------------------------
       
   317 //
       
   318 void CScpProfileHandler::TimedOut(
       
   319     CSIPServerTransaction& /*aTransaction*/ )
       
   320     {
       
   321     SCPLOGSTRING( "CScpProfileHandler::TimedOut" );
       
   322     }
       
   323 
       
   324 // -----------------------------------------------------------------------------
       
   325 // CScpProfileHandler::RemoveSipConnection
       
   326 // -----------------------------------------------------------------------------
       
   327 //
       
   328 TInt CScpProfileHandler::RemoveSipConnection( TUint32 aProfileId )
       
   329     {
       
   330     SCPLOGSTRING2( "CScpProfileHandler::RemoveSipConnection id: %i", 
       
   331                    aProfileId );
       
   332 
       
   333     TInt ret = ( KErrNotFound );
       
   334     CScpSipConnection* sipConnection( NULL );
       
   335     
       
   336     sipConnection = GetSipConnection( aProfileId );
       
   337     
       
   338     if ( sipConnection )
       
   339         {
       
   340         TInt index = iSipConnections.Find( sipConnection );
       
   341         iSipConnections.Remove( index );
       
   342         delete sipConnection;
       
   343         ret = KErrNone;
       
   344         }
       
   345     return ret;
       
   346     }
       
   347 
       
   348 
       
   349 // -----------------------------------------------------------------------------
       
   350 // CScpProfileHandler::ProfileRegistryEventOccurred
       
   351 // -----------------------------------------------------------------------------
       
   352 //
       
   353 void CScpProfileHandler::ProfileRegistryEventOccurred(
       
   354     TUint32 aProfileId,
       
   355     MSIPProfileRegistryObserver::TEvent aEvent )
       
   356     {
       
   357     SCPLOGSTRING3( "CScpProfileHandler::ProfileRegistryEventOccurred id: %i event: %d", 
       
   358                    aProfileId, aEvent );
       
   359 
       
   360     CScpSipConnection* sipConnection = GetSipConnection( aProfileId );
       
   361     
       
   362     if( sipConnection )
       
   363         {
       
   364         sipConnection->ProfileRegistryEventOccurred( aEvent );
       
   365         
       
   366         // Deal with sip profile removals
       
   367         switch ( aEvent )
       
   368             {
       
   369             case EProfileDeregistered:
       
   370                 {
       
   371                 // Remove sip connection only if user has requested deregistration
       
   372                 CScpSipConnection::TRegistrationRequestState state =
       
   373                     sipConnection->RegistrationRequestState();
       
   374 
       
   375                 if( state == CScpSipConnection::EDeregistrationRequested )
       
   376                     {
       
   377                     RemoveSipConnection( aProfileId );
       
   378                     }
       
   379                 }
       
   380                 break;
       
   381 
       
   382             case EProfileDestroyed:
       
   383                 {
       
   384                 RemoveSipConnection( aProfileId );
       
   385                 }
       
   386                 break;
       
   387                
       
   388             default:
       
   389                 break;
       
   390             }
       
   391         }      
       
   392     }
       
   393 
       
   394 // -----------------------------------------------------------------------------
       
   395 // CScpProfileHandler::ProfileRegistryErrorOccurred
       
   396 // -----------------------------------------------------------------------------
       
   397 //
       
   398 void CScpProfileHandler::ProfileRegistryErrorOccurred( TUint32 aProfileId,
       
   399                                                        TInt aError )
       
   400     {
       
   401     SCPLOGSTRING3( "CScpProfileHandler::ProfileRegistryErrorOccurred profile:%i error: %d", 
       
   402                    aProfileId, aError );
       
   403 
       
   404     CScpSipConnection* sipConnection = GetSipConnection( aProfileId );
       
   405     
       
   406     if( sipConnection )
       
   407         {
       
   408         sipConnection->ProfileRegistryErrorOccurred( aError );
       
   409         }
       
   410     }
       
   411       
       
   412 // -----------------------------------------------------------------------------
       
   413 // CScpProfileHandler::ProfileExist
       
   414 // -----------------------------------------------------------------------------
       
   415 //
       
   416 TBool CScpProfileHandler::ProfileExists( TUint32 aProfileId ) const
       
   417     {    
       
   418     SCPLOGSTRING2( "CScpProfileHandler::ProfileExists id: %d", aProfileId );
       
   419 
       
   420     CSIPProfile* profile( NULL );
       
   421     TRAPD( result, profile = iProfileRegistry->ProfileL( aProfileId ) );
       
   422 
       
   423     if( result == KErrNone && profile )
       
   424         {
       
   425         delete profile;
       
   426         return ETrue;
       
   427         }
       
   428 
       
   429     return EFalse;             
       
   430     }
       
   431 
       
   432 // -----------------------------------------------------------------------------
       
   433 // CScpProfileHandler::SetSipProfileReserved
       
   434 // -----------------------------------------------------------------------------
       
   435 //
       
   436 void CScpProfileHandler::SetSipProfileReserved( TUint32 aProfileId, TBool aReserved )
       
   437     {
       
   438     SCPLOGSTRING3( "CScpProfileHandler::SetSipProfileReserved id: %d reserved: %d", 
       
   439                    aProfileId, aReserved );
       
   440 
       
   441     CScpSipConnection* sipConnection = GetSipConnection( aProfileId );
       
   442     
       
   443     if( sipConnection )
       
   444         {
       
   445         sipConnection->SetReserved( aReserved );
       
   446 
       
   447         // If profile was freed and iap availability alr event had come
       
   448         // before, let ALR controller to know that the profile is up
       
   449         // for ALR
       
   450         if( aReserved == EFalse && 
       
   451             sipConnection->IapAvailableOffered() )
       
   452             {
       
   453             TRAP_IGNORE( iAlrController->RefreshIapAvailabilityL( aProfileId ) );
       
   454     
       
   455             // Reset the iap availability offered flag
       
   456             sipConnection->SetIapAvailableOffered( EFalse );
       
   457             }
       
   458         }
       
   459     }
       
   460 
       
   461 // -----------------------------------------------------------------------------
       
   462 // CScpProfileHandler::StartAlrMigration
       
   463 // -----------------------------------------------------------------------------
       
   464 //
       
   465 void CScpProfileHandler::StartAlrMigration( TUint32 aProfileId )
       
   466     {
       
   467     SCPLOGSTRING2( "CScpProfileHandler::StartMigrtion profile: %i", aProfileId );
       
   468     SCPLOGSTRING2( "CScpProfileHandler::StartMigrtion iNewAlrIapId: %i", iNewAlrIapId );
       
   469     
       
   470     TRAP_IGNORE(
       
   471        iAlrController->AllowMigrationL( aProfileId, iNewAlrIapId ) );
       
   472     
       
   473     CScpSipConnection* sipConnection( NULL );
       
   474     sipConnection = GetSipConnection( aProfileId );
       
   475     
       
   476     if ( sipConnection )
       
   477         {
       
   478         sipConnection->SetProfileCurrentlyRoaming();
       
   479         }
       
   480     
       
   481     SCPLOGSTRING( "CScpProfileHandler::StartMigrtion -exit" );
       
   482     }
       
   483 
       
   484 // -----------------------------------------------------------------------------
       
   485 // CScpProfileHandler::AlrEvent
       
   486 // -----------------------------------------------------------------------------
       
   487 //
       
   488 void CScpProfileHandler::AlrEvent( MSipProfileAlrObserver::TEvent aEvent,
       
   489                                    TUint32 aProfileId,
       
   490 #ifdef _DEBUG
       
   491                                    TUint32 aSnapId,
       
   492 #else
       
   493                                    TUint32 /*aSnapId*/,
       
   494 #endif
       
   495                                    TUint32 aIapId )
       
   496     {
       
   497     SCPLOGSTRING2("CScpProfileHandler::AlrEvent profile:%i", aProfileId);
       
   498     SCPLOGSTRING2("CScpProfileHandler::AlrEvent aEvent:%i", (TInt)aEvent);
       
   499     SCPLOGSTRING2("CScpProfileHandler::AlrEvent aSnapId:%i", aSnapId);
       
   500     SCPLOGSTRING2("CScpProfileHandler::AlrEvent aIapId:%i", aIapId);
       
   501 
       
   502     CScpSipConnection* sipConnection( NULL );
       
   503     
       
   504     switch( aEvent )
       
   505         {
       
   506         case MSipProfileAlrObserver::EIapAvailable:
       
   507             {
       
   508             TBool reserved( EFalse );
       
   509             sipConnection = GetSipConnection( aProfileId );
       
   510     
       
   511             if( sipConnection )
       
   512                 {
       
   513                 reserved = sipConnection->Reserved();
       
   514 
       
   515                 if( reserved )
       
   516                     {
       
   517                     TRAPD( result, 
       
   518                         iAlrController->DisallowMigrationL( aProfileId, aIapId ) );
       
   519 
       
   520                     if( result == KErrNone )
       
   521                         {
       
   522                         // Raise the iap availability offered flag
       
   523                         sipConnection->SetIapAvailableOffered( ETrue );
       
   524                         }
       
   525                     }
       
   526                 else
       
   527                     {
       
   528                     iAlrAllowedToStartImmediately = ETrue;
       
   529 
       
   530                     for ( TInt i = 0; i < iObservers.Count(); i++ )
       
   531                         {
       
   532                         if ( !iObservers[ i ]->IsSipProfileAllowedToStartAlr() )
       
   533                             {
       
   534                             iAlrAllowedToStartImmediately = EFalse;
       
   535                             break;
       
   536                             }
       
   537                         }
       
   538                     if ( iAlrAllowedToStartImmediately )
       
   539                         {
       
   540                         TRAP_IGNORE(
       
   541                             iAlrController->AllowMigrationL( aProfileId, aIapId ) );
       
   542                         sipConnection->SetProfileCurrentlyRoaming();
       
   543                         }
       
   544                     else
       
   545                         {
       
   546                         iNewAlrIapId = aIapId;
       
   547                         sipConnection->HandleMigrationStarted();
       
   548                         }
       
   549                     }
       
   550                 }
       
   551             break;
       
   552             }
       
   553 
       
   554         // For possible beyond use (E.g. Maybe we should change CHH state
       
   555         // during iap migration -> VoIP call not possible if iap migration
       
   556         // ongoing.)
       
   557         case MSipProfileAlrObserver::EMigrationStarted:
       
   558             {
       
   559             sipConnection = GetSipConnection( aProfileId );
       
   560             
       
   561             if( sipConnection && iAlrAllowedToStartImmediately )
       
   562                 {
       
   563                 sipConnection->HandleMigrationStarted();
       
   564                 }
       
   565             break;
       
   566             }
       
   567         
       
   568         case MSipProfileAlrObserver::EMigrationCompleted:
       
   569         default:
       
   570             {
       
   571             break;
       
   572             }            
       
   573         }
       
   574     }
       
   575     
       
   576 // -----------------------------------------------------------------------------
       
   577 // CScpProfileHandler::AlrError
       
   578 // -----------------------------------------------------------------------------
       
   579 //
       
   580 void CScpProfileHandler::AlrError( 
       
   581     TInt aError,
       
   582     TUint32 aProfileId,
       
   583     TUint32 aSnapId,
       
   584     TUint32 aIapId )
       
   585     {
       
   586     SCPLOGSTRING("CScpProfileHandler::AlrError");
       
   587     SCPLOGSTRING2("     profile id: %d", aProfileId);
       
   588     SCPLOGSTRING2("     error:      %d", aError);
       
   589     
       
   590     CScpSipConnection* sipConnection = GetSipConnection( aProfileId );
       
   591     if( sipConnection )
       
   592         {
       
   593         sipConnection->HandleMigrationError( 
       
   594             aError, aProfileId, aSnapId, aIapId );
       
   595         }
       
   596     }
       
   597 
       
   598 // -----------------------------------------------------------------------------
       
   599 // CScpProfileHandler::HandleSipConnectionEvent
       
   600 // -----------------------------------------------------------------------------
       
   601 //
       
   602 void CScpProfileHandler::HandleSipConnectionEvent( TUint32 aProfileId,
       
   603                                                    TScpConnectionEvent aSipEvent )
       
   604     {
       
   605     SCPLOGSTRING3( 
       
   606         "CScpProfileHandler::HandleSipConnectionEvent profile id: %d sip event: %d",
       
   607         aProfileId, aSipEvent );
       
   608 
       
   609     // Make a copy of the observers array, since observers
       
   610     // may be removed during the event handling
       
   611     RPointerArray< MScpSipConnectionObserver > observers;
       
   612     
       
   613     for ( TInt i=0; i < iObservers.Count(); i++ )
       
   614         {      
       
   615         observers.Append( iObservers[ i ] );
       
   616         }
       
   617 
       
   618     // Send notify to observers
       
   619     for( TInt i=0; i < observers.Count(); i++ )
       
   620         {
       
   621         //check that the observer is still valid before triggering notify
       
   622         if ( KErrNotFound != iObservers.Find( observers[ i ] ) )
       
   623             {
       
   624             observers[ i ]->HandleSipConnectionEvent( aProfileId, aSipEvent );
       
   625             }
       
   626         }
       
   627 
       
   628     observers.Close();
       
   629         
       
   630     // if registration failed, time to delete connection
       
   631     if ( aSipEvent == EScpRegistrationFailed )
       
   632         {
       
   633         RemoveSipConnection( aProfileId );        
       
   634         }
       
   635     }
       
   636 
       
   637 // -----------------------------------------------------------------------------
       
   638 // CScpProfileHandler::IsSipProfileAllowedToStartAlr
       
   639 // -----------------------------------------------------------------------------
       
   640 //
       
   641 TBool CScpProfileHandler::IsSipProfileAllowedToStartAlr()
       
   642     {
       
   643     return ETrue;
       
   644     }
       
   645     
       
   646 // -----------------------------------------------------------------------------
       
   647 // CScpProfileHandler::VmbxInterfaceL
       
   648 // -----------------------------------------------------------------------------
       
   649 //
       
   650 CIpVmbxInterface& CScpProfileHandler::VmbxInterfaceL( MIpVmbxObserver& aObserver )
       
   651     {
       
   652     SCPLOGSTRING( "CScpProfileHandler::VmbxInterfaceL" );
       
   653 
       
   654     // Todo: save observer and set MIpVmbxObserver to this class
       
   655 	// problem when several vmbx services, current observer logic isn't work
       
   656     
       
   657     if( !iVmbxInterface )
       
   658         {
       
   659         iVmbxInterface = CIpVmbxInterface::NewL( aObserver );
       
   660         }
       
   661 
       
   662     return *iVmbxInterface;
       
   663     }
       
   664 
       
   665 // -----------------------------------------------------------------------------
       
   666 // CScpProfileHandler::DeleteVmbxInterface
       
   667 // -----------------------------------------------------------------------------
       
   668 //
       
   669 void CScpProfileHandler::DeleteVmbxInterface()
       
   670     {
       
   671     SCPLOGSTRING( "CScpProfileHandler::DeleteVmbxInterface" );
       
   672 
       
   673     if( iVmbxInterface )
       
   674         {
       
   675         delete iVmbxInterface;
       
   676         iVmbxInterface = NULL;
       
   677         }
       
   678     }
       
   679 
       
   680 // -----------------------------------------------------------------------------
       
   681 // CScpProfileHandler::UpdateSipProfile
       
   682 // -----------------------------------------------------------------------------
       
   683 //
       
   684 void CScpProfileHandler::UpdateSipProfileL( 
       
   685     TUint32 aProfileId,
       
   686     TBool aTerminalType,
       
   687     TBool aWlanMac,
       
   688     TInt aStringLength )
       
   689     {
       
   690     SCPLOGSTRING( "CScpProfileHandler::UpdateSipProfileL IN" );
       
   691     SCPLOGSTRING2( " profile id:    %d", aProfileId );
       
   692     SCPLOGSTRING2( " terminal type: %d", aTerminalType );
       
   693     SCPLOGSTRING2( " wlan mac:      %d", aWlanMac );
       
   694     SCPLOGSTRING2( " string length: %d", aStringLength );
       
   695     
       
   696     // Do not update, if terminal type is not defined in user agent header
       
   697     if ( !aTerminalType )
       
   698         {
       
   699         return;
       
   700         }
       
   701     
       
   702     const MDesC8Array* array;
       
   703     CSIPProfile* sipProfile = iProfileRegistry->ProfileL( aProfileId );
       
   704     CleanupStack::PushL( sipProfile );
       
   705     User::LeaveIfError( sipProfile->GetParameter( KSIPHeaders, array ) );
       
   706 
       
   707     TBuf<KSCPMaxTerminalTypeLength> terminalType( KNullDesC );
       
   708     TBuf<KTempStringlength> tempHeader( KNullDesC );
       
   709     
       
   710     // 1. Get the terminal type and seek is user agent header's 
       
   711     // terminal type same, if so no need to continue
       
   712     CIpAppPhoneUtils* libIpAppPhoneUtils = CIpAppPhoneUtils::NewLC();
       
   713     libIpAppPhoneUtils->GetTerminalTypeL( terminalType );   
       
   714     CleanupStack::PopAndDestroy( libIpAppPhoneUtils );
       
   715     
       
   716     for( TInt t( 0 ); t < array->MdcaCount(); t++ )
       
   717         {
       
   718         tempHeader.Copy( array->MdcaPoint( t ) );
       
   719         if( tempHeader.Find( terminalType ) != KErrNotFound )
       
   720             {
       
   721             SCPLOGSTRING(" terminal type found");
       
   722             CleanupStack::PopAndDestroy( sipProfile );
       
   723             return;
       
   724             }
       
   725         }
       
   726     
       
   727     // 2. If appropriate terminal type not found, we need to update it
       
   728     if ( array->MdcaCount() )
       
   729         {
       
   730         TBuf8<KTempStringlength> userAgentHeader;
       
   731         CDesC8ArrayFlat* uahArray = new ( ELeave ) CDesC8ArrayFlat( 1 );
       
   732         CleanupStack::PushL( uahArray );
       
   733         //Find the User-Agent string
       
   734         for( TInt t( 0 ); t < array->MdcaCount(); t++ )
       
   735             {
       
   736             tempHeader.Copy( array->MdcaPoint( t ) );
       
   737             // user-Agent found, now change it
       
   738             if( tempHeader.Find( KScpUserAgentString ) != KErrNotFound )
       
   739                 {
       
   740                 // 3. now we have the right user agent header which is not correct
       
   741                 // replace the old terminal type 
       
   742                 TInt len( KErrNone );
       
   743                 // Get mac if defined
       
   744                 if ( aWlanMac )
       
   745                     {
       
   746                     TBuf8<KSCPWlanMacAddressLength> wlanMacAddress( KNullDesC8 );
       
   747                     TBuf<KSCPWlanMacAddressLength>  tempAddr( KNullDesC );
       
   748                     CIPAppUtilsAddressResolver* addressResolver = 
       
   749                         CIPAppUtilsAddressResolver::NewLC();    
       
   750                     User::LeaveIfError( addressResolver->GetWlanMACAddress( 
       
   751                         wlanMacAddress, KSCPWlanMacAddressFrmt ) );
       
   752                     CleanupStack::PopAndDestroy( addressResolver );
       
   753                     tempAddr.Copy( wlanMacAddress );
       
   754                     
       
   755                     // leave the space before MAC ADDR (-1)
       
   756                     len = tempHeader.Find( tempAddr ) - KSCPUserAgentStringLength - 1;
       
   757                     }
       
   758                 else
       
   759                     {
       
   760                     // leave the space before free string (-1)
       
   761                     len = tempHeader.Length() - KSCPUserAgentStringLength - aStringLength - 1;
       
   762                     }
       
   763                 // make sure that the len is correct value(not below zero)
       
   764                 if ( 0 >= len )
       
   765                     {
       
   766                     User::Leave( KErrGeneral );
       
   767                     }
       
   768                 tempHeader.Replace( KSCPUserAgentStringLength, len, terminalType );
       
   769                 SCPLOGSTRING2(" header:        %S", &tempHeader);
       
   770                 userAgentHeader.Append( tempHeader );
       
   771                 uahArray->AppendL( userAgentHeader );
       
   772                 }
       
   773             else
       
   774                 {
       
   775                 //if this entry is not user agent, leave it untouched
       
   776                 uahArray->AppendL( array->MdcaPoint( t ) );    
       
   777                 }
       
   778             }
       
   779 
       
   780         CSIPManagedProfile* managedProfile = static_cast<CSIPManagedProfile*>( 
       
   781             iProfileRegistry->ProfileL( aProfileId ) );
       
   782         CleanupStack::PushL( managedProfile );
       
   783         //Update the new user-agent to sipprofile
       
   784         User::LeaveIfError( 
       
   785             managedProfile->SetParameter( KSIPHeaders, *uahArray ) );
       
   786         iManagedProfileRegistry->SaveL( *managedProfile );
       
   787         CleanupStack::PopAndDestroy( managedProfile );
       
   788         CleanupStack::PopAndDestroy( uahArray );
       
   789         }        
       
   790     CleanupStack::PopAndDestroy( sipProfile );    
       
   791     SCPLOGSTRING( "CScpProfileHandler::UpdateSipProfileL OUT" );
       
   792     }
       
   793 
       
   794 // -----------------------------------------------------------------------------
       
   795 // CScpProfileHandler::SetUsernameAndPasswordL
       
   796 // -----------------------------------------------------------------------------
       
   797 //
       
   798 void CScpProfileHandler::SetUsernameAndPasswordL( TUint32 aProfileId,
       
   799                                                   const TDesC8& aUsername,
       
   800                                                   TBool aSetUsername,
       
   801                                                   const TDesC8& aPassword,
       
   802                                                   TBool aSetPassword )
       
   803     {
       
   804     SCPLOGSTRING( "CScpProfileHandler::SetUsernameAndPasswordL" );
       
   805 
       
   806     CSIPManagedProfile* profile = static_cast<CSIPManagedProfile*>
       
   807                          (iProfileRegistry->ProfileL( aProfileId ));
       
   808     CleanupStack::PushL( profile );
       
   809 
       
   810     SCPLOGSTRING( "CScpProfileHandler::SetUsernameAndPasswordL sip profile availble" );
       
   811     
       
   812     if( aSetUsername )
       
   813         {
       
   814         RBuf8 formattedUsername;
       
   815         CleanupClosePushL( formattedUsername );
       
   816         
       
   817         if( !TScpUtility::CheckSipUsername( aUsername ) )
       
   818             {
       
   819             // Since username is returned with prefix and domain, it needs
       
   820             // to be possible to also set it with prefix and domain. 
       
   821             // Strip prefix and domain from user aor if found.
       
   822             // Prefix might be sip or sips so search by colon.
       
   823             // If username is empty, leave with KErrArgument
       
   824             if ( aUsername == KNullDesC8 )
       
   825                 {
       
   826                 User::Leave( KErrArgument );
       
   827                 }
       
   828             else
       
   829                 {
       
   830                 User::LeaveIfError( TScpUtility::RemovePrefixAndDomain( 
       
   831                     aUsername, formattedUsername ) );
       
   832                 }
       
   833             }
       
   834         else
       
   835             {
       
   836             formattedUsername.CreateL( aUsername.Length() );
       
   837             formattedUsername.Copy( aUsername );
       
   838             }
       
   839         
       
   840         TBool validAorExists( EFalse );       
       
   841         const TDesC8* aorPtr( NULL );
       
   842         
       
   843         TInt err = profile->GetParameter( KSIPUserAor, aorPtr );
       
   844         
       
   845         TInt loc( KErrNotFound );
       
   846         if ( !err && aorPtr->Length() )
       
   847             {
       
   848             loc = aorPtr->Find( KAt() );
       
   849             
       
   850             if ( KErrNotFound != loc )
       
   851                 {         
       
   852                 TPtrC8 ptr = aorPtr->Right( 1 );
       
   853                 
       
   854                 if ( ptr.Compare( KAt ) != 0 )
       
   855                     {                    
       
   856                     // @ is found and there is something in domain part
       
   857                     validAorExists = ETrue;
       
   858                     }              
       
   859                 }
       
   860             }
       
   861        
       
   862        HBufC8* newAor = NULL;
       
   863        TBuf8<KSipSchemeMaxLength> prefix;
       
   864        TScpUtility::GetValidPrefix( aUsername, prefix );
       
   865        
       
   866        if ( validAorExists )
       
   867            {           
       
   868            SCPLOGSTRING( "CScpProfileHandler::SetUsernameAndPasswordL valid aor" );
       
   869            TPtrC8 domainPart = aorPtr->Mid( loc );
       
   870            
       
   871            newAor = HBufC8::NewLC( prefix.Length() + 
       
   872                                    domainPart.Length() + 
       
   873                                    formattedUsername.Length() );
       
   874            newAor->Des().Append( prefix );
       
   875            newAor->Des().Append( formattedUsername );
       
   876            newAor->Des().Append( domainPart );
       
   877            }
       
   878        else
       
   879            {         
       
   880            SCPLOGSTRING( "CScpProfileHandler::SetUsernameAndPasswordL no valid aor" );
       
   881            // Check if given username contains valid aor
       
   882            TInt loc = aUsername.Find( KAt() );
       
   883            
       
   884            if ( KErrNotFound != loc )
       
   885                {               
       
   886                TPtrC8 ptrRight = aorPtr->Right( 1 );
       
   887                TPtrC8 ptrLeft = aorPtr->Left( 1 );
       
   888                
       
   889                if ( ( ptrRight.Compare( KAt ) != 0 ) && 
       
   890                    ( ptrLeft.Compare( KAt ) != 0 ) )
       
   891                    {                   
       
   892                    // both sides of @ have data --> aor is valid
       
   893                    newAor = HBufC8::NewLC( aUsername.Length() );
       
   894                    newAor->Des().Copy( aUsername );
       
   895                    }
       
   896                else
       
   897                    {
       
   898                    User::Leave( KErrArgument );
       
   899                    }
       
   900                }
       
   901            else
       
   902                {               
       
   903                User::Leave( KErrArgument );
       
   904                }    
       
   905            }
       
   906 
       
   907         SCPLOGSTRING( "CScpProfileHandler::SetUsernameAndPasswordL set new aor value" );
       
   908         
       
   909         User::LeaveIfError( profile->SetParameter( KSIPUserAor, newAor->Des() ) );
       
   910         CleanupStack::PopAndDestroy( newAor );
       
   911         
       
   912         // Add digest username & password
       
   913         SCPLOGSTRING( "CScpProfileHandler:SetUsernameAndPasswordL outbound & digest username" );
       
   914         User::LeaveIfError( profile->SetParameter( KSIPOutboundProxy,
       
   915                                                    KSIPDigestUserName,
       
   916                                                    formattedUsername ) );
       
   917 
       
   918 
       
   919         SCPLOGSTRING( "CScpProfileHandler:SetUsernameAndPasswordL registrar & digest username" );
       
   920         User::LeaveIfError( profile->SetParameter( KSIPRegistrar,
       
   921                                                    KSIPDigestUserName,
       
   922                                                    formattedUsername ) );
       
   923         CleanupStack::PopAndDestroy( &formattedUsername );                                                   
       
   924         }
       
   925 
       
   926     if( aSetPassword )
       
   927         {
       
   928         if( !TScpUtility::CheckSipPassword( aPassword ) )
       
   929             {
       
   930             User::Leave( KErrArgument );
       
   931             }
       
   932         SCPLOGSTRING( "CScpProfileHandler:SetUsernameAndPasswordL outbound & digest pwd" );
       
   933         User::LeaveIfError( profile->SetParameter( KSIPOutboundProxy,
       
   934                                                    KSIPDigestPassword,
       
   935                                                    aPassword ) );    
       
   936         SCPLOGSTRING( "CScpProfileHandler:SetUsernameAndPasswordL registrar & digest pwd" );
       
   937         User::LeaveIfError( profile->SetParameter( KSIPRegistrar,
       
   938                                                    KSIPDigestPassword,
       
   939                                                    aPassword ) );
       
   940         }
       
   941     
       
   942     SCPLOGSTRING( "CScpProfileHandler:SetUsernameAndPasswordL save sip changes" );
       
   943 
       
   944     // Save changes
       
   945     TRAPD( err, iManagedProfileRegistry->SaveL( *profile ) );
       
   946 
       
   947     CleanupStack::PopAndDestroy( profile );    
       
   948     
       
   949     SCPLOGSTRING2( "CScpProfileHandler:SetUsernameAndPasswordL save sip err: %d", err );
       
   950     User::LeaveIfError( err );
       
   951     }
       
   952 
       
   953 
       
   954 
       
   955 // -----------------------------------------------------------------------------
       
   956 // CScpProfileHandler::GetDebugInfo
       
   957 // -----------------------------------------------------------------------------
       
   958 //
       
   959 #ifdef _DEBUG
       
   960 void CScpProfileHandler::GetDebugInfo( TDes& aInfo ) const
       
   961     {
       
   962     TInt connections = iSipConnections.Count();
       
   963     TInt registeredConnections( 0 );
       
   964     TInt registeringConnections( 0 );
       
   965     TInt unregisteringConnections( 0 );
       
   966     TInt unregisteredConnections( 0 );
       
   967     
       
   968     for( TInt i=0; i<iSipConnections.Count(); i++ )
       
   969         {
       
   970         CScpSipConnection* sipConnection = iSipConnections[ i ];
       
   971         CScpSipConnection::TConnectionState state;
       
   972         TInt error;
       
   973 
       
   974         sipConnection->GetState( state, error );
       
   975 
       
   976         switch( state )
       
   977             {
       
   978             case CScpSipConnection::ERegistered:
       
   979                 registeredConnections++;
       
   980                 break;
       
   981 
       
   982             case CScpSipConnection::ERegistering:
       
   983                 registeringConnections++;
       
   984                 break;
       
   985 
       
   986             case CScpSipConnection::EDeregistering:
       
   987                 unregisteringConnections++;
       
   988                 break;
       
   989 
       
   990             case CScpSipConnection::EDeregistered:
       
   991                 unregisteredConnections++;
       
   992                 break;
       
   993 
       
   994             default:
       
   995                 break;
       
   996             }
       
   997         }
       
   998 
       
   999     TBuf< 255 > buffer;
       
  1000     buffer.Format( _L( "Connections: %d\n Registered: %d\n Registering: %d\n Unregistering: %d\n Unregistered: %d\n" ),
       
  1001                         connections, registeredConnections, registeringConnections,
       
  1002                         unregisteringConnections, unregisteredConnections );
       
  1003 
       
  1004     aInfo.Append( buffer );
       
  1005     }
       
  1006 #endif
       
  1007             
       
  1008 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
  1009 
       
  1010 //  End of File