blob: 0490135c59c9eb057822e5efd818ebd326311217 [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>
James Feist8086aba2020-08-25 16:00:59 -070025#include <boost/asio/read_until.hpp>
James Feist6714a252018-09-10 15:26:18 -070026#include <boost/date_time/posix_time/posix_time.hpp>
Zhikui Ren347dd4e2019-12-12 13:39:50 -080027#include <gpiod.hpp>
James Feist38fb5982020-05-28 10:09:54 -070028#include <sdbusplus/asio/connection.hpp>
29#include <sdbusplus/asio/object_server.hpp>
30
31#include <fstream>
James Feist6714a252018-09-10 15:26:18 -070032#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <istream>
James Feist6714a252018-09-10 15:26:18 -070034#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <memory>
36#include <optional>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070038#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070039#include <utility>
40#include <vector>
James Feist6714a252018-09-10 15:26:18 -070041
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070042static constexpr unsigned int pwmPollMs = 500;
43static constexpr size_t warnAfterErrorCount = 10;
James Feist6714a252018-09-10 15:26:18 -070044
James Feistd8705872019-02-08 13:26:09 -080045TachSensor::TachSensor(const std::string& path, const std::string& objectType,
46 sdbusplus::asio::object_server& objectServer,
47 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist60c0ec72019-03-18 15:08:43 -070048 std::unique_ptr<PresenceSensor>&& presenceSensor,
James Feist7b18b1e2019-05-14 13:42:09 -070049 std::optional<RedundancySensor>* redundancy,
James Feistd8705872019-02-08 13:26:09 -080050 boost::asio::io_service& io, const std::string& fanName,
51 std::vector<thresholds::Threshold>&& _thresholds,
52 const std::string& sensorConfiguration,
Josh Lehanf920e092020-08-07 00:12:54 -070053 const std::pair<size_t, size_t>& limits,
James Feist49a8ccd2020-09-16 16:09:52 -070054 const PowerState& powerState,
55 const std::optional<std::string>& ledIn) :
James Feist930fcde2019-05-28 12:58:43 -070056 Sensor(boost::replace_all_copy(fanName, " ", "_"), std::move(_thresholds),
James Feiste3338522020-09-15 15:40:30 -070057 sensorConfiguration, objectType, limits.second, limits.first, conn,
Josh Lehanf920e092020-08-07 00:12:54 -070058 powerState),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050059 objServer(objectServer), redundancy(redundancy),
60 presence(std::move(presenceSensor)),
James Feist49a8ccd2020-09-16 16:09:52 -070061 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
62 led(ledIn)
James Feist6714a252018-09-10 15:26:18 -070063{
James Feist251c7822018-09-12 12:54:15 -070064 sensorInterface = objectServer.add_interface(
65 "/xyz/openbmc_project/sensors/fan_tach/" + name,
66 "xyz.openbmc_project.Sensor.Value");
67
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070068 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070069 {
James Feist251c7822018-09-12 12:54:15 -070070 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070071 "/xyz/openbmc_project/sensors/fan_tach/" + name,
72 "xyz.openbmc_project.Sensor.Threshold.Warning");
73 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070074 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070075 {
James Feist251c7822018-09-12 12:54:15 -070076 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070077 "/xyz/openbmc_project/sensors/fan_tach/" + name,
78 "xyz.openbmc_project.Sensor.Threshold.Critical");
79 }
James Feist078f2322019-03-08 11:09:05 -080080 association = objectServer.add_interface(
81 "/xyz/openbmc_project/sensors/fan_tach/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070082 association::interface);
James Feist60c0ec72019-03-18 15:08:43 -070083
84 if (presence)
85 {
86 itemIface =
James Feistd8bd5622019-06-26 12:09:05 -070087 objectServer.add_interface("/xyz/openbmc_project/inventory/" + name,
James Feist60c0ec72019-03-18 15:08:43 -070088 "xyz.openbmc_project.Inventory.Item");
89 itemIface->register_property("PrettyName",
90 std::string()); // unused property
91 itemIface->register_property("Present", true);
92 itemIface->initialize();
James Feist2adc95c2019-09-30 14:55:28 -070093 itemAssoc = objectServer.add_interface(
94 "/xyz/openbmc_project/inventory/" + name, association::interface);
James Feistd8bd5622019-06-26 12:09:05 -070095 itemAssoc->register_property(
96 "associations",
97 std::vector<Association>{
98 {"sensors", "inventory",
99 "/xyz/openbmc_project/sensors/fan_tach/" + name}});
100 itemAssoc->initialize();
James Feist60c0ec72019-03-18 15:08:43 -0700101 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700102 setInitialProperties(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700103 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700104}
105
106TachSensor::~TachSensor()
107{
108 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700109 inputDev.close();
110 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -0700111 objServer.remove_interface(thresholdInterfaceWarning);
112 objServer.remove_interface(thresholdInterfaceCritical);
113 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800114 objServer.remove_interface(association);
James Feist60c0ec72019-03-18 15:08:43 -0700115 objServer.remove_interface(itemIface);
James Feistd8bd5622019-06-26 12:09:05 -0700116 objServer.remove_interface(itemAssoc);
James Feist6714a252018-09-10 15:26:18 -0700117}
118
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700119void TachSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700120{
121 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700122 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -0800123 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700124 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700125}
126
James Feistd8705872019-02-08 13:26:09 -0800127void TachSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700128{
129 if (err == boost::system::errc::bad_file_descriptor)
130 {
131 return; // we're being destroyed
132 }
James Feist7bc2bab2018-10-26 14:09:45 -0700133 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700134 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700135 if (presence)
136 {
137 if (!presence->getValue())
138 {
James Feist961bf092020-07-01 16:38:12 -0700139 markAvailable(false);
James Feist7bc2bab2018-10-26 14:09:45 -0700140 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700141 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700142 }
James Feist60c0ec72019-03-18 15:08:43 -0700143 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700144 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700145 std::istream responseStream(&readBuf);
James Feist7bc2bab2018-10-26 14:09:45 -0700146 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700147 {
James Feist7bc2bab2018-10-26 14:09:45 -0700148 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700149 {
James Feist7bc2bab2018-10-26 14:09:45 -0700150 std::string response;
151 try
James Feist6714a252018-09-10 15:26:18 -0700152 {
James Feist7bc2bab2018-10-26 14:09:45 -0700153 std::getline(responseStream, response);
Zhikui Rend3da1282020-09-11 17:02:01 -0700154 rawValue = std::stod(response);
James Feist7bc2bab2018-10-26 14:09:45 -0700155 responseStream.clear();
Zhikui Rend3da1282020-09-11 17:02:01 -0700156 updateValue(rawValue);
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 {
James Feist961bf092020-07-01 16:38:12 -0700160 incrementError();
161 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700162 }
James Feist6714a252018-09-10 15:26:18 -0700163 }
164 else
165 {
James Feist961bf092020-07-01 16:38:12 -0700166 incrementError();
167 pollTime = sensorFailedPollTimeMs;
James Feist71d31b22019-01-02 16:57:54 -0800168 }
James Feist6714a252018-09-10 15:26:18 -0700169 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700170 responseStream.clear();
171 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700172 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700173 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700174 {
175 return; // we're no longer valid
176 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700177 inputDev.assign(fd);
James Feist7bc2bab2018-10-26 14:09:45 -0700178 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800179 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700180 if (ec == boost::asio::error::operation_aborted)
181 {
182 return; // we're being canceled
183 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700184 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700185 });
186}
187
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700188void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700189{
James Feistdc6c55f2018-10-31 12:53:20 -0700190 bool status = thresholds::checkThresholds(this);
James Feist95e54902019-09-04 15:13:36 -0700191
James Feist7b18b1e2019-05-14 13:42:09 -0700192 if (redundancy && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700193 {
James Feist7b18b1e2019-05-14 13:42:09 -0700194 (*redundancy)
195 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700196 }
James Feist49a8ccd2020-09-16 16:09:52 -0700197
198 bool curLed = !status;
199 if (led && ledState != curLed)
200 {
201 ledState = curLed;
202 setLed(dbusConnection, *led, curLed);
203 }
James Feist6714a252018-09-10 15:26:18 -0700204}
205
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800206PresenceSensor::PresenceSensor(const std::string& gpioName, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -0700207 boost::asio::io_service& io,
208 const std::string& name) :
James Feist7bc2bab2018-10-26 14:09:45 -0700209 inverted(inverted),
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800210 gpioLine(gpiod::find_line(gpioName)), gpioFd(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700211{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800212 if (!gpioLine)
James Feist7bc2bab2018-10-26 14:09:45 -0700213 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800214 std::cerr << "Error requesting gpio: " << gpioName << "\n";
215 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700216 return;
217 }
218
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800219 try
James Feist7bc2bab2018-10-26 14:09:45 -0700220 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800221 gpioLine.request({"FanSensor", gpiod::line_request::EVENT_BOTH_EDGES,
222 inverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0});
223 status = gpioLine.get_value();
224
225 int gpioLineFd = gpioLine.event_get_fd();
226 if (gpioLineFd < 0)
227 {
228 std::cerr << "Failed to get " << gpioName << " fd\n";
229 return;
230 }
231
232 gpioFd.assign(gpioLineFd);
233 }
234 catch (std::system_error&)
235 {
236 std::cerr << "Error reading gpio: " << gpioName << "\n";
237 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700238 return;
239 }
James Feist7bc2bab2018-10-26 14:09:45 -0700240
James Feist7bc2bab2018-10-26 14:09:45 -0700241 monitorPresence();
James Feist7bc2bab2018-10-26 14:09:45 -0700242}
243
244PresenceSensor::~PresenceSensor()
245{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800246 gpioFd.close();
247 gpioLine.release();
James Feist7bc2bab2018-10-26 14:09:45 -0700248}
249
250void PresenceSensor::monitorPresence(void)
251{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800252 gpioFd.async_wait(boost::asio::posix::stream_descriptor::wait_read,
253 [this](const boost::system::error_code& ec) {
254 if (ec == boost::system::errc::bad_file_descriptor)
255 {
256 return; // we're being destroyed
257 }
258 else if (ec)
259 {
260 std::cerr << "Error on presence sensor " << name
261 << " \n";
262 ;
263 }
264 else
265 {
266 read();
267 }
268 monitorPresence();
269 });
James Feist7bc2bab2018-10-26 14:09:45 -0700270}
271
272void PresenceSensor::read(void)
273{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800274 gpioLine.event_read();
275 status = gpioLine.get_value();
276 // Read is invoked when an edge event is detected by monitorPresence
277 if (status)
James Feist7bc2bab2018-10-26 14:09:45 -0700278 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800279 logFanInserted(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700280 }
281 else
282 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800283 logFanRemoved(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700284 }
285}
286
287bool PresenceSensor::getValue(void)
288{
289 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700290}
291
James Feist9bb67462019-03-15 15:09:50 -0700292RedundancySensor::RedundancySensor(size_t count,
293 const std::vector<std::string>& children,
294 sdbusplus::asio::object_server& objectServer,
295 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700296 count(count),
297 iface(objectServer.add_interface(
298 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700299 "xyz.openbmc_project.Control.FanRedundancy")),
300 association(objectServer.add_interface(
301 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700302 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700303 objectServer(objectServer)
304{
James Feist9bb67462019-03-15 15:09:50 -0700305 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700306 iface->register_property("Collection", children);
307 iface->register_property("Status", std::string("Full"));
308 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
309 iface->initialize();
310}
311RedundancySensor::~RedundancySensor()
312{
James Feist9bb67462019-03-15 15:09:50 -0700313 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700314 objectServer.remove_interface(iface);
315}
James Feistd8705872019-02-08 13:26:09 -0800316void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700317{
318 statuses[name] = failed;
319 size_t failedCount = 0;
320
James Feist7b18b1e2019-05-14 13:42:09 -0700321 std::string newState = redundancy::full;
James Feistd8705872019-02-08 13:26:09 -0800322 for (const auto& status : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700323 {
324 if (status.second)
325 {
326 failedCount++;
327 }
328 if (failedCount > count)
329 {
James Feist7b18b1e2019-05-14 13:42:09 -0700330 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700331 break;
332 }
333 else if (failedCount)
334 {
James Feist7b18b1e2019-05-14 13:42:09 -0700335 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700336 }
337 }
James Feist7b18b1e2019-05-14 13:42:09 -0700338 if (state != newState)
339 {
340 if (state == redundancy::full)
341 {
342 logFanRedundancyLost();
343 }
344 else if (newState == redundancy::full)
345 {
346 logFanRedundancyRestored();
347 }
348 state = newState;
349 iface->set_property("Status", state);
350 }
James Feistdc6c55f2018-10-31 12:53:20 -0700351}