|
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: OTG watcher state machine owner |
|
15 * |
|
16 */ |
|
17 |
|
18 #ifndef C_USBOTGWATCHER_H |
|
19 #define C_USBOTGWATCHER_H |
|
20 |
|
21 #include <e32base.h> |
|
22 |
|
23 #include <usbotgdefs.h> |
|
24 |
|
25 #include "definitions.h" |
|
26 |
|
27 #ifndef STIF |
|
28 #include <usbman.h> |
|
29 #include "cusbidpinobserver.h" |
|
30 #include "cusbvbusobserver.h" |
|
31 #include "cusbotgstateobserver.h" |
|
32 #include "cusbbusactivityobserver.h" |
|
33 #include "cusbhosteventnotificationobserver.h" |
|
34 #include "cusbmessagenotificationobserver.h" |
|
35 #else |
|
36 #include "mockusbman.h" |
|
37 #include "mockcusbidpinobserver.h" |
|
38 #include "mockcusbvbusobserver.h" |
|
39 #include "mockcusbotgstateobserver.h" |
|
40 #include "mockcusbbusactivityobserver.h" |
|
41 #include "mockcusbhosteventnotificationobserver.h" |
|
42 #include "mockcusbmessagenotificationobserver.h" |
|
43 #endif |
|
44 |
|
45 #include "cusbservicecontrol.h" |
|
46 |
|
47 class CUsbState; |
|
48 class CUsbStateHostHandle; |
|
49 |
|
50 class CUsbNotifManager; |
|
51 |
|
52 /** |
|
53 * Observers for usb otg watcher state nachine |
|
54 * Observers gets feedback by implementing this interface |
|
55 */ |
|
56 class MUsbOtgWatcherStateObserver |
|
57 { |
|
58 |
|
59 public: |
|
60 |
|
61 /** |
|
62 * Observer must implement this interface |
|
63 * which is called back when state changes |
|
64 * @param aState new state id |
|
65 */ |
|
66 virtual void OtgWatcherStateChangedL(TUsbStateIds aState) = 0; |
|
67 }; |
|
68 |
|
69 /** |
|
70 * UsbWatcher main class |
|
71 * Implements states machines and owns them |
|
72 * |
|
73 */ |
|
74 class CUsbOtgWatcher : public CBase, |
|
75 MUsbIdPinObserver, |
|
76 MUsbVBusObserver, |
|
77 MUsbOtgStateObserver, |
|
78 MUsbBusActivityObserver, |
|
79 MUsbHostEventNotificationObserver, |
|
80 MUsbMessageNotificationObserver, |
|
81 MUsbServiceControlObserver |
|
82 { |
|
83 |
|
84 // Making CUsbState friend secures changing states |
|
85 // Methods to change state to be called only by CUsbStates children |
|
86 friend class CUsbState; |
|
87 friend class CtUsbOtgWatcher; |
|
88 |
|
89 public: |
|
90 |
|
91 /** |
|
92 * Two-phased constructor. |
|
93 * @param aUsb usbman API |
|
94 */ |
|
95 IMPORT_C |
|
96 static CUsbOtgWatcher* NewL(RUsb& aUsb); |
|
97 |
|
98 /** |
|
99 * Check ID-Pin state |
|
100 * @return ETrue if the ID-Pin is present (A-Device) |
|
101 */ |
|
102 IMPORT_C |
|
103 TBool IsDeviceA(); |
|
104 |
|
105 /** |
|
106 * Destructor. |
|
107 */ |
|
108 IMPORT_C virtual ~CUsbOtgWatcher(); |
|
109 |
|
110 /** |
|
111 * Sets personality |
|
112 * @param aStatus for async. request |
|
113 * @param aPersonalityID to be set |
|
114 */ |
|
115 void SetPersonalityL(TRequestStatus& aStatus, TInt aPersonalityID); |
|
116 |
|
117 /** |
|
118 * Cancels setting personality async. |
|
119 */ |
|
120 void CancelSetPersonalityL(); |
|
121 |
|
122 /** |
|
123 * |
|
124 * Sets previous personality |
|
125 * @param aStatus Asynchronously |
|
126 */ |
|
127 void SetPreviousPersonalityL(TRequestStatus& aStatus); |
|
128 |
|
129 /** |
|
130 * Sets previous personality |
|
131 */ |
|
132 void SetPreviousPersonalityL(); |
|
133 |
|
134 /** |
|
135 * Cancels async. set prev personality request |
|
136 */ |
|
137 void CancelSetPreviousPersonalityL(); |
|
138 |
|
139 /** |
|
140 * Sets prev. personality on disconnecting device |
|
141 */ |
|
142 void SetPreviousPreviousPersonalityOnDisconnectL(); |
|
143 |
|
144 /** |
|
145 * Policy for session start defined here |
|
146 */ |
|
147 TBool CanStartSessionL(); |
|
148 |
|
149 /********************************************************************************/ |
|
150 // Events from observers come here |
|
151 |
|
152 // From IdPin observer |
|
153 /** |
|
154 * Id Pin OFF |
|
155 */ |
|
156 void IdPinOffL(); |
|
157 /** |
|
158 * Id Pin On |
|
159 */ |
|
160 void IdPinOnL(); |
|
161 /** |
|
162 * Id Pin On |
|
163 * @param aError |
|
164 */ |
|
165 void IdPinErrorL(TInt aError); |
|
166 |
|
167 // From VBus observer |
|
168 /** |
|
169 * VBus down |
|
170 */ |
|
171 void VBusDownL(); |
|
172 /** |
|
173 * VBus UP |
|
174 */ |
|
175 void VBusUpL(); |
|
176 /** |
|
177 * VBus observer error |
|
178 * @param aError error code |
|
179 */ |
|
180 void VBusObserverErrorL(TInt aError); |
|
181 |
|
182 // From OTG state observer |
|
183 /** |
|
184 * Became to Idle when A |
|
185 */ |
|
186 void AIdleL(); |
|
187 /** |
|
188 * Became a Host when A |
|
189 */ |
|
190 void AHostL(); |
|
191 /** |
|
192 * Became a peripheral when A |
|
193 */ |
|
194 void APeripheralL(); |
|
195 /** |
|
196 * VBus error happen |
|
197 */ |
|
198 void AVBusErrorL(); |
|
199 /** |
|
200 * Became to Idle when B |
|
201 */ |
|
202 void BIdleL(); |
|
203 /** |
|
204 * Became a peripheral when B |
|
205 */ |
|
206 void BPeripheralL(); |
|
207 /** |
|
208 * Became a Host when B |
|
209 */ |
|
210 void BHostL(); |
|
211 |
|
212 /** |
|
213 * Error handler |
|
214 * @param aError error code |
|
215 */ |
|
216 void OtgStateErrorL(TInt aError); |
|
217 |
|
218 // From bus activity observer |
|
219 /** |
|
220 * Bus is in idle |
|
221 */ |
|
222 void BusIdleL(); |
|
223 /** |
|
224 * Bus active |
|
225 */ |
|
226 void BusActiveL(); |
|
227 /** |
|
228 * error |
|
229 * @param aError error happened |
|
230 */ |
|
231 void BusActivityErrorL(TInt aError); |
|
232 |
|
233 // From Host Event notification observer |
|
234 /** |
|
235 * Device is attached |
|
236 * @param aInfo Device event data |
|
237 */ |
|
238 void DeviceAttachedL(TDeviceEventInformation aInfo); |
|
239 /** |
|
240 * Device is detached |
|
241 * @param aInfo Device event data |
|
242 */ |
|
243 void DeviceDetachedL(TDeviceEventInformation aInfo); |
|
244 /** |
|
245 * Drivers successfully loaded |
|
246 * @param aInfo Device event data |
|
247 */ |
|
248 void DriverLoadSuccessL(TDeviceEventInformation aInfo); |
|
249 /** |
|
250 * Drivers loaded partially |
|
251 * @param aInfo Device event data |
|
252 */ |
|
253 void DriverLoadPartialSuccessL(TDeviceEventInformation aInfo); |
|
254 /** |
|
255 * Drivers loading failed |
|
256 * @param aInfo Device event data |
|
257 */ |
|
258 void DriverLoadFailureL(TDeviceEventInformation aInfo); |
|
259 /** |
|
260 * Error happened during observing |
|
261 * @param aError error code |
|
262 */ |
|
263 void HostEventNotificationErrorL(TInt aError); |
|
264 |
|
265 // From message notification observer |
|
266 /** |
|
267 * Message received |
|
268 * @param aMassage message id |
|
269 */ |
|
270 void MessageNotificationReceivedL(TInt aMessage); |
|
271 /** |
|
272 * Connected to hub in wrong level |
|
273 */ |
|
274 void BadHubPositionL(); |
|
275 /** |
|
276 * VBus error happened |
|
277 */ |
|
278 void VBusErrorL(); |
|
279 /** |
|
280 * SRP request received |
|
281 */ |
|
282 void SrpReceivedL(); |
|
283 /** |
|
284 * Session request received |
|
285 */ |
|
286 void SessionRequestedL(); |
|
287 /** |
|
288 * Error handler |
|
289 * @param error code |
|
290 */ |
|
291 void MessageNotificationErrorL(TInt aError); |
|
292 |
|
293 // From CUsbServiceControl |
|
294 /** |
|
295 * called when request for usb services is completed |
|
296 * if there were few requests in a row, during operation, |
|
297 * only the last one's completion is notified |
|
298 * @param aError error code |
|
299 */ |
|
300 void UsbServiceControlReqCompletedL(TInt aError); |
|
301 |
|
302 /******************************************************************************* */ |
|
303 |
|
304 // getters |
|
305 |
|
306 /** |
|
307 * @return IdPin observer |
|
308 */ |
|
309 CUsbIdPinObserver* IdPinObserver() const; |
|
310 /** |
|
311 * @return VBus observer |
|
312 */ |
|
313 CUsbVBusObserver* VBusObserver() const; |
|
314 /** |
|
315 * @return OTG state observer |
|
316 */ |
|
317 CUsbOtgStateObserver* OtgStateObserver() const; |
|
318 /** |
|
319 * @return Bus Activity observer |
|
320 */ |
|
321 CUsbBusActivityObserver* BusActivityObserver() const; |
|
322 /** |
|
323 * @return HostEvent notification observer |
|
324 */ |
|
325 CUsbHostEventNotificationObserver* HostEventNotificationObserver() const; |
|
326 /** |
|
327 * @return Message notification observer |
|
328 */ |
|
329 CUsbMessageNotificationObserver* MessageNotificationObserver() const; |
|
330 /** |
|
331 * @return usbman |
|
332 */ |
|
333 RUsb& Usb(); |
|
334 /** |
|
335 * @return current device state |
|
336 */ |
|
337 CUsbState* CurrentState() const; |
|
338 /** |
|
339 * @return current host state |
|
340 */ |
|
341 CUsbState* CurrentHostState() const; |
|
342 /** |
|
343 * @param aStateId State Id |
|
344 * @return state object by its id |
|
345 */ |
|
346 CUsbState* State(TUsbStateIds aStateId) const; |
|
347 /** |
|
348 * @return notif manager |
|
349 */ |
|
350 CUsbNotifManager* NotifManager(); |
|
351 /** |
|
352 * @return Handle state |
|
353 * @param aStateId state id |
|
354 */ |
|
355 CUsbStateHostHandle* HostHandle(TUsbStateIds aStateId) const; |
|
356 |
|
357 /** |
|
358 * Handles problems in host functioning |
|
359 * @param aWhatKindOf problem Id to be handled |
|
360 * @param aInState state id where to handle |
|
361 */ |
|
362 void HandleHostProblemL(TInt aWhatKindOf, TUsbStateIds aInState); |
|
363 |
|
364 /** |
|
365 * Add observer to USb Otg state machine |
|
366 * @param aObserver Observer |
|
367 */ |
|
368 void SubscribeL(MUsbOtgWatcherStateObserver& aObserver); |
|
369 |
|
370 /** |
|
371 * Remove observer from UsbOtg state observer |
|
372 * @param aObserver Observer |
|
373 */ |
|
374 void UnsubscribeL(MUsbOtgWatcherStateObserver& aObserver); |
|
375 |
|
376 /** |
|
377 * Used for test purposes |
|
378 */ |
|
379 void PrintStateToLog(); |
|
380 |
|
381 private: |
|
382 |
|
383 /** |
|
384 * Default constructor |
|
385 * @param aUsb usbman API |
|
386 */ |
|
387 CUsbOtgWatcher(RUsb& aUsb); |
|
388 |
|
389 /** |
|
390 * 2nd phase construction |
|
391 */ |
|
392 void ConstructL(); |
|
393 |
|
394 /** |
|
395 * Starts session |
|
396 */ |
|
397 void StartSessionL(); |
|
398 |
|
399 /** |
|
400 * Changes device's state machine state |
|
401 * |
|
402 * @param aNewStateId New state id |
|
403 */ |
|
404 void ChangeStateL(TUsbStateIds aNewStateId); |
|
405 |
|
406 /** |
|
407 * Changes host's state machine state |
|
408 * |
|
409 * @param aNewStateId new host state (id) |
|
410 */ |
|
411 void ChangeHostStateL(TUsbStateIds aNewStateId); |
|
412 |
|
413 /** |
|
414 * Used for test purposes |
|
415 * @return KErrNone if test is OK, otherwise errorcode |
|
416 */ |
|
417 TInt SelfTestL(); |
|
418 |
|
419 private: |
|
420 // data |
|
421 |
|
422 /** |
|
423 * usbman API |
|
424 */ |
|
425 RUsb& iUsb; |
|
426 |
|
427 /** |
|
428 * Personality to be set in device role |
|
429 */ |
|
430 TInt iPersonalityId; |
|
431 |
|
432 /** |
|
433 * Device state machine |
|
434 * Own. |
|
435 */ |
|
436 CUsbState* iState; |
|
437 |
|
438 /** |
|
439 * Host state machine |
|
440 * Own. |
|
441 */ |
|
442 CUsbState* iHostState; |
|
443 |
|
444 /** |
|
445 * Id pin Observer |
|
446 * Own. |
|
447 */ |
|
448 CUsbIdPinObserver* iIdPinObserver; |
|
449 |
|
450 /** |
|
451 * vbus observer |
|
452 * Own. |
|
453 */ |
|
454 CUsbVBusObserver* iVBusObserver; |
|
455 |
|
456 /** |
|
457 * otg state observer |
|
458 * Own. |
|
459 */ |
|
460 CUsbOtgStateObserver* iOtgStateObserver; |
|
461 |
|
462 /** |
|
463 * bus activity observer |
|
464 * Own. |
|
465 */ |
|
466 CUsbBusActivityObserver* iBusActivityObserver; |
|
467 |
|
468 /** |
|
469 * host event notification observer |
|
470 * Own. |
|
471 */ |
|
472 CUsbHostEventNotificationObserver* iHostEventNotificationObserver; |
|
473 |
|
474 /** |
|
475 * message notification observer |
|
476 * Own. |
|
477 */ |
|
478 CUsbMessageNotificationObserver* iMessageNotificationObserver; |
|
479 |
|
480 /** |
|
481 * All states objects kept here |
|
482 * Own. |
|
483 */ |
|
484 RPointerArray<CUsbState> iStates; |
|
485 |
|
486 /** |
|
487 * notif manager |
|
488 * Own. |
|
489 */ |
|
490 CUsbNotifManager* iNotifManager; |
|
491 |
|
492 /** |
|
493 * usb service control |
|
494 * Starts and stops usb service, changes personalities |
|
495 * Own. |
|
496 */ |
|
497 CUsbServiceControl* iUsbServiceControl; |
|
498 |
|
499 /** |
|
500 * The observer reports state changes to its own observers |
|
501 * Not Own |
|
502 */ |
|
503 RPointerArray<MUsbOtgWatcherStateObserver> iOtgStateObservers; |
|
504 |
|
505 /** |
|
506 * The observer reports state changes to its own observers |
|
507 * Not Own |
|
508 */ |
|
509 CUsbServiceControl::TUsbServiceRequest iUsbServiceRequest; |
|
510 }; |
|
511 |
|
512 #endif // C_USBOTGWATCHER_H |