blob: 482448930703007f036467c611171661390697ca [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
Kuiying Wang105a1972020-08-28 19:07:53 +080027static constexpr size_t sysPwmMax = 255;
28static constexpr size_t psuPwmMax = 100;
James Feist3a07f552019-08-27 13:34:54 -070029static constexpr double defaultPwm = 30.0;
James Feisted3e9462020-09-24 15:31:03 -070030static constexpr size_t targetIfaceMax = 255;
James Feist6714a252018-09-10 15:26:18 -070031
Cheng C Yang916360b2019-05-07 18:47:16 +080032PwmSensor::PwmSensor(const std::string& name, const std::string& sysPath,
AppaRao Pulid9d8caf2020-02-27 20:56:59 +053033 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist82bac4c2019-03-11 11:16:53 -070034 sdbusplus::asio::object_server& objectServer,
AppaRao Pulid9d8caf2020-02-27 20:56:59 +053035 const std::string& sensorConfiguration,
36 const std::string& sensorType) :
Brad Bishopfbb44ad2019-11-08 09:42:37 -050037 sysPath(sysPath),
38 objectServer(objectServer), name(name)
James Feist6714a252018-09-10 15:26:18 -070039{
James Feist6714a252018-09-10 15:26:18 -070040 // add interface under sensor and Control.FanPwm as Control is used
41 // in obmc project, also add sensor so it can be viewed as a sensor
42 sensorInterface = objectServer.add_interface(
43 "/xyz/openbmc_project/sensors/fan_pwm/" + name,
44 "xyz.openbmc_project.Sensor.Value");
45 uint32_t pwmValue = getValue(false);
Kuiying Wang105a1972020-08-28 19:07:53 +080046 if (sensorType == "PSU")
47 {
48 pwmMax = psuPwmMax;
49 }
50 else
51 {
52 pwmMax = sysPwmMax;
53 }
54
James Feist3a07f552019-08-27 13:34:54 -070055 if (!pwmValue)
56 {
57 // default pwm to non 0
58 pwmValue = static_cast<uint32_t>(pwmMax * (defaultPwm / 100));
59 setValue(pwmValue);
60 }
James Feistb6c0b912019-07-09 12:21:44 -070061 double fValue = 100.0 * (static_cast<double>(pwmValue) / pwmMax);
James Feist6714a252018-09-10 15:26:18 -070062 sensorInterface->register_property(
63 "Value", fValue,
64 [this](const double& req, double& resp) {
65 if (req > 100 || req < 0)
66 {
67 throw std::runtime_error("Value out of range");
68 return -1;
69 }
James Feist46c5c1d2018-11-30 12:04:07 -080070 if (req == resp)
71 {
72 return 1;
73 }
James Feist6714a252018-09-10 15:26:18 -070074 double value = (req / 100) * pwmMax;
75 setValue(static_cast<int>(value));
76 resp = req;
James Feist46c5c1d2018-11-30 12:04:07 -080077
78 controlInterface->signal_property("Target");
79
James Feist6714a252018-09-10 15:26:18 -070080 return 1;
81 },
82 [this](double& curVal) {
James Feistb6c0b912019-07-09 12:21:44 -070083 double value = 100.0 * (static_cast<double>(getValue()) / pwmMax);
James Feist46c5c1d2018-11-30 12:04:07 -080084 if (curVal != value)
85 {
86 curVal = value;
87 controlInterface->signal_property("Target");
88 sensorInterface->signal_property("Value");
89 }
90
James Feist6714a252018-09-10 15:26:18 -070091 return curVal;
92 });
93 // pwm sensor interface is in percent
94 sensorInterface->register_property("MaxValue", static_cast<int64_t>(100));
95 sensorInterface->register_property("MinValue", static_cast<int64_t>(0));
96
97 controlInterface = objectServer.add_interface(
98 "/xyz/openbmc_project/control/fanpwm/" + name,
99 "xyz.openbmc_project.Control.FanPwm");
100 controlInterface->register_property(
101 "Target", static_cast<uint64_t>(pwmValue),
102 [this](const uint64_t& req, uint64_t& resp) {
James Feisted3e9462020-09-24 15:31:03 -0700103 if (req > targetIfaceMax)
James Feist6714a252018-09-10 15:26:18 -0700104 {
105 throw std::runtime_error("Value out of range");
106 return -1;
107 }
James Feist46c5c1d2018-11-30 12:04:07 -0800108 if (req == resp)
109 {
110 return 1;
111 }
James Feisted3e9462020-09-24 15:31:03 -0700112 setValue(
113 std::round(pwmMax * static_cast<double>(req) / targetIfaceMax));
James Feist6714a252018-09-10 15:26:18 -0700114 resp = req;
James Feist46c5c1d2018-11-30 12:04:07 -0800115
116 sensorInterface->signal_property("Value");
117
James Feist6714a252018-09-10 15:26:18 -0700118 return 1;
119 },
120 [this](uint64_t& curVal) {
James Feist46c5c1d2018-11-30 12:04:07 -0800121 uint64_t value = getValue();
James Feisted3e9462020-09-24 15:31:03 -0700122 value = static_cast<uint64_t>(std::round(
123 (static_cast<double>(value) / pwmMax) * targetIfaceMax));
James Feist46c5c1d2018-11-30 12:04:07 -0800124 if (curVal != value)
125 {
126 curVal = value;
127 controlInterface->signal_property("Target");
128 sensorInterface->signal_property("Value");
129 }
130
James Feist6714a252018-09-10 15:26:18 -0700131 return curVal;
132 });
133 sensorInterface->initialize();
134 controlInterface->initialize();
James Feist82bac4c2019-03-11 11:16:53 -0700135
136 association = objectServer.add_interface(
James Feist2adc95c2019-09-30 14:55:28 -0700137 "/xyz/openbmc_project/sensors/fan_pwm/" + name, association::interface);
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530138
139 // PowerSupply sensors should be associated with chassis board path
140 // and inventory along with psu object.
141 if (sensorType == "PSU")
142 {
143 createInventoryAssoc(conn, association, sensorConfiguration);
144 }
145 else
146 {
147 createAssociation(association, sensorConfiguration);
148 }
James Feist6714a252018-09-10 15:26:18 -0700149}
150PwmSensor::~PwmSensor()
151{
152 objectServer.remove_interface(sensorInterface);
153 objectServer.remove_interface(controlInterface);
AppaRao Pulid9d8caf2020-02-27 20:56:59 +0530154 objectServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -0700155}
156
157void PwmSensor::setValue(uint32_t value)
158{
159 std::ofstream ref(sysPath);
160 if (!ref.good())
161 {
162 throw std::runtime_error("Bad Write File");
James Feist6714a252018-09-10 15:26:18 -0700163 }
164 ref << value;
165}
166
167// on success returns pwm, on failure throws except on initialization, where it
168// prints an error and returns 0
169uint32_t PwmSensor::getValue(bool errThrow)
170{
171 std::ifstream ref(sysPath);
172 if (!ref.good())
173 {
174 return -1;
175 }
176 std::string line;
177 if (!std::getline(ref, line))
178 {
179 return -1;
180 }
181 try
182 {
183 uint32_t value = std::stoi(line);
184 return value;
185 }
James Feistb6c0b912019-07-09 12:21:44 -0700186 catch (std::invalid_argument&)
James Feist6714a252018-09-10 15:26:18 -0700187 {
188 std::cerr << "Error reading pwm at " << sysPath << "\n";
189 // throw if not initial read to be caught by dbus bindings
190 if (errThrow)
191 {
192 throw std::runtime_error("Bad Read");
193 }
194 }
195 return 0;
196}