|
1 /* |
|
2 * Copyright (c) 2006-2008 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: A package class for TConnMonId objects. |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <e32std.h> |
|
19 #include <rconnmon.h> |
|
20 |
|
21 #include "log.h" |
|
22 |
|
23 // ======== MEMBER FUNCTIONS ======== |
|
24 |
|
25 // --------------------------------------------------------------------------- |
|
26 // Construction |
|
27 // data is packed to the following format |
|
28 // |all_data_amount|amount_of_transferring_data_(n)|item0|....|item(n-1) |
|
29 // each item is packed in following format |
|
30 // |size_of_item|item's_data |
|
31 // item's data can be unpacked by MDesSerializer::FromPtr(), implemented by an item's class |
|
32 // --------------------------------------------------------------------------- |
|
33 // |
|
34 ConnMonIdsArrayPckg::ConnMonIdsArrayPckg( const RArray<TConnMonId>& aRef, TUint aBufLen ) |
|
35 { |
|
36 LOGENTRFN("ConnMonIdsArrayPckg::ConnMonIdsArrayPckg()") |
|
37 |
|
38 const TUint KItemLengthFieldSize = 1; |
|
39 |
|
40 // First 2 elements in buffer will contain the total number of objects and the number of transfered objects |
|
41 TUint currentPosition( 2 ); |
|
42 TInt totalItemCount = aRef.Count(); |
|
43 |
|
44 LOGIT2("ConnMonIdsArrayPckg: buffer size %d, item count %d", aBufLen, totalItemCount) |
|
45 |
|
46 // Create a temporary pointer array table for buffer objects |
|
47 RPointerArray<HBufC> items; |
|
48 // Add items until all added or buffer full |
|
49 for ( TUint i = 0; i < totalItemCount; ++i ) |
|
50 { |
|
51 const HBufC* item( aRef[i].ToBuf() ); |
|
52 if ( item ) |
|
53 { |
|
54 // Check if there is room in buffer for the next item |
|
55 if ( ( currentPosition + KItemLengthFieldSize + item->Length() ) > aBufLen ) |
|
56 { |
|
57 LOGIT4("Buffer full, current position %d, item length %d, buffer size %d, i %d", |
|
58 currentPosition, item->Length(), aBufLen, i) |
|
59 delete item; |
|
60 item = NULL; |
|
61 break; |
|
62 } |
|
63 currentPosition += KItemLengthFieldSize + item->Length(); |
|
64 items.Append( item ); |
|
65 } |
|
66 } |
|
67 |
|
68 // Check that given buffer length (aBufLen) is not smaller than one |
|
69 // item (CConnMonWlanNetwork) + aRef count + items count + item length |
|
70 if ( items.Count() == 0 && totalItemCount > 0 ) |
|
71 { |
|
72 aBufLen = 2; // aRef.Count(), items.Count() |
|
73 } |
|
74 |
|
75 // Allocate memory for buffer |
|
76 iBuf = HBufC::New( aBufLen ); |
|
77 if ( !iBuf ) |
|
78 { |
|
79 LOGIT("ConnMonIdsArrayPckg: out of memory error") |
|
80 } |
|
81 else |
|
82 { |
|
83 // Add total and transferred counts to buffer |
|
84 iBuf->Des().Append( totalItemCount ); // Total amount of objects |
|
85 iBuf->Des().Append( items.Count() ); // Amount of objects in buffer |
|
86 |
|
87 // Add item length and item data to buffer |
|
88 for ( TUint i = 0; i < items.Count(); ++i ) |
|
89 { |
|
90 iBuf->Des().Append( items[i]->Length() ); |
|
91 iBuf->Des().Append( *items[i] ); |
|
92 } |
|
93 LOGIT1("Used buffer %d", iBuf->Length()) |
|
94 } |
|
95 items.ResetAndDestroy(); |
|
96 |
|
97 LOGEXITFN("ConnMonIdsArrayPckg::ConnMonIdsArrayPckg()") |
|
98 } |
|
99 |
|
100 // --------------------------------------------------------------------------- |
|
101 // Construction |
|
102 // --------------------------------------------------------------------------- |
|
103 // |
|
104 EXPORT_C ConnMonIdsArrayPckg::ConnMonIdsArrayPckg( TUint aBufSize ) |
|
105 { |
|
106 iBuf = HBufC::New( aBufSize ); |
|
107 } |
|
108 |
|
109 // --------------------------------------------------------------------------- |
|
110 // Destruction |
|
111 // --------------------------------------------------------------------------- |
|
112 // |
|
113 EXPORT_C ConnMonIdsArrayPckg::~ConnMonIdsArrayPckg() |
|
114 { |
|
115 delete iBuf; |
|
116 } |
|
117 |
|
118 // --------------------------------------------------------------------------- |
|
119 // Unpacking |
|
120 // --------------------------------------------------------------------------- |
|
121 // |
|
122 EXPORT_C void ConnMonIdsArrayPckg::UnpackToL( RArray<TConnMonId>& aRef ) const |
|
123 { |
|
124 if ( !iBuf || iBuf->Length() < 2 ) |
|
125 { |
|
126 User::Leave( KErrBadDescriptor ); |
|
127 } |
|
128 |
|
129 TUint index( 0 ); |
|
130 TUint total( (*iBuf)[index++] ); // Amount of data totally to be transferred |
|
131 TUint count( (*iBuf)[index++] ); // Amount of packed data |
|
132 |
|
133 for ( TUint i = 0; i < count; ++i ) |
|
134 { |
|
135 TUint len( (*iBuf)[index++] ); |
|
136 TPtrC ptr( iBuf->Mid( index, len ) ); |
|
137 User::LeaveIfError( aRef.Append( TConnMonId::FromPtrC( ptr ) ) ); |
|
138 index += len; |
|
139 } |
|
140 } |
|
141 |
|
142 // --------------------------------------------------------------------------- |
|
143 // Getter |
|
144 // --------------------------------------------------------------------------- |
|
145 // |
|
146 EXPORT_C HBufC* ConnMonIdsArrayPckg::Buf() const |
|
147 { |
|
148 return iBuf; |
|
149 } |
|
150 |
|
151 // End-of-file |