blob: 25da60a0453c68aa0111f1aaeca94741a3df28c4 [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 "Subsystem.h"
32#include "ComponentLibrary.h"
33#include "InstanceDefinition.h"
34#include "XmlParameterSerializingContext.h"
35#include "ParameterAccessContext.h"
36#include "ConfigurationAccessContext.h"
37#include "SubsystemObjectCreator.h"
38#include <assert.h>
39
40#define base CConfigurableElement
41
42CSubsystem::CSubsystem(const string& strName) : base(strName), _pComponentLibrary(new CComponentLibrary), _pInstanceDefinition(new CInstanceDefinition), _bBigEndian(false)
43{
44 // Note: A subsystem contains instance components
45 // InstanceDefintion and ComponentLibrary objects are then not chosen to be children
46 // They'll be delt with locally
47}
48
49CSubsystem::~CSubsystem()
50{
51 // Remove subsystem objects
52 SubsystemObjectListIterator subsystemObjectIt;
53
54 for (subsystemObjectIt = _subsystemObjectList.begin(); subsystemObjectIt != _subsystemObjectList.end(); ++subsystemObjectIt) {
55
56 delete *subsystemObjectIt;
57 }
58
59 // Remove susbsystem creators
60 uint32_t uiIndex;
61
62 for (uiIndex = 0; uiIndex < _subsystemObjectCreatorArray.size(); uiIndex++) {
63
64 delete _subsystemObjectCreatorArray[uiIndex];
65 }
66
67 // Order matters!
68 delete _pInstanceDefinition;
69 delete _pComponentLibrary;
70}
71
72string CSubsystem::getKind() const
73{
74 return "Subsystem";
75}
76
77// Susbsystem Endianness
78bool CSubsystem::isBigEndian() const
79{
80 return _bBigEndian;
81}
82
83// From IXmlSink
84bool CSubsystem::fromXml(const CXmlElement& xmlElement, CXmlSerializingContext& serializingContext)
85{
86 // Context
87 CXmlParameterSerializingContext& parameterBuildContext = static_cast<CXmlParameterSerializingContext&>(serializingContext);
88
89 // Install temporary component library for further component creation
90 parameterBuildContext.setComponentLibrary(_pComponentLibrary);
91
92 CXmlElement childElement;
93
94 // XML populate ComponentLibrary
95 xmlElement.getChildElement("ComponentLibrary", childElement);
96
97 if (!_pComponentLibrary->fromXml(childElement, serializingContext)) {
98
99 return false;
100 }
101
102 // XML populate InstanceDefintion
103 xmlElement.getChildElement("InstanceDefintion", childElement);
104 if (!_pInstanceDefinition->fromXml(childElement, serializingContext)) {
105
106 return false;
107 }
108
109 // Create components
110 _pInstanceDefinition->createInstances(this);
111
112 // Execute mapping to create subsystem mapping entities
113 string strError;
114 if (!mapSubsystemElements(strError)) {
115
116 serializingContext.setError(strError);
117
118 return false;
119 }
120
121 // Endianness
122 _bBigEndian = xmlElement.getAttributeBoolean("Endianness", "Big");
123
124 return true;
125}
126
127// XML configuration settings parsing
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200128bool CSubsystem::serializeXmlSettings(CXmlElement& xmlConfigurationSettingsElementContent, CConfigurationAccessContext& configurationAccessContext) const
Patrick Benavoli68a91282011-08-31 11:23:23 +0200129{
130 // Fix Endianness
131 configurationAccessContext.setBigEndianSubsystem(_bBigEndian);
132
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200133 return base::serializeXmlSettings(xmlConfigurationSettingsElementContent, configurationAccessContext);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200134}
135
136
137bool CSubsystem::mapSubsystemElements(string& strError)
138{
139 // Default mapping context
140 _contextStack.push(CMappingContext(_contextMappingKeyArray.size()));
141
142 // Map all instantiated subelements in subsystem
143 uint32_t uiNbChildren = getNbChildren();
144 uint32_t uiChild;
145
146 for (uiChild = 0; uiChild < uiNbChildren; uiChild++) {
147
148 CInstanceConfigurableElement* pInstanceConfigurableChildElement = static_cast<CInstanceConfigurableElement*>(getChild(uiChild));
149
150 if (!pInstanceConfigurableChildElement->map(*this, strError)) {
151
152 return false;
153 }
154 }
155 return true;
156}
157
158// Parameter access
159bool CSubsystem::setValue(CPathNavigator& pathNavigator, const string& strValue, CErrorContext& errorContext) const
160{
161 CParameterAccessContext& parameterContext = static_cast<CParameterAccessContext&>(errorContext);
162
163 // Deal with Endianness
164 parameterContext.setBigEndianSubsystem(_bBigEndian);
165
166 return base::setValue(pathNavigator, strValue, errorContext);
167}
168
169bool CSubsystem::getValue(CPathNavigator& pathNavigator, string& strValue, CErrorContext& errorContext) const
170{
171 CParameterAccessContext& parameterContext = static_cast<CParameterAccessContext&>(errorContext);
172
173 // Deal with Endianness
174 parameterContext.setBigEndianSubsystem(_bBigEndian);
175
176 return base::getValue(pathNavigator, strValue, errorContext);
177}
178
179void CSubsystem::logValue(string& strValue, CErrorContext& errorContext) const
180{
181 CParameterAccessContext& parameterContext = static_cast<CParameterAccessContext&>(errorContext);
182
183 // Deal with Endianness
184 parameterContext.setBigEndianSubsystem(_bBigEndian);
185
186 return base::logValue(strValue, errorContext);
187}
188
189// Used for simulation only
190void CSubsystem::setDefaultValues(CParameterAccessContext& parameterAccessContext) const
191{
192 // Deal with Endianness
193 parameterAccessContext.setBigEndianSubsystem(_bBigEndian);
194
195 base::setDefaultValues(parameterAccessContext);
196}
197
198// Belonging subsystem
199const CSubsystem* CSubsystem::getBelongingSubsystem() const
200{
201 return this;
202}
203
204// Subsystem context mapping keys publication
205void CSubsystem::addContextMappingKey(const string& strMappingKey)
206{
207 _contextMappingKeyArray.push_back(strMappingKey);
208}
209
210// Subsystem object creator publication (strong reference)
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200211void CSubsystem::addSubsystemObjectFactory(CSubsystemObjectCreator* pSubsystemObjectCreator)
Patrick Benavoli68a91282011-08-31 11:23:23 +0200212{
213 _subsystemObjectCreatorArray.push_back(pSubsystemObjectCreator);
214}
215
216// Mapping generic context handling
217bool CSubsystem::handleMappingContext(const CInstanceConfigurableElement* pInstanceConfigurableElement, CMappingContext& context, string& strError)
218{
219 // Feed context with found mapping data
220 uint32_t uiItem;
221
222 for (uiItem = 0; uiItem < _contextMappingKeyArray.size(); uiItem++) {
223
224 string strKey = _contextMappingKeyArray[uiItem];
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200225 const string* pStrValue;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200226
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200227 if (pInstanceConfigurableElement->getMappingData(strKey, pStrValue)) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200228 // Assign item to context
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200229 if (!context.setItem(uiItem, pStrValue)) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200230
231 getMappingError(strError, strKey, "Already set", pInstanceConfigurableElement);
232
233 return false;
234 }
235 }
236 }
237 return true;
238}
239
240// Creation handling
241bool CSubsystem::handleSubsystemObjectCreation(CInstanceConfigurableElement* pInstanceConfigurableElement, CMappingContext& context, string& strError)
242{
243 uint32_t uiItem;
244
245 for (uiItem = 0; uiItem < _subsystemObjectCreatorArray.size(); uiItem++) {
246
247 const CSubsystemObjectCreator* pSubsystemObjectCreator = _subsystemObjectCreatorArray[uiItem];
248
249 // Mapping key
250 string strKey = pSubsystemObjectCreator->getMappingKey();
251 // Object id
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200252 const string* pStrValue;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200253
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200254 if (pInstanceConfigurableElement->getMappingData(strKey, pStrValue)) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200255
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200256 // First check context consistensy (required ancestors must have been set prior to object creation)
Patrick Benavoli68a91282011-08-31 11:23:23 +0200257 uint32_t uiAncestorKey;
258 uint32_t uiAncestorMask = pSubsystemObjectCreator->getAncestorMask();
259
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200260 for (uiAncestorKey = 0; uiAncestorKey < _contextMappingKeyArray.size(); uiAncestorKey++) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200261
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200262 if (!((1 << uiAncestorKey) & uiAncestorMask)) {
263 // Ancestor not required
264 continue;
265 }
266 // Check ancestor was provided
Patrick Benavoli68a91282011-08-31 11:23:23 +0200267 if (!context.iSet(uiAncestorKey)) {
268
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200269 getMappingError(strError, strKey, _contextMappingKeyArray[uiAncestorKey] + " not set", pInstanceConfigurableElement);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200270
271 return false;
272 }
273 }
274
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200275 // Then check configurable element size is correct
276 if (pInstanceConfigurableElement->getFootPrint() > pSubsystemObjectCreator->getMaxConfigurableElementSize()) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200277
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200278 string strSizeError = "Size should not exceed " + pInstanceConfigurableElement->getFootprintAsString();
Patrick Benavoli68a91282011-08-31 11:23:23 +0200279
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200280 getMappingError(strError, strKey, strSizeError, pInstanceConfigurableElement);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200281
282 return false;
283 }
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200284
285 // Do create object and keep its track
286 _subsystemObjectList.push_back(pSubsystemObjectCreator->objectCreate(*pStrValue, pInstanceConfigurableElement, context));
Patrick Benavoli68a91282011-08-31 11:23:23 +0200287
288 // Done
289 return true;
290 }
291 }
292 getMappingError(strError, "Mapping key", "Not found", pInstanceConfigurableElement);
293
294 return false;
295}
296
297// Generic error handling from derived subsystem classes
298void CSubsystem::getMappingError(string& strError, const string& strKey, const string& strMessage, const CInstanceConfigurableElement* pInstanceConfigurableElement)
299{
300 strError = getName() + " " + getKind() + " mapping:\n" + strKey + " error : \"" + strMessage + "\" for element " + pInstanceConfigurableElement->getPath();
301}
302
303// From IMapper
304bool CSubsystem::mapBegin(CInstanceConfigurableElement* pInstanceConfigurableElement, string& strError)
305{
306 // Get current context
307 CMappingContext context = _contextStack.top();
308
309 switch(pInstanceConfigurableElement->getType()) {
310
311 case CInstanceConfigurableElement::EComponent:
312
313 if (!handleMappingContext(pInstanceConfigurableElement, context, strError)) {
314
315 return false;
316 }
317 break;
318 case CInstanceConfigurableElement::EParameterBlock:
319 case CInstanceConfigurableElement::EBitParameterBlock:
320 case CInstanceConfigurableElement::EParameter:
321 {
322 if (!handleSubsystemObjectCreation(pInstanceConfigurableElement, context, strError)) {
323
324 return false;
325 }
326 break;
327 }
328 default:
329 assert(0);
330 return false;
331 }
332
333 // Push context
334 _contextStack.push(context);
335
336 return true;
337}
338
339void CSubsystem::mapEnd()
340{
341 // Unstack context
342 _contextStack.pop();
343}