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