| David Wagner | b76c9d6 | 2014-02-05 18:30:24 +0100 | [diff] [blame] | 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. |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 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" |
| Frédéric Boisnard | 9620e44 | 2012-05-30 16:15:02 +0200 | [diff] [blame] | 36 | #include "BitwiseAreaConfiguration.h" |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 37 | |
| Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 38 | #define base CBaseParameter |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 39 | |
| 40 | CBitParameter::CBitParameter(const string& strName, const CTypeElement* pTypeElement) : base(strName, pTypeElement) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | // Type |
| 45 | CInstanceConfigurableElement::Type CBitParameter::getType() const |
| 46 | { |
| 47 | return EBitParameter; |
| 48 | } |
| 49 | |
| 50 | // Size |
| Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 51 | uint32_t CBitParameter::getBelongingBlockSize() const |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 52 | { |
| 53 | return static_cast<const CBitParameterBlock*>(getParent())->getSize(); |
| 54 | } |
| 55 | |
| Patrick Benavoli | 2ecf900 | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 56 | // Instantiation, allocation |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 57 | uint32_t CBitParameter::getFootPrint() const |
| 58 | { |
| Frédéric Boisnard | 9620e44 | 2012-05-30 16:15:02 +0200 | [diff] [blame] | 59 | // Allocation done at parent level |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 63 | // Actual parameter access (tuning) |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 64 | bool CBitParameter::doSetValue(const string& strValue, uint32_t uiOffset, CParameterAccessContext& parameterAccessContext) const |
| 65 | { |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 66 | return doSet(strValue, uiOffset, parameterAccessContext); |
| 67 | } |
| 68 | |
| 69 | void CBitParameter::doGetValue(string& strValue, uint32_t uiOffset, CParameterAccessContext& parameterAccessContext) const |
| 70 | { |
| 71 | doGet(strValue, uiOffset, parameterAccessContext); |
| 72 | } |
| 73 | |
| 74 | /// Value access |
| 75 | // Boolean access |
| 76 | bool CBitParameter::accessAsBoolean(bool& bValue, bool bSet, CParameterAccessContext& parameterAccessContext) const |
| 77 | { |
| 78 | // Check boolean access validity here |
| 79 | if (static_cast<const CBitParameterType*>(getTypeElement())->getBitSize() != 1) { |
| 80 | |
| 81 | parameterAccessContext.setError("Type mismatch"); |
| Francois Gaffie | a1258f4 | 2014-04-07 13:47:48 +0200 | [diff] [blame^] | 82 | appendParameterPathToError(parameterAccessContext); |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 83 | |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | // Rely on integer access |
| Guillaume Denneulin | 9533156 | 2012-09-27 15:13:10 +0200 | [diff] [blame] | 88 | uint64_t uiValue; |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 89 | |
| 90 | if (bSet) { |
| 91 | |
| 92 | uiValue = bValue; |
| 93 | } |
| 94 | |
| 95 | if (!accessAsInteger(uiValue, bSet, parameterAccessContext)) { |
| 96 | |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | if (!bSet) { |
| 101 | |
| 102 | bValue = uiValue != 0; |
| 103 | } |
| 104 | |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | // Integer Access |
| Guillaume Denneulin | 9533156 | 2012-09-27 15:13:10 +0200 | [diff] [blame] | 109 | bool CBitParameter::accessAsInteger(uint64_t& uiValue, bool bSet, CParameterAccessContext& parameterAccessContext) const |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 110 | { |
| 111 | uint32_t uiOffset = getOffset(); |
| 112 | |
| 113 | if (bSet) { |
| 114 | |
| Francois Gaffie | 8ab7293 | 2014-04-07 13:46:05 +0200 | [diff] [blame] | 115 | // Set Value |
| 116 | if (!doSet(uiValue, uiOffset, parameterAccessContext)) { |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 117 | |
| Francois Gaffie | a1258f4 | 2014-04-07 13:47:48 +0200 | [diff] [blame^] | 118 | appendParameterPathToError(parameterAccessContext); |
| Francois Gaffie | 8ab7293 | 2014-04-07 13:46:05 +0200 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | // Synchronize |
| 122 | if (!sync(parameterAccessContext)) { |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 123 | |
| Francois Gaffie | a1258f4 | 2014-04-07 13:47:48 +0200 | [diff] [blame^] | 124 | appendParameterPathToError(parameterAccessContext); |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | } else { |
| 128 | |
| 129 | // Convert |
| 130 | doGet(uiValue, uiOffset, parameterAccessContext); |
| 131 | } |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | template <typename type> |
| 136 | bool CBitParameter::doSet(type value, uint32_t uiOffset, CParameterAccessContext& parameterAccessContext) const |
| 137 | { |
| Guillaume Denneulin | 9533156 | 2012-09-27 15:13:10 +0200 | [diff] [blame] | 138 | uint64_t uiData = 0; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 139 | |
| 140 | // Read/modify/write |
| 141 | CParameterBlackboard* pBlackboard = parameterAccessContext.getParameterBlackboard(); |
| 142 | |
| 143 | // Beware this code works on little endian architectures only! |
| Patrick Benavoli | 1352ae5 | 2011-10-21 16:48:04 +0200 | [diff] [blame] | 144 | pBlackboard->readInteger(&uiData, getBelongingBlockSize(), uiOffset, parameterAccessContext.isBigEndianSubsystem()); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 145 | |
| 146 | // Convert |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 147 | if (!static_cast<const CBitParameterType*>(getTypeElement())->toBlackboard(value, uiData, parameterAccessContext)) { |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 148 | |
| 149 | return false; |
| 150 | } |
| 151 | // Write blackboard |
| Patrick Benavoli | 1352ae5 | 2011-10-21 16:48:04 +0200 | [diff] [blame] | 152 | pBlackboard->writeInteger(&uiData, getBelongingBlockSize(), uiOffset, parameterAccessContext.isBigEndianSubsystem()); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 153 | |
| 154 | return true; |
| 155 | } |
| 156 | |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 157 | template <typename type> |
| 158 | void CBitParameter::doGet(type& value, uint32_t uiOffset, CParameterAccessContext& parameterAccessContext) const |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 159 | { |
| Guillaume Denneulin | 9533156 | 2012-09-27 15:13:10 +0200 | [diff] [blame] | 160 | uint64_t uiData = 0; |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 161 | |
| 162 | // Read blackboard |
| Patrick Benavoli | 1352ae5 | 2011-10-21 16:48:04 +0200 | [diff] [blame] | 163 | const CParameterBlackboard* pBlackboard = parameterAccessContext.getParameterBlackboard(); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 164 | |
| 165 | // Beware this code works on little endian architectures only! |
| Patrick Benavoli | 1352ae5 | 2011-10-21 16:48:04 +0200 | [diff] [blame] | 166 | pBlackboard->readInteger(&uiData, getBelongingBlockSize(), uiOffset, parameterAccessContext.isBigEndianSubsystem()); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 167 | |
| 168 | // Convert |
| Patrick Benavoli | 065264a | 2011-11-20 15:46:41 +0100 | [diff] [blame] | 169 | static_cast<const CBitParameterType*>(getTypeElement())->fromBlackboard(value, uiData, parameterAccessContext); |
| Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 170 | } |
| Frédéric Boisnard | 9620e44 | 2012-05-30 16:15:02 +0200 | [diff] [blame] | 171 | |
| 172 | // AreaConfiguration creation |
| 173 | CAreaConfiguration* CBitParameter::createAreaConfiguration(const CSyncerSet* pSyncerSet) const |
| 174 | { |
| 175 | return new CBitwiseAreaConfiguration(this, pSyncerSet); |
| 176 | } |
| 177 | |
| 178 | // Access from area configuration |
| Guillaume Denneulin | 9533156 | 2012-09-27 15:13:10 +0200 | [diff] [blame] | 179 | uint64_t CBitParameter::merge(uint64_t uiOriginData, uint64_t uiNewData) const |
| Frédéric Boisnard | 9620e44 | 2012-05-30 16:15:02 +0200 | [diff] [blame] | 180 | { |
| 181 | // Convert |
| 182 | return static_cast<const CBitParameterType*>(getTypeElement())->merge(uiOriginData, uiNewData); |
| 183 | } |