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" |
| 26 | #include "InstanceConfigurableElement.h" |
| 27 | #include "ParameterBlackboard.h" |
Guillaume Denneulin | 7fce1e3 | 2012-02-17 17:34:47 +0100 | [diff] [blame] | 28 | #include "MappingContext.h" |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 29 | #include <assert.h> |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <sstream> |
Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 33 | #include <stdarg.h> |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 34 | |
| 35 | CSubsystemObject::CSubsystemObject(CInstanceConfigurableElement* pInstanceConfigurableElement) |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 36 | : _pInstanceConfigurableElement(pInstanceConfigurableElement), |
| 37 | _uiDataSize(pInstanceConfigurableElement->getFootPrint()), |
| 38 | _pucBlackboardLocation(NULL), |
| 39 | _uiAccessedIndex(0) |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 40 | { |
| 41 | // Syncer |
| 42 | _pInstanceConfigurableElement->setSyncer(this); |
| 43 | } |
| 44 | |
| 45 | CSubsystemObject::~CSubsystemObject() |
| 46 | { |
| 47 | _pInstanceConfigurableElement->unsetSyncer(); |
| 48 | } |
| 49 | |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 50 | // Blackboard data location |
| 51 | uint8_t* CSubsystemObject::getBlackboardLocation() const |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 52 | { |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 53 | return _pucBlackboardLocation; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // Size |
| 57 | uint32_t CSubsystemObject::getSize() const |
| 58 | { |
| 59 | return _uiDataSize; |
| 60 | } |
| 61 | |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 62 | // Conversion utility |
| 63 | uint32_t CSubsystemObject::asInteger(const string& strValue) |
| 64 | { |
| 65 | return strtoul(strValue.c_str(), NULL, 0); |
| 66 | } |
| 67 | |
| 68 | string CSubsystemObject::asString(uint32_t uiValue) |
| 69 | { |
| 70 | ostringstream ostr; |
| 71 | |
| 72 | ostr << uiValue; |
| 73 | |
| 74 | return ostr.str(); |
| 75 | } |
| 76 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 77 | // Synchronization |
| 78 | bool CSubsystemObject::sync(CParameterBlackboard& parameterBlackboard, bool bBack, string& strError) |
| 79 | { |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 80 | // Get blackboard location |
| 81 | _pucBlackboardLocation = parameterBlackboard.getLocation(_pInstanceConfigurableElement->getOffset()); |
| 82 | // Access index init |
| 83 | _uiAccessedIndex = 0; |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 84 | |
| 85 | #ifdef SIMULATION |
| 86 | return true; |
| 87 | #endif |
| 88 | |
| 89 | // Synchronize to/from HW |
| 90 | if (bBack) { |
| 91 | |
| 92 | // Read from HW |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 93 | if (!accessHW(true, strError)) { |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 94 | |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 95 | strError = "Unable to back synchronize configurable element " + _pInstanceConfigurableElement->getPath() + ": " + strError; |
Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame^] | 96 | log_warning(strError); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 97 | |
| 98 | return false; |
| 99 | } |
| 100 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 101 | } else { |
| 102 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 103 | // Send to HW |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 104 | if (!accessHW(false, strError)) { |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 105 | |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 106 | strError = "Unable to synchronize configurable element " + _pInstanceConfigurableElement->getPath() + ": " + strError; |
Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame^] | 107 | log_warning(strError); |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 108 | |
| 109 | return false; |
| 110 | } |
| 111 | } |
| 112 | return true; |
| 113 | } |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 114 | |
| 115 | // Sync to/from HW |
| 116 | bool CSubsystemObject::sendToHW(string& strError) |
| 117 | { |
| 118 | strError = "Send to HW interface not implemented at subsystsem level!"; |
| 119 | |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | bool CSubsystemObject::receiveFromHW(string& strError) |
| 124 | { |
| 125 | strError = "Receive from HW interface not implemented at subsystsem level!"; |
| 126 | |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | // Fall back HW access |
| 131 | bool CSubsystemObject::accessHW(bool bReceive, string& strError) |
| 132 | { |
Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 133 | // Default access fall back |
Patrick Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 134 | if (bReceive) { |
| 135 | |
| 136 | return receiveFromHW(strError); |
| 137 | } else { |
| 138 | |
| 139 | return sendToHW(strError); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // Blackboard access from subsystems |
| 144 | void CSubsystemObject::blackboardRead(void* pvData, uint32_t uiSize) |
| 145 | { |
| 146 | assert(_uiAccessedIndex + uiSize <= _uiDataSize); |
| 147 | |
| 148 | memcpy(pvData, _pucBlackboardLocation + _uiAccessedIndex, uiSize); |
| 149 | |
| 150 | _uiAccessedIndex += uiSize; |
| 151 | } |
| 152 | |
| 153 | void CSubsystemObject::blackboardWrite(const void* pvData, uint32_t uiSize) |
| 154 | { |
| 155 | assert(_uiAccessedIndex + uiSize <= _uiDataSize); |
| 156 | |
| 157 | memcpy(_pucBlackboardLocation + _uiAccessedIndex, pvData, uiSize); |
| 158 | |
| 159 | _uiAccessedIndex += uiSize; |
| 160 | } |
Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 161 | |
| 162 | // Logging |
Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame^] | 163 | void CSubsystemObject::log_info(const string& strMessage, ...) const |
Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 164 | { |
| 165 | char acBuffer[512]; |
| 166 | va_list listPointer; |
| 167 | |
| 168 | va_start(listPointer, strMessage); |
| 169 | |
| 170 | vsnprintf(acBuffer, sizeof(acBuffer), strMessage.c_str(), listPointer); |
| 171 | |
| 172 | va_end(listPointer); |
| 173 | |
Kevin Rocard | ace81f8 | 2012-12-11 16:19:17 +0100 | [diff] [blame^] | 174 | _pInstanceConfigurableElement->log_info(acBuffer); |
| 175 | } |
| 176 | |
| 177 | void CSubsystemObject::log_warning(const string& strMessage, ...) const |
| 178 | { |
| 179 | char acBuffer[512]; |
| 180 | va_list listPointer; |
| 181 | |
| 182 | va_start(listPointer, strMessage); |
| 183 | |
| 184 | vsnprintf(acBuffer, sizeof(acBuffer), strMessage.c_str(), listPointer); |
| 185 | |
| 186 | va_end(listPointer); |
| 187 | |
| 188 | _pInstanceConfigurableElement->log_warning(acBuffer); |
Patrick Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 189 | } |
Guillaume Denneulin | 7fce1e3 | 2012-02-17 17:34:47 +0100 | [diff] [blame] | 190 | |
| 191 | // Amendment |
| 192 | string CSubsystemObject::formatMappingValue(const string& strMappingValue, uint32_t uiFirstAmendKey, uint32_t uiNbAmendKeys, const CMappingContext& context) |
| 193 | { |
| 194 | string strFormattedValue = strMappingValue; |
Patrick Benavoli | c3fe14f | 2012-03-06 14:54:07 +0100 | [diff] [blame] | 195 | |
Guillaume Denneulin | 7fce1e3 | 2012-02-17 17:34:47 +0100 | [diff] [blame] | 196 | // Search for amendment (only one supported for now) |
| 197 | size_t uiPercentPos = strFormattedValue.find('%', 0); |
| 198 | |
| 199 | // Amendment limited to one digit (values from 1 to 9) |
| 200 | assert((uiNbAmendKeys > 0) && (uiNbAmendKeys <= 9)); |
| 201 | |
| 202 | // Check we found one and that there's room for value |
| 203 | if (uiPercentPos != string::npos && uiPercentPos < strFormattedValue.size() - 1) { |
| 204 | |
| 205 | // Get Amend number |
| 206 | uint32_t uiAmendNumber = strFormattedValue[uiPercentPos + 1] - '0'; |
| 207 | |
| 208 | // Valid? |
| 209 | if (uiAmendNumber && uiAmendNumber <= uiNbAmendKeys) { |
| 210 | |
| 211 | uint32_t uiAmendType = uiFirstAmendKey + uiAmendNumber - 1; |
| 212 | |
| 213 | // Set? |
| 214 | if (context.iSet(uiAmendType)) { |
| 215 | |
Patrick Benavoli | c3fe14f | 2012-03-06 14:54:07 +0100 | [diff] [blame] | 216 | // Make the amendment on the part of the string after the current Amend |
| 217 | string strEndOfLine = strFormattedValue.substr(uiPercentPos + 2, strFormattedValue.size() - uiPercentPos - 2); |
| 218 | string strEndOfLineAmended = formatMappingValue(strEndOfLine, uiFirstAmendKey, uiNbAmendKeys, context); |
| 219 | |
| 220 | // Get current Amend value |
Guillaume Denneulin | 7fce1e3 | 2012-02-17 17:34:47 +0100 | [diff] [blame] | 221 | string strAmendValue = context.getItem(uiAmendType); |
| 222 | |
| 223 | // Make the amendment |
Patrick Benavoli | c3fe14f | 2012-03-06 14:54:07 +0100 | [diff] [blame] | 224 | strFormattedValue = strFormattedValue.substr(0, uiPercentPos) + strAmendValue + strEndOfLineAmended; |
| 225 | |
Guillaume Denneulin | 7fce1e3 | 2012-02-17 17:34:47 +0100 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | } |
| 229 | return strFormattedValue; |
| 230 | } |
Frederic Boisnard | 74edbc8 | 2012-02-28 15:59:22 +0100 | [diff] [blame] | 231 | |
| 232 | // Configurable element retrieval |
| 233 | const CInstanceConfigurableElement* CSubsystemObject::getConfigurableElement() const |
| 234 | { |
| 235 | return _pInstanceConfigurableElement; |
| 236 | } |