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