1 // Copyright (c) 2007-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 |
|
18 /** |
|
19 @file |
|
20 @internalComponent |
|
21 @released |
|
22 */ |
|
23 |
|
24 #include "avrcpfragmenter.h" |
|
25 #include "controlcommand.h" |
|
26 #include <e32debug.h> |
|
27 |
|
28 // Maximum size of data in a fragment response |
|
29 static const int KFragmentDataLength = KAVCMaxVendorDependentPayload - KAVRCPMinVendorDependentResponseLen; |
|
30 |
|
31 |
|
32 CAVRCPFragmenter* CAVRCPFragmenter::NewL() |
|
33 { |
|
34 CAVRCPFragmenter* self = new (ELeave) CAVRCPFragmenter(); |
|
35 self->Construct(); |
|
36 return self; |
|
37 } |
|
38 |
|
39 TBool CAVRCPFragmenter::InFragmentedState() |
|
40 { |
|
41 return iInFragmentedState; |
|
42 } |
|
43 |
|
44 void CAVRCPFragmenter::Reset() |
|
45 { |
|
46 iStart = KAVRCPMinVendorDependentResponseLen; |
|
47 iInFragmentedState = EFalse; |
|
48 } |
|
49 |
|
50 void CAVRCPFragmenter::AssignPayload(const RBuf8& aPayload) |
|
51 { |
|
52 // Assign the allocated memory containing the reponse to |
|
53 // this class, allowing the caller to Close() aPayload. |
|
54 iPayload.Close(); |
|
55 iPayload.Assign(aPayload); |
|
56 iInFragmentedState = ETrue; |
|
57 } |
|
58 |
|
59 TPtr8 CAVRCPFragmenter::GetNextFragmentHeader() |
|
60 { |
|
61 if (! InFragmentedState()) |
|
62 return TPtr8(0,0,0); |
|
63 |
|
64 // Fragmentation byte is 2nd byte of response; poke this |
|
65 // and set the length correctly |
|
66 if (iStart == KAVRCPMinVendorDependentResponseLen) |
|
67 { |
|
68 iPayload[1] = EFragmentStart; |
|
69 iPayload[2] = (KFragmentDataLength >> 8) & 0xff; |
|
70 iPayload[3] = (KFragmentDataLength) & 0xff; |
|
71 } |
|
72 else if (LastFragment()) |
|
73 { |
|
74 iPayload[1] = EFragmentEnd; |
|
75 TInt length = iPayload.Length() - iStart; |
|
76 iPayload[2] = (length >> 8) & 0xff; |
|
77 iPayload[3] = (length) & 0xff; |
|
78 } |
|
79 else |
|
80 { |
|
81 iPayload[1] = EFragmentContinue; |
|
82 iPayload[2] = (KFragmentDataLength >> 8) & 0xff; |
|
83 iPayload[3] = (KFragmentDataLength) & 0xff; |
|
84 } |
|
85 |
|
86 // Return the first 4 bytes, which contain the PDU id, |
|
87 // fragmentation status and parameter length (2 bytes) |
|
88 return iPayload.MidTPtr(0, KAVRCPMinVendorDependentResponseLen); |
|
89 } |
|
90 |
|
91 TPtr8 CAVRCPFragmenter::GetNextFragment() |
|
92 { |
|
93 if (! InFragmentedState()) |
|
94 return TPtr8(0,0,0); |
|
95 |
|
96 // Sigh. MidTPtr() PANICs if the 2nd argument is too large. |
|
97 // Other APIs (e.g. Java, STL) just return to end of string. |
|
98 TInt fragmentSize = KFragmentDataLength; |
|
99 if (LastFragment()) |
|
100 fragmentSize = iPayload.Length() - iStart; |
|
101 |
|
102 TPtr8 fragment = iPayload.MidTPtr(iStart, fragmentSize); |
|
103 |
|
104 if (LastFragment()) |
|
105 Reset(); |
|
106 else |
|
107 iStart += fragmentSize; |
|
108 return fragment; |
|
109 } |
|
110 |
|
111 TBool CAVRCPFragmenter::LastFragment() |
|
112 { |
|
113 if (iStart + KFragmentDataLength > iPayload.Length()) |
|
114 return ETrue; |
|
115 else |
|
116 return EFalse; |
|
117 } |
|
118 |
|
119 TInt CAVRCPFragmenter::GetPDU() |
|
120 { |
|
121 return iPayload[0]; |
|
122 } |
|
123 |
|
124 void CAVRCPFragmenter::Construct() |
|
125 { |
|
126 Reset(); |
|
127 } |
|
128 |
|