blob: dacbec6952ed3c3b50accbac94f54370d4940fa1 [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
17#include <unistd.h>
18
Ed Tanous8a57ec02020-10-09 12:46:52 -070019#include <TachSensor.hpp>
20#include <Utils.hpp>
James Feist8086aba2020-08-25 16:00:59 -070021#include <boost/asio/read_until.hpp>
James Feist6714a252018-09-10 15:26:18 -070022#include <boost/date_time/posix_time/posix_time.hpp>
Zhikui Ren347dd4e2019-12-12 13:39:50 -080023#include <gpiod.hpp>
James Feist38fb5982020-05-28 10:09:54 -070024#include <sdbusplus/asio/connection.hpp>
25#include <sdbusplus/asio/object_server.hpp>
26
Josh Lehan5170fe62022-08-03 13:17:41 -070027#include <charconv>
James Feist38fb5982020-05-28 10:09:54 -070028#include <fstream>
James Feist6714a252018-09-10 15:26:18 -070029#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070030#include <istream>
James Feist6714a252018-09-10 15:26:18 -070031#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <memory>
33#include <optional>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <stdexcept>
James Feist6714a252018-09-10 15:26:18 -070035#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070036#include <utility>
37#include <vector>
James Feist6714a252018-09-10 15:26:18 -070038
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070039static constexpr unsigned int pwmPollMs = 500;
James Feist6714a252018-09-10 15:26:18 -070040
James Feistd8705872019-02-08 13:26:09 -080041TachSensor::TachSensor(const std::string& path, const std::string& objectType,
42 sdbusplus::asio::object_server& objectServer,
43 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist60c0ec72019-03-18 15:08:43 -070044 std::unique_ptr<PresenceSensor>&& presenceSensor,
James Feist7b18b1e2019-05-14 13:42:09 -070045 std::optional<RedundancySensor>* redundancy,
James Feistd8705872019-02-08 13:26:09 -080046 boost::asio::io_service& io, const std::string& fanName,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080047 std::vector<thresholds::Threshold>&& thresholdsIn,
James Feistd8705872019-02-08 13:26:09 -080048 const std::string& sensorConfiguration,
Ed Tanousf69fbf92022-01-14 10:15:33 -080049 const std::pair<double, double>& limits,
James Feist49a8ccd2020-09-16 16:09:52 -070050 const PowerState& powerState,
51 const std::optional<std::string>& ledIn) :
Zhikui Renda98f092021-11-01 09:41:08 -070052 Sensor(escapeName(fanName), std::move(thresholdsIn), sensorConfiguration,
53 objectType, false, false, limits.second, limits.first, conn,
54 powerState),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050055 objServer(objectServer), redundancy(redundancy),
Josh Lehan5170fe62022-08-03 13:17:41 -070056 presence(std::move(presenceSensor)),
57 inputDev(io, path, boost::asio::random_access_file::read_only),
58 waitTimer(io), path(path), led(ledIn)
James Feist6714a252018-09-10 15:26:18 -070059{
James Feist251c7822018-09-12 12:54:15 -070060 sensorInterface = objectServer.add_interface(
61 "/xyz/openbmc_project/sensors/fan_tach/" + name,
62 "xyz.openbmc_project.Sensor.Value");
63
Jayashree Dhanapal56678082022-01-04 17:27:20 +053064 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070065 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053066 std::string interface = thresholds::getInterface(threshold.level);
67 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
68 objectServer.add_interface(
69 "/xyz/openbmc_project/sensors/fan_tach/" + name, interface);
James Feist6714a252018-09-10 15:26:18 -070070 }
James Feist078f2322019-03-08 11:09:05 -080071 association = objectServer.add_interface(
72 "/xyz/openbmc_project/sensors/fan_tach/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070073 association::interface);
James Feist60c0ec72019-03-18 15:08:43 -070074
75 if (presence)
76 {
77 itemIface =
James Feistd8bd5622019-06-26 12:09:05 -070078 objectServer.add_interface("/xyz/openbmc_project/inventory/" + name,
James Feist60c0ec72019-03-18 15:08:43 -070079 "xyz.openbmc_project.Inventory.Item");
80 itemIface->register_property("PrettyName",
81 std::string()); // unused property
82 itemIface->register_property("Present", true);
83 itemIface->initialize();
James Feist2adc95c2019-09-30 14:55:28 -070084 itemAssoc = objectServer.add_interface(
85 "/xyz/openbmc_project/inventory/" + name, association::interface);
James Feistd8bd5622019-06-26 12:09:05 -070086 itemAssoc->register_property(
87 "associations",
88 std::vector<Association>{
89 {"sensors", "inventory",
90 "/xyz/openbmc_project/sensors/fan_tach/" + name}});
91 itemAssoc->initialize();
James Feist60c0ec72019-03-18 15:08:43 -070092 }
Andrei Kartashev39287412022-02-04 16:04:47 +030093 setInitialProperties(sensor_paths::unitRPMs);
James Feist6714a252018-09-10 15:26:18 -070094}
95
96TachSensor::~TachSensor()
97{
98 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070099 inputDev.close();
100 waitTimer.cancel();
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530101 for (const auto& iface : thresholdInterfaces)
102 {
103 objServer.remove_interface(iface);
104 }
James Feist251c7822018-09-12 12:54:15 -0700105 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800106 objServer.remove_interface(association);
James Feist60c0ec72019-03-18 15:08:43 -0700107 objServer.remove_interface(itemIface);
James Feistd8bd5622019-06-26 12:09:05 -0700108 objServer.remove_interface(itemAssoc);
James Feist6714a252018-09-10 15:26:18 -0700109}
110
Josh Lehan5170fe62022-08-03 13:17:41 -0700111void TachSensor::setupRead()
James Feist6714a252018-09-10 15:26:18 -0700112{
Josh Lehan5170fe62022-08-03 13:17:41 -0700113 std::weak_ptr<TachSensor> weakRef = weak_from_this();
114 inputDev.async_read_some_at(
115 0, boost::asio::buffer(readBuf),
116 [weakRef](const boost::system::error_code& ec, std::size_t bytesRead) {
117 std::shared_ptr<TachSensor> self = weakRef.lock();
118 if (self)
119 {
120 self->handleResponse(ec, bytesRead);
121 }
122 });
123}
124
125void TachSensor::restartRead(size_t pollTime)
126{
127 std::weak_ptr<TachSensor> weakRef = weak_from_this();
128 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
129 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
130 if (ec == boost::asio::error::operation_aborted)
131 {
132 return; // we're being canceled
133 }
134 std::shared_ptr<TachSensor> self = weakRef.lock();
135 if (!self)
136 {
137 return;
138 }
139 self->setupRead();
Ed Tanousbb679322022-05-16 16:10:00 -0700140 });
James Feist6714a252018-09-10 15:26:18 -0700141}
142
Josh Lehan5170fe62022-08-03 13:17:41 -0700143void TachSensor::handleResponse(const boost::system::error_code& err,
144 size_t bytesRead)
James Feist6714a252018-09-10 15:26:18 -0700145{
Josh Lehan5170fe62022-08-03 13:17:41 -0700146 if ((err == boost::system::errc::bad_file_descriptor) ||
147 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700148 {
Josh Lehan5170fe62022-08-03 13:17:41 -0700149 std::cerr << "TachSensor " << name << " removed " << path << "\n";
James Feist6714a252018-09-10 15:26:18 -0700150 return; // we're being destroyed
151 }
James Feist7bc2bab2018-10-26 14:09:45 -0700152 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700153 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700154 if (presence)
155 {
156 if (!presence->getValue())
157 {
James Feist961bf092020-07-01 16:38:12 -0700158 markAvailable(false);
James Feist7bc2bab2018-10-26 14:09:45 -0700159 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700160 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700161 }
James Feist60c0ec72019-03-18 15:08:43 -0700162 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700163 }
Josh Lehan5170fe62022-08-03 13:17:41 -0700164
James Feist7bc2bab2018-10-26 14:09:45 -0700165 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700166 {
James Feist7bc2bab2018-10-26 14:09:45 -0700167 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700168 {
Josh Lehan5170fe62022-08-03 13:17:41 -0700169 const char* bufEnd = readBuf.data() + bytesRead;
170 int nvalue = 0;
171 std::from_chars_result ret =
172 std::from_chars(readBuf.data(), bufEnd, nvalue);
173 if (ret.ec != std::errc())
James Feist7bc2bab2018-10-26 14:09:45 -0700174 {
James Feist961bf092020-07-01 16:38:12 -0700175 incrementError();
176 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700177 }
Josh Lehan5170fe62022-08-03 13:17:41 -0700178 else
179 {
180 updateValue(nvalue);
181 }
James Feist6714a252018-09-10 15:26:18 -0700182 }
183 else
184 {
James Feist961bf092020-07-01 16:38:12 -0700185 incrementError();
186 pollTime = sensorFailedPollTimeMs;
James Feist71d31b22019-01-02 16:57:54 -0800187 }
James Feist6714a252018-09-10 15:26:18 -0700188 }
Ed Tanous99c44092022-01-14 09:59:09 -0800189
Josh Lehan5170fe62022-08-03 13:17:41 -0700190 restartRead(pollTime);
James Feist6714a252018-09-10 15:26:18 -0700191}
192
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700193void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700194{
Zhikui Ren00bd56d2021-11-19 12:32:27 -0800195 bool status = thresholds::checkThresholds(this);
James Feist95e54902019-09-04 15:13:36 -0700196
Ed Tanous2049bd22022-07-09 07:20:26 -0700197 if ((redundancy != nullptr) && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700198 {
James Feist7b18b1e2019-05-14 13:42:09 -0700199 (*redundancy)
200 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700201 }
James Feist49a8ccd2020-09-16 16:09:52 -0700202
203 bool curLed = !status;
204 if (led && ledState != curLed)
205 {
206 ledState = curLed;
207 setLed(dbusConnection, *led, curLed);
208 }
James Feist6714a252018-09-10 15:26:18 -0700209}
210
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800211PresenceSensor::PresenceSensor(const std::string& gpioName, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -0700212 boost::asio::io_service& io,
213 const std::string& name) :
Ed Tanous8a17c302021-09-02 15:07:11 -0700214 gpioLine(gpiod::find_line(gpioName)),
215 gpioFd(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700216{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800217 if (!gpioLine)
James Feist7bc2bab2018-10-26 14:09:45 -0700218 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800219 std::cerr << "Error requesting gpio: " << gpioName << "\n";
220 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700221 return;
222 }
223
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800224 try
James Feist7bc2bab2018-10-26 14:09:45 -0700225 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800226 gpioLine.request({"FanSensor", gpiod::line_request::EVENT_BOTH_EDGES,
227 inverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0});
Ed Tanous2049bd22022-07-09 07:20:26 -0700228 status = (gpioLine.get_value() != 0);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800229
230 int gpioLineFd = gpioLine.event_get_fd();
231 if (gpioLineFd < 0)
232 {
233 std::cerr << "Failed to get " << gpioName << " fd\n";
234 return;
235 }
236
237 gpioFd.assign(gpioLineFd);
238 }
Patrick Williams26601e82021-10-06 12:43:25 -0500239 catch (const std::system_error&)
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800240 {
241 std::cerr << "Error reading gpio: " << gpioName << "\n";
242 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700243 return;
244 }
James Feist7bc2bab2018-10-26 14:09:45 -0700245
James Feist7bc2bab2018-10-26 14:09:45 -0700246 monitorPresence();
James Feist7bc2bab2018-10-26 14:09:45 -0700247}
248
249PresenceSensor::~PresenceSensor()
250{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800251 gpioFd.close();
252 gpioLine.release();
James Feist7bc2bab2018-10-26 14:09:45 -0700253}
254
255void PresenceSensor::monitorPresence(void)
256{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800257 gpioFd.async_wait(boost::asio::posix::stream_descriptor::wait_read,
258 [this](const boost::system::error_code& ec) {
Ed Tanousbb679322022-05-16 16:10:00 -0700259 if (ec == boost::system::errc::bad_file_descriptor)
260 {
261 return; // we're being destroyed
262 }
263 if (ec)
264 {
265 std::cerr << "Error on presence sensor " << name << " \n";
266 ;
267 }
268 else
269 {
270 read();
271 }
272 monitorPresence();
273 });
James Feist7bc2bab2018-10-26 14:09:45 -0700274}
275
276void PresenceSensor::read(void)
277{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800278 gpioLine.event_read();
Ed Tanous2049bd22022-07-09 07:20:26 -0700279 status = (gpioLine.get_value() != 0);
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800280 // Read is invoked when an edge event is detected by monitorPresence
281 if (status)
James Feist7bc2bab2018-10-26 14:09:45 -0700282 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800283 logFanInserted(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700284 }
285 else
286 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800287 logFanRemoved(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700288 }
289}
290
Ed Tanous2049bd22022-07-09 07:20:26 -0700291bool PresenceSensor::getValue(void) const
James Feist7bc2bab2018-10-26 14:09:45 -0700292{
293 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700294}
295
James Feist9bb67462019-03-15 15:09:50 -0700296RedundancySensor::RedundancySensor(size_t count,
297 const std::vector<std::string>& children,
298 sdbusplus::asio::object_server& objectServer,
299 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700300 count(count),
301 iface(objectServer.add_interface(
302 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700303 "xyz.openbmc_project.Control.FanRedundancy")),
304 association(objectServer.add_interface(
305 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700306 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700307 objectServer(objectServer)
308{
James Feist9bb67462019-03-15 15:09:50 -0700309 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700310 iface->register_property("Collection", children);
311 iface->register_property("Status", std::string("Full"));
312 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
313 iface->initialize();
314}
315RedundancySensor::~RedundancySensor()
316{
James Feist9bb67462019-03-15 15:09:50 -0700317 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700318 objectServer.remove_interface(iface);
319}
James Feistd8705872019-02-08 13:26:09 -0800320void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700321{
322 statuses[name] = failed;
323 size_t failedCount = 0;
324
James Feist7b18b1e2019-05-14 13:42:09 -0700325 std::string newState = redundancy::full;
Zev Weiss0521a062022-08-12 18:21:02 -0700326 for (const auto& [name, status] : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700327 {
Zev Weiss0521a062022-08-12 18:21:02 -0700328 if (status)
James Feistdc6c55f2018-10-31 12:53:20 -0700329 {
330 failedCount++;
331 }
332 if (failedCount > count)
333 {
James Feist7b18b1e2019-05-14 13:42:09 -0700334 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700335 break;
336 }
Ed Tanous2049bd22022-07-09 07:20:26 -0700337 if (failedCount != 0U)
James Feistdc6c55f2018-10-31 12:53:20 -0700338 {
James Feist7b18b1e2019-05-14 13:42:09 -0700339 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700340 }
341 }
James Feist7b18b1e2019-05-14 13:42:09 -0700342 if (state != newState)
343 {
344 if (state == redundancy::full)
345 {
346 logFanRedundancyLost();
347 }
348 else if (newState == redundancy::full)
349 {
350 logFanRedundancyRestored();
351 }
352 state = newState;
353 iface->set_property("Status", state);
354 }
James Feistdc6c55f2018-10-31 12:53:20 -0700355}