userlibandfileserver/fileserver/shostmassstorage/server/controller/cusbhostmsdevicethread.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 // Implements a Session of a Symbian OS server for the RUsbMassStorage API
       
    15 //
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalTechnology
       
    21 */
       
    22 
       
    23 #include <e32base.h>
       
    24 #include <e32base_private.h>
       
    25 #include "msctypes.h"
       
    26 #include "mscutils.h"
       
    27 #include "shared.h"
       
    28 #include "msgservice.h"
       
    29 #include "cusbhostmslogicalunit.h"
       
    30 #include "cusbhostmsdevice.h"
       
    31 #include "cusbhostmsserver.h"
       
    32 #include "msdebug.h"
       
    33 #include "cusbhostmsdevicethread.h"
       
    34 #include "cusbhostmssession.h"
       
    35 #include "debug.h"
       
    36 
       
    37 /**
       
    38 Constructor
       
    39 */
       
    40 TDeviceHandler::TDeviceHandler(CUsbHostMsDevice& aDevice)
       
    41 :   iDevice(aDevice)
       
    42     {
       
    43     __MSFNLOG
       
    44     }
       
    45 
       
    46 
       
    47 /**
       
    48    Services messages directed to a device. Messages associated with a single
       
    49    Logical Unit contained by the device are forwarded to the Logical Unit
       
    50    Handler.
       
    51 
       
    52    @param aMessage
       
    53  */
       
    54 void TDeviceHandler::HandleMessageL(const RMessage2& aMessage)
       
    55     {
       
    56     __MSFNLOG
       
    57     TLun lun = iDevice.GetAndSetLunL(aMessage);
       
    58 
       
    59 	switch (aMessage.Function())
       
    60 		{
       
    61 	case EUsbHostMsSuspendLun:
       
    62         iDevice.SuspendLunL(lun);
       
    63 		break;
       
    64     case EUsbHostMsUnRegisterLun:
       
    65         iDevice.RemoveLunL(lun);
       
    66         break;
       
    67     default:
       
    68         // Try Logical Unit Handler
       
    69         CUsbHostMsLogicalUnit& lu = iDevice.GetLuL(lun);
       
    70         TLogicalUnitHandler luHandler(lu);
       
    71         luHandler.HandleMessageL(aMessage);
       
    72 		break;
       
    73 		}
       
    74     }
       
    75 
       
    76 
       
    77 /**
       
    78    Constructor
       
    79 
       
    80    @param aLu Reference to the logical unit object
       
    81  */
       
    82 TLogicalUnitHandler::TLogicalUnitHandler(CUsbHostMsLogicalUnit& aLu)
       
    83 :   iLu(aLu)
       
    84     {
       
    85     __MSFNLOG
       
    86     }
       
    87 
       
    88 
       
    89 /**
       
    90    Services messages directed to a specific Logical Unit. Unrecognized messages
       
    91    will cause a PANIC.
       
    92 
       
    93    @param aMessage
       
    94  */
       
    95 void TLogicalUnitHandler::HandleMessageL(const RMessage2& aMessage)
       
    96 	{
       
    97     __MSFNLOG
       
    98 	switch (aMessage.Function())
       
    99 		{
       
   100 	case EUsbHostMsNotifyChange:
       
   101         iLu.NotifyChange(aMessage);
       
   102 		break;
       
   103 	case EUsbHostMsCancelChangeNotifier:
       
   104         iLu.CancelChangeNotifierL();
       
   105 		break;
       
   106 	case EUsbHostMsForceRemount:
       
   107         iLu.ForceCompleteNotifyChangeL();
       
   108 		break;
       
   109 	case EUsbHostMsRead:
       
   110         iLu.ReadL(aMessage);
       
   111 		break;
       
   112 	case EUsbHostMsWrite:
       
   113         iLu.WriteL(aMessage);
       
   114 		break;
       
   115 	case EUsbHostMsErase:
       
   116         iLu.EraseL(aMessage);
       
   117 		break;
       
   118 	case EUsbHostMsCapacity:
       
   119         iLu.CapsL(aMessage);
       
   120 		break;
       
   121 	default:
       
   122 		aMessage.Panic(KUsbHostMsSrvPncCat, EUsbMsPanicIllegalIPC);
       
   123 		break;
       
   124 		}
       
   125 	}
       
   126 
       
   127 
       
   128 void CUsbHostMsDeviceThread::DoStartServerL(TAny* aPtr)
       
   129     {
       
   130     __MSFNSLOG
       
   131     CActiveScheduler* s = new(ELeave) CActiveScheduler;
       
   132     CActiveScheduler::Install(s);
       
   133 
       
   134 	CUsbHostMsDeviceThread* iThread = (CUsbHostMsDeviceThread*)aPtr;
       
   135 	CActiveScheduler::Add(iThread);
       
   136 
       
   137 	iThread->iStatus = KRequestPending;
       
   138 	iThread->SetActive();
       
   139 
       
   140 	RThread::Rendezvous(KErrNone);
       
   141 
       
   142 	//
       
   143     // Ready to run
       
   144     CActiveScheduler::Start();
       
   145 
       
   146     //
       
   147     // Cleanup the scheduler
       
   148 	delete s;
       
   149     }
       
   150 
       
   151 
       
   152 TInt CUsbHostMsDeviceThread::Entry(TAny* aPtr)
       
   153 	{
       
   154     __MSFNSLOG
       
   155 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   156     if (!cleanup)
       
   157         {
       
   158         return KErrNoMemory;
       
   159         }
       
   160 
       
   161     TRAPD(error, DoStartServerL(aPtr));
       
   162     delete cleanup;
       
   163     return error;
       
   164 	}
       
   165 
       
   166 
       
   167 void  CUsbHostMsDeviceThread::RunL()
       
   168 	{
       
   169     __MSFNLOG
       
   170 	Lock();
       
   171 	if (!iUsbHostMsDevice || iUsbHostMsDevice->IsActive())
       
   172         {
       
   173         // Note: In the case of suspended/resuming state we do not want to get
       
   174         // woken by the session msg handler repeatedly
       
   175         iIsSignalled = EFalse;
       
   176         }
       
   177 
       
   178 	Unlock();
       
   179 
       
   180 	RMessage2	msg;
       
   181 	TBool handleMsg = EFalse;
       
   182 
       
   183 	for(;;)
       
   184 		{
       
   185 		Lock();
       
   186 		if ((iQueueIndex != iDequeueIndex) || iQueueFull)
       
   187 			{
       
   188 			if (iUsbHostMsDevice && iUsbHostMsDevice->IsSuspended())
       
   189 				{
       
   190 				Unlock();
       
   191 				SetActive();
       
   192 				iUsbHostMsDevice->ResumeL(iStatus);
       
   193 				return;
       
   194 				}
       
   195 
       
   196 			msg = iRMessage2[iDequeueIndex];
       
   197 			handleMsg = ETrue;
       
   198 			iDequeueIndex++;
       
   199 
       
   200 			if(iDequeueIndex >= KMaxNumMessage)
       
   201 				{
       
   202 				iDequeueIndex = 0;
       
   203 				}
       
   204 			if(iQueueFull)
       
   205 				{
       
   206 				iQueueFull = EFalse;
       
   207 				}
       
   208 			}
       
   209 		Unlock();
       
   210 		if (handleMsg)
       
   211 			{
       
   212 			HandleMessage(msg);
       
   213 			handleMsg = EFalse;
       
   214 			}
       
   215 		else
       
   216 			{
       
   217 			break;
       
   218 			}
       
   219 		}
       
   220 	iStatus = KRequestPending;
       
   221 	SetActive();
       
   222 	}
       
   223 
       
   224 
       
   225 TInt CUsbHostMsDeviceThread::QueueMsg(const RMessage2& aMsg)
       
   226 	{
       
   227     __MSFNLOG
       
   228 	if (iQueueFull)
       
   229 		{
       
   230 		return KErrOverflow;
       
   231 		}
       
   232 
       
   233     Lock();
       
   234 	iRMessage2[iQueueIndex] = aMsg;
       
   235 	iQueueIndex++;
       
   236 
       
   237 	if (iQueueIndex >= KMaxNumMessage)
       
   238 		{
       
   239 		iQueueIndex = 0;
       
   240 		}
       
   241 
       
   242 	if (iQueueIndex == iDequeueIndex)
       
   243 		{
       
   244 		iQueueFull = ETrue;
       
   245 		}
       
   246 	Unlock();
       
   247 	return KErrNone;
       
   248 	}
       
   249 
       
   250 
       
   251 void CUsbHostMsDeviceThread::Lock()
       
   252 	{
       
   253     __MSFNLOG
       
   254 	iMutex.Wait();
       
   255 	}
       
   256 
       
   257 
       
   258 void CUsbHostMsDeviceThread::Unlock()
       
   259 	{
       
   260     __MSFNLOG
       
   261 	iMutex.Signal();
       
   262 	}
       
   263 
       
   264 
       
   265 CUsbHostMsDeviceThread::CUsbHostMsDeviceThread(TUint token)
       
   266 :	CActive(EPriorityStandard),
       
   267 	iIsSignalled(EFalse),
       
   268     iQueueFull(EFalse)
       
   269 	{
       
   270     TName nameBuf;
       
   271     nameBuf.Format(_L("Host Ms ThreadMutex%d"), token);
       
   272 	iMutex.CreateGlobal(nameBuf,EOwnerProcess);
       
   273 	}
       
   274 
       
   275 CUsbHostMsDeviceThread::~CUsbHostMsDeviceThread()
       
   276 	{
       
   277 	iMutex.Close();
       
   278 	}
       
   279 
       
   280 CUsbHostMsDeviceThread* CUsbHostMsDeviceThread::NewL(TUint aToken)
       
   281 	{
       
   282 	CUsbHostMsDeviceThread* r = new (ELeave) CUsbHostMsDeviceThread(aToken);
       
   283 	CleanupStack::PushL(r);
       
   284 	CleanupStack::Pop();
       
   285 	return r;
       
   286 	}
       
   287 
       
   288 
       
   289 /**
       
   290  Handles the request (in the form of a message) received from the client
       
   291 @param	aMessage	The received message
       
   292  */
       
   293 void CUsbHostMsDeviceThread::HandleMessage(const RMessage2& aMessage)
       
   294 	{
       
   295     __MSFNLOG
       
   296 	TInt ret = KErrNotReady;
       
   297     __HOSTPRINT2(_L(">> HOST DispatchMessageL Function=%d %d"), aMessage.Function(), aMessage.Int3());
       
   298 	switch (aMessage.Function())
       
   299 		{
       
   300 	case EUsbHostMsRegisterInterface:
       
   301 		TRAP(ret, RegisterInterfaceL(aMessage));
       
   302 		break;
       
   303     case EUsbHostMsInitialiseInterface:
       
   304 		TRAP(ret, InitialiseInterfaceL(aMessage));
       
   305         // CUsbInterfaceHandler::GetMaxLun() completes asynchronously
       
   306         if (ret)
       
   307             {
       
   308             // Error condition needs to be completed
       
   309             break;
       
   310             }
       
   311 		return;
       
   312 	case EUsbHostMsUnRegisterInterface:
       
   313 		TRAP(ret, UnRegisterInterfaceL(aMessage));
       
   314 		break;
       
   315 	case EUsbHostMsRegisterLun:
       
   316 		TRAP(ret, RegisterLogicalUnitL(aMessage));
       
   317 		break;
       
   318 	case EUsbHostMsGetNumLun:
       
   319 		TRAP(ret, GetNumLunL(aMessage));
       
   320 		break;
       
   321 	case EUsbHostMsShutdown:
       
   322 		ret = Shutdown();
       
   323 		break;
       
   324     default:
       
   325         // Try Device Handler and Logical Unit Handler
       
   326         TDeviceHandler deviceHandler(*iUsbHostMsDevice);
       
   327         TRAP(ret, deviceHandler.HandleMessageL(aMessage));
       
   328 		break;
       
   329 		}
       
   330     __HOSTPRINT1(_L(">> HOST returning %d"), ret);
       
   331     if (aMessage.Function() != EUsbHostMsNotifyChange)
       
   332         {
       
   333         aMessage.Complete(ret);
       
   334         }
       
   335 	}
       
   336 
       
   337 
       
   338 /**
       
   339 Client request to shut down the server
       
   340 
       
   341 return KErrNone
       
   342 */
       
   343 TInt CUsbHostMsDeviceThread::Shutdown()
       
   344     {
       
   345     __MSFNLOG
       
   346     CActiveScheduler::Stop();
       
   347 	return KErrNone;
       
   348 	}
       
   349 
       
   350 void CUsbHostMsDeviceThread::GetNumLunL(const RMessage2& aMessage)
       
   351 	{
       
   352 	__MSFNLOG
       
   353     if (!iUsbHostMsDevice)
       
   354         {
       
   355         User::Leave(KErrNotReady);
       
   356         }
       
   357 
       
   358 	TUint32 maxLun = iUsbHostMsDevice->GetMaxLun() + 1;
       
   359 	TPtrC8 pLun((TUint8*)&maxLun,sizeof(TUint32));
       
   360 	aMessage.WriteL(0,pLun);
       
   361 	}
       
   362 
       
   363 
       
   364 void CUsbHostMsDeviceThread::RegisterInterfaceL(const RMessage2& aMessage)
       
   365 	{
       
   366 	__MSFNLOG
       
   367 
       
   368 	THostMassStorageConfig msDeviceConfig;
       
   369 	TPtr8 ptr((TUint8*)&msDeviceConfig,sizeof(THostMassStorageConfig));
       
   370 	aMessage.ReadL(0, ptr);
       
   371 
       
   372     __HOSTPRINT1(_L("RegisterInterfaceL Token=%d "), msDeviceConfig.iInterfaceToken);
       
   373 
       
   374 	iUsbHostMsDevice = CUsbHostMsDevice::NewL(msDeviceConfig);
       
   375 	}
       
   376 
       
   377 
       
   378 void CUsbHostMsDeviceThread::InitialiseInterfaceL(const RMessage2& aMessage)
       
   379 	{
       
   380 	__MSFNLOG
       
   381     if (!iUsbHostMsDevice)
       
   382         {
       
   383         User::Leave(KErrNotReady);
       
   384         }
       
   385 
       
   386 	TRAPD(err, iUsbHostMsDevice->InitialiseL(aMessage));
       
   387     if (err != KErrNone)
       
   388         {
       
   389         delete iUsbHostMsDevice;
       
   390 		iUsbHostMsDevice = NULL;
       
   391         User::Leave(err);
       
   392         }
       
   393 	}
       
   394 
       
   395 
       
   396 void CUsbHostMsDeviceThread::UnRegisterInterfaceL(const RMessage2& aMessage)
       
   397     {
       
   398     __MSFNLOG
       
   399     if (!iUsbHostMsDevice)
       
   400         {
       
   401         User::Leave(KErrNotReady);
       
   402         }
       
   403 
       
   404 	TRAPD(err, iUsbHostMsDevice->UnInitialiseL());
       
   405 	delete iUsbHostMsDevice;
       
   406 	iUsbHostMsDevice = NULL;
       
   407     User::LeaveIfError(err);
       
   408     }
       
   409 
       
   410 
       
   411 void CUsbHostMsDeviceThread::RegisterLogicalUnitL(const RMessage2& aMessage)
       
   412 	{
       
   413     if (!iUsbHostMsDevice)
       
   414         {
       
   415         User::Leave(KErrNotReady);
       
   416         }
       
   417 
       
   418 	TUint32 iLunId = aMessage.Int0() + 1; // Subssessions need a positive value to store in the handles. We represent Luns as LunId+1
       
   419 	TPtrC8 pLun((TUint8*)&iLunId, sizeof(TUint32));
       
   420 	aMessage.WriteL(3, pLun);
       
   421 	iLunId -= 1;	// We represent LunId in MSC from 0 to MaxLun-1 as represented in BOT
       
   422 	iUsbHostMsDevice->AddLunL(iLunId);
       
   423 	iUsbHostMsDevice->InitLunL(iLunId);
       
   424 	}
       
   425