usbmgmt/usbmgr/host/functiondrivers/ms/msmm/server/src/eventhandler.cpp
changeset 0 c9bc50fca66e
child 5 c2db6e29750c
equal deleted inserted replaced
-1:000000000000 0:c9bc50fca66e
       
     1 /*
       
     2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalComponent
       
    21 */
       
    22 
       
    23 #include "eventhandler.h"
       
    24 #include <usb/hostms/srverr.h>
       
    25 #include <usb/usblogger.h>
       
    26 #include "msmmserver.h"
       
    27 #include "msmmengine.h"
       
    28 #include "subcommands.h"
       
    29 #include "msmmnodebase.h"
       
    30 
       
    31 #ifdef __FLOG_ACTIVE
       
    32 _LIT8(KLogComponent, "UsbHostMsmmServer");
       
    33 #endif
       
    34 
       
    35 // Push a sub-command into the queue and transfer the owership 
       
    36 // to the queue
       
    37 void RSubCommandQueue::PushL(TSubCommandBase* aCommand)
       
    38     {
       
    39     LOG_FUNC
       
    40     CleanupStack::PushL(aCommand);
       
    41     iQueue.AppendL(aCommand);
       
    42     CleanupStack::Pop(aCommand);
       
    43     }
       
    44 
       
    45 // Pop the head entity from the queue and destroy it
       
    46 void RSubCommandQueue::Pop()
       
    47     {
       
    48     LOG_FUNC
       
    49     if (iQueue.Count() == 0)
       
    50         {
       
    51         return;
       
    52         }
       
    53     
       
    54     TSubCommandBase* command = iQueue[0];
       
    55     iQueue.Remove(0);
       
    56     delete command;
       
    57     command = NULL;
       
    58     }
       
    59     
       
    60 // Insert a sub-command sequence after head entities
       
    61 void RSubCommandQueue::InsertAfterHeadL(TSubCommandBase* aCommand)
       
    62     {
       
    63     LOG_FUNC
       
    64     if (!aCommand)
       
    65         {
       
    66         User::Leave(KErrArgument);
       
    67         }
       
    68     
       
    69     iQueue.InsertL(aCommand, 1);
       
    70     }
       
    71     
       
    72 // Execute the head sub-comment
       
    73 void RSubCommandQueue::ExecuteHeadL()
       
    74     {
       
    75     LOG_FUNC
       
    76     Head().ExecuteL();
       
    77     }
       
    78     
       
    79 // Get a reference of head sub-command in queue
       
    80 TSubCommandBase& RSubCommandQueue::Head()
       
    81     {
       
    82     LOG_FUNC
       
    83     return *iQueue[0];
       
    84     }
       
    85     
       
    86 // Destory all entities and release the memory of queue
       
    87 void RSubCommandQueue::Release()
       
    88     {
       
    89     LOG_FUNC
       
    90     iQueue.ResetAndDestroy();
       
    91     }
       
    92 
       
    93 /*
       
    94  *  Public member functions
       
    95  */
       
    96 CDeviceEventHandler::~CDeviceEventHandler()
       
    97     {
       
    98     LOG_FUNC
       
    99     Cancel();
       
   100     delete iErrNotiData;
       
   101     iSubCommandQueue.Release();
       
   102     }
       
   103 
       
   104 CDeviceEventHandler* CDeviceEventHandler::NewL(MMsmmSrvProxy& aServer)
       
   105     {
       
   106     LOG_STATIC_FUNC_ENTRY
       
   107     CDeviceEventHandler* self = CDeviceEventHandler::NewLC(aServer);
       
   108     CleanupStack::Pop(self);
       
   109     
       
   110     return self;
       
   111     }
       
   112 
       
   113 CDeviceEventHandler* CDeviceEventHandler::NewLC(MMsmmSrvProxy& aServer)
       
   114     {
       
   115     LOG_STATIC_FUNC_ENTRY
       
   116     CDeviceEventHandler* self = 
       
   117         new (ELeave) CDeviceEventHandler(aServer);
       
   118     CleanupStack::PushL(self);
       
   119     self->ConstructL();
       
   120     
       
   121     return self;
       
   122     }
       
   123 
       
   124 void CDeviceEventHandler::CreateSubCmdForRetrieveDriveLetterL(
       
   125         TInt aLogicalUnitCount)
       
   126     {
       
   127     LOG_FUNC
       
   128     TRetrieveDriveLetter* command(NULL);
       
   129     THostMsSubCommandParam parameter(iServer, *this, *this, iIncomingEvent);
       
   130     for (TInt index = 0; index < aLogicalUnitCount; index++)
       
   131         {
       
   132         command = new (ELeave) TRetrieveDriveLetter(parameter, index);
       
   133         iSubCommandQueue.PushL(command);
       
   134         }
       
   135     }
       
   136 
       
   137 void CDeviceEventHandler::CreateSubCmdForMountingLogicalUnitL(TText aDrive, 
       
   138         TInt aLuNumber)
       
   139     {
       
   140     LOG_FUNC
       
   141     THostMsSubCommandParam parameter(iServer, *this, *this, iIncomingEvent);
       
   142     TMountLogicalUnit* command = new (ELeave) TMountLogicalUnit(
       
   143             parameter, aDrive, aLuNumber);
       
   144     iSubCommandQueue.InsertAfterHeadL(command);
       
   145     }
       
   146 
       
   147 void CDeviceEventHandler::CreateSubCmdForSaveLatestMountInfoL(TText aDrive, 
       
   148         TInt aLuNumber)
       
   149     {
       
   150     LOG_FUNC
       
   151     THostMsSubCommandParam parameter(iServer, *this, *this, iIncomingEvent);
       
   152     TSaveLatestMountInfo* command = 
       
   153         new (ELeave) TSaveLatestMountInfo(parameter, aDrive, aLuNumber);
       
   154     iSubCommandQueue.InsertAfterHeadL(command);
       
   155     }
       
   156 
       
   157 void CDeviceEventHandler::Start()
       
   158     {
       
   159     LOG_FUNC
       
   160     if (IsActive())
       
   161         {
       
   162         return;
       
   163         }
       
   164     iStatus = KRequestPending;
       
   165     SetActive();
       
   166     }
       
   167 
       
   168 void CDeviceEventHandler::Complete(TInt aError)
       
   169     {
       
   170     LOG_FUNC
       
   171     TRequestStatus* status = &iStatus;
       
   172     User::RequestComplete(status, aError);
       
   173     }
       
   174 
       
   175 TRequestStatus& CDeviceEventHandler::Status() const
       
   176     {
       
   177     LOG_FUNC
       
   178     const TRequestStatus& status = iStatus;
       
   179     return const_cast<TRequestStatus&>(status);
       
   180     }
       
   181 
       
   182 void CDeviceEventHandler::HandleEventL(TRequestStatus& aStatus, 
       
   183         const TDeviceEvent& aEvent)
       
   184     {
       
   185     LOG_FUNC
       
   186     if (IsActive())
       
   187         {
       
   188         // An event is being handled. Currently handler is busy.
       
   189         User::Leave(KErrInUse);
       
   190         }
       
   191     
       
   192     // Copy incoming event
       
   193     iIncomingEvent = aEvent;
       
   194 
       
   195     // Create sub-commands and append them to queue
       
   196     CreateSubCmdForDeviceEventL();
       
   197     
       
   198     aStatus = KRequestPending;
       
   199     iEvtQueueStatus = &aStatus;
       
   200     
       
   201     // Start the handler to handle the incoming event
       
   202     Start();
       
   203     Complete();
       
   204     }
       
   205 
       
   206 /*
       
   207  * Protected member functions 
       
   208  */
       
   209 
       
   210 void CDeviceEventHandler::DoCancel()
       
   211     {
       
   212     LOG_FUNC
       
   213     // Complete client with KErrCancel
       
   214     CompleteClient(KErrCancel);
       
   215     
       
   216     // Cancel current pending command
       
   217     iSubCommandQueue.Head().CancelAsyncCmd();
       
   218     }
       
   219 
       
   220 void CDeviceEventHandler::RunL( )
       
   221     {
       
   222     LOG_FUNC
       
   223     
       
   224     if (iSubCommandQueue.Count() == 0)
       
   225         {
       
   226         // Error occurs in lastest sub-command's DoExecuteL()
       
   227         // Or current command has been cancelled.
       
   228         return;
       
   229         }
       
   230     
       
   231     if (iSubCommandQueue.Head().IsExecuted())
       
   232         {
       
   233         // Complete the current sub-command
       
   234         iSubCommandQueue.Head().AsyncCmdCompleteL();
       
   235         iSubCommandQueue.Pop();
       
   236         }
       
   237 
       
   238     // Move to the next sub-command
       
   239     if (iSubCommandQueue.Count())
       
   240         {
       
   241         iSubCommandQueue.ExecuteHeadL();
       
   242         }
       
   243     else
       
   244         {
       
   245         // Run out of sub-commands. Current handling event achieved.
       
   246         // Complete client
       
   247         CompleteClient();
       
   248         }
       
   249     }
       
   250 
       
   251 TInt CDeviceEventHandler::RunError(TInt aError)
       
   252     {
       
   253     LOG_FUNC
       
   254     // Retrieve sub-command related error notification data
       
   255     iSubCommandQueue.Head().HandleError(*iErrNotiData, aError);
       
   256         
       
   257     // If current sub-command isn't a key one, the handler will continue to
       
   258     // execute rest sub-command in the queue. But, if current sub-command
       
   259     // is the last one in the queue, handler shall complete the client also. 
       
   260     if (iSubCommandQueue.Head().IsKeyCommand() || 
       
   261             (iSubCommandQueue.Count() == 1))
       
   262         {
       
   263         CompleteClient(aError);
       
   264         }
       
   265 
       
   266     //    CompleteClient(aError);
       
   267     if (IsActive())
       
   268         {
       
   269         Complete(aError);
       
   270         }
       
   271     
       
   272     if (iSubCommandQueue.Count())
       
   273         {
       
   274         iSubCommandQueue.Pop();
       
   275         }
       
   276     
       
   277     return KErrNone;
       
   278     }
       
   279 
       
   280 // Private member functions
       
   281 CDeviceEventHandler::CDeviceEventHandler(MMsmmSrvProxy& aServer):
       
   282     CActive(EPriorityStandard),
       
   283     iServer(aServer)
       
   284     {
       
   285     LOG_FUNC
       
   286     CActiveScheduler::Add(this);
       
   287     }
       
   288 
       
   289 void CDeviceEventHandler::ConstructL()
       
   290     {
       
   291     LOG_FUNC
       
   292     iErrNotiData = new (ELeave) THostMsErrData;
       
   293     ResetHandler();
       
   294     }
       
   295 
       
   296 void CDeviceEventHandler::CreateSubCmdForDeviceEventL()
       
   297     {
       
   298     LOG_FUNC
       
   299     switch (iIncomingEvent.iEvent)
       
   300         {
       
   301     case EDeviceEventAddFunction:
       
   302         CreateSubCmdForAddingUsbMsFunctionL();
       
   303         break;
       
   304     case EDeviceEventRemoveDevice:
       
   305         CreateSubCmdForRemovingUsbMsDeviceL();
       
   306         break;
       
   307         }
       
   308     }
       
   309 
       
   310 void CDeviceEventHandler::CreateSubCmdForAddingUsbMsFunctionL()
       
   311     {
       
   312     LOG_FUNC
       
   313     THostMsSubCommandParam parameter(iServer, *this, *this, iIncomingEvent);
       
   314     TRegisterInterface* command = new (ELeave) TRegisterInterface(parameter);
       
   315     iSubCommandQueue.PushL(command);
       
   316     }
       
   317 
       
   318 void CDeviceEventHandler::CreateSubCmdForRemovingUsbMsDeviceL()
       
   319     {
       
   320     LOG_FUNC
       
   321     CMsmmEngine& engine = iServer.Engine();
       
   322     TUsbMsDevice* device = engine.SearchDevice(iIncomingEvent.iDeviceId);
       
   323     if (!device)
       
   324         {
       
   325         User::Leave(KErrNotFound);
       
   326         }
       
   327     TUsbMsInterface* interface = device->FirstChild();
       
   328     THostMsSubCommandParam parameter(iServer, *this, *this, iIncomingEvent);
       
   329     while (interface)
       
   330         {
       
   331         TUsbMsLogicalUnit* logicalUnit = interface->FirstChild();
       
   332         while (logicalUnit)
       
   333             {
       
   334             TDismountLogicalUnit* dismount = 
       
   335                 new (ELeave) TDismountLogicalUnit(parameter, *logicalUnit);
       
   336             iSubCommandQueue.PushL(dismount);
       
   337             logicalUnit = logicalUnit->NextPeer();
       
   338             }
       
   339         TDeregisterInterface* deregister = new (ELeave) TDeregisterInterface(
       
   340                 parameter, 
       
   341                 interface->iInterfaceNumber, interface->iInterfaceToken);
       
   342         iSubCommandQueue.PushL(deregister);
       
   343         interface = interface->NextPeer();
       
   344         };
       
   345     TRemoveUsbMsDeviceNode* removeNode = 
       
   346         new (ELeave) TRemoveUsbMsDeviceNode(parameter, device);
       
   347     iSubCommandQueue.PushL(removeNode);
       
   348     }
       
   349 
       
   350 void CDeviceEventHandler::ResetHandler()
       
   351     {
       
   352     LOG_FUNC
       
   353     ResetHandlerData();
       
   354     ResetHandlerError();
       
   355     }
       
   356 
       
   357 void CDeviceEventHandler::ResetHandlerData()
       
   358     {
       
   359     LOG_FUNC
       
   360     // Reset event buffer
       
   361     iIncomingEvent.iDeviceId = 0;
       
   362     iIncomingEvent.iEvent = EDeviceEventEndMark;
       
   363     iIncomingEvent.iInterfaceNumber = 0;
       
   364     
       
   365     // Destory sub-command queue
       
   366     iSubCommandQueue.Release();
       
   367     }
       
   368 
       
   369 void CDeviceEventHandler::ResetHandlerError()
       
   370     {
       
   371     LOG_FUNC
       
   372     // Reset error notification data
       
   373     iErrNotiData->iDriveName = 0x0;
       
   374     iErrNotiData->iError = EHostMsErrorEndMarker;
       
   375     iErrNotiData->iE32Error = KErrNone;
       
   376     iErrNotiData->iManufacturerString.Zero();
       
   377     }
       
   378 
       
   379 void CDeviceEventHandler::CompleteClient(TInt aError/* = KErrNone*/)
       
   380     {
       
   381     LOG_FUNC
       
   382     if (iEvtQueueStatus)
       
   383         {
       
   384         User::RequestComplete(iEvtQueueStatus, aError);
       
   385         }
       
   386     }
       
   387 
       
   388 // End of file