blob: b03a927bc0b8d3933e53b3d838e8c9de6a11b518 [file] [log] [blame]
Brad Bishopdd62e362017-06-14 16:54:03 -04001/**
2 * Copyright © 2017 IBM 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 */
Matthew Barth2d2caa32020-05-26 11:07:24 -050016#include "tach.hpp"
17
Matt Spinlerc65d91d2021-04-21 13:09:49 -050018#include "logging.hpp"
Matthew Barth2d2caa32020-05-26 11:07:24 -050019#include "rpolicy.hpp"
20
Anwaar Hadiebead9a2025-03-19 20:35:57 +000021#include <phosphor-logging/lg2.hpp>
Matthew Barth2d2caa32020-05-26 11:07:24 -050022
Patrick Williamsfbf47032023-07-17 12:27:34 -050023#include <format>
Brad Bishopdd62e362017-06-14 16:54:03 -040024#include <string>
25#include <tuple>
26#include <vector>
Brad Bishopdd62e362017-06-14 16:54:03 -040027
28namespace phosphor
29{
30namespace fan
31{
32namespace presence
33{
34
35using namespace std::literals::string_literals;
36
37static const auto tachNamespace = "/xyz/openbmc_project/sensors/fan_tach/"s;
38static const auto tachIface = "xyz.openbmc_project.Sensor.Value"s;
39static const auto tachProperty = "Value"s;
40
Matthew Barth2d2caa32020-05-26 11:07:24 -050041Tach::Tach(const std::vector<std::string>& sensors) : currentState(false)
Brad Bishopdd62e362017-06-14 16:54:03 -040042{
43 // Initialize state.
44 for (const auto& s : sensors)
45 {
46 state.emplace_back(s, nullptr, 0);
47 }
48}
49
50bool Tach::start()
51{
52 for (size_t i = 0; i < state.size(); ++i)
53 {
54 auto& s = state[i];
55 auto tachPath = tachNamespace + std::get<std::string>(s);
56
57 // Register for signal callbacks.
Patrick Williamscb356d42022-07-22 19:26:53 -050058 std::get<1>(s) = std::make_unique<sdbusplus::bus::match_t>(
Matthew Barth2d2caa32020-05-26 11:07:24 -050059 util::SDBusPlus::getBus(),
60 sdbusplus::bus::match::rules::propertiesChanged(tachPath,
61 tachIface),
62 [this, i](auto& msg) { this->propertiesChanged(i, msg); });
Brad Bishopdd62e362017-06-14 16:54:03 -040063
64 // Get an initial tach speed.
Brad Bishopd37c0f82017-07-28 21:47:00 -040065 try
66 {
Matthew Barthad2cd242020-08-13 13:36:19 -050067 std::get<double>(s) = util::SDBusPlus::getProperty<double>(
Matthew Barth2d2caa32020-05-26 11:07:24 -050068 tachPath, tachIface, tachProperty);
Brad Bishopd37c0f82017-07-28 21:47:00 -040069 }
Patrick Williamsddb773b2021-10-06 11:24:49 -050070 catch (const std::exception&)
Brad Bishopd37c0f82017-07-28 21:47:00 -040071 {
72 // Assume not spinning.
73
Matthew Barthad2cd242020-08-13 13:36:19 -050074 std::get<double>(s) = 0;
Anwaar Hadiebead9a2025-03-19 20:35:57 +000075 lg2::info("Unable to read fan tach sensor {TACPATH}", "TACPATH",
76 tachPath);
Brad Bishopd37c0f82017-07-28 21:47:00 -040077 }
Brad Bishopdd62e362017-06-14 16:54:03 -040078 }
79
80 // Set the initial state of the sensor.
Matthew Barth2d2caa32020-05-26 11:07:24 -050081 currentState = std::any_of(state.begin(), state.end(), [](const auto& s) {
Matthew Barthad2cd242020-08-13 13:36:19 -050082 return std::get<double>(s) != 0;
Matthew Barth2d2caa32020-05-26 11:07:24 -050083 });
Brad Bishopdd62e362017-06-14 16:54:03 -040084
85 return currentState;
86}
87
88void Tach::stop()
89{
90 for (auto& s : state)
91 {
92 // De-register signal callbacks.
93 std::get<1>(s) = nullptr;
94 }
95}
96
97bool Tach::present()
98{
99 // Live query the tach readings.
Matthew Barthad2cd242020-08-13 13:36:19 -0500100 std::vector<double> values;
Brad Bishopdd62e362017-06-14 16:54:03 -0400101 for (const auto& s : state)
102 {
Matthew Barthad2cd242020-08-13 13:36:19 -0500103 values.push_back(util::SDBusPlus::getProperty<double>(
Matthew Barth2d2caa32020-05-26 11:07:24 -0500104 tachNamespace + std::get<std::string>(s), tachIface, tachProperty));
Brad Bishopdd62e362017-06-14 16:54:03 -0400105 }
106
Matthew Barth2d2caa32020-05-26 11:07:24 -0500107 return std::any_of(values.begin(), values.end(),
108 [](const auto& v) { return v != 0; });
Brad Bishopdd62e362017-06-14 16:54:03 -0400109}
110
Patrick Williamscb356d42022-07-22 19:26:53 -0500111void Tach::propertiesChanged(size_t sensor, sdbusplus::message_t& msg)
Brad Bishopdd62e362017-06-14 16:54:03 -0400112{
113 std::string iface;
Matthew Barthad2cd242020-08-13 13:36:19 -0500114 util::Properties<double> properties;
Brad Bishopdd62e362017-06-14 16:54:03 -0400115 msg.read(iface, properties);
116
117 propertiesChanged(sensor, properties);
118}
119
Matthew Barth2d2caa32020-05-26 11:07:24 -0500120void Tach::propertiesChanged(size_t sensor,
Matthew Barthad2cd242020-08-13 13:36:19 -0500121 const util::Properties<double>& props)
Brad Bishopdd62e362017-06-14 16:54:03 -0400122{
Brad Bishopdd62e362017-06-14 16:54:03 -0400123 // Find the Value property containing the speed.
124 auto it = props.find(tachProperty);
125 if (it != props.end())
126 {
Matthew Barth9f93bd32020-05-28 16:57:14 -0500127 auto& s = state[sensor];
Matthew Barthad2cd242020-08-13 13:36:19 -0500128 std::get<double>(s) = std::get<double>(it->second);
Brad Bishopdd62e362017-06-14 16:54:03 -0400129
Matthew Barth2d2caa32020-05-26 11:07:24 -0500130 auto newState =
Matthew Barthad2cd242020-08-13 13:36:19 -0500131 std::any_of(state.begin(), state.end(),
132 [](const auto& s) { return std::get<double>(s) != 0; });
Brad Bishopdd62e362017-06-14 16:54:03 -0400133
134 if (currentState != newState)
135 {
Brad Bishop11083ec2017-07-25 19:08:53 -0400136 getPolicy().stateChanged(newState, *this);
Brad Bishopdd62e362017-06-14 16:54:03 -0400137 currentState = newState;
138 }
139 }
140}
141
Matt Spinlerc65d91d2021-04-21 13:09:49 -0500142void Tach::logConflict(const std::string& fanInventoryPath) const
143{
Patrick Williamsfbf47032023-07-17 12:27:34 -0500144 getLogger().log(std::format(
Matt Spinlerc65d91d2021-04-21 13:09:49 -0500145 "Tach sensor presence detect for fan {} said not present but "
146 "other methods indicated present",
147 fanInventoryPath));
148
149 // Let the code that monitors fan faults create the event
150 // logs for stopped rotors.
151}
152
Brad Bishopdd62e362017-06-14 16:54:03 -0400153} // namespace presence
154} // namespace fan
155} // namespace phosphor