mtpfws/mtpfw/src/cmtpconnectionmgr.cpp
changeset 17 aabe5387f5ce
parent 0 d0791faffa3f
child 22 a5c0bb5018eb
child 29 3ae5cb0b4c02
--- a/mtpfws/mtpfw/src/cmtpconnectionmgr.cpp	Tue Feb 02 01:11:40 2010 +0200
+++ b/mtpfws/mtpfw/src/cmtpconnectionmgr.cpp	Fri Mar 19 09:40:39 2010 +0200
@@ -13,6 +13,8 @@
 // Description:
 //
 
+#include <usbman.h>
+#include <usbstates.h>
 #include "cmtpconnectionmgr.h"
 
 #include "cmtpconnection.h"
@@ -93,9 +95,59 @@
     return iTransportUid;
     }
 
+void CMTPConnectionMgr::ConnectionCloseComplete(const TUint& /*aConnUid*/)
+    {
+    __FLOG(_L8("ConnectionCloseComplete - Entry"));
+    ResumeSuspendedTransport();
+    __FLOG(_L8("ConnectionCloseComplete - exit"));
+    }
+
 EXPORT_C void CMTPConnectionMgr::StartTransportL(TUid aTransport)
     {
-    StartTransportL( aTransport, NULL );
+    
+    TInt32 bluetoothUid = 0x10286FCB;
+    
+    RUsb usb;
+    User::LeaveIfError(usb.Connect());
+    TInt usbMode;
+    TUsbServiceState usbStat;
+    TInt err = usb.GetCurrentPersonalityId(usbMode);
+    __FLOG_1(_L8("The return value of GetCurrentPersonalityId is %d"), err);
+    
+    err = usb.GetServiceState(usbStat);
+    __FLOG_1(_L8("The return value of GetServiceState is %d"), err);        
+    
+    usb.Close();
+    
+    __FLOG_1(_L8("The current usb mode is %d"), usbMode);
+    __FLOG_1(_L8("The current usb service state is %d"), usbStat);
+
+    TInt massStorageMode = 0x02;
+    
+    if(usbMode == massStorageMode && usbStat != EUsbServiceIdle)
+        {
+        __FLOG(_L8("StartTransportL without parameter!"));
+        StartTransportL( aTransport, NULL );
+        return;
+        }
+    
+    
+    
+    //When USB plug out, BT will start Master mode to reconnect remote device. Else BT will start slave mode to listen connection.
+    if(aTransport.iUid == bluetoothUid && iRemoteDevice.iDeviceAddr != 0 && aTransport != iTransportUid)
+        {
+        __FLOG(_L8("StartTransportL with parameter!"));
+        TMTPBTRemoteDeviceBuf tmpdata(iRemoteDevice);
+        StartTransportL( aTransport, &tmpdata );
+        iRemoteDevice.iDeviceAddr = 0;
+        iRemoteDevice.iDeviceServicePort = 0;
+        }
+    else
+        {
+        __FLOG(_L8("StartTransportL without parameter!"));
+        StartTransportL( aTransport, NULL );
+        }
+    
     }
 
 /**
@@ -110,33 +162,70 @@
 */
 EXPORT_C void CMTPConnectionMgr::StartTransportL(TUid aTransport, const TAny* aParameter)
     {
-	__FLOG(_L8("StartTransportL - Entry"));
-	
+    __FLOG(_L8("StartTransportL - Entry"));
+    
+    TInt32 bluetoothUid = 0x10286FCB;
+    
     if (iTransport)
         {
+        __FLOG(_L8("The transport is not none."));
         if (aTransport != iTransportUid)
             {
             // Multiple transports not currently supported.
+            __FLOG(_L8("Multiple transports are not supported now!"));
             User::Leave(KErrNotSupported);
             }
+        else
+            {
+            __FLOG_1(_L8("Relaunch the transport 0x%X"), iTransportUid.iUid);
+            if(aTransport.iUid == bluetoothUid)
+                {
+                iTransport->Stop(*this);
+                delete iTransport;
+                
+                iTransport = CMTPTransportPlugin::NewL(aTransport, aParameter);
+                
+                TRAPD(err, iTransport->StartL(*this));
+                if (err != KErrNone)
+                    {
+                    __FLOG_VA( ( _L8("StartTransportL error, error code = %d"), err) );
+                    delete iTransport;
+                    iTransport = NULL;
+                    User::Leave(err);
+                    }
+                iTransportUid = aTransport;       
+             
+                iTransportCount++;
+                }
+
+            }
         }
     else
         {
-
+        __FLOG(_L8("begin start transport."));
         iTransport = CMTPTransportPlugin::NewL(aTransport, aParameter);
 
         TRAPD(err, iTransport->StartL(*this));
-		if (err != KErrNone)
-			{
-			__FLOG_VA( ( _L8("StartTransportL error, error code = %d"), err) );
-			delete iTransport;
-			iTransport = NULL;
-			User::Leave(err);
-			}
+        if (err != KErrNone)
+            {
+            __FLOG_VA( ( _L8("StartTransportL error, error code = %d"), err) );
+            delete iTransport;
+            iTransport = NULL;
+            User::Leave(err);
+            }
         iTransportUid = aTransport;       
-		
+        
         iTransportCount++;
-        UnsuspendTransport( iTransportUid );
+        
+        if(iTransportUid.iUid != bluetoothUid)
+            {
+            UnsuspendTransport( iTransportUid );
+            }
+        else 
+            {
+            //Suspend BT transport to handle switching with Mass Storage 
+            SuspendTransportL( iTransportUid);
+            }
         }
 		
 	__FLOG(_L8("StartTransportL - Exit"));
@@ -181,6 +270,15 @@
     {
 	__FLOG(_L8("StopTransport - Entry"));
 	
+
+    __FLOG_1(_L8("aTransport is 0x%X"), aTransport.iUid);
+    __FLOG_1(_L8("iTransportUid is 0x%X"), aTransport.iUid);
+
+    if ( aByBearer )
+        {
+        UnsuspendTransport( aTransport );
+        }
+    
     if ( ( iTransport ) && ( aTransport == iTransportUid ) )
         {
         if ( !aByBearer )
@@ -192,14 +290,8 @@
         iTransport = NULL;
         iTransportUid = KNullUid;
         iTransportCount--;
-
-
         }
-    if ( aByBearer )
-        {
-        UnsuspendTransport( aTransport );
-        }
-		
+    
 	__FLOG(_L8("StopTransport - Exit"));
     }
 
@@ -224,10 +316,28 @@
 */
 EXPORT_C TInt CMTPConnectionMgr::TransportCount() const
     {
-	return iTransportCount;
+    return iTransportCount;
     }
 
-void CMTPConnectionMgr::ConnectionClosed(MMTPTransportConnection& aTransportConnection)
+/*
+Record the remote device bluetooth address when connection setup.
+*/
+EXPORT_C void CMTPConnectionMgr::SetBTResumeParameter(const TBTDevAddr& aBTAddr, const TUint16& aPSMPort)
+    {
+    TInt64 addr(0);
+    TUint8 i(0);
+    addr += aBTAddr[i++];
+    for(; i<KBTDevAddrSize; ++i)
+        {
+        addr <<= 8;
+        addr += aBTAddr[i];
+        }
+    
+    iRemoteDevice.iDeviceAddr = addr;
+    iRemoteDevice.iDeviceServicePort = aPSMPort;
+    }
+
+TBool CMTPConnectionMgr::ConnectionClosed(MMTPTransportConnection& aTransportConnection)
     {
     __FLOG(_L8("ConnectionClosed - Entry"));
     
@@ -236,11 +346,9 @@
     __ASSERT_DEBUG((idx != KErrNotFound), User::Invariant());
     
     CMTPConnection* connection(iConnections[idx]);
-    connection->ConnectionSuspended();
-    
-    ResumeSuspendedTransport();
     
     __FLOG(_L8("ConnectionClosed - Exit"));
+    return connection->ConnectionSuspended();
     }
     
 void CMTPConnectionMgr::ConnectionOpenedL(MMTPTransportConnection& aTransportConnection)
@@ -267,26 +375,6 @@
     __FLOG(_L8("ConnectionOpenedL - Exit"));
     }
 
