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