1/*
2 * Copyright (c) 2011-2015, 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 <assert.h>
34
35CAreaConfiguration::CAreaConfiguration(const CConfigurableElement *pConfigurableElement,
36                                       const CSyncerSet *pSyncerSet)
37    : _pConfigurableElement(pConfigurableElement), _pSyncerSet(pSyncerSet)
38{
39    // Size blackboard
40    _blackboard.setSize(_pConfigurableElement->getFootPrint());
41}
42
43CAreaConfiguration::CAreaConfiguration(const CConfigurableElement *pConfigurableElement,
44                                       const CSyncerSet *pSyncerSet, size_t size)
45    : _pConfigurableElement(pConfigurableElement), _pSyncerSet(pSyncerSet)
46{
47    // Size blackboard
48    _blackboard.setSize(size);
49}
50
51// Save data from current
52void CAreaConfiguration::save(const CParameterBlackboard *pMainBlackboard)
53{
54    copyFrom(pMainBlackboard, _pConfigurableElement->getOffset());
55}
56
57// Apply data to current
58bool CAreaConfiguration::restore(CParameterBlackboard *pMainBlackboard, bool bSync,
59                                 core::Results *errors) const
60{
61    assert(_bValid);
62
63    copyTo(pMainBlackboard, _pConfigurableElement->getOffset());
64
65    // Synchronize if required
66    return !bSync || _pSyncerSet->sync(*pMainBlackboard, false, errors);
67}
68
69// Ensure validity
70void CAreaConfiguration::validate(const CParameterBlackboard *pMainBlackboard)
71{
72    if (!_bValid) {
73
74        // Saving from blackboard make area configuration valid
75        save(pMainBlackboard);
76
77        _bValid = true;
78    }
79}
80
81// Return validity
82bool CAreaConfiguration::isValid() const
83{
84    return _bValid;
85}
86
87// Ensure validity against given valid area configuration
88void CAreaConfiguration::validateAgainst(const CAreaConfiguration *pValidAreaConfiguration)
89{
90    // Should be called on purpose
91    assert(!_bValid);
92
93    // Check proper against area given
94    assert(pValidAreaConfiguration->isValid());
95
96    // Check compatibility
97    assert(_pConfigurableElement == pValidAreaConfiguration->_pConfigurableElement);
98
99    // Copy
100    _blackboard.restoreFrom(&pValidAreaConfiguration->_blackboard, 0);
101
102    // Set as valid
103    _bValid = true;
104}
105
106// XML configuration settings parsing
107bool CAreaConfiguration::serializeXmlSettings(
108    CXmlElement &xmlConfigurableElementSettingsElementContent,
109    CConfigurationAccessContext &configurationAccessContext)
110{
111    // Assign blackboard to configuration context
112    configurationAccessContext.setParameterBlackboard(&_blackboard);
113
114    // Assign base offset to configuration context
115    configurationAccessContext.setBaseOffset(_pConfigurableElement->getOffset());
116
117    // Parse configuration settings (element contents)
118    if (_pConfigurableElement->serializeXmlSettings(xmlConfigurableElementSettingsElementContent,
119                                                    configurationAccessContext)) {
120
121        if (!configurationAccessContext.serializeOut()) {
122
123            // Serialized-in areas are valid
124            _bValid = true;
125        }
126        return true;
127    }
128    return false;
129}
130
131// Compound handling
132const CConfigurableElement *CAreaConfiguration::getConfigurableElement() const
133{
134    return _pConfigurableElement;
135}
136
137void CAreaConfiguration::copyToOuter(CAreaConfiguration *pToAreaConfiguration) const
138{
139    assert(_pConfigurableElement->isDescendantOf(pToAreaConfiguration->getConfigurableElement()));
140
141    copyTo(&pToAreaConfiguration->_blackboard,
142           _pConfigurableElement->getOffset() -
143               pToAreaConfiguration->getConfigurableElement()->getOffset());
144}
145
146void CAreaConfiguration::copyFromOuter(const CAreaConfiguration *pFromAreaConfiguration)
147{
148    assert(_pConfigurableElement->isDescendantOf(pFromAreaConfiguration->getConfigurableElement()));
149
150    copyFrom(&pFromAreaConfiguration->_blackboard,
151             _pConfigurableElement->getOffset() -
152                 pFromAreaConfiguration->getConfigurableElement()->getOffset());
153
154    // Inner becomes valid
155    setValid(true);
156}
157
158CParameterBlackboard &CAreaConfiguration::getBlackboard()
159{
160    return _blackboard;
161}
162
163const CParameterBlackboard &CAreaConfiguration::getBlackboard() const
164{
165    return _blackboard;
166}
167
168// Store validity
169void CAreaConfiguration::setValid(bool bValid)
170{
171    _bValid = bValid;
172}
173
174// Blackboard copies
175void CAreaConfiguration::copyTo(CParameterBlackboard *pToBlackboard, size_t offset) const
176{
177    pToBlackboard->restoreFrom(&_blackboard, offset);
178}
179
180void CAreaConfiguration::copyFrom(const CParameterBlackboard *pFromBlackboard, size_t offset)
181{
182    pFromBlackboard->saveTo(&_blackboard, offset);
183}
184