blob: 66bca1f168d6bbbea27a59587fceebe376f7a0fa [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) :
38 path(path),
39 objectType(objectType), objServer(objectServer),
40 name(boost::replace_all_copy(sensorName, " ", "_")), dbusConnection(conn),
41 thresholds(std::move(_thresholds)), configuration(sensorConfiguration),
42 sensor_interface(objectServer.add_interface(
43 "/xyz/openbmc_project/sensors/temperature/" + name,
44 "xyz.openbmc_project.Sensor.Value")),
45 input_dev(io, open(path.c_str(), O_RDONLY)), wait_timer(io),
46 value(std::numeric_limits<double>::quiet_NaN()), err_count(0),
47 // todo, get these from config
48 max_value(127), min_value(-128)
49{
50 if (thresholds::HasWarningInterface(thresholds))
51 {
52 threshold_interface_warning = objectServer.add_interface(
53 "/xyz/openbmc_project/sensors/temperature/" + name,
54 "xyz.openbmc_project.Sensor.Threshold.Warning");
55 }
56 if (thresholds::HasCriticalInterface(thresholds))
57 {
58 threshold_interface_critical = objectServer.add_interface(
59 "/xyz/openbmc_project/sensors/temperature/" + name,
60 "xyz.openbmc_project.Sensor.Threshold.Critical");
61 }
62 set_initial_properties(conn);
63 isPowerOn(dbusConnection); // first call initializes
64 setup_read();
65}
66
67CPUSensor::~CPUSensor()
68{
69 // close the input dev to cancel async operations
70 input_dev.close();
71 wait_timer.cancel();
72 objServer.remove_interface(threshold_interface_warning);
73 objServer.remove_interface(threshold_interface_critical);
74 objServer.remove_interface(sensor_interface);
75}
76
77void CPUSensor::setup_read(void)
78{
79 boost::asio::async_read_until(
80 input_dev, read_buf, '\n',
81 [&](const boost::system::error_code &ec,
82 std::size_t /*bytes_transfered*/) { handle_response(ec); });
83}
84
85void CPUSensor::handle_response(const boost::system::error_code &err)
86{
87 if (err == boost::system::errc::bad_file_descriptor)
88 {
89 return; // we're being destroyed
90 }
91 std::istream response_stream(&read_buf);
92 if (!err)
93 {
94 std::string response;
95 try
96 {
97 std::getline(response_stream, response);
98 float nvalue = std::stof(response);
99 response_stream.clear();
100 nvalue /= CPUSensor::SENSOR_SCALE_FACTOR;
101 if (nvalue != value)
102 {
103 update_value(nvalue);
104 }
105 err_count = 0;
106 }
107 catch (const std::invalid_argument &)
108 {
109 err_count++;
110 }
111 }
112 else
113 {
114 err_count++;
115 }
116
117 // only send value update once
118 if (err_count == WARN_AFTER_ERROR_COUNT)
119 {
120 // only an error if power is on
121 if (isPowerOn(dbusConnection))
122 {
123 std::cerr << "Failure to read sensor " << name << " at " << path
124 << "\n";
125 update_value(0);
126 err_count++;
127 }
128 else
129 {
130 err_count = 0; // check power again in 10 cycles
131 sensor_interface->set_property(
132 "Value", std::numeric_limits<double>::quiet_NaN());
133 }
134 }
135
136 response_stream.clear();
137 input_dev.close();
138 int fd = open(path.c_str(), O_RDONLY);
139 if (fd <= 0)
140 {
141 return; // we're no longer valid
142 }
143 input_dev.assign(fd);
144 wait_timer.expires_from_now(
145 boost::posix_time::milliseconds(CPUSensor::SENSOR_POLL_MS));
146 wait_timer.async_wait([&](const boost::system::error_code &ec) {
147 if (ec == boost::asio::error::operation_aborted)
148 {
149 return; // we're being canceled
150 }
151 setup_read();
152 });
153}
154
155void CPUSensor::check_thresholds(void)
156{
157 if (thresholds.empty())
158 return;
159 for (auto threshold : thresholds)
160 {
161 if (threshold.direction == thresholds::Direction::HIGH)
162 {
163 if (value > threshold.value)
164 {
165 assert_thresholds(threshold.level, threshold.direction, true);
166 }
167 else
168 {
169 assert_thresholds(threshold.level, threshold.direction, false);
170 }
171 }
172 else
173 {
174 if (value < threshold.value)
175 {
176 assert_thresholds(threshold.level, threshold.direction, true);
177 }
178 else
179 {
180 assert_thresholds(threshold.level, threshold.direction, false);
181 }
182 }
183 }
184}
185
186void CPUSensor::update_value(const double &new_value)
187{
188 sensor_interface->set_property("Value", new_value);
189 value = new_value;
190 check_thresholds();
191}
192
193void CPUSensor::assert_thresholds(thresholds::Level level,
194 thresholds::Direction direction, bool assert)
195{
196 std::string property;
197 std::shared_ptr<sdbusplus::asio::dbus_interface> interface;
198 if (level == thresholds::Level::WARNING &&
199 direction == thresholds::Direction::HIGH)
200 {
201 property = "WarningAlarmHigh";
202 interface = threshold_interface_warning;
203 }
204 else if (level == thresholds::Level::WARNING &&
205 direction == thresholds::Direction::LOW)
206 {
207 property = "WarningAlarmLow";
208 interface = threshold_interface_warning;
209 }
210 else if (level == thresholds::Level::CRITICAL &&
211 direction == thresholds::Direction::HIGH)
212 {
213 property = "CriticalAlarmHigh";
214 interface = threshold_interface_critical;
215 }
216 else if (level == thresholds::Level::CRITICAL &&
217 direction == thresholds::Direction::LOW)
218 {
219 property = "CriticalAlarmLow";
220 interface = threshold_interface_critical;
221 }
222 else
223 {
224 std::cerr << "Unknown threshold, level " << level << "direction "
225 << direction << "\n";
226 return;
227 }
228 if (!interface)
229 {
230 std::cout << "trying to set uninitialized interface\n";
231 return;
232 }
233 interface->set_property(property, assert);
234}
235
236void CPUSensor::set_initial_properties(
237 std::shared_ptr<sdbusplus::asio::connection> &conn)
238{
239 // todo, get max and min from configuration
240 sensor_interface->register_property("MaxValue", max_value);
241 sensor_interface->register_property("MinValue", min_value);
242 sensor_interface->register_property("Value", value);
243
244 for (auto &threshold : thresholds)
245 {
246 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
247 std::string level;
248 std::string alarm;
249 if (threshold.level == thresholds::Level::CRITICAL)
250 {
251 iface = threshold_interface_critical;
252 if (threshold.direction == thresholds::Direction::HIGH)
253 {
254 level = "CriticalHigh";
255 alarm = "CriticalAlarmHigh";
256 }
257 else
258 {
259 level = "CriticalLow";
260 alarm = "CriticalAlarmLow";
261 }
262 }
263 else if (threshold.level == thresholds::Level::WARNING)
264 {
265 iface = threshold_interface_warning;
266 if (threshold.direction == thresholds::Direction::HIGH)
267 {
268 level = "WarningHigh";
269 alarm = "WarningAlarmHigh";
270 }
271 else
272 {
273 level = "WarningLow";
274 alarm = "WarningAlarmLow";
275 }
276 }
277 else
278 {
279 std::cerr << "Unknown threshold level" << threshold.level << "\n";
280 continue;
281 }
282 if (!iface)
283 {
284 std::cout << "trying to set uninitialized interface\n";
285 continue;
286 }
287 if (threshold.writeable)
288 {
289 iface->register_property(
290 level, threshold.value,
291 [&](const double &request, double &oldValue) {
292 oldValue = request; // todo, just let the config do this?
293 threshold.value = request;
294 thresholds::persistThreshold(configuration, objectType,
295 threshold, conn);
296 return 1;
297 });
298 }
299 else
300 {
301 iface->register_property(level, threshold.value);
302 }
303 iface->register_property(alarm, false);
304 }
305 if (!sensor_interface->initialize())
306 {
307 std::cerr << "error initializing value interface\n";
308 }
309 if (threshold_interface_warning &&
310 !threshold_interface_warning->initialize())
311 {
312 std::cerr << "error initializing warning threshold interface\n";
313 }
314
315 if (threshold_interface_critical &&
316 !threshold_interface_critical->initialize())
317 {
318 std::cerr << "error initializing critical threshold interface\n";
319 }
320}