|
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 /** |
|
17 @file |
|
18 @publishedPartner |
|
19 */ |
|
20 |
|
21 #include <mtp/mtpdatatypeconstants.h> |
|
22 #include <mtp/mtptypessimple.h> |
|
23 #include <mtp/tmtptypeflatbase.h> |
|
24 |
|
25 #include "mtpdatatypespanic.h" |
|
26 |
|
27 /** |
|
28 Default constructor. |
|
29 */ |
|
30 EXPORT_C TMTPTypeFlatBase::TMTPTypeFlatBase() : |
|
31 iBuffer(NULL, 0, 0) |
|
32 { |
|
33 |
|
34 } |
|
35 |
|
36 /** |
|
37 Resets the dataset by zero filling it.. |
|
38 */ |
|
39 EXPORT_C void TMTPTypeFlatBase::Reset() |
|
40 { |
|
41 iBuffer.FillZ(); |
|
42 } |
|
43 |
|
44 /** |
|
45 Provides a copy of the specified element. |
|
46 @param aElementId The identifier of the requested element. |
|
47 @param aElement The MMTPType target data buffer. |
|
48 @leave One of the system wide error code, if a processing error occurs. |
|
49 @panic MTPDataTypes 3, if the target buffer type does not match the requested |
|
50 element. |
|
51 */ |
|
52 EXPORT_C void TMTPTypeFlatBase::GetL(TInt aElementId, MMTPType& aElement) const |
|
53 { |
|
54 const TElementInfo& info(ElementInfo(aElementId)); |
|
55 __ASSERT_ALWAYS((info.iType == aElement.Type()), Panic(EMTPTypeIdMismatch)); |
|
56 |
|
57 TBool commit(aElement.CommitRequired()); |
|
58 TBool complete(EFalse); |
|
59 TPtr8 dest(NULL, 0); |
|
60 TInt offset(info.iOffset); |
|
61 |
|
62 TInt err(aElement.FirstWriteChunk(dest)); |
|
63 while (((err == KMTPChunkSequenceCompletion) || (err == KErrNone)) && (!complete)) |
|
64 { |
|
65 complete = (err == KMTPChunkSequenceCompletion); |
|
66 |
|
67 TPtrC8 src(&(iBuffer[offset]), (info.iSize - (offset - info.iOffset))); |
|
68 dest.Copy(src); |
|
69 offset += dest.Length(); |
|
70 |
|
71 if (commit) |
|
72 { |
|
73 aElement.CommitChunkL(dest); |
|
74 } |
|
75 |
|
76 if (!complete) |
|
77 { |
|
78 err = aElement.NextWriteChunk(dest); |
|
79 } |
|
80 } |
|
81 |
|
82 if (err != KMTPChunkSequenceCompletion) |
|
83 { |
|
84 User::Leave(err); |
|
85 } |
|
86 } |
|
87 |
|
88 /** |
|
89 Updates the specified element. |
|
90 @param aElementId The identifier of the element to update. |
|
91 @param aElement The MMTPType source data buffer. |
|
92 @leave One of the system wide error code, if a processing error occurs. |
|
93 @panic MTPDataTypes 3, if the source buffer type does not match the requested |
|
94 element. |
|
95 @panic MTPDataTypes 4, if the source buffer size does not match the requested |
|
96 element. |
|
97 */ |
|
98 EXPORT_C void TMTPTypeFlatBase::SetL(TInt aElementId, const MMTPType& aElement) |
|
99 { |
|
100 const TElementInfo& info(ElementInfo(aElementId)); |
|
101 __ASSERT_ALWAYS((info.iType == aElement.Type()), Panic(EMTPTypeIdMismatch)); |
|
102 __ASSERT_ALWAYS((info.iSize == aElement.Size()), Panic(EMTPTypeSizeMismatch)); |
|
103 |
|
104 TBool complete(EFalse); |
|
105 TInt offset(info.iOffset); |
|
106 TPtrC8 src; |
|
107 |
|
108 TInt err(aElement.FirstReadChunk(src)); |
|
109 while (((err == KMTPChunkSequenceCompletion) || (err == KErrNone)) && (!complete)) |
|
110 { |
|
111 __ASSERT_ALWAYS((((offset + src.Length()) - info.iOffset) <= info.iSize), Panic(EMTPTypeSizeMismatch)); |
|
112 complete = (err == KMTPChunkSequenceCompletion); |
|
113 |
|
114 iBuffer.Replace(offset, src.Length(), src); |
|
115 offset += src.Length(); |
|
116 |
|
117 if (!complete) |
|
118 { |
|
119 err = aElement.NextReadChunk(src); |
|
120 } |
|
121 } |
|
122 |
|
123 if (err != KMTPChunkSequenceCompletion) |
|
124 { |
|
125 User::Leave(err); |
|
126 } |
|
127 } |
|
128 |
|
129 /** |
|
130 Updates the specified element. |
|
131 @param aElementId The identifier of the element to update. |
|
132 @param aElement The source data. |
|
133 @panic MTPDataTypes 3, if the source data type does not match the requested |
|
134 element. |
|
135 */ |
|
136 EXPORT_C void TMTPTypeFlatBase::SetInt8(TInt aElementId, TInt8 aData) |
|
137 { |
|
138 const TElementInfo& info(ElementInfo(aElementId)); |
|
139 __ASSERT_ALWAYS((info.iType == EMTPTypeINT8), Panic(EMTPTypeIdMismatch)); |
|
140 |
|
141 TPtrC8 data(reinterpret_cast<TUint8*>(&aData), sizeof(aData)); |
|
142 iBuffer.Replace(info.iOffset, data.Length(), data); |
|
143 } |
|
144 |
|
145 /** |
|
146 Updates the specified element. |
|
147 @param aElementId The identifier of the element to update. |
|
148 @param aElement The source data. |
|
149 @panic MTPDataTypes 3, if the source data type does not match the requested |
|
150 element. |
|
151 */ |
|
152 EXPORT_C void TMTPTypeFlatBase::SetInt16(TInt aElementId, TInt16 aData) |
|
153 { |
|
154 const TElementInfo& info(ElementInfo(aElementId)); |
|
155 __ASSERT_ALWAYS((info.iType == EMTPTypeINT16), Panic(EMTPTypeIdMismatch)); |
|
156 |
|
157 TPtrC8 data(reinterpret_cast<TUint8*>(&aData), sizeof(aData)); |
|
158 iBuffer.Replace(info.iOffset, data.Length(), data); |
|
159 } |
|
160 |
|
161 /** |
|
162 Updates the specified element. |
|
163 @param aElementId The identifier of the element to update. |
|
164 @param aElement The source data. |
|
165 @panic MTPDataTypes 3, if the source data type does not match the requested |
|
166 element. |
|
167 */ |
|
168 EXPORT_C void TMTPTypeFlatBase::SetInt32(TInt aElementId, TInt32 aData) |
|
169 { |
|
170 const TElementInfo& info(ElementInfo(aElementId)); |
|
171 __ASSERT_ALWAYS((info.iType == EMTPTypeINT32), Panic(EMTPTypeIdMismatch)); |
|
172 |
|
173 TPtrC8 data(reinterpret_cast<TUint8*>(&aData), sizeof(aData)); |
|
174 iBuffer.Replace(info.iOffset, data.Length(), data); |
|
175 } |
|
176 |
|
177 /** |
|
178 Updates the specified element. |
|
179 @param aElementId The identifier of the element to update. |
|
180 @param aElement The source data. |
|
181 @panic MTPDataTypes 3, if the source data type does not match the requested |
|
182 element. |
|
183 */ |
|
184 EXPORT_C void TMTPTypeFlatBase::SetInt64(TInt aElementId, TInt64 aData) |
|
185 { |
|
186 const TElementInfo& info(ElementInfo(aElementId)); |
|
187 __ASSERT_ALWAYS((info.iType == EMTPTypeINT64), Panic(EMTPTypeIdMismatch)); |
|
188 |
|
189 TPtrC8 data(reinterpret_cast<TUint8*>(&aData), sizeof(aData)); |
|
190 iBuffer.Replace(info.iOffset, data.Length(), data); |
|
191 } |
|
192 |
|
193 /** |
|
194 Updates the specified element. |
|
195 @param aElementId The identifier of the element to update. |
|
196 @param aElement The source data. |
|
197 @panic MTPDataTypes 3, if the source data type does not match the requested |
|
198 element. |
|
199 */ |
|
200 EXPORT_C void TMTPTypeFlatBase::SetUint8(TInt aElementId, TUint8 aData) |
|
201 { |
|
202 const TElementInfo& info(ElementInfo(aElementId)); |
|
203 __ASSERT_ALWAYS((info.iType == EMTPTypeUINT8), Panic(EMTPTypeIdMismatch)); |
|
204 |
|
205 TPtrC8 data(reinterpret_cast<TUint8*>(&aData), sizeof(aData)); |
|
206 iBuffer.Replace(info.iOffset, data.Length(), data); |
|
207 } |
|
208 |
|
209 /** |
|
210 Updates the specified element. |
|
211 @param aElementId The identifier of the element to update. |
|
212 @param aElement The source data. |
|
213 @panic MTPDataTypes 3, if the source data type does not match the requested |
|
214 element. |
|
215 */ |
|
216 EXPORT_C void TMTPTypeFlatBase::SetUint16(TInt aElementId, TUint16 aData) |
|
217 { |
|
218 const TElementInfo& info(ElementInfo(aElementId)); |
|
219 __ASSERT_ALWAYS((info.iType == EMTPTypeUINT16), Panic(EMTPTypeIdMismatch)); |
|
220 |
|
221 TPtrC8 data(reinterpret_cast<TUint8*>(&aData), sizeof(aData)); |
|
222 iBuffer.Replace(info.iOffset, data.Length(), data); |
|
223 } |
|
224 |
|
225 /** |
|
226 Updates the specified element. |
|
227 @param aElementId The identifier of the element to update. |
|
228 @param aElement The source data. |
|
229 @panic MTPDataTypes 3, if the source data type does not match the requested |
|
230 element. |
|
231 */ |
|
232 EXPORT_C void TMTPTypeFlatBase::SetUint32(TInt aElementId, TUint32 aData) |
|
233 { |
|
234 const TElementInfo& info(ElementInfo(aElementId)); |
|
235 __ASSERT_ALWAYS((info.iType == EMTPTypeUINT32), Panic(EMTPTypeIdMismatch)); |
|
236 |
|
237 TPtrC8 data(reinterpret_cast<TUint8*>(&aData), sizeof(aData)); |
|
238 iBuffer.Replace(info.iOffset, data.Length(), data); |
|
239 } |
|
240 |
|
241 /** |
|
242 Updates the specified element. |
|
243 @param aElementId The identifier of the element to update. |
|
244 @param aElement The source data. |
|
245 @panic MTPDataTypes 3, if the source data type does not match the requested |
|
246 element. |
|
247 */ |
|
248 EXPORT_C void TMTPTypeFlatBase::SetUint64(TInt aElementId, TUint64 aData) |
|
249 { |
|
250 const TElementInfo& info(ElementInfo(aElementId)); |
|
251 __ASSERT_ALWAYS((info.iType == EMTPTypeUINT64), Panic(EMTPTypeIdMismatch)); |
|
252 |
|
253 TPtrC8 data(reinterpret_cast<TUint8*>(&aData), sizeof(aData)); |
|
254 iBuffer.Replace(info.iOffset, data.Length(), data); |
|
255 } |
|
256 |
|
257 /** |
|
258 Provides the value of the specified element. |
|
259 @param aElementId The identifier of the requested element. |
|
260 @return The value of the element. |
|
261 @panic MTPDataTypes 3, if the source buffer type does not match the requested |
|
262 element. |
|
263 */ |
|
264 EXPORT_C TInt8 TMTPTypeFlatBase::Int8(TInt aElementId) const |
|
265 { |
|
266 const TElementInfo& info(ElementInfo(aElementId)); |
|
267 __ASSERT_ALWAYS((info.iType == EMTPTypeINT8), Panic(EMTPTypeIdMismatch)); |
|
268 return (static_cast<TInt8>(iBuffer[info.iOffset])); |
|
269 } |
|
270 |
|
271 /** |
|
272 Provides the value of the specified element. |
|
273 @param aElementId The identifier of the requested element. |
|
274 @return The value of the element. |
|
275 @panic MTPDataTypes 3, if the source buffer type does not match the requested |
|
276 element. |
|
277 */ |
|
278 EXPORT_C TInt16 TMTPTypeFlatBase::Int16(TInt aElementId) const |
|
279 { |
|
280 const TElementInfo& info(ElementInfo(aElementId)); |
|
281 __ASSERT_ALWAYS((info.iType == EMTPTypeINT16), Panic(EMTPTypeIdMismatch)); |
|
282 |
|
283 // memcpy the element data to avoid alignment errors. |
|
284 TInt16 ret; |
|
285 memcpy(&ret, &iBuffer[info.iOffset], sizeof(ret)); |
|
286 return ret; |
|
287 } |
|
288 |
|
289 /** |
|
290 Provides the value of the specified element. |
|
291 @param aElementId The identifier of the requested element. |
|
292 @return The value of the element. |
|
293 @panic MTPDataTypes 3, if the source buffer type does not match the requested |
|
294 element. |
|
295 */ |
|
296 EXPORT_C TInt32 TMTPTypeFlatBase::Int32(TInt aElementId) const |
|
297 { |
|
298 const TElementInfo& info(ElementInfo(aElementId)); |
|
299 __ASSERT_ALWAYS((info.iType == EMTPTypeINT32), Panic(EMTPTypeIdMismatch)); |
|
300 |
|
301 // memcpy the element data to avoid alignment errors. |
|
302 TInt32 ret; |
|
303 memcpy(&ret, &iBuffer[info.iOffset], sizeof(ret)); |
|
304 return ret; |
|
305 } |
|
306 |
|
307 /** |
|
308 Provides the value of the specified element. |
|
309 @param aElementId The identifier of the requested element. |
|
310 @return The value of the element. |
|
311 @panic MTPDataTypes 3, if the source buffer type does not match the requested |
|
312 element. |
|
313 */ |
|
314 EXPORT_C TInt64 TMTPTypeFlatBase::Int64(TInt aElementId) const |
|
315 { |
|
316 const TElementInfo& info(ElementInfo(aElementId)); |
|
317 __ASSERT_ALWAYS((info.iType == EMTPTypeINT64), Panic(EMTPTypeIdMismatch)); |
|
318 |
|
319 // memcpy the element data to avoid alignment errors. |
|
320 TInt64 ret; |
|
321 memcpy(&ret, &iBuffer[info.iOffset], sizeof(ret)); |
|
322 return ret; |
|
323 } |
|
324 |
|
325 /** |
|
326 Provides the value of the specified element. |
|
327 @param aElementId The identifier of the requested element. |
|
328 @return The value of the element. |
|
329 @panic MTPDataTypes 3, if the source buffer type does not match the requested |
|
330 element. |
|
331 */ |
|
332 EXPORT_C TUint8 TMTPTypeFlatBase::Uint8(TInt aElementId) const |
|
333 { |
|
334 const TElementInfo& info(ElementInfo(aElementId)); |
|
335 __ASSERT_ALWAYS((info.iType == EMTPTypeUINT8), Panic(EMTPTypeIdMismatch)); |
|
336 return (static_cast<TUint8>(iBuffer[info.iOffset])); |
|
337 } |
|
338 |
|
339 /** |
|
340 Provides the value of the specified element. |
|
341 @param aElementId The identifier of the requested element. |
|
342 @return The value of the element. |
|
343 @panic MTPDataTypes 3, if the source buffer type does not match the requested |
|
344 element. |
|
345 */ |
|
346 EXPORT_C TUint16 TMTPTypeFlatBase::Uint16(TInt aElementId) const |
|
347 { |
|
348 const TElementInfo& info(ElementInfo(aElementId)); |
|
349 __ASSERT_ALWAYS((info.iType == EMTPTypeUINT16), Panic(EMTPTypeIdMismatch)); |
|
350 |
|
351 // memcpy the element data to avoid alignment errors. |
|
352 TUint16 ret; |
|
353 memcpy(&ret, &iBuffer[info.iOffset], sizeof(ret)); |
|
354 return ret; |
|
355 } |
|
356 |
|
357 /** |
|
358 Provides the value of the specified element. |
|
359 @param aElementId The identifier of the requested element. |
|
360 @return The value of the element. |
|
361 @panic MTPDataTypes 3, if the source buffer type does not match the requested |
|
362 element. |
|
363 */ |
|
364 EXPORT_C TUint32 TMTPTypeFlatBase::Uint32(TInt aElementId) const |
|
365 { |
|
366 const TElementInfo& info(ElementInfo(aElementId)); |
|
367 __ASSERT_ALWAYS((info.iType == EMTPTypeUINT32), Panic(EMTPTypeIdMismatch)); |
|
368 |
|
369 // memcpy the element data to avoid alignment errors. |
|
370 TUint32 ret; |
|
371 memcpy(&ret, &iBuffer[info.iOffset], sizeof(ret)); |
|
372 return ret; |
|
373 } |
|
374 |
|
375 /** |
|
376 Provides the value of the specified element. |
|
377 @param aElementId The identifier of the requested element. |
|
378 @return The value of the element. |
|
379 @panic MTPDataTypes 3, if the source buffer type does not match the requested |
|
380 element. |
|
381 */ |
|
382 EXPORT_C TUint64 TMTPTypeFlatBase::Uint64(TInt aElementId) const |
|
383 { |
|
384 const TElementInfo& info(ElementInfo(aElementId)); |
|
385 __ASSERT_ALWAYS((info.iType == EMTPTypeUINT64), Panic(EMTPTypeIdMismatch)); |
|
386 |
|
387 // memcpy the element data to avoid alignment errors. |
|
388 TUint64 ret; |
|
389 memcpy(&ret, &iBuffer[info.iOffset], sizeof(ret)); |
|
390 return ret; |
|
391 } |
|
392 |
|
393 EXPORT_C TInt TMTPTypeFlatBase::FirstReadChunk(TPtrC8& aChunk) const |
|
394 { |
|
395 __ASSERT_ALWAYS(iBuffer.MaxLength() > 0, Panic(EMTPTypeBadStorage)); |
|
396 aChunk.Set(iBuffer); |
|
397 return KMTPChunkSequenceCompletion; |
|
398 } |
|
399 |
|
400 EXPORT_C TInt TMTPTypeFlatBase::NextReadChunk(TPtrC8& aChunk) const |
|
401 { |
|
402 aChunk.Set(NULL, 0); |
|
403 return KErrNotReady; |
|
404 } |
|
405 |
|
406 EXPORT_C TInt TMTPTypeFlatBase::FirstWriteChunk(TPtr8& aChunk) |
|
407 { |
|
408 __ASSERT_ALWAYS(iBuffer.MaxLength() > 0, Panic(EMTPTypeBadStorage)); |
|
409 aChunk.Set(&iBuffer[0], 0, iBuffer.MaxLength()); |
|
410 return KMTPChunkSequenceCompletion; |
|
411 } |
|
412 |
|
413 EXPORT_C TInt TMTPTypeFlatBase::NextWriteChunk(TPtr8& aChunk) |
|
414 { |
|
415 aChunk.Set(NULL, 0, 0); |
|
416 return KErrNotReady; |
|
417 } |
|
418 |
|
419 EXPORT_C TUint64 TMTPTypeFlatBase::Size() const |
|
420 { |
|
421 return iBuffer.Size(); |
|
422 } |
|
423 |
|
424 EXPORT_C TUint TMTPTypeFlatBase::Type() const |
|
425 { |
|
426 return EMTPTypeFlat; |
|
427 } |
|
428 |
|
429 /** |
|
430 Sets the MTP dataset buffer to be managed. |
|
431 @param aBuffer The MTP dataset buffer to be managed. |
|
432 */ |
|
433 EXPORT_C void TMTPTypeFlatBase::SetBuffer(const TDes8& aBuffer) |
|
434 { |
|
435 iBuffer.Set(aBuffer.MidTPtr(0)); |
|
436 iBuffer.SetLength(iBuffer.MaxLength()); |
|
437 } |
|
438 |
|
439 |