dvrengine/CommonRecordingEngine/src/CCRConnection.cpp
branchRCL_3
changeset 47 826cea16efd9
parent 45 798ee5f1972c
child 48 13a33d82ad98
equal deleted inserted replaced
45:798ee5f1972c 47:826cea16efd9
     1 /*
       
     2 * Copyright (c) 2007 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 the License "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:    Wrap rconnection*
       
    15 */
       
    16 
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CCRConnection.h"
       
    22 #include <e32msgqueue.h>
       
    23 #include <ipvideo/CRTypeDefs.h>
       
    24 #include "videoserviceutilsLogger.h"
       
    25 
       
    26 // CONSTANTS
       
    27 // None.
       
    28 
       
    29 // ============================ MEMBER FUNCTIONS ===============================
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // CCRConnection::CCRConnection
       
    33 // C++ default constructor can NOT contain any code, that might leave.
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 CCRConnection::CCRConnection( RSocketServ& aSockServer )
       
    37   : CActive( EPriorityStandard ), 
       
    38     iSockServer( aSockServer ),
       
    39     iState( CCRConnection::EIdle )
       
    40     {
       
    41     // None
       
    42     }
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CCRConnection::NewL
       
    46 // Two-phased constructor.
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 CCRConnection* CCRConnection::NewL( RSocketServ& aSockServer )
       
    50     {
       
    51     CCRConnection* self = new( ELeave ) CCRConnection( aSockServer );
       
    52     CleanupStack::PushL( self );
       
    53     self->ConstructL();
       
    54     CleanupStack::Pop( self );
       
    55     return self;
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CCRConnection::ConstructL
       
    60 // Symbian 2nd phase constructor can leave.
       
    61 // -----------------------------------------------------------------------------
       
    62 //
       
    63 void CCRConnection::ConstructL()
       
    64     {
       
    65     CActiveScheduler::Add( this );
       
    66     User::LeaveIfError( iConnection.Open( iSockServer ) );
       
    67     User::LeaveIfError( iConMon.ConnectL() );
       
    68 
       
    69     // Request bearer changes events from RConnectionMonitor
       
    70     User::LeaveIfError( iConMon.NotifyEventL( *this ) );
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CCRConnection::~CCRConnection
       
    75 // Destructor.
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 CCRConnection::~CCRConnection()
       
    79     {
       
    80     LOG( "CCRConnection::~CCRConnection()" );
       
    81 
       
    82     if ( iConMonProgressNotifyPending && iConnection.SubSessionHandle() )
       
    83         {
       
    84         LOG( "~CCRConnection CancelProgressNotification" );
       
    85         iConnection.CancelProgressNotification();
       
    86         }
       
    87     
       
    88     Cancel();
       
    89     iConMon.CancelNotifications();
       
    90     iConMon.Close();
       
    91     CloseRConnection();
       
    92     iObservers.Close();
       
    93     }
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // CCRConnection::Start
       
    97 // -----------------------------------------------------------------------------
       
    98 //      
       
    99 void CCRConnection::Attach( TUint aConnectionId ) 
       
   100     {
       
   101     LOG1( "CCRConnection::Attach: aConnectionId: %u", aConnectionId );
       
   102     Cancel();
       
   103     iCurrentConnectionId = aConnectionId;
       
   104     iConMon.GetConnectionCount( iConnectionCount, iStatus );
       
   105     SetActive();
       
   106     iState = CCRConnection::EFindingAP;
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // CCRConnection::DoCancel
       
   111 // If we need to cancel.
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 void CCRConnection::DoCancel()
       
   115     {
       
   116     LOG( "CCRConnection::DoCancel" );
       
   117     
       
   118     if ( iState == CCRConnection::EFindingAP )
       
   119         {
       
   120         LOG( "CCRConnection::DoCancel in EFindingAP" );
       
   121         iConMon.CancelAsyncRequest( EConnMonGetConnectionCount );
       
   122         }
       
   123     else if ( iState == CCRConnection::EFindingBearer )
       
   124         {
       
   125         LOG( "CCRConnection::DoCancel in EFindingBearer" ); 
       
   126         iConMon.CancelAsyncRequest( EConnMonGetIntAttribute );
       
   127         }
       
   128     else if ( iState == CCRConnection::EOpen && iConnection.SubSessionHandle() )
       
   129         {
       
   130         LOG( "CCRConnection::DoCancel in EOpen" );
       
   131         iConMonProgressNotifyPending = EFalse;
       
   132         iConnection.CancelProgressNotification();
       
   133         }
       
   134     else if ( iState == CCRConnection::EConnecting )
       
   135         {
       
   136         LOG( "CCRConnection::DoCancel in EConnecting" );
       
   137         // How to cancel a RConnection::Start?
       
   138         }
       
   139     
       
   140     LOG( "CCRConnection::DoCancel() out" );
       
   141     }
       
   142 
       
   143 // -----------------------------------------------------------------------------
       
   144 // CCRConnection::RunL
       
   145 // Request succesful completion.
       
   146 // -----------------------------------------------------------------------------
       
   147 //
       
   148 void CCRConnection::RunL()
       
   149     {
       
   150     LOG2( "CCRConnection::RunL(), iState: %d, iStatus: %d", 
       
   151         ( TInt )iState, iStatus.Int() );
       
   152     
       
   153     // Not pending any longer
       
   154     iConMonProgressNotifyPending = EFalse;
       
   155     
       
   156     // Handle state
       
   157     TInt err( KErrNone );   
       
   158     if ( iStatus.Int() == KErrNone )
       
   159         {
       
   160         if (  iState == CCRConnection::EFindingAP )
       
   161             {
       
   162             err = FindApL();
       
   163             }
       
   164     	else if ( iState == CCRConnection::EFindingBearer )
       
   165             {
       
   166             FindBearerL();
       
   167             }
       
   168         else if ( iState == CCRConnection::EOpen )
       
   169             {
       
   170             NotificationL();
       
   171             }
       
   172         }
       
   173     else
       
   174         {
       
   175         SendConnectionErrorToQueue( iStatus.Int() );
       
   176         }
       
   177 
       
   178     // Verify status
       
   179     if ( err )
       
   180         {
       
   181         LOG1( "CCRConnection::RunL(), err: %d", err );  
       
   182         SendConnectionErrorToQueue( err );
       
   183         }
       
   184     }
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CCRConnection::SendConnectionErrorToQueue
       
   188 // -----------------------------------------------------------------------------
       
   189 //
       
   190 void CCRConnection::SendConnectionErrorToQueue( TInt aError )
       
   191     {
       
   192     LOG1( "CCRConnection::SendConnectionErrorToQueue() aError: %d", aError );
       
   193     
       
   194     iState = CCRConnection::EIdle;
       
   195     iCurrentConnectionId = 0;
       
   196     iBearerType = EBearerUnknown;
       
   197 	CloseRConnection();
       
   198     MCRConnectionObserver::TCRConnectionStatus status;
       
   199     status = MCRConnectionObserver::ECRConnectionError;
       
   200     
       
   201     for ( TInt i( 0 ); i < iObservers.Count(); i++ )
       
   202         {
       
   203         iObservers[i]->ConnectionStatusChange( 0, status, aError );
       
   204         }
       
   205     }
       
   206 
       
   207 // -----------------------------------------------------------------------------
       
   208 // CCRConnection::RunError
       
   209 // If anything goes wrong.
       
   210 // -----------------------------------------------------------------------------
       
   211 //
       
   212 TInt CCRConnection::RunError( TInt aError )
       
   213     {
       
   214     LOG1( "CCRConnection::RunError: aError %d", aError );
       
   215     ( void )aError; // Prevent compiler warning
       
   216     iState = CCRConnection::EIdle;
       
   217     iCurrentConnectionId = 0;
       
   218     iBearerType = EBearerUnknown;
       
   219     CloseRConnection();
       
   220     return KErrNone; 
       
   221     }
       
   222 
       
   223 // -----------------------------------------------------------------------------
       
   224 // CCRConnection::CloseRConnection
       
   225 // -----------------------------------------------------------------------------
       
   226 //
       
   227 void CCRConnection::CloseRConnection()
       
   228     {
       
   229     LOG( "CCRConnection::CloseRConnection()" );
       
   230 
       
   231     if ( iConnection.SubSessionHandle() )
       
   232     	{
       
   233     	iConnection.Close();
       
   234     	}
       
   235     }
       
   236 
       
   237 // -----------------------------------------------------------------------------
       
   238 // CCRConnection::Connection
       
   239 // Returns the connection.
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 RConnection& CCRConnection::Connection ( void ) 
       
   243     {
       
   244     return iConnection; 
       
   245     }
       
   246     
       
   247 // -----------------------------------------------------------------------------
       
   248 // CCRConnection::State
       
   249 // Returns state of connection.
       
   250 // -----------------------------------------------------------------------------
       
   251 //
       
   252 CCRConnection::TConnectionState CCRConnection::State( void ) const 
       
   253     {
       
   254     return iState;  
       
   255     }
       
   256 
       
   257 // -----------------------------------------------------------------------------
       
   258 // CCRConnection::BearerType
       
   259 // -----------------------------------------------------------------------------
       
   260 //
       
   261 TConnMonBearerType CCRConnection::BearerType() const
       
   262     {
       
   263     return iBearerType;
       
   264     }
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // CCRConnection::RegisterObserver
       
   268 // -----------------------------------------------------------------------------
       
   269 //
       
   270 TInt CCRConnection::RegisterObserver( MCRConnectionObserver* aObserver )
       
   271     {
       
   272     LOG( "CCRConnection::RegisterObserver" );
       
   273     return iObservers.Append( aObserver );
       
   274     }
       
   275 
       
   276 // -----------------------------------------------------------------------------
       
   277 // CCRConnection::UnregisterObserver
       
   278 // -----------------------------------------------------------------------------
       
   279 //
       
   280 TInt CCRConnection::UnregisterObserver( MCRConnectionObserver* aObserver )
       
   281     {
       
   282     LOG( "CCRConnection::UnregisterObserver" );
       
   283     TInt pos = iObservers.Find( aObserver );
       
   284     if ( pos < KErrNone ) 
       
   285         {
       
   286         return pos;
       
   287         }
       
   288 
       
   289     iObservers.Remove( pos );
       
   290     return KErrNone;
       
   291     }
       
   292 
       
   293 // -----------------------------------------------------------------------------
       
   294 // CCRConnection::GetHeuristic
       
   295 // -----------------------------------------------------------------------------
       
   296 //
       
   297 TBool CCRConnection::GetHeuristic( TConnectionHeuristic aHeuristic )
       
   298     {
       
   299     TInt bit( 1 << ( TInt )aHeuristic );
       
   300     return ( TBool )( iHeuristics & bit );
       
   301     }
       
   302 
       
   303 // -----------------------------------------------------------------------------
       
   304 // CCRConnection::SetHeuristic
       
   305 // -----------------------------------------------------------------------------
       
   306 //
       
   307 void CCRConnection::SetHeuristic( TConnectionHeuristic aHeuristic, TBool aValue )
       
   308     {
       
   309     TInt mask( 1 << ( TInt )aHeuristic );
       
   310     if ( aValue )
       
   311         {
       
   312         iHeuristics |= mask;
       
   313         }
       
   314     else
       
   315         {
       
   316         iHeuristics &= ~mask;
       
   317         }
       
   318     }
       
   319 
       
   320 // -----------------------------------------------------------------------------
       
   321 // CCRConnection::EventL
       
   322 // -----------------------------------------------------------------------------
       
   323 //
       
   324 void CCRConnection::EventL( const CConnMonEventBase& aEvent )
       
   325     {
       
   326 	// bearer change events
       
   327     if( aEvent.EventType()==EConnMonBearerChange && iState==EOpen )
       
   328         {
       
   329         // IMPORTANT: EConnMonBearerChange event report changes in *some* connection, not
       
   330         // necessarly ours and aEvent.ConnectionId() doest *not* contain plain 'connection id',
       
   331         // it has 'bearer id'. So make a new bearertype query to make sure it's ours.
       
   332         LOG2( "CCRConnection::EventL: bearer changed, id=%d, bearer=%d", 
       
   333             aEvent.ConnectionId(), ( ( CConnMonBearerChange* )( &aEvent) )->Bearer() );
       
   334 
       
   335         // Cancel ongoing requests
       
   336         if ( IsActive() )
       
   337             {
       
   338             Cancel();
       
   339             }
       
   340 
       
   341         iState = CCRConnection::EFindingBearer;
       
   342         iConMon.GetIntAttribute( iCurrentConnectionId, 0, KBearer,
       
   343                                  ( TInt& )iNewBearerType, iStatus );
       
   344 		SetActive();
       
   345         }
       
   346     // other unhandled events
       
   347     else
       
   348         {
       
   349         LOG2( "CCRConnection::EventL: unknown event=%d, connection=%d",
       
   350                               aEvent.EventType(), aEvent.ConnectionId() );
       
   351         }
       
   352     }
       
   353 
       
   354 // -----------------------------------------------------------------------------
       
   355 // CCRConnection::IsBearerWLANor3G
       
   356 // -----------------------------------------------------------------------------
       
   357 //
       
   358 TBool CCRConnection::IsBearerWLANor3G( TConnMonBearerType aBearer )
       
   359     {
       
   360     return aBearer == EBearerWCDMA    ||  // from CIptvNetworkEngine::IsBearer3GOrWLAN,
       
   361            aBearer == EBearerWLAN     ||  // EBearerLAN is returned by emulator
       
   362            aBearer == EBearerCDMA2000 ||
       
   363            aBearer == EBearerLAN;
       
   364     }
       
   365 
       
   366 // -----------------------------------------------------------------------------
       
   367 // CCRConnection::MaximumBandwidth
       
   368 // -----------------------------------------------------------------------------
       
   369 //
       
   370 TInt CCRConnection::MaximumBandwidth()
       
   371     {
       
   372     // Determine bandwidth based on bearer or from ConnectionMonitor attributes
       
   373     // (KMaximumBitrateDownlink, KGuaranteedBitrateDownlink) when QoS is supported
       
   374     TConnMonBearerType bearer = BearerType();
       
   375     TInt bandwidth( 0 );
       
   376 
       
   377     switch( bearer )
       
   378         {
       
   379         case EBearerGPRS:
       
   380             bandwidth = KCRBandwidthGPRS;
       
   381             break;
       
   382         
       
   383         case EBearerEdgeGPRS:
       
   384             bandwidth = KCRBandwidthEdgeGPRS;
       
   385             break;
       
   386         
       
   387         case EBearerWCDMA:
       
   388             bandwidth = KCRBandwidthWCDMA;
       
   389             break;
       
   390         
       
   391         case EBearerWLAN:
       
   392             bandwidth = KCRBandwidthWLAN;
       
   393             break;
       
   394         
       
   395         case EBearerLAN:
       
   396             bandwidth = KCRBandwidthLAN;
       
   397             break;
       
   398         
       
   399         default:
       
   400             // None
       
   401             break;
       
   402         }
       
   403 
       
   404     return bandwidth;
       
   405     }
       
   406 
       
   407 // -----------------------------------------------------------------------------
       
   408 // CCRConnection::FindApL
       
   409 // Request succesful completion.
       
   410 // -----------------------------------------------------------------------------
       
   411 //
       
   412 TInt CCRConnection::FindApL()
       
   413     {
       
   414     CloseRConnection();
       
   415     TInt err( iConnection.Open( iSockServer ) );
       
   416     if ( err )
       
   417         {
       
   418         LOG1( "CCRConnection::FindApL(), Open iConnection err: %d", err );
       
   419         return err;
       
   420         }
       
   421     
       
   422     // Connections
       
   423     TUint foundConnections( 0 );
       
   424     err = iConnection.EnumerateConnections( foundConnections );
       
   425     if ( err )
       
   426         {
       
   427         LOG1( "CCRConnection::FindApL(), EnumerateConnections fail: %d", err );
       
   428         return err;
       
   429         }
       
   430 
       
   431     // Active connection found
       
   432     TUint conId( 0 ); // connection id
       
   433     TUint subId( 0 ); // subconnection id
       
   434     TUint conToUse( 0 );
       
   435     for ( TInt i( 1 ); i <= foundConnections && !err; i++ )
       
   436         {
       
   437         err = iConMon.GetConnectionInfo( i, conId, subId );
       
   438         if ( !err && conId == iCurrentConnectionId ) 
       
   439             {
       
   440             conToUse = i;   
       
   441             i = foundConnections + 1; // break from loop
       
   442             }
       
   443         }
       
   444     if ( err )
       
   445         {
       
   446         LOG1( "CCRConnection::FindApL() GetConnectionInfo loop err: %d", err );
       
   447         return err;
       
   448         }
       
   449     
       
   450     // Connection info
       
   451     TPckgBuf<TConnectionInfo> info;
       
   452     err = iConnection.GetConnectionInfo( conToUse, info );
       
   453     if ( err )
       
   454         {
       
   455         LOG1( "CCRConnection::FindApL(), GetConnectionInfo 2 fail: %d", err );
       
   456         return err;
       
   457         }
       
   458 
       
   459     // Attach
       
   460     err = iConnection.Attach( info, RConnection::EAttachTypeNormal );
       
   461     if ( err )
       
   462         {
       
   463         LOG1( "CCRConnection::FindApL(), Attach failed: %d", err );
       
   464         }
       
   465     
       
   466     // Bearer type
       
   467     iState = CCRConnection::EFindingBearer;
       
   468     iBearerType = EBearerUnknown;
       
   469     iConMon.GetIntAttribute( 
       
   470         iCurrentConnectionId, 0, KBearer, ( TInt& )iNewBearerType, iStatus );
       
   471     SetActive();
       
   472     return err;
       
   473     }
       
   474 
       
   475 // -----------------------------------------------------------------------------
       
   476 // CCRConnection::FindBearerL
       
   477 // Request succesful completion.
       
   478 // -----------------------------------------------------------------------------
       
   479 //
       
   480 void CCRConnection::FindBearerL()
       
   481     {
       
   482     LOG2( "CCRConnection::FindBearerL(), iCurrentConnectionId: %d, iNewBearerType: %d",
       
   483         iCurrentConnectionId, iNewBearerType );
       
   484 
       
   485     iState = CCRConnection::EOpen;
       
   486     TBool was3g( IsBearerWLANor3G( iBearerType ) );
       
   487     TBool is3g( IsBearerWLANor3G( iNewBearerType ) );
       
   488     TBool genChanged( was3g^is3g );
       
   489     TBool justConnected( iBearerType == EBearerUnknown );
       
   490     iBearerType = iNewBearerType;
       
   491 
       
   492     // Notify if connection has just gone up
       
   493     if ( justConnected )
       
   494         {
       
   495         // Broadcast IapUp&Running for observers
       
   496         MCRConnectionObserver::TCRConnectionStatus status;
       
   497         status = MCRConnectionObserver::ECRAttachCompleted;
       
   498         for ( TInt i( 0 ); i < iObservers.Count(); i++ )
       
   499             {
       
   500             iObservers[i]->ConnectionStatusChange( 0, status, iStatus.Int() );
       
   501             }
       
   502         }
       
   503 
       
   504     // Notify if connection has just gone up or generation changed
       
   505     if ( genChanged )
       
   506         {
       
   507         // Broadcast IapUp&Running for observers
       
   508         MCRConnectionObserver::TCRConnectionStatus status;
       
   509         status = MCRConnectionObserver::ECRBearerChanged;
       
   510         for( TInt i=0 ; i<iObservers.Count() ; i++ )
       
   511             {
       
   512             iObservers[i]->ConnectionStatusChange( 0, status, iStatus.Int() );
       
   513             }
       
   514         }
       
   515     if ( iConnection.SubSessionHandle() )
       
   516         {
       
   517         // Request connection progress notifications from RConnection
       
   518         iConMonProgressNotifyPending = ETrue;
       
   519         iConnection.ProgressNotification( iNotification, iStatus );
       
   520         SetActive();    
       
   521         }
       
   522     }
       
   523 
       
   524 // -----------------------------------------------------------------------------
       
   525 // CCRConnection::NotificationL
       
   526 // Request succesful completion.
       
   527 // -----------------------------------------------------------------------------
       
   528 //
       
   529 void CCRConnection::NotificationL()
       
   530     {
       
   531     TNifProgress notification = iNotification();
       
   532     LOG2( "CCRConnection::NotificationL stage: %d err: %d",
       
   533         ( TInt )( notification.iStage ), ( TInt )( notification.iError ) );
       
   534 
       
   535     if ( notification.iError != KErrNone ) 
       
   536         {
       
   537         // Notify UI
       
   538         iState = CCRConnection::EIdle;
       
   539     
       
   540         MCRConnectionObserver::TCRConnectionStatus status;
       
   541         status = MCRConnectionObserver::ECRIapDown;
       
   542         for ( TInt i( 0 ); i < iObservers.Count(); i++ )
       
   543             {
       
   544             iObservers[i]->ConnectionStatusChange( 0, status, notification.iError );
       
   545             }
       
   546     
       
   547         if ( iConnection.SubSessionHandle() )
       
   548             {
       
   549             iConnection.CancelProgressNotification();
       
   550             iConMonProgressNotifyPending = EFalse;
       
   551             CloseRConnection();
       
   552             }
       
   553         
       
   554         iCurrentConnectionId = 0;
       
   555         iBearerType = EBearerUnknown;   
       
   556         }
       
   557     else
       
   558         {
       
   559         switch ( notification.iStage ) 
       
   560             {
       
   561             case KLinkLayerOpen:
       
   562                 // this means we're open
       
   563                 break;
       
   564     
       
   565             case KConfigDaemonStartingDeregistration:
       
   566             case KConfigDaemonUnloading:
       
   567             case KConfigDaemonUnloaded:
       
   568             case KLinkLayerClosed:
       
   569                 // And we're closed or closing
       
   570                 break;
       
   571     
       
   572             default:
       
   573                 // Do nothing in rest of the cases
       
   574                 break;
       
   575             }
       
   576     
       
   577         if ( iConnection.SubSessionHandle() )
       
   578             {
       
   579             iConnection.ProgressNotification( iNotification, iStatus );
       
   580             iConMonProgressNotifyPending = ETrue;
       
   581             SetActive();    
       
   582             }
       
   583         }
       
   584     }
       
   585 
       
   586 //  End of File