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 "BitParameter.h"
31 #include "BitParameterType.h"
32 #include "ParameterAccessContext.h"
33 #include "ConfigurationAccessContext.h"
34 #include "ParameterBlackboard.h"
35 #include "BitParameterBlock.h"
36 #include "BitwiseAreaConfiguration.h"
37 
38 #define base CBaseParameter
39 
40 using std::string;
41 
CBitParameter(const string & strName,const CTypeElement * pTypeElement)42 CBitParameter::CBitParameter(const string& strName, const CTypeElement* pTypeElement) : base(strName, pTypeElement)
43 {
44 }
45 
46 // Type
getType() const47 CInstanceConfigurableElement::Type CBitParameter::getType() const
48 {
49     return EBitParameter;
50 }
51 
52 // Size
getBelongingBlockSize() const53 uint32_t CBitParameter::getBelongingBlockSize() const
54 {
55     return static_cast<const CBitParameterBlock*>(getParent())->getSize();
56 }
57 
58 // Instantiation, allocation
getFootPrint() const59 uint32_t CBitParameter::getFootPrint() const
60 {
61     // Allocation done at parent level
62     return 0;
63 }
64 
65 // Actual parameter access (tuning)
doSetValue(const string & strValue,uint32_t uiOffset,CParameterAccessContext & parameterAccessContext) const66 bool CBitParameter::doSetValue(const string& strValue, uint32_t uiOffset, CParameterAccessContext& parameterAccessContext) const
67 {
68     return doSet(strValue, uiOffset, parameterAccessContext);
69 }
70 
doGetValue(string & strValue,uint32_t uiOffset,CParameterAccessContext & parameterAccessContext) const71 void CBitParameter::doGetValue(string& strValue, uint32_t uiOffset, CParameterAccessContext& parameterAccessContext) const
72 {
73     doGet(strValue, uiOffset, parameterAccessContext);
74 }
75 
76 /// Value access
77 // Boolean access
accessAsBoolean(bool & bValue,bool bSet,CParameterAccessContext & parameterAccessContext) const78 bool CBitParameter::accessAsBoolean(bool& bValue, bool bSet, CParameterAccessContext& parameterAccessContext) const
79 {
80     // Check boolean access validity here
81     if (static_cast<const CBitParameterType*>(getTypeElement())->getBitSize() != 1) {
82 
83         parameterAccessContext.setError("Type mismatch");
84         appendParameterPathToError(parameterAccessContext);
85 
86         return false;
87     }
88 
89     // Rely on integer access
90     uint32_t uiValue;
91 
92     if (bSet) {
93 
94         uiValue = bValue;
95     }
96 
97     if (!accessAsInteger(uiValue, bSet, parameterAccessContext)) {
98 
99         return false;
100     }
101 
102     if (!bSet) {
103 
104         bValue = uiValue != 0;
105     }
106 
107     return true;
108 }
109 
110 // Integer Access
accessAsInteger(uint32_t & uiValue,bool bSet,CParameterAccessContext & parameterAccessContext) const111 bool CBitParameter::accessAsInteger(uint32_t& uiValue, bool bSet, CParameterAccessContext& parameterAccessContext) const
112 {
113     uint32_t uiOffset = getOffset();
114 
115     if (bSet) {
116 
117         // Set Value
118         if (!doSet(uiValue, uiOffset, parameterAccessContext)) {
119 
120             appendParameterPathToError(parameterAccessContext);
121             return false;
122         }
123         // Synchronize
124         if (!sync(parameterAccessContext)) {
125 
126             appendParameterPathToError(parameterAccessContext);
127             return false;
128         }
129     } else {
130 
131         // Convert
132         doGet(uiValue, uiOffset, parameterAccessContext);
133     }
134     return true;
135 }
136 
137 template <typename type>
doSet(type value,uint32_t uiOffset,CParameterAccessContext & parameterAccessContext) const138 bool CBitParameter::doSet(type value, uint32_t uiOffset, CParameterAccessContext& parameterAccessContext) const
139 {
140     uint64_t uiData = 0;
141 
142     // Read/modify/write
143     CParameterBlackboard* pBlackboard = parameterAccessContext.getParameterBlackboard();
144 
145     // Beware this code works on little endian architectures only!
146     pBlackboard->readInteger(&uiData, getBelongingBlockSize(), uiOffset, parameterAccessContext.isBigEndianSubsystem());
147 
148     // Convert
149     if (!static_cast<const CBitParameterType*>(getTypeElement())->toBlackboard(value, uiData, parameterAccessContext)) {
150 
151         return false;
152     }
153     // Write blackboard
154     pBlackboard->writeInteger(&uiData, getBelongingBlockSize(), uiOffset, parameterAccessContext.isBigEndianSubsystem());
155 
156     return true;
157 }
158 
159 template <typename type>
doGet(type & value,uint32_t uiOffset,CParameterAccessContext & parameterAccessContext) const160 void CBitParameter::doGet(type& value, uint32_t uiOffset, CParameterAccessContext& parameterAccessContext) const
161 {
162     uint64_t uiData = 0;
163 
164     // Read blackboard
165     const CParameterBlackboard* pBlackboard = parameterAccessContext.getParameterBlackboard();
166 
167     // Beware this code works on little endian architectures only!
168     pBlackboard->readInteger(&uiData, getBelongingBlockSize(), uiOffset, parameterAccessContext.isBigEndianSubsystem());
169 
170     // Convert
171     static_cast<const CBitParameterType*>(getTypeElement())->fromBlackboard(value, uiData, parameterAccessContext);
172 }
173 
174 // AreaConfiguration creation
createAreaConfiguration(const CSyncerSet * pSyncerSet) const175 CAreaConfiguration* CBitParameter::createAreaConfiguration(const CSyncerSet* pSyncerSet) const
176 {
177     return new CBitwiseAreaConfiguration(this, pSyncerSet);
178 }
179 
180 // Access from area configuration
merge(uint64_t uiOriginData,uint64_t uiNewData) const181 uint64_t CBitParameter::merge(uint64_t uiOriginData, uint64_t uiNewData) const
182 {
183     // Convert
184     return static_cast<const CBitParameterType*>(getTypeElement())->merge(uiOriginData, uiNewData);
185 }
186