author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:57:48 +0300 | |
changeset 88 | c3d148cdbed2 |
parent 35 | 9d8b04ca6939 |
permissions | -rw-r--r-- |
35 | 1 |
/* |
2 |
* Copyright (c) 2007 - 2010 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 |
FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ActAccordinglyToUsbStateL State: Attached, ignored" ) ); |
|
176 |
||
177 |
break; |
|
178 |
} |
|
179 |
||
180 |
case EUsbcDeviceStateSuspended: |
|
181 |
{ |
|
182 |
FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ActAccordinglyToUsbStateL State: Suspended" ) ); |
|
183 |
// NO break here |
|
184 |
} |
|
185 |
case EUsbcDeviceStatePowered: |
|
186 |
{ |
|
187 |
FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ActAccordinglyToUsbStateL State: Powered" ) ); |
|
188 |
||
189 |
// In powered or suspended state, we are not allowed to do any data |
|
190 |
// communication. Hence if there are pending read/write requests, |
|
191 |
// we need cancel them. |
|
192 |
// Not call StopL() here because we do not want to shut down this |
|
193 |
// process so earlier but in Undefined state. |
|
194 |
if ( iStateMachine->IsStarted() ) |
|
195 |
{ |
|
196 |
iStateMachine->Stop(); |
|
197 |
// release device control |
|
198 |
User::LeaveIfError(iLdd.ReleaseDeviceControl()); |
|
199 |
} |
|
200 |
break; |
|
201 |
} |
|
202 |
||
203 |
case EUsbcDeviceStateDefault: |
|
204 |
{ |
|
205 |
// The request will only be started from default state. |
|
206 |
// If it has been started already, nothing will be done. |
|
207 |
FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ActAccordinglyToUsbStateL State: Default" ) ); |
|
208 |
||
209 |
StartL(); |
|
210 |
break; |
|
211 |
} |
|
212 |
||
213 |
case EUsbcDeviceStateAddress: |
|
214 |
{ |
|
215 |
FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ActAccordinglyToUsbStateL State: Addressed" ) ); |
|
216 |
||
217 |
StartL(); |
|
218 |
break; |
|
219 |
} |
|
220 |
||
221 |
case EUsbcDeviceStateConfigured: |
|
222 |
{ |
|
223 |
FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ActAccordinglyToUsbStateL State: Configured" ) ); |
|
224 |
||
225 |
StartL(); |
|
226 |
||
227 |
break; |
|
228 |
} |
|
229 |
default: |
|
230 |
{ |
|
231 |
||
232 |
FLOG( _L( "[USBDEVCON]\tCUsbDevCon::ActAccordinglyToUsbStateL State: ***Unknown***" ) ); |
|
233 |
||
234 |
StopL(); |
|
235 |
break; |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
iPrevUsbState = aUsbcState; |
|
240 |
||
241 |
// listen to USB states change |
|
242 |
iUsbStateWatcher->Activate(); |
|
243 |
||
244 |
} |
|
245 |
||
246 |
// --------------------------------------------------------------------------- |
|
247 |
// Timer is completed |
|
248 |
// --------------------------------------------------------------------------- |
|
249 |
// |
|
250 |
void CUsbDevCon::RunL() |
|
251 |
{ |
|
252 |
FTRACE(FPrint( |
|
253 |
_L("[USBDEVCON]\tCUsbDevCon::RunL: iStatus = %d" ),iStatus.Int())); |
|
254 |
||
255 |
if(KErrNone == iStatus.Int()) |
|
256 |
{ |
|
257 |
FLOG( _L( "[USBDEVCON]\tCUsbDevCon::RunL Exiting usbdevcon" ) ); |
|
258 |
||
259 |
// Shutdown timer is finished, exit program |
|
260 |
CActiveScheduler::Stop(); // destruct resources |
|
261 |
} |
|
262 |
} |
|
263 |
||
264 |
// --------------------------------------------------------------------------- |
|
265 |
// Cancellation |
|
266 |
// --------------------------------------------------------------------------- |
|
267 |
// |
|
268 |
void CUsbDevCon::DoCancel() |
|
269 |
{ |
|
270 |
FLOG( _L( "[USBDEVCON]\tCUsbDevCon::DoCancel" ) ) |
|
271 |
iShutdownTimer.Cancel(); |
|
272 |
} |
|
273 |
||
274 |
// ---------------------------------------------------------------------------- |
|
275 |
// Standard active object error function. |
|
276 |
// ---------------------------------------------------------------------------- |
|
277 |
// |
|
278 |
TInt CUsbDevCon::RunError( TInt /*aError*/ ) |
|
279 |
{ |
|
280 |
return KErrNone; |
|
281 |
} |
|
282 |
||
283 |
// --------------------------------------------------------------------------- |
|
284 |
// Starts UsbDevCon services |
|
285 |
// --------------------------------------------------------------------------- |
|
286 |
// |
|
287 |
void CUsbDevCon::StartL() |
|
288 |
{ |
|
289 |
||
290 |
FLOG( _L( "[USBDEVCON]\tCUsbDevCon::StartL" ) ); |
|
291 |
||
292 |
if(!iStateMachine->IsStarted()) |
|
293 |
{ |
|
294 |
// set device control |
|
295 |
User::LeaveIfError(iLdd.SetDeviceControl()); |
|
296 |
||
297 |
// start state machine |
|
88
c3d148cdbed2
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
35
diff
changeset
|
298 |
iStateMachine->StartL(); |
35 | 299 |
|
300 |
} |
|
301 |
||
302 |
// Cancel shutdown timer, if it is started |
|
303 |
iShutdownTimer.Cancel(); |
|
304 |
} |
|
305 |
||
306 |
// --------------------------------------------------------------------------- |
|
307 |
// Stops UsbDevCon services |
|
308 |
// --------------------------------------------------------------------------- |
|
309 |
// |
|
310 |
void CUsbDevCon::StopL() |
|
311 |
{ |
|
312 |
||
313 |
FLOG( _L( "[USBDEVCON]\tCUsbDevCon::StopL" ) ); |
|
314 |
||
315 |
if(iStateMachine->IsStarted()) |
|
316 |
{ |
|
317 |
||
318 |
// stop state machine |
|
319 |
iStateMachine->Stop(); |
|
320 |
||
321 |
// release device control |
|
322 |
User::LeaveIfError(iLdd.ReleaseDeviceControl()); |
|
323 |
||
324 |
} |
|
325 |
||
326 |
if(!IsActive()) // not waiting for timer |
|
327 |
{ |
|
328 |
FLOG( _L( "[USBDEVCON]\tCUsbDevCon::StopL Starting timer" ) ); |
|
329 |
// run timer |
|
330 |
iShutdownTimer.Cancel(); |
|
331 |
||
332 |
// RunL will be called after KInactiveTimeForShutDown milliseconds |
|
333 |
iShutdownTimer.After(iStatus, TTimeIntervalMicroSeconds32(KInactiveTimeForShutDown)); |
|
334 |
SetActive(); |
|
335 |
FLOG( _L( "[USBDEVCON]\tCUsbDevCon::StopL Timer is started" ) ); |
|
336 |
} |
|
337 |
} |
|
338 |
||
339 |
||
340 |
// ---------------------------------------------------------------------------- |
|
341 |
// Constructs and installs the active scheduler, constructs UsbDevCon object. |
|
342 |
// ---------------------------------------------------------------------------- |
|
343 |
// |
|
344 |
static void StartUsbDevConL() |
|
345 |
{ |
|
346 |
||
347 |
FLOG( _L( "[USBDEVCON]\tStartUsbDevConL" ) ); |
|
348 |
||
349 |
// Construct and install the active scheduler |
|
350 |
CActiveScheduler *myScheduler = new ( ELeave ) CActiveScheduler(); |
|
351 |
||
352 |
// Push onto the cleanup stack |
|
353 |
CleanupStack::PushL( myScheduler ); |
|
354 |
||
355 |
// Install as the active scheduler |
|
356 |
CActiveScheduler::Install( myScheduler ); |
|
357 |
||
358 |
CUsbDevCon* instance = CUsbDevCon::NewLC(); |
|
359 |
||
360 |
RProcess::Rendezvous(KErrNone); // signal to starter process, that usbdevcon started OK or failed to start |
|
361 |
||
362 |
FLOG( _L( "[USBDEVCON]\tStartUsbDevConL Usbdevcon is started successfully" ) ); |
|
363 |
||
364 |
// returns only when UsbDevCon closing |
|
365 |
CActiveScheduler::Start(); |
|
366 |
||
367 |
CleanupStack::PopAndDestroy( instance ); |
|
368 |
CleanupStack::PopAndDestroy( myScheduler ); |
|
369 |
} |
|
370 |
||
371 |
// --------------------------------------------------------------------------- |
|
372 |
// Main function of the application executable. |
|
373 |
// --------------------------------------------------------------------------- |
|
374 |
// |
|
375 |
GLDEF_C TInt E32Main() |
|
376 |
{ |
|
377 |
TInt err; |
|
378 |
||
379 |
// rename the thread so it is easy to find the panic application |
|
380 |
err = User::RenameThread(KUsbDevConName); |
|
381 |
||
382 |
if(KErrNone != err) |
|
383 |
{ |
|
384 |
return err; |
|
385 |
} |
|
386 |
||
387 |
__UHEAP_MARK; |
|
388 |
||
389 |
// create clean-up stack |
|
390 |
CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
391 |
||
392 |
TRAP( err, StartUsbDevConL() ); |
|
393 |
||
394 |
delete cleanup; // destroy clean-up stack |
|
395 |
__UHEAP_MARKEND; |
|
396 |
||
397 |
return err; |
|
398 |
} |