blob: 7ad0cad4bd3590a9beeabee3c05115379f425bb4 [file] [log] [blame]
Bob King717d2da2020-06-02 11:11:15 +08001/**
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
26namespace phosphor::power::regulators
27{
28
29bool PMBusReadSensorAction::execute(ActionEnvironment& environment)
30{
31 try
32 {
Bob King717d2da2020-06-02 11:11:15 +080033 // Get I2C interface to current device
34 i2c::I2CInterface& interface = getI2CInterface(environment);
35
36 // Read value of the PMBus command code
37 uint16_t value;
38 interface.read(command, value);
39
Shawn McCarney5135df62021-04-28 15:53:24 -050040 // Convert PMBus 2-byte value to a double
41 // TODO: Store result of conversion in a double. Not doing it yet
42 // because it results in an unused variable compile error during CI.
Bob King717d2da2020-06-02 11:11:15 +080043 switch (format)
44 {
45 case pmbus_utils::SensorDataFormat::linear_11:
46 // Convert linear_11 format to a normal decimal number
Shawn McCarney5135df62021-04-28 15:53:24 -050047 pmbus_utils::convertFromLinear(value);
Bob King717d2da2020-06-02 11:11:15 +080048 break;
49 case pmbus_utils::SensorDataFormat::linear_16:
50 // Get exponent value for converting linear format to volts
Shawn McCarney5b819f42021-03-16 14:41:15 -050051 int8_t exponentValue = getExponentValue(environment, interface);
Bob King717d2da2020-06-02 11:11:15 +080052
53 // Convert linear_16 format to a normal decimal number
Shawn McCarney5135df62021-04-28 15:53:24 -050054 pmbus_utils::convertFromVoutLinear(value, exponentValue);
Bob King717d2da2020-06-02 11:11:15 +080055 break;
56 }
57
Shawn McCarney5135df62021-04-28 15:53:24 -050058 // TODO: Publish sensor value using Sensors service
Bob King717d2da2020-06-02 11:11:15 +080059 }
60 // Nest the following exception types within an ActionError so the caller
61 // will have both the low level error information and the action information
62 catch (const i2c::I2CException& i2cError)
63 {
64 std::throw_with_nested(ActionError(*this));
65 }
66 catch (const PMBusError& pmbusError)
67 {
68 std::throw_with_nested(ActionError(*this));
69 }
70 return true;
71}
72
73std::string PMBusReadSensorAction::toString() const
74{
75 std::ostringstream ss;
76 ss << "pmbus_read_sensor: { ";
Shawn McCarney2f9e14f2021-04-29 02:45:18 -050077 ss << "type: " << sensors::toString(type) << ", " << std::hex
Bob King717d2da2020-06-02 11:11:15 +080078 << std::uppercase;
79 ss << "command: 0x" << static_cast<uint16_t>(command) << ", " << std::dec
80 << std::nouppercase;
81 ss << "format: " << pmbus_utils::toString(format);
82
83 if (exponent.has_value())
84 {
85 ss << ", exponent: " << static_cast<int16_t>(exponent.value());
86 }
87
88 ss << " }";
89
90 return ss.str();
91}
92
Shawn McCarney5b819f42021-03-16 14:41:15 -050093int8_t PMBusReadSensorAction::getExponentValue(ActionEnvironment& environment,
94 i2c::I2CInterface& interface)
Bob King717d2da2020-06-02 11:11:15 +080095{
96 // Check if an exponent value is defined for this action
97 if (exponent.has_value())
98 {
99 return exponent.value();
100 }
101
102 // Read value of the VOUT_MODE command
103 uint8_t voutModeValue;
104 interface.read(pmbus_utils::VOUT_MODE, voutModeValue);
105
106 // Parse VOUT_MODE value to get data format and parameter value
107 pmbus_utils::VoutDataFormat format;
108 int8_t parameter;
109 pmbus_utils::parseVoutMode(voutModeValue, format, parameter);
110
111 // Verify format is linear; other formats not currently supported
112 if (format != pmbus_utils::VoutDataFormat::linear)
113 {
Shawn McCarney5b819f42021-03-16 14:41:15 -0500114 throw PMBusError("VOUT_MODE contains unsupported data format",
115 environment.getDeviceID(),
116 environment.getDevice().getFRU());
Bob King717d2da2020-06-02 11:11:15 +0800117 }
118
119 // Return parameter value; it contains the exponent when format is linear
120 return parameter;
121}
122
123} // namespace phosphor::power::regulators