mtpfws/mtpfw/src/cmtpconnectionmgr.cpp
changeset 17 aabe5387f5ce
parent 0 d0791faffa3f
child 22 a5c0bb5018eb
child 29 3ae5cb0b4c02
equal deleted inserted replaced
0:d0791faffa3f 17:aabe5387f5ce
    11 // Contributors:
    11 // Contributors:
    12 //
    12 //
    13 // Description:
    13 // Description:
    14 //
    14 //
    15 
    15 
       
    16 #include <usbman.h>
       
    17 #include <usbstates.h>
    16 #include "cmtpconnectionmgr.h"
    18 #include "cmtpconnectionmgr.h"
    17 
    19 
    18 #include "cmtpconnection.h"
    20 #include "cmtpconnection.h"
    19 #include "cmtptransportplugin.h"
    21 #include "cmtptransportplugin.h"
    20 #include "mmtptransportconnection.h"
    22 #include "mmtptransportconnection.h"
    91 EXPORT_C TUid CMTPConnectionMgr::TransportUid()
    93 EXPORT_C TUid CMTPConnectionMgr::TransportUid()
    92     {
    94     {
    93     return iTransportUid;
    95     return iTransportUid;
    94     }
    96     }
    95 
    97 
       
    98 void CMTPConnectionMgr::ConnectionCloseComplete(const TUint& /*aConnUid*/)
       
    99     {
       
   100     __FLOG(_L8("ConnectionCloseComplete - Entry"));
       
   101     ResumeSuspendedTransport();
       
   102     __FLOG(_L8("ConnectionCloseComplete - exit"));
       
   103     }
       
   104 
    96 EXPORT_C void CMTPConnectionMgr::StartTransportL(TUid aTransport)
   105 EXPORT_C void CMTPConnectionMgr::StartTransportL(TUid aTransport)
    97     {
   106     {
    98     StartTransportL( aTransport, NULL );
   107     
       
   108     TInt32 bluetoothUid = 0x10286FCB;
       
   109     
       
   110     RUsb usb;
       
   111     User::LeaveIfError(usb.Connect());
       
   112     TInt usbMode;
       
   113     TUsbServiceState usbStat;
       
   114     TInt err = usb.GetCurrentPersonalityId(usbMode);
       
   115     __FLOG_1(_L8("The return value of GetCurrentPersonalityId is %d"), err);
       
   116     
       
   117     err = usb.GetServiceState(usbStat);
       
   118     __FLOG_1(_L8("The return value of GetServiceState is %d"), err);        
       
   119     
       
   120     usb.Close();
       
   121     
       
   122     __FLOG_1(_L8("The current usb mode is %d"), usbMode);
       
   123     __FLOG_1(_L8("The current usb service state is %d"), usbStat);
       
   124 
       
   125     TInt massStorageMode = 0x02;
       
   126     
       
   127     if(usbMode == massStorageMode && usbStat != EUsbServiceIdle)
       
   128         {
       
   129         __FLOG(_L8("StartTransportL without parameter!"));
       
   130         StartTransportL( aTransport, NULL );
       
   131         return;
       
   132         }
       
   133     
       
   134     
       
   135     
       
   136     //When USB plug out, BT will start Master mode to reconnect remote device. Else BT will start slave mode to listen connection.
       
   137     if(aTransport.iUid == bluetoothUid && iRemoteDevice.iDeviceAddr != 0 && aTransport != iTransportUid)
       
   138         {
       
   139         __FLOG(_L8("StartTransportL with parameter!"));
       
   140         TMTPBTRemoteDeviceBuf tmpdata(iRemoteDevice);
       
   141         StartTransportL( aTransport, &tmpdata );
       
   142         iRemoteDevice.iDeviceAddr = 0;
       
   143         iRemoteDevice.iDeviceServicePort = 0;
       
   144         }
       
   145     else
       
   146         {
       
   147         __FLOG(_L8("StartTransportL without parameter!"));
       
   148         StartTransportL( aTransport, NULL );
       
   149         }
       
   150     
    99     }
   151     }
   100 
   152 
   101 /**
   153 /**
   102 Loads and starts up the MTP transport plug-in with the specified 
   154 Loads and starts up the MTP transport plug-in with the specified 
   103 CMTPTransportPlugin interface implementation UID. Only one MTP transport 
   155 CMTPTransportPlugin interface implementation UID. Only one MTP transport 
   108 @leave One of the system wide error codes, if a processing failure occurs.
   160 @leave One of the system wide error codes, if a processing failure occurs.
   109 @see StopTransport
   161 @see StopTransport
   110 */
   162 */
   111 EXPORT_C void CMTPConnectionMgr::StartTransportL(TUid aTransport, const TAny* aParameter)
   163 EXPORT_C void CMTPConnectionMgr::StartTransportL(TUid aTransport, const TAny* aParameter)
   112     {
   164     {
   113 	__FLOG(_L8("StartTransportL - Entry"));
   165     __FLOG(_L8("StartTransportL - Entry"));
   114 	
   166     
       
   167     TInt32 bluetoothUid = 0x10286FCB;
       
   168     
   115     if (iTransport)
   169     if (iTransport)
   116         {
   170         {
       
   171         __FLOG(_L8("The transport is not none."));
   117         if (aTransport != iTransportUid)
   172         if (aTransport != iTransportUid)
   118             {
   173             {
   119             // Multiple transports not currently supported.
   174             // Multiple transports not currently supported.
       
   175             __FLOG(_L8("Multiple transports are not supported now!"));
   120             User::Leave(KErrNotSupported);
   176             User::Leave(KErrNotSupported);
   121             }
   177             }
       
   178         else
       
   179             {
       
   180             __FLOG_1(_L8("Relaunch the transport 0x%X"), iTransportUid.iUid);
       
   181             if(aTransport.iUid == bluetoothUid)
       
   182                 {
       
   183                 iTransport->Stop(*this);
       
   184                 delete iTransport;
       
   185                 
       
   186                 iTransport = CMTPTransportPlugin::NewL(aTransport, aParameter);
       
   187                 
       
   188                 TRAPD(err, iTransport->StartL(*this));
       
   189                 if (err != KErrNone)
       
   190                     {
       
   191                     __FLOG_VA( ( _L8("StartTransportL error, error code = %d"), err) );
       
   192                     delete iTransport;
       
   193                     iTransport = NULL;
       
   194                     User::Leave(err);
       
   195                     }
       
   196                 iTransportUid = aTransport;       
       
   197              
       
   198                 iTransportCount++;
       
   199                 }
       
   200 
       
   201             }
   122         }
   202         }
   123     else
   203     else
   124         {
   204         {
   125 
   205         __FLOG(_L8("begin start transport."));
   126         iTransport = CMTPTransportPlugin::NewL(aTransport, aParameter);
   206         iTransport = CMTPTransportPlugin::NewL(aTransport, aParameter);
   127 
   207 
   128         TRAPD(err, iTransport->StartL(*this));
   208         TRAPD(err, iTransport->StartL(*this));
   129 		if (err != KErrNone)
   209         if (err != KErrNone)
   130 			{
   210             {
   131 			__FLOG_VA( ( _L8("StartTransportL error, error code = %d"), err) );
   211             __FLOG_VA( ( _L8("StartTransportL error, error code = %d"), err) );
   132 			delete iTransport;
   212             delete iTransport;
   133 			iTransport = NULL;
   213             iTransport = NULL;
   134 			User::Leave(err);
   214             User::Leave(err);
   135 			}
   215             }
   136         iTransportUid = aTransport;       
   216         iTransportUid = aTransport;       
   137 		
   217         
   138         iTransportCount++;
   218         iTransportCount++;
   139         UnsuspendTransport( iTransportUid );
   219         
       
   220         if(iTransportUid.iUid != bluetoothUid)
       
   221             {
       
   222             UnsuspendTransport( iTransportUid );
       
   223             }
       
   224         else 
       
   225             {
       
   226             //Suspend BT transport to handle switching with Mass Storage 
       
   227             SuspendTransportL( iTransportUid);
       
   228             }
   140         }
   229         }
   141 		
   230 		
   142 	__FLOG(_L8("StartTransportL - Exit"));
   231 	__FLOG(_L8("StartTransportL - Exit"));
   143     }
   232     }
   144 
   233 
   179 */
   268 */
   180 EXPORT_C void CMTPConnectionMgr::StopTransport( TUid aTransport, TBool aByBearer )
   269 EXPORT_C void CMTPConnectionMgr::StopTransport( TUid aTransport, TBool aByBearer )
   181     {
   270     {
   182 	__FLOG(_L8("StopTransport - Entry"));
   271 	__FLOG(_L8("StopTransport - Entry"));
   183 	
   272 	
       
   273 
       
   274     __FLOG_1(_L8("aTransport is 0x%X"), aTransport.iUid);
       
   275     __FLOG_1(_L8("iTransportUid is 0x%X"), aTransport.iUid);
       
   276 
       
   277     if ( aByBearer )
       
   278         {
       
   279         UnsuspendTransport( aTransport );
       
   280         }
       
   281     
   184     if ( ( iTransport ) && ( aTransport == iTransportUid ) )
   282     if ( ( iTransport ) && ( aTransport == iTransportUid ) )
   185         {
   283         {
   186         if ( !aByBearer )
   284         if ( !aByBearer )
   187             {
   285             {
   188             TRAP_IGNORE( SuspendTransportL( aTransport ) );
   286             TRAP_IGNORE( SuspendTransportL( aTransport ) );
   190         iTransport->Stop(*this);
   288         iTransport->Stop(*this);
   191         delete iTransport;
   289         delete iTransport;
   192         iTransport = NULL;
   290         iTransport = NULL;
   193         iTransportUid = KNullUid;
   291         iTransportUid = KNullUid;
   194         iTransportCount--;
   292         iTransportCount--;
   195 
   293         }
   196 
   294     
   197         }
       
   198     if ( aByBearer )
       
   199         {
       
   200         UnsuspendTransport( aTransport );
       
   201         }
       
   202 		
       
   203 	__FLOG(_L8("StopTransport - Exit"));
   295 	__FLOG(_L8("StopTransport - Exit"));
   204     }
   296     }
   205 
   297 
   206 /**
   298 /**
   207 Shuts down and unloads all active MTP transport plug-ins.
   299 Shuts down and unloads all active MTP transport plug-ins.
   222 Returns the number of active Transports.
   314 Returns the number of active Transports.
   223 @return Number of active transports
   315 @return Number of active transports
   224 */
   316 */
   225 EXPORT_C TInt CMTPConnectionMgr::TransportCount() const
   317 EXPORT_C TInt CMTPConnectionMgr::TransportCount() const
   226     {
   318     {
   227 	return iTransportCount;
   319     return iTransportCount;
   228     }
   320     }
   229 
   321 
   230 void CMTPConnectionMgr::ConnectionClosed(MMTPTransportConnection& aTransportConnection)
   322 /*
       
   323 Record the remote device bluetooth address when connection setup.
       
   324 */
       
   325 EXPORT_C void CMTPConnectionMgr::SetBTResumeParameter(const TBTDevAddr& aBTAddr, const TUint16& aPSMPort)
       
   326     {
       
   327     TInt64 addr(0);
       
   328     TUint8 i(0);
       
   329     addr += aBTAddr[i++];
       
   330     for(; i<KBTDevAddrSize; ++i)
       
   331         {
       
   332         addr <<= 8;
       
   333         addr += aBTAddr[i];
       
   334         }
       
   335     
       
   336     iRemoteDevice.iDeviceAddr = addr;
       
   337     iRemoteDevice.iDeviceServicePort = aPSMPort;
       
   338     }
       
   339 
       
   340 TBool CMTPConnectionMgr::ConnectionClosed(MMTPTransportConnection& aTransportConnection)
   231     {
   341     {
   232     __FLOG(_L8("ConnectionClosed - Entry"));
   342     __FLOG(_L8("ConnectionClosed - Entry"));
   233     
   343     
   234     TInt idx(ConnectionFind(aTransportConnection.BoundProtocolLayer().ConnectionId()));
   344     TInt idx(ConnectionFind(aTransportConnection.BoundProtocolLayer().ConnectionId()));
   235     __FLOG_VA((_L8("idx is %d "), idx));
   345     __FLOG_VA((_L8("idx is %d "), idx));
   236     __ASSERT_DEBUG((idx != KErrNotFound), User::Invariant());
   346     __ASSERT_DEBUG((idx != KErrNotFound), User::Invariant());
   237     
   347     
   238     CMTPConnection* connection(iConnections[idx]);
   348     CMTPConnection* connection(iConnections[idx]);
   239     connection->ConnectionSuspended();
       
   240     
       
   241     ResumeSuspendedTransport();
       
   242     
   349     
   243     __FLOG(_L8("ConnectionClosed - Exit"));
   350     __FLOG(_L8("ConnectionClosed - Exit"));
       
   351     return connection->ConnectionSuspended();
   244     }
   352     }
   245     
   353     
   246 void CMTPConnectionMgr::ConnectionOpenedL(MMTPTransportConnection& aTransportConnection)
   354 void CMTPConnectionMgr::ConnectionOpenedL(MMTPTransportConnection& aTransportConnection)
   247     {   
   355     {   
   248     __FLOG(_L8("ConnectionOpenedL - Entry"));
   356     __FLOG(_L8("ConnectionOpenedL - Entry"));
   265     connection->ConnectionResumedL(aTransportConnection);
   373     connection->ConnectionResumedL(aTransportConnection);
   266     
   374     
   267     __FLOG(_L8("ConnectionOpenedL - Exit"));
   375     __FLOG(_L8("ConnectionOpenedL - Exit"));
   268     }
   376     }
   269 
   377 
   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()
   378 EXPORT_C TUid CMTPConnectionMgr::ClientSId()
   291 	{
   379 	{
   292 	return iSecureId;
   380 	return iSecureId;
   293 	}
   381 	}
   294 /**
   382 /**
   297 CMTPConnectionMgr::CMTPConnectionMgr() :
   385 CMTPConnectionMgr::CMTPConnectionMgr() :
   298     iConnectionOrder(ConnectionOrderCompare),
   386     iConnectionOrder(ConnectionOrderCompare),
   299     iShutdownConnectionIdx(KErrNotFound),
   387     iShutdownConnectionIdx(KErrNotFound),
   300 	iTransportUid(KNullUid)
   388 	iTransportUid(KNullUid)
   301     {
   389     {
   302     __FLOG_OPEN(KMTPSubsystem, KComponent);    
   390     __FLOG_OPEN(KMTPSubsystem, KComponent);
       
   391     iRemoteDevice.iDeviceAddr = 0;
       
   392     iRemoteDevice.iDeviceServicePort = 0;
   303     }
   393     }
   304 
   394 
   305 /**
   395 /**
   306 Provides the connections table index of the connection with the specified 
   396 Provides the connections table index of the connection with the specified 
   307 connection identifier.
   397 connection identifier.
   376 */
   466 */
   377 void CMTPConnectionMgr::ResumeSuspendedTransport()
   467 void CMTPConnectionMgr::ResumeSuspendedTransport()
   378     {
   468     {
   379     __FLOG( _L8("+ResumeSuspendedTransport") );
   469     __FLOG( _L8("+ResumeSuspendedTransport") );
   380     const TInt count = iSuspendedTransports.Count();
   470     const TInt count = iSuspendedTransports.Count();
       
   471     __FLOG_1(_L8("The count number is %d"), count);
       
   472     __FLOG_1(_L8("The transportport id is 0x%X"), iTransportUid.iUid);
       
   473     
       
   474     TInt32 bluetoothUid = 0x10286FCB;
       
   475     
   381     if ( ( count > 0 )
   476     if ( ( count > 0 )
   382         // If the transport was just switched and suspended, it shouldn't be resumed.
   477         // If the transport was just switched and suspended, it shouldn't be resumed.
   383         && ( iTransportUid != iSuspendedTransports[count-1] ) )
   478         && (( iTransportUid != iSuspendedTransports[count-1] ) || iTransportUid.iUid == bluetoothUid))
   384         {
   479         {
   385         __FLOG( _L8("Found suspended transport(s).") );
   480         __FLOG( _L8("Found suspended transport(s).") );
   386         if ( !iTransportTrigger )
   481         if ( !iTransportTrigger )
   387             {
   482             {
   388             iTransportTrigger = new( ELeave ) CAsyncCallBack( CActive::EPriorityStandard );
   483             iTransportTrigger = new( ELeave ) CAsyncCallBack( CActive::EPriorityStandard );