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 | */ |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 16 | #include "PwmSensor.hpp" |
| 17 | |
| 18 | #include "Utils.hpp" |
| 19 | |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 20 | #include <sdbusplus/asio/object_server.hpp> |
| 21 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 22 | #include <fstream> |
| 23 | #include <iostream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 24 | #include <stdexcept> |
| 25 | #include <string> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 26 | |
Kuiying Wang | 105a197 | 2020-08-28 19:07:53 +0800 | [diff] [blame] | 27 | static constexpr size_t sysPwmMax = 255; |
| 28 | static constexpr size_t psuPwmMax = 100; |
James Feist | 3a07f55 | 2019-08-27 13:34:54 -0700 | [diff] [blame] | 29 | static constexpr double defaultPwm = 30.0; |
James Feist | ed3e946 | 2020-09-24 15:31:03 -0700 | [diff] [blame] | 30 | static constexpr size_t targetIfaceMax = 255; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 31 | |
Cheng C Yang | 916360b | 2019-05-07 18:47:16 +0800 | [diff] [blame] | 32 | PwmSensor::PwmSensor(const std::string& name, const std::string& sysPath, |
AppaRao Puli | d9d8caf | 2020-02-27 20:56:59 +0530 | [diff] [blame] | 33 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 34 | sdbusplus::asio::object_server& objectServer, |
AppaRao Puli | d9d8caf | 2020-02-27 20:56:59 +0530 | [diff] [blame] | 35 | const std::string& sensorConfiguration, |
| 36 | const std::string& sensorType) : |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 37 | sysPath(sysPath), |
| 38 | objectServer(objectServer), name(name) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 39 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 40 | // add interface under sensor and Control.FanPwm as Control is used |
| 41 | // in obmc project, also add sensor so it can be viewed as a sensor |
| 42 | sensorInterface = objectServer.add_interface( |
| 43 | "/xyz/openbmc_project/sensors/fan_pwm/" + name, |
| 44 | "xyz.openbmc_project.Sensor.Value"); |
| 45 | uint32_t pwmValue = getValue(false); |
Kuiying Wang | 105a197 | 2020-08-28 19:07:53 +0800 | [diff] [blame] | 46 | if (sensorType == "PSU") |
| 47 | { |
| 48 | pwmMax = psuPwmMax; |
| 49 | } |
| 50 | else |
| 51 | { |
| 52 | pwmMax = sysPwmMax; |
| 53 | } |
| 54 | |
James Feist | 3a07f55 | 2019-08-27 13:34:54 -0700 | [diff] [blame] | 55 | if (!pwmValue) |
| 56 | { |
| 57 | // default pwm to non 0 |
| 58 | pwmValue = static_cast<uint32_t>(pwmMax * (defaultPwm / 100)); |
| 59 | setValue(pwmValue); |
| 60 | } |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 61 | double fValue = 100.0 * (static_cast<double>(pwmValue) / pwmMax); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 62 | sensorInterface->register_property( |
| 63 | "Value", fValue, |
| 64 | [this](const double& req, double& resp) { |
| 65 | if (req > 100 || req < 0) |
| 66 | { |
| 67 | throw std::runtime_error("Value out of range"); |
| 68 | return -1; |
| 69 | } |
James Feist | 46c5c1d | 2018-11-30 12:04:07 -0800 | [diff] [blame] | 70 | if (req == resp) |
| 71 | { |
| 72 | return 1; |
| 73 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 74 | double value = (req / 100) * pwmMax; |
| 75 | setValue(static_cast<int>(value)); |
| 76 | resp = req; |
James Feist | 46c5c1d | 2018-11-30 12:04:07 -0800 | [diff] [blame] | 77 | |
| 78 | controlInterface->signal_property("Target"); |
| 79 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 80 | return 1; |
| 81 | }, |
| 82 | [this](double& curVal) { |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 83 | double value = 100.0 * (static_cast<double>(getValue()) / pwmMax); |
James Feist | 46c5c1d | 2018-11-30 12:04:07 -0800 | [diff] [blame] | 84 | if (curVal != value) |
| 85 | { |
| 86 | curVal = value; |
| 87 | controlInterface->signal_property("Target"); |
| 88 | sensorInterface->signal_property("Value"); |
| 89 | } |
| 90 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 91 | return curVal; |
| 92 | }); |
| 93 | // pwm sensor interface is in percent |
| 94 | sensorInterface->register_property("MaxValue", static_cast<int64_t>(100)); |
| 95 | sensorInterface->register_property("MinValue", static_cast<int64_t>(0)); |
| 96 | |
| 97 | controlInterface = objectServer.add_interface( |
| 98 | "/xyz/openbmc_project/control/fanpwm/" + name, |
| 99 | "xyz.openbmc_project.Control.FanPwm"); |
| 100 | controlInterface->register_property( |
| 101 | "Target", static_cast<uint64_t>(pwmValue), |
| 102 | [this](const uint64_t& req, uint64_t& resp) { |
James Feist | ed3e946 | 2020-09-24 15:31:03 -0700 | [diff] [blame] | 103 | if (req > targetIfaceMax) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 104 | { |
| 105 | throw std::runtime_error("Value out of range"); |
| 106 | return -1; |
| 107 | } |
James Feist | 46c5c1d | 2018-11-30 12:04:07 -0800 | [diff] [blame] | 108 | if (req == resp) |
| 109 | { |
| 110 | return 1; |
| 111 | } |
James Feist | ed3e946 | 2020-09-24 15:31:03 -0700 | [diff] [blame] | 112 | setValue( |
| 113 | std::round(pwmMax * static_cast<double>(req) / targetIfaceMax)); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 114 | resp = req; |
James Feist | 46c5c1d | 2018-11-30 12:04:07 -0800 | [diff] [blame] | 115 | |
| 116 | sensorInterface->signal_property("Value"); |
| 117 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 118 | return 1; |
| 119 | }, |
| 120 | [this](uint64_t& curVal) { |
James Feist | 46c5c1d | 2018-11-30 12:04:07 -0800 | [diff] [blame] | 121 | uint64_t value = getValue(); |
James Feist | ed3e946 | 2020-09-24 15:31:03 -0700 | [diff] [blame] | 122 | value = static_cast<uint64_t>(std::round( |
| 123 | (static_cast<double>(value) / pwmMax) * targetIfaceMax)); |
James Feist | 46c5c1d | 2018-11-30 12:04:07 -0800 | [diff] [blame] | 124 | if (curVal != value) |
| 125 | { |
| 126 | curVal = value; |
| 127 | controlInterface->signal_property("Target"); |
| 128 | sensorInterface->signal_property("Value"); |
| 129 | } |
| 130 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 131 | return curVal; |
| 132 | }); |
| 133 | sensorInterface->initialize(); |
| 134 | controlInterface->initialize(); |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 135 | |
| 136 | association = objectServer.add_interface( |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 137 | "/xyz/openbmc_project/sensors/fan_pwm/" + name, association::interface); |
AppaRao Puli | d9d8caf | 2020-02-27 20:56:59 +0530 | [diff] [blame] | 138 | |
| 139 | // PowerSupply sensors should be associated with chassis board path |
| 140 | // and inventory along with psu object. |
| 141 | if (sensorType == "PSU") |
| 142 | { |
| 143 | createInventoryAssoc(conn, association, sensorConfiguration); |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | createAssociation(association, sensorConfiguration); |
| 148 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 149 | } |
| 150 | PwmSensor::~PwmSensor() |
| 151 | { |
| 152 | objectServer.remove_interface(sensorInterface); |
| 153 | objectServer.remove_interface(controlInterface); |
AppaRao Puli | d9d8caf | 2020-02-27 20:56:59 +0530 | [diff] [blame] | 154 | objectServer.remove_interface(association); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void PwmSensor::setValue(uint32_t value) |
| 158 | { |
| 159 | std::ofstream ref(sysPath); |
| 160 | if (!ref.good()) |
| 161 | { |
| 162 | throw std::runtime_error("Bad Write File"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 163 | } |
| 164 | ref << value; |
| 165 | } |
| 166 | |
| 167 | // on success returns pwm, on failure throws except on initialization, where it |
| 168 | // prints an error and returns 0 |
| 169 | uint32_t PwmSensor::getValue(bool errThrow) |
| 170 | { |
| 171 | std::ifstream ref(sysPath); |
| 172 | if (!ref.good()) |
| 173 | { |
| 174 | return -1; |
| 175 | } |
| 176 | std::string line; |
| 177 | if (!std::getline(ref, line)) |
| 178 | { |
| 179 | return -1; |
| 180 | } |
| 181 | try |
| 182 | { |
| 183 | uint32_t value = std::stoi(line); |
| 184 | return value; |
| 185 | } |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 186 | catch (std::invalid_argument&) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 187 | { |
| 188 | std::cerr << "Error reading pwm at " << sysPath << "\n"; |
| 189 | // throw if not initial read to be caught by dbus bindings |
| 190 | if (errThrow) |
| 191 | { |
| 192 | throw std::runtime_error("Bad Read"); |
| 193 | } |
| 194 | } |
| 195 | return 0; |
| 196 | } |