voipplugins/sipconnectionprovider/src/scpsipconnection.cpp
changeset 0 a4daefaec16c
child 10 ed1e38b404e5
equal deleted inserted replaced
-1:000000000000 0:a4daefaec16c
       
     1 /*
       
     2 * Copyright (c) 2007-2009 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:  Interface to SIP profiles.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <sipprofileregistry.h>
       
    19 #include <sipprofile.h>
       
    20 #include <sipmanagedprofile.h>
       
    21 #include <sipmanagedprofileregistry.h>
       
    22 #include <sipconnection.h>
       
    23 #include <commdb.h>
       
    24 #include <ccherror.h>
       
    25 
       
    26 #include "scpsipconnection.h"
       
    27 #include "scplogger.h"
       
    28 #include "scpsipconnectionobserver.h"
       
    29 #include "scputility.h"
       
    30 #include "voipeventlog.h"
       
    31 
       
    32 // If EnableL fails a timeout for reporting error
       
    33 const TInt KEnableTimeout = 120000000;
       
    34 
       
    35 const TInt KUsernameMaxLength = 255;
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CScpSipConnection::CScpSipConnection
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 CScpSipConnection::CScpSipConnection( 
       
    42     TInt aProfileId, 
       
    43     CSIPProfileRegistry& aProfileRegistry,
       
    44     CSIPManagedProfileRegistry& aManagedProfileRegistry,
       
    45     CSIP& aSip ) :
       
    46     iProfileId( aProfileId ),
       
    47     iProfileRegistry( aProfileRegistry ),
       
    48     iManagedProfileRegistry( aManagedProfileRegistry ),
       
    49     iSip( aSip ),
       
    50     iRegistrationRequestState( ENoRequest ),
       
    51     iConnectionStateError( KErrNone ),
       
    52     iReserved( EFalse ),
       
    53     iIapAvailableOffered( EFalse )
       
    54     {
       
    55     SCPLOGSTRING3( "CScpSipConnection[0x%x]::CScpSipConnection profile id: %d", 
       
    56                    this, aProfileId );
       
    57     }
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 // CScpSipConnection::ConstructL
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 void CScpSipConnection::ConstructL()
       
    64     {
       
    65     SCPLOGSTRING2( "CScpSipConnection[0x%x]::ConstructL", this );
       
    66 
       
    67     iSipProfile = iProfileRegistry.ProfileL( iProfileId );
       
    68 
       
    69     if ( !iSipProfile )
       
    70         {
       
    71         User::Leave( KErrNotFound );
       
    72         }
       
    73     
       
    74     iEnableTimeoutTimer = CPeriodic::NewL( CPeriodic::EPriorityStandard );
       
    75     
       
    76     User::LeaveIfError( iConnectionMonitor.ConnectL() );
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CScpSipConnection::NewL
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 CScpSipConnection* CScpSipConnection::NewL( 
       
    84     TInt aProfileId, 
       
    85     CSIPProfileRegistry& aProfileRegistry,
       
    86     CSIPManagedProfileRegistry& aManagedProfileRegistry,
       
    87     CSIP& aSip )
       
    88     {
       
    89     SCPLOGSTRING( "CScpSipConnection::NewL" );
       
    90     __ASSERT_DEBUG( aProfileId > 0, User::Panic( KNullDesC, KErrGeneral ) );
       
    91 
       
    92     CScpSipConnection* self = new (ELeave) CScpSipConnection( aProfileId,
       
    93                                                               aProfileRegistry,
       
    94                                                               aManagedProfileRegistry,
       
    95                                                               aSip );
       
    96     CleanupStack::PushL( self );
       
    97     self->ConstructL();
       
    98     CleanupStack::Pop( self );
       
    99 
       
   100     return self;
       
   101     }
       
   102     
       
   103 // -----------------------------------------------------------------------------
       
   104 // CScpSipConnection::~CScpSipConnection
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 CScpSipConnection::~CScpSipConnection()
       
   108     {  
       
   109     SCPLOGSTRING2( "CScpSipConnection[0x%x]::~CScpSipConnection", this );
       
   110 
       
   111     delete iSipConnection;
       
   112     iConnectionMonitor.Close();
       
   113     delete iEnableTimeoutTimer;
       
   114     delete iSipProfile;
       
   115     }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // CScpSipConnection::AddObserver
       
   119 // -----------------------------------------------------------------------------
       
   120 //
       
   121 void CScpSipConnection::AddObserver( MScpSipConnectionObserver& aObserver )
       
   122     {
       
   123     SCPLOGSTRING3( "CScpSipConnection[0x%x]::AddObserverL: 0x%x",this, &aObserver );
       
   124 
       
   125     iObserver = &aObserver;
       
   126     } 
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 // CScpSipConnection::RemoveObserver
       
   130 // -----------------------------------------------------------------------------
       
   131 //
       
   132 TInt CScpSipConnection::RemoveObserver( MScpSipConnectionObserver& aObserver )
       
   133     {
       
   134     SCPLOGSTRING3( "CScpSipConnection[0x%x]::RemoveObserver: 0x%x", this, &aObserver );
       
   135 
       
   136     if ( &aObserver == iObserver )
       
   137         {
       
   138         iObserver = NULL;
       
   139         return KErrNone;
       
   140         }
       
   141 
       
   142     return KErrNotFound;
       
   143     }
       
   144 
       
   145 // -----------------------------------------------------------------------------
       
   146 // CScpSipConnection::ProfileId
       
   147 // -----------------------------------------------------------------------------
       
   148 //
       
   149 TInt CScpSipConnection::ProfileId() const
       
   150     {
       
   151     SCPLOGSTRING2( "CScpSipConnection[0x%x]::ProfileId", this );
       
   152 
       
   153     return iProfileId;
       
   154     }
       
   155 
       
   156 // -----------------------------------------------------------------------------
       
   157 // CScpSipConnection::EnableL
       
   158 // -----------------------------------------------------------------------------
       
   159 //
       
   160 void CScpSipConnection::EnableL()
       
   161     {
       
   162     SCPLOGSTRING2( "CScpSipConnection[0x%x]::EnableL", this );
       
   163 
       
   164     if ( iRegistrationRequestState != ERegistrationRequested )
       
   165         {
       
   166         iConnectionStateError = KErrNone;
       
   167         iRegistrationRequestState = ERegistrationRequested;
       
   168 
       
   169         TUint32 iapId( 0 );
       
   170         TUint32 snapId( 0 );
       
   171 
       
   172         TInt result = GetSnap( snapId );
       
   173 
       
   174         if ( result == KErrNotFound || snapId == 0 )
       
   175             {
       
   176             SCPLOGSTRING2( "CScpSipConnection[0x%x]::EnableL - SNAP not found", this );
       
   177             result = GetIap( iapId );
       
   178             }
       
   179    
       
   180         // If username is not set leave directly with KCCHErrorAuthenticationFailed
       
   181         if ( !IsUsernameSetL() )
       
   182             {
       
   183             SCPLOGSTRING2( "CScpSipConnection[0x%x]::EnableL - AuthenticationFailed !!!", this );
       
   184             User::Leave( KCCHErrorAuthenticationFailed );
       
   185             }
       
   186         
       
   187         iProfileRegistry.EnableL( *iSipProfile, *this );
       
   188           
       
   189         if ( !iSipConnection && 0 != iapId )
       
   190              {
       
   191              iSipConnection = CSIPConnection::NewL( iSip, iapId, *this );
       
   192              }
       
   193         
       
   194         TBool connectionAvailable = IsNetworkConnectionAvailable();
       
   195         
       
   196         if ( connectionAvailable )
       
   197             {
       
   198             TBool registered( EFalse );
       
   199             User::LeaveIfError( iSipProfile->GetParameter( KSIPProfileRegistered, 
       
   200                                                            registered ) );
       
   201 
       
   202             if ( !registered )
       
   203                 {
       
   204                 StartEnableTimeoutTimer( CScpSipConnection::EnableTimeout );
       
   205                 }
       
   206             }        
       
   207         else
       
   208             {
       
   209             iConnectionStateError = KCCHErrorNetworkLost;
       
   210             }
       
   211         }
       
   212     }
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // CScpSipConnection::Disable
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 TInt CScpSipConnection::Disable()
       
   219     {
       
   220     SCPLOGSTRING2( "CScpSipConnection[0x%x]::Disable", this );
       
   221 
       
   222     iRegistrationRequestState = EDeregistrationRequested;
       
   223     
       
   224     CancelEnableTimeoutTimer();
       
   225     
       
   226     delete iSipConnection;
       
   227     iSipConnection = NULL;
       
   228     
       
   229     TInt err = iProfileRegistry.Disable( *iSipProfile );
       
   230     TRAPD( err1, DisableAlwaysOnModeL() );
       
   231        
       
   232     if ( err == KErrNone )
       
   233         {
       
   234         err = err1;
       
   235         }
       
   236 
       
   237     return err;
       
   238     }
       
   239 
       
   240 // -----------------------------------------------------------------------------
       
   241 // CScpSipConnection::DisableAlwaysOnModeL()
       
   242 // -----------------------------------------------------------------------------
       
   243 //
       
   244 void CScpSipConnection::DisableAlwaysOnModeL()
       
   245     {
       
   246     SCPLOGSTRING2( "CScpSipConnection[0x%x]::DisableAlwaysOnModeL", this );
       
   247 
       
   248     TBool autoRegistration( EFalse );
       
   249     User::LeaveIfError( iSipProfile->GetParameter( KSIPAutoRegistration, 
       
   250                                                    autoRegistration ) );
       
   251 
       
   252     if ( autoRegistration )
       
   253         {
       
   254         CSIPManagedProfile* profile = static_cast<CSIPManagedProfile*> ( iSipProfile );
       
   255         User::LeaveIfError( profile->SetParameter( KSIPAutoRegistration, EFalse ) );
       
   256         iManagedProfileRegistry.SaveL( *profile );
       
   257         }
       
   258     }
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 // CScpSipConnection::RegistrationRequestState
       
   262 // -----------------------------------------------------------------------------
       
   263 //
       
   264 CScpSipConnection::TRegistrationRequestState 
       
   265     CScpSipConnection::RegistrationRequestState() const
       
   266     {
       
   267     SCPLOGSTRING2( "CScpSipConnection[0x%x]::RegistrationRequestState", this );
       
   268 
       
   269     return iRegistrationRequestState;
       
   270     }
       
   271 
       
   272 // -----------------------------------------------------------------------------
       
   273 // CScpSipConnection::GetState
       
   274 // -----------------------------------------------------------------------------
       
   275 //
       
   276 void CScpSipConnection::GetState( CScpSipConnection::TConnectionState& aState,
       
   277                                   TInt& aError ) const
       
   278     {
       
   279     SCPLOGSTRING2( "CScpSipConnection[0x%x]::GetState", this );
       
   280 
       
   281     aError = iConnectionStateError;
       
   282 
       
   283     TBool registered( EFalse );
       
   284     TInt result = iSipProfile->GetParameter( KSIPProfileRegistered, registered );
       
   285     
       
   286     if ( result == KErrNone )
       
   287         {
       
   288         switch ( iRegistrationRequestState )
       
   289             {
       
   290             case ENoRequest:
       
   291                 if( registered )
       
   292                     {
       
   293                     aState = ERegistered;
       
   294                     }
       
   295                 else
       
   296                     {
       
   297                     aState = EDeregistered;
       
   298                     }
       
   299                 break;
       
   300 
       
   301             case ERegistrationRequested:
       
   302                 if ( registered )
       
   303                     {
       
   304                     aState = ERegistered;
       
   305                     }
       
   306                 else
       
   307                     {
       
   308                     aState = ERegistering;
       
   309                     }
       
   310                 break;
       
   311 
       
   312             case EDeregistrationRequested:
       
   313                 if ( registered )
       
   314                     {
       
   315                     aState = EDeregistering;
       
   316                     }
       
   317                 else
       
   318                     {
       
   319                     aState = EDeregistered;
       
   320                     }
       
   321                 break;
       
   322 
       
   323             default:
       
   324                 __ASSERT_DEBUG( EFalse, User::Panic( KNullDesC, KErrGeneral ) );
       
   325                 break;
       
   326             }
       
   327         }
       
   328     }
       
   329 
       
   330 // -----------------------------------------------------------------------------
       
   331 // CScpSipConnection::ProfileRegistryEventOccurred
       
   332 // -----------------------------------------------------------------------------
       
   333 //
       
   334 void CScpSipConnection::ProfileRegistryEventOccurred( 
       
   335     MSIPProfileRegistryObserver::TEvent aEvent )
       
   336     {
       
   337     SCPLOGSTRING3( "CScpSipConnection[0x%x]::ProfileRegistryEventOccurred event: %d", 
       
   338                    this, aEvent );
       
   339     
       
   340     TScpConnectionEvent event( EScpUnknown );
       
   341     
       
   342     switch ( aEvent )
       
   343         {
       
   344         case MSIPProfileRegistryObserver::EProfileRegistered:
       
   345             {
       
   346             CancelEnableTimeoutTimer();
       
   347             
       
   348             delete iSipConnection;
       
   349             iSipConnection = NULL;
       
   350             
       
   351             iConnectionStateError = KErrNone;
       
   352             event = EScpRegistered;
       
   353             }
       
   354             break;
       
   355 
       
   356         case MSIPProfileRegistryObserver::EProfileDeregistered:
       
   357             {
       
   358             CancelEnableTimeoutTimer();
       
   359                 
       
   360             if ( iRegistrationRequestState == EDeregistrationRequested )
       
   361                 {
       
   362                 iConnectionStateError = KErrNone;
       
   363                 event = EScpDeregistered;            
       
   364                 }
       
   365             else
       
   366                 {
       
   367 
       
   368                 // SIP will still notify the client with 
       
   369                 // MSIPProfileRegistryObserver::ProfileRegistryEventOccurred( EProfileDeregistered ) 
       
   370                 // but the reason for this event can be checked by the client
       
   371                 // by calling CSIPProfileRegistryBase::LastRegistrationError.
       
   372                 // If this function returns KErrDisconnected,
       
   373                 // the client can determine that the reason for the event was that the network connection was dropped.
       
   374 
       
   375                 TInt lastRegistrationError =
       
   376                     iProfileRegistry.LastRegistrationError( *iSipProfile );
       
   377                 
       
   378                 SCPLOGSTRING2( "    -> last registration error: %d", lastRegistrationError );
       
   379                 
       
   380                 if (  KErrDisconnected != lastRegistrationError )
       
   381                     {
       
   382                     if ( iProfileCurrentlyRoaming )
       
   383                         {
       
   384                         iProfileCurrentlyRoaming = EFalse;
       
   385                         iConnectionStateError = KCCHErrorNetworkLost;
       
   386                         event = EScpNetworkLost;
       
   387                         }
       
   388                     else
       
   389                         {
       
   390                         iConnectionStateError = KErrNone;
       
   391                         event = EScpDeregistered;
       
   392                         } 
       
   393                     }
       
   394                 else
       
   395                     {
       
   396                     iConnectionStateError = KCCHErrorNetworkLost;
       
   397                     event = EScpNetworkLost;
       
   398                     }
       
   399                 }
       
   400             }
       
   401             break;
       
   402 
       
   403         case MSIPProfileRegistryObserver::EProfileUpdated:
       
   404             {
       
   405             // Should we start EnableTimeoutTimer if registration
       
   406             // was requested?
       
   407             
       
   408             if ( iProfileCurrentlyRoaming )
       
   409                 {
       
   410                 iProfileCurrentlyRoaming = EFalse;
       
   411                 iConnectionStateError = KErrNone;
       
   412                 event = EScpRegistered;
       
   413                 }
       
   414             }
       
   415             break;
       
   416             
       
   417         default:
       
   418             break;
       
   419         }
       
   420 
       
   421     if ( event != EScpUnknown && iObserver )
       
   422         {
       
   423         SCPLOGSTRING3( "id: %d HandleSipConnectionEvent event: %d", 
       
   424                        iProfileId, event );
       
   425 
       
   426         iObserver->HandleSipConnectionEvent( iProfileId, event );
       
   427         }
       
   428     }
       
   429 
       
   430 // -----------------------------------------------------------------------------
       
   431 // CScpSipConnection::ProfileRegistryErrorOccurred
       
   432 // -----------------------------------------------------------------------------
       
   433 //
       
   434 void CScpSipConnection::ProfileRegistryErrorOccurred( TInt aError )
       
   435     {
       
   436     SCPLOGSTRING3( "CScpSipConnection[0x%x]::ProfileRegistryErrorOccurred error: %d", 
       
   437                    this, aError );
       
   438     
       
   439     CancelEnableTimeoutTimer();
       
   440 
       
   441     iConnectionStateError = TScpUtility::ConvertToCchError( aError );
       
   442     TScpConnectionEvent event = EScpUnknown;
       
   443     if ( iConnectionStateError == KCCHErrorAuthenticationFailed )
       
   444         {
       
   445         event = EScpAuthenticationFailed;
       
   446         }
       
   447     else if ( iConnectionStateError == KCCHErrorInvalidSettings )
       
   448         {
       
   449         event = EScpInvalidSettings;
       
   450         }
       
   451     else
       
   452         {
       
   453         if ( KErrCancel == aError )
       
   454             {
       
   455             iConnectionStateError = KErrCancel;
       
   456             event = EScpRegistrationCanceled;
       
   457             iRegistrationRequestState = EDeregistrationRequested;
       
   458             }
       
   459         else
       
   460             {
       
   461             iConnectionStateError = KCCHErrorLoginFailed;
       
   462             event = EScpRegistrationFailed;
       
   463             }
       
   464         }    
       
   465     
       
   466     TInt error = KErrNone;
       
   467     CVoipEventLog* eventLog = NULL;
       
   468     
       
   469     TRAP( error, eventLog = CVoipEventLog::NewL() );
       
   470     
       
   471     if ( KErrNone == error )
       
   472         {
       
   473         eventLog->WriteError( aError );
       
   474         }
       
   475     
       
   476     delete eventLog;
       
   477     
       
   478     iObserver->HandleSipConnectionEvent( iProfileId, event );    
       
   479     }
       
   480 
       
   481 // -----------------------------------------------------------------------------
       
   482 // CScpSipConnection::StartEnableTimeoutTimer
       
   483 // -----------------------------------------------------------------------------
       
   484 //
       
   485 void CScpSipConnection::StartEnableTimeoutTimer( TInt (*aFunction)(TAny* aPtr) )
       
   486     {
       
   487     SCPLOGSTRING2( "CScpSipConnection[0x%x]::StartEnableTimeoutTimer", this );
       
   488     __ASSERT_DEBUG( !iEnableTimeoutTimer->IsActive(), 
       
   489                     User::Panic( KNullDesC, KErrGeneral ) );
       
   490 
       
   491     if ( !iEnableTimeoutTimer->IsActive() )
       
   492         {
       
   493         iEnableTimeoutTimer->Start( KEnableTimeout, 
       
   494                               0, 
       
   495                               TCallBack( aFunction, this ) );
       
   496         }
       
   497     }
       
   498 
       
   499 // -----------------------------------------------------------------------------
       
   500 // CScpSipConnection::CancelEnableTimeoutTimer
       
   501 // -----------------------------------------------------------------------------
       
   502 //
       
   503 void CScpSipConnection::CancelEnableTimeoutTimer()
       
   504     {
       
   505     SCPLOGSTRING2( "CScpSipConnection[0x%x]::CancelEnableTimeoutTimer", this );
       
   506 
       
   507     if ( iEnableTimeoutTimer->IsActive() )
       
   508         {
       
   509         iEnableTimeoutTimer->Cancel();
       
   510         }
       
   511     }
       
   512 
       
   513 // -----------------------------------------------------------------------------
       
   514 // CScpSipConnection::EnableTimeout
       
   515 // -----------------------------------------------------------------------------
       
   516 //
       
   517 TInt CScpSipConnection::EnableTimeout( TAny* aSelf )
       
   518     {
       
   519     SCPLOGSTRING( "CScpSipConnection::EnableTimeout" );
       
   520 
       
   521     CScpSipConnection* self = static_cast<CScpSipConnection*>( aSelf );
       
   522     
       
   523     self->HandleEnableTimeout();
       
   524 
       
   525     return 0;
       
   526     }
       
   527 
       
   528 // -----------------------------------------------------------------------------
       
   529 // CScpSipConnection::HandleEnableTimeout
       
   530 // -----------------------------------------------------------------------------
       
   531 //
       
   532 void CScpSipConnection::HandleEnableTimeout()
       
   533     {
       
   534     SCPLOGSTRING2( "CScpSipConnection[0x%x]::HandleEnableTimeout",
       
   535                    this );
       
   536     ProfileRegistryErrorOccurred( EScpRegistrationFailed );
       
   537     }
       
   538 
       
   539 // -----------------------------------------------------------------------------
       
   540 // CScpSipConnection::GetIap
       
   541 // -----------------------------------------------------------------------------
       
   542 //
       
   543 TInt CScpSipConnection::GetIap( TUint32& aIapId ) const
       
   544     {
       
   545     SCPLOGSTRING2( "CScpSipConnection[0x%x]::GetIap", this );
       
   546     
       
   547     return iSipProfile->GetParameter( KSIPAccessPointId, aIapId ); 
       
   548     }
       
   549 
       
   550 // -----------------------------------------------------------------------------
       
   551 // CScpSipConnection::GetSnap
       
   552 // -----------------------------------------------------------------------------
       
   553 //
       
   554 TInt CScpSipConnection::GetSnap( TUint32& aSnapId ) const
       
   555     {
       
   556     SCPLOGSTRING2( "CScpSipConnection[0x%x]::GetSnap", this );
       
   557 
       
   558     return iSipProfile->GetParameter( KSIPSnapId, aSnapId );          
       
   559     }
       
   560 
       
   561 // -----------------------------------------------------------------------------
       
   562 // CScpSipConnection::GetUsername
       
   563 // -----------------------------------------------------------------------------
       
   564 //
       
   565 TInt CScpSipConnection::GetUsername( TDes8& aUsername ) const
       
   566     {
       
   567     SCPLOGSTRING2( "CScpSipConnection[0x%x]::GetUsername", this );
       
   568 
       
   569     TInt result( KErrNotFound );
       
   570     const TDesC8* aor( NULL );
       
   571 
       
   572     // Get SIP's Username    
       
   573     result = iSipProfile->GetParameter( KSIPUserAor, aor );
       
   574     
       
   575     if ( result == KErrNone && aor )
       
   576         {
       
   577         if ( aor->Length() <= aUsername.MaxLength() )
       
   578             {
       
   579             aUsername.Copy( *aor );
       
   580             }
       
   581         else
       
   582             {
       
   583             result = KErrOverflow;
       
   584             }
       
   585         }
       
   586 
       
   587     return result;                
       
   588     }
       
   589 
       
   590 // -----------------------------------------------------------------------------
       
   591 // CScpSipConnection::GetDomain
       
   592 // -----------------------------------------------------------------------------
       
   593 //
       
   594 TInt CScpSipConnection::GetDomain( TDes8& aDomain ) const
       
   595     {
       
   596     SCPLOGSTRING2( "CScpSipConnection[0x%x]::GetDomain", this );
       
   597 
       
   598     TInt result( KErrNotFound );
       
   599     const TDesC8* domain( NULL );
       
   600 
       
   601     // Get SIP's Username    
       
   602     result = iSipProfile->GetParameter( KSIPRegistrar, KSIPServerAddress, domain );
       
   603     
       
   604     if ( result == KErrNone && domain )
       
   605         {
       
   606         if ( domain->Length() <= aDomain.MaxLength() )
       
   607             {
       
   608             aDomain.Copy( *domain );
       
   609             }
       
   610         else
       
   611             {
       
   612             result = KErrOverflow;
       
   613             }
       
   614         }
       
   615 
       
   616     return result;                
       
   617     }
       
   618 
       
   619 // -----------------------------------------------------------------------------
       
   620 // CScpSipConnection::GetContactHeaderUser
       
   621 // -----------------------------------------------------------------------------
       
   622 //
       
   623 TInt CScpSipConnection::GetContactHeaderUser( RBuf8& aContactHeaderUser ) const
       
   624     {
       
   625     SCPLOGSTRING2( "CScpSipConnection[0x%x]::GetContactHeader", this );
       
   626 
       
   627     TInt result( KErrNotFound );
       
   628     const TDesC8* contactHeaderUser( NULL );
       
   629 
       
   630     // Get SIP's Username    
       
   631     result = iSipProfile->GetParameter( KSIPContactHeaderUser, contactHeaderUser );
       
   632     
       
   633     if ( result == KErrNone && contactHeaderUser )
       
   634         {
       
   635         result = aContactHeaderUser.ReAlloc( contactHeaderUser->Length() );
       
   636         if ( KErrNone == result )
       
   637             {
       
   638             aContactHeaderUser.Copy( *contactHeaderUser );
       
   639             }
       
   640         }
       
   641 
       
   642     return result;                
       
   643     }
       
   644 
       
   645 // -----------------------------------------------------------------------------
       
   646 // CScpSipConnection::SipProfile
       
   647 // -----------------------------------------------------------------------------
       
   648 //
       
   649 CSIPProfile& CScpSipConnection::SipProfile() const
       
   650     {
       
   651     SCPLOGSTRING2( "CScpSipConnection[0x%x]::SipProfile", this );
       
   652     
       
   653     return *iSipProfile;
       
   654     }
       
   655 
       
   656 // -----------------------------------------------------------------------------
       
   657 // CScpSipConnection::SetIap
       
   658 // -----------------------------------------------------------------------------
       
   659 //
       
   660 TInt CScpSipConnection::SetIap( TUint32 aProfileIapId )
       
   661     {
       
   662     SCPLOGSTRING3( "CScpSipConnection[0x%x]::SetIap iap: %d",
       
   663                    this, aProfileIapId );
       
   664 
       
   665     TInt result( KErrNone );
       
   666 
       
   667     // If snap exists, iap doesn't have an effect
       
   668     TUint32 snapId( 0 );
       
   669     result = GetSnap( snapId );
       
   670 
       
   671     if ( result == KErrNotFound ||
       
   672         snapId == 0 )
       
   673         {
       
   674         // Managed profile so we can change it
       
   675         CSIPManagedProfile* profile = static_cast<CSIPManagedProfile*> ( iSipProfile );
       
   676         
       
   677         // Add new IAP Id to SIP Profile
       
   678         result = profile->SetParameter( KSIPAccessPointId, aProfileIapId );
       
   679         if ( KErrNone == result )
       
   680             {
       
   681             // Save changes
       
   682             TRAP( result, iManagedProfileRegistry.SaveL( *profile ) );
       
   683             }
       
   684         }
       
   685     else
       
   686         {
       
   687         result = KErrNotSupported;
       
   688         }
       
   689 
       
   690     return result; 
       
   691     }
       
   692 
       
   693 // -----------------------------------------------------------------------------
       
   694 // CScpSipConnection::SetIap
       
   695 // -----------------------------------------------------------------------------
       
   696 //
       
   697 TInt CScpSipConnection::SetSnap( TUint32 aProfileSnapId )
       
   698     {
       
   699     SCPLOGSTRING3( "CScpSipConnection[0x%x]::SetSnap snap: %d", 
       
   700                    this, aProfileSnapId );
       
   701         
       
   702     // Managed profile so we can change it
       
   703     CSIPManagedProfile* profile = static_cast<CSIPManagedProfile*> ( iSipProfile );
       
   704     
       
   705     // Add new SNAP Id to SIP Profile
       
   706     TInt result = profile->SetParameter( KSIPSnapId, 
       
   707                                          aProfileSnapId );
       
   708 
       
   709     if ( KErrNone == result )
       
   710         {
       
   711         // Save changes
       
   712         TRAP( result, iManagedProfileRegistry.SaveL( *profile ) );
       
   713         }
       
   714 
       
   715     return result; 
       
   716     }
       
   717 
       
   718 // -----------------------------------------------------------------------------
       
   719 // CScpSipConnection::GetIapTypeL
       
   720 // -----------------------------------------------------------------------------
       
   721 //
       
   722 TScpIapType CScpSipConnection::GetIapTypeL() const
       
   723     {
       
   724     SCPLOGSTRING2( "CScpSipConnection[0x%x]::GetIapTypeL", this );
       
   725     
       
   726     TScpIapType type = EScpUnknownType;
       
   727 
       
   728     CCommsDatabase* commsDb = CCommsDatabase::NewL( EDatabaseTypeIAP );
       
   729     CleanupStack::PushL( commsDb );
       
   730 
       
   731     CCommsDbTableView* iapTable = commsDb->OpenTableLC( TPtrC( IAP ) );
       
   732 
       
   733     if ( iapTable->GotoFirstRecord() == KErrNone )
       
   734         { 
       
   735         TUint32 profilesIap = 0;
       
   736         User::LeaveIfError( GetIap( profilesIap ) );
       
   737 
       
   738         do 
       
   739             {
       
   740             TUint32 uid = 0;
       
   741             iapTable->ReadUintL( TPtrC( COMMDB_ID ), uid );
       
   742 
       
   743             if ( uid == profilesIap )
       
   744                 {
       
   745                 // The length should be defined somewhere
       
   746                 TBuf<256> serviceType;
       
   747                 iapTable->ReadTextL( TPtrC( IAP_SERVICE_TYPE ), serviceType );
       
   748 
       
   749                 if ( serviceType == TPtrC( INCOMING_GPRS ) ||
       
   750                     serviceType == TPtrC( OUTGOING_GPRS ) ||
       
   751                     serviceType == TPtrC( INCOMING_WCDMA) ||
       
   752                     serviceType == TPtrC( OUTGOING_WCDMA) )
       
   753                     {
       
   754                     type = EScpGprs;
       
   755                     }
       
   756                 else if ( serviceType == TPtrC( LAN_SERVICE ) )
       
   757                     {
       
   758                     type = EScpWlan;
       
   759                     }
       
   760                 else
       
   761                     {
       
   762                     type = EScpUnknownType;
       
   763                     }
       
   764 
       
   765                 break;
       
   766                 }
       
   767 
       
   768             }
       
   769         while( iapTable->GotoNextRecord() == KErrNone );
       
   770     }
       
   771 
       
   772     CleanupStack::PopAndDestroy( iapTable );
       
   773     CleanupStack::PopAndDestroy( commsDb );    
       
   774 
       
   775     SCPLOGSTRING2( "GetIapTypeL result: %d", type );
       
   776 
       
   777     return type;
       
   778     }
       
   779 
       
   780 // -----------------------------------------------------------------------------
       
   781 // CScpSipConnection::SetReserved
       
   782 // -----------------------------------------------------------------------------
       
   783 //
       
   784 void CScpSipConnection::SetReserved( TBool aReserved )
       
   785     {
       
   786     SCPLOGSTRING3( "CScpSipConnection[0x%x]::SetReserved: %d", this, aReserved );
       
   787     
       
   788     iReserved = aReserved;
       
   789     }
       
   790 
       
   791 // -----------------------------------------------------------------------------
       
   792 // CScpSipConnection::Reserved
       
   793 // -----------------------------------------------------------------------------
       
   794 //
       
   795 TBool CScpSipConnection::Reserved() const
       
   796     {
       
   797     SCPLOGSTRING3( "CScpSipConnection[0x%x]::Reserved: %d", this, iReserved );
       
   798 
       
   799     return iReserved;
       
   800     }
       
   801 
       
   802 // -----------------------------------------------------------------------------
       
   803 // CScpSipConnection::SetIapAvailableOffered
       
   804 // -----------------------------------------------------------------------------
       
   805 //
       
   806 void CScpSipConnection::SetIapAvailableOffered( TBool aIapAvailableOffered )   
       
   807     {
       
   808     SCPLOGSTRING3( "CScpSipConnection[0x%x]::SetIapAvailableOffered: %d", 
       
   809     this, aIapAvailableOffered );
       
   810     
       
   811     iIapAvailableOffered = aIapAvailableOffered;
       
   812     }
       
   813 
       
   814 // -----------------------------------------------------------------------------
       
   815 // CScpSipConnection::IapAvailableOffered
       
   816 // -----------------------------------------------------------------------------
       
   817 //
       
   818 TBool CScpSipConnection::IapAvailableOffered() const
       
   819     {
       
   820     SCPLOGSTRING3( "CScpSipConnection[0x%x]::IapAvaiableOffered: %d", 
       
   821     this, iIapAvailableOffered );
       
   822     
       
   823     return iIapAvailableOffered;
       
   824     }
       
   825 
       
   826 // -----------------------------------------------------------------------------
       
   827 // CScpSipConnection::IapAvailableOffered
       
   828 // -----------------------------------------------------------------------------
       
   829 //
       
   830 void CScpSipConnection::SetProfileCurrentlyRoaming()
       
   831     {
       
   832     SCPLOGSTRING( "CScpSipConnection[0x%x]::SetProfileCurrentlyRoaming" );
       
   833     
       
   834     iProfileCurrentlyRoaming = ETrue;
       
   835     }
       
   836 
       
   837 // -----------------------------------------------------------------------------
       
   838 // CScpSipConnection::IsPasswordSet
       
   839 // -----------------------------------------------------------------------------
       
   840 //
       
   841 TBool CScpSipConnection::IsPasswordSet() const
       
   842     {
       
   843     SCPLOGSTRING( "CScpSipConnection::IsPasswordSet" );
       
   844 
       
   845     // no support exist in sip for getting is password set information */
       
   846     SCPLOGSTRING( "CScpSipConnection::IsPasswordSet: pwd is set is always returned" );
       
   847     return ETrue;
       
   848     }
       
   849 
       
   850 // -----------------------------------------------------------------------------
       
   851 // CScpSipConnection::IsUsernameSetL
       
   852 // -----------------------------------------------------------------------------
       
   853 //
       
   854 TBool CScpSipConnection::IsUsernameSetL() const
       
   855     {
       
   856     SCPLOGSTRING( "CScpSipConnection::IsUsernameSetL" );
       
   857  
       
   858     TBool userNameSet( EFalse );
       
   859     
       
   860     // check from aor
       
   861     RBuf8 username;
       
   862     CleanupClosePushL( username );
       
   863     username.CreateL( KUsernameMaxLength );
       
   864     
       
   865     TInt err = GetUsername( username );
       
   866 
       
   867     if ( !err && username.Length() )
       
   868         {
       
   869         RBuf8 formattedUsername;
       
   870         TScpUtility::RemovePrefixAndDomain( username, formattedUsername );
       
   871         
       
   872         if ( formattedUsername.Length() )
       
   873             {
       
   874             userNameSet = ETrue;
       
   875             }   
       
   876         
       
   877         formattedUsername.Zero();
       
   878         formattedUsername.Close();
       
   879         }
       
   880     else if ( KErrNone == err && SipProfileIMS() )
       
   881         {
       
   882         SCPLOGSTRING( "CScpSipConnection::IsUsernameSetL - SipProfileIMS" );
       
   883         // Check if this is IMS case; then we can think username is set
       
   884         // (SIP stack takes care of SIM handling)
       
   885         userNameSet = ETrue;
       
   886         }
       
   887     
       
   888     else
       
   889         {
       
   890         SCPLOGSTRING( "CScpSipConnection::IsUsernameSetL - ELSE!" );
       
   891         }
       
   892         
       
   893     CleanupStack::PopAndDestroy( &username );
       
   894     
       
   895     return userNameSet;
       
   896     }
       
   897 
       
   898 // -----------------------------------------------------------------------------
       
   899 // CScpSipConnection::SipProfileIMS
       
   900 // -----------------------------------------------------------------------------
       
   901 //
       
   902 TBool CScpSipConnection::SipProfileIMS() const
       
   903     {
       
   904     SCPLOGSTRING( "CScpSipConnection::SipProfileIMS" );
       
   905     if( TSIPProfileTypeInfo::EIms == iSipProfile->Type().iSIPProfileClass )
       
   906         {
       
   907         SCPLOGSTRING( "CScpSipConnection::SipProfileIMS - ETrue" );
       
   908         return ETrue;
       
   909         }
       
   910     else
       
   911         {
       
   912         SCPLOGSTRING( "CScpSipConnection::SipProfileIMS - EFalse" );
       
   913         return EFalse;
       
   914         }
       
   915     }
       
   916 
       
   917 // -----------------------------------------------------------------------------
       
   918 // CScpSipConnection::HandleMigrationStarted
       
   919 // -----------------------------------------------------------------------------
       
   920 //
       
   921 void CScpSipConnection::HandleMigrationStarted()
       
   922     {
       
   923     if( iObserver )
       
   924         {
       
   925         SCPLOGSTRING2( "    -> roaming - id: %d", iProfileId );
       
   926 
       
   927         iObserver->HandleSipConnectionEvent( iProfileId, EScpRoaming );
       
   928         }
       
   929     }
       
   930 
       
   931 // -----------------------------------------------------------------------------
       
   932 // CScpSipConnection::HandleMigrationError
       
   933 // -----------------------------------------------------------------------------
       
   934 //
       
   935 void CScpSipConnection::HandleMigrationError(
       
   936     TInt /*aError*/,
       
   937     TUint32 /*aProfileId*/,
       
   938     TUint32 /*aSnapId*/,
       
   939     TUint32 /*aIapId*/ )
       
   940     {
       
   941     // Migration has failed -> check is sip still registered via old iap 
       
   942     // and if is, set state back to registered
       
   943     
       
   944     TScpConnectionEvent event( EScpUnknown );
       
   945     
       
   946     if ( iProfileCurrentlyRoaming && iObserver )
       
   947         {
       
   948         iProfileCurrentlyRoaming = EFalse;
       
   949         iConnectionStateError = KErrNone;
       
   950         event = EScpRegistered;
       
   951         SCPLOGSTRING3( "id: %d HandleSipConnectionEvent event: %d", 
       
   952                        iProfileId, event );
       
   953         iObserver->HandleSipConnectionEvent( iProfileId, event );
       
   954         }
       
   955     }
       
   956 
       
   957 // -----------------------------------------------------------------------------
       
   958 // CScpSipConnection::IsNetworkConnectionAvailable
       
   959 // -----------------------------------------------------------------------------
       
   960 //
       
   961 TBool CScpSipConnection::IsNetworkConnectionAvailable() const
       
   962     {
       
   963     SCPLOGSTRING2( "CScpSipConnection[0x%x]::IsNetworkConnectionAvailable", this );
       
   964 
       
   965     TBool isAvailable = EFalse;
       
   966 
       
   967     TUint32 snapId = 0;
       
   968     TInt result = GetSnap( snapId );
       
   969 
       
   970     if ( result == KErrNone ||
       
   971         result == KErrNotFound )
       
   972         {
       
   973         if ( snapId != 0 )
       
   974             {
       
   975             isAvailable = IsSnapConnectionAvailable( snapId );
       
   976             }   
       
   977         else
       
   978             {
       
   979             TUint32 iapId = 0;
       
   980             result = GetIap( iapId );
       
   981 
       
   982             if( result == KErrNone )
       
   983                 {
       
   984                 isAvailable = IsIapConnectionAvailable( iapId );
       
   985                 }
       
   986             }
       
   987         }
       
   988     
       
   989     return isAvailable;
       
   990     }
       
   991 
       
   992 // -----------------------------------------------------------------------------
       
   993 // CScpSipConnection::IsSnapConnectionAvailable
       
   994 // -----------------------------------------------------------------------------
       
   995 //
       
   996 TBool CScpSipConnection::IsSnapConnectionAvailable( TInt aSnapId ) const
       
   997     {
       
   998     SCPLOGSTRING2( "CScpSipConnection[0x%x]::IsSnapConnectionAvailable", this );
       
   999 
       
  1000     TBool isAvailable = EFalse;
       
  1001 
       
  1002     TConnMonSNAPInfoBuf snapInfoBuf;
       
  1003     TRequestStatus status;
       
  1004     iConnectionMonitor.GetPckgAttribute( EBearerIdAll, 0, KSNAPsAvailability,
       
  1005                                          snapInfoBuf, status );
       
  1006 
       
  1007     User::WaitForRequest( status );
       
  1008 
       
  1009     for ( TInt i = 0; i < snapInfoBuf().Count(); i++ )
       
  1010         {
       
  1011         if ( aSnapId == snapInfoBuf().iSNAP[ i ].iSNAPId )
       
  1012             {
       
  1013             SCPLOGSTRING( "SNAP IS STILL ALIVE!!!" );
       
  1014             isAvailable = ETrue;
       
  1015             break;
       
  1016             } 
       
  1017         }
       
  1018 
       
  1019     return isAvailable;
       
  1020     }
       
  1021 
       
  1022 // -----------------------------------------------------------------------------
       
  1023 // CScpSipConnection::IsIapConnectionAvailable
       
  1024 // -----------------------------------------------------------------------------
       
  1025 //
       
  1026 TBool CScpSipConnection::IsIapConnectionAvailable( TInt aIapId ) const
       
  1027     {
       
  1028     SCPLOGSTRING2( "CScpSipConnection[0x%x]::IsIapConnectionAvailable", this );
       
  1029 
       
  1030     TBool isAvailable = EFalse;
       
  1031 
       
  1032     TConnMonIapInfoBuf iapInfoBuf;
       
  1033     TRequestStatus status;
       
  1034     iConnectionMonitor.GetPckgAttribute( EBearerIdAll, 0, KIapAvailability,
       
  1035                                          iapInfoBuf, status );
       
  1036 
       
  1037     User::WaitForRequest( status );
       
  1038 
       
  1039     for ( TInt i = 0; i < iapInfoBuf().Count(); i++ )
       
  1040         {
       
  1041         if  ( aIapId == iapInfoBuf().iIap[ i ].iIapId )
       
  1042             {
       
  1043             SCPLOGSTRING( "IAP IS STILL ALIVE!!!" );
       
  1044             isAvailable = ETrue;
       
  1045             break;
       
  1046             } 
       
  1047         }
       
  1048 
       
  1049     return isAvailable;
       
  1050     }
       
  1051 
       
  1052 // -----------------------------------------------------------------------------
       
  1053 // CScpSipConnection::ConnectionStateChanged
       
  1054 // -----------------------------------------------------------------------------
       
  1055 //
       
  1056 void CScpSipConnection::ConnectionStateChanged( CSIPConnection::TState aState )
       
  1057     {
       
  1058     SCPLOGSTRING2( "CScpSipConnection[0x%x]::ConnectionStateChanged - IN", this );
       
  1059     
       
  1060     switch ( aState )
       
  1061             {
       
  1062             case CSIPConnection::EInactive:
       
  1063                 {
       
  1064                 SCPLOGSTRING2( "CScpSipConnection[0x%x]::ConnectionStateChanged: EInactive", this );
       
  1065                 
       
  1066                 TUint32 iapId( 0 );
       
  1067                 TInt result = GetIap( iapId );
       
  1068                     
       
  1069                 if ( KErrNone == result && iSipConnection &&
       
  1070                         iapId == iSipConnection->IapId() &&
       
  1071                         KErrNone == iConnectionStateError )
       
  1072                     {
       
  1073                     delete iSipConnection;
       
  1074                     iSipConnection = NULL;
       
  1075                     
       
  1076                     iConnectionStateError = KCCHErrorLoginFailed;
       
  1077                     iObserver->HandleSipConnectionEvent( iProfileId, EScpRegistrationFailed );
       
  1078                     }
       
  1079                 }
       
  1080                 break;
       
  1081                 
       
  1082             default:
       
  1083                 {
       
  1084                 SCPLOGSTRING2( "CScpSipConnection[0x%x]::ConnectionStateChanged: default", this );
       
  1085                 }
       
  1086                 break;
       
  1087             }
       
  1088     SCPLOGSTRING2( "CScpSipConnection[0x%x]::ConnectionStateChanged - OUT", this );
       
  1089     }
       
  1090 
       
  1091 //  End of File