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