blob: cc9d37d5d2fe0aa465ce6b16f90afe022036038e [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
James Feist58295ad2019-05-30 15:01:41 -070017#include "TachSensor.hpp"
18
19#include "Utils.hpp"
20
James Feist6714a252018-09-10 15:26:18 -070021#include <unistd.h>
22
James Feist6714a252018-09-10 15:26:18 -070023#include <boost/algorithm/string/predicate.hpp>
24#include <boost/algorithm/string/replace.hpp>
25#include <boost/date_time/posix_time/posix_time.hpp>
James Feist7bc2bab2018-10-26 14:09:45 -070026#include <fstream>
Zhikui Ren347dd4e2019-12-12 13:39:50 -080027#include <gpiod.hpp>
James Feist6714a252018-09-10 15:26:18 -070028#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <istream>
James Feist6714a252018-09-10 15:26:18 -070030#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <memory>
32#include <optional>
James Feist6714a252018-09-10 15:26:18 -070033#include <sdbusplus/asio/connection.hpp>
34#include <sdbusplus/asio/object_server.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070036#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <utility>
38#include <vector>
James Feist6714a252018-09-10 15:26:18 -070039
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070040static constexpr unsigned int pwmPollMs = 500;
41static constexpr size_t warnAfterErrorCount = 10;
James Feist6714a252018-09-10 15:26:18 -070042
James Feistd8705872019-02-08 13:26:09 -080043TachSensor::TachSensor(const std::string& path, const std::string& objectType,
44 sdbusplus::asio::object_server& objectServer,
45 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist60c0ec72019-03-18 15:08:43 -070046 std::unique_ptr<PresenceSensor>&& presenceSensor,
James Feist7b18b1e2019-05-14 13:42:09 -070047 std::optional<RedundancySensor>* redundancy,
James Feistd8705872019-02-08 13:26:09 -080048 boost::asio::io_service& io, const std::string& fanName,
49 std::vector<thresholds::Threshold>&& _thresholds,
50 const std::string& sensorConfiguration,
51 const std::pair<size_t, size_t>& limits) :
James Feist930fcde2019-05-28 12:58:43 -070052 Sensor(boost::replace_all_copy(fanName, " ", "_"), std::move(_thresholds),
53 sensorConfiguration, objectType, limits.second, limits.first),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050054 objServer(objectServer), redundancy(redundancy),
55 presence(std::move(presenceSensor)),
56 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
57 errCount(0)
James Feist6714a252018-09-10 15:26:18 -070058{
James Feist251c7822018-09-12 12:54:15 -070059 sensorInterface = objectServer.add_interface(
60 "/xyz/openbmc_project/sensors/fan_tach/" + name,
61 "xyz.openbmc_project.Sensor.Value");
62
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070063 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070064 {
James Feist251c7822018-09-12 12:54:15 -070065 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070066 "/xyz/openbmc_project/sensors/fan_tach/" + name,
67 "xyz.openbmc_project.Sensor.Threshold.Warning");
68 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070069 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070070 {
James Feist251c7822018-09-12 12:54:15 -070071 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070072 "/xyz/openbmc_project/sensors/fan_tach/" + name,
73 "xyz.openbmc_project.Sensor.Threshold.Critical");
74 }
James Feist078f2322019-03-08 11:09:05 -080075 association = objectServer.add_interface(
76 "/xyz/openbmc_project/sensors/fan_tach/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070077 association::interface);
James Feist60c0ec72019-03-18 15:08:43 -070078
79 if (presence)
80 {
81 itemIface =
James Feistd8bd5622019-06-26 12:09:05 -070082 objectServer.add_interface("/xyz/openbmc_project/inventory/" + name,
James Feist60c0ec72019-03-18 15:08:43 -070083 "xyz.openbmc_project.Inventory.Item");
84 itemIface->register_property("PrettyName",
85 std::string()); // unused property
86 itemIface->register_property("Present", true);
87 itemIface->initialize();
James Feist2adc95c2019-09-30 14:55:28 -070088 itemAssoc = objectServer.add_interface(
89 "/xyz/openbmc_project/inventory/" + name, association::interface);
James Feistd8bd5622019-06-26 12:09:05 -070090 itemAssoc->register_property(
91 "associations",
92 std::vector<Association>{
93 {"sensors", "inventory",
94 "/xyz/openbmc_project/sensors/fan_tach/" + name}});
95 itemAssoc->initialize();
James Feist60c0ec72019-03-18 15:08:43 -070096 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070097 setInitialProperties(conn);
James Feist6ef20402019-01-07 16:45:08 -080098 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070099 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700100}
101
102TachSensor::~TachSensor()
103{
104 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700105 inputDev.close();
106 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -0700107 objServer.remove_interface(thresholdInterfaceWarning);
108 objServer.remove_interface(thresholdInterfaceCritical);
109 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800110 objServer.remove_interface(association);
James Feist60c0ec72019-03-18 15:08:43 -0700111 objServer.remove_interface(itemIface);
James Feistd8bd5622019-06-26 12:09:05 -0700112 objServer.remove_interface(itemAssoc);
James Feist6714a252018-09-10 15:26:18 -0700113}
114
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700115void TachSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700116{
117 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700118 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -0800119 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700120 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700121}
122
James Feistd8705872019-02-08 13:26:09 -0800123void TachSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700124{
125 if (err == boost::system::errc::bad_file_descriptor)
126 {
127 return; // we're being destroyed
128 }
James Feist7bc2bab2018-10-26 14:09:45 -0700129 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700130 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700131 if (presence)
132 {
133 if (!presence->getValue())
134 {
James Feist1169eb42018-10-31 10:08:47 -0700135 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700136 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700137 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700138 }
James Feist60c0ec72019-03-18 15:08:43 -0700139 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700140 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700141 std::istream responseStream(&readBuf);
James Feist7bc2bab2018-10-26 14:09:45 -0700142 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700143 {
James Feist7bc2bab2018-10-26 14:09:45 -0700144 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700145 {
James Feist7bc2bab2018-10-26 14:09:45 -0700146 std::string response;
147 try
James Feist6714a252018-09-10 15:26:18 -0700148 {
James Feist7bc2bab2018-10-26 14:09:45 -0700149 std::getline(responseStream, response);
150 float nvalue = std::stof(response);
151 responseStream.clear();
James Feistb6c0b912019-07-09 12:21:44 -0700152 if (static_cast<double>(nvalue) != value)
James Feist7bc2bab2018-10-26 14:09:45 -0700153 {
154 updateValue(nvalue);
155 }
156 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700157 }
James Feistd8705872019-02-08 13:26:09 -0800158 catch (const std::invalid_argument&)
James Feist7bc2bab2018-10-26 14:09:45 -0700159 {
160 errCount++;
161 }
James Feist6714a252018-09-10 15:26:18 -0700162 }
163 else
164 {
James Feist71d31b22019-01-02 16:57:54 -0800165 if (!isPowerOn())
James Feist7bc2bab2018-10-26 14:09:45 -0700166 {
James Feist71d31b22019-01-02 16:57:54 -0800167 errCount = 0;
168 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700169 }
170 else
171 {
James Feist71d31b22019-01-02 16:57:54 -0800172 pollTime = sensorFailedPollTimeMs;
173 errCount++;
James Feist7bc2bab2018-10-26 14:09:45 -0700174 }
James Feist6714a252018-09-10 15:26:18 -0700175 }
James Feist71d31b22019-01-02 16:57:54 -0800176 if (errCount >= warnAfterErrorCount)
177 {
178 // only print once
179 if (errCount == warnAfterErrorCount)
180 {
181 std::cerr << "Failure to read sensor " << name << " at " << path
182 << " ec:" << err << "\n";
183 }
184 updateValue(0);
185 }
James Feist6714a252018-09-10 15:26:18 -0700186 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700187 responseStream.clear();
188 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700189 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700190 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700191 {
192 return; // we're no longer valid
193 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700194 inputDev.assign(fd);
James Feist7bc2bab2018-10-26 14:09:45 -0700195 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800196 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700197 if (ec == boost::asio::error::operation_aborted)
198 {
199 return; // we're being canceled
200 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700201 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700202 });
203}
204
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700205void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700206{
James Feist95e54902019-09-04 15:13:36 -0700207 if (!isPowerOn())
208 {
209 return;
210 }
211
James Feistdc6c55f2018-10-31 12:53:20 -0700212 bool status = thresholds::checkThresholds(this);
James Feist95e54902019-09-04 15:13:36 -0700213
James Feist7b18b1e2019-05-14 13:42:09 -0700214 if (redundancy && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700215 {
James Feist7b18b1e2019-05-14 13:42:09 -0700216 (*redundancy)
217 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700218 }
James Feist6714a252018-09-10 15:26:18 -0700219}
220
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800221PresenceSensor::PresenceSensor(const std::string& gpioName, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -0700222 boost::asio::io_service& io,
223 const std::string& name) :
James Feist7bc2bab2018-10-26 14:09:45 -0700224 inverted(inverted),
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800225 gpioLine(gpiod::find_line(gpioName)), gpioFd(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700226{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800227 if (!gpioLine)
James Feist7bc2bab2018-10-26 14:09:45 -0700228 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800229 std::cerr << "Error requesting gpio: " << gpioName << "\n";
230 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700231 return;
232 }
233
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800234 try
James Feist7bc2bab2018-10-26 14:09:45 -0700235 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800236 gpioLine.request({"FanSensor", gpiod::line_request::EVENT_BOTH_EDGES,
237 inverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0});
238 status = gpioLine.get_value();
239
240 int gpioLineFd = gpioLine.event_get_fd();
241 if (gpioLineFd < 0)
242 {
243 std::cerr << "Failed to get " << gpioName << " fd\n";
244 return;
245 }
246
247 gpioFd.assign(gpioLineFd);
248 }
249 catch (std::system_error&)
250 {
251 std::cerr << "Error reading gpio: " << gpioName << "\n";
252 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700253 return;
254 }
James Feist7bc2bab2018-10-26 14:09:45 -0700255
James Feist7bc2bab2018-10-26 14:09:45 -0700256 monitorPresence();
James Feist7bc2bab2018-10-26 14:09:45 -0700257}
258
259PresenceSensor::~PresenceSensor()
260{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800261 gpioFd.close();
262 gpioLine.release();
James Feist7bc2bab2018-10-26 14:09:45 -0700263}
264
265void PresenceSensor::monitorPresence(void)
266{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800267 gpioFd.async_wait(boost::asio::posix::stream_descriptor::wait_read,
268 [this](const boost::system::error_code& ec) {
269 if (ec == boost::system::errc::bad_file_descriptor)
270 {
271 return; // we're being destroyed
272 }
273 else if (ec)
274 {
275 std::cerr << "Error on presence sensor " << name
276 << " \n";
277 ;
278 }
279 else
280 {
281 read();
282 }
283 monitorPresence();
284 });
James Feist7bc2bab2018-10-26 14:09:45 -0700285}
286
287void PresenceSensor::read(void)
288{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800289 gpioLine.event_read();
290 status = gpioLine.get_value();
291 // Read is invoked when an edge event is detected by monitorPresence
292 if (status)
James Feist7bc2bab2018-10-26 14:09:45 -0700293 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800294 logFanInserted(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700295 }
296 else
297 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800298 logFanRemoved(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700299 }
300}
301
302bool PresenceSensor::getValue(void)
303{
304 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700305}
306
James Feist9bb67462019-03-15 15:09:50 -0700307RedundancySensor::RedundancySensor(size_t count,
308 const std::vector<std::string>& children,
309 sdbusplus::asio::object_server& objectServer,
310 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700311 count(count),
312 iface(objectServer.add_interface(
313 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700314 "xyz.openbmc_project.Control.FanRedundancy")),
315 association(objectServer.add_interface(
316 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700317 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700318 objectServer(objectServer)
319{
James Feist9bb67462019-03-15 15:09:50 -0700320 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700321 iface->register_property("Collection", children);
322 iface->register_property("Status", std::string("Full"));
323 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
324 iface->initialize();
325}
326RedundancySensor::~RedundancySensor()
327{
James Feist9bb67462019-03-15 15:09:50 -0700328 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700329 objectServer.remove_interface(iface);
330}
James Feistd8705872019-02-08 13:26:09 -0800331void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700332{
333 statuses[name] = failed;
334 size_t failedCount = 0;
335
James Feist7b18b1e2019-05-14 13:42:09 -0700336 std::string newState = redundancy::full;
James Feistd8705872019-02-08 13:26:09 -0800337 for (const auto& status : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700338 {
339 if (status.second)
340 {
341 failedCount++;
342 }
343 if (failedCount > count)
344 {
James Feist7b18b1e2019-05-14 13:42:09 -0700345 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700346 break;
347 }
348 else if (failedCount)
349 {
James Feist7b18b1e2019-05-14 13:42:09 -0700350 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700351 }
352 }
James Feist7b18b1e2019-05-14 13:42:09 -0700353 if (state != newState)
354 {
355 if (state == redundancy::full)
356 {
357 logFanRedundancyLost();
358 }
359 else if (newState == redundancy::full)
360 {
361 logFanRedundancyRestored();
362 }
363 state = newState;
364 iface->set_property("Status", state);
365 }
James Feistdc6c55f2018-10-31 12:53:20 -0700366}