blob: a501f910b550c58bc4b566d787431e6c45f111db [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
128bool CSubsystem::serializeXmlSettings(CXmlElement& xmlConfigurableElementSettingsElement, CConfigurationAccessContext& configurationAccessContext) const
129{
130 // Fix Endianness
131 configurationAccessContext.setBigEndianSubsystem(_bBigEndian);
132
133 return base::serializeXmlSettings(xmlConfigurableElementSettingsElement, configurationAccessContext);
134}
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)
211void CSubsystem::addSubsystemObjectCreator(CSubsystemObjectCreator* pSubsystemObjectCreator)
212{
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];
225 string strValue;
226
227 if (pInstanceConfigurableElement->getMappingData(strKey, strValue)) {
228 // Assign item to context
229 if (!context.setItem(uiItem, strValue)) {
230
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
252 string strId;
253
254 if (pInstanceConfigurableElement->getMappingData(strKey, strId)) {
255
256 // First check context consisteny
257 uint32_t uiAncestorKey;
258 uint32_t uiAncestorMask = pSubsystemObjectCreator->getAncestorMask();
259
260 for (uiAncestorKey = 0; uiAncestorKey & uiAncestorMask; uiAncestorKey++) {
261
262 string strAncestorKey = _subsystemObjectCreatorArray[uiAncestorKey]->getMappingKey();
263
264 if (!context.iSet(uiAncestorKey)) {
265
266 getMappingError(strError, strKey, strAncestorKey + " not set", pInstanceConfigurableElement);
267
268 return false;
269 }
270 }
271
272 // Do create object
273 string strCreationError;
274
275 CSubsystemObject* pSubsystemObject = pSubsystemObjectCreator->objectCreate(strId, pInstanceConfigurableElement, context, strCreationError);
276
277 if (!pSubsystemObject) {
278
279 getMappingError(strError, strKey, strCreationError, pInstanceConfigurableElement);
280
281 return false;
282 }
283 // Keep track of created object
284 _subsystemObjectList.push_back(pSubsystemObject);
285
286 // Done
287 return true;
288 }
289 }
290 getMappingError(strError, "Mapping key", "Not found", pInstanceConfigurableElement);
291
292 return false;
293}
294
295// Generic error handling from derived subsystem classes
296void CSubsystem::getMappingError(string& strError, const string& strKey, const string& strMessage, const CInstanceConfigurableElement* pInstanceConfigurableElement)
297{
298 strError = getName() + " " + getKind() + " mapping:\n" + strKey + " error : \"" + strMessage + "\" for element " + pInstanceConfigurableElement->getPath();
299}
300
301// From IMapper
302bool CSubsystem::mapBegin(CInstanceConfigurableElement* pInstanceConfigurableElement, string& strError)
303{
304 // Get current context
305 CMappingContext context = _contextStack.top();
306
307 switch(pInstanceConfigurableElement->getType()) {
308
309 case CInstanceConfigurableElement::EComponent:
310
311 if (!handleMappingContext(pInstanceConfigurableElement, context, strError)) {
312
313 return false;
314 }
315 break;
316 case CInstanceConfigurableElement::EParameterBlock:
317 case CInstanceConfigurableElement::EBitParameterBlock:
318 case CInstanceConfigurableElement::EParameter:
319 {
320 if (!handleSubsystemObjectCreation(pInstanceConfigurableElement, context, strError)) {
321
322 return false;
323 }
324 break;
325 }
326 default:
327 assert(0);
328 return false;
329 }
330
331 // Push context
332 _contextStack.push(context);
333
334 return true;
335}
336
337void CSubsystem::mapEnd()
338{
339 // Unstack context
340 _contextStack.pop();
341}