Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
19 #include <e32property.h>
22 #include "cusbotgwatcher.h"
23 #include "rusbmspublisher.h"
29 _LIT(KOtgdiLddFileName, "otgdi");
32 ROtgStateChangeNotifier::ROtgStateChangeNotifier()
39 ROtgStateChangeNotifier::~ROtgStateChangeNotifier()
43 iMessage.Complete(KErrDisconnected);
47 Initialise notifier to enable media change notfications.
49 @param aMessage The message to commplete the notification
51 void ROtgStateChangeNotifier::Register(const RMessage2& aMessage)
59 void ROtgStateChangeNotifier::DoNotifyL()
62 CompleteNotifierL(KErrNone);
66 void ROtgStateChangeNotifier::DoCancelL()
69 CompleteNotifierL(KErrCancel);
73 void ROtgStateChangeNotifier::CompleteNotifierL(TInt aReason)
78 TBool changed = ETrue;
79 TPckgBuf<TBool> p(changed);
80 iMessage.WriteL(0, p);
81 iMessage.Complete(aReason);
87 CUsbOtg* CUsbOtg::NewL()
90 CUsbOtg* self = new (ELeave) CUsbOtg();
91 CleanupStack::PushL(self);
93 CleanupStack::Pop(self);
98 void CUsbOtg::ConstructL()
102 TInt r = User::LoadLogicalDevice(KOtgdiLddFileName);
104 if (r != KErrNone && r != KErrAlreadyExists)
106 __USBOTGPRINT1(_L(" LoadLogicalDevice(KOtgdiLddFileName) error = %d"), r);
110 r = iUsbOtgDriver.Open();
111 if (r != KErrNone && r != KErrAlreadyExists)
113 __USBOTGPRINT1(_L(" otg.Open fails %d"), r);
114 User::FreeLogicalDevice(RUsbOtgDriver::Name());
120 r = iUsbOtgDriver.StartStacks();
123 __USBOTGPRINT1(_L(" otg.StartStacks fails %d"), r);
124 User::FreeLogicalDevice(RUsbOtgDriver::Name());
128 __USBOTGPRINT(_L(" otg stacks successfully started"));
133 : iOtgState(KOtgStateStart)
144 TInt r = iUsbOtgDriver.BusDrop();
146 // Unload OTGDI components if it was ever started
148 if (iUsbOtgDriver.Handle())
150 iUsbOtgDriver.StopStacks();
151 iUsbOtgDriver.Close();
155 TInt err = User::FreeLogicalDevice(RUsbOtgDriver::Name());
159 void CUsbOtg::StartL()
162 // Request Otg notifications
163 iOtgEventWatcher = CUsbOtgEventWatcher::NewL(iUsbOtgDriver, *this);
164 iOtgEventWatcher->Start();
166 iRequestSessionWatcher = CRequestSessionWatcher::NewL(*this);
172 * Stop the USB OTG events watcher
177 if (iOtgEventWatcher)
179 iOtgEventWatcher->Cancel();
180 delete iOtgEventWatcher;
181 iOtgEventWatcher = NULL;
184 if (iRequestSessionWatcher)
186 delete iRequestSessionWatcher;
187 iRequestSessionWatcher = NULL;
192 void CUsbOtg::BusRequestL()
195 if (iOtgState == KOtgStateAPlugInserted)
197 TInt err = iUsbOtgDriver.BusRequest();
200 __USBOTGPRINT1(_L("OTG::BusRequest[%d] failed !"), err);
202 User::LeaveIfError(err);
203 iOtgState = KOtgStateSessionOpen;
209 void CUsbOtg::HandleUsbOtgEvent(RUsbOtgDriver::TOtgEvent aEvent)
215 case RUsbOtgDriver::EEventAPlugInserted:
217 __USBOTGPRINT(_L(">> UsbOtgEvent[EEventAPlugInserted]"));
219 RUsbOtgEventPublisher eventPublisher;
220 eventPublisher.PublishEvent(aEvent);
222 iNotifier.DoNotifyL();
223 iOtgState = KOtgStateAPlugInserted;
227 case RUsbOtgDriver::EEventBusConnectionBusy:
229 RUsbManConnectionStatePublisher publisher;
230 publisher.PublishEvent(ETrue);
233 case RUsbOtgDriver::EEventBusConnectionIdle:
235 RUsbManConnectionStatePublisher publisher;
236 publisher.PublishEvent(EFalse);
240 __USBOTGPRINT1(_L(">> UsbOtgEvent[%x]"), aEvent);
246 TBool CUsbOtg::DeviceInserted()
249 return iOtgState == KOtgStateAPlugInserted ? ETrue : EFalse;
252 void CUsbOtg::NotifyChange(const RMessage2& aMessage)
255 iNotifier.Register(aMessage);
259 void CUsbOtg::NotifyChangeCancel()
262 iNotifier.DoCancelL();
266 TInt CUsbOtg::BusDrop()
269 return iUsbOtgDriver.BusDrop();