blob: 10a34a492f01828af78414569a0b4584bafedadb [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
Chris Cain83929002024-03-06 14:20:09 -060019#include "PresenceGpio.hpp"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070020#include "SensorPaths.hpp"
21#include "Thresholds.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103022#include "Utils.hpp"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070023#include "sensor.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103024
Ed Tanouseacbfdd2024-04-04 12:00:24 -070025#include <boost/asio/buffer.hpp>
26#include <boost/asio/error.hpp>
27#include <boost/asio/io_context.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070028#include <boost/asio/random_access_file.hpp>
James Feist38fb5982020-05-28 10:09:54 -070029#include <sdbusplus/asio/connection.hpp>
30#include <sdbusplus/asio/object_server.hpp>
31
Josh Lehan5170fe62022-08-03 13:17:41 -070032#include <charconv>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070033#include <chrono>
34#include <cstddef>
35#include <cstdint>
James Feist6714a252018-09-10 15:26:18 -070036#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <memory>
38#include <optional>
James Feist6714a252018-09-10 15:26:18 -070039#include <string>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070040#include <system_error>
Patrick Venture96e97db2019-10-31 13:44:38 -070041#include <utility>
42#include <vector>
James Feist6714a252018-09-10 15:26:18 -070043
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070044static constexpr unsigned int pwmPollMs = 500;
James Feist6714a252018-09-10 15:26:18 -070045
Patrick Williams2aaf7172024-08-16 15:20:40 -040046TachSensor::TachSensor(
47 const std::string& path, const std::string& objectType,
48 sdbusplus::asio::object_server& objectServer,
49 std::shared_ptr<sdbusplus::asio::connection>& conn,
Chris Cain83929002024-03-06 14:20:09 -060050 std::shared_ptr<PresenceGpio>& presenceGpio,
Patrick Williams2aaf7172024-08-16 15:20:40 -040051 std::optional<RedundancySensor>* redundancy, boost::asio::io_context& io,
52 const std::string& fanName,
53 std::vector<thresholds::Threshold>&& thresholdsIn,
54 const std::string& sensorConfiguration,
55 const std::pair<double, double>& limits, const PowerState& powerState,
56 const std::optional<std::string>& ledIn) :
Zhikui Renda98f092021-11-01 09:41:08 -070057 Sensor(escapeName(fanName), std::move(thresholdsIn), sensorConfiguration,
58 objectType, false, false, limits.second, limits.first, conn,
59 powerState),
Chris Cain83929002024-03-06 14:20:09 -060060 objServer(objectServer), redundancy(redundancy), presence(presenceGpio),
Josh Lehan5170fe62022-08-03 13:17:41 -070061 inputDev(io, path, boost::asio::random_access_file::read_only),
62 waitTimer(io), path(path), 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
Jayashree Dhanapal56678082022-01-04 17:27:20 +053068 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070069 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053070 std::string interface = thresholds::getInterface(threshold.level);
71 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
72 objectServer.add_interface(
73 "/xyz/openbmc_project/sensors/fan_tach/" + name, interface);
James Feist6714a252018-09-10 15:26:18 -070074 }
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 {
Chris Cainc45e18f2024-07-24 15:58:00 -050081 presence->monitorPresence();
James Feist60c0ec72019-03-18 15:08:43 -070082 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(
Glukhov Mikhail6e6561d2024-01-10 11:58:48 +030092 "Associations",
James Feistd8bd5622019-06-26 12:09:05 -070093 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 }
Andrei Kartashev39287412022-02-04 16:04:47 +030098 setInitialProperties(sensor_paths::unitRPMs);
James Feist6714a252018-09-10 15:26:18 -070099}
100
101TachSensor::~TachSensor()
102{
103 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700104 inputDev.close();
105 waitTimer.cancel();
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530106 for (const auto& iface : thresholdInterfaces)
107 {
108 objServer.remove_interface(iface);
109 }
James Feist251c7822018-09-12 12:54:15 -0700110 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
Josh Lehan5170fe62022-08-03 13:17:41 -0700116void TachSensor::setupRead()
James Feist6714a252018-09-10 15:26:18 -0700117{
Josh Lehan5170fe62022-08-03 13:17:41 -0700118 std::weak_ptr<TachSensor> weakRef = weak_from_this();
119 inputDev.async_read_some_at(
120 0, boost::asio::buffer(readBuf),
121 [weakRef](const boost::system::error_code& ec, std::size_t bytesRead) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400122 std::shared_ptr<TachSensor> self = weakRef.lock();
123 if (self)
124 {
125 self->handleResponse(ec, bytesRead);
126 }
127 });
Josh Lehan5170fe62022-08-03 13:17:41 -0700128}
129
130void TachSensor::restartRead(size_t pollTime)
131{
132 std::weak_ptr<TachSensor> weakRef = weak_from_this();
Ed Tanous83db50c2023-03-01 10:20:24 -0800133 waitTimer.expires_after(std::chrono::milliseconds(pollTime));
Josh Lehan5170fe62022-08-03 13:17:41 -0700134 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
135 if (ec == boost::asio::error::operation_aborted)
136 {
137 return; // we're being canceled
138 }
139 std::shared_ptr<TachSensor> self = weakRef.lock();
140 if (!self)
141 {
142 return;
143 }
144 self->setupRead();
Ed Tanousbb679322022-05-16 16:10:00 -0700145 });
James Feist6714a252018-09-10 15:26:18 -0700146}
147
Josh Lehan5170fe62022-08-03 13:17:41 -0700148void TachSensor::handleResponse(const boost::system::error_code& err,
149 size_t bytesRead)
James Feist6714a252018-09-10 15:26:18 -0700150{
Josh Lehan5170fe62022-08-03 13:17:41 -0700151 if ((err == boost::system::errc::bad_file_descriptor) ||
152 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700153 {
Josh Lehan5170fe62022-08-03 13:17:41 -0700154 std::cerr << "TachSensor " << name << " removed " << path << "\n";
James Feist6714a252018-09-10 15:26:18 -0700155 return; // we're being destroyed
156 }
James Feist7bc2bab2018-10-26 14:09:45 -0700157 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700158 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700159 if (presence)
160 {
Chris Cain83929002024-03-06 14:20:09 -0600161 if (!presence->isPresent())
James Feist7bc2bab2018-10-26 14:09:45 -0700162 {
James Feist961bf092020-07-01 16:38:12 -0700163 markAvailable(false);
James Feist7bc2bab2018-10-26 14:09:45 -0700164 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700165 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700166 }
James Feist60c0ec72019-03-18 15:08:43 -0700167 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700168 }
Josh Lehan5170fe62022-08-03 13:17:41 -0700169
James Feist7bc2bab2018-10-26 14:09:45 -0700170 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700171 {
James Feist7bc2bab2018-10-26 14:09:45 -0700172 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700173 {
Josh Lehan5170fe62022-08-03 13:17:41 -0700174 const char* bufEnd = readBuf.data() + bytesRead;
175 int nvalue = 0;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400176 std::from_chars_result ret =
177 std::from_chars(readBuf.data(), bufEnd, nvalue);
Josh Lehan5170fe62022-08-03 13:17:41 -0700178 if (ret.ec != std::errc())
James Feist7bc2bab2018-10-26 14:09:45 -0700179 {
James Feist961bf092020-07-01 16:38:12 -0700180 incrementError();
181 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700182 }
Josh Lehan5170fe62022-08-03 13:17:41 -0700183 else
184 {
185 updateValue(nvalue);
186 }
James Feist6714a252018-09-10 15:26:18 -0700187 }
188 else
189 {
James Feist961bf092020-07-01 16:38:12 -0700190 incrementError();
191 pollTime = sensorFailedPollTimeMs;
James Feist71d31b22019-01-02 16:57:54 -0800192 }
James Feist6714a252018-09-10 15:26:18 -0700193 }
Ed Tanous99c44092022-01-14 09:59:09 -0800194
Josh Lehan5170fe62022-08-03 13:17:41 -0700195 restartRead(pollTime);
James Feist6714a252018-09-10 15:26:18 -0700196}
197
Ed Tanous201a1012024-04-03 18:07:28 -0700198void TachSensor::checkThresholds()
James Feist6714a252018-09-10 15:26:18 -0700199{
Zhikui Ren00bd56d2021-11-19 12:32:27 -0800200 bool status = thresholds::checkThresholds(this);
James Feist95e54902019-09-04 15:13:36 -0700201
Ed Tanous2049bd22022-07-09 07:20:26 -0700202 if ((redundancy != nullptr) && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700203 {
James Feist7b18b1e2019-05-14 13:42:09 -0700204 (*redundancy)
205 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700206 }
James Feist49a8ccd2020-09-16 16:09:52 -0700207
208 bool curLed = !status;
209 if (led && ledState != curLed)
210 {
211 ledState = curLed;
212 setLed(dbusConnection, *led, curLed);
213 }
James Feist6714a252018-09-10 15:26:18 -0700214}
215
James Feist9bb67462019-03-15 15:09:50 -0700216RedundancySensor::RedundancySensor(size_t count,
217 const std::vector<std::string>& children,
218 sdbusplus::asio::object_server& objectServer,
219 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700220 count(count),
221 iface(objectServer.add_interface(
222 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700223 "xyz.openbmc_project.Control.FanRedundancy")),
224 association(objectServer.add_interface(
225 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700226 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700227 objectServer(objectServer)
228{
James Feist9bb67462019-03-15 15:09:50 -0700229 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700230 iface->register_property("Collection", children);
231 iface->register_property("Status", std::string("Full"));
232 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
233 iface->initialize();
234}
235RedundancySensor::~RedundancySensor()
236{
James Feist9bb67462019-03-15 15:09:50 -0700237 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700238 objectServer.remove_interface(iface);
239}
James Feistd8705872019-02-08 13:26:09 -0800240void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700241{
242 statuses[name] = failed;
243 size_t failedCount = 0;
244
James Feist7b18b1e2019-05-14 13:42:09 -0700245 std::string newState = redundancy::full;
Zev Weiss0521a062022-08-12 18:21:02 -0700246 for (const auto& [name, status] : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700247 {
Zev Weiss0521a062022-08-12 18:21:02 -0700248 if (status)
James Feistdc6c55f2018-10-31 12:53:20 -0700249 {
250 failedCount++;
251 }
252 if (failedCount > count)
253 {
James Feist7b18b1e2019-05-14 13:42:09 -0700254 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700255 break;
256 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700257 if (failedCount != 0U)
James Feistdc6c55f2018-10-31 12:53:20 -0700258 {
James Feist7b18b1e2019-05-14 13:42:09 -0700259 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700260 }
261 }
James Feist7b18b1e2019-05-14 13:42:09 -0700262 if (state != newState)
263 {
264 if (state == redundancy::full)
265 {
266 logFanRedundancyLost();
267 }
268 else if (newState == redundancy::full)
269 {
270 logFanRedundancyRestored();
271 }
272 state = newState;
273 iface->set_property("Status", state);
274 }
James Feistdc6c55f2018-10-31 12:53:20 -0700275}