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