|
1 // Copyright (c) 2006-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 "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 // |
|
15 |
|
16 #include <mtp/mtpprotocolconstants.h> |
|
17 #include <mtp/tmtptyperequest.h> |
|
18 #include <mtp/mmtpdataproviderframework.h> |
|
19 #include <mtp/tmtptypeevent.h> |
|
20 #include <mtp/mmtpconnection.h> |
|
21 #include <mtp/mmtpobjectmgr.h> |
|
22 |
|
23 #include "cmtprequestprocessor.h" |
|
24 #include "cmtprequestchecker.h" |
|
25 |
|
26 const static TInt KNullBufferSize = 4096; |
|
27 __FLOG_STMT(_LIT8(KComponent,"MTPRequestProcessor");) |
|
28 |
|
29 /** |
|
30 Standard c++ constructor |
|
31 @param aFramework The data provider framework |
|
32 @param aConnection The connection from which the request comes |
|
33 @param aElementCount Number of element in the request. |
|
34 @param aElements The element info data. |
|
35 @return a pointer to the created request processor object |
|
36 */ |
|
37 EXPORT_C CMTPRequestProcessor::CMTPRequestProcessor( |
|
38 MMTPDataProviderFramework& aFramework, |
|
39 MMTPConnection& aConnection, |
|
40 TInt aElementCount, |
|
41 const TMTPRequestElementInfo* aElements) |
|
42 :CActive(EPriorityStandard), |
|
43 iFramework(aFramework), |
|
44 iConnection(aConnection), |
|
45 iElementCount(aElementCount), |
|
46 iElements(aElements) |
|
47 { |
|
48 CActiveScheduler::Add(this); |
|
49 __FLOG_OPEN(KMTPSubsystem, KComponent); |
|
50 } |
|
51 |
|
52 /** |
|
53 Destructor |
|
54 */ |
|
55 EXPORT_C CMTPRequestProcessor::~CMTPRequestProcessor() |
|
56 { |
|
57 Cancel(); |
|
58 iNullBuffer.Close(); |
|
59 delete iRequestChecker; |
|
60 __FLOG_CLOSE; |
|
61 } |
|
62 |
|
63 /** |
|
64 Relese (delete) this request processor |
|
65 */ |
|
66 EXPORT_C void CMTPRequestProcessor::Release() |
|
67 { |
|
68 delete this; |
|
69 } |
|
70 |
|
71 /** |
|
72 Send a response to the initiator |
|
73 @param aResponseCode The response code to send |
|
74 @param aParamCount The number of parameters |
|
75 @param aParmas The pointer to array of parameters |
|
76 */ |
|
77 EXPORT_C void CMTPRequestProcessor::SendResponseL(TMTPResponseCode aResponseCode, TInt aParameterCount, TUint32* aParams) |
|
78 { |
|
79 __ASSERT_DEBUG(aParameterCount < TMTPTypeRequest::ENumElements, User::Invariant()); |
|
80 |
|
81 iResponse.SetUint16(TMTPTypeResponse::EResponseCode, aResponseCode); |
|
82 |
|
83 iResponse.SetUint32(TMTPTypeResponse::EResponseSessionID, iSessionId); |
|
84 |
|
85 iResponse.SetUint32(TMTPTypeResponse::EResponseTransactionID, iTransactionCode); |
|
86 |
|
87 TInt i = 0; |
|
88 for(i = 0; i < aParameterCount; i++) |
|
89 { |
|
90 iResponse.SetUint32(TMTPTypeResponse::EResponseParameter1 + i, aParams[i]); |
|
91 } |
|
92 |
|
93 i += TMTPTypeResponse::EResponseParameter1; |
|
94 while(i <= TMTPTypeResponse::EResponseParameter5) |
|
95 { |
|
96 iResponse.SetUint32(i, KMTPNotSpecified32); |
|
97 i++; |
|
98 } |
|
99 |
|
100 __ASSERT_DEBUG(iRequest, User::Invariant()); |
|
101 iFramework.SendResponseL(iResponse, *iRequest, iConnection); |
|
102 } |
|
103 |
|
104 /** |
|
105 The current active request |
|
106 @return A reference to the current request |
|
107 */ |
|
108 EXPORT_C const TMTPTypeRequest& CMTPRequestProcessor::Request() const |
|
109 { |
|
110 __ASSERT_DEBUG(iRequest, User::Invariant()); |
|
111 return *iRequest; |
|
112 } |
|
113 |
|
114 /** |
|
115 The connection from which the current request comes |
|
116 @return A reference to the current connection |
|
117 */ |
|
118 EXPORT_C MMTPConnection& CMTPRequestProcessor::Connection() const |
|
119 { |
|
120 return iConnection; |
|
121 } |
|
122 /** |
|
123 The Session ID from the current request |
|
124 @return the value of the session ID |
|
125 */ |
|
126 EXPORT_C TUint32 CMTPRequestProcessor::SessionId() |
|
127 { |
|
128 return iSessionId; |
|
129 } |
|
130 /** |
|
131 Signal to the framework that the current request transaction has completed |
|
132 */ |
|
133 EXPORT_C void CMTPRequestProcessor::CompleteRequestL() |
|
134 { |
|
135 __ASSERT_DEBUG(iRequest, User::Invariant()); |
|
136 iFramework.TransactionCompleteL(*iRequest, iConnection); |
|
137 } |
|
138 |
|
139 /** |
|
140 Send data to the initiator |
|
141 @param aData The data to send |
|
142 */ |
|
143 EXPORT_C void CMTPRequestProcessor::SendDataL(const MMTPType& aData) |
|
144 { |
|
145 __ASSERT_DEBUG(iRequest, User::Invariant()); |
|
146 iFramework.SendDataL(aData, *iRequest, iConnection); |
|
147 } |
|
148 |
|
149 /** |
|
150 Receive data from the initiator |
|
151 @param aData The data to receive |
|
152 */ |
|
153 EXPORT_C void CMTPRequestProcessor::ReceiveDataL(MMTPType& aData) |
|
154 { |
|
155 __ASSERT_DEBUG(iRequest, User::Invariant()); |
|
156 iFramework.ReceiveDataL(aData, *iRequest, iConnection); |
|
157 } |
|
158 |
|
159 /** |
|
160 Handle the request |
|
161 @param aRequest The request to be processed |
|
162 @param aPhase The current transaction phase of the request |
|
163 @return ETrue if the transaction has completed, otherwise, EFalse |
|
164 */ |
|
165 EXPORT_C TBool CMTPRequestProcessor::HandleRequestL(const TMTPTypeRequest& aRequest, TMTPTransactionPhase aPhase) |
|
166 { |
|
167 iRequest = &aRequest; |
|
168 __ASSERT_DEBUG(iRequest, User::Invariant()); |
|
169 TBool result = EFalse; |
|
170 switch(aPhase) |
|
171 { |
|
172 case ERequestPhase: |
|
173 ExtractSessionTransactionId(); |
|
174 result = DoHandleRequestPhaseL(); |
|
175 break; |
|
176 case EDataIToRPhase: |
|
177 result = DoHandleDataIToRPhaseL(); |
|
178 break; |
|
179 case EDataRToIPhase: |
|
180 result = DoHandleRToIPhaseL(); |
|
181 break; |
|
182 case EResponsePhase: |
|
183 if (iResponseCode != EMTPRespCodeOK && HasDataphase()) |
|
184 { |
|
185 SendResponseL(TMTPResponseCode(iResponseCode)); |
|
186 iNullBuffer.Close(); |
|
187 } |
|
188 else |
|
189 { |
|
190 result = DoHandleResponsePhaseL(); |
|
191 } |
|
192 break; |
|
193 |
|
194 case ECompletingPhase: |
|
195 result = DoHandleCompletingPhaseL(); |
|
196 break; |
|
197 } |
|
198 return result; |
|
199 } |
|
200 |
|
201 /** |
|
202 Handle the event |
|
203 @param aEvent The event to be processed |
|
204 */ |
|
205 EXPORT_C void CMTPRequestProcessor::HandleEventL(const TMTPTypeEvent& aEvent) |
|
206 { |
|
207 TUint16 eventCode = aEvent.Uint16(TMTPTypeEvent::EEventCode); |
|
208 iCancelled = (eventCode == EMTPEventCodeCancelTransaction); |
|
209 } |
|
210 |
|
211 /** |
|
212 Check whether the processor can process the request |
|
213 @param aRequest The request to be processed |
|
214 @param aConnection The connection from which the request comes |
|
215 @return ETrue if the processor can process the request, otherwise EFalse |
|
216 */ |
|
217 EXPORT_C TBool CMTPRequestProcessor::Match(const TMTPTypeRequest& aRequest, MMTPConnection& aConnection) const |
|
218 { |
|
219 __ASSERT_DEBUG(iRequest, User::Invariant()); |
|
220 TBool result = ((&aRequest == iRequest) && (&iConnection == &aConnection)); |
|
221 return result; |
|
222 } |
|
223 |
|
224 /** |
|
225 Check whether the processor can process the event |
|
226 @param aEvent The event to be processed |
|
227 @param aConnection The connection from which the request comes |
|
228 @return ETrue if the processor can process the request, otherwise EFalse |
|
229 */ |
|
230 EXPORT_C TBool CMTPRequestProcessor::Match(const TMTPTypeEvent& aEvent, MMTPConnection& aConnection) const |
|
231 { |
|
232 |
|
233 TUint32 eventSessionId = aEvent.Uint32(TMTPTypeEvent::EEventSessionID); |
|
234 TUint32 eventTransactionCode = aEvent.Uint32(TMTPTypeEvent::EEventTransactionID); |
|
235 |
|
236 TBool result = EFalse; |
|
237 if(iSessionId == eventSessionId && |
|
238 iTransactionCode == eventTransactionCode && |
|
239 &iConnection == &aConnection) |
|
240 { |
|
241 result = ETrue; |
|
242 } |
|
243 return result; |
|
244 } |
|
245 |
|
246 /** |
|
247 Handle the request phase of the current request |
|
248 @return EFalse |
|
249 */ |
|
250 EXPORT_C TBool CMTPRequestProcessor::DoHandleRequestPhaseL() |
|
251 { |
|
252 __FLOG(_L8("DoHandleRequestPhaseL - Entry.")); |
|
253 TRAPD(err, iResponseCode = CheckRequestL()); |
|
254 if ((err != KErrNone) || (iResponseCode != EMTPRespCodeOK)) |
|
255 { |
|
256 if (HasDataphase()) |
|
257 { |
|
258 // If we have a dataphase |
|
259 // we need to read in the data and discard it |
|
260 __FLOG(_L8("Response code is not OK, there is data phase.")); |
|
261 iNullBuffer.Close(); |
|
262 iNullBuffer.CreateMaxL(KNullBufferSize); |
|
263 iNull.SetBuffer(iNullBuffer); |
|
264 ReceiveDataL(iNull); |
|
265 } |
|
266 else |
|
267 { |
|
268 if(err != KErrNone) |
|
269 { |
|
270 User::Leave(err); |
|
271 } |
|
272 SendResponseL(TMTPResponseCode(iResponseCode)); |
|
273 } |
|
274 } |
|
275 else |
|
276 { |
|
277 TRAP(err, ServiceL()); |
|
278 if (err != KErrNone) |
|
279 { |
|
280 iResponseCode = EMTPRespCodeGeneralError; |
|
281 if (HasDataphase()) |
|
282 { |
|
283 // If we have a dataphase |
|
284 // we need to read in the data and discard it |
|
285 iNullBuffer.Close(); |
|
286 iNullBuffer.CreateMaxL(KNullBufferSize); |
|
287 iNull.SetBuffer(iNullBuffer); |
|
288 ReceiveDataL(iNull); |
|
289 } |
|
290 else |
|
291 { |
|
292 User::Leave(err); |
|
293 } |
|
294 } |
|
295 } |
|
296 __FLOG(_L8("DoHandleRequestPhaseL - Exit.")); |
|
297 return EFalse; |
|
298 } |
|
299 |
|
300 /** |
|
301 Handle the receiving data phase of the current request |
|
302 @return EFalse |
|
303 */ |
|
304 EXPORT_C TBool CMTPRequestProcessor::DoHandleDataIToRPhaseL() |
|
305 { |
|
306 User::Invariant(); |
|
307 return EFalse; |
|
308 } |
|
309 |
|
310 /** |
|
311 Handle the sending data phase of the current request |
|
312 @return EFalse |
|
313 */ |
|
314 EXPORT_C TBool CMTPRequestProcessor::DoHandleRToIPhaseL() |
|
315 { |
|
316 User::Invariant(); |
|
317 return EFalse; |
|
318 } |
|
319 |
|
320 /** |
|
321 Handle the response phase of the current request |
|
322 @return EFalse |
|
323 */ |
|
324 EXPORT_C TBool CMTPRequestProcessor::DoHandleResponsePhaseL() |
|
325 { |
|
326 TMTPResponseCode responseCode = (iCancelled ? EMTPRespCodeIncompleteTransfer : EMTPRespCodeOK); |
|
327 SendResponseL(responseCode); |
|
328 return EFalse; |
|
329 } |
|
330 |
|
331 /** |
|
332 Handle the completing phase of the current request |
|
333 @return ETrue |
|
334 */ |
|
335 EXPORT_C TBool CMTPRequestProcessor::DoHandleCompletingPhaseL() |
|
336 { |
|
337 CompleteRequestL(); |
|
338 return ETrue; |
|
339 } |
|
340 |
|
341 /** |
|
342 Check the current request |
|
343 @return EMTPRespCodeOK if the reqeust is good, otherwise, one of the error response codes |
|
344 */ |
|
345 EXPORT_C TMTPResponseCode CMTPRequestProcessor::CheckRequestL() |
|
346 { |
|
347 if(!iRequestChecker) |
|
348 { |
|
349 iRequestChecker = CMTPRequestChecker::NewL(iFramework, iConnection); |
|
350 } |
|
351 __ASSERT_DEBUG(iRequest, User::Invariant()); |
|
352 return iRequestChecker->VerifyRequestL(*iRequest, iElementCount, iElements); |
|
353 } |
|
354 |
|
355 /** |
|
356 part of active object framework, provide default implementation |
|
357 */ |
|
358 EXPORT_C void CMTPRequestProcessor::RunL() |
|
359 { |
|
360 } |
|
361 |
|
362 /** |
|
363 part of active object framework, provide default implementation |
|
364 */ |
|
365 EXPORT_C void CMTPRequestProcessor::DoCancel() |
|
366 { |
|
367 } |
|
368 |
|
369 /** |
|
370 part of active object framework, provide default implementation |
|
371 */ |
|
372 EXPORT_C TInt CMTPRequestProcessor::RunError(TInt /*aError*/) |
|
373 { |
|
374 TRAP_IGNORE(SendResponseL(EMTPRespCodeGeneralError)); |
|
375 return KErrNone; |
|
376 } |
|
377 |
|
378 EXPORT_C TBool CMTPRequestProcessor::HasDataphase() const |
|
379 { |
|
380 return EFalse; |
|
381 } |
|
382 |
|
383 /** |
|
384 retrieve the session id and transaction code from the current request |
|
385 */ |
|
386 void CMTPRequestProcessor::ExtractSessionTransactionId() |
|
387 { |
|
388 iSessionId = iRequest->Uint32(TMTPTypeRequest::ERequestSessionID); |
|
389 iTransactionCode = iRequest->Uint32(TMTPTypeRequest::ERequestTransactionID); |
|
390 } |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |