1 /* |
|
2 * Copyright (c) 2008 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: Implementation |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <usbotgdefs.h> |
|
19 |
|
20 #include "cusbbusactivityobserver.h" |
|
21 |
|
22 #include "definitions.h" |
|
23 #include "debug.h" |
|
24 #include "panic.h" |
|
25 |
|
26 // --------------------------------------------------------------------------- |
|
27 // |
|
28 // --------------------------------------------------------------------------- |
|
29 // |
|
30 CUsbBusActivityObserver::CUsbBusActivityObserver() : |
|
31 CActive(EPriorityStandard) |
|
32 { |
|
33 CActiveScheduler::Add(this); |
|
34 } |
|
35 |
|
36 // --------------------------------------------------------------------------- |
|
37 // |
|
38 // --------------------------------------------------------------------------- |
|
39 // |
|
40 void CUsbBusActivityObserver::ConstructL() |
|
41 { |
|
42 LOG_FUNC |
|
43 |
|
44 LEAVEIFERROR(iBusActivity.Attach(KUidUsbManCategory, |
|
45 KUsbOtgConnectionIdleProperty)); |
|
46 |
|
47 } |
|
48 |
|
49 // --------------------------------------------------------------------------- |
|
50 // |
|
51 // --------------------------------------------------------------------------- |
|
52 // |
|
53 CUsbBusActivityObserver* CUsbBusActivityObserver::NewL() |
|
54 { |
|
55 LOG_FUNC |
|
56 |
|
57 CUsbBusActivityObserver* self = new (ELeave) CUsbBusActivityObserver(); |
|
58 CleanupStack::PushL(self); |
|
59 self->ConstructL(); |
|
60 CleanupStack::Pop(self); |
|
61 return self; |
|
62 } |
|
63 |
|
64 // --------------------------------------------------------------------------- |
|
65 // |
|
66 // --------------------------------------------------------------------------- |
|
67 // |
|
68 CUsbBusActivityObserver::~CUsbBusActivityObserver() |
|
69 |
|
70 { |
|
71 LOG_FUNC |
|
72 |
|
73 Cancel(); |
|
74 |
|
75 iBusActivity.Close(); |
|
76 |
|
77 iObservers.Close(); |
|
78 |
|
79 } |
|
80 |
|
81 // --------------------------------------------------------------------------- |
|
82 // |
|
83 // --------------------------------------------------------------------------- |
|
84 // |
|
85 CUsbBusActivityObserver::TBusActivity CUsbBusActivityObserver::BusActivity() |
|
86 { |
|
87 |
|
88 TInt val(0); |
|
89 |
|
90 TInt err = iBusActivity.Get(val); |
|
91 |
|
92 if (KErrNone != err) |
|
93 { |
|
94 LOG("ECanNotGetBusActivityProperty" ); |
|
95 PANIC( ECanNotGetBusActivityProperty); |
|
96 } |
|
97 |
|
98 return (0 == val ? EBusActive : EBusIdle); |
|
99 |
|
100 } |
|
101 |
|
102 // --------------------------------------------------------------------------- |
|
103 // |
|
104 // --------------------------------------------------------------------------- |
|
105 // |
|
106 void CUsbBusActivityObserver::SubscribeL(MUsbBusActivityObserver& aObserver) |
|
107 { |
|
108 LOG_FUNC |
|
109 |
|
110 // check if the same observer already exist in a list |
|
111 if (KErrNotFound != iObservers.Find(&aObserver)) |
|
112 { |
|
113 LOG( "Observer already exists" ); |
|
114 PANIC( EObserverAlreadyExists); |
|
115 return; |
|
116 } |
|
117 iObservers.AppendL(&aObserver); |
|
118 |
|
119 if (KFirst == iObservers.Count()) // first item |
|
120 { |
|
121 iBusActivity.Subscribe(iStatus); |
|
122 SetActive(); |
|
123 |
|
124 } |
|
125 } |
|
126 |
|
127 // --------------------------------------------------------------------------- |
|
128 // |
|
129 // --------------------------------------------------------------------------- |
|
130 // |
|
131 void CUsbBusActivityObserver::UnsubscribeL(MUsbBusActivityObserver& aObserver) |
|
132 { |
|
133 LOG_FUNC |
|
134 |
|
135 TInt i(iObservers.Find(&aObserver)); |
|
136 if (KErrNotFound == i) |
|
137 { |
|
138 LOG( "Observer not found" ); |
|
139 PANIC( ECanNotFindBusActivityObserver); |
|
140 return; |
|
141 } |
|
142 |
|
143 iObservers.Remove(i); |
|
144 |
|
145 if (0 == iObservers.Count()) // no observers anymore |
|
146 { |
|
147 // cancel pending request |
|
148 Cancel(); |
|
149 } |
|
150 } |
|
151 |
|
152 // --------------------------------------------------------------------------- |
|
153 |
|
154 // --------------------------------------------------------------------------- |
|
155 // |
|
156 void CUsbBusActivityObserver::RunL() |
|
157 { |
|
158 LOG_FUNC |
|
159 |
|
160 LOG1( "iStatus = %d" , iStatus.Int()); |
|
161 |
|
162 // if error occured, inform observers |
|
163 if (KErrNone != iStatus.Int()) |
|
164 { |
|
165 for (TInt i(0); i < iObservers.Count(); ++i) |
|
166 { |
|
167 iObservers[i]->BusActivityErrorL(iStatus.Int()); |
|
168 } |
|
169 } |
|
170 |
|
171 // re-issue request first |
|
172 iBusActivity.Subscribe(iStatus); |
|
173 SetActive(); |
|
174 |
|
175 // then process property change |
|
176 TBusActivity state(BusActivity()); |
|
177 |
|
178 switch (state) |
|
179 { |
|
180 case EBusIdle: |
|
181 { |
|
182 LOG("BusIdle"); |
|
183 |
|
184 for (TInt i(0); i < iObservers.Count(); ++i) |
|
185 { |
|
186 iObservers[i]->BusIdleL(); |
|
187 } |
|
188 break; |
|
189 } |
|
190 |
|
191 case EBusActive: |
|
192 { |
|
193 LOG("BusActive"); |
|
194 |
|
195 for (TInt i(0); i < iObservers.Count(); ++i) |
|
196 { |
|
197 iObservers[i]->BusActiveL(); |
|
198 } |
|
199 break; |
|
200 } |
|
201 |
|
202 default: |
|
203 { |
|
204 LOG("WrongBusState" ); |
|
205 PANIC( EWrongBusState); |
|
206 } |
|
207 } |
|
208 |
|
209 } |
|
210 |
|
211 // --------------------------------------------------------------------------- |
|
212 // |
|
213 // --------------------------------------------------------------------------- |
|
214 // |
|
215 void CUsbBusActivityObserver::DoCancel() |
|
216 { |
|
217 iBusActivity.Cancel(); |
|
218 } |
|
219 |
|
220 // --------------------------------------------------------------------------- |
|
221 // |
|
222 // --------------------------------------------------------------------------- |
|
223 // |
|
224 TInt CUsbBusActivityObserver::RunError(TInt aError) |
|
225 { |
|
226 LOG_FUNC |
|
227 |
|
228 LOG1( "aError = %d", aError ); |
|
229 |
|
230 // try to continue |
|
231 return KErrNone; |
|
232 |
|
233 } |
|
234 |
|