simpleengine/siputils/src/simplesipconnection.cpp
branchRCL_3
changeset 34 2669f8761a99
parent 25 e53c01f160bc
child 35 fbd2e7cec7ef
equal deleted inserted replaced
31:2580314736af 34:2669f8761a99
     1 /*
     1 /*
     2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    34 #include <siprequestelements.h>
    34 #include <siprequestelements.h>
    35 #include <sipclienttransaction.h>
    35 #include <sipclienttransaction.h>
    36 #include <siprefresh.h>
    36 #include <siprefresh.h>
    37 #include <sipmessageelements.h>
    37 #include <sipmessageelements.h>
    38 #include <sipsubscribedialogassoc.h>
    38 #include <sipsubscribedialogassoc.h>
    39 #include <sipregistrationcontext.h>
       
    40 
    39 
    41 // sip codec api
    40 // sip codec api
    42 #include <sipfromheader.h>
    41 #include <sipfromheader.h>
    43 #include <siptoheader.h>
    42 #include <siptoheader.h>
    44 #include <sipexpiresheader.h>
    43 #include <sipexpiresheader.h>
    50 #include <sipextensionheader.h>
    49 #include <sipextensionheader.h>
    51 #include <sipsubscriptionstateheader.h>
    50 #include <sipsubscriptionstateheader.h>
    52 #include <sipsupportedheader.h>
    51 #include <sipsupportedheader.h>
    53 #include <sipacceptheader.h>
    52 #include <sipacceptheader.h>
    54 
    53 
    55 #include <cvimpstsettingsstore.h>
       
    56 
       
    57 // own simple
    54 // own simple
    58 #include "simplesipconnection.h"
    55 #include "simplesipconnection.h"
    59 #include "simplesipconnectionobserver.h"
    56 #include "simplesipconnectionobserver.h"
    60 #include "simplesipprofileobserver.h"
    57 #include "simplesipprofileobserver.h"
    61 #include "simplerefreshtimer.h"
    58 #include "simplerefreshtimer.h"
    81 _LIT8 ( KSimpleTimeout, "timeout" );
    78 _LIT8 ( KSimpleTimeout, "timeout" );
    82 _LIT8 ( KSimpleGiveup, "giveup" );
    79 _LIT8 ( KSimpleGiveup, "giveup" );
    83 _LIT8 ( KSimpleNoresource, "Noresource" );
    80 _LIT8 ( KSimpleNoresource, "Noresource" );
    84 _LIT8 ( KSipPrefix, "sip:" );
    81 _LIT8 ( KSipPrefix, "sip:" );
    85 
    82 
       
    83 // UID given for CSIP class. SIP Utils listens to new SIP requests 
       
    84 // (when receiving IM) outside SIP dialogs.
       
    85 const TUid KSimpleSIPUtilsUID = { 0x10281EEC };
       
    86 
    86 // ================= MEMBER FUNCTIONS =======================
    87 // ================= MEMBER FUNCTIONS =======================
    87 //
    88 //
    88 
    89 
    89 // ----------------------------------------------------------
    90 // ----------------------------------------------------------
    90 // CSimpleSipConnection::CSimpleSipConnection
    91 // CSimpleSipConnection::CSimpleSipConnection
   111     delete iSipConnection;
   112     delete iSipConnection;
   112     delete iProfileObserver;
   113     delete iProfileObserver;
   113     delete iConnectionObserver;
   114     delete iConnectionObserver;
   114     delete iSip;
   115     delete iSip;
   115     DeleteRequests();
   116     DeleteRequests();
   116     DeleteRequests( CSimpleRequest::EReqReceiveIM );
       
   117     SIPStrings::Close();
   117     SIPStrings::Close();
   118     }
   118     }
   119 
   119 
   120 // ----------------------------------------------------------
   120 // ----------------------------------------------------------
   121 // CSimpleSipConnection::NewL
   121 // CSimpleSipConnection::NewL
   138     {
   138     {
   139 #ifdef _DEBUG
   139 #ifdef _DEBUG
   140     TSimpleLogger::Log(
   140     TSimpleLogger::Log(
   141         _L("SipConnection: ConstructL 20-01-07 this=%d" ), (TInt)this );
   141         _L("SipConnection: ConstructL 20-01-07 this=%d" ), (TInt)this );
   142 #endif
   142 #endif
   143     
   143     // initialize members
       
   144     iSip = CSIP::NewL( KSimpleSIPUtilsUID, *this );
   144     iConnectionObserver = CSimpleSipConnectionObserver::NewL( *this);
   145     iConnectionObserver = CSimpleSipConnectionObserver::NewL( *this);
       
   146     iProfileObserver = 
       
   147         CSimpleSipProfileObserver::NewL( iSip, *iConnectionObserver);
   145     SIPStrings::OpenL();
   148     SIPStrings::OpenL();
   146     // read static cenrep settings
   149     // read static cenrep settings
   147     iSettings.ReadCentRepSettings();
   150     iSettings.ReadCentRepSettings();
   148 
   151 
   149 #ifdef _DEBUG
   152 #ifdef _DEBUG
   445         CUri8* uri = CUri8::NewL( parser );
   448         CUri8* uri = CUri8::NewL( parser );
   446         
   449         
   447         // Start to fill header, Remote URI
   450         // Start to fill header, Remote URI
   448         CSIPRequestElements* elems = CSIPRequestElements::NewL( uri );
   451         CSIPRequestElements* elems = CSIPRequestElements::NewL( uri );
   449         CleanupStack::PushL( elems );  // CS: 3
   452         CleanupStack::PushL( elems );  // CS: 3
   450         
   453 
   451         TUriParser8 parser3;
   454         TUriParser8 parser3;
   452         User::LeaveIfError( parser3.Parse( iProfileObserver->
   455         User::LeaveIfError( parser3.Parse( iProfileObserver->
   453             GiveUserAorL() ));
   456             GiveUserAorL() ));
   454         uri = CUri8::NewL( parser3 );
   457         uri = CUri8::NewLC( parser3 );  // CS: 4
       
   458         
   455         CSIPAddress* sipAddress = CSIPAddress::NewL( uri );
   459         CSIPAddress* sipAddress = CSIPAddress::NewL( uri );
       
   460         CleanupStack::Pop( uri ); // ownership given to sipAddress  // CS: 3
       
   461         CleanupStack::PushL( sipAddress );  // CS: 4
   456         
   462         
   457         // From Header
   463         // From Header
   458         CSIPFromHeader* fromH = CSIPFromHeader::NewL( sipAddress );
   464         CSIPFromHeader* fromH = CSIPFromHeader::NewL( sipAddress );
   459         elems->SetFromHeaderL( fromH ); // fromH, ownership given to elems
   465         CleanupStack::Pop( sipAddress ); // ownership given to FromH  // CS: 3
       
   466         CleanupStack::PushL( fromH );  // CS: 4
       
   467         elems->SetFromHeaderL( fromH );
       
   468         // fromH, ownership given to elems
       
   469         CleanupStack::Pop( fromH );  // CS: 3
   460         
   470         
   461         uri = CUri8::NewL( parser );
   471         uri = CUri8::NewLC( parser );  // CS: 4
       
   472         
   462         CSIPAddress* addr = CSIPAddress::NewL( uri );
   473         CSIPAddress* addr = CSIPAddress::NewL( uri );
       
   474         CleanupStack::Pop( uri );  // CS: 3
       
   475         CleanupStack::PushL( addr );  // CS: 4
       
   476         
   463         // To Header
   477         // To Header
   464         CSIPToHeader* toHeader = CSIPToHeader::NewL( addr );
   478         CSIPToHeader* toHeader = CSIPToHeader::NewL( addr );
       
   479         CleanupStack::Pop( addr );  // CS: 3
       
   480         CleanupStack::PushL( toHeader );  // CS: 4   
   465         elems->SetToHeaderL( toHeader );
   481         elems->SetToHeaderL( toHeader );
       
   482         CleanupStack::Pop( toHeader );  // CS: 3
   466         
   483         
   467         CSIPMessageElements& mesElems = elems->MessageElements();
   484         CSIPMessageElements& mesElems = elems->MessageElements();
   468         
   485         
   469         // Add Expires Header
   486         // Add Expires Header
   470         if ( aReq.RefreshTime() )
   487         if ( aReq.RefreshTime() )
   478             User::LeaveIfError( headers.Append( exprH ) );
   495             User::LeaveIfError( headers.Append( exprH ) );
   479             CleanupStack::Pop( exprH ); // CS: 4
   496             CleanupStack::Pop( exprH ); // CS: 4
   480             
   497             
   481             mesElems.SetUserHeadersL( headers );
   498             mesElems.SetUserHeadersL( headers );
   482             // headers ownership given to mesElems
   499             // headers ownership given to mesElems
   483             CleanupStack::Pop( &headers ); // CS: 3
   500             CleanupStack::Pop( &headers ); // CS: 3            
   484             }
   501             }
   485         
   502         
   486         // Set content type and content
   503         // Set content type and content
   487         HBufC8* buffer = aContent.AllocLC(); // CS: 4
   504         CSIPContentTypeHeader* contTypeH = CSIPContentTypeHeader::NewLC(
   488         CSIPContentTypeHeader* contTypeH = CSIPContentTypeHeader::NewL(
   505             KSimpleMediaType, KSimpleMediaSubType );  // CS: 4
   489             KSimpleMediaType, KSimpleMediaSubType );
   506         HBufC8* buffer = aContent.AllocLC();  // CS: 5
   490         CleanupStack::Pop( buffer ); // CS: 3
       
   491         mesElems.SetContentL( buffer, contTypeH );
   507         mesElems.SetContentL( buffer, contTypeH );
   492         // buffer ownership given to mesElems
   508         // buffer ownership given to mesElems
       
   509         CleanupStack::Pop( buffer );  // CS: 4
   493         // contTypeH ownership given to mesElems
   510         // contTypeH ownership given to mesElems
       
   511         CleanupStack::Pop( contTypeH );  // CS: 3
   494         
   512         
   495         // Set method
   513         // Set method
   496         elems->SetMethodL( SIPStrings::StringF( SipStrConsts::EMessage ) );
   514         elems->SetMethodL( SIPStrings::StringF( SipStrConsts::EMessage ) );
   497         
   515         
   498         CleanupStack::Pop( elems ); // CS: 2
       
   499         // Send the request transaction
   516         // Send the request transaction
   500         // elems, ownership given
       
   501         CSIPClientTransaction* sipTrans = iSipConnection->SendRequestL( elems,
   517         CSIPClientTransaction* sipTrans = iSipConnection->SendRequestL( elems,
   502             *regContext );
   518             *regContext );
   503         
   519         
   504         // Save SIP client transaction
   520         // Save SIP client transaction
   505         request->SetTransaction( sipTrans );
   521         request->SetTransaction( sipTrans );
   506         
   522         CleanupStack::Pop( elems ); // elems, ownership given  // CS: 2
   507         CleanupStack::PopAndDestroy( temp ); // CS: 1
   523         CleanupStack::PopAndDestroy( temp ); // CS: 1
   508         
   524         
   509         // Start refresh timer, it's used for garbage collection too.
   525         // Start refresh timer, it's used for garbage collection too.
   510         request->StartRefreshTimer();
   526         request->StartRefreshTimer();
   511         }
   527         }
  1073 
  1089 
  1074         acceH = CSIPAcceptHeader::NewLC(
  1090         acceH = CSIPAcceptHeader::NewLC(
  1075             KSimpleMultiType, KSimpleMultipartSubType );
  1091             KSimpleMultiType, KSimpleMultipartSubType );
  1076         User::LeaveIfError( headers.Append( acceH ));
  1092         User::LeaveIfError( headers.Append( acceH ));
  1077         ++popCount;
  1093         ++popCount;
  1078 
       
  1079         // add supported header with value eventlist
       
  1080         RPointerArray<CSIPSupportedHeader> suppHs =
       
  1081             CSIPSupportedHeader::DecodeL( KSimpleEventlist);
       
  1082         for( TInt count=0; count < suppHs.Count(); count++ )
       
  1083             {
       
  1084             User::LeaveIfError( headers.Append( suppHs[count] ));
       
  1085             }
       
  1086         suppHs.Close();
       
  1087         }
  1094         }
  1088     if ( r->Match( CSimpleRequest::EReqSubscribeWinfo ))
  1095     if ( r->Match( CSimpleRequest::EReqSubscribeWinfo ))
  1089         {
  1096         {
  1090         acceH = CSIPAcceptHeader::NewLC(
  1097         acceH = CSIPAcceptHeader::NewLC(
  1091             KSimpleApplicationType, KSimpleWinfoSubType );
  1098             KSimpleApplicationType, KSimpleWinfoSubType );
  1261     // memory for heap descriptors in DoHandleReceivedMessageL
  1268     // memory for heap descriptors in DoHandleReceivedMessageL
  1262     return error;
  1269     return error;
  1263     }
  1270     }
  1264 
  1271 
  1265 // ----------------------------------------------------------
  1272 // ----------------------------------------------------------
  1266 // CSimpleSipConnection::ConnectionChanged
       
  1267 // ----------------------------------------------------------
       
  1268 //
       
  1269 void CSimpleSipConnection::ConnectionChanged() 
       
  1270     {
       
  1271 #ifdef _DEBUG
       
  1272     TSimpleLogger::Log(_L("SipConnection: ConnectionChanged"));
       
  1273     if( iSipConnection )
       
  1274         {
       
  1275         TSimpleLogger::Log(_L("SipConnection: ConnectionChanged : old conn state %d (internal:%d)"), iSipConnection->State(), iSipState );
       
  1276         TSimpleLogger::Log(_L("SipConnection: ConnectionChanged : old IAP %d"),  iSipConnection->IapId() );
       
  1277         }
       
  1278 #endif
       
  1279 
       
  1280     iCurrentNbrSubs = 0;
       
  1281     CSIPConnection* conn = NULL;
       
  1282     TRAPD( err, conn = iProfileObserver->GiveConnectionL() );
       
  1283     if( !err )
       
  1284         {
       
  1285         delete iSipConnection;
       
  1286         iSipConnection = conn;
       
  1287 #ifdef _DEBUG
       
  1288         TSimpleLogger::Log(_L("SipConnection: ConnectionChanged : new conn state %d"), iSipConnection->State() );
       
  1289         TSimpleLogger::Log(_L("SipConnection: ConnectionChanged : new IAP %d"),  iSipConnection->IapId() );
       
  1290 #endif
       
  1291         }
       
  1292 #ifdef _DEBUG
       
  1293     else
       
  1294         {
       
  1295         TSimpleLogger::Log(_L("SipConnection: ConnectionChanged : Get SIP connection error %d"), err );        
       
  1296         }
       
  1297 #endif
       
  1298     
       
  1299     if( iSipConnection )
       
  1300         {
       
  1301         if( iSipState != iSipConnection->State() )
       
  1302             {
       
  1303             iConnectionObserver->ConnectionStateChanged( iSipConnection->State() );
       
  1304             }
       
  1305         }
       
  1306     }
       
  1307 
       
  1308 // ----------------------------------------------------------
       
  1309 // CSimpleSipConnection::StartToCheckExpiryL
  1273 // CSimpleSipConnection::StartToCheckExpiryL
  1310 // ----------------------------------------------------------
  1274 // ----------------------------------------------------------
  1311 //
  1275 //
  1312 void CSimpleSipConnection::StartToCheckExpiryL( CSimpleRequest& aReq )
  1276 void CSimpleSipConnection::StartToCheckExpiryL( CSimpleRequest& aReq )
  1313     {
  1277     {
  1404 
  1368 
  1405     while ( rIter )
  1369     while ( rIter )
  1406         {
  1370         {
  1407         CSimpleRequest* req = rIter;
  1371         CSimpleRequest* req = rIter;
  1408         rIter++; //lint !e1757
  1372         rIter++; //lint !e1757
  1409         
  1373         req->Destroy();
  1410         // open request EReqReceiveIM should not be deleted
       
  1411         // will be deleted only when im message received
       
  1412         // or destructor is called.
       
  1413         if ( !req->Match( CSimpleRequest::EReqReceiveIM ) )
       
  1414             {
       
  1415             req->Destroy();
       
  1416             }
       
  1417         }
       
  1418     }
       
  1419 
       
  1420 // -----------------------------------------------------------------------------
       
  1421 // CSimpleSipConnection::DeleteRequests
       
  1422 // -----------------------------------------------------------------------------
       
  1423 void CSimpleSipConnection::DeleteRequests( 
       
  1424     CSimpleRequest::TSimpleSipReqType aRequestType )
       
  1425     {
       
  1426 #ifdef _DEBUG
       
  1427     TSimpleLogger::Log(_L("SipConnection: DeleteRequests type=%d" ),
       
  1428         aRequestType );
       
  1429 #endif
       
  1430     // Delete buffered transaction requests match to the aRequestType
       
  1431     TDblQueIter<CSimpleRequest> rIter( iRequestList );
       
  1432     rIter.SetToFirst();
       
  1433 
       
  1434     while ( rIter )
       
  1435         {
       
  1436         CSimpleRequest* req = rIter;
       
  1437         rIter++; //lint !e1757
       
  1438         
       
  1439         if ( req->Match( aRequestType ) )
       
  1440             {
       
  1441             req->Destroy();
       
  1442             }
       
  1443         }
  1374         }
  1444     }
  1375     }
  1445 
  1376 
  1446 // -----------------------------------------------------------------------------
  1377 // -----------------------------------------------------------------------------
  1447 // CSimpleSipConnection::DeleteRequest
  1378 // CSimpleSipConnection::DeleteRequest
  1743         if ( header->Name() == extensionName )
  1674         if ( header->Name() == extensionName )
  1744             {
  1675             {
  1745             HBufC8* hValue = header->ToTextValueL();
  1676             HBufC8* hValue = header->ToTextValueL();
  1746             // hValue ownership is transferred
  1677             // hValue ownership is transferred
  1747             aReq->SetETag( hValue );
  1678             aReq->SetETag( hValue );
  1748             // store etag to vimpstsettingstore
       
  1749             StoreETagL( *hValue );
       
  1750             ETagReceived = ETrue;
  1679             ETagReceived = ETrue;
  1751             }
  1680             }
  1752         else if ( header->Name() == SIPStrings::StringF( SipStrConsts::EExpiresHeader))
  1681         else if ( header->Name() == SIPStrings::StringF( SipStrConsts::EExpiresHeader))
  1753             {
  1682             {
  1754             // Save the new refresh time.
  1683             // Save the new refresh time.
  1764         }
  1693         }
  1765     if ( !ETagReceived || !aReq->RefreshTime() )
  1694     if ( !ETagReceived || !aReq->RefreshTime() )
  1766         {
  1695         {
  1767         // Remove old ETag if nore received or if the expires header was 0 in our request.
  1696         // Remove old ETag if nore received or if the expires header was 0 in our request.
  1768         aReq->SetETag( NULL );
  1697         aReq->SetETag( NULL );
  1769         //TPtrC8 nullETag = nullETag.Alloc( KNullDesC8 ); 
       
  1770         TBufC8<1> nullETag( KNullDesC8 );
       
  1771         HBufC8* buf;
       
  1772         buf = nullETag.Alloc();
       
  1773         StoreETagL( *buf );
       
  1774         delete buf;
       
  1775         }
  1698         }
  1776     // PopAndDestroy calls extensionName.Close()
  1699     // PopAndDestroy calls extensionName.Close()
  1777     CleanupStack::PopAndDestroy( &extensionName );
  1700     CleanupStack::PopAndDestroy( &extensionName );
  1778     }
  1701     }
  1779 
  1702 
  2145 #ifdef _DEBUG
  2068 #ifdef _DEBUG
  2146     TSimpleLogger::Log(_L("SipConnection: RegisterAnyL" ));
  2069     TSimpleLogger::Log(_L("SipConnection: RegisterAnyL" ));
  2147 #endif
  2070 #endif
  2148 
  2071 
  2149     iSettings.ReadOTASettingsL( aReq.Aux() );
  2072     iSettings.ReadOTASettingsL( aReq.Aux() );
  2150     
  2073 
  2151     TInt32 uniqueId = iSettings.SipProfileId();
       
  2152     TUid uniqueUid;
       
  2153     uniqueUid.iUid = uniqueId;
       
  2154         
       
  2155     if ( !iSip && !iProfileObserver )
       
  2156         {        
       
  2157         iSip = CSIP::NewL( uniqueUid, *this );
       
  2158         
       
  2159         iProfileObserver = CSimpleSipProfileObserver::NewL( 
       
  2160             iSip, *iConnectionObserver);        
       
  2161         }
       
  2162     
       
  2163     iProfileObserver->RegisterGivenProfileL( iSettings.SipProfileId() );
  2074     iProfileObserver->RegisterGivenProfileL( iSettings.SipProfileId() );
  2164     SetSipState( ESimpleSipIdle );
  2075     SetSipState( ESimpleSipIdle );
  2165     TRAPD( err, iSipConnection = iProfileObserver->GiveConnectionL() );
  2076     TRAPD( err, iSipConnection = iProfileObserver->GiveConnectionL() );
  2166     if ( !err )
  2077     if ( !err )
  2167         {
  2078         {
  2626 void CSimpleSipConnection::RecognizeSipState( 
  2537 void CSimpleSipConnection::RecognizeSipState( 
  2627     TSimpleSipState aState )
  2538     TSimpleSipState aState )
  2628     {
  2539     {
  2629     if ( aState == ESimpleSipActive )
  2540     if ( aState == ESimpleSipActive )
  2630         {
  2541         {
  2631         // Check that SIP Profile is ready.
  2542         // Check that both SIP Profile and SIP connection are ready.
  2632         // iProfileObserver is created in ConstructL.
  2543         // iProfileObserver is created in ConstructL.
  2633         if ( iProfileObserver->IsProfileActive() )
  2544         if ( iProfileObserver->IsProfileActive() && 
       
  2545              iSipConnection &&
       
  2546              iSipConnection->State() == CSIPConnection::EActive )
  2634             {
  2547             {
  2635             SetSipState( aState );            
  2548             SetSipState( aState );            
  2636             }
  2549             }
  2637         }
  2550         }
  2638     else
  2551     else
  2662     TSimpleLogger::Log( _L(
  2575     TSimpleLogger::Log( _L(
  2663         "CSimpleSipConnection::DoHandleReceivedMessageL - End" ) );
  2576         "CSimpleSipConnection::DoHandleReceivedMessageL - End" ) );
  2664 #endif
  2577 #endif
  2665     }
  2578     }
  2666 
  2579 
  2667 // -----------------------------------------------------------------------------
       
  2668 // CSimpleRequest::SetServiceId
       
  2669 // -----------------------------------------------------------------------------
       
  2670 EXPORT_C void CSimpleSipConnection::SetServiceId( TInt32 aServiceId )
       
  2671     {
       
  2672 #ifdef _DEBUG
       
  2673     TSimpleLogger::Log( _L(
       
  2674                "CSimpleSipConnection::SetServiceId old serviceId = %d, new serviceId = %d" ),
       
  2675                    iServiceId, aServiceId );
       
  2676 #endif
       
  2677     iServiceId = aServiceId;
       
  2678     }
       
  2679 
       
  2680 // -----------------------------------------------------------------------------
       
  2681 // CSimpleRequest::StoreETag
       
  2682 // -----------------------------------------------------------------------------
       
  2683 void CSimpleSipConnection::StoreETagL( HBufC8& aETag )
       
  2684     {
       
  2685 #ifdef _DEBUG
       
  2686     TBuf<255> printDocumentId;
       
  2687         printDocumentId.Copy( aETag );
       
  2688     TSimpleLogger::Log(_L("CSimpleSipConnection: StoreETag ETag = %S, serviceId = %d" ),
       
  2689         &printDocumentId, iServiceId );
       
  2690 #endif
       
  2691     MVIMPSTSettingsStore* settings = CVIMPSTSettingsStore::NewLC();
       
  2692     
       
  2693     // Store ETag to uiservicetabsettings
       
  2694     User::LeaveIfError( settings->SetL( 
       
  2695         iServiceId, EServicePresenceSessionIdentifier, aETag ) );
       
  2696     
       
  2697     CleanupStack::PopAndDestroy(); //settings   
       
  2698     }
       
  2699