1 /*
2  * Copyright (c) 2011-2014, Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #include "AreaConfiguration.h"
31 #include "ConfigurableElement.h"
32 #include "ConfigurationAccessContext.h"
33 #include "BinaryStream.h"
34 #include <assert.h>
35 
CAreaConfiguration(const CConfigurableElement * pConfigurableElement,const CSyncerSet * pSyncerSet)36 CAreaConfiguration::CAreaConfiguration(const CConfigurableElement* pConfigurableElement, const CSyncerSet* pSyncerSet)
37     : _pConfigurableElement(pConfigurableElement), _pSyncerSet(pSyncerSet), _bValid(false)
38 {
39     // Size blackboard
40     _blackboard.setSize(_pConfigurableElement->getFootPrint());
41 }
42 
CAreaConfiguration(const CConfigurableElement * pConfigurableElement,const CSyncerSet * pSyncerSet,uint32_t uiSize)43 CAreaConfiguration::CAreaConfiguration(const CConfigurableElement* pConfigurableElement, const CSyncerSet* pSyncerSet, uint32_t uiSize)
44     : _pConfigurableElement(pConfigurableElement), _pSyncerSet(pSyncerSet), _bValid(false)
45 {
46     // Size blackboard
47     _blackboard.setSize(uiSize);
48 }
49 
50 // Save data from current
save(const CParameterBlackboard * pMainBlackboard)51 void CAreaConfiguration::save(const CParameterBlackboard* pMainBlackboard)
52 {
53     copyFrom(pMainBlackboard, _pConfigurableElement->getOffset());
54 }
55 
56 // Apply data to current
restore(CParameterBlackboard * pMainBlackboard,bool bSync,std::list<std::string> * plstrError) const57 bool CAreaConfiguration::restore(CParameterBlackboard* pMainBlackboard, bool bSync, std::list<std::string>* plstrError) const
58 {
59     assert(_bValid);
60 
61     copyTo(pMainBlackboard, _pConfigurableElement->getOffset());
62 
63     // Synchronize if required
64     return !bSync || _pSyncerSet->sync(*pMainBlackboard, false, plstrError);
65 }
66 
67 // Ensure validity
validate(const CParameterBlackboard * pMainBlackboard)68 void CAreaConfiguration::validate(const CParameterBlackboard* pMainBlackboard)
69 {
70     if (!_bValid) {
71 
72         // Saving from blackboard make area configuration valid
73         save(pMainBlackboard);
74 
75         _bValid = true;
76     }
77 }
78 
79 // Return validity
isValid() const80 bool CAreaConfiguration::isValid() const
81 {
82     return _bValid;
83 }
84 
85 // Ensure validity against given valid area configuration
validateAgainst(const CAreaConfiguration * pValidAreaConfiguration)86 void CAreaConfiguration::validateAgainst(const CAreaConfiguration* pValidAreaConfiguration)
87 {
88     // Should be called on purpose
89     assert(!_bValid);
90 
91     // Check proper against area given
92     assert(pValidAreaConfiguration->isValid());
93 
94     // Check compatibility
95     assert(_pConfigurableElement == pValidAreaConfiguration->_pConfigurableElement);
96 
97     // Copy
98     _blackboard.restoreFrom(&pValidAreaConfiguration->_blackboard, 0);
99 
100     // Set as valid
101     _bValid = true;
102 }
103 
104 // XML configuration settings parsing
serializeXmlSettings(CXmlElement & xmlConfigurableElementSettingsElementContent,CConfigurationAccessContext & configurationAccessContext)105 bool CAreaConfiguration::serializeXmlSettings(CXmlElement& xmlConfigurableElementSettingsElementContent, CConfigurationAccessContext& configurationAccessContext)
106 {
107     // Assign blackboard to configuration context
108     configurationAccessContext.setParameterBlackboard(&_blackboard);
109 
110     // Assign base offset to configuration context
111     configurationAccessContext.setBaseOffset(_pConfigurableElement->getOffset());
112 
113     // Parse configuration settings (element contents)
114     if (_pConfigurableElement->serializeXmlSettings(xmlConfigurableElementSettingsElementContent, configurationAccessContext)) {
115 
116         if (!configurationAccessContext.serializeOut()) {
117 
118             // Serialized-in areas are valid
119             _bValid = true;
120         }
121         return true;
122     }
123     return false;
124 }
125 
126 // Compound handling
getConfigurableElement() const127 const CConfigurableElement* CAreaConfiguration::getConfigurableElement() const
128 {
129     return _pConfigurableElement;
130 }
131 
copyToOuter(CAreaConfiguration * pToAreaConfiguration) const132 void CAreaConfiguration::copyToOuter(CAreaConfiguration* pToAreaConfiguration) const
133 {
134     assert(_pConfigurableElement->isDescendantOf(pToAreaConfiguration->getConfigurableElement()));
135 
136     copyTo(&pToAreaConfiguration->_blackboard, _pConfigurableElement->getOffset() - pToAreaConfiguration->getConfigurableElement()->getOffset());
137 }
138 
copyFromOuter(const CAreaConfiguration * pFromAreaConfiguration)139 void CAreaConfiguration::copyFromOuter(const CAreaConfiguration* pFromAreaConfiguration)
140 {
141     assert(_pConfigurableElement->isDescendantOf(pFromAreaConfiguration->getConfigurableElement()));
142 
143     copyFrom(&pFromAreaConfiguration->_blackboard, _pConfigurableElement->getOffset() - pFromAreaConfiguration->getConfigurableElement()->getOffset());
144 
145     // Inner becomes valid
146     setValid(true);
147 }
148 
149 // Serialization
serialize(CBinaryStream & binaryStream)150 void CAreaConfiguration::serialize(CBinaryStream& binaryStream)
151 {
152     // Delegate to blackboard
153     _blackboard.serialize(binaryStream);
154 
155     if (!binaryStream.isOut()) {
156 
157         // Serialized in areas are valid
158         _bValid = true;
159     }
160 }
161 
162 // Data size
getSize() const163 uint32_t CAreaConfiguration::getSize() const
164 {
165     return _blackboard.getSize();
166 }
167 
getBlackboard()168 CParameterBlackboard& CAreaConfiguration::getBlackboard()
169 {
170     return _blackboard;
171 }
172 
getBlackboard() const173 const CParameterBlackboard& CAreaConfiguration::getBlackboard() const
174 {
175      return _blackboard;
176 }
177 
178 // Store validity
setValid(bool bValid)179 void CAreaConfiguration::setValid(bool bValid)
180 {
181     _bValid = bValid;
182 }
183 
184 // Blackboard copies
copyTo(CParameterBlackboard * pToBlackboard,uint32_t uiOffset) const185 void CAreaConfiguration::copyTo(CParameterBlackboard* pToBlackboard, uint32_t uiOffset) const
186 {
187     pToBlackboard->restoreFrom(&_blackboard, uiOffset);
188 }
189 
copyFrom(const CParameterBlackboard * pFromBlackboard,uint32_t uiOffset)190 void CAreaConfiguration::copyFrom(const CParameterBlackboard* pFromBlackboard, uint32_t uiOffset)
191 {
192    pFromBlackboard->saveTo(&_blackboard, uiOffset);
193 }
194 
195