Bob King | 717d2da | 2020-06-02 11:11:15 +0800 | [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 "pmbus_read_sensor_action.hpp" |
| 18 | |
| 19 | #include "action_error.hpp" |
| 20 | #include "pmbus_error.hpp" |
| 21 | |
| 22 | #include <exception> |
| 23 | #include <ios> |
| 24 | #include <sstream> |
| 25 | |
| 26 | namespace phosphor::power::regulators |
| 27 | { |
| 28 | |
| 29 | bool PMBusReadSensorAction::execute(ActionEnvironment& environment) |
| 30 | { |
| 31 | try |
| 32 | { |
| 33 | pmbus_utils::SensorReading reading{}; |
| 34 | reading.type = type; |
| 35 | |
| 36 | // Get I2C interface to current device |
| 37 | i2c::I2CInterface& interface = getI2CInterface(environment); |
| 38 | |
| 39 | // Read value of the PMBus command code |
| 40 | uint16_t value; |
| 41 | interface.read(command, value); |
| 42 | |
| 43 | switch (format) |
| 44 | { |
| 45 | case pmbus_utils::SensorDataFormat::linear_11: |
| 46 | // Convert linear_11 format to a normal decimal number |
| 47 | reading.value = pmbus_utils::convertFromLinear(value); |
| 48 | break; |
| 49 | case pmbus_utils::SensorDataFormat::linear_16: |
| 50 | // Get exponent value for converting linear format to volts |
| 51 | // value |
| 52 | int8_t exponentValue = getExponentValue(interface); |
| 53 | |
| 54 | // Convert linear_16 format to a normal decimal number |
| 55 | reading.value = |
| 56 | pmbus_utils::convertFromVoutLinear(value, exponentValue); |
| 57 | break; |
| 58 | } |
| 59 | |
| 60 | environment.addSensorReading(reading); |
| 61 | } |
| 62 | // Nest the following exception types within an ActionError so the caller |
| 63 | // will have both the low level error information and the action information |
| 64 | catch (const i2c::I2CException& i2cError) |
| 65 | { |
| 66 | std::throw_with_nested(ActionError(*this)); |
| 67 | } |
| 68 | catch (const PMBusError& pmbusError) |
| 69 | { |
| 70 | std::throw_with_nested(ActionError(*this)); |
| 71 | } |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | std::string PMBusReadSensorAction::toString() const |
| 76 | { |
| 77 | std::ostringstream ss; |
| 78 | ss << "pmbus_read_sensor: { "; |
| 79 | ss << "type: " << pmbus_utils::toString(type) << ", " << std::hex |
| 80 | << std::uppercase; |
| 81 | ss << "command: 0x" << static_cast<uint16_t>(command) << ", " << std::dec |
| 82 | << std::nouppercase; |
| 83 | ss << "format: " << pmbus_utils::toString(format); |
| 84 | |
| 85 | if (exponent.has_value()) |
| 86 | { |
| 87 | ss << ", exponent: " << static_cast<int16_t>(exponent.value()); |
| 88 | } |
| 89 | |
| 90 | ss << " }"; |
| 91 | |
| 92 | return ss.str(); |
| 93 | } |
| 94 | |
| 95 | int8_t PMBusReadSensorAction::getExponentValue(i2c::I2CInterface& interface) |
| 96 | { |
| 97 | // Check if an exponent value is defined for this action |
| 98 | if (exponent.has_value()) |
| 99 | { |
| 100 | return exponent.value(); |
| 101 | } |
| 102 | |
| 103 | // Read value of the VOUT_MODE command |
| 104 | uint8_t voutModeValue; |
| 105 | interface.read(pmbus_utils::VOUT_MODE, voutModeValue); |
| 106 | |
| 107 | // Parse VOUT_MODE value to get data format and parameter value |
| 108 | pmbus_utils::VoutDataFormat format; |
| 109 | int8_t parameter; |
| 110 | pmbus_utils::parseVoutMode(voutModeValue, format, parameter); |
| 111 | |
| 112 | // Verify format is linear; other formats not currently supported |
| 113 | if (format != pmbus_utils::VoutDataFormat::linear) |
| 114 | { |
| 115 | throw PMBusError("VOUT_MODE contains unsupported data format"); |
| 116 | } |
| 117 | |
| 118 | // Return parameter value; it contains the exponent when format is linear |
| 119 | return parameter; |
| 120 | } |
| 121 | |
| 122 | } // namespace phosphor::power::regulators |