blob: c45fcb0e5f4f2336a69c993769da5860f3511768 [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>
Zhikui Ren347dd4e2019-12-12 13:39:50 -080026#include <gpiod.hpp>
James Feist38fb5982020-05-28 10:09:54 -070027#include <sdbusplus/asio/connection.hpp>
28#include <sdbusplus/asio/object_server.hpp>
29
30#include <fstream>
James Feist6714a252018-09-10 15:26:18 -070031#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <istream>
James Feist6714a252018-09-10 15:26:18 -070033#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <memory>
35#include <optional>
Patrick Venture96e97db2019-10-31 13:44:38 -070036#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070037#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070038#include <utility>
39#include <vector>
James Feist6714a252018-09-10 15:26:18 -070040
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070041static constexpr unsigned int pwmPollMs = 500;
42static constexpr size_t warnAfterErrorCount = 10;
James Feist6714a252018-09-10 15:26:18 -070043
James Feistd8705872019-02-08 13:26:09 -080044TachSensor::TachSensor(const std::string& path, const std::string& objectType,
45 sdbusplus::asio::object_server& objectServer,
46 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist60c0ec72019-03-18 15:08:43 -070047 std::unique_ptr<PresenceSensor>&& presenceSensor,
James Feist7b18b1e2019-05-14 13:42:09 -070048 std::optional<RedundancySensor>* redundancy,
James Feistd8705872019-02-08 13:26:09 -080049 boost::asio::io_service& io, const std::string& fanName,
50 std::vector<thresholds::Threshold>&& _thresholds,
51 const std::string& sensorConfiguration,
52 const std::pair<size_t, size_t>& limits) :
James Feist930fcde2019-05-28 12:58:43 -070053 Sensor(boost::replace_all_copy(fanName, " ", "_"), std::move(_thresholds),
54 sensorConfiguration, objectType, limits.second, limits.first),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050055 objServer(objectServer), redundancy(redundancy),
56 presence(std::move(presenceSensor)),
57 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
58 errCount(0)
James Feist6714a252018-09-10 15:26:18 -070059{
James Feist251c7822018-09-12 12:54:15 -070060 sensorInterface = objectServer.add_interface(
61 "/xyz/openbmc_project/sensors/fan_tach/" + name,
62 "xyz.openbmc_project.Sensor.Value");
63
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070064 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070065 {
James Feist251c7822018-09-12 12:54:15 -070066 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070067 "/xyz/openbmc_project/sensors/fan_tach/" + name,
68 "xyz.openbmc_project.Sensor.Threshold.Warning");
69 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070070 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070071 {
James Feist251c7822018-09-12 12:54:15 -070072 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070073 "/xyz/openbmc_project/sensors/fan_tach/" + name,
74 "xyz.openbmc_project.Sensor.Threshold.Critical");
75 }
James Feist078f2322019-03-08 11:09:05 -080076 association = objectServer.add_interface(
77 "/xyz/openbmc_project/sensors/fan_tach/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070078 association::interface);
James Feist60c0ec72019-03-18 15:08:43 -070079
80 if (presence)
81 {
82 itemIface =
James Feistd8bd5622019-06-26 12:09:05 -070083 objectServer.add_interface("/xyz/openbmc_project/inventory/" + name,
James Feist60c0ec72019-03-18 15:08:43 -070084 "xyz.openbmc_project.Inventory.Item");
85 itemIface->register_property("PrettyName",
86 std::string()); // unused property
87 itemIface->register_property("Present", true);
88 itemIface->initialize();
James Feist2adc95c2019-09-30 14:55:28 -070089 itemAssoc = objectServer.add_interface(
90 "/xyz/openbmc_project/inventory/" + name, association::interface);
James Feistd8bd5622019-06-26 12:09:05 -070091 itemAssoc->register_property(
92 "associations",
93 std::vector<Association>{
94 {"sensors", "inventory",
95 "/xyz/openbmc_project/sensors/fan_tach/" + name}});
96 itemAssoc->initialize();
James Feist60c0ec72019-03-18 15:08:43 -070097 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070098 setInitialProperties(conn);
James Feist6ef20402019-01-07 16:45:08 -080099 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700100 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700101}
102
103TachSensor::~TachSensor()
104{
105 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700106 inputDev.close();
107 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -0700108 objServer.remove_interface(thresholdInterfaceWarning);
109 objServer.remove_interface(thresholdInterfaceCritical);
110 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800111 objServer.remove_interface(association);
James Feist60c0ec72019-03-18 15:08:43 -0700112 objServer.remove_interface(itemIface);
James Feistd8bd5622019-06-26 12:09:05 -0700113 objServer.remove_interface(itemAssoc);
James Feist6714a252018-09-10 15:26:18 -0700114}
115
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700116void TachSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700117{
118 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700119 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -0800120 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700121 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700122}
123
James Feistd8705872019-02-08 13:26:09 -0800124void TachSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700125{
126 if (err == boost::system::errc::bad_file_descriptor)
127 {
128 return; // we're being destroyed
129 }
James Feist7bc2bab2018-10-26 14:09:45 -0700130 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700131 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700132 if (presence)
133 {
134 if (!presence->getValue())
135 {
James Feist1169eb42018-10-31 10:08:47 -0700136 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700137 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700138 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700139 }
James Feist60c0ec72019-03-18 15:08:43 -0700140 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700141 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700142 std::istream responseStream(&readBuf);
James Feist7bc2bab2018-10-26 14:09:45 -0700143 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700144 {
James Feist7bc2bab2018-10-26 14:09:45 -0700145 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700146 {
James Feist7bc2bab2018-10-26 14:09:45 -0700147 std::string response;
148 try
James Feist6714a252018-09-10 15:26:18 -0700149 {
James Feist7bc2bab2018-10-26 14:09:45 -0700150 std::getline(responseStream, response);
Josh Lehan833661a2020-03-04 17:35:41 -0800151 double nvalue = std::stod(response);
James Feist7bc2bab2018-10-26 14:09:45 -0700152 responseStream.clear();
Josh Lehan833661a2020-03-04 17:35:41 -0800153 updateValue(nvalue);
James Feist7bc2bab2018-10-26 14:09:45 -0700154 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700155 }
James Feistd8705872019-02-08 13:26:09 -0800156 catch (const std::invalid_argument&)
James Feist7bc2bab2018-10-26 14:09:45 -0700157 {
158 errCount++;
159 }
James Feist6714a252018-09-10 15:26:18 -0700160 }
161 else
162 {
James Feist71d31b22019-01-02 16:57:54 -0800163 if (!isPowerOn())
James Feist7bc2bab2018-10-26 14:09:45 -0700164 {
James Feist71d31b22019-01-02 16:57:54 -0800165 errCount = 0;
166 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700167 }
168 else
169 {
James Feist71d31b22019-01-02 16:57:54 -0800170 pollTime = sensorFailedPollTimeMs;
171 errCount++;
James Feist7bc2bab2018-10-26 14:09:45 -0700172 }
James Feist6714a252018-09-10 15:26:18 -0700173 }
James Feist71d31b22019-01-02 16:57:54 -0800174 if (errCount >= warnAfterErrorCount)
175 {
176 // only print once
177 if (errCount == warnAfterErrorCount)
178 {
179 std::cerr << "Failure to read sensor " << name << " at " << path
180 << " ec:" << err << "\n";
181 }
182 updateValue(0);
183 }
James Feist6714a252018-09-10 15:26:18 -0700184 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700185 responseStream.clear();
186 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700187 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700188 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700189 {
190 return; // we're no longer valid
191 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700192 inputDev.assign(fd);
James Feist7bc2bab2018-10-26 14:09:45 -0700193 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800194 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700195 if (ec == boost::asio::error::operation_aborted)
196 {
197 return; // we're being canceled
198 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700199 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700200 });
201}
202
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700203void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700204{
James Feist95e54902019-09-04 15:13:36 -0700205 if (!isPowerOn())
206 {
207 return;
208 }
209
James Feistdc6c55f2018-10-31 12:53:20 -0700210 bool status = thresholds::checkThresholds(this);
James Feist95e54902019-09-04 15:13:36 -0700211
James Feist7b18b1e2019-05-14 13:42:09 -0700212 if (redundancy && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700213 {
James Feist7b18b1e2019-05-14 13:42:09 -0700214 (*redundancy)
215 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700216 }
James Feist6714a252018-09-10 15:26:18 -0700217}
218
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800219PresenceSensor::PresenceSensor(const std::string& gpioName, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -0700220 boost::asio::io_service& io,
221 const std::string& name) :
James Feist7bc2bab2018-10-26 14:09:45 -0700222 inverted(inverted),
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800223 gpioLine(gpiod::find_line(gpioName)), gpioFd(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700224{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800225 if (!gpioLine)
James Feist7bc2bab2018-10-26 14:09:45 -0700226 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800227 std::cerr << "Error requesting gpio: " << gpioName << "\n";
228 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700229 return;
230 }
231
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800232 try
James Feist7bc2bab2018-10-26 14:09:45 -0700233 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800234 gpioLine.request({"FanSensor", gpiod::line_request::EVENT_BOTH_EDGES,
235 inverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0});
236 status = gpioLine.get_value();
237
238 int gpioLineFd = gpioLine.event_get_fd();
239 if (gpioLineFd < 0)
240 {
241 std::cerr << "Failed to get " << gpioName << " fd\n";
242 return;
243 }
244
245 gpioFd.assign(gpioLineFd);
246 }
247 catch (std::system_error&)
248 {
249 std::cerr << "Error reading gpio: " << gpioName << "\n";
250 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700251 return;
252 }
James Feist7bc2bab2018-10-26 14:09:45 -0700253
James Feist7bc2bab2018-10-26 14:09:45 -0700254 monitorPresence();
James Feist7bc2bab2018-10-26 14:09:45 -0700255}
256
257PresenceSensor::~PresenceSensor()
258{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800259 gpioFd.close();
260 gpioLine.release();
James Feist7bc2bab2018-10-26 14:09:45 -0700261}
262
263void PresenceSensor::monitorPresence(void)
264{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800265 gpioFd.async_wait(boost::asio::posix::stream_descriptor::wait_read,
266 [this](const boost::system::error_code& ec) {
267 if (ec == boost::system::errc::bad_file_descriptor)
268 {
269 return; // we're being destroyed
270 }
271 else if (ec)
272 {
273 std::cerr << "Error on presence sensor " << name
274 << " \n";
275 ;
276 }
277 else
278 {
279 read();
280 }
281 monitorPresence();
282 });
James Feist7bc2bab2018-10-26 14:09:45 -0700283}
284
285void PresenceSensor::read(void)
286{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800287 gpioLine.event_read();
288 status = gpioLine.get_value();
289 // Read is invoked when an edge event is detected by monitorPresence
290 if (status)
James Feist7bc2bab2018-10-26 14:09:45 -0700291 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800292 logFanInserted(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700293 }
294 else
295 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800296 logFanRemoved(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700297 }
298}
299
300bool PresenceSensor::getValue(void)
301{
302 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700303}
304
James Feist9bb67462019-03-15 15:09:50 -0700305RedundancySensor::RedundancySensor(size_t count,
306 const std::vector<std::string>& children,
307 sdbusplus::asio::object_server& objectServer,
308 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700309 count(count),
310 iface(objectServer.add_interface(
311 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700312 "xyz.openbmc_project.Control.FanRedundancy")),
313 association(objectServer.add_interface(
314 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700315 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700316 objectServer(objectServer)
317{
James Feist9bb67462019-03-15 15:09:50 -0700318 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700319 iface->register_property("Collection", children);
320 iface->register_property("Status", std::string("Full"));
321 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
322 iface->initialize();
323}
324RedundancySensor::~RedundancySensor()
325{
James Feist9bb67462019-03-15 15:09:50 -0700326 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700327 objectServer.remove_interface(iface);
328}
James Feistd8705872019-02-08 13:26:09 -0800329void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700330{
331 statuses[name] = failed;
332 size_t failedCount = 0;
333
James Feist7b18b1e2019-05-14 13:42:09 -0700334 std::string newState = redundancy::full;
James Feistd8705872019-02-08 13:26:09 -0800335 for (const auto& status : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700336 {
337 if (status.second)
338 {
339 failedCount++;
340 }
341 if (failedCount > count)
342 {
James Feist7b18b1e2019-05-14 13:42:09 -0700343 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700344 break;
345 }
346 else if (failedCount)
347 {
James Feist7b18b1e2019-05-14 13:42:09 -0700348 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700349 }
350 }
James Feist7b18b1e2019-05-14 13:42:09 -0700351 if (state != newState)
352 {
353 if (state == redundancy::full)
354 {
355 logFanRedundancyLost();
356 }
357 else if (newState == redundancy::full)
358 {
359 logFanRedundancyRestored();
360 }
361 state = newState;
362 iface->set_property("Status", state);
363 }
James Feistdc6c55f2018-10-31 12:53:20 -0700364}