blob: e777c61f7b962d0e20e0f57b3811b40318018a67 [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
Patrick Benavoli4bed9212011-10-27 14:18:00 +0200107 // Check for dynamic access
108 if (!checkForDynamicAccess(parameterContext)) {
109
110 return false;
111 }
112
Patrick Benavoli68a91282011-08-31 11:23:23 +0200113 if (uiStartIndex == (uint32_t)-1) {
114
115 // No index provided, start with 0
116 uiStartIndex = 0;
117 }
118
119 // Actually set values
120 if (!setValues(uiStartIndex, 0, strValue, parameterContext)) {
121
122 return false;
123 }
124
125 // Synchronize
126 if (parameterContext.getAutoSync() && !sync(parameterContext)) {
127
128 // Append parameter path to error
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200129 parameterContext.appendToError(" " + getPath());
Patrick Benavoli68a91282011-08-31 11:23:23 +0200130
131 return false;
132 }
133
134 return true;
135}
136
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200137bool CArrayParameter::getValue(CPathNavigator& pathNavigator, string& strValue, CParameterAccessContext& parameterContext) const
Patrick Benavoli68a91282011-08-31 11:23:23 +0200138{
Patrick Benavoli68a91282011-08-31 11:23:23 +0200139 uint32_t uiIndex;
140
141 if (!getIndex(pathNavigator, uiIndex, parameterContext)) {
142
143 return false;
144 }
Patrick Benavoli4bed9212011-10-27 14:18:00 +0200145
146 // Check for dynamic access
147 if (!checkForDynamicAccess(parameterContext)) {
148
149 return false;
150 }
151
Patrick Benavoli68a91282011-08-31 11:23:23 +0200152 if (uiIndex == (uint32_t)-1) {
153
154 // Whole array requested
155 getValues(0, strValue, parameterContext);
156
157 } else {
158 // Scalar requested
159 doGetValue(strValue, getOffset() + uiIndex * getSize(), parameterContext);
160 }
161
162 return true;
163}
164
165void CArrayParameter::logValue(string& strValue, CErrorContext& errorContext) const
166{
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200167 // Parameter context
Patrick Benavoli68a91282011-08-31 11:23:23 +0200168 CParameterAccessContext& parameterContext = static_cast<CParameterAccessContext&>(errorContext);
169
170 // Dump values
171 getValues(0, strValue, parameterContext);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200172}
173
174// Used for simulation only
175void CArrayParameter::setDefaultValues(CParameterAccessContext& parameterAccessContext) const
176{
177 // Get default value from type
178 uint32_t uiDefaultValue = static_cast<const CParameterType*>(getTypeElement())->getDefaultValue();
179
180 // Write blackboard
181 CParameterBlackboard* pBlackboard = parameterAccessContext.getParameterBlackboard();
182
183 // Process
184 uint32_t uiValueIndex;
185 uint32_t uiSize = getSize();
186 uint32_t uiOffset = getOffset();
187 bool bSubsystemIsBigEndian = parameterAccessContext.isBigEndianSubsystem();
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200188 uint32_t uiArrayLength = getArrayLength();
Patrick Benavoli68a91282011-08-31 11:23:23 +0200189
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200190 for (uiValueIndex = 0; uiValueIndex < uiArrayLength; uiValueIndex++) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200191
192 // Beware this code works on little endian architectures only!
Patrick Benavoli1352ae52011-10-21 16:48:04 +0200193 pBlackboard->writeInteger(&uiDefaultValue, uiSize, uiOffset, bSubsystemIsBigEndian);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200194
195 uiOffset += uiSize;
196 }
197}
198
199// Index from path
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200200bool CArrayParameter::getIndex(CPathNavigator& pathNavigator, uint32_t& uiIndex, CParameterAccessContext& parameterContext) const
Patrick Benavoli68a91282011-08-31 11:23:23 +0200201{
202 uiIndex = (uint32_t)-1;
203
204 string* pStrChildName = pathNavigator.next();
205
206 if (pStrChildName) {
207
208 // Check index is numeric
209 istringstream iss(*pStrChildName);
210
211 iss >> uiIndex;
212
213 if (!iss) {
214
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200215 parameterContext.setError("Expected numerical expression as last item in " + pathNavigator.getCurrentPath());
Patrick Benavoli68a91282011-08-31 11:23:23 +0200216
217 return false;
218 }
219
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200220 if (uiIndex >= getArrayLength()) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200221 ostringstream oss;
222
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200223 oss << "Provided index out of range (max is " << getArrayLength() - 1 << ")";
Patrick Benavoli68a91282011-08-31 11:23:23 +0200224
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200225 parameterContext.setError(oss.str());
Patrick Benavoli68a91282011-08-31 11:23:23 +0200226
227 return false;
228 }
229
230 // Check no other item provided in path
231 pStrChildName = pathNavigator.next();
232
233 if (pStrChildName) {
234
235 // Should be leaf element
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200236 parameterContext.setError("Path not found: " + pathNavigator.getCurrentPath());
Patrick Benavoli68a91282011-08-31 11:23:23 +0200237
238 return false;
239 }
240 }
241
242 return true;
243}
244
245// Common set value processing
246bool CArrayParameter::setValues(uint32_t uiStartIndex, uint32_t uiBaseOffset, const string& strValue, CParameterAccessContext& parameterContext) const
247{
248 // Deal with value(s)
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200249 Tokenizer tok(strValue, DEFAULT_DELIMITER + ",");
Patrick Benavoli68a91282011-08-31 11:23:23 +0200250
251 vector<string> astrValues = tok.split();
252 uint32_t uiNbValues = astrValues.size();
253
254 // Check number of provided values
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200255 if (uiNbValues + uiStartIndex > getArrayLength()) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200256
257 // Out of bounds
258 parameterContext.setError("Too many values provided");
259
260 return false;
261 }
262
263 // Process
264 uint32_t uiValueIndex;
265 uint32_t uiSize = getSize();
266 uint32_t uiOffset = getOffset() + uiStartIndex * uiSize - uiBaseOffset;
267
268 for (uiValueIndex = 0; uiValueIndex < uiNbValues; uiValueIndex++) {
269
270 if (!doSetValue(astrValues[uiValueIndex], uiOffset, parameterContext)) {
271
272 // Append parameter path to error
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200273 parameterContext.appendToError(" " + getPath() + "/" + toString(uiValueIndex + uiStartIndex));
Patrick Benavoli68a91282011-08-31 11:23:23 +0200274
275 return false;
276 }
277
278 uiOffset += uiSize;
279 }
280 return true;
281}
282
283// Common get value processing
284void CArrayParameter::getValues(uint32_t uiBaseOffset, string& strValues, CParameterAccessContext& parameterContext) const
285{
286 uint32_t uiValueIndex;
287 uint32_t uiSize = getSize();
288 uint32_t uiOffset = getOffset() - uiBaseOffset;
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200289 uint32_t uiArrayLength = getArrayLength();
Patrick Benavoli68a91282011-08-31 11:23:23 +0200290
Patrick Benavoli4bed9212011-10-27 14:18:00 +0200291 strValues.clear();
292
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200293 bool bFirst = true;
294
Patrick Benavoli2ecf9002011-08-31 11:23:24 +0200295 for (uiValueIndex = 0; uiValueIndex < uiArrayLength; uiValueIndex++) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200296 string strReadValue;
297
298 doGetValue(strReadValue, uiOffset, parameterContext);
299
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200300 if (!bFirst) {
301
302 strValues += " ";
303 } else {
304
305 bFirst = false;
306 }
307
308 strValues += strReadValue;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200309
310 uiOffset += uiSize;
311 }
312}