blob: 91f05457f7a4ccdb3a0ed6fb5c7248262d1d187e [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 <TachSensor.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 unsigned int PWM_POLL_MS = 500;
31static constexpr size_t WARN_AFTER_ERROR_COUNT = 10;
32
33TachSensor::TachSensor(const std::string &path,
34 sdbusplus::asio::object_server &objectServer,
35 std::shared_ptr<sdbusplus::asio::connection> &conn,
36 boost::asio::io_service &io, const std::string &fanName,
37 std::vector<thresholds::Threshold> &&_thresholds,
38 const std::string &sensorConfiguration) :
39 path(path),
40 objServer(objectServer), dbusConnection(conn),
41 name(boost::replace_all_copy(fanName, " ", "_")),
42 configuration(sensorConfiguration), thresholds(std::move(_thresholds)),
43 sensor_interface(objectServer.add_interface(
44 "/xyz/openbmc_project/sensors/fan_tach/" + name,
45 "xyz.openbmc_project.Sensor.Value")),
46 input_dev(io, open(path.c_str(), O_RDONLY)), wait_timer(io),
47 value(std::numeric_limits<double>::quiet_NaN()), err_count(0),
48 // todo, get these from config
49 max_value(25000), min_value(0)
50{
51 if (thresholds::HasWarningInterface(thresholds))
52 {
53 threshold_interface_warning = objectServer.add_interface(
54 "/xyz/openbmc_project/sensors/fan_tach/" + name,
55 "xyz.openbmc_project.Sensor.Threshold.Warning");
56 }
57 if (thresholds::HasCriticalInterface(thresholds))
58 {
59 threshold_interface_critical = objectServer.add_interface(
60 "/xyz/openbmc_project/sensors/fan_tach/" + 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
68TachSensor::~TachSensor()
69{
70 // close the input dev to cancel async operations
71 input_dev.close();
72 wait_timer.cancel();
73 objServer.remove_interface(threshold_interface_warning);
74 objServer.remove_interface(threshold_interface_critical);
75 objServer.remove_interface(sensor_interface);
76}
77
78void TachSensor::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 TachSensor::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 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
115 err_count++;
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 }
127 else
128 {
129 err_count = 0; // check power again in 10 cycles
130 sensor_interface->set_property(
131 "Value", std::numeric_limits<double>::quiet_NaN());
132 }
133 }
134 response_stream.clear();
135 input_dev.close();
136 int fd = open(path.c_str(), O_RDONLY);
137 if (fd <= 0)
138 {
139 return; // we're no longer valid
140 }
141 input_dev.assign(fd);
142 wait_timer.expires_from_now(boost::posix_time::milliseconds(PWM_POLL_MS));
143 wait_timer.async_wait([&](const boost::system::error_code &ec) {
144 if (ec == boost::asio::error::operation_aborted)
145 {
146 return; // we're being canceled
147 }
148 setup_read();
149 });
150}
151
152void TachSensor::check_thresholds(void)
153{
154 if (thresholds.empty())
155 return;
156 for (auto &threshold : thresholds)
157 {
158 if (threshold.direction == thresholds::Direction::HIGH)
159 {
160 if (value > threshold.value)
161 {
162 assert_thresholds(threshold.level, threshold.direction, true);
163 }
164 else
165 {
166 assert_thresholds(threshold.level, threshold.direction, false);
167 }
168 }
169 else
170 {
171 if (value < threshold.value)
172 {
173 assert_thresholds(threshold.level, threshold.direction, true);
174 }
175 else
176 {
177 assert_thresholds(threshold.level, threshold.direction, false);
178 }
179 }
180 }
181}
182
183void TachSensor::update_value(const double &new_value)
184{
185 sensor_interface->set_property("Value", new_value);
186 value = new_value;
187 check_thresholds();
188}
189
190void TachSensor::assert_thresholds(thresholds::Level level,
191 thresholds::Direction direction, bool assert)
192{
193 std::string property;
194 std::shared_ptr<sdbusplus::asio::dbus_interface> interface;
195 if (level == thresholds::Level::WARNING &&
196 direction == thresholds::Direction::HIGH)
197 {
198 property = "WarningAlarmHigh";
199 interface = threshold_interface_warning;
200 }
201 else if (level == thresholds::Level::WARNING &&
202 direction == thresholds::Direction::LOW)
203 {
204 property = "WarningAlarmLow";
205 interface = threshold_interface_warning;
206 }
207 else if (level == thresholds::Level::CRITICAL &&
208 direction == thresholds::Direction::HIGH)
209 {
210 property = "CriticalAlarmHigh";
211 interface = threshold_interface_critical;
212 }
213 else if (level == thresholds::Level::CRITICAL &&
214 direction == thresholds::Direction::LOW)
215 {
216 property = "CriticalAlarmLow";
217 interface = threshold_interface_critical;
218 }
219 else
220 {
221 std::cerr << "Unknown threshold, level " << level << "direction "
222 << direction << "\n";
223 return;
224 }
225 if (!interface)
226 {
227 std::cout << "trying to set uninitialized interface\n";
228 return;
229 }
230 interface->set_property(property, assert);
231}
232
233void TachSensor::set_initial_properties(
234 std::shared_ptr<sdbusplus::asio::connection> &conn)
235{
236 // todo, get max and min from configuration
237 sensor_interface->register_property("MaxValue", max_value);
238 sensor_interface->register_property("MinValue", min_value);
239 sensor_interface->register_property("Value", value);
240
241 for (auto &threshold : thresholds)
242 {
243 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
244 std::string level;
245 std::string alarm;
246 if (threshold.level == thresholds::Level::CRITICAL)
247 {
248 iface = threshold_interface_critical;
249 if (threshold.direction == thresholds::Direction::HIGH)
250 {
251 level = "CriticalHigh";
252 alarm = "CriticalAlarmHigh";
253 }
254 else
255 {
256 level = "CriticalLow";
257 alarm = "CriticalAlarmLow";
258 }
259 }
260 else if (threshold.level == thresholds::Level::WARNING)
261 {
262 iface = threshold_interface_warning;
263 if (threshold.direction == thresholds::Direction::HIGH)
264 {
265 level = "WarningHigh";
266 alarm = "WarningAlarmHigh";
267 }
268 else
269 {
270 level = "WarningLow";
271 alarm = "WarningAlarmLow";
272 }
273 }
274 else
275 {
276 std::cerr << "Unknown threshold level" << threshold.level << "\n";
277 continue;
278 }
279 if (!iface)
280 {
281 std::cout << "trying to set uninitialized interface\n";
282 continue;
283 }
284 iface->register_property(
285 level, threshold.value,
286 [&](const double &request, double &oldValue) {
287 oldValue = request; // todo, just let the config do this?
288 threshold.value = request;
289 thresholds::persistThreshold(
290 configuration,
291 "xyz.openbmc_project.Configuration.AspeedFan", threshold,
292 conn);
293 return 1;
294 });
295 iface->register_property(alarm, false);
296 }
297 if (!sensor_interface->initialize())
298 {
299 std::cerr << "error initializing value interface\n";
300 }
301 if (threshold_interface_warning &&
302 !threshold_interface_warning->initialize())
303 {
304 std::cerr << "error initializing warning threshold interface\n";
305 }
306
307 if (threshold_interface_critical &&
308 !threshold_interface_critical->initialize())
309 {
310 std::cerr << "error initializing critical threshold interface\n";
311 }
312}