blob: 8510563da0110530780b958e8b9ca33c9881781a [file] [log] [blame]
James Feist139cb572018-09-10 15:26:18 -07001/*
2// Copyright (c) 2017 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*/
16
17#include <unistd.h>
18
19#include <HwmonTempSensor.hpp>
20#include <boost/algorithm/string/predicate.hpp>
21#include <boost/algorithm/string/replace.hpp>
22#include <boost/date_time/posix_time/posix_time.hpp>
23#include <iostream>
24#include <limits>
25#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27#include <string>
28
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070029static constexpr unsigned int sensorPollMs = 500;
30static constexpr unsigned int sensorScaleFactor = 1000;
31static constexpr size_t warnAfterErrorCount = 10;
James Feist139cb572018-09-10 15:26:18 -070032
33HwmonTempSensor::HwmonTempSensor(
34 const std::string &path, const std::string &objectType,
35 sdbusplus::asio::object_server &objectServer,
36 std::shared_ptr<sdbusplus::asio::connection> &conn,
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070037 boost::asio::io_service &io, const std::string &sensorName,
James Feist139cb572018-09-10 15:26:18 -070038 std::vector<thresholds::Threshold> &&_thresholds,
39 const std::string &sensorConfiguration) :
James Feist39132a62018-10-31 12:53:20 -070040 Sensor(boost::replace_all_copy(sensorName, " ", "_"), path,
41 std::move(_thresholds)),
42 objectType(objectType), configuration(sensorConfiguration),
43 objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)),
44 waitTimer(io), errCount(0),
James Feist139cb572018-09-10 15:26:18 -070045 // todo, get these from config
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070046 maxValue(127), minValue(-128)
James Feist139cb572018-09-10 15:26:18 -070047{
James Feistaf79dd32018-09-12 12:54:15 -070048 sensorInterface = objectServer.add_interface(
49 "/xyz/openbmc_project/sensors/temperature/" + name,
50 "xyz.openbmc_project.Sensor.Value");
51
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070052 if (thresholds::hasWarningInterface(thresholds))
James Feist139cb572018-09-10 15:26:18 -070053 {
James Feistaf79dd32018-09-12 12:54:15 -070054 thresholdInterfaceWarning = objectServer.add_interface(
James Feist139cb572018-09-10 15:26:18 -070055 "/xyz/openbmc_project/sensors/temperature/" + name,
56 "xyz.openbmc_project.Sensor.Threshold.Warning");
57 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070058 if (thresholds::hasCriticalInterface(thresholds))
James Feist139cb572018-09-10 15:26:18 -070059 {
James Feistaf79dd32018-09-12 12:54:15 -070060 thresholdInterfaceCritical = objectServer.add_interface(
James Feist139cb572018-09-10 15:26:18 -070061 "/xyz/openbmc_project/sensors/temperature/" + name,
62 "xyz.openbmc_project.Sensor.Threshold.Critical");
63 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070064 setInitialProperties(conn);
65 setupRead();
James Feist139cb572018-09-10 15:26:18 -070066}
67
68HwmonTempSensor::~HwmonTempSensor()
69{
70 // close the input dev to cancel async operations
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070071 inputDev.close();
72 waitTimer.cancel();
James Feistaf79dd32018-09-12 12:54:15 -070073 objServer.remove_interface(thresholdInterfaceWarning);
74 objServer.remove_interface(thresholdInterfaceCritical);
75 objServer.remove_interface(sensorInterface);
James Feist139cb572018-09-10 15:26:18 -070076}
77
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070078void HwmonTempSensor::setupRead(void)
James Feist139cb572018-09-10 15:26:18 -070079{
80 boost::asio::async_read_until(
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070081 inputDev, readBuf, '\n',
James Feist139cb572018-09-10 15:26:18 -070082 [&](const boost::system::error_code &ec,
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070083 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist139cb572018-09-10 15:26:18 -070084}
85
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070086void HwmonTempSensor::handleResponse(const boost::system::error_code &err)
James Feist139cb572018-09-10 15:26:18 -070087{
88 if (err == boost::system::errc::bad_file_descriptor)
89 {
90 return; // we're being destroyed
91 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070092 std::istream responseStream(&readBuf);
James Feist139cb572018-09-10 15:26:18 -070093 if (!err)
94 {
95 std::string response;
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070096 std::getline(responseStream, response);
James Feist139cb572018-09-10 15:26:18 -070097 try
98 {
99 float nvalue = std::stof(response);
100
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700101 nvalue /= sensorScaleFactor;
James Feist139cb572018-09-10 15:26:18 -0700102 if (nvalue != value)
103 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700104 updateValue(nvalue);
James Feist139cb572018-09-10 15:26:18 -0700105 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700106 errCount = 0;
James Feist139cb572018-09-10 15:26:18 -0700107 }
108 catch (const std::invalid_argument &)
109 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700110 errCount++;
James Feist139cb572018-09-10 15:26:18 -0700111 }
112 }
113 else
114 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700115 errCount++;
James Feist139cb572018-09-10 15:26:18 -0700116 }
James Feist39132a62018-10-31 12:53:20 -0700117
118 // only print once
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700119 if (errCount == warnAfterErrorCount)
James Feist139cb572018-09-10 15:26:18 -0700120 {
James Feist39132a62018-10-31 12:53:20 -0700121 std::cerr << "Failure to read sensor " << name << " at " << path
122 << " ec:" << err << "\n";
123 }
124
125 if (errCount >= warnAfterErrorCount)
126 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700127 updateValue(0);
James Feist139cb572018-09-10 15:26:18 -0700128 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700129 responseStream.clear();
130 inputDev.close();
James Feist139cb572018-09-10 15:26:18 -0700131 int fd = open(path.c_str(), O_RDONLY);
132 if (fd <= 0)
133 {
134 return; // we're no longer valid
135 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700136 inputDev.assign(fd);
137 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
James Feist139cb572018-09-10 15:26:18 -0700138 ;
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700139 waitTimer.async_wait([&](const boost::system::error_code &ec) {
James Feist139cb572018-09-10 15:26:18 -0700140 if (ec == boost::asio::error::operation_aborted)
141 {
142 return; // we're being canceled
143 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700144 setupRead();
James Feist139cb572018-09-10 15:26:18 -0700145 });
146}
147
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700148void HwmonTempSensor::checkThresholds(void)
James Feist139cb572018-09-10 15:26:18 -0700149{
James Feistaf79dd32018-09-12 12:54:15 -0700150 thresholds::checkThresholds(this);
James Feist139cb572018-09-10 15:26:18 -0700151}
152
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700153void HwmonTempSensor::updateValue(const double &newValue)
James Feist139cb572018-09-10 15:26:18 -0700154{
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700155 sensorInterface->set_property("Value", newValue);
156 value = newValue;
157 checkThresholds();
James Feist139cb572018-09-10 15:26:18 -0700158}
159
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700160void HwmonTempSensor::setInitialProperties(
James Feist139cb572018-09-10 15:26:18 -0700161 std::shared_ptr<sdbusplus::asio::connection> &conn)
162{
163 // todo, get max and min from configuration
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700164 sensorInterface->register_property("MaxValue", maxValue);
165 sensorInterface->register_property("MinValue", minValue);
James Feistaf79dd32018-09-12 12:54:15 -0700166 sensorInterface->register_property("Value", value);
James Feist139cb572018-09-10 15:26:18 -0700167
168 for (auto &threshold : thresholds)
169 {
170 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
171 std::string level;
172 std::string alarm;
173 if (threshold.level == thresholds::Level::CRITICAL)
174 {
James Feistaf79dd32018-09-12 12:54:15 -0700175 iface = thresholdInterfaceCritical;
James Feist139cb572018-09-10 15:26:18 -0700176 if (threshold.direction == thresholds::Direction::HIGH)
177 {
178 level = "CriticalHigh";
179 alarm = "CriticalAlarmHigh";
180 }
181 else
182 {
183 level = "CriticalLow";
184 alarm = "CriticalAlarmLow";
185 }
186 }
187 else if (threshold.level == thresholds::Level::WARNING)
188 {
James Feistaf79dd32018-09-12 12:54:15 -0700189 iface = thresholdInterfaceWarning;
James Feist139cb572018-09-10 15:26:18 -0700190 if (threshold.direction == thresholds::Direction::HIGH)
191 {
192 level = "WarningHigh";
193 alarm = "WarningAlarmHigh";
194 }
195 else
196 {
197 level = "WarningLow";
198 alarm = "WarningAlarmLow";
199 }
200 }
201 else
202 {
203 std::cerr << "Unknown threshold level" << threshold.level << "\n";
204 continue;
205 }
206 if (!iface)
207 {
208 std::cout << "trying to set uninitialized interface\n";
209 continue;
210 }
211 iface->register_property(
212 level, threshold.value,
213 [&](const double &request, double &oldValue) {
214 oldValue = request; // todo, just let the config do this?
215 threshold.value = request;
216 thresholds::persistThreshold(configuration, objectType,
217 threshold, conn);
218 return 1;
219 });
220 iface->register_property(alarm, false);
221 }
James Feistaf79dd32018-09-12 12:54:15 -0700222 if (!sensorInterface->initialize())
James Feist139cb572018-09-10 15:26:18 -0700223 {
224 std::cerr << "error initializing value interface\n";
225 }
James Feistaf79dd32018-09-12 12:54:15 -0700226 if (thresholdInterfaceWarning && !thresholdInterfaceWarning->initialize())
James Feist139cb572018-09-10 15:26:18 -0700227 {
228 std::cerr << "error initializing warning threshold interface\n";
229 }
230
James Feistaf79dd32018-09-12 12:54:15 -0700231 if (thresholdInterfaceCritical && !thresholdInterfaceCritical->initialize())
James Feist139cb572018-09-10 15:26:18 -0700232 {
233 std::cerr << "error initializing critical threshold interface\n";
234 }
235}