|
1 /* |
|
2 * Copyright (c) 2007 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: Device state watcher class. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include "USBDeviceStateWatcher.h" |
|
21 |
|
22 // CONSTANTS |
|
23 const TUint KUsbAllStates = 0xFFFFFFFF; |
|
24 |
|
25 // -------------------------------------------------------------------------- |
|
26 // Two-phased constructor. Uses existing usb manager session. |
|
27 // -------------------------------------------------------------------------- |
|
28 CUSBDeviceStateWatcher* |
|
29 CUSBDeviceStateWatcher::NewL(MUSBDeviceStateObserver& aObserver, RUsb& aUsbMan) |
|
30 { |
|
31 CUSBDeviceStateWatcher* self = new(ELeave)CUSBDeviceStateWatcher(aObserver); |
|
32 CleanupStack::PushL(self); |
|
33 self->ConstructL(aUsbMan); |
|
34 CleanupStack::Pop(self); |
|
35 return self; |
|
36 } |
|
37 |
|
38 // -------------------------------------------------------------------------- |
|
39 // Two-phased constructor. Creates its own usb manager session. |
|
40 // -------------------------------------------------------------------------- |
|
41 CUSBDeviceStateWatcher* |
|
42 CUSBDeviceStateWatcher::NewL(MUSBDeviceStateObserver& aObserver) |
|
43 { |
|
44 CUSBDeviceStateWatcher* self = new(ELeave)CUSBDeviceStateWatcher(aObserver); |
|
45 CleanupStack::PushL(self); |
|
46 self->ConstructL(); |
|
47 CleanupStack::Pop(self); |
|
48 return self; |
|
49 } |
|
50 |
|
51 // -------------------------------------------------------------------------- |
|
52 // C++ constructor |
|
53 // -------------------------------------------------------------------------- |
|
54 CUSBDeviceStateWatcher::CUSBDeviceStateWatcher(MUSBDeviceStateObserver& aObserver) |
|
55 : CActive(EPriorityStandard), iObserver(aObserver) |
|
56 { |
|
57 CActiveScheduler::Add(this); |
|
58 } |
|
59 |
|
60 // -------------------------------------------------------------------------- |
|
61 // C++ destructor |
|
62 // -------------------------------------------------------------------------- |
|
63 CUSBDeviceStateWatcher::~CUSBDeviceStateWatcher() |
|
64 { |
|
65 Cancel(); |
|
66 } |
|
67 |
|
68 // -------------------------------------------------------------------------- |
|
69 // Symbian 2nd phase constructor. Uses existing usb manager session. |
|
70 // -------------------------------------------------------------------------- |
|
71 void CUSBDeviceStateWatcher::ConstructL(RUsb& aUsbMan) |
|
72 { |
|
73 iUsbMan.SetHandleNC(aUsbMan.Handle()); // NC == NoClose |
|
74 CommonConstructL(); |
|
75 } |
|
76 |
|
77 // -------------------------------------------------------------------------- |
|
78 // Symbian 2nd phase constructor. Creates its own usb manager session. |
|
79 // -------------------------------------------------------------------------- |
|
80 void CUSBDeviceStateWatcher::ConstructL() |
|
81 { |
|
82 User::LeaveIfError(iUsbMan.Connect()); |
|
83 CommonConstructL(); |
|
84 } |
|
85 |
|
86 // -------------------------------------------------------------------------- |
|
87 // Code shared by all ConstructL methods. |
|
88 // Usb manager session has already been set up. |
|
89 // -------------------------------------------------------------------------- |
|
90 void CUSBDeviceStateWatcher::CommonConstructL() |
|
91 { |
|
92 User::LeaveIfError(iUsbMan.GetDeviceState(iCurrentState)); |
|
93 iPreviousState = iCurrentState; |
|
94 iUsbMan.DeviceStateNotification(KUsbAllStates, iCurrentState, iStatus); |
|
95 SetActive(); |
|
96 } |
|
97 |
|
98 // -------------------------------------------------------------------------- |
|
99 // The device state has changed. |
|
100 // -------------------------------------------------------------------------- |
|
101 void CUSBDeviceStateWatcher::RunL() |
|
102 { |
|
103 if (iStatus == KErrNone) |
|
104 { |
|
105 if (iCurrentState != iPreviousState) |
|
106 { |
|
107 iObserver.DeviceStateChanged(iPreviousState, iCurrentState); |
|
108 iPreviousState = iCurrentState; |
|
109 } |
|
110 iUsbMan.DeviceStateNotification(KUsbAllStates, iCurrentState, iStatus); |
|
111 SetActive(); |
|
112 } |
|
113 } |
|
114 |
|
115 // ---------------------------------------------------------------------------- |
|
116 // Standard active object error function. |
|
117 // ---------------------------------------------------------------------------- |
|
118 TInt CUSBDeviceStateWatcher::RunError(TInt /*aError*/) |
|
119 { |
|
120 // Currently no leaving functions called in RunL, thus nothing should cause |
|
121 // this to be called -> return. |
|
122 return KErrNone; |
|
123 } |
|
124 |
|
125 // ---------------------------------------------------------------------------- |
|
126 // Standard active object cancellation function. |
|
127 // ---------------------------------------------------------------------------- |
|
128 void CUSBDeviceStateWatcher::DoCancel() |
|
129 { |
|
130 iUsbMan.DeviceStateNotificationCancel(); |
|
131 } |