mtpfws/mtpfw/src/cmtpconnectionmgr.cpp
changeset 0 d0791faffa3f
child 6 f8e15b44d440
child 17 aabe5387f5ce
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "cmtpconnectionmgr.h"
       
    17 
       
    18 #include "cmtpconnection.h"
       
    19 #include "cmtptransportplugin.h"
       
    20 #include "mmtptransportconnection.h"
       
    21 
       
    22 // Class constants.
       
    23 __FLOG_STMT(_LIT8(KComponent,"ConnectionMgr");)
       
    24 
       
    25 /**
       
    26 CMTPConnectionMgr factory method.
       
    27 @leave If a failure occurs, one of the system wide error codes.
       
    28 */
       
    29 CMTPConnectionMgr* CMTPConnectionMgr::NewL()
       
    30     {
       
    31     CMTPConnectionMgr* self = new(ELeave) CMTPConnectionMgr();
       
    32     return self;
       
    33     }
       
    34 
       
    35 /**
       
    36 Destructor.
       
    37 */  
       
    38 CMTPConnectionMgr::~CMTPConnectionMgr()
       
    39     {
       
    40     StopTransport( iTransportUid, ETrue );
       
    41     iConnections.ResetAndDestroy();
       
    42     iSuspendedTransports.Reset();
       
    43     iSuspendedTransports.Close();
       
    44     delete iTransportTrigger;
       
    45     __FLOG_CLOSE;
       
    46     }
       
    47 
       
    48 /**
       
    49 Provides a reference to the connection with the specified connection identifier.
       
    50 @param aConnectionId The connection identifier.
       
    51 @return The connection reference.
       
    52 @leave KErrNotFound If a connection with the specified identifier does not 
       
    53 exist.
       
    54 */
       
    55 EXPORT_C CMTPConnection& CMTPConnectionMgr::ConnectionL(TUint aConnectionId) const
       
    56     {   
       
    57     __FLOG(_L8("ConnectionL - Entry"));
       
    58     
       
    59     TInt idx(ConnectionFind(aConnectionId));
       
    60     
       
    61     __FLOG_VA((_L8("idx is %d "), idx));
       
    62     __ASSERT_ALWAYS((idx != KErrNotFound), User::Invariant());
       
    63     
       
    64     __FLOG(_L8("ConnectionL - Exit"));
       
    65     return *iConnections[idx];
       
    66     }
       
    67 
       
    68 /**
       
    69 Provides a count of the number of currently open connections.
       
    70 @return The count of currently open connections.
       
    71 */  
       
    72 TUint CMTPConnectionMgr::ConnectionCount() const
       
    73     {
       
    74     return iConnections.Count();
       
    75     }
       
    76 
       
    77 /**
       
    78 Provide a non-const reference to the located at the specified position within 
       
    79 the connection table.
       
    80 @return A non-const reference to the required connection.
       
    81 */
       
    82 CMTPConnection& CMTPConnectionMgr::operator[](TInt aIndex) const
       
    83     {
       
    84     return *iConnections[aIndex];
       
    85     }
       
    86     
       
    87 /**
       
    88 Returns the current transportID.
       
    89 @return The CMTPTransportPlugin interface implementation UID.
       
    90 */
       
    91 EXPORT_C TUid CMTPConnectionMgr::TransportUid()
       
    92     {
       
    93     return iTransportUid;
       
    94     }
       
    95 
       
    96 EXPORT_C void CMTPConnectionMgr::StartTransportL(TUid aTransport)
       
    97     {
       
    98     StartTransportL( aTransport, NULL );
       
    99     }
       
   100 
       
   101 /**
       
   102 Loads and starts up the MTP transport plug-in with the specified 
       
   103 CMTPTransportPlugin interface implementation UID. Only one MTP transport 
       
   104 plug-in can be loaded at any given time.
       
   105 @param The CMTPTransportPlugin interface implementation UID.
       
   106 @leave KErrNotSupported If an attempt is made to load a second MTP transport
       
   107 plug-in.
       
   108 @leave One of the system wide error codes, if a processing failure occurs.
       
   109 @see StopTransport
       
   110 */
       
   111 EXPORT_C void CMTPConnectionMgr::StartTransportL(TUid aTransport, const TAny* aParameter)
       
   112     {
       
   113 	__FLOG(_L8("StartTransportL - Entry"));
       
   114 	
       
   115     if (iTransport)
       
   116         {
       
   117         if (aTransport != iTransportUid)
       
   118             {
       
   119             // Multiple transports not currently supported.
       
   120             User::Leave(KErrNotSupported);
       
   121             }
       
   122         }
       
   123     else
       
   124         {
       
   125 
       
   126         iTransport = CMTPTransportPlugin::NewL(aTransport, aParameter);
       
   127 
       
   128         TRAPD(err, iTransport->StartL(*this));
       
   129 		if (err != KErrNone)
       
   130 			{
       
   131 			__FLOG_VA( ( _L8("StartTransportL error, error code = %d"), err) );
       
   132 			delete iTransport;
       
   133 			iTransport = NULL;
       
   134 			User::Leave(err);
       
   135 			}
       
   136         iTransportUid = aTransport;       
       
   137 		
       
   138         iTransportCount++;
       
   139         UnsuspendTransport( iTransportUid );
       
   140         }
       
   141 		
       
   142 	__FLOG(_L8("StartTransportL - Exit"));
       
   143     }
       
   144 
       
   145 /**
       
   146 Queue the transport to start when there is no running transport
       
   147 @param aTransport, The CMTPTransportPlugin interface implementation UID.
       
   148 @param aParameter, reserved
       
   149 @leave One of the system wide error codes, if the operation fails.
       
   150 */
       
   151 EXPORT_C void CMTPConnectionMgr::QueueTransportL( TUid aTransport, const TAny* /*aParameter*/ )
       
   152     {
       
   153     __FLOG_VA( ( _L8("+QueueTransportL( 0x%08X )"), aTransport.iUid ) );
       
   154     __ASSERT_DEBUG( ( KErrNotFound == iSuspendedTransports.Find( aTransport ) ), User::Invariant() );
       
   155     iSuspendedTransports.InsertL( aTransport, 0 );
       
   156     __FLOG( _L8("-QueueTransportL") );
       
   157     }
       
   158 
       
   159 EXPORT_C void CMTPConnectionMgr::SetClientSId(TUid aSecureId)
       
   160 	{
       
   161 	iSecureId=aSecureId;
       
   162 	}
       
   163 
       
   164 /**
       
   165 Shuts down and unloads the MTP transport plug-in with the specified 
       
   166 CMTPTransportPlugin interface implementation UID.
       
   167 @param The CMTPTransportPlugin interface implementation UID.
       
   168 */
       
   169 EXPORT_C void CMTPConnectionMgr::StopTransport(TUid aTransport)
       
   170     {
       
   171     StopTransport( aTransport, EFalse );
       
   172     }
       
   173 
       
   174 /**
       
   175 Shuts down and unloads the MTP transport plug-in with the specified 
       
   176 CMTPTransportPlugin interface implementation UID.
       
   177 @param aTransport The CMTPTransportPlugin interface implementation UID.
       
   178 @param aByBearer If ETrue, it means the transport plugin is stopped because the bearer is turned off or not activated.
       
   179 */
       
   180 EXPORT_C void CMTPConnectionMgr::StopTransport( TUid aTransport, TBool aByBearer )
       
   181     {
       
   182 	__FLOG(_L8("StopTransport - Entry"));
       
   183 	
       
   184     if ( ( iTransport ) && ( aTransport == iTransportUid ) )
       
   185         {
       
   186         if ( !aByBearer )
       
   187             {
       
   188             TRAP_IGNORE( SuspendTransportL( aTransport ) );
       
   189             }
       
   190         iTransport->Stop(*this);
       
   191         delete iTransport;
       
   192         iTransport = NULL;
       
   193         iTransportUid = KNullUid;
       
   194         iTransportCount--;
       
   195 
       
   196 
       
   197         }
       
   198     if ( aByBearer )
       
   199         {
       
   200         UnsuspendTransport( aTransport );
       
   201         }
       
   202 		
       
   203 	__FLOG(_L8("StopTransport - Exit"));
       
   204     }
       
   205 
       
   206 /**
       
   207 Shuts down and unloads all active MTP transport plug-ins.
       
   208 */
       
   209 EXPORT_C void CMTPConnectionMgr::StopTransports()
       
   210     {
       
   211     if (iTransport)
       
   212         {
       
   213         iTransport->Stop(*this);
       
   214         delete iTransport;
       
   215         iTransport = NULL;
       
   216         iTransportUid = KNullUid;
       
   217         iTransportCount--;
       
   218         }
       
   219     }
       
   220 
       
   221 /**
       
   222 Returns the number of active Transports.
       
   223 @return Number of active transports
       
   224 */
       
   225 EXPORT_C TInt CMTPConnectionMgr::TransportCount() const
       
   226     {
       
   227 	return iTransportCount;
       
   228     }
       
   229 
       
   230 void CMTPConnectionMgr::ConnectionClosed(MMTPTransportConnection& aTransportConnection)
       
   231     {
       
   232     __FLOG(_L8("ConnectionClosed - Entry"));
       
   233     
       
   234     TInt idx(ConnectionFind(aTransportConnection.BoundProtocolLayer().ConnectionId()));
       
   235     __FLOG_VA((_L8("idx is %d "), idx));
       
   236     __ASSERT_DEBUG((idx != KErrNotFound), User::Invariant());
       
   237     
       
   238     CMTPConnection* connection(iConnections[idx]);
       
   239     connection->ConnectionSuspended();
       
   240     
       
   241     ResumeSuspendedTransport();
       
   242     
       
   243     __FLOG(_L8("ConnectionClosed - Exit"));
       
   244     }
       
   245     
       
   246 void CMTPConnectionMgr::ConnectionOpenedL(MMTPTransportConnection& aTransportConnection)
       
   247     {   
       
   248     __FLOG(_L8("ConnectionOpenedL - Entry"));
       
   249     
       
   250     TUint impUid = aTransportConnection.GetImplementationUid();
       
   251     TInt idx = ConnectionFind(impUid);
       
   252     CMTPConnection* connection = NULL;
       
   253     
       
   254     if (idx == KErrNotFound)
       
   255         {
       
   256         // take transport connection implementation UID as connection ID
       
   257         connection = CMTPConnection::NewLC(impUid, aTransportConnection);
       
   258         iConnections.InsertInOrder(connection, iConnectionOrder);
       
   259         CleanupStack::Pop(connection); 
       
   260         }
       
   261     else
       
   262         {
       
   263         connection = iConnections[idx];
       
   264         }
       
   265     connection->ConnectionResumedL(aTransportConnection);
       
   266     
       
   267     __FLOG(_L8("ConnectionOpenedL - Exit"));
       
   268     }
       
   269 
       
   270 TBool CMTPConnectionMgr::DeleteConnection(TUint aConnectionId)
       
   271     {
       
   272     __FLOG(_L8("DeleteConnection - Entry"));
       
   273     
       
   274     TBool ret = EFalse;    
       
   275     TInt idx = ConnectionFind(aConnectionId);
       
   276     
       
   277     if (idx != KErrNotFound)
       
   278         {
       
   279         CMTPConnection* connection(iConnections[idx]);
       
   280         iConnections.Remove(idx);
       
   281         delete connection;
       
   282         ret = ETrue;
       
   283         }
       
   284     
       
   285     __FLOG(_L8("DeleteConnection - Entry"));
       
   286     
       
   287     return ret;
       
   288     }
       
   289 
       
   290 EXPORT_C TUid CMTPConnectionMgr::ClientSId()
       
   291 	{
       
   292 	return iSecureId;
       
   293 	}
       
   294 /**
       
   295 Constructor.
       
   296 */
       
   297 CMTPConnectionMgr::CMTPConnectionMgr() :
       
   298     iConnectionOrder(ConnectionOrderCompare),
       
   299     iShutdownConnectionIdx(KErrNotFound),
       
   300 	iTransportUid(KNullUid)
       
   301     {
       
   302     __FLOG_OPEN(KMTPSubsystem, KComponent);    
       
   303     }
       
   304 
       
   305 /**
       
   306 Provides the connections table index of the connection with the specified 
       
   307 connection identifier.
       
   308 @param The identifier of the required connection.
       
   309 @return The connection table index of the required connection, or KErrNotFound
       
   310 if the connection identifier could not be found.
       
   311 */ 
       
   312 TInt CMTPConnectionMgr::ConnectionFind(TUint aConnectionId) const
       
   313     {
       
   314     __FLOG_VA((_L8("ConnectionFind - Entry with connectionId %d "), aConnectionId));
       
   315     TInt ret(KErrNotFound);
       
   316     
       
   317     const TUint noConnections = iConnections.Count();
       
   318     for (TUint i(0); ((i < noConnections) && (ret == KErrNotFound)); i++)
       
   319         {
       
   320         TInt id(iConnections[i]->ConnectionId());
       
   321         if (aConnectionId == id)
       
   322             {
       
   323             ret = i;
       
   324             break;
       
   325             }
       
   326         }
       
   327     __FLOG(_L8("ConnectionFind - Exit"));    
       
   328     return ret;
       
   329     }
       
   330 
       
   331 /**
       
   332 Determines the relative order of two CMTPConnection objects based on their 
       
   333 connection IDs.
       
   334 @return Zero, if the two objects are equal; a negative value, if the aFirst 
       
   335 is less than aSecond, or; a positive value, if the aFirst is greater than 
       
   336 aSecond.
       
   337 */
       
   338 TInt CMTPConnectionMgr::ConnectionOrderCompare(const CMTPConnection& aFirst, const CMTPConnection& aSecond)
       
   339     {
       
   340     return aFirst.ConnectionId() - aSecond.ConnectionId();
       
   341     }
       
   342 
       
   343 /**
       
   344 Append the transport to the suspended transport list
       
   345 @param aTransport, The implementation UID of the suspended transport plugin
       
   346 @leave One of the system wide error codes, if the operation fails.
       
   347 */
       
   348 void CMTPConnectionMgr::SuspendTransportL( TUid aTransport )
       
   349     {
       
   350     __FLOG_1( _L8("+SuspendTransportL( 0x%08X )"), aTransport );
       
   351     if ( KErrNotFound == iSuspendedTransports.Find( aTransport ) )
       
   352         {
       
   353         iSuspendedTransports.AppendL( aTransport );
       
   354         }
       
   355     __FLOG( _L8("-SuspendTransportL") );
       
   356     }
       
   357 
       
   358 /**
       
   359 Remove transport from the suspended transports list
       
   360 @param aTransport, The CMTPTransportPlugin interface implementation UID 
       
   361 */
       
   362 void CMTPConnectionMgr::UnsuspendTransport( TUid aTransport )
       
   363     {
       
   364     __FLOG_1( _L8("+UnsuspendTransport( 0x%08X )"), aTransport.iUid );
       
   365     TInt idx = iSuspendedTransports.Find( aTransport );
       
   366     if ( KErrNotFound != idx )
       
   367         {
       
   368         __FLOG_1( _L8("Remove the number %d suspended transport"), idx );
       
   369         iSuspendedTransports.Remove( idx );
       
   370         }
       
   371     __FLOG( _L8("-UnsuspendTransport") );
       
   372     }
       
   373 
       
   374 /**
       
   375 Prepare to resume suspended transport
       
   376 */
       
   377 void CMTPConnectionMgr::ResumeSuspendedTransport()
       
   378     {
       
   379     __FLOG( _L8("+ResumeSuspendedTransport") );
       
   380     const TInt count = iSuspendedTransports.Count();
       
   381     if ( ( count > 0 )
       
   382         // If the transport was just switched and suspended, it shouldn't be resumed.
       
   383         && ( iTransportUid != iSuspendedTransports[count-1] ) )
       
   384         {
       
   385         __FLOG( _L8("Found suspended transport(s).") );
       
   386         if ( !iTransportTrigger )
       
   387             {
       
   388             iTransportTrigger = new( ELeave ) CAsyncCallBack( CActive::EPriorityStandard );
       
   389             }
       
   390         __ASSERT_DEBUG( ( !iTransportTrigger->IsActive() ), User::Invariant() );
       
   391         TCallBack callback( CMTPConnectionMgr::DoResumeSuspendedTransport, this );
       
   392         iTransportTrigger->Set( callback );
       
   393         iTransportTrigger->CallBack();
       
   394         }
       
   395     __FLOG( _L8("-ResumeSuspendedTransport") );
       
   396     }
       
   397 
       
   398 /**
       
   399 Resume suspended transport
       
   400 @param aSelf, The memory address of the CMTPConnectionMgr instance
       
   401 @return KErrNone, but the value is ignored.
       
   402 */
       
   403 TInt CMTPConnectionMgr::DoResumeSuspendedTransport( TAny* aSelf )
       
   404     {
       
   405     CMTPConnectionMgr* self = reinterpret_cast< CMTPConnectionMgr* >( aSelf );
       
   406     __ASSERT_DEBUG( ( self->iSuspendedTransports.Count() > 0 ), User::Invariant() );
       
   407     TRAP_IGNORE( self->StartTransportL( self->iSuspendedTransports[self->iSuspendedTransports.Count()-1] ) );
       
   408     return KErrNone;
       
   409     }
       
   410