blob: ce9e60b397c6d1333ec80d62e330a7846517a795 [file] [log] [blame]
James Feist139cb572018-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
30static constexpr size_t WARN_AFTER_ERROR_COUNT = 10;
31
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 Feistaf79dd32018-09-12 12:54:15 -070038 Sensor(),
39 path(path), objectType(objectType), objServer(objectServer),
James Feist139cb572018-09-10 15:26:18 -070040 name(boost::replace_all_copy(sensorName, " ", "_")), dbusConnection(conn),
James Feistaf79dd32018-09-12 12:54:15 -070041 configuration(sensorConfiguration),
42
43 input_dev(io, open(path.c_str(), O_RDONLY)), wait_timer(io), err_count(0),
James Feist139cb572018-09-10 15:26:18 -070044 // todo, get these from config
45 max_value(127), min_value(-128)
46{
James Feistaf79dd32018-09-12 12:54:15 -070047 thresholds = std::move(_thresholds);
48 sensorInterface = objectServer.add_interface(
49 "/xyz/openbmc_project/sensors/temperature/" + name,
50 "xyz.openbmc_project.Sensor.Value");
James Feist139cb572018-09-10 15:26:18 -070051 if (thresholds::HasWarningInterface(thresholds))
52 {
James Feistaf79dd32018-09-12 12:54:15 -070053 thresholdInterfaceWarning = objectServer.add_interface(
James Feist139cb572018-09-10 15:26:18 -070054 "/xyz/openbmc_project/sensors/temperature/" + name,
55 "xyz.openbmc_project.Sensor.Threshold.Warning");
56 }
57 if (thresholds::HasCriticalInterface(thresholds))
58 {
James Feistaf79dd32018-09-12 12:54:15 -070059 thresholdInterfaceCritical = objectServer.add_interface(
James Feist139cb572018-09-10 15:26:18 -070060 "/xyz/openbmc_project/sensors/temperature/" + name,
61 "xyz.openbmc_project.Sensor.Threshold.Critical");
62 }
63 set_initial_properties(conn);
64 isPowerOn(dbusConnection); // first call initializes
65 setup_read();
66}
67
68CPUSensor::~CPUSensor()
69{
70 // close the input dev to cancel async operations
71 input_dev.close();
72 wait_timer.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
78void CPUSensor::setup_read(void)
79{
80 boost::asio::async_read_until(
81 input_dev, read_buf, '\n',
82 [&](const boost::system::error_code &ec,
83 std::size_t /*bytes_transfered*/) { handle_response(ec); });
84}
85
86void CPUSensor::handle_response(const boost::system::error_code &err)
87{
88 if (err == boost::system::errc::bad_file_descriptor)
89 {
90 return; // we're being destroyed
91 }
92 std::istream response_stream(&read_buf);
93 if (!err)
94 {
95 std::string response;
96 try
97 {
98 std::getline(response_stream, response);
99 float nvalue = std::stof(response);
100 response_stream.clear();
101 nvalue /= CPUSensor::SENSOR_SCALE_FACTOR;
102 if (nvalue != value)
103 {
104 update_value(nvalue);
105 }
106 err_count = 0;
107 }
108 catch (const std::invalid_argument &)
109 {
110 err_count++;
111 }
112 }
113 else
114 {
115 err_count++;
116 }
117
118 // only send value update once
119 if (err_count == WARN_AFTER_ERROR_COUNT)
120 {
121 // only an error if power is on
122 if (isPowerOn(dbusConnection))
123 {
124 std::cerr << "Failure to read sensor " << name << " at " << path
125 << "\n";
126 update_value(0);
127 err_count++;
128 }
129 else
130 {
131 err_count = 0; // check power again in 10 cycles
James Feistaf79dd32018-09-12 12:54:15 -0700132 sensorInterface->set_property(
James Feist139cb572018-09-10 15:26:18 -0700133 "Value", std::numeric_limits<double>::quiet_NaN());
134 }
135 }
136
137 response_stream.clear();
138 input_dev.close();
139 int fd = open(path.c_str(), O_RDONLY);
140 if (fd <= 0)
141 {
142 return; // we're no longer valid
143 }
144 input_dev.assign(fd);
145 wait_timer.expires_from_now(
146 boost::posix_time::milliseconds(CPUSensor::SENSOR_POLL_MS));
147 wait_timer.async_wait([&](const boost::system::error_code &ec) {
148 if (ec == boost::asio::error::operation_aborted)
149 {
150 return; // we're being canceled
151 }
152 setup_read();
153 });
154}
155
156void CPUSensor::check_thresholds(void)
157{
James Feistaf79dd32018-09-12 12:54:15 -0700158 thresholds::checkThresholds(this);
James Feist139cb572018-09-10 15:26:18 -0700159}
160
161void CPUSensor::update_value(const double &new_value)
162{
James Feistaf79dd32018-09-12 12:54:15 -0700163 sensorInterface->set_property("Value", new_value);
James Feist139cb572018-09-10 15:26:18 -0700164 value = new_value;
165 check_thresholds();
166}
167
James Feist139cb572018-09-10 15:26:18 -0700168void CPUSensor::set_initial_properties(
169 std::shared_ptr<sdbusplus::asio::connection> &conn)
170{
171 // todo, get max and min from configuration
James Feistaf79dd32018-09-12 12:54:15 -0700172 sensorInterface->register_property("MaxValue", max_value);
173 sensorInterface->register_property("MinValue", min_value);
174 sensorInterface->register_property("Value", value);
James Feist139cb572018-09-10 15:26:18 -0700175
176 for (auto &threshold : thresholds)
177 {
178 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
179 std::string level;
180 std::string alarm;
181 if (threshold.level == thresholds::Level::CRITICAL)
182 {
James Feistaf79dd32018-09-12 12:54:15 -0700183 iface = thresholdInterfaceCritical;
James Feist139cb572018-09-10 15:26:18 -0700184 if (threshold.direction == thresholds::Direction::HIGH)
185 {
186 level = "CriticalHigh";
187 alarm = "CriticalAlarmHigh";
188 }
189 else
190 {
191 level = "CriticalLow";
192 alarm = "CriticalAlarmLow";
193 }
194 }
195 else if (threshold.level == thresholds::Level::WARNING)
196 {
James Feistaf79dd32018-09-12 12:54:15 -0700197 iface = thresholdInterfaceWarning;
James Feist139cb572018-09-10 15:26:18 -0700198 if (threshold.direction == thresholds::Direction::HIGH)
199 {
200 level = "WarningHigh";
201 alarm = "WarningAlarmHigh";
202 }
203 else
204 {
205 level = "WarningLow";
206 alarm = "WarningAlarmLow";
207 }
208 }
209 else
210 {
211 std::cerr << "Unknown threshold level" << threshold.level << "\n";
212 continue;
213 }
214 if (!iface)
215 {
216 std::cout << "trying to set uninitialized interface\n";
217 continue;
218 }
219 if (threshold.writeable)
220 {
221 iface->register_property(
222 level, threshold.value,
223 [&](const double &request, double &oldValue) {
224 oldValue = request; // todo, just let the config do this?
225 threshold.value = request;
226 thresholds::persistThreshold(configuration, objectType,
227 threshold, conn);
228 return 1;
229 });
230 }
231 else
232 {
233 iface->register_property(level, threshold.value);
234 }
235 iface->register_property(alarm, false);
236 }
James Feistaf79dd32018-09-12 12:54:15 -0700237 if (!sensorInterface->initialize())
James Feist139cb572018-09-10 15:26:18 -0700238 {
239 std::cerr << "error initializing value interface\n";
240 }
James Feistaf79dd32018-09-12 12:54:15 -0700241 if (thresholdInterfaceWarning && !thresholdInterfaceWarning->initialize())
James Feist139cb572018-09-10 15:26:18 -0700242 {
243 std::cerr << "error initializing warning threshold interface\n";
244 }
245
James Feistaf79dd32018-09-12 12:54:15 -0700246 if (thresholdInterfaceCritical && !thresholdInterfaceCritical->initialize())
James Feist139cb572018-09-10 15:26:18 -0700247 {
248 std::cerr << "error initializing critical threshold interface\n";
249 }
250}