blob: 3e5c8a1ad9fbd3758231d35afe56c2d11571d621 [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 Feist38fb5982020-05-28 10:09:54 -070020#include <sdbusplus/asio/object_server.hpp>
21
James Feist6714a252018-09-10 15:26:18 -070022#include <fstream>
23#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070024#include <stdexcept>
25#include <string>
James Feist6714a252018-09-10 15:26:18 -070026
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070027static constexpr size_t pwmMax = 255;
James Feist3a07f552019-08-27 13:34:54 -070028static constexpr double defaultPwm = 30.0;
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);
James Feist3a07f552019-08-27 13:34:54 -070044 if (!pwmValue)
45 {
46 // default pwm to non 0
47 pwmValue = static_cast<uint32_t>(pwmMax * (defaultPwm / 100));
48 setValue(pwmValue);
49 }
James Feistb6c0b912019-07-09 12:21:44 -070050 double fValue = 100.0 * (static_cast<double>(pwmValue) / pwmMax);
James Feist6714a252018-09-10 15:26:18 -070051 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 Feist46c5c1d2018-11-30 12:04:07 -080059 if (req == resp)
60 {
61 return 1;
62 }
James Feist6714a252018-09-10 15:26:18 -070063 double value = (req / 100) * pwmMax;
64 setValue(static_cast<int>(value));
65 resp = req;
James Feist46c5c1d2018-11-30 12:04:07 -080066
67 controlInterface->signal_property("Target");
68
James Feist6714a252018-09-10 15:26:18 -070069 return 1;
70 },
71 [this](double& curVal) {
James Feistb6c0b912019-07-09 12:21:44 -070072 double value = 100.0 * (static_cast<double>(getValue()) / pwmMax);
James Feist46c5c1d2018-11-30 12:04:07 -080073 if (curVal != value)
74 {
75 curVal = value;
76 controlInterface->signal_property("Target");
77 sensorInterface->signal_property("Value");
78 }
79
James Feist6714a252018-09-10 15:26:18 -070080 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 Feistb6c0b912019-07-09 12:21:44 -070092 if (req > pwmMax)
James Feist6714a252018-09-10 15:26:18 -070093 {
94 throw std::runtime_error("Value out of range");
95 return -1;
96 }
James Feist46c5c1d2018-11-30 12:04:07 -080097 if (req == resp)
98 {
99 return 1;
100 }
James Feist6714a252018-09-10 15:26:18 -0700101 setValue(req);
102 resp = req;
James Feist46c5c1d2018-11-30 12:04:07 -0800103
104 sensorInterface->signal_property("Value");
105
James Feist6714a252018-09-10 15:26:18 -0700106 return 1;
107 },
108 [this](uint64_t& curVal) {
James Feist46c5c1d2018-11-30 12:04:07 -0800109 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 Feist6714a252018-09-10 15:26:18 -0700117 return curVal;
118 });
119 sensorInterface->initialize();
120 controlInterface->initialize();
James Feist82bac4c2019-03-11 11:16:53 -0700121
122 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -0700123 "/xyz/openbmc_project/sensors/fan_pwm/" + name, association::interface);
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530124
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 Feist6714a252018-09-10 15:26:18 -0700135}
136PwmSensor::~PwmSensor()
137{
138 objectServer.remove_interface(sensorInterface);
139 objectServer.remove_interface(controlInterface);
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530140 objectServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -0700141}
142
143void 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 Feist6714a252018-09-10 15:26:18 -0700149 }
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
155uint32_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 Feistb6c0b912019-07-09 12:21:44 -0700172 catch (std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700173 {
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}