|
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of the License "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // Implements a Session of a Symbian OS server for the RUsbMassStorage API |
|
15 // |
|
16 // |
|
17 |
|
18 /** |
|
19 @file |
|
20 @internalTechnology |
|
21 */ |
|
22 |
|
23 #include <e32base.h> |
|
24 |
|
25 #include "msctypes.h" |
|
26 #include "shared.h" |
|
27 #include "msgservice.h" |
|
28 #include "cusbhostmslogicalunit.h" |
|
29 #include "cusbhostmsdevice.h" |
|
30 #include "msdebug.h" |
|
31 #include "cusbhostmssession.h" |
|
32 #include "cusbhostmsdevicethread.h" |
|
33 #include "debug.h" |
|
34 |
|
35 /** |
|
36 Constructor |
|
37 */ |
|
38 TDeviceHandler::TDeviceHandler(CUsbHostMsDevice& aDevice) |
|
39 : iDevice(aDevice) |
|
40 { |
|
41 __MSFNLOG |
|
42 } |
|
43 |
|
44 |
|
45 /** |
|
46 Services messages directed to a device. Messages associated with a single |
|
47 Logical Unit contained by the device are forwarded to the Logical Unit |
|
48 Handler. |
|
49 |
|
50 @param aMessage |
|
51 */ |
|
52 void TDeviceHandler::HandleMessageL(const RMessage2& aMessage) |
|
53 { |
|
54 __MSFNLOG |
|
55 TLun lun = iDevice.GetAndSetLunL(aMessage); |
|
56 |
|
57 switch (aMessage.Function()) |
|
58 { |
|
59 case EUsbHostMsSuspendLun: |
|
60 iDevice.SuspendLunL(lun); |
|
61 break; |
|
62 case EUsbHostMsUnRegisterLun: |
|
63 iDevice.RemoveLunL(lun); |
|
64 break; |
|
65 default: |
|
66 // Try Logical Unit Handler |
|
67 CUsbHostMsLogicalUnit& lu = iDevice.GetLuL(lun); |
|
68 TLogicalUnitHandler luHandler(lu); |
|
69 luHandler.HandleMessageL(aMessage); |
|
70 break; |
|
71 } |
|
72 } |
|
73 |
|
74 |
|
75 /** |
|
76 Constructor |
|
77 |
|
78 @param aLu Reference to the logical unit object |
|
79 */ |
|
80 TLogicalUnitHandler::TLogicalUnitHandler(CUsbHostMsLogicalUnit& aLu) |
|
81 : iLu(aLu) |
|
82 { |
|
83 __MSFNLOG |
|
84 } |
|
85 |
|
86 |
|
87 /** |
|
88 Services messages directed to a specific Logical Unit. Unrecognized messages |
|
89 will cause a PANIC. |
|
90 |
|
91 @param aMessage |
|
92 */ |
|
93 void TLogicalUnitHandler::HandleMessageL(const RMessage2& aMessage) |
|
94 { |
|
95 __MSFNLOG |
|
96 switch (aMessage.Function()) |
|
97 { |
|
98 case EUsbHostMsNotifyChange: |
|
99 iLu.NotifyChange(aMessage); |
|
100 break; |
|
101 case EUsbHostMsCancelChangeNotifier: |
|
102 iLu.CancelChangeNotifierL(); |
|
103 break; |
|
104 case EUsbHostMsForceRemount: |
|
105 iLu.ForceCompleteNotifyChangeL(); |
|
106 break; |
|
107 case EUsbHostMsRead: |
|
108 iLu.ReadL(aMessage); |
|
109 break; |
|
110 case EUsbHostMsWrite: |
|
111 iLu.WriteL(aMessage); |
|
112 break; |
|
113 case EUsbHostMsErase: |
|
114 iLu.EraseL(aMessage); |
|
115 break; |
|
116 case EUsbHostMsCapacity: |
|
117 iLu.CapsL(aMessage); |
|
118 break; |
|
119 default: |
|
120 aMessage.Panic(KUsbHostMsSrvPncCat, EUsbMsPanicIllegalIPC); |
|
121 break; |
|
122 } |
|
123 } |
|
124 |
|
125 |
|
126 void CUsbHostMsDeviceThread::DoStartServerL(TAny* aPtr) |
|
127 { |
|
128 __MSFNSLOG |
|
129 CActiveScheduler* s = new(ELeave) CActiveScheduler; |
|
130 CActiveScheduler::Install(s); |
|
131 |
|
132 CUsbHostMsDeviceThread* iThread = (CUsbHostMsDeviceThread*)aPtr; |
|
133 CActiveScheduler::Add(iThread); |
|
134 |
|
135 iThread->Start(); |
|
136 |
|
137 RThread::Rendezvous(KErrNone); |
|
138 |
|
139 // |
|
140 // Ready to run |
|
141 CActiveScheduler::Start(); |
|
142 |
|
143 // |
|
144 // Cleanup the scheduler |
|
145 delete s; |
|
146 } |
|
147 |
|
148 |
|
149 TInt CUsbHostMsDeviceThread::Entry(TAny* aPtr) |
|
150 { |
|
151 __MSFNSLOG |
|
152 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
153 if (!cleanup) |
|
154 { |
|
155 return KErrNoMemory; |
|
156 } |
|
157 |
|
158 TRAPD(error, DoStartServerL(aPtr)); |
|
159 delete cleanup; |
|
160 return error; |
|
161 } |
|
162 |
|
163 |
|
164 void CUsbHostMsDeviceThread::RunL() |
|
165 { |
|
166 __MSFNLOG |
|
167 |
|
168 // called on completion of MessageRequest() or Resume() |
|
169 User::LeaveIfError(iStatus.Int()); |
|
170 |
|
171 Lock(); |
|
172 if (iUsbHostMsDevice) |
|
173 { |
|
174 if (iUsbHostMsDevice->IsSuspended()) |
|
175 { |
|
176 // request resume |
|
177 Unlock(); |
|
178 iUsbHostMsDevice->Resume(iStatus); |
|
179 SetActive(); |
|
180 return; |
|
181 } |
|
182 } |
|
183 |
|
184 // process message queue |
|
185 RMessage2 msg = iRMessage2[iDequeueIndex]; |
|
186 |
|
187 iDequeueIndex++; |
|
188 |
|
189 if(iDequeueIndex >= KMaxNumMessage) |
|
190 iDequeueIndex = 0; |
|
191 if(iQueueFull) |
|
192 iQueueFull = EFalse; |
|
193 |
|
194 HandleMessage(msg); |
|
195 |
|
196 if ((iQueueIndex != iDequeueIndex) || iQueueFull) |
|
197 { |
|
198 // self completion |
|
199 TRequestStatus* status = &iStatus; |
|
200 User::RequestComplete(status, KErrNone); |
|
201 SetActive(); |
|
202 } |
|
203 else |
|
204 { |
|
205 iUsbHostMsSession.MessageRequest(iStatus); |
|
206 SetActive(); |
|
207 } |
|
208 Unlock(); |
|
209 } |
|
210 |
|
211 |
|
212 void CUsbHostMsDeviceThread::DoCancel() |
|
213 { |
|
214 TRequestStatus* status = &iStatus; |
|
215 User::RequestComplete(status, KErrCancel); |
|
216 } |
|
217 |
|
218 |
|
219 TInt CUsbHostMsDeviceThread::RunError(TInt aError) |
|
220 { |
|
221 __HOSTPRINT1(_L(">> HOST RunError returning %d"), aError); |
|
222 return KErrNone; |
|
223 } |
|
224 |
|
225 |
|
226 TInt CUsbHostMsDeviceThread::QueueMsg(const RMessage2& aMsg) |
|
227 { |
|
228 __MSFNLOG |
|
229 |
|
230 if (iQueueFull) |
|
231 { |
|
232 return KErrOverflow; |
|
233 } |
|
234 |
|
235 Lock(); |
|
236 |
|
237 iRMessage2[iQueueIndex] = aMsg; |
|
238 iQueueIndex++; |
|
239 |
|
240 if (iQueueIndex >= KMaxNumMessage) |
|
241 { |
|
242 iQueueIndex = 0; |
|
243 } |
|
244 |
|
245 if (iQueueIndex == iDequeueIndex) |
|
246 { |
|
247 iQueueFull = ETrue; |
|
248 } |
|
249 Unlock(); |
|
250 return KErrNone; |
|
251 } |
|
252 |
|
253 |
|
254 CUsbHostMsDeviceThread::CUsbHostMsDeviceThread(CUsbHostMsSession& aUsbHostMsSession, TUint aToken) |
|
255 : CActive(EPriorityStandard), |
|
256 iUsbHostMsSession(aUsbHostMsSession), |
|
257 iQueueFull(EFalse) |
|
258 { |
|
259 __MSFNLOG |
|
260 TBuf<30> nameBuf; |
|
261 nameBuf.Format(_L("Host Ms ThreadMutex%d"), aToken); |
|
262 iMutex.CreateGlobal(nameBuf,EOwnerProcess); |
|
263 } |
|
264 |
|
265 CUsbHostMsDeviceThread::~CUsbHostMsDeviceThread() |
|
266 { |
|
267 __MSFNLOG |
|
268 Cancel(); |
|
269 iMutex.Close(); |
|
270 } |
|
271 |
|
272 CUsbHostMsDeviceThread* CUsbHostMsDeviceThread::NewL(CUsbHostMsSession& aUsbHostMsSession, TUint aToken) |
|
273 { |
|
274 __MSFNSLOG |
|
275 CUsbHostMsDeviceThread* r = new (ELeave) CUsbHostMsDeviceThread(aUsbHostMsSession, aToken); |
|
276 return r; |
|
277 } |
|
278 |
|
279 |
|
280 void CUsbHostMsDeviceThread::Start() |
|
281 { |
|
282 iUsbHostMsSession.MessageRequest(iStatus); |
|
283 SetActive(); |
|
284 } |
|
285 |
|
286 |
|
287 /** |
|
288 Handles the request (in the form of a message) received from the client |
|
289 @param aMessage The received message |
|
290 */ |
|
291 void CUsbHostMsDeviceThread::HandleMessage(const RMessage2& aMessage) |
|
292 { |
|
293 __MSFNLOG |
|
294 TInt ret = KErrNotReady; |
|
295 __HOSTPRINT2(_L(">> HOST DispatchMessageL Function=%d %d"), aMessage.Function(), aMessage.Int3()); |
|
296 switch (aMessage.Function()) |
|
297 { |
|
298 case EUsbHostMsRegisterInterface: |
|
299 TRAP(ret, RegisterInterfaceL(aMessage)); |
|
300 break; |
|
301 case EUsbHostMsInitialiseInterface: |
|
302 TRAP(ret, InitialiseInterfaceL(aMessage)); |
|
303 // CUsbInterfaceHandler::GetMaxLun() completes asynchronously |
|
304 if (ret) |
|
305 { |
|
306 // Error condition needs to be completed |
|
307 break; |
|
308 } |
|
309 return; |
|
310 case EUsbHostMsUnRegisterInterface: |
|
311 TRAP(ret, UnRegisterInterfaceL(aMessage)); |
|
312 break; |
|
313 case EUsbHostMsRegisterLun: |
|
314 TRAP(ret, RegisterLogicalUnitL(aMessage)); |
|
315 break; |
|
316 case EUsbHostMsGetNumLun: |
|
317 TRAP(ret, GetNumLunL(aMessage)); |
|
318 break; |
|
319 case EUsbHostMsShutdown: |
|
320 ret = Shutdown(); |
|
321 break; |
|
322 default: |
|
323 // Try Device Handler and Logical Unit Handler |
|
324 __ASSERT_DEBUG(iUsbHostMsDevice, User::Invariant()); |
|
325 TDeviceHandler deviceHandler(*iUsbHostMsDevice); |
|
326 TRAP(ret, deviceHandler.HandleMessageL(aMessage)); |
|
327 break; |
|
328 } |
|
329 __HOSTPRINT1(_L(">> HOST returning %d"), ret); |
|
330 |
|
331 if (aMessage.Function() != EUsbHostMsNotifyChange) |
|
332 { |
|
333 aMessage.Complete(ret); |
|
334 } |
|
335 } |
|
336 |
|
337 |
|
338 /** |
|
339 Client request to shut down the server |
|
340 |
|
341 return KErrNone |
|
342 */ |
|
343 TInt CUsbHostMsDeviceThread::Shutdown() |
|
344 { |
|
345 __MSFNLOG |
|
346 CActiveScheduler::Stop(); |
|
347 return KErrNone; |
|
348 } |
|
349 |
|
350 void CUsbHostMsDeviceThread::GetNumLunL(const RMessage2& aMessage) |
|
351 { |
|
352 __MSFNLOG |
|
353 if (!iUsbHostMsDevice) |
|
354 { |
|
355 User::Leave(KErrNotReady); |
|
356 } |
|
357 |
|
358 TUint32 maxLun = iUsbHostMsDevice->GetMaxLun() + 1; |
|
359 TPtrC8 pLun((TUint8*)&maxLun,sizeof(TUint32)); |
|
360 aMessage.WriteL(0,pLun); |
|
361 } |
|
362 |
|
363 |
|
364 void CUsbHostMsDeviceThread::RegisterInterfaceL(const RMessage2& aMessage) |
|
365 { |
|
366 __MSFNLOG |
|
367 |
|
368 THostMassStorageConfig msDeviceConfig; |
|
369 TPtr8 ptr((TUint8*)&msDeviceConfig,sizeof(THostMassStorageConfig)); |
|
370 aMessage.ReadL(0, ptr); |
|
371 |
|
372 __HOSTPRINT1(_L("RegisterInterfaceL Token=%d "), msDeviceConfig.iInterfaceToken); |
|
373 |
|
374 iUsbHostMsDevice = CUsbHostMsDevice::NewL(msDeviceConfig); |
|
375 } |
|
376 |
|
377 |
|
378 void CUsbHostMsDeviceThread::InitialiseInterfaceL(const RMessage2& aMessage) |
|
379 { |
|
380 __MSFNLOG |
|
381 if (!iUsbHostMsDevice) |
|
382 { |
|
383 User::Leave(KErrNotReady); |
|
384 } |
|
385 |
|
386 TRAPD(err, iUsbHostMsDevice->InitialiseL(aMessage)); |
|
387 if (err != KErrNone) |
|
388 { |
|
389 delete iUsbHostMsDevice; |
|
390 iUsbHostMsDevice = NULL; |
|
391 User::Leave(err); |
|
392 } |
|
393 } |
|
394 |
|
395 |
|
396 void CUsbHostMsDeviceThread::UnRegisterInterfaceL(const RMessage2& aMessage) |
|
397 { |
|
398 __MSFNLOG |
|
399 if (!iUsbHostMsDevice) |
|
400 { |
|
401 User::Leave(KErrNotReady); |
|
402 } |
|
403 |
|
404 TRAPD(err, iUsbHostMsDevice->UnInitialiseL()); |
|
405 delete iUsbHostMsDevice; |
|
406 iUsbHostMsDevice = NULL; |
|
407 User::LeaveIfError(err); |
|
408 } |
|
409 |
|
410 |
|
411 void CUsbHostMsDeviceThread::RegisterLogicalUnitL(const RMessage2& aMessage) |
|
412 { |
|
413 if (!iUsbHostMsDevice) |
|
414 { |
|
415 User::Leave(KErrNotReady); |
|
416 } |
|
417 |
|
418 TUint32 iLunId = aMessage.Int0() + 1; // Subssessions need a positive value to store in the handles. We represent Luns as LunId+1 |
|
419 TPtrC8 pLun((TUint8*)&iLunId, sizeof(TUint32)); |
|
420 aMessage.WriteL(3, pLun); |
|
421 iLunId -= 1; // We represent LunId in MSC from 0 to MaxLun-1 as represented in BOT |
|
422 iUsbHostMsDevice->AddLunL(iLunId); |
|
423 iUsbHostMsDevice->InitLunL(iLunId); |
|
424 } |
|
425 |