blob: 1d58f07305d6073d92f4c0bc3b391d05b961e4b7 [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 Feistaf79dd32018-09-12 12:54:15 -070040 Sensor(),
41 path(path), objectType(objectType), configuration(sensorConfiguration),
James Feist139cb572018-09-10 15:26:18 -070042 objServer(objectServer),
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070043 name(boost::replace_all_copy(sensorName, " ", "_")),
44 inputDev(io, open(path.c_str(), O_RDONLY)), 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 thresholds = std::move(_thresholds);
49 sensorInterface = objectServer.add_interface(
50 "/xyz/openbmc_project/sensors/temperature/" + name,
51 "xyz.openbmc_project.Sensor.Value");
52
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070053 if (thresholds::hasWarningInterface(thresholds))
James Feist139cb572018-09-10 15:26:18 -070054 {
James Feistaf79dd32018-09-12 12:54:15 -070055 thresholdInterfaceWarning = objectServer.add_interface(
James Feist139cb572018-09-10 15:26:18 -070056 "/xyz/openbmc_project/sensors/temperature/" + name,
57 "xyz.openbmc_project.Sensor.Threshold.Warning");
58 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070059 if (thresholds::hasCriticalInterface(thresholds))
James Feist139cb572018-09-10 15:26:18 -070060 {
James Feistaf79dd32018-09-12 12:54:15 -070061 thresholdInterfaceCritical = objectServer.add_interface(
James Feist139cb572018-09-10 15:26:18 -070062 "/xyz/openbmc_project/sensors/temperature/" + name,
63 "xyz.openbmc_project.Sensor.Threshold.Critical");
64 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070065 setInitialProperties(conn);
66 setupRead();
James Feist139cb572018-09-10 15:26:18 -070067}
68
69HwmonTempSensor::~HwmonTempSensor()
70{
71 // close the input dev to cancel async operations
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070072 inputDev.close();
73 waitTimer.cancel();
James Feistaf79dd32018-09-12 12:54:15 -070074 objServer.remove_interface(thresholdInterfaceWarning);
75 objServer.remove_interface(thresholdInterfaceCritical);
76 objServer.remove_interface(sensorInterface);
James Feist139cb572018-09-10 15:26:18 -070077}
78
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070079void HwmonTempSensor::setupRead(void)
James Feist139cb572018-09-10 15:26:18 -070080{
81 boost::asio::async_read_until(
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070082 inputDev, readBuf, '\n',
James Feist139cb572018-09-10 15:26:18 -070083 [&](const boost::system::error_code &ec,
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070084 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist139cb572018-09-10 15:26:18 -070085}
86
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070087void HwmonTempSensor::handleResponse(const boost::system::error_code &err)
James Feist139cb572018-09-10 15:26:18 -070088{
89 if (err == boost::system::errc::bad_file_descriptor)
90 {
91 return; // we're being destroyed
92 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070093 std::istream responseStream(&readBuf);
James Feist139cb572018-09-10 15:26:18 -070094 if (!err)
95 {
96 std::string response;
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070097 std::getline(responseStream, response);
James Feist139cb572018-09-10 15:26:18 -070098 try
99 {
100 float nvalue = std::stof(response);
101
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700102 nvalue /= sensorScaleFactor;
James Feist139cb572018-09-10 15:26:18 -0700103 if (nvalue != value)
104 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700105 updateValue(nvalue);
James Feist139cb572018-09-10 15:26:18 -0700106 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700107 errCount = 0;
James Feist139cb572018-09-10 15:26:18 -0700108 }
109 catch (const std::invalid_argument &)
110 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700111 errCount++;
James Feist139cb572018-09-10 15:26:18 -0700112 }
113 }
114 else
115 {
116 std::cerr << "Failure to read sensor " << name << " at " << path
117 << "\n";
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700118 errCount++;
James Feist139cb572018-09-10 15:26:18 -0700119 }
120 // only send value update once
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700121 if (errCount == warnAfterErrorCount)
James Feist139cb572018-09-10 15:26:18 -0700122 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700123 updateValue(0);
James Feist139cb572018-09-10 15:26:18 -0700124 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700125 responseStream.clear();
126 inputDev.close();
James Feist139cb572018-09-10 15:26:18 -0700127 int fd = open(path.c_str(), O_RDONLY);
128 if (fd <= 0)
129 {
130 return; // we're no longer valid
131 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700132 inputDev.assign(fd);
133 waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs));
James Feist139cb572018-09-10 15:26:18 -0700134 ;
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700135 waitTimer.async_wait([&](const boost::system::error_code &ec) {
James Feist139cb572018-09-10 15:26:18 -0700136 if (ec == boost::asio::error::operation_aborted)
137 {
138 return; // we're being canceled
139 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700140 setupRead();
James Feist139cb572018-09-10 15:26:18 -0700141 });
142}
143
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700144void HwmonTempSensor::checkThresholds(void)
James Feist139cb572018-09-10 15:26:18 -0700145{
James Feistaf79dd32018-09-12 12:54:15 -0700146 thresholds::checkThresholds(this);
James Feist139cb572018-09-10 15:26:18 -0700147}
148
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700149void HwmonTempSensor::updateValue(const double &newValue)
James Feist139cb572018-09-10 15:26:18 -0700150{
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700151 sensorInterface->set_property("Value", newValue);
152 value = newValue;
153 checkThresholds();
James Feist139cb572018-09-10 15:26:18 -0700154}
155
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700156void HwmonTempSensor::setInitialProperties(
James Feist139cb572018-09-10 15:26:18 -0700157 std::shared_ptr<sdbusplus::asio::connection> &conn)
158{
159 // todo, get max and min from configuration
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700160 sensorInterface->register_property("MaxValue", maxValue);
161 sensorInterface->register_property("MinValue", minValue);
James Feistaf79dd32018-09-12 12:54:15 -0700162 sensorInterface->register_property("Value", value);
James Feist139cb572018-09-10 15:26:18 -0700163
164 for (auto &threshold : thresholds)
165 {
166 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
167 std::string level;
168 std::string alarm;
169 if (threshold.level == thresholds::Level::CRITICAL)
170 {
James Feistaf79dd32018-09-12 12:54:15 -0700171 iface = thresholdInterfaceCritical;
James Feist139cb572018-09-10 15:26:18 -0700172 if (threshold.direction == thresholds::Direction::HIGH)
173 {
174 level = "CriticalHigh";
175 alarm = "CriticalAlarmHigh";
176 }
177 else
178 {
179 level = "CriticalLow";
180 alarm = "CriticalAlarmLow";
181 }
182 }
183 else if (threshold.level == thresholds::Level::WARNING)
184 {
James Feistaf79dd32018-09-12 12:54:15 -0700185 iface = thresholdInterfaceWarning;
James Feist139cb572018-09-10 15:26:18 -0700186 if (threshold.direction == thresholds::Direction::HIGH)
187 {
188 level = "WarningHigh";
189 alarm = "WarningAlarmHigh";
190 }
191 else
192 {
193 level = "WarningLow";
194 alarm = "WarningAlarmLow";
195 }
196 }
197 else
198 {
199 std::cerr << "Unknown threshold level" << threshold.level << "\n";
200 continue;
201 }
202 if (!iface)
203 {
204 std::cout << "trying to set uninitialized interface\n";
205 continue;
206 }
207 iface->register_property(
208 level, threshold.value,
209 [&](const double &request, double &oldValue) {
210 oldValue = request; // todo, just let the config do this?
211 threshold.value = request;
212 thresholds::persistThreshold(configuration, objectType,
213 threshold, conn);
214 return 1;
215 });
216 iface->register_property(alarm, false);
217 }
James Feistaf79dd32018-09-12 12:54:15 -0700218 if (!sensorInterface->initialize())
James Feist139cb572018-09-10 15:26:18 -0700219 {
220 std::cerr << "error initializing value interface\n";
221 }
James Feistaf79dd32018-09-12 12:54:15 -0700222 if (thresholdInterfaceWarning && !thresholdInterfaceWarning->initialize())
James Feist139cb572018-09-10 15:26:18 -0700223 {
224 std::cerr << "error initializing warning threshold interface\n";
225 }
226
James Feistaf79dd32018-09-12 12:54:15 -0700227 if (thresholdInterfaceCritical && !thresholdInterfaceCritical->initialize())
James Feist139cb572018-09-10 15:26:18 -0700228 {
229 std::cerr << "error initializing critical threshold interface\n";
230 }
231}