sipvoipprovider/src/svpemergencyconnection.cpp
branchRCL_3
changeset 22 d38647835c2e
equal deleted inserted replaced
21:f742655b05bf 22:d38647835c2e
       
     1 /*
       
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Provides connection for emergency call when necessary
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <nifman.h>   // For global variables
       
    21 
       
    22 #include "svpemergencyconnection.h"
       
    23 #include "svplogger.h" // For logging
       
    24 
       
    25 
       
    26 // ======== MEMBER FUNCTIONS ========
       
    27 
       
    28 // ---------------------------------------------------------------------------
       
    29 // CSVPEmergencyConnection
       
    30 // ---------------------------------------------------------------------------
       
    31 //
       
    32 CSVPEmergencyConnection::CSVPEmergencyConnection( 
       
    33     TPriority aPriority, MSVPEmergencyConnectionObserver& aObserver )
       
    34     : CActive( aPriority ), 
       
    35       iObserver( aObserver )
       
    36     {
       
    37     CActiveScheduler::Add( this );
       
    38     }
       
    39 
       
    40 // ---------------------------------------------------------------------------
       
    41 // ConstructL
       
    42 // ---------------------------------------------------------------------------
       
    43 //
       
    44 void CSVPEmergencyConnection::ConstructL()
       
    45     {
       
    46     SVPDEBUG1("CSVPEmergencyConnection::ConstructL()")
       
    47     
       
    48     User::LeaveIfError( iSocketServer.Connect() );
       
    49     User::LeaveIfError( iConnection.Open( iSocketServer ) );
       
    50     }
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // NewL
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 CSVPEmergencyConnection* CSVPEmergencyConnection::NewL( 
       
    57     TPriority aPriority, MSVPEmergencyConnectionObserver& aObserver )
       
    58     {
       
    59     CSVPEmergencyConnection* self = CSVPEmergencyConnection::NewLC( 
       
    60         aPriority, aObserver );
       
    61     CleanupStack::Pop( self );
       
    62     return self;
       
    63     }
       
    64     
       
    65 // ---------------------------------------------------------------------------
       
    66 // NewLC
       
    67 // ---------------------------------------------------------------------------
       
    68 //
       
    69 CSVPEmergencyConnection* CSVPEmergencyConnection::NewLC( 
       
    70     TPriority aPriority, MSVPEmergencyConnectionObserver& aObserver )
       
    71     {
       
    72     CSVPEmergencyConnection* self = 
       
    73         new( ELeave ) CSVPEmergencyConnection( aPriority, aObserver );
       
    74     CleanupStack::PushL( self );
       
    75     self->ConstructL();
       
    76     return self;
       
    77     }
       
    78     
       
    79 // ---------------------------------------------------------------------------
       
    80 // Destructor
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 CSVPEmergencyConnection::~CSVPEmergencyConnection()
       
    84     {
       
    85     SVPDEBUG1("CSVPEmergencyConnection::~CSVPEmergencyConnection()")
       
    86     
       
    87     Cancel();
       
    88 
       
    89     iConnection.Close();
       
    90     iSocketServer.Close();
       
    91     }
       
    92 
       
    93 // ---------------------------------------------------------------------------
       
    94 // Connects asynchronically with SNAP ID
       
    95 // ---------------------------------------------------------------------------
       
    96 //
       
    97 void CSVPEmergencyConnection::ConnectWithSnapIdL( TUint32 aSnapId )
       
    98     {
       
    99     SVPDEBUG2("CSVPEmergencyConnection::ConnectWithSnapIdL, ID: %d", aSnapId)
       
   100 
       
   101     if ( IsActive() )
       
   102         {
       
   103         User::Leave( KErrInUse );
       
   104         }
       
   105 
       
   106     // Start connection
       
   107     iRequestType = ESVPSnapConnect;
       
   108     iSnapConnPref.SetSnap( aSnapId );
       
   109     iConnection.Start( iSnapConnPref, iStatus );
       
   110     SetActive();
       
   111     }
       
   112 
       
   113 // ---------------------------------------------------------------------------
       
   114 // Returns the used IAP ID of SNAP connection
       
   115 // ---------------------------------------------------------------------------
       
   116 //
       
   117 TInt CSVPEmergencyConnection::IapId( TUint32& aIapId )
       
   118     {
       
   119     SVPDEBUG1("CSVPEmergencyConnection::IapIdL()")
       
   120     
       
   121     _LIT( KIapId, "IAP\\Id" );
       
   122     TRAPD( error, iConnection.GetIntSetting( KIapId, aIapId ) )
       
   123     return error;
       
   124     }
       
   125 
       
   126 // ---------------------------------------------------------------------------
       
   127 // Connects with IAP ID
       
   128 // ---------------------------------------------------------------------------
       
   129 //
       
   130 void CSVPEmergencyConnection::ConnectL( TUint32 aIapId )
       
   131     {
       
   132     SVPDEBUG2("CSVPEmergencyConnection::ConnectL(), IAP ID: %d", aIapId)
       
   133     
       
   134     if ( IsActive() )
       
   135         {
       
   136         User::Leave( KErrInUse );
       
   137         }
       
   138     
       
   139     // Start connection
       
   140     iRequestType = ESVPConnect;
       
   141     iConnPref.SetIapId( aIapId );
       
   142     iConnPref.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
       
   143     iConnection.Start( iConnPref, iStatus );
       
   144     SetActive();
       
   145     }
       
   146 
       
   147 // ---------------------------------------------------------------------------
       
   148 // Requests for SIP proxy address
       
   149 // ---------------------------------------------------------------------------
       
   150 //
       
   151 #ifdef _DEBUG
       
   152 void CSVPEmergencyConnection::RequestSipProxyAddressL( TUint32 aIapId )
       
   153 #else
       
   154 void CSVPEmergencyConnection::RequestSipProxyAddressL( TUint32 /*aIapId*/ )
       
   155 #endif // __DEBUG
       
   156     {
       
   157     SVPDEBUG2("CSVPEmergencyConnection::RequestSipProxyAddressL(),\
       
   158         IAP ID: %d", aIapId)
       
   159 
       
   160     if ( IsActive() )
       
   161         {
       
   162         User::Leave( KErrInUse );
       
   163         }
       
   164 
       
   165     // Request SIP proxy address
       
   166     iRequestType = ESVPSipProxyAddress;
       
   167     iSipServerAddrBuf().index = 0;
       
   168     iConnection.Ioctl( 
       
   169         KCOLConfiguration, 
       
   170         KConnGetSipServerAddr, // DHCP option 120
       
   171         iStatus, 
       
   172         &iSipServerAddrBuf );
       
   173     SetActive();
       
   174     }
       
   175     
       
   176 // ---------------------------------------------------------------------------
       
   177 // From class CSVPEmergencyConnection.
       
   178 // DoCancel
       
   179 // ---------------------------------------------------------------------------
       
   180 //
       
   181 void CSVPEmergencyConnection::DoCancel()
       
   182     {
       
   183     SVPDEBUG1("CSVPEmergencyConnection::DoCancel()")
       
   184     
       
   185     iRequestType = ESVPNone;
       
   186     iConnection.CancelIoctl();
       
   187     iConnection.Close();
       
   188     }
       
   189     
       
   190 // ---------------------------------------------------------------------------
       
   191 // From class CActive.
       
   192 // RunL
       
   193 // ---------------------------------------------------------------------------
       
   194 //
       
   195 void CSVPEmergencyConnection::RunL()
       
   196     {
       
   197     TInt error = iStatus.Int();
       
   198     SVPDEBUG2( "CSVPEmergencyConnection::RunL(), error: %d", error )
       
   199     SVPDEBUG2( "CSVPEmergencyConnection::RunL(), Request type: %d", iRequestType )
       
   200     
       
   201     // execution goes to RunError() if error occured
       
   202     User::LeaveIfError( iStatus.Int() );
       
   203     switch ( iRequestType )
       
   204         {
       
   205         case ESVPSnapConnect:
       
   206             SVPDEBUG1( "CSVPEmergencyConnection::RunL() - ESVPSnapConnect" )
       
   207             iRequestType = ESVPNone;
       
   208             iObserver.SnapConnected();
       
   209             break;
       
   210         
       
   211         case ESVPConnect:
       
   212             SVPDEBUG1( "CSVPEmergencyConnection::RunL() - ESVPConnect" )
       
   213             iRequestType = ESVPNone;
       
   214             iObserver.Connected();
       
   215             break;
       
   216             
       
   217         case ESVPSipProxyAddress:
       
   218             {
       
   219             SVPDEBUG1( "CSVPEmergencyConnection::RunL() - ESVPSipProxyAddress" )
       
   220             iRequestType = ESVPNone;
       
   221             
       
   222             // Copy SIP proxy address in dotted-decimal notation
       
   223             HBufC16* sipProxyAddrBuf = HBufC16::NewLC( 39 ); // CS:1
       
   224             TPtr16 sipProxyAddrPtr = sipProxyAddrBuf->Des();
       
   225             iSipServerAddrBuf().address.Output( sipProxyAddrPtr );
       
   226             SVPDEBUG2(
       
   227                     "CSVPEmergencyConnection::RunL(), iSipServerAddrBuf: %S",
       
   228                     sipProxyAddrBuf )
       
   229             // Call observer
       
   230             iObserver.SipProxyAddressReady( *sipProxyAddrBuf );
       
   231             CleanupStack::PopAndDestroy( sipProxyAddrBuf ); // CS:0
       
   232             break;
       
   233             }
       
   234         case ESVPSipDomainAddress:
       
   235             {
       
   236             SVPDEBUG1( "CSVPEmergencyConnection::RunL() - ESVPSipDomainAddress" )
       
   237             iRequestType = ESVPNone;
       
   238             
       
   239             // Copy SIP proxy domain in dotted-decimal notation
       
   240             HBufC16* sipDomainAddrBuf = HBufC16::NewLC( iSipServerDomainBuf().domainName.Length() ); // CS:1
       
   241             TPtr16 sipDomainAddrPtr = sipDomainAddrBuf->Des();
       
   242             sipDomainAddrPtr.Copy( iSipServerDomainBuf().domainName );
       
   243             SVPDEBUG2(
       
   244                     "CSVPEmergencyConnection::RunL(), iSipDomainAddrBuf: %S",
       
   245                     sipDomainAddrBuf )
       
   246             // Call observer
       
   247             iObserver.SipProxyAddressReady( *sipDomainAddrBuf );
       
   248             CleanupStack::PopAndDestroy( sipDomainAddrBuf ); // CS:0
       
   249             break;
       
   250             }
       
   251             
       
   252         default:
       
   253             SVPDEBUG1( "CSVPEmergencyConnection::RunL() - Default" )
       
   254             iRequestType = ESVPNone;
       
   255             iObserver.ConnectionError( KErrGeneral );
       
   256             break;
       
   257         }
       
   258     }
       
   259 
       
   260 // ---------------------------------------------------------------------------
       
   261 // CSVPEmergencyConnection::RunError
       
   262 //
       
   263 // ---------------------------------------------------------------------------
       
   264 //
       
   265 TInt CSVPEmergencyConnection::RunError( TInt aError )
       
   266     {
       
   267     SVPDEBUG2( "CSVPEmergencyConnection::RunError() %d", aError )
       
   268     if ( iRequestType == ESVPSipProxyAddress )
       
   269         {
       
   270         /* 
       
   271          * sip proxy address might be NULL if requested with KConnGetSipServerAddr
       
   272          * and dhcp returns domain name to sip, instead of ipv4 or ipv6 type address.
       
   273          * So try once again with KConnGetSipServerDomain.
       
   274          */
       
   275         SVPDEBUG1( "CSVPEmergencyConnection::RunError() - RequestDomain" )
       
   276         RequestSipServerDomainL();    
       
   277         return KErrNone;
       
   278         }
       
   279     else
       
   280         {
       
   281         iRequestType = ESVPNone;
       
   282         SVPDEBUG2( "CSVPEmergencyConnection::RunError() - Notify Observer ConnectionError( %d )", aError )
       
   283         iObserver.ConnectionError( aError );
       
   284         return KErrNone;
       
   285         }
       
   286     }
       
   287 
       
   288 // ---------------------------------------------------------------------------
       
   289 // Requests for SIP proxy address
       
   290 // ---------------------------------------------------------------------------
       
   291 //
       
   292 void CSVPEmergencyConnection::RequestSipServerDomainL()
       
   293     {
       
   294     SVPDEBUG1( 
       
   295         "CSVPEmergencyConnection::RequestSipServerDomainL() - Try to get Sip server domain" )
       
   296 
       
   297     if ( IsActive() )
       
   298         {
       
   299         User::Leave( KErrInUse );
       
   300         }
       
   301 
       
   302     iRequestType = ESVPSipDomainAddress;
       
   303     iSipServerDomainBuf().index = 0;
       
   304     // Request SIP server domain
       
   305     iConnection.Ioctl(
       
   306         KCOLConfiguration,
       
   307         KConnGetSipServerDomain, // domain in textual format
       
   308         iStatus,
       
   309         &iSipServerDomainBuf );
       
   310     SetActive();
       
   311     }