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