Kevin Rocard | 93250d1 | 2012-07-19 17:48:30 +0200 | [diff] [blame] | 1 | /* |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 2 | * INTEL CONFIDENTIAL |
| 3 | * Copyright © 2011 Intel |
| 4 | * Corporation All Rights Reserved. |
| 5 | * |
| 6 | * The source code contained or described herein and all documents related to |
| 7 | * the source code ("Material") are owned by Intel Corporation or its suppliers |
| 8 | * or licensors. Title to the Material remains with Intel Corporation or its |
| 9 | * suppliers and licensors. The Material contains trade secrets and proprietary |
| 10 | * and confidential information of Intel or its suppliers and licensors. The |
| 11 | * Material is protected by worldwide copyright and trade secret laws and |
| 12 | * treaty provisions. No part of the Material may be used, copied, reproduced, |
| 13 | * modified, published, uploaded, posted, transmitted, distributed, or |
| 14 | * disclosed in any way without Intel’s prior express written permission. |
| 15 | * |
| 16 | * No license under any patent, copyright, trade secret or other intellectual |
| 17 | * property right is granted to or conferred upon you by disclosure or delivery |
| 18 | * of the Materials, either expressly, by implication, inducement, estoppel or |
| 19 | * otherwise. Any license under such intellectual property rights must be |
| 20 | * express and approved by Intel in writing. |
| 21 | * |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 22 | * CREATED: 2011-06-01 |
| 23 | * UPDATED: 2011-07-27 |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 24 | */ |
| 25 | #include "SubsystemObject.h" |
Guillaume Denneulin | f2fd15a | 2012-12-20 17:53:29 +0100 | [diff] [blame] | 26 | #include "Subsystem.h" |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 27 | #include "InstanceConfigurableElement.h" |
| 28 | #include "ParameterBlackboard.h" |
Guillaume Denneulin | f2fd15a | 2012-12-20 17:53:29 +0100 | [diff] [blame] | 29 | #include "ParameterAccessContext.h" |
Guillaume Denneulin | 7fce1e3 | 2012-02-17 17:34:47 +0100 | [diff] [blame] | 30 | #include "MappingContext.h" |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 31 | #include <assert.h> |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <sstream> |
Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 35 | #include <stdarg.h> |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 36 | |
| 37 | CSubsystemObject::CSubsystemObject(CInstanceConfigurableElement* pInstanceConfigurableElement) |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 38 | : _pInstanceConfigurableElement(pInstanceConfigurableElement), |
| 39 | _uiDataSize(pInstanceConfigurableElement->getFootPrint()), |
| 40 | _pucBlackboardLocation(NULL), |
| 41 | _uiAccessedIndex(0) |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 42 | { |
| 43 | // Syncer |
| 44 | _pInstanceConfigurableElement->setSyncer(this); |
| 45 | } |
| 46 | |
| 47 | CSubsystemObject::~CSubsystemObject() |
| 48 | { |
| 49 | _pInstanceConfigurableElement->unsetSyncer(); |
| 50 | } |
| 51 | |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 52 | // Blackboard data location |
| 53 | uint8_t* CSubsystemObject::getBlackboardLocation() const |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 54 | { |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 55 | return _pucBlackboardLocation; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // Size |
| 59 | uint32_t CSubsystemObject::getSize() const |
| 60 | { |
| 61 | return _uiDataSize; |
| 62 | } |
| 63 | |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 64 | // Conversion utility |
| 65 | uint32_t CSubsystemObject::asInteger(const string& strValue) |
| 66 | { |
| 67 | return strtoul(strValue.c_str(), NULL, 0); |
| 68 | } |
| 69 | |
| 70 | string CSubsystemObject::asString(uint32_t uiValue) |
| 71 | { |
| 72 | ostringstream ostr; |
| 73 | |
| 74 | ostr << uiValue; |
| 75 | |
| 76 | return ostr.str(); |
| 77 | } |
| 78 | |
Guillaume Denneulin | f2fd15a | 2012-12-20 17:53:29 +0100 | [diff] [blame] | 79 | // Default back synchronization |
| 80 | void CSubsystemObject::setDefaultValues(CParameterBlackboard& parameterBlackboard) const |
| 81 | { |
| 82 | string strError; |
| 83 | |
| 84 | // Create access context |
| 85 | CParameterAccessContext parameterAccessContext(strError, ¶meterBlackboard, false); |
| 86 | |
| 87 | // Just implement back synchronization with default values |
| 88 | _pInstanceConfigurableElement->setDefaultValues(parameterAccessContext); |
| 89 | } |
| 90 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 91 | // Synchronization |
| 92 | bool CSubsystemObject::sync(CParameterBlackboard& parameterBlackboard, bool bBack, string& strError) |
| 93 | { |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 94 | // Get blackboard location |
| 95 | _pucBlackboardLocation = parameterBlackboard.getLocation(_pInstanceConfigurableElement->getOffset()); |
| 96 | // Access index init |
| 97 | _uiAccessedIndex = 0; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 98 | |
| 99 | #ifdef SIMULATION |
| 100 | return true; |
| 101 | #endif |
| 102 | |
Guillaume Denneulin | f2fd15a | 2012-12-20 17:53:29 +0100 | [diff] [blame] | 103 | // Retrieve subsystem |
| 104 | const CSubsystem* pSubsystem = _pInstanceConfigurableElement->getBelongingSubsystem(); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 105 | |
Guillaume Denneulin | f2fd15a | 2012-12-20 17:53:29 +0100 | [diff] [blame] | 106 | // Get it's health insdicator |
| 107 | bool bIsSubsystemAlive = pSubsystem->isAlive(); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 108 | |
Guillaume Denneulin | f2fd15a | 2012-12-20 17:53:29 +0100 | [diff] [blame] | 109 | // Check subsystem health |
| 110 | if (!bIsSubsystemAlive) { |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 111 | |
Guillaume Denneulin | f2fd15a | 2012-12-20 17:53:29 +0100 | [diff] [blame] | 112 | strError = "Susbsystem not alive"; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 113 | } |
Guillaume Denneulin | f2fd15a | 2012-12-20 17:53:29 +0100 | [diff] [blame] | 114 | |
| 115 | // Synchronize to/from HW |
| 116 | if (!bIsSubsystemAlive || !accessHW(bBack, strError)) { |
| 117 | |
| 118 | strError = string("Unable to ") + (bBack ? "back" : "forward") + " synchronize configurable element " + |
| 119 | _pInstanceConfigurableElement->getPath() + ": " + strError; |
| 120 | |
| 121 | log_warning(strError); |
| 122 | |
| 123 | // Fall back to parameter default initialization |
| 124 | if (bBack) { |
| 125 | |
| 126 | setDefaultValues(parameterBlackboard); |
| 127 | } |
| 128 | return false; |
| 129 | } |
| 130 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 131 | return true; |
| 132 | } |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 133 | |
| 134 | // Sync to/from HW |
| 135 | bool CSubsystemObject::sendToHW(string& strError) |
| 136 | { |
| 137 | strError = "Send to HW interface not implemented at subsystsem level!"; |
| 138 | |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | bool CSubsystemObject::receiveFromHW(string& strError) |
| 143 | { |
| 144 | strError = "Receive from HW interface not implemented at subsystsem level!"; |
| 145 | |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | // Fall back HW access |
| 150 | bool CSubsystemObject::accessHW(bool bReceive, string& strError) |
| 151 | { |
Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 152 | // Default access fall back |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 153 | if (bReceive) { |
| 154 | |
| 155 | return receiveFromHW(strError); |
| 156 | } else { |
| 157 | |
| 158 | return sendToHW(strError); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // Blackboard access from subsystems |
| 163 | void CSubsystemObject::blackboardRead(void* pvData, uint32_t uiSize) |
| 164 | { |
| 165 | assert(_uiAccessedIndex + uiSize <= _uiDataSize); |
| 166 | |
| 167 | memcpy(pvData, _pucBlackboardLocation + _uiAccessedIndex, uiSize); |
| 168 | |
| 169 | _uiAccessedIndex += uiSize; |
| 170 | } |
| 171 | |
| 172 | void CSubsystemObject::blackboardWrite(const void* pvData, uint32_t uiSize) |
| 173 | { |
| 174 | assert(_uiAccessedIndex + uiSize <= _uiDataSize); |
| 175 | |
| 176 | memcpy(_pucBlackboardLocation + _uiAccessedIndex, pvData, uiSize); |
| 177 | |
| 178 | _uiAccessedIndex += uiSize; |
| 179 | } |
Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 180 | |
| 181 | // Logging |
Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 182 | void CSubsystemObject::log_info(const string& strMessage, ...) const |
Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 183 | { |
| 184 | char acBuffer[512]; |
| 185 | va_list listPointer; |
| 186 | |
| 187 | va_start(listPointer, strMessage); |
| 188 | |
| 189 | vsnprintf(acBuffer, sizeof(acBuffer), strMessage.c_str(), listPointer); |
| 190 | |
| 191 | va_end(listPointer); |
| 192 | |
Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame] | 193 | _pInstanceConfigurableElement->log_info(acBuffer); |
| 194 | } |
| 195 | |
| 196 | void CSubsystemObject::log_warning(const string& strMessage, ...) const |
| 197 | { |
| 198 | char acBuffer[512]; |
| 199 | va_list listPointer; |
| 200 | |
| 201 | va_start(listPointer, strMessage); |
| 202 | |
| 203 | vsnprintf(acBuffer, sizeof(acBuffer), strMessage.c_str(), listPointer); |
| 204 | |
| 205 | va_end(listPointer); |
| 206 | |
| 207 | _pInstanceConfigurableElement->log_warning(acBuffer); |
Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 208 | } |
Guillaume Denneulin | 7fce1e3 | 2012-02-17 17:34:47 +0100 | [diff] [blame] | 209 | |
| 210 | // Amendment |
| 211 | string CSubsystemObject::formatMappingValue(const string& strMappingValue, uint32_t uiFirstAmendKey, uint32_t uiNbAmendKeys, const CMappingContext& context) |
| 212 | { |
| 213 | string strFormattedValue = strMappingValue; |
Patrick Benavoli | c3fe14f | 2012-03-06 14:54:07 +0100 | [diff] [blame] | 214 | |
Guillaume Denneulin | 7fce1e3 | 2012-02-17 17:34:47 +0100 | [diff] [blame] | 215 | // Search for amendment (only one supported for now) |
| 216 | size_t uiPercentPos = strFormattedValue.find('%', 0); |
| 217 | |
| 218 | // Amendment limited to one digit (values from 1 to 9) |
| 219 | assert((uiNbAmendKeys > 0) && (uiNbAmendKeys <= 9)); |
| 220 | |
| 221 | // Check we found one and that there's room for value |
| 222 | if (uiPercentPos != string::npos && uiPercentPos < strFormattedValue.size() - 1) { |
| 223 | |
| 224 | // Get Amend number |
| 225 | uint32_t uiAmendNumber = strFormattedValue[uiPercentPos + 1] - '0'; |
| 226 | |
| 227 | // Valid? |
| 228 | if (uiAmendNumber && uiAmendNumber <= uiNbAmendKeys) { |
| 229 | |
| 230 | uint32_t uiAmendType = uiFirstAmendKey + uiAmendNumber - 1; |
| 231 | |
| 232 | // Set? |
| 233 | if (context.iSet(uiAmendType)) { |
| 234 | |
Patrick Benavoli | c3fe14f | 2012-03-06 14:54:07 +0100 | [diff] [blame] | 235 | // Make the amendment on the part of the string after the current Amend |
| 236 | string strEndOfLine = strFormattedValue.substr(uiPercentPos + 2, strFormattedValue.size() - uiPercentPos - 2); |
| 237 | string strEndOfLineAmended = formatMappingValue(strEndOfLine, uiFirstAmendKey, uiNbAmendKeys, context); |
| 238 | |
| 239 | // Get current Amend value |
Guillaume Denneulin | 7fce1e3 | 2012-02-17 17:34:47 +0100 | [diff] [blame] | 240 | string strAmendValue = context.getItem(uiAmendType); |
| 241 | |
| 242 | // Make the amendment |
Patrick Benavoli | c3fe14f | 2012-03-06 14:54:07 +0100 | [diff] [blame] | 243 | strFormattedValue = strFormattedValue.substr(0, uiPercentPos) + strAmendValue + strEndOfLineAmended; |
| 244 | |
Guillaume Denneulin | 7fce1e3 | 2012-02-17 17:34:47 +0100 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | } |
| 248 | return strFormattedValue; |
| 249 | } |
Frederic Boisnard | 74edbc8 | 2012-02-28 15:59:22 +0100 | [diff] [blame] | 250 | |
| 251 | // Configurable element retrieval |
| 252 | const CInstanceConfigurableElement* CSubsystemObject::getConfigurableElement() const |
| 253 | { |
| 254 | return _pInstanceConfigurableElement; |
| 255 | } |
Guillaume Denneulin | 935f2af | 2013-01-22 15:16:08 +0100 | [diff] [blame] | 256 | |
| 257 | // Belonging Subsystem retrieval |
| 258 | const CSubsystem* CSubsystemObject::getSubsystem() const |
| 259 | { |
| 260 | return _pInstanceConfigurableElement->getBelongingSubsystem(); |
| 261 | } |