James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel 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 | */ |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 16 | #include <PwmSensor.hpp> |
| 17 | #include <Utils.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 18 | #include <sdbusplus/asio/object_server.hpp> |
| 19 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 20 | #include <fstream> |
| 21 | #include <iostream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 22 | #include <stdexcept> |
| 23 | #include <string> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 24 | |
Josh Lehan | 6e6ac0f | 2020-12-30 22:32:57 -0800 | [diff] [blame] | 25 | static constexpr double sysPwmMax = 255.0; |
| 26 | static constexpr double psuPwmMax = 100.0; |
James Feist | 3a07f55 | 2019-08-27 13:34:54 -0700 | [diff] [blame] | 27 | static constexpr double defaultPwm = 30.0; |
Josh Lehan | 6e6ac0f | 2020-12-30 22:32:57 -0800 | [diff] [blame] | 28 | static constexpr double targetIfaceMax = sysPwmMax; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 29 | |
Cheng C Yang | 916360b | 2019-05-07 18:47:16 +0800 | [diff] [blame] | 30 | PwmSensor::PwmSensor(const std::string& name, const std::string& sysPath, |
AppaRao Puli | d9d8caf | 2020-02-27 20:56:59 +0530 | [diff] [blame] | 31 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 32 | sdbusplus::asio::object_server& objectServer, |
AppaRao Puli | d9d8caf | 2020-02-27 20:56:59 +0530 | [diff] [blame] | 33 | const std::string& sensorConfiguration, |
Jie Yang | 3291b9c | 2021-07-29 14:46:51 -0700 | [diff] [blame^] | 34 | const std::string& sensorType, bool isValueMutable) : |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 35 | sysPath(sysPath), |
| 36 | objectServer(objectServer), name(name) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 37 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 38 | // add interface under sensor and Control.FanPwm as Control is used |
| 39 | // in obmc project, also add sensor so it can be viewed as a sensor |
| 40 | sensorInterface = objectServer.add_interface( |
| 41 | "/xyz/openbmc_project/sensors/fan_pwm/" + name, |
| 42 | "xyz.openbmc_project.Sensor.Value"); |
| 43 | uint32_t pwmValue = getValue(false); |
Kuiying Wang | 105a197 | 2020-08-28 19:07:53 +0800 | [diff] [blame] | 44 | if (sensorType == "PSU") |
| 45 | { |
| 46 | pwmMax = psuPwmMax; |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | pwmMax = sysPwmMax; |
| 51 | } |
| 52 | |
James Feist | 3a07f55 | 2019-08-27 13:34:54 -0700 | [diff] [blame] | 53 | if (!pwmValue) |
| 54 | { |
| 55 | // default pwm to non 0 |
Josh Lehan | 6e6ac0f | 2020-12-30 22:32:57 -0800 | [diff] [blame] | 56 | pwmValue = static_cast<uint32_t>(pwmMax * (defaultPwm / 100.0)); |
James Feist | 3a07f55 | 2019-08-27 13:34:54 -0700 | [diff] [blame] | 57 | setValue(pwmValue); |
| 58 | } |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 59 | double fValue = 100.0 * (static_cast<double>(pwmValue) / pwmMax); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 60 | sensorInterface->register_property( |
| 61 | "Value", fValue, |
| 62 | [this](const double& req, double& resp) { |
Josh Lehan | 6e6ac0f | 2020-12-30 22:32:57 -0800 | [diff] [blame] | 63 | if (!std::isfinite(req)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 64 | { |
Josh Lehan | 6e6ac0f | 2020-12-30 22:32:57 -0800 | [diff] [blame] | 65 | // Reject attempted change, if to NaN or other non-numeric |
| 66 | return -1; |
| 67 | } |
| 68 | if (req > 100.0 || req < 0.0) |
| 69 | { |
| 70 | // TODO(): It does not seem desirable to halt daemon here, |
| 71 | // probably should just reject the change, continue running? |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 72 | throw std::runtime_error("Value out of range"); |
| 73 | return -1; |
| 74 | } |
Josh Lehan | 6e6ac0f | 2020-12-30 22:32:57 -0800 | [diff] [blame] | 75 | |
| 76 | double reqValue = (req / 100.0) * pwmMax; |
| 77 | double respValue = (resp / 100.0) * pwmMax; |
| 78 | auto reqInt = static_cast<uint32_t>(std::round(reqValue)); |
| 79 | auto respInt = static_cast<uint32_t>(std::round(respValue)); |
| 80 | // Avoid floating-point equality, compare as integers |
| 81 | if (reqInt == respInt) |
James Feist | 46c5c1d | 2018-11-30 12:04:07 -0800 | [diff] [blame] | 82 | { |
| 83 | return 1; |
| 84 | } |
Josh Lehan | 6e6ac0f | 2020-12-30 22:32:57 -0800 | [diff] [blame] | 85 | setValue(reqInt); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 86 | resp = req; |
James Feist | 46c5c1d | 2018-11-30 12:04:07 -0800 | [diff] [blame] | 87 | |
| 88 | controlInterface->signal_property("Target"); |
| 89 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 90 | return 1; |
| 91 | }, |
| 92 | [this](double& curVal) { |
Josh Lehan | 6e6ac0f | 2020-12-30 22:32:57 -0800 | [diff] [blame] | 93 | double currScaled = (curVal / 100.0) * pwmMax; |
| 94 | auto currInt = static_cast<uint32_t>(std::round(currScaled)); |
| 95 | auto getInt = getValue(); |
| 96 | // Avoid floating-point equality, compare as integers |
| 97 | if (currInt != getInt) |
James Feist | 46c5c1d | 2018-11-30 12:04:07 -0800 | [diff] [blame] | 98 | { |
Josh Lehan | 6e6ac0f | 2020-12-30 22:32:57 -0800 | [diff] [blame] | 99 | double getScaled = |
| 100 | 100.0 * (static_cast<double>(getInt) / pwmMax); |
| 101 | curVal = getScaled; |
James Feist | 46c5c1d | 2018-11-30 12:04:07 -0800 | [diff] [blame] | 102 | controlInterface->signal_property("Target"); |
| 103 | sensorInterface->signal_property("Value"); |
| 104 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 105 | return curVal; |
| 106 | }); |
| 107 | // pwm sensor interface is in percent |
| 108 | sensorInterface->register_property("MaxValue", static_cast<int64_t>(100)); |
| 109 | sensorInterface->register_property("MinValue", static_cast<int64_t>(0)); |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 110 | sensorInterface->register_property("Unit", sensor_paths::unitPercent); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 111 | |
| 112 | controlInterface = objectServer.add_interface( |
| 113 | "/xyz/openbmc_project/control/fanpwm/" + name, |
| 114 | "xyz.openbmc_project.Control.FanPwm"); |
| 115 | controlInterface->register_property( |
| 116 | "Target", static_cast<uint64_t>(pwmValue), |
| 117 | [this](const uint64_t& req, uint64_t& resp) { |
Josh Lehan | 6e6ac0f | 2020-12-30 22:32:57 -0800 | [diff] [blame] | 118 | if (req > static_cast<uint64_t>(targetIfaceMax)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 119 | { |
| 120 | throw std::runtime_error("Value out of range"); |
| 121 | return -1; |
| 122 | } |
James Feist | 46c5c1d | 2018-11-30 12:04:07 -0800 | [diff] [blame] | 123 | if (req == resp) |
| 124 | { |
| 125 | return 1; |
| 126 | } |
Josh Lehan | 6e6ac0f | 2020-12-30 22:32:57 -0800 | [diff] [blame] | 127 | auto scaledValue = static_cast<double>(req) / targetIfaceMax; |
| 128 | auto roundValue = std::round(scaledValue * pwmMax); |
| 129 | setValue(static_cast<uint32_t>(roundValue)); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 130 | resp = req; |
James Feist | 46c5c1d | 2018-11-30 12:04:07 -0800 | [diff] [blame] | 131 | |
| 132 | sensorInterface->signal_property("Value"); |
| 133 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 134 | return 1; |
| 135 | }, |
| 136 | [this](uint64_t& curVal) { |
Josh Lehan | 6e6ac0f | 2020-12-30 22:32:57 -0800 | [diff] [blame] | 137 | auto getInt = getValue(); |
| 138 | auto scaledValue = static_cast<double>(getInt) / pwmMax; |
| 139 | auto roundValue = std::round(scaledValue * targetIfaceMax); |
| 140 | auto value = static_cast<uint64_t>(roundValue); |
James Feist | 46c5c1d | 2018-11-30 12:04:07 -0800 | [diff] [blame] | 141 | if (curVal != value) |
| 142 | { |
| 143 | curVal = value; |
| 144 | controlInterface->signal_property("Target"); |
| 145 | sensorInterface->signal_property("Value"); |
| 146 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 147 | return curVal; |
| 148 | }); |
Jie Yang | 3291b9c | 2021-07-29 14:46:51 -0700 | [diff] [blame^] | 149 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 150 | sensorInterface->initialize(); |
| 151 | controlInterface->initialize(); |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 152 | |
Jie Yang | 3291b9c | 2021-07-29 14:46:51 -0700 | [diff] [blame^] | 153 | if (isValueMutable) |
| 154 | { |
| 155 | valueMutabilityInterface = |
| 156 | std::make_shared<sdbusplus::asio::dbus_interface>( |
| 157 | conn, sensorInterface->get_object_path(), |
| 158 | valueMutabilityInterfaceName); |
| 159 | valueMutabilityInterface->register_property("Mutable", true); |
| 160 | if (!valueMutabilityInterface->initialize()) |
| 161 | { |
| 162 | std::cerr |
| 163 | << "error initializing sensor value mutability interface\n"; |
| 164 | valueMutabilityInterface = nullptr; |
| 165 | } |
| 166 | } |
| 167 | |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 168 | association = objectServer.add_interface( |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 169 | "/xyz/openbmc_project/sensors/fan_pwm/" + name, association::interface); |
AppaRao Puli | d9d8caf | 2020-02-27 20:56:59 +0530 | [diff] [blame] | 170 | |
| 171 | // PowerSupply sensors should be associated with chassis board path |
| 172 | // and inventory along with psu object. |
| 173 | if (sensorType == "PSU") |
| 174 | { |
| 175 | createInventoryAssoc(conn, association, sensorConfiguration); |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | createAssociation(association, sensorConfiguration); |
| 180 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 181 | } |
| 182 | PwmSensor::~PwmSensor() |
| 183 | { |
| 184 | objectServer.remove_interface(sensorInterface); |
| 185 | objectServer.remove_interface(controlInterface); |
AppaRao Puli | d9d8caf | 2020-02-27 20:56:59 +0530 | [diff] [blame] | 186 | objectServer.remove_interface(association); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void PwmSensor::setValue(uint32_t value) |
| 190 | { |
| 191 | std::ofstream ref(sysPath); |
| 192 | if (!ref.good()) |
| 193 | { |
| 194 | throw std::runtime_error("Bad Write File"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 195 | } |
| 196 | ref << value; |
| 197 | } |
| 198 | |
| 199 | // on success returns pwm, on failure throws except on initialization, where it |
| 200 | // prints an error and returns 0 |
| 201 | uint32_t PwmSensor::getValue(bool errThrow) |
| 202 | { |
| 203 | std::ifstream ref(sysPath); |
| 204 | if (!ref.good()) |
| 205 | { |
| 206 | return -1; |
| 207 | } |
| 208 | std::string line; |
| 209 | if (!std::getline(ref, line)) |
| 210 | { |
| 211 | return -1; |
| 212 | } |
| 213 | try |
| 214 | { |
| 215 | uint32_t value = std::stoi(line); |
| 216 | return value; |
| 217 | } |
Patrick Williams | 26601e8 | 2021-10-06 12:43:25 -0500 | [diff] [blame] | 218 | catch (const std::invalid_argument&) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 219 | { |
| 220 | std::cerr << "Error reading pwm at " << sysPath << "\n"; |
| 221 | // throw if not initial read to be caught by dbus bindings |
| 222 | if (errThrow) |
| 223 | { |
| 224 | throw std::runtime_error("Bad Read"); |
| 225 | } |
| 226 | } |
| 227 | return 0; |
| 228 | } |