Shawn McCarney | 8215be3 | 2020-02-19 10:00:57 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "i2c_compare_bit_action.hpp" |
| 18 | |
| 19 | #include "action_error.hpp" |
| 20 | #include "i2c_interface.hpp" |
| 21 | |
| 22 | #include <exception> |
| 23 | #include <ios> |
| 24 | #include <sstream> |
| 25 | |
| 26 | namespace phosphor::power::regulators |
| 27 | { |
| 28 | |
| 29 | bool I2CCompareBitAction::execute(ActionEnvironment& environment) |
| 30 | { |
| 31 | bool isEqual{false}; |
| 32 | try |
| 33 | { |
| 34 | // Read actual value of device register |
| 35 | uint8_t registerValue{0x00}; |
| 36 | i2c::I2CInterface& interface = getI2CInterface(environment); |
| 37 | interface.read(reg, registerValue); |
| 38 | |
| 39 | // Get actual bit value |
| 40 | uint8_t actualValue = (registerValue >> position) & 0x01; |
| 41 | |
| 42 | // Check if actual bit value equals expected bit value |
| 43 | isEqual = (actualValue == value); |
| 44 | } |
| 45 | catch (const i2c::I2CException& e) |
| 46 | { |
| 47 | // Nest I2CException within an ActionError so caller will have both the |
| 48 | // low level I2C error information and the action information |
| 49 | std::throw_with_nested(ActionError(*this)); |
| 50 | } |
| 51 | return isEqual; |
| 52 | } |
| 53 | |
| 54 | std::string I2CCompareBitAction::toString() const |
| 55 | { |
| 56 | std::ostringstream ss; |
| 57 | ss << "i2c_compare_bit: { register: 0x" << std::hex << std::uppercase |
| 58 | << static_cast<uint16_t>(reg) << ", position: " << std::dec |
| 59 | << static_cast<uint16_t>(position) |
| 60 | << ", value: " << static_cast<uint16_t>(value) << " }"; |
| 61 | return ss.str(); |
| 62 | } |
| 63 | |
| 64 | } // namespace phosphor::power::regulators |