|
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: Takes control of device&vendor-specific control messages over EP0 |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <usbman.h> |
|
20 |
|
21 #include "cusbdevcon.h" |
|
22 #include "cusbstatewatcher.h" |
|
23 #include "crequestshandler.h" |
|
24 #include "cstatemachine.h" |
|
25 #include "debug.h" |
|
26 |
|
27 // LITERALS |
|
28 _LIT( KUsbDevConName, "UsbDevCon" ); |
|
29 |
|
30 // --------------------------------------------------------------------------- |
|
31 // Two-phase construction |
|
32 // --------------------------------------------------------------------------- |
|
33 // |
|
34 CUsbDevCon* CUsbDevCon::NewLC() |
|
35 { |
|
36 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::NewLC" ) ); |
|
37 |
|
38 CUsbDevCon* self = new (ELeave) CUsbDevCon(); |
|
39 CleanupStack::PushL(self); |
|
40 self->ConstructL(); |
|
41 return self; |
|
42 } |
|
43 |
|
44 // --------------------------------------------------------------------------- |
|
45 // Two-phase construction |
|
46 // --------------------------------------------------------------------------- |
|
47 // |
|
48 CUsbDevCon* CUsbDevCon::NewL() |
|
49 { |
|
50 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::NewL" ) ); |
|
51 |
|
52 CUsbDevCon* self = CUsbDevCon::NewLC(); |
|
53 CleanupStack::Pop(self); |
|
54 return self; |
|
55 } |
|
56 |
|
57 // --------------------------------------------------------------------------- |
|
58 // Two-phase construction |
|
59 // --------------------------------------------------------------------------- |
|
60 // |
|
61 void CUsbDevCon::ConstructL() |
|
62 { |
|
63 |
|
64 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ConstructL" ) ); |
|
65 |
|
66 // usbc |
|
67 User::LeaveIfError(iLdd.Open(0)); |
|
68 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ConstructL RDevUsbcClient opened OK" ) ); |
|
69 |
|
70 // usb manager |
|
71 User::LeaveIfError(iUsbManager.Connect()); |
|
72 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ConstructL RUsb connected OK" ) ); |
|
73 |
|
74 // usb watcher |
|
75 User::LeaveIfError(iUsbWatcher.Connect()); |
|
76 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ConstructL RUsbWatcher connected OK" ) ); |
|
77 |
|
78 // device state watcher |
|
79 iUsbStateWatcher = CUsbStateWatcher::NewL(*this, iLdd); |
|
80 |
|
81 // Requests handler |
|
82 iRequestsHandler = CRequestsHandler::NewL(iLdd, iUsbWatcher, iUsbManager); |
|
83 |
|
84 // state machine |
|
85 iStateMachine = CStateMachine::NewL(*iRequestsHandler, iLdd); |
|
86 |
|
87 User::LeaveIfError(iShutdownTimer.CreateLocal()); |
|
88 |
|
89 // get usb state, and act accordingly to it |
|
90 TUsbcDeviceState usbcstate(EUsbcDeviceStateUndefined); |
|
91 iLdd.DeviceStatus(usbcstate); |
|
92 |
|
93 FTRACE(FPrint( |
|
94 _L("[USBDEVCON]\tCUsbDevCon::ConstructL: Usbc state = %d" ),usbcstate)); |
|
95 |
|
96 ActAccordinglyToUsbStateL(usbcstate); |
|
97 |
|
98 } |
|
99 |
|
100 // --------------------------------------------------------------------------- |
|
101 // Default construction |
|
102 // --------------------------------------------------------------------------- |
|
103 // |
|
104 CUsbDevCon::CUsbDevCon() : CActive(EPriorityStandard), |
|
105 iUsbStateWatcher(0), |
|
106 iStateMachine (0), |
|
107 iRequestsHandler(0), |
|
108 iPrevUsbState(EUsbcDeviceStateUndefined) |
|
109 { |
|
110 CActiveScheduler::Add(this); |
|
111 } |
|
112 |
|
113 // --------------------------------------------------------------------------- |
|
114 // Destruction |
|
115 // --------------------------------------------------------------------------- |
|
116 // |
|
117 CUsbDevCon::~CUsbDevCon() |
|
118 { |
|
119 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::~CUsbDevCon" ) ); |
|
120 |
|
121 Cancel(); |
|
122 |
|
123 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::~CUsbDevCon Cancel" ) ); |
|
124 |
|
125 delete iStateMachine; |
|
126 |
|
127 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::~CUsbDevCon StateMachine" ) ); |
|
128 |
|
129 delete iRequestsHandler; |
|
130 |
|
131 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::~CUsbDevCon RequestsHandler" ) ); |
|
132 |
|
133 delete iUsbStateWatcher; |
|
134 |
|
135 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::~CUsbDevCon UsbStateWatcher" ) ); |
|
136 |
|
137 iUsbWatcher.Close(); |
|
138 |
|
139 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::~CUsbDevCon UsbWatcher" ) ); |
|
140 |
|
141 iUsbManager.Close(); |
|
142 |
|
143 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::~CUsbDevCon UsbManager" ) ); |
|
144 |
|
145 iLdd.Close(); |
|
146 |
|
147 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::~CUsbDevCon LDD" ) ); |
|
148 |
|
149 iShutdownTimer.Close(); |
|
150 |
|
151 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::~CUsbDevCon Timer" ) ); |
|
152 |
|
153 } |
|
154 |
|
155 // --------------------------------------------------------------------------- |
|
156 // Acts accordingly to USB state |
|
157 // --------------------------------------------------------------------------- |
|
158 // |
|
159 void CUsbDevCon::ActAccordinglyToUsbStateL(TUsbcDeviceState aUsbcState) |
|
160 { |
|
161 |
|
162 switch (aUsbcState) |
|
163 { |
|
164 case EUsbcDeviceStateUndefined: |
|
165 { |
|
166 |
|
167 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ActAccordinglyToUsbStateL State: Undefined" ) ); |
|
168 |
|
169 StopL(); |
|
170 break; |
|
171 } |
|
172 |
|
173 case EUsbcDeviceStateAttached: |
|
174 { |
|
175 |
|
176 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ActAccordinglyToUsbStateL State: Attached" ) ); |
|
177 |
|
178 StartL(); |
|
179 break; |
|
180 } |
|
181 |
|
182 case EUsbcDeviceStatePowered: |
|
183 { |
|
184 |
|
185 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ActAccordinglyToUsbStateL State: Powered" ) ); |
|
186 |
|
187 StartL(); |
|
188 break; |
|
189 } |
|
190 |
|
191 case EUsbcDeviceStateDefault: |
|
192 { |
|
193 |
|
194 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ActAccordinglyToUsbStateL State: Default" ) ); |
|
195 |
|
196 StartL(); |
|
197 break; |
|
198 } |
|
199 |
|
200 case EUsbcDeviceStateAddress: |
|
201 { |
|
202 |
|
203 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ActAccordinglyToUsbStateL State: Addressed" ) ); |
|
204 |
|
205 StartL(); |
|
206 break; |
|
207 } |
|
208 |
|
209 case EUsbcDeviceStateConfigured: |
|
210 { |
|
211 |
|
212 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ActAccordinglyToUsbStateL State: Configured" ) ); |
|
213 |
|
214 if(iPrevUsbState == EUsbcDeviceStateSuspended) |
|
215 { |
|
216 ResumeL(); |
|
217 } |
|
218 else |
|
219 { |
|
220 StartL(); |
|
221 } |
|
222 |
|
223 break; |
|
224 } |
|
225 case EUsbcDeviceStateSuspended: |
|
226 { |
|
227 |
|
228 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ActAccordinglyToUsbStateL State: Suspended" ) ); |
|
229 |
|
230 break; |
|
231 } |
|
232 |
|
233 default: |
|
234 { |
|
235 |
|
236 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ActAccordinglyToUsbStateL State: ***Undefined***" ) ); |
|
237 |
|
238 StopL(); |
|
239 break; |
|
240 } |
|
241 } |
|
242 |
|
243 iPrevUsbState = aUsbcState; |
|
244 |
|
245 // listen to USB states change |
|
246 iUsbStateWatcher->Activate(); |
|
247 |
|
248 } |
|
249 |
|
250 // --------------------------------------------------------------------------- |
|
251 // Timer is completed |
|
252 // --------------------------------------------------------------------------- |
|
253 // |
|
254 void CUsbDevCon::RunL() |
|
255 { |
|
256 FTRACE(FPrint( |
|
257 _L("[USBDEVCON]\tCUsbDevCon::RunL: iStatus = %d" ),iStatus.Int())); |
|
258 |
|
259 if(KErrNone == iStatus.Int()) |
|
260 { |
|
261 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::RunL Exiting usbdevcon" ) ); |
|
262 |
|
263 // Shutdown timer is finished, exit program |
|
264 CUsbDevCon:: ~CUsbDevCon(); // destruct resources |
|
265 User::Exit(KErrNone); |
|
266 } |
|
267 } |
|
268 |
|
269 // --------------------------------------------------------------------------- |
|
270 // Cancellation |
|
271 // --------------------------------------------------------------------------- |
|
272 // |
|
273 void CUsbDevCon::DoCancel() |
|
274 { |
|
275 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::DoCancel" ) ) |
|
276 iShutdownTimer.Cancel(); |
|
277 } |
|
278 |
|
279 // ---------------------------------------------------------------------------- |
|
280 // Standard active object error function. |
|
281 // ---------------------------------------------------------------------------- |
|
282 // |
|
283 TInt CUsbDevCon::RunError( TInt /*aError*/ ) |
|
284 { |
|
285 return KErrNone; |
|
286 } |
|
287 |
|
288 // --------------------------------------------------------------------------- |
|
289 // Starts UsbDevCon services |
|
290 // --------------------------------------------------------------------------- |
|
291 // |
|
292 void CUsbDevCon::StartL() |
|
293 { |
|
294 |
|
295 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::StartL" ) ); |
|
296 |
|
297 if(!iStateMachine->IsStarted()) |
|
298 { |
|
299 // set device control |
|
300 User::LeaveIfError(iLdd.SetDeviceControl()); |
|
301 |
|
302 // start state machine |
|
303 iStateMachine->Start(); |
|
304 |
|
305 } |
|
306 |
|
307 // Cancel shutdown timer, if it is started |
|
308 iShutdownTimer.Cancel(); |
|
309 } |
|
310 |
|
311 // --------------------------------------------------------------------------- |
|
312 // Stops UsbDevCon services |
|
313 // --------------------------------------------------------------------------- |
|
314 // |
|
315 void CUsbDevCon::StopL() |
|
316 { |
|
317 |
|
318 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::StopL" ) ); |
|
319 |
|
320 if(iStateMachine->IsStarted()) |
|
321 { |
|
322 |
|
323 // stop state machine |
|
324 iStateMachine->Stop(); |
|
325 |
|
326 // release device control |
|
327 User::LeaveIfError(iLdd.ReleaseDeviceControl()); |
|
328 |
|
329 } |
|
330 |
|
331 if(!IsActive()) // not waiting for timer |
|
332 { |
|
333 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::StopL Starting timer" ) ); |
|
334 // run timer |
|
335 iShutdownTimer.Cancel(); |
|
336 |
|
337 // RunL will be called after KInactiveTimeForShutDown milliseconds |
|
338 iShutdownTimer.After(iStatus, TTimeIntervalMicroSeconds32(KInactiveTimeForShutDown)); |
|
339 SetActive(); |
|
340 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::StopL Timer is started" ) ); |
|
341 } |
|
342 } |
|
343 |
|
344 // --------------------------------------------------------------------------- |
|
345 // Resumes UsbDevCon services |
|
346 // --------------------------------------------------------------------------- |
|
347 // |
|
348 void CUsbDevCon::ResumeL() |
|
349 { |
|
350 |
|
351 FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ResumeL" ) ); |
|
352 |
|
353 // Resume state machine |
|
354 StartL(); |
|
355 |
|
356 } |
|
357 |
|
358 // ---------------------------------------------------------------------------- |
|
359 // Constructs and installs the active scheduler, constructs UsbDevCon object. |
|
360 // ---------------------------------------------------------------------------- |
|
361 // |
|
362 static void StartUsbDevConL() |
|
363 { |
|
364 |
|
365 FLOG( _L( "[USBDEVCON]\tStartUsbDevConL" ) ); |
|
366 |
|
367 // Construct and install the active scheduler |
|
368 CActiveScheduler *myScheduler = new ( ELeave ) CActiveScheduler(); |
|
369 |
|
370 // Push onto the cleanup stack |
|
371 CleanupStack::PushL( myScheduler ); |
|
372 |
|
373 // Install as the active scheduler |
|
374 CActiveScheduler::Install( myScheduler ); |
|
375 |
|
376 CUsbDevCon* instance = CUsbDevCon::NewLC(); |
|
377 |
|
378 RProcess::Rendezvous(KErrNone); // signal to starter process, that usbdevcon started OK or failed to start |
|
379 |
|
380 FLOG( _L( "[USBDEVCON]\tStartUsbDevConL Usbdevcon is started successfully" ) ); |
|
381 |
|
382 // returns only when UsbDevCon closing |
|
383 CActiveScheduler::Start(); |
|
384 |
|
385 CleanupStack::PopAndDestroy( instance ); |
|
386 CleanupStack::PopAndDestroy( myScheduler ); |
|
387 } |
|
388 |
|
389 // --------------------------------------------------------------------------- |
|
390 // Main function of the application executable. |
|
391 // --------------------------------------------------------------------------- |
|
392 // |
|
393 GLDEF_C TInt E32Main() |
|
394 { |
|
395 TInt err; |
|
396 |
|
397 // rename the thread so it is easy to find the panic application |
|
398 err = User::RenameThread(KUsbDevConName); |
|
399 |
|
400 if(KErrNone != err) |
|
401 { |
|
402 return err; |
|
403 } |
|
404 |
|
405 __UHEAP_MARK; |
|
406 |
|
407 // create clean-up stack |
|
408 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
409 |
|
410 TRAP( err, StartUsbDevConL() ); |
|
411 |
|
412 delete cleanup; // destroy clean-up stack |
|
413 __UHEAP_MARKEND; |
|
414 |
|
415 return err; |
|
416 } |
|
417 |
|
418 |
|
419 |