blob: 15bba6cb078ec8791133cbb473a6e81781554307 [file] [log] [blame]
Kevin Rocard93250d12012-07-19 17:48:30 +02001/*
Patrick Benavoli68a91282011-08-31 11:23:23 +02002 * 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 Benavoli68a91282011-08-31 11:23:23 +020022 * CREATED: 2011-06-01
23 * UPDATED: 2011-07-27
Patrick Benavoli68a91282011-08-31 11:23:23 +020024 */
25#include "SubsystemObject.h"
Guillaume Denneulinf2fd15a2012-12-20 17:53:29 +010026#include "Subsystem.h"
Patrick Benavoli68a91282011-08-31 11:23:23 +020027#include "InstanceConfigurableElement.h"
28#include "ParameterBlackboard.h"
Guillaume Denneulinf2fd15a2012-12-20 17:53:29 +010029#include "ParameterAccessContext.h"
Guillaume Denneulin7fce1e32012-02-17 17:34:47 +010030#include "MappingContext.h"
Patrick Benavoli68a91282011-08-31 11:23:23 +020031#include <assert.h>
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020032#include <stdlib.h>
33#include <string.h>
34#include <sstream>
Patrick Benavoli63499d42011-10-24 18:50:03 +020035#include <stdarg.h>
Patrick Benavoli68a91282011-08-31 11:23:23 +020036
37CSubsystemObject::CSubsystemObject(CInstanceConfigurableElement* pInstanceConfigurableElement)
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020038 : _pInstanceConfigurableElement(pInstanceConfigurableElement),
39 _uiDataSize(pInstanceConfigurableElement->getFootPrint()),
40 _pucBlackboardLocation(NULL),
41 _uiAccessedIndex(0)
Patrick Benavoli68a91282011-08-31 11:23:23 +020042{
43 // Syncer
44 _pInstanceConfigurableElement->setSyncer(this);
45}
46
47CSubsystemObject::~CSubsystemObject()
48{
49 _pInstanceConfigurableElement->unsetSyncer();
50}
51
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020052// Blackboard data location
53uint8_t* CSubsystemObject::getBlackboardLocation() const
Patrick Benavoli68a91282011-08-31 11:23:23 +020054{
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020055 return _pucBlackboardLocation;
Patrick Benavoli68a91282011-08-31 11:23:23 +020056}
57
58// Size
59uint32_t CSubsystemObject::getSize() const
60{
61 return _uiDataSize;
62}
63
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020064// Conversion utility
65uint32_t CSubsystemObject::asInteger(const string& strValue)
66{
67 return strtoul(strValue.c_str(), NULL, 0);
68}
69
70string CSubsystemObject::asString(uint32_t uiValue)
71{
72 ostringstream ostr;
73
74 ostr << uiValue;
75
76 return ostr.str();
77}
78
Guillaume Denneulinf2fd15a2012-12-20 17:53:29 +010079// Default back synchronization
80void CSubsystemObject::setDefaultValues(CParameterBlackboard& parameterBlackboard) const
81{
82 string strError;
83
84 // Create access context
85 CParameterAccessContext parameterAccessContext(strError, &parameterBlackboard, false);
86
87 // Just implement back synchronization with default values
88 _pInstanceConfigurableElement->setDefaultValues(parameterAccessContext);
89}
90
Patrick Benavoli68a91282011-08-31 11:23:23 +020091// Synchronization
92bool CSubsystemObject::sync(CParameterBlackboard& parameterBlackboard, bool bBack, string& strError)
93{
Patrick Benavoli6ba361d2011-08-31 11:23:24 +020094 // Get blackboard location
95 _pucBlackboardLocation = parameterBlackboard.getLocation(_pInstanceConfigurableElement->getOffset());
96 // Access index init
97 _uiAccessedIndex = 0;
Patrick Benavoli68a91282011-08-31 11:23:23 +020098
99#ifdef SIMULATION
100 return true;
101#endif
102
Guillaume Denneulinf2fd15a2012-12-20 17:53:29 +0100103 // Retrieve subsystem
104 const CSubsystem* pSubsystem = _pInstanceConfigurableElement->getBelongingSubsystem();
Patrick Benavoli68a91282011-08-31 11:23:23 +0200105
Guillaume Denneulinf2fd15a2012-12-20 17:53:29 +0100106 // Get it's health insdicator
107 bool bIsSubsystemAlive = pSubsystem->isAlive();
Patrick Benavoli68a91282011-08-31 11:23:23 +0200108
Guillaume Denneulinf2fd15a2012-12-20 17:53:29 +0100109 // Check subsystem health
110 if (!bIsSubsystemAlive) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200111
Guillaume Denneulinf2fd15a2012-12-20 17:53:29 +0100112 strError = "Susbsystem not alive";
Patrick Benavoli68a91282011-08-31 11:23:23 +0200113 }
Guillaume Denneulinf2fd15a2012-12-20 17:53:29 +0100114
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 Benavoli68a91282011-08-31 11:23:23 +0200131 return true;
132}
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200133
134// Sync to/from HW
135bool CSubsystemObject::sendToHW(string& strError)
136{
137 strError = "Send to HW interface not implemented at subsystsem level!";
138
139 return false;
140}
141
142bool 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
150bool CSubsystemObject::accessHW(bool bReceive, string& strError)
151{
Patrick Benavoli63499d42011-10-24 18:50:03 +0200152 // Default access fall back
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200153 if (bReceive) {
154
155 return receiveFromHW(strError);
156 } else {
157
158 return sendToHW(strError);
159 }
160}
161
162// Blackboard access from subsystems
163void 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
172void 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 Benavoli63499d42011-10-24 18:50:03 +0200180
181// Logging
Kevin Rocardace81f82012-12-11 16:19:17 +0100182void CSubsystemObject::log_info(const string& strMessage, ...) const
Patrick Benavoli63499d42011-10-24 18:50:03 +0200183{
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 Rocardace81f82012-12-11 16:19:17 +0100193 _pInstanceConfigurableElement->log_info(acBuffer);
194}
195
196void 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 Benavoli63499d42011-10-24 18:50:03 +0200208}
Guillaume Denneulin7fce1e32012-02-17 17:34:47 +0100209
210// Amendment
211string CSubsystemObject::formatMappingValue(const string& strMappingValue, uint32_t uiFirstAmendKey, uint32_t uiNbAmendKeys, const CMappingContext& context)
212{
213 string strFormattedValue = strMappingValue;
Patrick Benavolic3fe14f2012-03-06 14:54:07 +0100214
Guillaume Denneulin7fce1e32012-02-17 17:34:47 +0100215 // 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 Benavolic3fe14f2012-03-06 14:54:07 +0100235 // 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 Denneulin7fce1e32012-02-17 17:34:47 +0100240 string strAmendValue = context.getItem(uiAmendType);
241
242 // Make the amendment
Patrick Benavolic3fe14f2012-03-06 14:54:07 +0100243 strFormattedValue = strFormattedValue.substr(0, uiPercentPos) + strAmendValue + strEndOfLineAmended;
244
Guillaume Denneulin7fce1e32012-02-17 17:34:47 +0100245 }
246 }
247 }
248 return strFormattedValue;
249}
Frederic Boisnard74edbc82012-02-28 15:59:22 +0100250
251// Configurable element retrieval
252const CInstanceConfigurableElement* CSubsystemObject::getConfigurableElement() const
253{
254 return _pInstanceConfigurableElement;
255}