blob: 5e0ea1edb23408f0c0b3da92b8450a4545e1c993 [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"
38
39#define base CParameter
40
Patrick Benavoli2ecf9002011-08-31 11:23:24 +020041CArrayParameter::CArrayParameter(const string& strName, const CTypeElement* pTypeElement) : base(strName, pTypeElement)
Patrick Benavoli68a91282011-08-31 11:23:23 +020042{
43}
44
45uint32_t CArrayParameter::getFootPrint() const
46{
Patrick Benavoli2ecf9002011-08-31 11:23:24 +020047 return getSize() * getArrayLength();
48}
49
50// Array length
51uint32_t CArrayParameter::getArrayLength() const
52{
53 return getTypeElement()->getArrayLength();
54}
55
56// Element properties
57void CArrayParameter::showProperties(string& strResult) const
58{
59 base::showProperties(strResult);
60
61 // Array length
62 strResult += "Array length: ";
63 strResult += toString(getArrayLength());
64 strResult += "\n";
Patrick Benavoli68a91282011-08-31 11:23:23 +020065}
66
67// XML configuration settings parsing
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020068bool CArrayParameter::serializeXmlSettings(CXmlElement& xmlConfigurationSettingsElementContent, CConfigurationAccessContext& configurationAccessContext) const
Patrick Benavoli68a91282011-08-31 11:23:23 +020069{
70 // Check for value space
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020071 handleValueSpaceAttribute(xmlConfigurationSettingsElementContent, configurationAccessContext);
Patrick Benavoli68a91282011-08-31 11:23:23 +020072
73 // Handle access
74 if (!configurationAccessContext.serializeOut()) {
75
76 // Actually set values to blackboard
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020077 if (!setValues(0, configurationAccessContext.getBaseOffset(), xmlConfigurationSettingsElementContent.getTextContent(), configurationAccessContext)) {
Patrick Benavoli68a91282011-08-31 11:23:23 +020078
79 return false;
80 }
81 } else {
82
83 // Get string value
84 string strValue;
85
86 // Whole array requested
87 getValues(configurationAccessContext.getBaseOffset(), strValue, configurationAccessContext);
88
89 // Populate value into xml text node
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020090 xmlConfigurationSettingsElementContent.setTextContent(strValue);
Patrick Benavoli68a91282011-08-31 11:23:23 +020091 }
92
93 // Done
94 return true;
95}
96
97// User set/get
Patrick Benavoli2ecf9002011-08-31 11:23:24 +020098bool CArrayParameter::setValue(CPathNavigator& pathNavigator, const string& strValue, CParameterAccessContext& parameterContext) const
Patrick Benavoli68a91282011-08-31 11:23:23 +020099{
Patrick Benavoli68a91282011-08-31 11:23:23 +0200100 uint32_t uiStartIndex;
101
102 if (!getIndex(pathNavigator, uiStartIndex, parameterContext)) {
103
104 return false;
105 }
106
107 if (uiStartIndex == (uint32_t)-1) {
108
109 // No index provided, start with 0
110 uiStartIndex = 0;
111 }
112
113 // Actually set values
114 if (!setValues(uiStartIndex, 0, strValue, parameterContext)) {
115
116 return false;
117 }
118
119 // Synchronize
120 if (parameterContext.getAutoSync() && !sync(parameterContext)) {
121
122 // Append parameter path to error
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200123 parameterContext.appendToError(" " + getPath());
Patrick Benavoli68a91282011-08-31 11:23:23 +0200124
125 return false;
126 }
127
128 return true;
129}
130
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200131bool CArrayParameter::getValue(CPathNavigator& pathNavigator, string& strValue, CParameterAccessContext& parameterContext) const
Patrick Benavoli68a91282011-08-31 11:23:23 +0200132{
Patrick Benavoli68a91282011-08-31 11:23:23 +0200133 uint32_t uiIndex;
134
135 if (!getIndex(pathNavigator, uiIndex, parameterContext)) {
136
137 return false;
138 }
139 if (uiIndex == (uint32_t)-1) {
140
141 // Whole array requested
142 getValues(0, strValue, parameterContext);
143
144 } else {
145 // Scalar requested
146 doGetValue(strValue, getOffset() + uiIndex * getSize(), parameterContext);
147 }
148
149 return true;
150}
151
152void CArrayParameter::logValue(string& strValue, CErrorContext& errorContext) const
153{
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200154 // Parameter context
Patrick Benavoli68a91282011-08-31 11:23:23 +0200155 CParameterAccessContext& parameterContext = static_cast<CParameterAccessContext&>(errorContext);
156
157 // Dump values
158 getValues(0, strValue, parameterContext);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200159}
160
161// Used for simulation only
162void CArrayParameter::setDefaultValues(CParameterAccessContext& parameterAccessContext) const
163{
164 // Get default value from type
165 uint32_t uiDefaultValue = static_cast<const CParameterType*>(getTypeElement())->getDefaultValue();
166
167 // Write blackboard
168 CParameterBlackboard* pBlackboard = parameterAccessContext.getParameterBlackboard();
169
170 // Process
171 uint32_t uiValueIndex;
172 uint32_t uiSize = getSize();
173 uint32_t uiOffset = getOffset();
174 bool bSubsystemIsBigEndian = parameterAccessContext.isBigEndianSubsystem();
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200175 uint32_t uiArrayLength = getArrayLength();
Patrick Benavoli68a91282011-08-31 11:23:23 +0200176
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200177 for (uiValueIndex = 0; uiValueIndex < uiArrayLength; uiValueIndex++) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200178
179 // Beware this code works on little endian architectures only!
180 pBlackboard->write(&uiDefaultValue, uiSize, uiOffset, bSubsystemIsBigEndian);
181
182 uiOffset += uiSize;
183 }
184}
185
186// Index from path
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200187bool CArrayParameter::getIndex(CPathNavigator& pathNavigator, uint32_t& uiIndex, CParameterAccessContext& parameterContext) const
Patrick Benavoli68a91282011-08-31 11:23:23 +0200188{
189 uiIndex = (uint32_t)-1;
190
191 string* pStrChildName = pathNavigator.next();
192
193 if (pStrChildName) {
194
195 // Check index is numeric
196 istringstream iss(*pStrChildName);
197
198 iss >> uiIndex;
199
200 if (!iss) {
201
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200202 parameterContext.setError("Expected numerical expression as last item in " + pathNavigator.getCurrentPath());
Patrick Benavoli68a91282011-08-31 11:23:23 +0200203
204 return false;
205 }
206
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200207 if (uiIndex >= getArrayLength()) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200208 ostringstream oss;
209
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200210 oss << "Provided index out of range (max is " << getArrayLength() - 1 << ")";
Patrick Benavoli68a91282011-08-31 11:23:23 +0200211
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200212 parameterContext.setError(oss.str());
Patrick Benavoli68a91282011-08-31 11:23:23 +0200213
214 return false;
215 }
216
217 // Check no other item provided in path
218 pStrChildName = pathNavigator.next();
219
220 if (pStrChildName) {
221
222 // Should be leaf element
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200223 parameterContext.setError("Path not found: " + pathNavigator.getCurrentPath());
Patrick Benavoli68a91282011-08-31 11:23:23 +0200224
225 return false;
226 }
227 }
228
229 return true;
230}
231
232// Common set value processing
233bool CArrayParameter::setValues(uint32_t uiStartIndex, uint32_t uiBaseOffset, const string& strValue, CParameterAccessContext& parameterContext) const
234{
235 // Deal with value(s)
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200236 Tokenizer tok(strValue, DEFAULT_DELIMITER + ",");
Patrick Benavoli68a91282011-08-31 11:23:23 +0200237
238 vector<string> astrValues = tok.split();
239 uint32_t uiNbValues = astrValues.size();
240
241 // Check number of provided values
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200242 if (uiNbValues + uiStartIndex > getArrayLength()) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200243
244 // Out of bounds
245 parameterContext.setError("Too many values provided");
246
247 return false;
248 }
249
250 // Process
251 uint32_t uiValueIndex;
252 uint32_t uiSize = getSize();
253 uint32_t uiOffset = getOffset() + uiStartIndex * uiSize - uiBaseOffset;
254
255 for (uiValueIndex = 0; uiValueIndex < uiNbValues; uiValueIndex++) {
256
257 if (!doSetValue(astrValues[uiValueIndex], uiOffset, parameterContext)) {
258
259 // Append parameter path to error
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200260 parameterContext.appendToError(" " + getPath() + "/" + toString(uiValueIndex + uiStartIndex));
Patrick Benavoli68a91282011-08-31 11:23:23 +0200261
262 return false;
263 }
264
265 uiOffset += uiSize;
266 }
267 return true;
268}
269
270// Common get value processing
271void CArrayParameter::getValues(uint32_t uiBaseOffset, string& strValues, CParameterAccessContext& parameterContext) const
272{
273 uint32_t uiValueIndex;
274 uint32_t uiSize = getSize();
275 uint32_t uiOffset = getOffset() - uiBaseOffset;
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200276 uint32_t uiArrayLength = getArrayLength();
Patrick Benavoli68a91282011-08-31 11:23:23 +0200277
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200278 bool bFirst = true;
279
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200280 for (uiValueIndex = 0; uiValueIndex < uiArrayLength; uiValueIndex++) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200281 string strReadValue;
282
283 doGetValue(strReadValue, uiOffset, parameterContext);
284
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200285 if (!bFirst) {
286
287 strValues += " ";
288 } else {
289
290 bFirst = false;
291 }
292
293 strValues += strReadValue;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200294
295 uiOffset += uiSize;
296 }
297}