blob: 9f6e10546c386d276620c48f3aedd09f59f32074 [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
Brad Bishopd37c0f82017-07-28 21:47:00 -040021#include <phosphor-logging/log.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
Brad Bishopd37c0f82017-07-28 21:47:00 -040035using namespace phosphor::logging;
Brad Bishopdd62e362017-06-14 16:54:03 -040036using namespace std::literals::string_literals;
37
38static const auto tachNamespace = "/xyz/openbmc_project/sensors/fan_tach/"s;
39static const auto tachIface = "xyz.openbmc_project.Sensor.Value"s;
40static const auto tachProperty = "Value"s;
41
Matthew Barth2d2caa32020-05-26 11:07:24 -050042Tach::Tach(const std::vector<std::string>& sensors) : currentState(false)
Brad Bishopdd62e362017-06-14 16:54:03 -040043{
44 // Initialize state.
45 for (const auto& s : sensors)
46 {
47 state.emplace_back(s, nullptr, 0);
48 }
49}
50
51bool Tach::start()
52{
53 for (size_t i = 0; i < state.size(); ++i)
54 {
55 auto& s = state[i];
56 auto tachPath = tachNamespace + std::get<std::string>(s);
57
58 // Register for signal callbacks.
Patrick Williamscb356d42022-07-22 19:26:53 -050059 std::get<1>(s) = std::make_unique<sdbusplus::bus::match_t>(
Matthew Barth2d2caa32020-05-26 11:07:24 -050060 util::SDBusPlus::getBus(),
61 sdbusplus::bus::match::rules::propertiesChanged(tachPath,
62 tachIface),
63 [this, i](auto& msg) { this->propertiesChanged(i, msg); });
Brad Bishopdd62e362017-06-14 16:54:03 -040064
65 // Get an initial tach speed.
Brad Bishopd37c0f82017-07-28 21:47:00 -040066 try
67 {
Matthew Barthad2cd242020-08-13 13:36:19 -050068 std::get<double>(s) = util::SDBusPlus::getProperty<double>(
Matthew Barth2d2caa32020-05-26 11:07:24 -050069 tachPath, tachIface, tachProperty);
Brad Bishopd37c0f82017-07-28 21:47:00 -040070 }
Patrick Williamsddb773b2021-10-06 11:24:49 -050071 catch (const std::exception&)
Brad Bishopd37c0f82017-07-28 21:47:00 -040072 {
73 // Assume not spinning.
74
Matthew Barthad2cd242020-08-13 13:36:19 -050075 std::get<double>(s) = 0;
Jay Meyerae226192020-10-15 15:33:17 -050076 log<level::INFO>(
Patrick Williamsfbf47032023-07-17 12:27:34 -050077 std::format("Unable to read fan tach sensor {}", tachPath)
Jay Meyerae226192020-10-15 15:33:17 -050078 .c_str());
Brad Bishopd37c0f82017-07-28 21:47:00 -040079 }
Brad Bishopdd62e362017-06-14 16:54:03 -040080 }
81
82 // Set the initial state of the sensor.
Matthew Barth2d2caa32020-05-26 11:07:24 -050083 currentState = std::any_of(state.begin(), state.end(), [](const auto& s) {
Matthew Barthad2cd242020-08-13 13:36:19 -050084 return std::get<double>(s) != 0;
Matthew Barth2d2caa32020-05-26 11:07:24 -050085 });
Brad Bishopdd62e362017-06-14 16:54:03 -040086
87 return currentState;
88}
89
90void Tach::stop()
91{
92 for (auto& s : state)
93 {
94 // De-register signal callbacks.
95 std::get<1>(s) = nullptr;
96 }
97}
98
99bool Tach::present()
100{
101 // Live query the tach readings.
Matthew Barthad2cd242020-08-13 13:36:19 -0500102 std::vector<double> values;
Brad Bishopdd62e362017-06-14 16:54:03 -0400103 for (const auto& s : state)
104 {
Matthew Barthad2cd242020-08-13 13:36:19 -0500105 values.push_back(util::SDBusPlus::getProperty<double>(
Matthew Barth2d2caa32020-05-26 11:07:24 -0500106 tachNamespace + std::get<std::string>(s), tachIface, tachProperty));
Brad Bishopdd62e362017-06-14 16:54:03 -0400107 }
108
Matthew Barth2d2caa32020-05-26 11:07:24 -0500109 return std::any_of(values.begin(), values.end(),
110 [](const auto& v) { return v != 0; });
Brad Bishopdd62e362017-06-14 16:54:03 -0400111}
112
Patrick Williamscb356d42022-07-22 19:26:53 -0500113void Tach::propertiesChanged(size_t sensor, sdbusplus::message_t& msg)
Brad Bishopdd62e362017-06-14 16:54:03 -0400114{
115 std::string iface;
Matthew Barthad2cd242020-08-13 13:36:19 -0500116 util::Properties<double> properties;
Brad Bishopdd62e362017-06-14 16:54:03 -0400117 msg.read(iface, properties);
118
119 propertiesChanged(sensor, properties);
120}
121
Matthew Barth2d2caa32020-05-26 11:07:24 -0500122void Tach::propertiesChanged(size_t sensor,
Matthew Barthad2cd242020-08-13 13:36:19 -0500123 const util::Properties<double>& props)
Brad Bishopdd62e362017-06-14 16:54:03 -0400124{
Brad Bishopdd62e362017-06-14 16:54:03 -0400125 // Find the Value property containing the speed.
126 auto it = props.find(tachProperty);
127 if (it != props.end())
128 {
Matthew Barth9f93bd32020-05-28 16:57:14 -0500129 auto& s = state[sensor];
Matthew Barthad2cd242020-08-13 13:36:19 -0500130 std::get<double>(s) = std::get<double>(it->second);
Brad Bishopdd62e362017-06-14 16:54:03 -0400131
Matthew Barth2d2caa32020-05-26 11:07:24 -0500132 auto newState =
Matthew Barthad2cd242020-08-13 13:36:19 -0500133 std::any_of(state.begin(), state.end(),
134 [](const auto& s) { return std::get<double>(s) != 0; });
Brad Bishopdd62e362017-06-14 16:54:03 -0400135
136 if (currentState != newState)
137 {
Brad Bishop11083ec2017-07-25 19:08:53 -0400138 getPolicy().stateChanged(newState, *this);
Brad Bishopdd62e362017-06-14 16:54:03 -0400139 currentState = newState;
140 }
141 }
142}
143
Matt Spinlerc65d91d2021-04-21 13:09:49 -0500144void Tach::logConflict(const std::string& fanInventoryPath) const
145{
Patrick Williamsfbf47032023-07-17 12:27:34 -0500146 getLogger().log(std::format(
Matt Spinlerc65d91d2021-04-21 13:09:49 -0500147 "Tach sensor presence detect for fan {} said not present but "
148 "other methods indicated present",
149 fanInventoryPath));
150
151 // Let the code that monitors fan faults create the event
152 // logs for stopped rotors.
153}
154
Brad Bishopdd62e362017-06-14 16:54:03 -0400155} // namespace presence
156} // namespace fan
157} // namespace phosphor