blob: bd9c9d6dc2363b7cc563456af7a51fefb1799510 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
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 Tanous8a57ec02020-10-09 12:46:52 -070016#include <PwmSensor.hpp>
17#include <Utils.hpp>
James Feist38fb5982020-05-28 10:09:54 -070018#include <sdbusplus/asio/object_server.hpp>
19
James Feist6714a252018-09-10 15:26:18 -070020#include <fstream>
21#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070022#include <stdexcept>
23#include <string>
James Feist6714a252018-09-10 15:26:18 -070024
Josh Lehan6e6ac0f2020-12-30 22:32:57 -080025static constexpr double sysPwmMax = 255.0;
26static constexpr double psuPwmMax = 100.0;
James Feist3a07f552019-08-27 13:34:54 -070027static constexpr double defaultPwm = 30.0;
Josh Lehan6e6ac0f2020-12-30 22:32:57 -080028static constexpr double targetIfaceMax = sysPwmMax;
James Feist6714a252018-09-10 15:26:18 -070029
Cheng C Yang916360b2019-05-07 18:47:16 +080030PwmSensor::PwmSensor(const std::string& name, const std::string& sysPath,
AppaRao Pulid9d8caf2020-02-27 20:56:59 +053031 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist82bac4c2019-03-11 11:16:53 -070032 sdbusplus::asio::object_server& objectServer,
AppaRao Pulid9d8caf2020-02-27 20:56:59 +053033 const std::string& sensorConfiguration,
34 const std::string& sensorType) :
Brad Bishopfbb44ad2019-11-08 09:42:37 -050035 sysPath(sysPath),
36 objectServer(objectServer), name(name)
James Feist6714a252018-09-10 15:26:18 -070037{
James Feist6714a252018-09-10 15:26:18 -070038 // 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 Wang105a1972020-08-28 19:07:53 +080044 if (sensorType == "PSU")
45 {
46 pwmMax = psuPwmMax;
47 }
48 else
49 {
50 pwmMax = sysPwmMax;
51 }
52
James Feist3a07f552019-08-27 13:34:54 -070053 if (!pwmValue)
54 {
55 // default pwm to non 0
Josh Lehan6e6ac0f2020-12-30 22:32:57 -080056 pwmValue = static_cast<uint32_t>(pwmMax * (defaultPwm / 100.0));
James Feist3a07f552019-08-27 13:34:54 -070057 setValue(pwmValue);
58 }
James Feistb6c0b912019-07-09 12:21:44 -070059 double fValue = 100.0 * (static_cast<double>(pwmValue) / pwmMax);
James Feist6714a252018-09-10 15:26:18 -070060 sensorInterface->register_property(
61 "Value", fValue,
62 [this](const double& req, double& resp) {
Josh Lehan6e6ac0f2020-12-30 22:32:57 -080063 if (!std::isfinite(req))
James Feist6714a252018-09-10 15:26:18 -070064 {
Josh Lehan6e6ac0f2020-12-30 22:32:57 -080065 // 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 Feist6714a252018-09-10 15:26:18 -070072 throw std::runtime_error("Value out of range");
73 return -1;
74 }
Josh Lehan6e6ac0f2020-12-30 22:32:57 -080075
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 Feist46c5c1d2018-11-30 12:04:07 -080082 {
83 return 1;
84 }
Josh Lehan6e6ac0f2020-12-30 22:32:57 -080085 setValue(reqInt);
James Feist6714a252018-09-10 15:26:18 -070086 resp = req;
James Feist46c5c1d2018-11-30 12:04:07 -080087
88 controlInterface->signal_property("Target");
89
James Feist6714a252018-09-10 15:26:18 -070090 return 1;
91 },
92 [this](double& curVal) {
Josh Lehan6e6ac0f2020-12-30 22:32:57 -080093 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 Feist46c5c1d2018-11-30 12:04:07 -080098 {
Josh Lehan6e6ac0f2020-12-30 22:32:57 -080099 double getScaled =
100 100.0 * (static_cast<double>(getInt) / pwmMax);
101 curVal = getScaled;
James Feist46c5c1d2018-11-30 12:04:07 -0800102 controlInterface->signal_property("Target");
103 sensorInterface->signal_property("Value");
104 }
James Feist6714a252018-09-10 15:26:18 -0700105 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));
110
111 controlInterface = objectServer.add_interface(
112 "/xyz/openbmc_project/control/fanpwm/" + name,
113 "xyz.openbmc_project.Control.FanPwm");
114 controlInterface->register_property(
115 "Target", static_cast<uint64_t>(pwmValue),
116 [this](const uint64_t& req, uint64_t& resp) {
Josh Lehan6e6ac0f2020-12-30 22:32:57 -0800117 if (req > static_cast<uint64_t>(targetIfaceMax))
James Feist6714a252018-09-10 15:26:18 -0700118 {
119 throw std::runtime_error("Value out of range");
120 return -1;
121 }
James Feist46c5c1d2018-11-30 12:04:07 -0800122 if (req == resp)
123 {
124 return 1;
125 }
Josh Lehan6e6ac0f2020-12-30 22:32:57 -0800126 auto scaledValue = static_cast<double>(req) / targetIfaceMax;
127 auto roundValue = std::round(scaledValue * pwmMax);
128 setValue(static_cast<uint32_t>(roundValue));
James Feist6714a252018-09-10 15:26:18 -0700129 resp = req;
James Feist46c5c1d2018-11-30 12:04:07 -0800130
131 sensorInterface->signal_property("Value");
132
James Feist6714a252018-09-10 15:26:18 -0700133 return 1;
134 },
135 [this](uint64_t& curVal) {
Josh Lehan6e6ac0f2020-12-30 22:32:57 -0800136 auto getInt = getValue();
137 auto scaledValue = static_cast<double>(getInt) / pwmMax;
138 auto roundValue = std::round(scaledValue * targetIfaceMax);
139 auto value = static_cast<uint64_t>(roundValue);
James Feist46c5c1d2018-11-30 12:04:07 -0800140 if (curVal != value)
141 {
142 curVal = value;
143 controlInterface->signal_property("Target");
144 sensorInterface->signal_property("Value");
145 }
James Feist6714a252018-09-10 15:26:18 -0700146 return curVal;
147 });
148 sensorInterface->initialize();
149 controlInterface->initialize();
James Feist82bac4c2019-03-11 11:16:53 -0700150
151 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -0700152 "/xyz/openbmc_project/sensors/fan_pwm/" + name, association::interface);
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530153
154 // PowerSupply sensors should be associated with chassis board path
155 // and inventory along with psu object.
156 if (sensorType == "PSU")
157 {
158 createInventoryAssoc(conn, association, sensorConfiguration);
159 }
160 else
161 {
162 createAssociation(association, sensorConfiguration);
163 }
James Feist6714a252018-09-10 15:26:18 -0700164}
165PwmSensor::~PwmSensor()
166{
167 objectServer.remove_interface(sensorInterface);
168 objectServer.remove_interface(controlInterface);
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530169 objectServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -0700170}
171
172void PwmSensor::setValue(uint32_t value)
173{
174 std::ofstream ref(sysPath);
175 if (!ref.good())
176 {
177 throw std::runtime_error("Bad Write File");
James Feist6714a252018-09-10 15:26:18 -0700178 }
179 ref << value;
180}
181
182// on success returns pwm, on failure throws except on initialization, where it
183// prints an error and returns 0
184uint32_t PwmSensor::getValue(bool errThrow)
185{
186 std::ifstream ref(sysPath);
187 if (!ref.good())
188 {
189 return -1;
190 }
191 std::string line;
192 if (!std::getline(ref, line))
193 {
194 return -1;
195 }
196 try
197 {
198 uint32_t value = std::stoi(line);
199 return value;
200 }
James Feistb6c0b912019-07-09 12:21:44 -0700201 catch (std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700202 {
203 std::cerr << "Error reading pwm at " << sysPath << "\n";
204 // throw if not initial read to be caught by dbus bindings
205 if (errThrow)
206 {
207 throw std::runtime_error("Bad Read");
208 }
209 }
210 return 0;
211}