userlibandfileserver/fileserver/shostmassstorage/server/src/cusbhostmssession.cpp
changeset 297 b2826f67641f
parent 271 dc268b18d709
equal deleted inserted replaced
296:94f2adf59133 297:b2826f67641f
    17  @file
    17  @file
    18  @internalTechnology
    18  @internalTechnology
    19 */
    19 */
    20 
    20 
    21 #include <e32base.h>
    21 #include <e32base.h>
       
    22 
       
    23 #include "OstTraceDefinitions.h"
       
    24 #ifdef OST_TRACE_COMPILER_IN_USE
       
    25 #include "cusbhostmssessionTraces.h"
       
    26 #endif
    22 
    27 
    23 #include "msctypes.h"
    28 #include "msctypes.h"
    24 #include "shared.h"
    29 #include "shared.h"
    25 #include "msgservice.h"
    30 #include "msgservice.h"
    26 #include "cusbhostmslogicalunit.h"
    31 #include "cusbhostmslogicalunit.h"
    27 #include "cusbhostmsdevice.h"
    32 #include "cusbhostmsdevice.h"
    28 #include "cusbhostmsserver.h"
    33 #include "cusbhostmsserver.h"
    29 #include "usbmshostpanic.h"
    34 #include "usbmshostpanic.h"
    30 #include "cusbhostmsdevicethread.h"
    35 #include "cusbhostmsdevicethread.h"
    31 #include "cusbhostmssession.h"
    36 #include "cusbhostmssession.h"
    32 #include "msdebug.h"
    37 
    33 #include "debug.h"
       
    34 
    38 
    35 /** Construct a Symbian OS session object.
    39 /** Construct a Symbian OS session object.
    36 
    40 
    37 	param	aServer		Service the session will be a member of
    41     param   aServer     Service the session will be a member of
    38 	param	aMessage	The message from the client.
    42     param   aMessage    The message from the client.
    39 	return	A new CUsbHostMsSession object
    43     return  A new CUsbHostMsSession object
    40  */
    44  */
    41 CUsbHostMsSession* CUsbHostMsSession::NewL(CUsbHostMsServer& aServer)
    45 CUsbHostMsSession* CUsbHostMsSession::NewL(CUsbHostMsServer& aServer)
    42 	{
    46     {
    43     __MSFNSLOG
    47     CUsbHostMsSession* r = new (ELeave) CUsbHostMsSession(aServer);
    44 	CUsbHostMsSession* r = new (ELeave) CUsbHostMsSession(aServer);
    48     CleanupStack::PushL(r);
    45 	CleanupStack::PushL(r);
    49     r->ConstructL();
    46 	r->ConstructL();
    50     CleanupStack::Pop();
    47 	CleanupStack::Pop();
    51     return r;
    48 	return r;
    52     }
    49 	}
       
    50 
    53 
    51 /**
    54 /**
    52  Constructor.
    55  Constructor.
    53 
    56 
    54 	param	aServer	Service the session will be a member of
    57     param   aServer Service the session will be a member of
    55  */
    58  */
    56 CUsbHostMsSession::CUsbHostMsSession(CUsbHostMsServer& aServer)
    59 CUsbHostMsSession::CUsbHostMsSession(CUsbHostMsServer& aServer)
    57 	: iUsbHostMsServer(aServer)
    60     : iUsbHostMsServer(aServer)
    58 	{
    61     {
    59     __MSFNLOG
    62     }
    60 	}
       
    61 
    63 
    62 
    64 
    63 /**
    65 /**
    64  2nd Phase Construction.
    66  2nd Phase Construction.
    65  */
    67  */
    66 void CUsbHostMsSession::ConstructL()
    68 void CUsbHostMsSession::ConstructL()
    67 	{
    69     {
    68     __MSFNLOG
    70     iUsbHostMsServer.IncrementSessionCount();
    69 	iUsbHostMsServer.IncrementSessionCount();
    71     OstTrace1(TRACE_SHOSTMASSSTORAGE_HOST, CUSBHOSTMSSESSION_10,
    70     __HOSTPRINT1(_L("\tiSessionCount: %d\n"), iUsbHostMsServer.SessionCount());
    72               "iSessionCount: %d", iUsbHostMsServer.SessionCount());
    71 	}
    73     }
    72 
    74 
    73 
    75 
    74 /**
    76 /**
    75  Destructor.
    77  Destructor.
    76  */
    78  */
    77 CUsbHostMsSession::~CUsbHostMsSession()
    79 CUsbHostMsSession::~CUsbHostMsSession()
    78 	{
    80     {
    79     __MSFNLOG
    81     iUsbHostMsServer.DecrementSessionCount();
    80 
    82     iThread.Close();
    81 	iUsbHostMsServer.DecrementSessionCount();
    83     OstTrace1(TRACE_SHOSTMASSSTORAGE_HOST, CUSBHOSTMSSESSION_11,
    82 	iThread.Close();
    84               "Closed a session -> iSessionCount: %d",
    83     __HOSTPRINT1(_L("\tClosed a session -> iSessionCount: %d\n"), iUsbHostMsServer.SessionCount());
    85               iUsbHostMsServer.SessionCount());
    84 	}
    86     }
    85 
    87 
    86 /**
    88 /**
    87  Called when a message is received from the client.
    89  Called when a message is received from the client.
    88 
    90 
    89 	param	aMessage	Message received from the client
    91     param   aMessage    Message received from the client
    90  */
    92  */
    91 void CUsbHostMsSession::ServiceL(const RMessage2& aMessage)
    93 void CUsbHostMsSession::ServiceL(const RMessage2& aMessage)
    92 	{
    94     {
    93     __MSFNLOG
    95     DispatchMessageL(aMessage);
    94 	DispatchMessageL(aMessage);
    96     }
    95 	}
       
    96 
    97 
    97 
    98 
    98 /**
    99 /**
    99  Handles the request (in the form of a message) received from the client
   100  Handles the request (in the form of a message) received from the client
   100 
   101 
   101 	param	aMessage	The received message
   102     param   aMessage    The received message
   102  */
   103  */
   103 void CUsbHostMsSession::DispatchMessageL(const RMessage2& aMessage)
   104 void CUsbHostMsSession::DispatchMessageL(const RMessage2& aMessage)
   104 	{
   105     {
   105     __MSFNLOG
   106     TInt r = KErrNone;
   106 	TInt r = KErrNone;
   107     switch (aMessage.Function())
   107 	switch (aMessage.Function())
   108         {
   108 		{
   109     case EUsbHostMsRegisterInterface:
   109 	case EUsbHostMsRegisterInterface:
   110         TRAP(r, CreateDeviceThreadL(aMessage));
   110 		TRAP(r, CreateDeviceThreadL(aMessage));
   111         if(r != KErrNone)
   111 		if(r != KErrNone)
   112             {
   112 			{
   113             aMessage.Complete(r);
   113 			aMessage.Complete(r);
   114             return;
   114 			return;
   115             }
   115 			}
   116         break;
   116 		break;
       
   117     case EUsbHostMsUnRegisterInterface:
   117     case EUsbHostMsUnRegisterInterface:
   118         if(iCleanupInProgress)
   118         if(iCleanupInProgress)
   119             {
   119             {
   120             aMessage.Complete(KErrInUse);
   120             aMessage.Complete(KErrInUse);
   121             return;
   121             return;
   122             }
   122             }
   123         else
   123         else
   124             {
   124             {
   125             iCleanupInProgress = ETrue;
   125             iCleanupInProgress = ETrue;
   126             }
   126             }
   127 		break;
   127         break;
   128 	/* If it is a cleanup then we need to delete the iDeviceThread */
   128     /* If it is a cleanup then we need to delete the iDeviceThread */
   129 	case EUsbHostMsFinalCleanup:	
   129     case EUsbHostMsFinalCleanup:
   130 		delete iDeviceThread;
   130         delete iDeviceThread;
   131 		iDeviceThread = NULL;
   131         iDeviceThread = NULL;
   132 		iThread.Kill(KErrNone);
   132         iThread.Kill(KErrNone);
   133 		aMessage.Complete(KErrNone);
   133         aMessage.Complete(KErrNone);
   134 		return;
   134         return;
   135 	default:
   135     default:
   136 		break;
   136         break;
   137 		}
   137         }
   138 
   138 
   139 	if (iDeviceThread == NULL) 
   139     if (iDeviceThread == NULL)
   140 		{
       
   141 		aMessage.Complete(KErrBadHandle);
       
   142 		return;
       
   143 		}
       
   144 
       
   145     if (iCleanupInProgress && aMessage.Function() != EUsbHostMsUnRegisterInterface) 
       
   146         {
   140         {
   147         aMessage.Complete(KErrBadHandle);
   141         aMessage.Complete(KErrBadHandle);
   148         return;
   142         return;
   149         }
   143         }
   150 
   144 
   151 	r = iDeviceThread->QueueMsg(aMessage);
   145     if (iCleanupInProgress && aMessage.Function() != EUsbHostMsUnRegisterInterface)
   152 	if (r != KErrNone)
   146         {
   153 		{
   147         aMessage.Complete(KErrBadHandle);
   154 		aMessage.Complete(r);
   148         return;
   155 		return;
   149         }
   156 		}
   150 
   157 
   151     r = iDeviceThread->QueueMsg(aMessage);
   158 	if (iClientStatus && *iClientStatus == KRequestPending)
   152     if (r != KErrNone)
   159 		{
   153         {
   160 		__HOSTPRINT(_L("Signaling device thread to handle message"));
   154         aMessage.Complete(r);
   161 		iThread.RequestComplete(iClientStatus, KErrNone);
   155         return;
   162 		}
   156         }
   163 	}
   157 
       
   158     if (iClientStatus && *iClientStatus == KRequestPending)
       
   159         {
       
   160         OstTrace0(TRACE_SHOSTMASSSTORAGE_HOST, CUSBHOSTMSSESSION_20,
       
   161                   "Signaling device thread to handle message");
       
   162         iThread.RequestComplete(iClientStatus, KErrNone);
       
   163         }
       
   164     }
   164 
   165 
   165 
   166 
   166 void CUsbHostMsSession::CreateDeviceThreadL(const RMessage2& aMessage)
   167 void CUsbHostMsSession::CreateDeviceThreadL(const RMessage2& aMessage)
   167 	{
   168     {
   168     __MSFNLOG
   169     THostMassStorageConfig msDeviceConfig;
   169 	THostMassStorageConfig msDeviceConfig;
   170     TPtr8 ptr((TUint8*)&msDeviceConfig,sizeof(THostMassStorageConfig));
   170 	TPtr8 ptr((TUint8*)&msDeviceConfig,sizeof(THostMassStorageConfig));
   171 
   171 
   172     aMessage.ReadL(0, ptr);
   172 	aMessage.ReadL(0, ptr);
   173     OstTrace1(TRACE_SHOSTMASSSTORAGE_HOST, CUSBHOSTMSSESSION_21,
   173 	__HOSTPRINT1(_L("EUsbHostMsRegisterInterface Token=%d "), msDeviceConfig.iInterfaceToken);
   174               "EUsbHostMsRegisterInterface Token=%d ", msDeviceConfig.iInterfaceToken);
   174 
   175 
   175     TBuf<32> nameBuf;
   176     TBuf<32> nameBuf;
   176 	nameBuf.Format(_L("Host Ms Thread%8x"), msDeviceConfig.iInterfaceToken);
   177     nameBuf.Format(_L("Host Ms Thread%8x"), msDeviceConfig.iInterfaceToken);
   177 	iDeviceThread = CUsbHostMsDeviceThread::NewL(*this, msDeviceConfig.iInterfaceToken);
   178     iDeviceThread = CUsbHostMsDeviceThread::NewL(*this, msDeviceConfig.iInterfaceToken);
   178 
   179 
   179 	RHeap* h = (RHeap*)&User::Allocator();
   180     RHeap* h = (RHeap*)&User::Allocator();
   180 	TInt maxsize = h->MaxLength();	// loader heap max size = file server heap max size
   181     TInt maxsize = h->MaxLength();  // loader heap max size = file server heap max size
   181 	const TUint KHeapMinSize = 2048;
   182     const TUint KHeapMinSize = 2048;
   182 
   183 
   183 	TInt r = iThread.Create(nameBuf, CUsbHostMsDeviceThread::Entry, KDefaultStackSize, KHeapMinSize, maxsize, iDeviceThread);
   184     TInt r = iThread.Create(nameBuf, CUsbHostMsDeviceThread::Entry, KDefaultStackSize, KHeapMinSize, maxsize, iDeviceThread);
   184 	if(r != KErrNone)
   185     if(r != KErrNone)
   185 		{
   186         {
   186 		delete iDeviceThread;
   187         delete iDeviceThread;
   187         iDeviceThread = NULL;
   188         iDeviceThread = NULL;
   188 		User::Leave(r);
   189         User::Leave(r);
   189 		}
   190         }
   190 	iThread.SetPriority(EPriorityAbsoluteForeground);
   191     iThread.SetPriority(EPriorityAbsoluteForeground);
   191 	TRequestStatus status;
   192     TRequestStatus status;
   192 	iThread.Rendezvous(status);
   193     iThread.Rendezvous(status);
   193 	iThread.Resume();
   194     iThread.Resume();
   194 	User::WaitForRequest(status);
   195     User::WaitForRequest(status);
   195 	if(status != KErrNone)
   196     if(status != KErrNone)
   196 		{
   197         {
   197 		delete iDeviceThread;
   198         delete iDeviceThread;
   198         iDeviceThread = NULL;
   199         iDeviceThread = NULL;
   199 		iThread.Kill(KErrNone);
   200         iThread.Kill(KErrNone);
   200 		User::Leave(status.Int());
   201         User::Leave(status.Int());
   201 		}
   202         }
   202 	}
   203     }
   203 
   204 
   204 
   205 
   205 
   206 
   206 void CUsbHostMsSession::MessageRequest(TRequestStatus& aStatus)
   207 void CUsbHostMsSession::MessageRequest(TRequestStatus& aStatus)
   207     {
   208     {
   208     __MSFNLOG
       
   209     iClientStatus = &aStatus;
   209     iClientStatus = &aStatus;
   210     *iClientStatus = KRequestPending;
   210     *iClientStatus = KRequestPending;
   211     }
   211     }