blob: 41d625b8fbb12a34a2e0006260d95537d9668aa8 [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
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103017#include "TachSensor.hpp"
18
19#include "Utils.hpp"
20
James Feist6714a252018-09-10 15:26:18 -070021#include <unistd.h>
22
James Feist8086aba2020-08-25 16:00:59 -070023#include <boost/asio/read_until.hpp>
Zhikui Ren347dd4e2019-12-12 13:39:50 -080024#include <gpiod.hpp>
James Feist38fb5982020-05-28 10:09:54 -070025#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27
Josh Lehan5170fe62022-08-03 13:17:41 -070028#include <charconv>
James Feist38fb5982020-05-28 10:09:54 -070029#include <fstream>
James Feist6714a252018-09-10 15:26:18 -070030#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <istream>
James Feist6714a252018-09-10 15:26:18 -070032#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <memory>
34#include <optional>
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;
James Feist6714a252018-09-10 15:26:18 -070041
James Feistd8705872019-02-08 13:26:09 -080042TachSensor::TachSensor(const std::string& path, const std::string& objectType,
43 sdbusplus::asio::object_server& objectServer,
44 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist60c0ec72019-03-18 15:08:43 -070045 std::unique_ptr<PresenceSensor>&& presenceSensor,
James Feist7b18b1e2019-05-14 13:42:09 -070046 std::optional<RedundancySensor>* redundancy,
Ed Tanous1f978632023-02-28 18:16:39 -080047 boost::asio::io_context& io, const std::string& fanName,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080048 std::vector<thresholds::Threshold>&& thresholdsIn,
James Feistd8705872019-02-08 13:26:09 -080049 const std::string& sensorConfiguration,
Ed Tanousf69fbf92022-01-14 10:15:33 -080050 const std::pair<double, double>& limits,
James Feist49a8ccd2020-09-16 16:09:52 -070051 const PowerState& powerState,
52 const std::optional<std::string>& ledIn) :
Zhikui Renda98f092021-11-01 09:41:08 -070053 Sensor(escapeName(fanName), std::move(thresholdsIn), sensorConfiguration,
54 objectType, false, false, limits.second, limits.first, conn,
55 powerState),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050056 objServer(objectServer), redundancy(redundancy),
Josh Lehan5170fe62022-08-03 13:17:41 -070057 presence(std::move(presenceSensor)),
58 inputDev(io, path, boost::asio::random_access_file::read_only),
59 waitTimer(io), path(path), led(ledIn)
James Feist6714a252018-09-10 15:26:18 -070060{
James Feist251c7822018-09-12 12:54:15 -070061 sensorInterface = objectServer.add_interface(
62 "/xyz/openbmc_project/sensors/fan_tach/" + name,
63 "xyz.openbmc_project.Sensor.Value");
64
Jayashree Dhanapal56678082022-01-04 17:27:20 +053065 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070066 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053067 std::string interface = thresholds::getInterface(threshold.level);
68 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
69 objectServer.add_interface(
70 "/xyz/openbmc_project/sensors/fan_tach/" + name, interface);
James Feist6714a252018-09-10 15:26:18 -070071 }
James Feist078f2322019-03-08 11:09:05 -080072 association = objectServer.add_interface(
73 "/xyz/openbmc_project/sensors/fan_tach/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070074 association::interface);
James Feist60c0ec72019-03-18 15:08:43 -070075
76 if (presence)
77 {
78 itemIface =
James Feistd8bd5622019-06-26 12:09:05 -070079 objectServer.add_interface("/xyz/openbmc_project/inventory/" + name,
James Feist60c0ec72019-03-18 15:08:43 -070080 "xyz.openbmc_project.Inventory.Item");
81 itemIface->register_property("PrettyName",
82 std::string()); // unused property
83 itemIface->register_property("Present", true);
84 itemIface->initialize();
James Feist2adc95c2019-09-30 14:55:28 -070085 itemAssoc = objectServer.add_interface(
86 "/xyz/openbmc_project/inventory/" + name, association::interface);
James Feistd8bd5622019-06-26 12:09:05 -070087 itemAssoc->register_property(
88 "associations",
89 std::vector<Association>{
90 {"sensors", "inventory",
91 "/xyz/openbmc_project/sensors/fan_tach/" + name}});
92 itemAssoc->initialize();
James Feist60c0ec72019-03-18 15:08:43 -070093 }
Andrei Kartashev39287412022-02-04 16:04:47 +030094 setInitialProperties(sensor_paths::unitRPMs);
James Feist6714a252018-09-10 15:26:18 -070095}
96
97TachSensor::~TachSensor()
98{
99 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700100 inputDev.close();
101 waitTimer.cancel();
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530102 for (const auto& iface : thresholdInterfaces)
103 {
104 objServer.remove_interface(iface);
105 }
James Feist251c7822018-09-12 12:54:15 -0700106 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800107 objServer.remove_interface(association);
James Feist60c0ec72019-03-18 15:08:43 -0700108 objServer.remove_interface(itemIface);
James Feistd8bd5622019-06-26 12:09:05 -0700109 objServer.remove_interface(itemAssoc);
James Feist6714a252018-09-10 15:26:18 -0700110}
111
Josh Lehan5170fe62022-08-03 13:17:41 -0700112void TachSensor::setupRead()
James Feist6714a252018-09-10 15:26:18 -0700113{
Josh Lehan5170fe62022-08-03 13:17:41 -0700114 std::weak_ptr<TachSensor> weakRef = weak_from_this();
115 inputDev.async_read_some_at(
116 0, boost::asio::buffer(readBuf),
117 [weakRef](const boost::system::error_code& ec, std::size_t bytesRead) {
118 std::shared_ptr<TachSensor> self = weakRef.lock();
119 if (self)
120 {
121 self->handleResponse(ec, bytesRead);
122 }
123 });
124}
125
126void TachSensor::restartRead(size_t pollTime)
127{
128 std::weak_ptr<TachSensor> weakRef = weak_from_this();
Ed Tanous83db50c2023-03-01 10:20:24 -0800129 waitTimer.expires_after(std::chrono::milliseconds(pollTime));
Josh Lehan5170fe62022-08-03 13:17:41 -0700130 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
131 if (ec == boost::asio::error::operation_aborted)
132 {
133 return; // we're being canceled
134 }
135 std::shared_ptr<TachSensor> self = weakRef.lock();
136 if (!self)
137 {
138 return;
139 }
140 self->setupRead();
Ed Tanousbb679322022-05-16 16:10:00 -0700141 });
James Feist6714a252018-09-10 15:26:18 -0700142}
143
Josh Lehan5170fe62022-08-03 13:17:41 -0700144void TachSensor::handleResponse(const boost::system::error_code& err,
145 size_t bytesRead)
James Feist6714a252018-09-10 15:26:18 -0700146{
Josh Lehan5170fe62022-08-03 13:17:41 -0700147 if ((err == boost::system::errc::bad_file_descriptor) ||
148 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700149 {
Josh Lehan5170fe62022-08-03 13:17:41 -0700150 std::cerr << "TachSensor " << name << " removed " << path << "\n";
James Feist6714a252018-09-10 15:26:18 -0700151 return; // we're being destroyed
152 }
James Feist7bc2bab2018-10-26 14:09:45 -0700153 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700154 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700155 if (presence)
156 {
157 if (!presence->getValue())
158 {
James Feist961bf092020-07-01 16:38:12 -0700159 markAvailable(false);
James Feist7bc2bab2018-10-26 14:09:45 -0700160 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700161 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700162 }
James Feist60c0ec72019-03-18 15:08:43 -0700163 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700164 }
Josh Lehan5170fe62022-08-03 13:17:41 -0700165
James Feist7bc2bab2018-10-26 14:09:45 -0700166 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700167 {
James Feist7bc2bab2018-10-26 14:09:45 -0700168 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700169 {
Josh Lehan5170fe62022-08-03 13:17:41 -0700170 const char* bufEnd = readBuf.data() + bytesRead;
171 int nvalue = 0;
Patrick Williams779c96a2023-05-10 07:50:42 -0500172 std::from_chars_result ret = std::from_chars(readBuf.data(), bufEnd,
173 nvalue);
Josh Lehan5170fe62022-08-03 13:17:41 -0700174 if (ret.ec != std::errc())
James Feist7bc2bab2018-10-26 14:09:45 -0700175 {
James Feist961bf092020-07-01 16:38:12 -0700176 incrementError();
177 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700178 }
Josh Lehan5170fe62022-08-03 13:17:41 -0700179 else
180 {
181 updateValue(nvalue);
182 }
James Feist6714a252018-09-10 15:26:18 -0700183 }
184 else
185 {
James Feist961bf092020-07-01 16:38:12 -0700186 incrementError();
187 pollTime = sensorFailedPollTimeMs;
James Feist71d31b22019-01-02 16:57:54 -0800188 }
James Feist6714a252018-09-10 15:26:18 -0700189 }
Ed Tanous99c44092022-01-14 09:59:09 -0800190
Josh Lehan5170fe62022-08-03 13:17:41 -0700191 restartRead(pollTime);
James Feist6714a252018-09-10 15:26:18 -0700192}
193
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700194void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700195{
Zhikui Ren00bd56d2021-11-19 12:32:27 -0800196 bool status = thresholds::checkThresholds(this);
James Feist95e54902019-09-04 15:13:36 -0700197
Ed Tanous2049bd22022-07-09 07:20:26 -0700198 if ((redundancy != nullptr) && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700199 {
James Feist7b18b1e2019-05-14 13:42:09 -0700200 (*redundancy)
201 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700202 }
James Feist49a8ccd2020-09-16 16:09:52 -0700203
204 bool curLed = !status;
205 if (led && ledState != curLed)
206 {
207 ledState = curLed;
208 setLed(dbusConnection, *led, curLed);
209 }
James Feist6714a252018-09-10 15:26:18 -0700210}
211
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800212PresenceSensor::PresenceSensor(const std::string& gpioName, bool inverted,
Ed Tanous1f978632023-02-28 18:16:39 -0800213 boost::asio::io_context& io,
James Feist7b18b1e2019-05-14 13:42:09 -0700214 const std::string& name) :
Ed Tanous8a17c302021-09-02 15:07:11 -0700215 gpioLine(gpiod::find_line(gpioName)),
216 gpioFd(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700217{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800218 if (!gpioLine)
James Feist7bc2bab2018-10-26 14:09:45 -0700219 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800220 std::cerr << "Error requesting gpio: " << gpioName << "\n";
221 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700222 return;
223 }
224
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800225 try
James Feist7bc2bab2018-10-26 14:09:45 -0700226 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800227 gpioLine.request({"FanSensor", gpiod::line_request::EVENT_BOTH_EDGES,
228 inverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0});
Ed Tanous2049bd22022-07-09 07:20:26 -0700229 status = (gpioLine.get_value() != 0);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800230
231 int gpioLineFd = gpioLine.event_get_fd();
232 if (gpioLineFd < 0)
233 {
234 std::cerr << "Failed to get " << gpioName << " fd\n";
235 return;
236 }
237
238 gpioFd.assign(gpioLineFd);
239 }
Patrick Williams26601e82021-10-06 12:43:25 -0500240 catch (const std::system_error&)
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800241 {
242 std::cerr << "Error reading gpio: " << gpioName << "\n";
243 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700244 return;
245 }
James Feist7bc2bab2018-10-26 14:09:45 -0700246
James Feist7bc2bab2018-10-26 14:09:45 -0700247 monitorPresence();
James Feist7bc2bab2018-10-26 14:09:45 -0700248}
249
250PresenceSensor::~PresenceSensor()
251{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800252 gpioFd.close();
253 gpioLine.release();
James Feist7bc2bab2018-10-26 14:09:45 -0700254}
255
256void PresenceSensor::monitorPresence(void)
257{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800258 gpioFd.async_wait(boost::asio::posix::stream_descriptor::wait_read,
259 [this](const boost::system::error_code& ec) {
Ed Tanousbb679322022-05-16 16:10:00 -0700260 if (ec == boost::system::errc::bad_file_descriptor)
261 {
262 return; // we're being destroyed
263 }
264 if (ec)
265 {
266 std::cerr << "Error on presence sensor " << name << " \n";
267 ;
268 }
269 else
270 {
271 read();
272 }
273 monitorPresence();
274 });
James Feist7bc2bab2018-10-26 14:09:45 -0700275}
276
277void PresenceSensor::read(void)
278{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800279 gpioLine.event_read();
Ed Tanous2049bd22022-07-09 07:20:26 -0700280 status = (gpioLine.get_value() != 0);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800281 // Read is invoked when an edge event is detected by monitorPresence
282 if (status)
James Feist7bc2bab2018-10-26 14:09:45 -0700283 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800284 logFanInserted(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700285 }
286 else
287 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800288 logFanRemoved(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700289 }
290}
291
Ed Tanous2049bd22022-07-09 07:20:26 -0700292bool PresenceSensor::getValue(void) const
James Feist7bc2bab2018-10-26 14:09:45 -0700293{
294 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700295}
296
James Feist9bb67462019-03-15 15:09:50 -0700297RedundancySensor::RedundancySensor(size_t count,
298 const std::vector<std::string>& children,
299 sdbusplus::asio::object_server& objectServer,
300 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700301 count(count),
302 iface(objectServer.add_interface(
303 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700304 "xyz.openbmc_project.Control.FanRedundancy")),
305 association(objectServer.add_interface(
306 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700307 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700308 objectServer(objectServer)
309{
James Feist9bb67462019-03-15 15:09:50 -0700310 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700311 iface->register_property("Collection", children);
312 iface->register_property("Status", std::string("Full"));
313 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
314 iface->initialize();
315}
316RedundancySensor::~RedundancySensor()
317{
James Feist9bb67462019-03-15 15:09:50 -0700318 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700319 objectServer.remove_interface(iface);
320}
James Feistd8705872019-02-08 13:26:09 -0800321void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700322{
323 statuses[name] = failed;
324 size_t failedCount = 0;
325
James Feist7b18b1e2019-05-14 13:42:09 -0700326 std::string newState = redundancy::full;
Zev Weiss0521a062022-08-12 18:21:02 -0700327 for (const auto& [name, status] : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700328 {
Zev Weiss0521a062022-08-12 18:21:02 -0700329 if (status)
James Feistdc6c55f2018-10-31 12:53:20 -0700330 {
331 failedCount++;
332 }
333 if (failedCount > count)
334 {
James Feist7b18b1e2019-05-14 13:42:09 -0700335 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700336 break;
337 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700338 if (failedCount != 0U)
James Feistdc6c55f2018-10-31 12:53:20 -0700339 {
James Feist7b18b1e2019-05-14 13:42:09 -0700340 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700341 }
342 }
James Feist7b18b1e2019-05-14 13:42:09 -0700343 if (state != newState)
344 {
345 if (state == redundancy::full)
346 {
347 logFanRedundancyLost();
348 }
349 else if (newState == redundancy::full)
350 {
351 logFanRedundancyRestored();
352 }
353 state = newState;
354 iface->set_property("Status", state);
355 }
James Feistdc6c55f2018-10-31 12:53:20 -0700356}