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