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 "InstanceConfigurableElement.h"
31 #include "Mapper.h"
32 #include "SyncerSet.h"
33 #include "Syncer.h"
34 #include "TypeElement.h"
35 #include "ParameterAccessContext.h"
36 #include <assert.h>
37 
38 #define base CConfigurableElement
39 
CInstanceConfigurableElement(const std::string & strName,const CTypeElement * pTypeElement)40 CInstanceConfigurableElement::CInstanceConfigurableElement(const std::string &strName,
41                                                            const CTypeElement *pTypeElement)
42     : base(strName), _pTypeElement(pTypeElement)
43 {
44 }
45 
getKind() const46 std::string CInstanceConfigurableElement::getKind() const
47 {
48     // Delegate
49     return _pTypeElement->getKind();
50 }
51 
getXmlElementName() const52 std::string CInstanceConfigurableElement::getXmlElementName() const
53 {
54     // Delegate
55     return _pTypeElement->getXmlElementName();
56 }
57 
58 // Type element
getTypeElement() const59 const CTypeElement *CInstanceConfigurableElement::getTypeElement() const
60 {
61     return _pTypeElement;
62 }
63 
64 // Mapping
getMappingData(const std::string & strKey,const std::string * & pStrValue) const65 bool CInstanceConfigurableElement::getMappingData(const std::string &strKey,
66                                                   const std::string *&pStrValue) const
67 {
68     // Delegate
69     return getTypeElement()->getMappingData(strKey, pStrValue);
70 }
71 
72 // Returns the formatted mapping
getFormattedMapping() const73 std::string CInstanceConfigurableElement::getFormattedMapping() const
74 {
75     // Delegate
76     return getTypeElement()->getFormattedMapping();
77 }
78 
map(IMapper & mapper,std::string & strError)79 bool CInstanceConfigurableElement::map(IMapper &mapper, std::string &strError)
80 {
81     bool bHasMappingData = getTypeElement()->hasMappingData();
82     bool bKeepDiving = true;
83 
84     // Begin
85     if (bHasMappingData && !mapper.mapBegin(this, bKeepDiving, strError)) {
86 
87         return false;
88     }
89 
90     // Go on through children?
91     if (bKeepDiving) {
92 
93         // Map children
94         size_t uiNbChildren = getNbChildren();
95         size_t uiChild;
96 
97         for (uiChild = 0; uiChild < uiNbChildren; uiChild++) {
98 
99             CInstanceConfigurableElement *pInstanceConfigurableChildElement =
100                 static_cast<CInstanceConfigurableElement *>(getChild(uiChild));
101 
102             if (!pInstanceConfigurableChildElement->map(mapper, strError)) {
103 
104                 return false;
105             }
106         }
107     }
108 
109     // End
110     if (bHasMappingData) {
111 
112         mapper.mapEnd();
113     }
114     return true;
115 }
116 
117 // Element properties
showProperties(std::string & strResult) const118 void CInstanceConfigurableElement::showProperties(std::string &strResult) const
119 {
120     base::showProperties(strResult);
121 
122     // Delegate to type element
123     _pTypeElement->showProperties(strResult);
124 }
125 
126 // Scalar or Array?
isScalar() const127 bool CInstanceConfigurableElement::isScalar() const
128 {
129     return _pTypeElement->isScalar();
130 }
131 
132 // Array Length
getArrayLength() const133 size_t CInstanceConfigurableElement::getArrayLength() const
134 {
135     return _pTypeElement->getArrayLength();
136 }
137 
138 // Sync to HW
setSyncer(ISyncer * pSyncer)139 void CInstanceConfigurableElement::setSyncer(ISyncer *pSyncer)
140 {
141     assert(!_pSyncer);
142 
143     _pSyncer = pSyncer;
144 }
145 
unsetSyncer()146 void CInstanceConfigurableElement::unsetSyncer()
147 {
148     _pSyncer = NULL;
149 }
150 
151 // Syncer
getSyncer() const152 ISyncer *CInstanceConfigurableElement::getSyncer() const
153 {
154     if (_pSyncer) {
155 
156         return _pSyncer;
157     }
158     // Check parent
159     return base::getSyncer();
160 }
161 
162 // Syncer set (descendant)
fillSyncerSetFromDescendant(CSyncerSet & syncerSet) const163 void CInstanceConfigurableElement::fillSyncerSetFromDescendant(CSyncerSet &syncerSet) const
164 {
165     if (_pSyncer) {
166 
167         syncerSet += _pSyncer;
168     } else {
169         // Continue digging
170         base::fillSyncerSetFromDescendant(syncerSet);
171     }
172 }
173 
sync(CParameterAccessContext & parameterAccessContext) const174 bool CInstanceConfigurableElement::sync(CParameterAccessContext &parameterAccessContext) const
175 {
176     if (!parameterAccessContext.getAutoSync()) {
177 
178         // AutoSync is disabled, do not perform the sync.
179         // This is not an error, but the expected behavior so return true anyway.
180         return true;
181     }
182     ISyncer *pSyncer = getSyncer();
183 
184     if (!pSyncer) {
185 
186         parameterAccessContext.setError("Unable to synchronize modification. No Syncer object "
187                                         "associated to configurable element:");
188 
189         return false;
190     }
191     std::string strError;
192 
193     if (!pSyncer->sync(*parameterAccessContext.getParameterBlackboard(), false, strError)) {
194 
195         parameterAccessContext.setError(strError);
196 
197         return false;
198     }
199     return true;
200 }
201 
202 // Check parameter access path well formed for leaf elements
checkPathExhausted(CPathNavigator & pathNavigator,utility::ErrorContext & errorContext)203 bool CInstanceConfigurableElement::checkPathExhausted(CPathNavigator &pathNavigator,
204                                                       utility::ErrorContext &errorContext)
205 {
206     std::string *pStrChildName = pathNavigator.next();
207 
208     if (pStrChildName) {
209 
210         // Should be leaf element
211         errorContext.setError("Path not found: " + pathNavigator.getCurrentPath());
212 
213         return false;
214     }
215     return true;
216 }
217 
structureToXml(CXmlElement & xmlElement,CXmlSerializingContext & serializingContext) const218 void CInstanceConfigurableElement::structureToXml(CXmlElement &xmlElement,
219                                                   CXmlSerializingContext &serializingContext) const
220 {
221     base::structureToXml(xmlElement, serializingContext);
222     // Since Description belongs to the Type of Element, delegate it to the type element.
223     getTypeElement()->setXmlDescriptionAttribute(xmlElement);
224 }
225