-TBool CMTPConnectionMgr::DeleteConnection(TUint aConnectionId)
-    {
-    __FLOG(_L8("DeleteConnection - Entry"));
-    
-    TBool ret = EFalse;    
-    TInt idx = ConnectionFind(aConnectionId);
-    
-    if (idx != KErrNotFound)
-        {
-        CMTPConnection* connection(iConnections[idx]);
-        iConnections.Remove(idx);
-        delete connection;
-        ret = ETrue;
-        }
-    
-    __FLOG(_L8("DeleteConnection - Entry"));
-    
-    return ret;
-    }
-
 EXPORT_C TUid CMTPConnectionMgr::ClientSId()
 	{
 	return iSecureId;
@@ -299,7 +387,9 @@
     iShutdownConnectionIdx(KErrNotFound),
 	iTransportUid(KNullUid)
     {
-    __FLOG_OPEN(KMTPSubsystem, KComponent);    
+    __FLOG_OPEN(KMTPSubsystem, KComponent);
+    iRemoteDevice.iDeviceAddr = 0;
+    iRemoteDevice.iDeviceServicePort = 0;
     }
 
 /**
@@ -378,9 +468,14 @@
     {
     __FLOG( _L8("+ResumeSuspendedTransport") );
     const TInt count = iSuspendedTransports.Count();
+    __FLOG_1(_L8("The count number is %d"), count);
+    __FLOG_1(_L8("The transportport id is 0x%X"), iTransportUid.iUid);
+    
+    TInt32 bluetoothUid = 0x10286FCB;
+    
     if ( ( count > 0 )
         // If the transport was just switched and suspended, it shouldn't be resumed.
-        && ( iTransportUid != iSuspendedTransports[count-1] ) )
+        && (( iTransportUid != iSuspendedTransports[count-1] ) || iTransportUid.iUid == bluetoothUid))
         {
         __FLOG( _L8("Found suspended transport(s).") );
         if ( !iTransportTrigger )