1*c33452fbSAndroid Build Coastguard Worker /*
2*c33452fbSAndroid Build Coastguard Worker * Copyright (c) 2011-2014, Intel Corporation
3*c33452fbSAndroid Build Coastguard Worker * All rights reserved.
4*c33452fbSAndroid Build Coastguard Worker *
5*c33452fbSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without modification,
6*c33452fbSAndroid Build Coastguard Worker * are permitted provided that the following conditions are met:
7*c33452fbSAndroid Build Coastguard Worker *
8*c33452fbSAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright notice, this
9*c33452fbSAndroid Build Coastguard Worker * list of conditions and the following disclaimer.
10*c33452fbSAndroid Build Coastguard Worker *
11*c33452fbSAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright notice,
12*c33452fbSAndroid Build Coastguard Worker * this list of conditions and the following disclaimer in the documentation and/or
13*c33452fbSAndroid Build Coastguard Worker * other materials provided with the distribution.
14*c33452fbSAndroid Build Coastguard Worker *
15*c33452fbSAndroid Build Coastguard Worker * 3. Neither the name of the copyright holder nor the names of its contributors
16*c33452fbSAndroid Build Coastguard Worker * may be used to endorse or promote products derived from this software without
17*c33452fbSAndroid Build Coastguard Worker * specific prior written permission.
18*c33452fbSAndroid Build Coastguard Worker *
19*c33452fbSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20*c33452fbSAndroid Build Coastguard Worker * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21*c33452fbSAndroid Build Coastguard Worker * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22*c33452fbSAndroid Build Coastguard Worker * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23*c33452fbSAndroid Build Coastguard Worker * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24*c33452fbSAndroid Build Coastguard Worker * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25*c33452fbSAndroid Build Coastguard Worker * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26*c33452fbSAndroid Build Coastguard Worker * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*c33452fbSAndroid Build Coastguard Worker * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28*c33452fbSAndroid Build Coastguard Worker * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*c33452fbSAndroid Build Coastguard Worker */
30*c33452fbSAndroid Build Coastguard Worker #include "InstanceConfigurableElement.h"
31*c33452fbSAndroid Build Coastguard Worker #include "Mapper.h"
32*c33452fbSAndroid Build Coastguard Worker #include "SyncerSet.h"
33*c33452fbSAndroid Build Coastguard Worker #include "Syncer.h"
34*c33452fbSAndroid Build Coastguard Worker #include "TypeElement.h"
35*c33452fbSAndroid Build Coastguard Worker #include "ParameterAccessContext.h"
36*c33452fbSAndroid Build Coastguard Worker #include <assert.h>
37*c33452fbSAndroid Build Coastguard Worker
38*c33452fbSAndroid Build Coastguard Worker #define base CConfigurableElement
39*c33452fbSAndroid Build Coastguard Worker
CInstanceConfigurableElement(const std::string & strName,const CTypeElement * pTypeElement)40*c33452fbSAndroid Build Coastguard Worker CInstanceConfigurableElement::CInstanceConfigurableElement(const std::string &strName,
41*c33452fbSAndroid Build Coastguard Worker const CTypeElement *pTypeElement)
42*c33452fbSAndroid Build Coastguard Worker : base(strName), _pTypeElement(pTypeElement)
43*c33452fbSAndroid Build Coastguard Worker {
44*c33452fbSAndroid Build Coastguard Worker }
45*c33452fbSAndroid Build Coastguard Worker
getKind() const46*c33452fbSAndroid Build Coastguard Worker std::string CInstanceConfigurableElement::getKind() const
47*c33452fbSAndroid Build Coastguard Worker {
48*c33452fbSAndroid Build Coastguard Worker // Delegate
49*c33452fbSAndroid Build Coastguard Worker return _pTypeElement->getKind();
50*c33452fbSAndroid Build Coastguard Worker }
51*c33452fbSAndroid Build Coastguard Worker
getXmlElementName() const52*c33452fbSAndroid Build Coastguard Worker std::string CInstanceConfigurableElement::getXmlElementName() const
53*c33452fbSAndroid Build Coastguard Worker {
54*c33452fbSAndroid Build Coastguard Worker // Delegate
55*c33452fbSAndroid Build Coastguard Worker return _pTypeElement->getXmlElementName();
56*c33452fbSAndroid Build Coastguard Worker }
57*c33452fbSAndroid Build Coastguard Worker
58*c33452fbSAndroid Build Coastguard Worker // Type element
getTypeElement() const59*c33452fbSAndroid Build Coastguard Worker const CTypeElement *CInstanceConfigurableElement::getTypeElement() const
60*c33452fbSAndroid Build Coastguard Worker {
61*c33452fbSAndroid Build Coastguard Worker return _pTypeElement;
62*c33452fbSAndroid Build Coastguard Worker }
63*c33452fbSAndroid Build Coastguard Worker
64*c33452fbSAndroid Build Coastguard Worker // Mapping
getMappingData(const std::string & strKey,const std::string * & pStrValue) const65*c33452fbSAndroid Build Coastguard Worker bool CInstanceConfigurableElement::getMappingData(const std::string &strKey,
66*c33452fbSAndroid Build Coastguard Worker const std::string *&pStrValue) const
67*c33452fbSAndroid Build Coastguard Worker {
68*c33452fbSAndroid Build Coastguard Worker // Delegate
69*c33452fbSAndroid Build Coastguard Worker return getTypeElement()->getMappingData(strKey, pStrValue);
70*c33452fbSAndroid Build Coastguard Worker }
71*c33452fbSAndroid Build Coastguard Worker
72*c33452fbSAndroid Build Coastguard Worker // Returns the formatted mapping
getFormattedMapping() const73*c33452fbSAndroid Build Coastguard Worker std::string CInstanceConfigurableElement::getFormattedMapping() const
74*c33452fbSAndroid Build Coastguard Worker {
75*c33452fbSAndroid Build Coastguard Worker // Delegate
76*c33452fbSAndroid Build Coastguard Worker return getTypeElement()->getFormattedMapping();
77*c33452fbSAndroid Build Coastguard Worker }
78*c33452fbSAndroid Build Coastguard Worker
map(IMapper & mapper,std::string & strError)79*c33452fbSAndroid Build Coastguard Worker bool CInstanceConfigurableElement::map(IMapper &mapper, std::string &strError)
80*c33452fbSAndroid Build Coastguard Worker {
81*c33452fbSAndroid Build Coastguard Worker bool bHasMappingData = getTypeElement()->hasMappingData();
82*c33452fbSAndroid Build Coastguard Worker bool bKeepDiving = true;
83*c33452fbSAndroid Build Coastguard Worker
84*c33452fbSAndroid Build Coastguard Worker // Begin
85*c33452fbSAndroid Build Coastguard Worker if (bHasMappingData && !mapper.mapBegin(this, bKeepDiving, strError)) {
86*c33452fbSAndroid Build Coastguard Worker
87*c33452fbSAndroid Build Coastguard Worker return false;
88*c33452fbSAndroid Build Coastguard Worker }
89*c33452fbSAndroid Build Coastguard Worker
90*c33452fbSAndroid Build Coastguard Worker // Go on through children?
91*c33452fbSAndroid Build Coastguard Worker if (bKeepDiving) {
92*c33452fbSAndroid Build Coastguard Worker
93*c33452fbSAndroid Build Coastguard Worker // Map children
94*c33452fbSAndroid Build Coastguard Worker size_t uiNbChildren = getNbChildren();
95*c33452fbSAndroid Build Coastguard Worker size_t uiChild;
96*c33452fbSAndroid Build Coastguard Worker
97*c33452fbSAndroid Build Coastguard Worker for (uiChild = 0; uiChild < uiNbChildren; uiChild++) {
98*c33452fbSAndroid Build Coastguard Worker
99*c33452fbSAndroid Build Coastguard Worker CInstanceConfigurableElement *pInstanceConfigurableChildElement =
100*c33452fbSAndroid Build Coastguard Worker static_cast<CInstanceConfigurableElement *>(getChild(uiChild));
101*c33452fbSAndroid Build Coastguard Worker
102*c33452fbSAndroid Build Coastguard Worker if (!pInstanceConfigurableChildElement->map(mapper, strError)) {
103*c33452fbSAndroid Build Coastguard Worker
104*c33452fbSAndroid Build Coastguard Worker return false;
105*c33452fbSAndroid Build Coastguard Worker }
106*c33452fbSAndroid Build Coastguard Worker }
107*c33452fbSAndroid Build Coastguard Worker }
108*c33452fbSAndroid Build Coastguard Worker
109*c33452fbSAndroid Build Coastguard Worker // End
110*c33452fbSAndroid Build Coastguard Worker if (bHasMappingData) {
111*c33452fbSAndroid Build Coastguard Worker
112*c33452fbSAndroid Build Coastguard Worker mapper.mapEnd();
113*c33452fbSAndroid Build Coastguard Worker }
114*c33452fbSAndroid Build Coastguard Worker return true;
115*c33452fbSAndroid Build Coastguard Worker }
116*c33452fbSAndroid Build Coastguard Worker
117*c33452fbSAndroid Build Coastguard Worker // Element properties
showProperties(std::string & strResult) const118*c33452fbSAndroid Build Coastguard Worker void CInstanceConfigurableElement::showProperties(std::string &strResult) const
119*c33452fbSAndroid Build Coastguard Worker {
120*c33452fbSAndroid Build Coastguard Worker base::showProperties(strResult);
121*c33452fbSAndroid Build Coastguard Worker
122*c33452fbSAndroid Build Coastguard Worker // Delegate to type element
123*c33452fbSAndroid Build Coastguard Worker _pTypeElement->showProperties(strResult);
124*c33452fbSAndroid Build Coastguard Worker }
125*c33452fbSAndroid Build Coastguard Worker
126*c33452fbSAndroid Build Coastguard Worker // Scalar or Array?
isScalar() const127*c33452fbSAndroid Build Coastguard Worker bool CInstanceConfigurableElement::isScalar() const
128*c33452fbSAndroid Build Coastguard Worker {
129*c33452fbSAndroid Build Coastguard Worker return _pTypeElement->isScalar();
130*c33452fbSAndroid Build Coastguard Worker }
131*c33452fbSAndroid Build Coastguard Worker
132*c33452fbSAndroid Build Coastguard Worker // Array Length
getArrayLength() const133*c33452fbSAndroid Build Coastguard Worker size_t CInstanceConfigurableElement::getArrayLength() const
134*c33452fbSAndroid Build Coastguard Worker {
135*c33452fbSAndroid Build Coastguard Worker return _pTypeElement->getArrayLength();
136*c33452fbSAndroid Build Coastguard Worker }
137*c33452fbSAndroid Build Coastguard Worker
138*c33452fbSAndroid Build Coastguard Worker // Sync to HW
setSyncer(ISyncer * pSyncer)139*c33452fbSAndroid Build Coastguard Worker void CInstanceConfigurableElement::setSyncer(ISyncer *pSyncer)
140*c33452fbSAndroid Build Coastguard Worker {
141*c33452fbSAndroid Build Coastguard Worker assert(!_pSyncer);
142*c33452fbSAndroid Build Coastguard Worker
143*c33452fbSAndroid Build Coastguard Worker _pSyncer = pSyncer;
144*c33452fbSAndroid Build Coastguard Worker }
145*c33452fbSAndroid Build Coastguard Worker
unsetSyncer()146*c33452fbSAndroid Build Coastguard Worker void CInstanceConfigurableElement::unsetSyncer()
147*c33452fbSAndroid Build Coastguard Worker {
148*c33452fbSAndroid Build Coastguard Worker _pSyncer = nullptr;
149*c33452fbSAndroid Build Coastguard Worker }
150*c33452fbSAndroid Build Coastguard Worker
151*c33452fbSAndroid Build Coastguard Worker // Syncer
getSyncer() const152*c33452fbSAndroid Build Coastguard Worker ISyncer *CInstanceConfigurableElement::getSyncer() const
153*c33452fbSAndroid Build Coastguard Worker {
154*c33452fbSAndroid Build Coastguard Worker if (_pSyncer) {
155*c33452fbSAndroid Build Coastguard Worker
156*c33452fbSAndroid Build Coastguard Worker return _pSyncer;
157*c33452fbSAndroid Build Coastguard Worker }
158*c33452fbSAndroid Build Coastguard Worker // Check parent
159*c33452fbSAndroid Build Coastguard Worker return base::getSyncer();
160*c33452fbSAndroid Build Coastguard Worker }
161*c33452fbSAndroid Build Coastguard Worker
162*c33452fbSAndroid Build Coastguard Worker // Syncer set (descendant)
fillSyncerSetFromDescendant(CSyncerSet & syncerSet) const163*c33452fbSAndroid Build Coastguard Worker void CInstanceConfigurableElement::fillSyncerSetFromDescendant(CSyncerSet &syncerSet) const
164*c33452fbSAndroid Build Coastguard Worker {
165*c33452fbSAndroid Build Coastguard Worker if (_pSyncer) {
166*c33452fbSAndroid Build Coastguard Worker
167*c33452fbSAndroid Build Coastguard Worker syncerSet += _pSyncer;
168*c33452fbSAndroid Build Coastguard Worker } else {
169*c33452fbSAndroid Build Coastguard Worker // Continue digging
170*c33452fbSAndroid Build Coastguard Worker base::fillSyncerSetFromDescendant(syncerSet);
171*c33452fbSAndroid Build Coastguard Worker }
172*c33452fbSAndroid Build Coastguard Worker }
173*c33452fbSAndroid Build Coastguard Worker
sync(CParameterAccessContext & parameterAccessContext) const174*c33452fbSAndroid Build Coastguard Worker bool CInstanceConfigurableElement::sync(CParameterAccessContext ¶meterAccessContext) const
175*c33452fbSAndroid Build Coastguard Worker {
176*c33452fbSAndroid Build Coastguard Worker if (!parameterAccessContext.getAutoSync()) {
177*c33452fbSAndroid Build Coastguard Worker
178*c33452fbSAndroid Build Coastguard Worker // AutoSync is disabled, do not perform the sync.
179*c33452fbSAndroid Build Coastguard Worker // This is not an error, but the expected behavior so return true anyway.
180*c33452fbSAndroid Build Coastguard Worker return true;
181*c33452fbSAndroid Build Coastguard Worker }
182*c33452fbSAndroid Build Coastguard Worker ISyncer *pSyncer = getSyncer();
183*c33452fbSAndroid Build Coastguard Worker
184*c33452fbSAndroid Build Coastguard Worker if (!pSyncer) {
185*c33452fbSAndroid Build Coastguard Worker
186*c33452fbSAndroid Build Coastguard Worker parameterAccessContext.setError("Unable to synchronize modification. No Syncer object "
187*c33452fbSAndroid Build Coastguard Worker "associated to configurable element:");
188*c33452fbSAndroid Build Coastguard Worker
189*c33452fbSAndroid Build Coastguard Worker return false;
190*c33452fbSAndroid Build Coastguard Worker }
191*c33452fbSAndroid Build Coastguard Worker std::string strError;
192*c33452fbSAndroid Build Coastguard Worker
193*c33452fbSAndroid Build Coastguard Worker if (!pSyncer->sync(*parameterAccessContext.getParameterBlackboard(), false, strError)) {
194*c33452fbSAndroid Build Coastguard Worker
195*c33452fbSAndroid Build Coastguard Worker parameterAccessContext.setError(strError);
196*c33452fbSAndroid Build Coastguard Worker
197*c33452fbSAndroid Build Coastguard Worker return false;
198*c33452fbSAndroid Build Coastguard Worker }
199*c33452fbSAndroid Build Coastguard Worker return true;
200*c33452fbSAndroid Build Coastguard Worker }
201*c33452fbSAndroid Build Coastguard Worker
202*c33452fbSAndroid Build Coastguard Worker // Check parameter access path well formed for leaf elements
checkPathExhausted(CPathNavigator & pathNavigator,utility::ErrorContext & errorContext)203*c33452fbSAndroid Build Coastguard Worker bool CInstanceConfigurableElement::checkPathExhausted(CPathNavigator &pathNavigator,
204*c33452fbSAndroid Build Coastguard Worker utility::ErrorContext &errorContext)
205*c33452fbSAndroid Build Coastguard Worker {
206*c33452fbSAndroid Build Coastguard Worker std::string *pStrChildName = pathNavigator.next();
207*c33452fbSAndroid Build Coastguard Worker
208*c33452fbSAndroid Build Coastguard Worker if (pStrChildName) {
209*c33452fbSAndroid Build Coastguard Worker
210*c33452fbSAndroid Build Coastguard Worker // Should be leaf element
211*c33452fbSAndroid Build Coastguard Worker errorContext.setError("Path not found: " + pathNavigator.getCurrentPath());
212*c33452fbSAndroid Build Coastguard Worker
213*c33452fbSAndroid Build Coastguard Worker return false;
214*c33452fbSAndroid Build Coastguard Worker }
215*c33452fbSAndroid Build Coastguard Worker return true;
216*c33452fbSAndroid Build Coastguard Worker }
217*c33452fbSAndroid Build Coastguard Worker
structureToXml(CXmlElement & xmlElement,CXmlSerializingContext & serializingContext) const218*c33452fbSAndroid Build Coastguard Worker void CInstanceConfigurableElement::structureToXml(CXmlElement &xmlElement,
219*c33452fbSAndroid Build Coastguard Worker CXmlSerializingContext &serializingContext) const
220*c33452fbSAndroid Build Coastguard Worker {
221*c33452fbSAndroid Build Coastguard Worker base::structureToXml(xmlElement, serializingContext);
222*c33452fbSAndroid Build Coastguard Worker // Since Description belongs to the Type of Element, delegate it to the type element.
223*c33452fbSAndroid Build Coastguard Worker getTypeElement()->setXmlDescriptionAttribute(xmlElement);
224*c33452fbSAndroid Build Coastguard Worker }
225