|
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: Implementation |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <usbman.h> |
|
20 |
|
21 #include "cusbservicecontrol.h" |
|
22 #include "debug.h" |
|
23 #include "panic.h" |
|
24 |
|
25 // --------------------------------------------------------------------------- |
|
26 // |
|
27 // --------------------------------------------------------------------------- |
|
28 // |
|
29 CUsbServiceControl::CUsbServiceControl(MUsbServiceControlObserver* aObserver, |
|
30 RUsb& aUsb) : |
|
31 CActive(CActive::EPriorityStandard), iObserver(aObserver), iUsb(aUsb), |
|
32 iPersonalityId(0) |
|
33 { |
|
34 CActiveScheduler::Add(this); |
|
35 } |
|
36 |
|
37 // --------------------------------------------------------------------------- |
|
38 // |
|
39 // --------------------------------------------------------------------------- |
|
40 // |
|
41 CUsbServiceControl::~CUsbServiceControl() |
|
42 { |
|
43 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::~CUsbServiceControl" ) ); |
|
44 Cancel(); |
|
45 } |
|
46 |
|
47 // --------------------------------------------------------------------------- |
|
48 // |
|
49 // --------------------------------------------------------------------------- |
|
50 // |
|
51 void CUsbServiceControl::ConstructL() |
|
52 { |
|
53 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::ConstructL" ) ); |
|
54 } |
|
55 |
|
56 // --------------------------------------------------------------------------- |
|
57 // |
|
58 // --------------------------------------------------------------------------- |
|
59 // |
|
60 CUsbServiceControl* CUsbServiceControl::NewL( |
|
61 MUsbServiceControlObserver* aObserver, RUsb& aUsb) |
|
62 { |
|
63 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::NewL" ) ); |
|
64 |
|
65 CUsbServiceControl* self = new (ELeave) CUsbServiceControl(aObserver, |
|
66 aUsb); |
|
67 CleanupStack::PushL(self); |
|
68 self->ConstructL(); |
|
69 CleanupStack::Pop(self); // pop self |
|
70 return self; |
|
71 } |
|
72 |
|
73 // --------------------------------------------------------------------------- |
|
74 // |
|
75 // --------------------------------------------------------------------------- |
|
76 // |
|
77 TInt CUsbServiceControl::Start(TInt aPersonalityId) |
|
78 { |
|
79 FTRACE(FPrint(_L( "[USBOTGWATCHER]\tCUsbServiceControl::Start aPersonalityId = %d" ), aPersonalityId)); |
|
80 |
|
81 TUsbServiceState serviceState; |
|
82 TInt err = iUsb.GetServiceState(serviceState); |
|
83 |
|
84 if (KErrNone != err) |
|
85 { |
|
86 return err; |
|
87 } |
|
88 |
|
89 switch (serviceState) |
|
90 { |
|
91 case EUsbServiceIdle: |
|
92 { |
|
93 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Start UsbServiceState == EUsbServiceIdle" ) ); |
|
94 |
|
95 iPersonalityId = aPersonalityId; // when request completed, this will indicate that we started what we wanted |
|
96 if (IsActive()) |
|
97 { |
|
98 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Start Request is outstanding, cancelling first." ) ); |
|
99 Cancel(); |
|
100 } |
|
101 iUsb.TryStart(aPersonalityId, iStatus); |
|
102 SetActive(); |
|
103 break; |
|
104 } |
|
105 |
|
106 case EUsbServiceStarted: |
|
107 { |
|
108 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Start UsbServiceState == EUsbServiceStarted" ) ); |
|
109 |
|
110 TInt currentPersonality(0); |
|
111 err = iUsb.GetCurrentPersonalityId(currentPersonality); |
|
112 if (KErrNone != err) |
|
113 { |
|
114 FTRACE(FPrint(_L( "[USBOTGWATCHER]\tCUsbServiceControl::Start Error getting current personality err = %d" ), err)); |
|
115 return err; |
|
116 } |
|
117 |
|
118 if (aPersonalityId == currentPersonality) // already started |
|
119 { |
|
120 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Start Personality already sterted" ) ); |
|
121 iObserver->UsbServiceControlReqCompletedL(KErrNone); |
|
122 return KErrNone; |
|
123 } |
|
124 |
|
125 // we need to stop current personality and start service with new personailty id |
|
126 iPersonalityId = aPersonalityId; // this will indicate that we want to start this personality after |
|
127 |
|
128 Stop(); |
|
129 |
|
130 // start new personality in RunL() when state is Idle |
|
131 |
|
132 break; |
|
133 } |
|
134 case EUsbServiceStarting: |
|
135 { |
|
136 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Start UsbServiceState == EUsbServiceStarting" ) ); |
|
137 // do exactly the same as in case of EUsbServiceStopping; |
|
138 // break statement is not required here |
|
139 } |
|
140 case EUsbServiceStopping: |
|
141 { |
|
142 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Start UsbServiceState == EUsbServiceStopping" ) ); |
|
143 |
|
144 // subscribe for usb service state change, and start new usb service once current one started/stopped |
|
145 iPersonalityId = aPersonalityId; // this will indicate that we want to start this personality |
|
146 |
|
147 // this check is needed due to usb service might be stopping by other client |
|
148 if (IsActive()) |
|
149 { |
|
150 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Start Request outstanding. Waiting for completion." ) ); |
|
151 return KErrNone; // when the outstanding request get completed, we start usb services with iPersonalityId |
|
152 } |
|
153 |
|
154 // otherwise, monitor usb service state |
|
155 iUsb.ServiceStateNotification(iServiceState, iStatus); |
|
156 SetActive(); |
|
157 |
|
158 break; |
|
159 } |
|
160 case EUsbServiceFatalError: |
|
161 { |
|
162 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Start UsbServiceState == EUsbServiceFatalError" ) ); |
|
163 return KErrGeneral; |
|
164 } |
|
165 default: |
|
166 { |
|
167 Panic(EUnknownUsbServiceState); |
|
168 } |
|
169 } |
|
170 |
|
171 return KErrNone; |
|
172 |
|
173 } |
|
174 |
|
175 // --------------------------------------------------------------------------- |
|
176 // |
|
177 // --------------------------------------------------------------------------- |
|
178 // |
|
179 TInt CUsbServiceControl::Stop() |
|
180 { |
|
181 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Stop" ) ); |
|
182 |
|
183 TUsbServiceState serviceState; |
|
184 TInt err = iUsb.GetServiceState(serviceState); |
|
185 |
|
186 if (KErrNone != err) |
|
187 { |
|
188 return err; |
|
189 } |
|
190 |
|
191 switch (serviceState) |
|
192 { |
|
193 case EUsbServiceIdle: |
|
194 { |
|
195 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Stop UsbServiceState == EUsbServiceIdle" ) ); |
|
196 |
|
197 return KErrNone; |
|
198 } |
|
199 |
|
200 case EUsbServiceStarted: |
|
201 { |
|
202 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Stop UsbServiceState == EUsbServiceStarted" ) ); |
|
203 |
|
204 if (IsActive()) |
|
205 { |
|
206 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Stop Request is outstanding, cancelling it." ) ); |
|
207 Cancel(); |
|
208 } |
|
209 iUsb.TryStop(iStatus); |
|
210 SetActive(); |
|
211 |
|
212 break; |
|
213 } |
|
214 case EUsbServiceStopping: |
|
215 { |
|
216 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Stop UsbServiceState == EUsbServiceStopping" ) ); |
|
217 // do exactly the same as in case of EUsbServiceStarting; |
|
218 // break statement is not required here |
|
219 } |
|
220 case EUsbServiceStarting: |
|
221 { |
|
222 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Stop UsbServiceState == EUsbServiceStarting" ) ); |
|
223 |
|
224 // subscribe for usb service state change, and stop usb service once current one started |
|
225 iPersonalityId = 0; // this will indicate that we do not want to start this personality |
|
226 |
|
227 // this check is needed due to usb service might be starting by other client |
|
228 if (IsActive()) |
|
229 { |
|
230 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Stop Request is outstanding, waiting for completion. " ) ); |
|
231 return KErrNone; // when this request get completed, we request to stop usb services |
|
232 } |
|
233 |
|
234 // otherwise, monitor usb service state |
|
235 iUsb.ServiceStateNotification(iServiceState, iStatus); |
|
236 SetActive(); |
|
237 |
|
238 break; |
|
239 } |
|
240 case EUsbServiceFatalError: |
|
241 { |
|
242 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Start UsbServiceState == EUsbServiceFatalError" ) ); |
|
243 return KErrGeneral; |
|
244 } |
|
245 default: |
|
246 { |
|
247 Panic(EUnknownUsbServiceState); |
|
248 } |
|
249 } |
|
250 return KErrNone; |
|
251 } |
|
252 |
|
253 // --------------------------------------------------------------------------- |
|
254 // |
|
255 // --------------------------------------------------------------------------- |
|
256 // |
|
257 void CUsbServiceControl::RunL() |
|
258 { |
|
259 FTRACE(FPrint(_L( "[USBOTGWATCHER]\tCUsbServiceControl::RunL iStatus %d" ), iStatus.Int())); |
|
260 if (KErrNone != iStatus.Int()) |
|
261 { |
|
262 //User::Leave(iStatus.Int()); |
|
263 iObserver->UsbServiceControlReqCompletedL(iStatus.Int()); |
|
264 return; |
|
265 } |
|
266 |
|
267 TUsbServiceState serviceState; |
|
268 TInt err = iUsb.GetServiceState(serviceState); |
|
269 |
|
270 if (KErrNone != err) |
|
271 { |
|
272 FTRACE(FPrint(_L( "[USBOTGWATCHER]\tCUsbServiceControl::RunL error while getting service state %d" ), err)); |
|
273 //User::Leave(err); |
|
274 iObserver->UsbServiceControlReqCompletedL(err); |
|
275 return; |
|
276 } |
|
277 |
|
278 switch (serviceState) |
|
279 { |
|
280 case EUsbServiceIdle: // usb service stopped |
|
281 { |
|
282 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::RunL UsbServiceState == EUsbServiceIdle" ) ); |
|
283 |
|
284 if (iPersonalityId != 0) // during service stopping, requested to start it |
|
285 { |
|
286 FTRACE(FPrint(_L( "[USBOTGWATCHER]\tCUsbServiceControl::RunL Requested to start personality %d. Starting it." ), iPersonalityId)); |
|
287 TInt personalityId = iPersonalityId; |
|
288 iPersonalityId = 0; // reset |
|
289 err = Start(personalityId); |
|
290 if(KErrNone != err) |
|
291 { |
|
292 iObserver->UsbServiceControlReqCompletedL(err); |
|
293 } |
|
294 return; |
|
295 } |
|
296 |
|
297 // otherwise, we've done, notify |
|
298 iObserver->UsbServiceControlReqCompletedL(KErrNone); |
|
299 break; |
|
300 } |
|
301 |
|
302 case EUsbServiceStarted: |
|
303 { |
|
304 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Start UsbServiceState == EUsbServiceStarted" ) ); |
|
305 |
|
306 TInt currentPersonality(0); |
|
307 err = iUsb.GetCurrentPersonalityId(currentPersonality); |
|
308 if (KErrNone != err) |
|
309 { |
|
310 FTRACE(FPrint(_L( "[USBOTGWATCHER]\tCUsbServiceControl::RunL error while getting PersonalityId err = %d" ), err)); |
|
311 //User::Leave(err); |
|
312 iObserver->UsbServiceControlReqCompletedL(err); |
|
313 return; |
|
314 } |
|
315 |
|
316 if (iPersonalityId == currentPersonality) // already done |
|
317 { |
|
318 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Start Personality already started." ) ); |
|
319 iPersonalityId = 0; |
|
320 iObserver->UsbServiceControlReqCompletedL(KErrNone); |
|
321 return; |
|
322 } |
|
323 |
|
324 if (iPersonalityId == 0) // during service start requested to stop it |
|
325 { |
|
326 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Start Requested to stop personality. Stopping." ) ); |
|
327 err = Stop(); |
|
328 if(KErrNone != err) |
|
329 { |
|
330 FTRACE(FPrint(_L( "[USBOTGWATCHER]\tCUsbServiceControl::RunL error while stopping personality err = %d" ), err)); |
|
331 iObserver->UsbServiceControlReqCompletedL(err); |
|
332 } |
|
333 return; |
|
334 } |
|
335 |
|
336 // otherwise, during service start, requested to start it with another personality |
|
337 FTRACE(FPrint(_L( "[USBOTGWATCHER]\tCUsbServiceControl::RunL Requested to start personality %d. Starting it." ), iPersonalityId)); |
|
338 TInt personalityId = iPersonalityId; |
|
339 iPersonalityId = 0; // reset |
|
340 //User::LeaveIfError(Start(personalityId)); |
|
341 err = Start(personalityId); |
|
342 if(KErrNone != err) |
|
343 { |
|
344 FTRACE(FPrint(_L( "[USBOTGWATCHER]\tCUsbServiceControl::RunL error while starting personality err = %d" ), err)); |
|
345 iObserver->UsbServiceControlReqCompletedL(err); |
|
346 } |
|
347 break; |
|
348 } |
|
349 |
|
350 case EUsbServiceStarting: |
|
351 { |
|
352 // do exactly same as in EUsbServiceStopping |
|
353 // break statement is not required here |
|
354 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Start State == EUsbServiceStarting. Continue." ) ); |
|
355 } |
|
356 case EUsbServiceStopping: |
|
357 { |
|
358 // we are not interested in these states, just continue monitoring |
|
359 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Start State == EUsbServiceStopping. Continue." ) ); |
|
360 iUsb.ServiceStateNotification(iServiceState, iStatus); |
|
361 SetActive(); |
|
362 break; |
|
363 } |
|
364 case EUsbServiceFatalError: |
|
365 { |
|
366 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::Start UsbServiceState == EUsbServiceFatalError" ) ); |
|
367 //User::Leave(KErrGeneral); |
|
368 iObserver->UsbServiceControlReqCompletedL(KErrGeneral); |
|
369 break; |
|
370 } |
|
371 default: |
|
372 { |
|
373 Panic(EUnknownUsbServiceState); |
|
374 } |
|
375 } |
|
376 |
|
377 } |
|
378 |
|
379 // --------------------------------------------------------------------------- |
|
380 // |
|
381 // --------------------------------------------------------------------------- |
|
382 // |
|
383 TInt CUsbServiceControl::RunError(TInt aError) |
|
384 { |
|
385 FTRACE(FPrint(_L( "[USBOTGWATCHER]\tCUsbServiceControl::RunError aError %d" ), aError )); |
|
386 //TRAP_IGNORE(iObserver->UsbServiceControlReqCompletedL(aError)); |
|
387 |
|
388 return KErrNone; |
|
389 } |
|
390 |
|
391 // --------------------------------------------------------------------------- |
|
392 // |
|
393 // --------------------------------------------------------------------------- |
|
394 // |
|
395 void CUsbServiceControl::DoCancel() |
|
396 { |
|
397 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::DoCancel Cancelling interest to Usb TryStart.")) |
|
398 iUsb.CancelInterest(RUsb::ETryStart); |
|
399 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::DoCancel Cancelling interest to Usb TrySop.")) |
|
400 iUsb.CancelInterest(RUsb::ETryStop); |
|
401 FLOG( _L( "[USBOTGWATCHER]\tCUsbServiceControl::DoCancel Cancelling interest to usb states notifications.")) |
|
402 iUsb.ServiceStateNotificationCancel(); |
|
403 } |