blob: eee29efd81bc07e199f7f20397fdac6d68d73174 [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*/
James Feist82bac4c2019-03-11 11:16:53 -070016#include "PwmSensor.hpp"
17
18#include "Utils.hpp"
19
James Feist6714a252018-09-10 15:26:18 -070020#include <fstream>
21#include <iostream>
22#include <sdbusplus/asio/object_server.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070023#include <stdexcept>
24#include <string>
James Feist6714a252018-09-10 15:26:18 -070025
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070026static constexpr size_t pwmMax = 255;
James Feist3a07f552019-08-27 13:34:54 -070027static constexpr double defaultPwm = 30.0;
James Feist6714a252018-09-10 15:26:18 -070028
Cheng C Yang916360b2019-05-07 18:47:16 +080029PwmSensor::PwmSensor(const std::string& name, const std::string& sysPath,
AppaRao Pulid9d8caf2020-02-27 20:56:59 +053030 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist82bac4c2019-03-11 11:16:53 -070031 sdbusplus::asio::object_server& objectServer,
AppaRao Pulid9d8caf2020-02-27 20:56:59 +053032 const std::string& sensorConfiguration,
33 const std::string& sensorType) :
Brad Bishopfbb44ad2019-11-08 09:42:37 -050034 sysPath(sysPath),
35 objectServer(objectServer), name(name)
James Feist6714a252018-09-10 15:26:18 -070036{
James Feist6714a252018-09-10 15:26:18 -070037 // add interface under sensor and Control.FanPwm as Control is used
38 // in obmc project, also add sensor so it can be viewed as a sensor
39 sensorInterface = objectServer.add_interface(
40 "/xyz/openbmc_project/sensors/fan_pwm/" + name,
41 "xyz.openbmc_project.Sensor.Value");
42 uint32_t pwmValue = getValue(false);
James Feist3a07f552019-08-27 13:34:54 -070043 if (!pwmValue)
44 {
45 // default pwm to non 0
46 pwmValue = static_cast<uint32_t>(pwmMax * (defaultPwm / 100));
47 setValue(pwmValue);
48 }
James Feistb6c0b912019-07-09 12:21:44 -070049 double fValue = 100.0 * (static_cast<double>(pwmValue) / pwmMax);
James Feist6714a252018-09-10 15:26:18 -070050 sensorInterface->register_property(
51 "Value", fValue,
52 [this](const double& req, double& resp) {
53 if (req > 100 || req < 0)
54 {
55 throw std::runtime_error("Value out of range");
56 return -1;
57 }
James Feist46c5c1d2018-11-30 12:04:07 -080058 if (req == resp)
59 {
60 return 1;
61 }
James Feist6714a252018-09-10 15:26:18 -070062 double value = (req / 100) * pwmMax;
63 setValue(static_cast<int>(value));
64 resp = req;
James Feist46c5c1d2018-11-30 12:04:07 -080065
66 controlInterface->signal_property("Target");
67
James Feist6714a252018-09-10 15:26:18 -070068 return 1;
69 },
70 [this](double& curVal) {
James Feistb6c0b912019-07-09 12:21:44 -070071 double value = 100.0 * (static_cast<double>(getValue()) / pwmMax);
James Feist46c5c1d2018-11-30 12:04:07 -080072 if (curVal != value)
73 {
74 curVal = value;
75 controlInterface->signal_property("Target");
76 sensorInterface->signal_property("Value");
77 }
78
James Feist6714a252018-09-10 15:26:18 -070079 return curVal;
80 });
81 // pwm sensor interface is in percent
82 sensorInterface->register_property("MaxValue", static_cast<int64_t>(100));
83 sensorInterface->register_property("MinValue", static_cast<int64_t>(0));
84
85 controlInterface = objectServer.add_interface(
86 "/xyz/openbmc_project/control/fanpwm/" + name,
87 "xyz.openbmc_project.Control.FanPwm");
88 controlInterface->register_property(
89 "Target", static_cast<uint64_t>(pwmValue),
90 [this](const uint64_t& req, uint64_t& resp) {
James Feistb6c0b912019-07-09 12:21:44 -070091 if (req > pwmMax)
James Feist6714a252018-09-10 15:26:18 -070092 {
93 throw std::runtime_error("Value out of range");
94 return -1;
95 }
James Feist46c5c1d2018-11-30 12:04:07 -080096 if (req == resp)
97 {
98 return 1;
99 }
James Feist6714a252018-09-10 15:26:18 -0700100 setValue(req);
101 resp = req;
James Feist46c5c1d2018-11-30 12:04:07 -0800102
103 sensorInterface->signal_property("Value");
104
James Feist6714a252018-09-10 15:26:18 -0700105 return 1;
106 },
107 [this](uint64_t& curVal) {
James Feist46c5c1d2018-11-30 12:04:07 -0800108 uint64_t value = getValue();
109 if (curVal != value)
110 {
111 curVal = value;
112 controlInterface->signal_property("Target");
113 sensorInterface->signal_property("Value");
114 }
115
James Feist6714a252018-09-10 15:26:18 -0700116 return curVal;
117 });
118 sensorInterface->initialize();
119 controlInterface->initialize();
James Feist82bac4c2019-03-11 11:16:53 -0700120
121 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -0700122 "/xyz/openbmc_project/sensors/fan_pwm/" + name, association::interface);
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530123
124 // PowerSupply sensors should be associated with chassis board path
125 // and inventory along with psu object.
126 if (sensorType == "PSU")
127 {
128 createInventoryAssoc(conn, association, sensorConfiguration);
129 }
130 else
131 {
132 createAssociation(association, sensorConfiguration);
133 }
James Feist6714a252018-09-10 15:26:18 -0700134}
135PwmSensor::~PwmSensor()
136{
137 objectServer.remove_interface(sensorInterface);
138 objectServer.remove_interface(controlInterface);
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530139 objectServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -0700140}
141
142void PwmSensor::setValue(uint32_t value)
143{
144 std::ofstream ref(sysPath);
145 if (!ref.good())
146 {
147 throw std::runtime_error("Bad Write File");
James Feist6714a252018-09-10 15:26:18 -0700148 }
149 ref << value;
150}
151
152// on success returns pwm, on failure throws except on initialization, where it
153// prints an error and returns 0
154uint32_t PwmSensor::getValue(bool errThrow)
155{
156 std::ifstream ref(sysPath);
157 if (!ref.good())
158 {
159 return -1;
160 }
161 std::string line;
162 if (!std::getline(ref, line))
163 {
164 return -1;
165 }
166 try
167 {
168 uint32_t value = std::stoi(line);
169 return value;
170 }
James Feistb6c0b912019-07-09 12:21:44 -0700171 catch (std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700172 {
173 std::cerr << "Error reading pwm at " << sysPath << "\n";
174 // throw if not initial read to be caught by dbus bindings
175 if (errThrow)
176 {
177 throw std::runtime_error("Bad Read");
178 }
179 }
180 return 0;
181}