blob: 0021a2003c7eadbdf95166ca2942a0cb9dbb493b [file] [log] [blame]
Patrick Benavoli68a91282011-08-31 11:23:23 +02001/* <auto_header>
2 * <FILENAME>
3 *
4 * INTEL CONFIDENTIAL
5 * Copyright © 2011 Intel
6 * Corporation All Rights Reserved.
7 *
8 * The source code contained or described herein and all documents related to
9 * the source code ("Material") are owned by Intel Corporation or its suppliers
10 * or licensors. Title to the Material remains with Intel Corporation or its
11 * suppliers and licensors. The Material contains trade secrets and proprietary
12 * and confidential information of Intel or its suppliers and licensors. The
13 * Material is protected by worldwide copyright and trade secret laws and
14 * treaty provisions. No part of the Material may be used, copied, reproduced,
15 * modified, published, uploaded, posted, transmitted, distributed, or
16 * disclosed in any way without Intel’s prior express written permission.
17 *
18 * No license under any patent, copyright, trade secret or other intellectual
19 * property right is granted to or conferred upon you by disclosure or delivery
20 * of the Materials, either expressly, by implication, inducement, estoppel or
21 * otherwise. Any license under such intellectual property rights must be
22 * express and approved by Intel in writing.
23 *
24 * AUTHOR: Patrick Benavoli (patrickx.benavoli@intel.com)
25 * CREATED: 2011-06-01
26 * UPDATED: 2011-07-27
27 *
28 *
29 * </auto_header>
30 */
31#include "ArrayParameter.h"
32#include <sstream> // for istringstream
33#include "Tokenizer.h"
34#include "ParameterType.h"
35#include "ParameterAccessContext.h"
36#include "ConfigurationAccessContext.h"
37#include "ParameterBlackboard.h"
Patrick Benavoli065264a2011-11-20 15:46:41 +010038#include <assert.h>
Patrick Benavoli68a91282011-08-31 11:23:23 +020039
40#define base CParameter
41
Patrick Benavoli2ecf9002011-08-31 11:23:24 +020042CArrayParameter::CArrayParameter(const string& strName, const CTypeElement* pTypeElement) : base(strName, pTypeElement)
Patrick Benavoli68a91282011-08-31 11:23:23 +020043{
44}
45
46uint32_t CArrayParameter::getFootPrint() const
47{
Patrick Benavoli2ecf9002011-08-31 11:23:24 +020048 return getSize() * getArrayLength();
49}
50
51// Array length
52uint32_t CArrayParameter::getArrayLength() const
53{
54 return getTypeElement()->getArrayLength();
55}
56
57// Element properties
58void CArrayParameter::showProperties(string& strResult) const
59{
60 base::showProperties(strResult);
61
62 // Array length
63 strResult += "Array length: ";
64 strResult += toString(getArrayLength());
65 strResult += "\n";
Patrick Benavoli68a91282011-08-31 11:23:23 +020066}
67
68// XML configuration settings parsing
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020069bool CArrayParameter::serializeXmlSettings(CXmlElement& xmlConfigurationSettingsElementContent, CConfigurationAccessContext& configurationAccessContext) const
Patrick Benavoli68a91282011-08-31 11:23:23 +020070{
71 // Check for value space
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020072 handleValueSpaceAttribute(xmlConfigurationSettingsElementContent, configurationAccessContext);
Patrick Benavoli68a91282011-08-31 11:23:23 +020073
74 // Handle access
75 if (!configurationAccessContext.serializeOut()) {
76
77 // Actually set values to blackboard
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020078 if (!setValues(0, configurationAccessContext.getBaseOffset(), xmlConfigurationSettingsElementContent.getTextContent(), configurationAccessContext)) {
Patrick Benavoli68a91282011-08-31 11:23:23 +020079
80 return false;
81 }
82 } else {
83
84 // Get string value
85 string strValue;
86
87 // Whole array requested
88 getValues(configurationAccessContext.getBaseOffset(), strValue, configurationAccessContext);
89
90 // Populate value into xml text node
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020091 xmlConfigurationSettingsElementContent.setTextContent(strValue);
Patrick Benavoli68a91282011-08-31 11:23:23 +020092 }
93
94 // Done
95 return true;
96}
97
98// User set/get
Patrick Benavoli065264a2011-11-20 15:46:41 +010099bool CArrayParameter::accessValue(CPathNavigator& pathNavigator, string& strValue, bool bSet, CParameterAccessContext& parameterAccessContext) const
Patrick Benavoli68a91282011-08-31 11:23:23 +0200100{
Patrick Benavoli68a91282011-08-31 11:23:23 +0200101 uint32_t uiIndex;
102
Patrick Benavoli065264a2011-11-20 15:46:41 +0100103 if (!getIndex(pathNavigator, uiIndex, parameterAccessContext)) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200104
105 return false;
106 }
Patrick Benavoli4bed9212011-10-27 14:18:00 +0200107
Patrick Benavoli065264a2011-11-20 15:46:41 +0100108 if (bSet) {
109 // Set
110 if (uiIndex == (uint32_t)-1) {
Patrick Benavoli4bed9212011-10-27 14:18:00 +0200111
Patrick Benavoli065264a2011-11-20 15:46:41 +0100112 // No index provided, start with 0
113 uiIndex = 0;
114 }
Patrick Benavoli4bed9212011-10-27 14:18:00 +0200115
Patrick Benavoli065264a2011-11-20 15:46:41 +0100116 // Actually set values
117 if (!setValues(uiIndex, 0, strValue, parameterAccessContext)) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200118
Patrick Benavoli065264a2011-11-20 15:46:41 +0100119 return false;
120 }
Patrick Benavoli68a91282011-08-31 11:23:23 +0200121
Patrick Benavoli065264a2011-11-20 15:46:41 +0100122 // Synchronize
123 if (parameterAccessContext.getAutoSync() && !sync(parameterAccessContext)) {
124
125 // Append parameter path to error
126 parameterAccessContext.appendToError(" " + getPath());
127
128 return false;
129 }
Patrick Benavoli68a91282011-08-31 11:23:23 +0200130 } else {
Patrick Benavoli065264a2011-11-20 15:46:41 +0100131 // Get
132 if (uiIndex == (uint32_t)-1) {
133
134 // Whole array requested
135 getValues(0, strValue, parameterAccessContext);
136
137 } else {
138 // Scalar requested
139 doGetValue(strValue, getOffset() + uiIndex * getSize(), parameterAccessContext);
140 }
Patrick Benavoli68a91282011-08-31 11:23:23 +0200141 }
142
143 return true;
144}
145
Patrick Benavoli065264a2011-11-20 15:46:41 +0100146// Boolean
147bool CArrayParameter::accessAsBooleanArray(vector<bool>& abValues, bool bSet, CParameterAccessContext& parameterAccessContext) const
148{
149 return accessValues(abValues, bSet, parameterAccessContext);
150}
151
152// Integer
153bool CArrayParameter::accessAsIntegerArray(vector<uint32_t>& auiValues, bool bSet, CParameterAccessContext& parameterAccessContext) const
154{
155 return accessValues(auiValues, bSet, parameterAccessContext);
156}
157
158// Signed Integer Access
159bool CArrayParameter::accessAsSignedIntegerArray(vector<int32_t>& aiValues, bool bSet, CParameterAccessContext& parameterAccessContext) const
160{
161 return accessValues(aiValues, bSet, parameterAccessContext);
162}
163
164// Double Access
165bool CArrayParameter::accessAsDoubleArray(vector<double>& adValues, bool bSet, CParameterAccessContext& parameterAccessContext) const
166{
167 return accessValues(adValues, bSet, parameterAccessContext);
168}
169
170// String Access
171bool CArrayParameter::accessAsStringArray(vector<string>& astrValues, bool bSet, CParameterAccessContext& parameterAccessContext) const
172{
173 return accessValues(astrValues, bSet, parameterAccessContext);
174}
175
176// Dump
Patrick Benavoli68a91282011-08-31 11:23:23 +0200177void CArrayParameter::logValue(string& strValue, CErrorContext& errorContext) const
178{
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200179 // Parameter context
Patrick Benavoli065264a2011-11-20 15:46:41 +0100180 CParameterAccessContext& parameterAccessContext = static_cast<CParameterAccessContext&>(errorContext);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200181
182 // Dump values
Patrick Benavoli065264a2011-11-20 15:46:41 +0100183 getValues(0, strValue, parameterAccessContext);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200184}
185
Patrick Benavoli6ccab9d2011-11-10 23:21:01 +0100186// Used for simulation and virtual subsystems
Patrick Benavoli68a91282011-08-31 11:23:23 +0200187void CArrayParameter::setDefaultValues(CParameterAccessContext& parameterAccessContext) const
188{
189 // Get default value from type
190 uint32_t uiDefaultValue = static_cast<const CParameterType*>(getTypeElement())->getDefaultValue();
191
192 // Write blackboard
193 CParameterBlackboard* pBlackboard = parameterAccessContext.getParameterBlackboard();
194
195 // Process
196 uint32_t uiValueIndex;
197 uint32_t uiSize = getSize();
198 uint32_t uiOffset = getOffset();
199 bool bSubsystemIsBigEndian = parameterAccessContext.isBigEndianSubsystem();
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200200 uint32_t uiArrayLength = getArrayLength();
Patrick Benavoli68a91282011-08-31 11:23:23 +0200201
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200202 for (uiValueIndex = 0; uiValueIndex < uiArrayLength; uiValueIndex++) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200203
204 // Beware this code works on little endian architectures only!
Patrick Benavoli1352ae52011-10-21 16:48:04 +0200205 pBlackboard->writeInteger(&uiDefaultValue, uiSize, uiOffset, bSubsystemIsBigEndian);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200206
207 uiOffset += uiSize;
208 }
209}
210
211// Index from path
Patrick Benavoli065264a2011-11-20 15:46:41 +0100212bool CArrayParameter::getIndex(CPathNavigator& pathNavigator, uint32_t& uiIndex, CParameterAccessContext& parameterAccessContext) const
Patrick Benavoli68a91282011-08-31 11:23:23 +0200213{
214 uiIndex = (uint32_t)-1;
215
216 string* pStrChildName = pathNavigator.next();
217
218 if (pStrChildName) {
219
220 // Check index is numeric
221 istringstream iss(*pStrChildName);
222
223 iss >> uiIndex;
224
225 if (!iss) {
226
Patrick Benavoli065264a2011-11-20 15:46:41 +0100227 parameterAccessContext.setError("Expected numerical expression as last item in " + pathNavigator.getCurrentPath());
Patrick Benavoli68a91282011-08-31 11:23:23 +0200228
229 return false;
230 }
231
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200232 if (uiIndex >= getArrayLength()) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200233 ostringstream oss;
234
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200235 oss << "Provided index out of range (max is " << getArrayLength() - 1 << ")";
Patrick Benavoli68a91282011-08-31 11:23:23 +0200236
Patrick Benavoli065264a2011-11-20 15:46:41 +0100237 parameterAccessContext.setError(oss.str());
Patrick Benavoli68a91282011-08-31 11:23:23 +0200238
239 return false;
240 }
241
242 // Check no other item provided in path
243 pStrChildName = pathNavigator.next();
244
245 if (pStrChildName) {
246
247 // Should be leaf element
Patrick Benavoli065264a2011-11-20 15:46:41 +0100248 parameterAccessContext.setError("Path not found: " + pathNavigator.getCurrentPath());
Patrick Benavoli68a91282011-08-31 11:23:23 +0200249
250 return false;
251 }
252 }
253
254 return true;
255}
256
257// Common set value processing
Patrick Benavoli065264a2011-11-20 15:46:41 +0100258bool CArrayParameter::setValues(uint32_t uiStartIndex, uint32_t uiBaseOffset, const string& strValue, CParameterAccessContext& parameterAccessContext) const
Patrick Benavoli68a91282011-08-31 11:23:23 +0200259{
260 // Deal with value(s)
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200261 Tokenizer tok(strValue, DEFAULT_DELIMITER + ",");
Patrick Benavoli68a91282011-08-31 11:23:23 +0200262
263 vector<string> astrValues = tok.split();
264 uint32_t uiNbValues = astrValues.size();
265
266 // Check number of provided values
Patrick Benavoli065264a2011-11-20 15:46:41 +0100267 if (uiNbValues + uiStartIndex > getArrayLength()) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200268
269 // Out of bounds
Patrick Benavoli065264a2011-11-20 15:46:41 +0100270 parameterAccessContext.setError("Too many values provided");
Patrick Benavoli68a91282011-08-31 11:23:23 +0200271
272 return false;
273 }
274
275 // Process
276 uint32_t uiValueIndex;
277 uint32_t uiSize = getSize();
278 uint32_t uiOffset = getOffset() + uiStartIndex * uiSize - uiBaseOffset;
279
280 for (uiValueIndex = 0; uiValueIndex < uiNbValues; uiValueIndex++) {
281
Patrick Benavoli065264a2011-11-20 15:46:41 +0100282 if (!doSetValue(astrValues[uiValueIndex], uiOffset, parameterAccessContext)) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200283
284 // Append parameter path to error
Patrick Benavoli065264a2011-11-20 15:46:41 +0100285 parameterAccessContext.appendToError(" " + getPath() + "/" + toString(uiValueIndex + uiStartIndex));
Patrick Benavoli68a91282011-08-31 11:23:23 +0200286
287 return false;
288 }
289
290 uiOffset += uiSize;
291 }
292 return true;
293}
294
295// Common get value processing
Patrick Benavoli065264a2011-11-20 15:46:41 +0100296void CArrayParameter::getValues(uint32_t uiBaseOffset, string& strValues, CParameterAccessContext& parameterAccessContext) const
Patrick Benavoli68a91282011-08-31 11:23:23 +0200297{
298 uint32_t uiValueIndex;
299 uint32_t uiSize = getSize();
300 uint32_t uiOffset = getOffset() - uiBaseOffset;
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200301 uint32_t uiArrayLength = getArrayLength();
Patrick Benavoli68a91282011-08-31 11:23:23 +0200302
Patrick Benavoli4bed9212011-10-27 14:18:00 +0200303 strValues.clear();
304
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200305 bool bFirst = true;
306
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200307 for (uiValueIndex = 0; uiValueIndex < uiArrayLength; uiValueIndex++) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200308 string strReadValue;
309
Patrick Benavoli065264a2011-11-20 15:46:41 +0100310 doGetValue(strReadValue, uiOffset, parameterAccessContext);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200311
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200312 if (!bFirst) {
313
314 strValues += " ";
315 } else {
316
317 bFirst = false;
318 }
319
320 strValues += strReadValue;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200321
322 uiOffset += uiSize;
323 }
324}
Patrick Benavoli065264a2011-11-20 15:46:41 +0100325
326// Generic Access
327template <typename type>
328bool CArrayParameter::accessValues(vector<type>& values, bool bSet, CParameterAccessContext& parameterAccessContext) const
329{
330 bool bSuccess;
331
332 if (bSet) {
333
334 if (setValues(values, parameterAccessContext)) {
335
336 // Synchronize
337 bSuccess = sync(parameterAccessContext);
338 } else {
339
340 bSuccess = false;
341 }
342 } else {
343
344 bSuccess = getValues(values, parameterAccessContext);
345 }
346 if (!bSuccess) {
347
348 // Append parameter path to error
349 parameterAccessContext.appendToError(" " + getPath());
350 }
351 return bSuccess;
352}
353
354template <typename type>
355bool CArrayParameter::setValues(const vector<type>& values, CParameterAccessContext& parameterAccessContext) const
356{
357 uint32_t uiNbValues = getArrayLength();
358 uint32_t uiValueIndex;
359 uint32_t uiSize = getSize();
360 uint32_t uiOffset = getOffset();
361
362 assert(values.size() == uiNbValues);
363
364 // Process
365 for (uiValueIndex = 0; uiValueIndex < uiNbValues; uiValueIndex++) {
366
367 if (!doSet(values[uiValueIndex], uiOffset, parameterAccessContext)) {
368
369 return false;
370 }
371
372 uiOffset += uiSize;
373 }
374
375 return true;
376}
377
378template <typename type>
379bool CArrayParameter::getValues(vector<type>& values, CParameterAccessContext& parameterAccessContext) const
380{
381 uint32_t uiNbValues = getArrayLength();
382 uint32_t uiValueIndex;
383 uint32_t uiSize = getSize();
384 uint32_t uiOffset = getOffset();
385
386 values.clear();
387
388 for (uiValueIndex = 0; uiValueIndex < uiNbValues; uiValueIndex++) {
389 type readValue;
390
391 if (!doGet(readValue, uiOffset, parameterAccessContext)) {
392
393 return false;
394 }
395
396 values.push_back(readValue);
397
398 uiOffset += uiSize;
399 }
400 return true;
401}
402
403template <typename type>
404bool CArrayParameter::doSet(type value, uint32_t uiOffset, CParameterAccessContext& parameterAccessContext) const
405{
406 uint32_t uiData;
407
408 if (!static_cast<const CParameterType*>(getTypeElement())->toBlackboard(value, uiData, parameterAccessContext)) {
409
410 return false;
411 }
412 // Write blackboard
413 CParameterBlackboard* pBlackboard = parameterAccessContext.getParameterBlackboard();
414
415 // Beware this code works on little endian architectures only!
416 pBlackboard->writeInteger(&uiData, getSize(), uiOffset, parameterAccessContext.isBigEndianSubsystem());
417
418 return true;
419}
420
421template <typename type>
422bool CArrayParameter::doGet(type& value, uint32_t uiOffset, CParameterAccessContext& parameterAccessContext) const
423{
424 uint32_t uiData = 0;
425
426 // Read blackboard
427 const CParameterBlackboard* pBlackboard = parameterAccessContext.getParameterBlackboard();
428
429 // Beware this code works on little endian architectures only!
430 pBlackboard->readInteger(&uiData, getSize(), uiOffset, parameterAccessContext.isBigEndianSubsystem());
431
432 return static_cast<const CParameterType*>(getTypeElement())->fromBlackboard(value, uiData, parameterAccessContext);
433}
434