blob: 24e30c833f46f268d04db2a92b9e3eb3ab99558e [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*/
16
17#include <unistd.h>
18
19#include <CPUSensor.hpp>
20#include <Utils.hpp>
21#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
23#include <boost/date_time/posix_time/posix_time.hpp>
24#include <iostream>
25#include <limits>
26#include <sdbusplus/asio/connection.hpp>
27#include <sdbusplus/asio/object_server.hpp>
28#include <string>
29
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070030static constexpr size_t warnAfterErrorCount = 10;
James Feist6714a252018-09-10 15:26:18 -070031
32CPUSensor::CPUSensor(const std::string &path, const std::string &objectType,
33 sdbusplus::asio::object_server &objectServer,
34 std::shared_ptr<sdbusplus::asio::connection> &conn,
35 boost::asio::io_service &io, const std::string &sensorName,
36 std::vector<thresholds::Threshold> &&_thresholds,
37 const std::string &sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -070038 Sensor(boost::replace_all_copy(sensorName, " ", "_"), path,
39 std::move(_thresholds)),
40 objectType(objectType), objServer(objectServer), dbusConnection(conn),
James Feist251c7822018-09-12 12:54:15 -070041 configuration(sensorConfiguration),
42
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070043 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), errCount(0),
James Feist6714a252018-09-10 15:26:18 -070044 // todo, get these from config
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070045 maxValue(127), minValue(-128)
James Feist6714a252018-09-10 15:26:18 -070046{
James Feist251c7822018-09-12 12:54:15 -070047 sensorInterface = objectServer.add_interface(
48 "/xyz/openbmc_project/sensors/temperature/" + name,
49 "xyz.openbmc_project.Sensor.Value");
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070050 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070051 {
James Feist251c7822018-09-12 12:54:15 -070052 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070053 "/xyz/openbmc_project/sensors/temperature/" + name,
54 "xyz.openbmc_project.Sensor.Threshold.Warning");
55 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070056 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070057 {
James Feist251c7822018-09-12 12:54:15 -070058 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070059 "/xyz/openbmc_project/sensors/temperature/" + name,
60 "xyz.openbmc_project.Sensor.Threshold.Critical");
61 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070062 setInitialProperties(conn);
James Feist6714a252018-09-10 15:26:18 -070063 isPowerOn(dbusConnection); // first call initializes
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070064 setupRead();
James Feist6714a252018-09-10 15:26:18 -070065}
66
67CPUSensor::~CPUSensor()
68{
69 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070070 inputDev.close();
71 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070072 objServer.remove_interface(thresholdInterfaceWarning);
73 objServer.remove_interface(thresholdInterfaceCritical);
74 objServer.remove_interface(sensorInterface);
James Feist6714a252018-09-10 15:26:18 -070075}
76
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070077void CPUSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070078{
79 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070080 inputDev, readBuf, '\n',
James Feist6714a252018-09-10 15:26:18 -070081 [&](const boost::system::error_code &ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070082 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -070083}
84
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070085void CPUSensor::handleResponse(const boost::system::error_code &err)
James Feist6714a252018-09-10 15:26:18 -070086{
87 if (err == boost::system::errc::bad_file_descriptor)
88 {
89 return; // we're being destroyed
90 }
James Feist1169eb42018-10-31 10:08:47 -070091 size_t pollTime = CPUSensor::sensorPollMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070092 std::istream responseStream(&readBuf);
James Feist6714a252018-09-10 15:26:18 -070093 if (!err)
94 {
95 std::string response;
96 try
97 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070098 std::getline(responseStream, response);
James Feist6714a252018-09-10 15:26:18 -070099 float nvalue = std::stof(response);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700100 responseStream.clear();
101 nvalue /= CPUSensor::sensorScaleFactor;
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530102 if (!isnan(overriddenValue))
103 {
104 nvalue = overriddenValue;
105 }
James Feist6714a252018-09-10 15:26:18 -0700106 if (nvalue != value)
107 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700108 updateValue(nvalue);
James Feist6714a252018-09-10 15:26:18 -0700109 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700110 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700111 }
112 catch (const std::invalid_argument &)
113 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700114 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700115 }
116 }
117 else
118 {
James Feist1169eb42018-10-31 10:08:47 -0700119 pollTime = sensorFailedPollTimeMs;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700120 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700121 }
122
James Feistdc6c55f2018-10-31 12:53:20 -0700123 if (errCount >= warnAfterErrorCount)
James Feist6714a252018-09-10 15:26:18 -0700124 {
125 // only an error if power is on
126 if (isPowerOn(dbusConnection))
127 {
James Feistdc6c55f2018-10-31 12:53:20 -0700128 // only print once
129 if (errCount == warnAfterErrorCount)
130 {
131 std::cerr << "Failure to read sensor " << name << " at " << path
132 << "\n";
133 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700134 updateValue(0);
135 errCount++;
James Feist6714a252018-09-10 15:26:18 -0700136 }
137 else
138 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700139 errCount = 0; // check power again in 10 cycles
James Feist1169eb42018-10-31 10:08:47 -0700140 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist6714a252018-09-10 15:26:18 -0700141 }
142 }
143
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700144 responseStream.clear();
145 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700146 int fd = open(path.c_str(), O_RDONLY);
147 if (fd <= 0)
148 {
149 return; // we're no longer valid
150 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700151 inputDev.assign(fd);
James Feist1169eb42018-10-31 10:08:47 -0700152 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700153 waitTimer.async_wait([&](const boost::system::error_code &ec) {
James Feist6714a252018-09-10 15:26:18 -0700154 if (ec == boost::asio::error::operation_aborted)
155 {
156 return; // we're being canceled
157 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700158 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700159 });
160}
161
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700162void CPUSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700163{
James Feist251c7822018-09-12 12:54:15 -0700164 thresholds::checkThresholds(this);
James Feist6714a252018-09-10 15:26:18 -0700165}
166
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700167void CPUSensor::updateValue(const double &newValue)
James Feist6714a252018-09-10 15:26:18 -0700168{
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530169 // Indicate that it is internal set call
170 internalSet = true;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700171 sensorInterface->set_property("Value", newValue);
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530172 internalSet = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700173 value = newValue;
174 checkThresholds();
James Feist6714a252018-09-10 15:26:18 -0700175}
176
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700177void CPUSensor::setInitialProperties(
James Feist6714a252018-09-10 15:26:18 -0700178 std::shared_ptr<sdbusplus::asio::connection> &conn)
179{
180 // todo, get max and min from configuration
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700181 sensorInterface->register_property("MaxValue", maxValue);
182 sensorInterface->register_property("MinValue", minValue);
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530183 sensorInterface->register_property(
184 "Value", value, [&](const double &newValue, double &oldValue) {
185 return setSensorValue(newValue, oldValue);
186 });
James Feist6714a252018-09-10 15:26:18 -0700187
188 for (auto &threshold : thresholds)
189 {
190 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
191 std::string level;
192 std::string alarm;
193 if (threshold.level == thresholds::Level::CRITICAL)
194 {
James Feist251c7822018-09-12 12:54:15 -0700195 iface = thresholdInterfaceCritical;
James Feist6714a252018-09-10 15:26:18 -0700196 if (threshold.direction == thresholds::Direction::HIGH)
197 {
198 level = "CriticalHigh";
199 alarm = "CriticalAlarmHigh";
200 }
201 else
202 {
203 level = "CriticalLow";
204 alarm = "CriticalAlarmLow";
205 }
206 }
207 else if (threshold.level == thresholds::Level::WARNING)
208 {
James Feist251c7822018-09-12 12:54:15 -0700209 iface = thresholdInterfaceWarning;
James Feist6714a252018-09-10 15:26:18 -0700210 if (threshold.direction == thresholds::Direction::HIGH)
211 {
212 level = "WarningHigh";
213 alarm = "WarningAlarmHigh";
214 }
215 else
216 {
217 level = "WarningLow";
218 alarm = "WarningAlarmLow";
219 }
220 }
221 else
222 {
223 std::cerr << "Unknown threshold level" << threshold.level << "\n";
224 continue;
225 }
226 if (!iface)
227 {
228 std::cout << "trying to set uninitialized interface\n";
229 continue;
230 }
231 if (threshold.writeable)
232 {
233 iface->register_property(
234 level, threshold.value,
235 [&](const double &request, double &oldValue) {
236 oldValue = request; // todo, just let the config do this?
237 threshold.value = request;
238 thresholds::persistThreshold(configuration, objectType,
239 threshold, conn);
240 return 1;
241 });
242 }
243 else
244 {
245 iface->register_property(level, threshold.value);
246 }
247 iface->register_property(alarm, false);
248 }
James Feist251c7822018-09-12 12:54:15 -0700249 if (!sensorInterface->initialize())
James Feist6714a252018-09-10 15:26:18 -0700250 {
251 std::cerr << "error initializing value interface\n";
252 }
James Feist251c7822018-09-12 12:54:15 -0700253 if (thresholdInterfaceWarning && !thresholdInterfaceWarning->initialize())
James Feist6714a252018-09-10 15:26:18 -0700254 {
255 std::cerr << "error initializing warning threshold interface\n";
256 }
257
James Feist251c7822018-09-12 12:54:15 -0700258 if (thresholdInterfaceCritical && !thresholdInterfaceCritical->initialize())
James Feist6714a252018-09-10 15:26:18 -0700259 {
260 std::cerr << "error initializing critical threshold interface\n";
261 }
262}