Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 1 | /* <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 Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 41 | CArrayParameter::CArrayParameter(const string& strName, const CTypeElement* pTypeElement) : base(strName, pTypeElement) |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | |
| 45 | uint32_t CArrayParameter::getFootPrint() const |
| 46 | { |
Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 47 | return getSize() * getArrayLength(); |
| 48 | } |
| 49 | |
| 50 | // Array length |
| 51 | uint32_t CArrayParameter::getArrayLength() const |
| 52 | { |
| 53 | return getTypeElement()->getArrayLength(); |
| 54 | } |
| 55 | |
| 56 | // Element properties |
| 57 | void 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 Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // XML configuration settings parsing |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 68 | bool CArrayParameter::serializeXmlSettings(CXmlElement& xmlConfigurationSettingsElementContent, CConfigurationAccessContext& configurationAccessContext) const |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 69 | { |
| 70 | // Check for value space |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 71 | handleValueSpaceAttribute(xmlConfigurationSettingsElementContent, configurationAccessContext); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 72 | |
| 73 | // Handle access |
| 74 | if (!configurationAccessContext.serializeOut()) { |
| 75 | |
| 76 | // Actually set values to blackboard |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 77 | if (!setValues(0, configurationAccessContext.getBaseOffset(), xmlConfigurationSettingsElementContent.getTextContent(), configurationAccessContext)) { |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 78 | |
| 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 Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 90 | xmlConfigurationSettingsElementContent.setTextContent(strValue); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // Done |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | // User set/get |
Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 98 | bool CArrayParameter::setValue(CPathNavigator& pathNavigator, const string& strValue, CParameterAccessContext& parameterContext) const |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 99 | { |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 100 | 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 Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 123 | parameterContext.appendToError(" " + getPath()); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 124 | |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | return true; |
| 129 | } |
| 130 | |
Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 131 | bool CArrayParameter::getValue(CPathNavigator& pathNavigator, string& strValue, CParameterAccessContext& parameterContext) const |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 132 | { |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 133 | 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 | |
| 152 | void CArrayParameter::logValue(string& strValue, CErrorContext& errorContext) const |
| 153 | { |
Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 154 | // Parameter context |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 155 | CParameterAccessContext& parameterContext = static_cast<CParameterAccessContext&>(errorContext); |
| 156 | |
| 157 | // Dump values |
| 158 | getValues(0, strValue, parameterContext); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // Used for simulation only |
| 162 | void 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 Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 175 | uint32_t uiArrayLength = getArrayLength(); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 176 | |
Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 177 | for (uiValueIndex = 0; uiValueIndex < uiArrayLength; uiValueIndex++) { |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 178 | |
| 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 Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 187 | bool CArrayParameter::getIndex(CPathNavigator& pathNavigator, uint32_t& uiIndex, CParameterAccessContext& parameterContext) const |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 188 | { |
| 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 Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 202 | parameterContext.setError("Expected numerical expression as last item in " + pathNavigator.getCurrentPath()); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 203 | |
| 204 | return false; |
| 205 | } |
| 206 | |
Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 207 | if (uiIndex >= getArrayLength()) { |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 208 | ostringstream oss; |
| 209 | |
Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 210 | oss << "Provided index out of range (max is " << getArrayLength() - 1 << ")"; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 211 | |
Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 212 | parameterContext.setError(oss.str()); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 213 | |
| 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 Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 223 | parameterContext.setError("Path not found: " + pathNavigator.getCurrentPath()); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 224 | |
| 225 | return false; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | // Common set value processing |
| 233 | bool CArrayParameter::setValues(uint32_t uiStartIndex, uint32_t uiBaseOffset, const string& strValue, CParameterAccessContext& parameterContext) const |
| 234 | { |
| 235 | // Deal with value(s) |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 236 | Tokenizer tok(strValue, DEFAULT_DELIMITER + ","); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 237 | |
| 238 | vector<string> astrValues = tok.split(); |
| 239 | uint32_t uiNbValues = astrValues.size(); |
| 240 | |
| 241 | // Check number of provided values |
Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 242 | if (uiNbValues + uiStartIndex > getArrayLength()) { |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 243 | |
| 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 Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 260 | parameterContext.appendToError(" " + getPath() + "/" + toString(uiValueIndex + uiStartIndex)); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 261 | |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | uiOffset += uiSize; |
| 266 | } |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | // Common get value processing |
| 271 | void 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 Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 276 | uint32_t uiArrayLength = getArrayLength(); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 277 | |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 278 | bool bFirst = true; |
| 279 | |
Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame^] | 280 | for (uiValueIndex = 0; uiValueIndex < uiArrayLength; uiValueIndex++) { |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 281 | string strReadValue; |
| 282 | |
| 283 | doGetValue(strReadValue, uiOffset, parameterContext); |
| 284 | |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 285 | if (!bFirst) { |
| 286 | |
| 287 | strValues += " "; |
| 288 | } else { |
| 289 | |
| 290 | bFirst = false; |
| 291 | } |
| 292 | |
| 293 | strValues += strReadValue; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 294 | |
| 295 | uiOffset += uiSize; |
| 296 | } |
| 297 | } |