|
1 // Copyright (c) 2003-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 // Name : StateMgr.cpp |
|
15 // Part of : SigComp / state manager |
|
16 // State manager |
|
17 // Version : 1.0 |
|
18 // |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 // INCLUDE FILES |
|
24 #include "StateMgr.h" |
|
25 #include "sigcompcompartment.h" |
|
26 #include "SigCompCompartmentStatesHolder.h" |
|
27 |
|
28 |
|
29 // ============================ MEMBER FUNCTIONS ============================== |
|
30 |
|
31 void CStateMgr::ConstructL() |
|
32 { |
|
33 iSHA1 = CSHA1::NewL(); |
|
34 } |
|
35 |
|
36 CStateMgr::CStateMgr() |
|
37 { |
|
38 } |
|
39 |
|
40 CStateMgr* CStateMgr::NewLC() |
|
41 { |
|
42 CStateMgr* self= new (ELeave) CStateMgr(); |
|
43 CleanupStack::PushL(self); |
|
44 self->ConstructL(); |
|
45 return self; |
|
46 } |
|
47 |
|
48 CStateMgr* CStateMgr::NewL() |
|
49 { |
|
50 CStateMgr* self= NewLC(); |
|
51 CleanupStack::Pop(); |
|
52 return self; |
|
53 } |
|
54 |
|
55 |
|
56 // Destructor |
|
57 CStateMgr::~CStateMgr() |
|
58 { |
|
59 |
|
60 iStateItems.ResetAndDestroy(); |
|
61 |
|
62 delete iSHA1; |
|
63 } |
|
64 |
|
65 TInt CStateMgr::Align4(TInt aValue) const |
|
66 { |
|
67 // align value to boundary of 4 |
|
68 return (((aValue) + 3) & (~3)); |
|
69 } |
|
70 |
|
71 // ---------------------------------------------------------------------------- |
|
72 // CStateMgr::CreateStateItemL |
|
73 // allocate TStateItem object and add to state items list |
|
74 // ---------------------------------------------------------------------------- |
|
75 // |
|
76 TStateItem* CStateMgr::CreateStateItemL(TInt aStateLength) |
|
77 { |
|
78 TStateItem* si = reinterpret_cast<TStateItem*> |
|
79 (User::AllocL(Align4(sizeof(TStateItem) - |
|
80 1 + aStateLength))); |
|
81 CleanupStack::PushL(si); |
|
82 User::LeaveIfError(iStateItems.Append(si)); |
|
83 CleanupStack::Pop(); |
|
84 return si; |
|
85 } |
|
86 |
|
87 |
|
88 // ---------------------------------------------------------------------------- |
|
89 // CStateMgr::ReclaimStateItem |
|
90 // reclaim state item from list and remove if not used |
|
91 // ---------------------------------------------------------------------------- |
|
92 // |
|
93 |
|
94 void CStateMgr::ReclaimStateItem(TStateItem* aStateItem) |
|
95 { |
|
96 |
|
97 if (aStateItem) |
|
98 { |
|
99 if (aStateItem->iUsageCounter > 0) |
|
100 { |
|
101 aStateItem->iUsageCounter--; |
|
102 } |
|
103 |
|
104 if (aStateItem->iUsageCounter == 0) |
|
105 { |
|
106 TInt i = iStateItems.Find(aStateItem); |
|
107 if (i >= 0) |
|
108 { |
|
109 iStateItems.Remove(i); |
|
110 User::Free(aStateItem); |
|
111 } |
|
112 } |
|
113 } |
|
114 } |
|
115 |
|
116 |
|
117 // ---------------------------------------------------------------------------- |
|
118 // CStateMgr::FindStateItem |
|
119 // search state item on list |
|
120 // ---------------------------------------------------------------------------- |
|
121 // |
|
122 |
|
123 TStateItem* CStateMgr::FindStateItem(const TDesC8& aPartialIdentifier) |
|
124 { |
|
125 |
|
126 TInt piLen = aPartialIdentifier.Size(); |
|
127 |
|
128 TStateItem* si = NULL; |
|
129 |
|
130 TInt siNum = iStateItems.Count(); |
|
131 for (TInt i = 0; i < siNum; i++) |
|
132 { |
|
133 if (Mem::Compare(iStateItems[i]->iStateIdentifier, piLen, |
|
134 aPartialIdentifier.Ptr(), piLen) == 0) |
|
135 { |
|
136 if (si != NULL) |
|
137 { |
|
138 return NULL; |
|
139 } |
|
140 |
|
141 si = iStateItems[i]; |
|
142 } |
|
143 } |
|
144 |
|
145 return si; |
|
146 } |
|
147 |
|
148 |
|
149 // ---------------------------------------------------------------------------- |
|
150 // CStateMgr::CreateStateL |
|
151 // create state |
|
152 // ---------------------------------------------------------------------------- |
|
153 // |
|
154 |
|
155 TStateItem* CStateMgr::CreateStateL(CSigCompCompartment* aCompartment, |
|
156 TUint16 aStateLength, |
|
157 TUint16 aStateAddress, |
|
158 TUint16 aStateInstruction, |
|
159 TUint16 aMinimumAccessLength, |
|
160 const TUint8* aStateValue, |
|
161 TUint16 aStateRetentionPriority) |
|
162 { |
|
163 |
|
164 if (aCompartment) |
|
165 { |
|
166 TUint sms = aCompartment->StateMemorySize(); |
|
167 if (sms == CSigComp::E0) |
|
168 { |
|
169 // empty compartment |
|
170 return NULL; |
|
171 } |
|
172 |
|
173 if (sms < (static_cast<TUint>(aStateLength) + KStateCostExtension)) |
|
174 { |
|
175 aStateLength = static_cast<TUint16>((sms - KStateCostExtension)); |
|
176 } |
|
177 } |
|
178 |
|
179 TUint8 stateData[8]; |
|
180 |
|
181 // extract values in big endian convention |
|
182 stateData[0] = static_cast<TUint8>((aStateLength >> 8) & 0xff); |
|
183 stateData[1] = static_cast<TUint8>(aStateLength & 0xff); |
|
184 stateData[2] = static_cast<TUint8>((aStateAddress >> 8) & 0xff); |
|
185 stateData[3] = static_cast<TUint8>(aStateAddress & 0xff); |
|
186 stateData[4] = static_cast<TUint8>((aStateInstruction >> 8) & 0xff); |
|
187 stateData[5] = static_cast<TUint8>(aStateInstruction & 0xff); |
|
188 stateData[6] = static_cast<TUint8>((aMinimumAccessLength >> 8) & 0xff); |
|
189 stateData[7] = static_cast<TUint8>(aMinimumAccessLength & 0xff); |
|
190 |
|
191 TPtrC8 data = TPtrC8(stateData, 8); |
|
192 TPtrC8 value = TPtrC8(aStateValue, aStateLength); |
|
193 |
|
194 iSHA1->Reset(); |
|
195 iSHA1->Hash(data); |
|
196 TPtrC8 hash = iSHA1->Hash(value); |
|
197 |
|
198 const TUint8* stateIdentifier = hash.Ptr(); |
|
199 |
|
200 TStateItem* si = FindStateItem(hash); |
|
201 if (si && |
|
202 ((si->iStateLength != aStateLength) || |
|
203 (si->iStateAddress != aStateAddress) || |
|
204 (si->iStateInstruction != aStateInstruction) || |
|
205 (si->iMinimumAccessLength != aMinimumAccessLength) || |
|
206 (Mem::Compare(si->iStateValue, |
|
207 si->iStateLength, |
|
208 aStateValue, |
|
209 aStateLength) != 0))) User::Leave(KErrInUse); |
|
210 /* NOT COVERED !!! there would have to be 2 different data sets which |
|
211 produce exactly the same SHA1 hash sum */ |
|
212 |
|
213 if (si == NULL) |
|
214 { |
|
215 si = CreateStateItemL(aStateLength); |
|
216 Mem::Copy(si->iStateIdentifier, stateIdentifier, |
|
217 KStateIdentifierMaxLength); |
|
218 si->iStateLength = aStateLength; |
|
219 si->iStateAddress = aStateAddress; |
|
220 si->iStateInstruction = aStateInstruction; |
|
221 si->iMinimumAccessLength = aMinimumAccessLength; |
|
222 Mem::Copy(si->iStateValue, aStateValue, aStateLength); |
|
223 si->iUsageCounter = 0; |
|
224 } |
|
225 |
|
226 if (aCompartment) |
|
227 { |
|
228 CSigCompCompartmentStatesHolder* sh = |
|
229 const_cast<CSigCompCompartmentStatesHolder*>( |
|
230 aCompartment->StatesHolder()); |
|
231 |
|
232 if (sh->AddStateItemL(si, |
|
233 aStateRetentionPriority)) |
|
234 { |
|
235 si->iUsageCounter++; |
|
236 } |
|
237 } |
|
238 else |
|
239 { |
|
240 si->iUsageCounter++; |
|
241 } |
|
242 |
|
243 return si; |
|
244 } |
|
245 |
|
246 |
|
247 // ---------------------------------------------------------------------------- |
|
248 // CStateMgr::FreeStateL |
|
249 // free state |
|
250 // ---------------------------------------------------------------------------- |
|
251 // |
|
252 |
|
253 TInt CStateMgr::FreeStateL(CSigCompCompartment* aCompartment, |
|
254 const TDesC8& aPartialIdentifier) |
|
255 { |
|
256 |
|
257 TStateItem* si = FindStateItem(aPartialIdentifier); |
|
258 if (si) |
|
259 { |
|
260 if (aCompartment) |
|
261 { |
|
262 CSigCompCompartmentStatesHolder* sh = |
|
263 const_cast<CSigCompCompartmentStatesHolder*>( |
|
264 aCompartment->StatesHolder()); |
|
265 |
|
266 sh->RemoveStateItemL(si); |
|
267 return KErrNone; |
|
268 } |
|
269 } |
|
270 |
|
271 return KErrNotFound; |
|
272 } |
|
273 |
|
274 |
|
275 // ---------------------------------------------------------------------------- |
|
276 // CStateMgr::SetReturnedFeedbackL |
|
277 // set returned feedback in compartment |
|
278 // ---------------------------------------------------------------------------- |
|
279 // |
|
280 |
|
281 void CStateMgr::SetReturnedFeedbackL(CSigCompCompartment& aCompartment, |
|
282 const TDesC8& aReturnedFeedback) const |
|
283 { |
|
284 aCompartment.SetReturnedFeedbackL(aReturnedFeedback); |
|
285 } |
|
286 |
|
287 |
|
288 // ---------------------------------------------------------------------------- |
|
289 // CStateMgr::SetRequestedFeedbackL |
|
290 // set requested feedback in compartment |
|
291 // ---------------------------------------------------------------------------- |
|
292 // |
|
293 |
|
294 void CStateMgr::SetRequestedFeedbackL(CSigCompCompartment& aCompartment, |
|
295 const TDesC8& aRequestedFeedback) const |
|
296 { |
|
297 aCompartment.SetRequestedFeedbackL(aRequestedFeedback); |
|
298 } |
|
299 |
|
300 |
|
301 // ---------------------------------------------------------------------------- |
|
302 // CStateMgr::SetReturnedParametersL |
|
303 // set returned parameters in compartment |
|
304 // ---------------------------------------------------------------------------- |
|
305 // |
|
306 |
|
307 void CStateMgr::SetReturnedParametersL(CSigCompCompartment& aCompartment, |
|
308 const TDesC8& aReturnedParameters) const |
|
309 { |
|
310 aCompartment.SetReturnedParametersL(aReturnedParameters); |
|
311 } |