kerneltest/f32test/shostmassstorage/msman/src/cusbotg.cpp
changeset 0 a41df078684a
child 297 b2826f67641f
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 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 #include <e32cmn.h>
       
    17 #include <e32base.h>
       
    18 #include <d32otgdi.h>
       
    19 #include <e32property.h>
       
    20 
       
    21 #include "cusbotg.h"
       
    22 #include "cusbotgwatcher.h"
       
    23 #include "rusbmspublisher.h"
       
    24 
       
    25 
       
    26 #include "tmslog.h"
       
    27 #include "debug.h"
       
    28 
       
    29 _LIT(KOtgdiLddFileName, "otgdi");
       
    30 
       
    31 
       
    32 ROtgStateChangeNotifier::ROtgStateChangeNotifier()
       
    33 :   iRegistered(EFalse)
       
    34     {
       
    35     __MSFNSLOG
       
    36     }
       
    37 
       
    38 
       
    39 ROtgStateChangeNotifier::~ROtgStateChangeNotifier()
       
    40     {
       
    41     __MSFNSLOG
       
    42     if (iRegistered)
       
    43         iMessage.Complete(KErrDisconnected);
       
    44     }
       
    45 
       
    46 /**
       
    47 Initialise notifier to enable media change notfications.
       
    48 
       
    49 @param aMessage The message to commplete the notification
       
    50 */
       
    51 void ROtgStateChangeNotifier::Register(const RMessage2& aMessage)
       
    52     {
       
    53     __MSFNLOG
       
    54 	iRegistered = ETrue;
       
    55 	iMessage = aMessage;
       
    56     }
       
    57 
       
    58 
       
    59 void ROtgStateChangeNotifier::DoNotifyL()
       
    60     {
       
    61 	__MSFNLOG
       
    62 	CompleteNotifierL(KErrNone);
       
    63     }
       
    64 
       
    65 
       
    66 void ROtgStateChangeNotifier::DoCancelL()
       
    67     {
       
    68 	__MSFNLOG
       
    69 	CompleteNotifierL(KErrCancel);
       
    70     }
       
    71 
       
    72 
       
    73 void ROtgStateChangeNotifier::CompleteNotifierL(TInt aReason)
       
    74 	{
       
    75     __MSFNLOG
       
    76 	if (iRegistered)
       
    77         {
       
    78 		TBool changed = ETrue;
       
    79         TPckgBuf<TBool> p(changed);
       
    80 		iMessage.WriteL(0, p);
       
    81 		iMessage.Complete(aReason);
       
    82 		iRegistered = EFalse;
       
    83         }
       
    84 	}
       
    85 
       
    86 
       
    87 CUsbOtg* CUsbOtg::NewL()
       
    88 	{
       
    89     __MSFNSLOG
       
    90 	CUsbOtg* self = new (ELeave) CUsbOtg();
       
    91 	CleanupStack::PushL(self);
       
    92 	self->ConstructL();
       
    93 	CleanupStack::Pop(self);
       
    94 	return self;
       
    95 	}
       
    96 
       
    97 
       
    98 void CUsbOtg::ConstructL()
       
    99 	{
       
   100     __MSFNLOG
       
   101 
       
   102     TInt r = User::LoadLogicalDevice(KOtgdiLddFileName);
       
   103 
       
   104     if (r != KErrNone && r != KErrAlreadyExists)
       
   105         {
       
   106         __USBOTGPRINT1(_L("   LoadLogicalDevice(KOtgdiLddFileName) error = %d"), r);
       
   107         User::Leave(r);
       
   108         }
       
   109 
       
   110     r = iUsbOtgDriver.Open();
       
   111     if (r != KErrNone && r != KErrAlreadyExists)
       
   112         {
       
   113         __USBOTGPRINT1(_L("   otg.Open fails %d"), r);
       
   114         User::FreeLogicalDevice(RUsbOtgDriver::Name());
       
   115         User::Leave(r);
       
   116         }
       
   117 
       
   118     StartL();
       
   119 
       
   120     r = iUsbOtgDriver.StartStacks();
       
   121     if (r != KErrNone)
       
   122         {
       
   123         __USBOTGPRINT1(_L("   otg.StartStacks fails %d"), r);
       
   124         User::FreeLogicalDevice(RUsbOtgDriver::Name());
       
   125         User::Leave(r);
       
   126         }
       
   127 
       
   128     __USBOTGPRINT(_L("   otg stacks successfully started"));
       
   129 	}
       
   130 
       
   131 
       
   132 CUsbOtg::CUsbOtg()
       
   133 :   iOtgState(KOtgStateStart)
       
   134     {
       
   135     __MSFNLOG
       
   136     }
       
   137 
       
   138 
       
   139 CUsbOtg::~CUsbOtg()
       
   140 	{
       
   141     __MSFNLOG
       
   142     Stop();
       
   143 
       
   144     TInt r = iUsbOtgDriver.BusDrop();
       
   145 
       
   146     // Unload OTGDI components if it was ever started
       
   147 
       
   148     if (iUsbOtgDriver.Handle())
       
   149         {
       
   150         iUsbOtgDriver.StopStacks();
       
   151         iUsbOtgDriver.Close();
       
   152 
       
   153         }
       
   154 
       
   155     TInt err = User::FreeLogicalDevice(RUsbOtgDriver::Name());
       
   156 	}
       
   157 
       
   158 
       
   159 void CUsbOtg::StartL()
       
   160     {
       
   161     __MSFNLOG
       
   162 	// Request Otg notifications
       
   163     iOtgEventWatcher = CUsbOtgEventWatcher::NewL(iUsbOtgDriver, *this);
       
   164 	iOtgEventWatcher->Start();
       
   165 
       
   166     iRequestSessionWatcher = CRequestSessionWatcher::NewL(*this);
       
   167     }
       
   168 
       
   169 
       
   170 void CUsbOtg::Stop()
       
   171 /**
       
   172  * Stop the USB OTG events watcher
       
   173  */
       
   174     {
       
   175     __MSFNLOG
       
   176 
       
   177     if (iOtgEventWatcher)
       
   178         {
       
   179         iOtgEventWatcher->Cancel();
       
   180         delete iOtgEventWatcher;
       
   181         iOtgEventWatcher = NULL;
       
   182         }
       
   183 
       
   184     if (iRequestSessionWatcher)
       
   185         {
       
   186         delete iRequestSessionWatcher;
       
   187         iRequestSessionWatcher = NULL;
       
   188         }
       
   189     }
       
   190 
       
   191 
       
   192 void CUsbOtg::BusRequestL()
       
   193     {
       
   194     __MSFNLOG
       
   195     if (iOtgState == KOtgStateAPlugInserted)
       
   196         {
       
   197         TInt err = iUsbOtgDriver.BusRequest();
       
   198         if (err)
       
   199             {
       
   200             __USBOTGPRINT1(_L("OTG::BusRequest[%d] failed !"), err);
       
   201             }
       
   202         User::LeaveIfError(err);
       
   203         iOtgState = KOtgStateSessionOpen;
       
   204         }
       
   205 
       
   206     }
       
   207 
       
   208 
       
   209 void CUsbOtg::HandleUsbOtgEvent(RUsbOtgDriver::TOtgEvent aEvent)
       
   210     {
       
   211     __MSFNLOG
       
   212 
       
   213     switch (aEvent)
       
   214         {
       
   215         case RUsbOtgDriver::EEventAPlugInserted:
       
   216             {
       
   217             __USBOTGPRINT(_L(">> UsbOtgEvent[EEventAPlugInserted]"));
       
   218             /*
       
   219             RUsbOtgEventPublisher eventPublisher;
       
   220             eventPublisher.PublishEvent(aEvent);
       
   221             */
       
   222             iNotifier.DoNotifyL();
       
   223             iOtgState = KOtgStateAPlugInserted;
       
   224             }
       
   225             break;
       
   226 
       
   227         case RUsbOtgDriver::EEventBusConnectionBusy:
       
   228             {
       
   229             RUsbManConnectionStatePublisher publisher;
       
   230             publisher.PublishEvent(ETrue);
       
   231             }
       
   232             break;
       
   233         case RUsbOtgDriver::EEventBusConnectionIdle:
       
   234             {
       
   235             RUsbManConnectionStatePublisher publisher;
       
   236             publisher.PublishEvent(EFalse);
       
   237             }
       
   238             break;
       
   239         default:
       
   240             __USBOTGPRINT1(_L(">> UsbOtgEvent[%x]"), aEvent);
       
   241             break;
       
   242         }
       
   243     }
       
   244 
       
   245 
       
   246 TBool CUsbOtg::DeviceInserted()
       
   247     {
       
   248     __MSFNLOG
       
   249     return iOtgState == KOtgStateAPlugInserted ? ETrue : EFalse;
       
   250     }
       
   251 
       
   252 void CUsbOtg::NotifyChange(const RMessage2& aMessage)
       
   253     {
       
   254     __MSFNLOG
       
   255     iNotifier.Register(aMessage);
       
   256     }
       
   257 
       
   258 
       
   259 void CUsbOtg::NotifyChangeCancel()
       
   260     {
       
   261     __MSFNLOG
       
   262     iNotifier.DoCancelL();
       
   263     }
       
   264 
       
   265 
       
   266 TInt CUsbOtg::BusDrop()
       
   267     {
       
   268     __MSFNLOG
       
   269     return iUsbOtgDriver.BusDrop();
       
   270     }