blob: d2afba7ce6a525c15777667c21e3561b6273e518 [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 Feist6714a252018-09-10 15:26:18 -070021#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
James Feist8086aba2020-08-25 16:00:59 -070023#include <boost/asio/read_until.hpp>
James Feist6714a252018-09-10 15:26:18 -070024#include <boost/date_time/posix_time/posix_time.hpp>
Zhikui Ren347dd4e2019-12-12 13:39:50 -080025#include <gpiod.hpp>
James Feist38fb5982020-05-28 10:09:54 -070026#include <sdbusplus/asio/connection.hpp>
27#include <sdbusplus/asio/object_server.hpp>
28
29#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,
James Feistd8705872019-02-08 13:26:09 -080047 boost::asio::io_service& 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,
Josh Lehanf920e092020-08-07 00:12:54 -070050 const std::pair<size_t, size_t>& limits,
James Feist49a8ccd2020-09-16 16:09:52 -070051 const PowerState& powerState,
52 const std::optional<std::string>& ledIn) :
Jeff Lin7b7a9de2021-02-22 11:16:27 +080053 Sensor(boost::replace_all_copy(fanName, " ", "_"), std::move(thresholdsIn),
Bruce Lee1263c3d2021-06-04 15:16:33 +080054 sensorConfiguration, objectType, false, limits.second, limits.first,
55 conn, powerState),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050056 objServer(objectServer), redundancy(redundancy),
57 presence(std::move(presenceSensor)),
James Feist49a8ccd2020-09-16 16:09:52 -070058 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
59 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
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070065 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070066 {
James Feist251c7822018-09-12 12:54:15 -070067 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070068 "/xyz/openbmc_project/sensors/fan_tach/" + name,
69 "xyz.openbmc_project.Sensor.Threshold.Warning");
70 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070071 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070072 {
James Feist251c7822018-09-12 12:54:15 -070073 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070074 "/xyz/openbmc_project/sensors/fan_tach/" + name,
75 "xyz.openbmc_project.Sensor.Threshold.Critical");
76 }
James Feist078f2322019-03-08 11:09:05 -080077 association = objectServer.add_interface(
78 "/xyz/openbmc_project/sensors/fan_tach/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070079 association::interface);
James Feist60c0ec72019-03-18 15:08:43 -070080
81 if (presence)
82 {
83 itemIface =
James Feistd8bd5622019-06-26 12:09:05 -070084 objectServer.add_interface("/xyz/openbmc_project/inventory/" + name,
James Feist60c0ec72019-03-18 15:08:43 -070085 "xyz.openbmc_project.Inventory.Item");
86 itemIface->register_property("PrettyName",
87 std::string()); // unused property
88 itemIface->register_property("Present", true);
89 itemIface->initialize();
James Feist2adc95c2019-09-30 14:55:28 -070090 itemAssoc = objectServer.add_interface(
91 "/xyz/openbmc_project/inventory/" + name, association::interface);
James Feistd8bd5622019-06-26 12:09:05 -070092 itemAssoc->register_property(
93 "associations",
94 std::vector<Association>{
95 {"sensors", "inventory",
96 "/xyz/openbmc_project/sensors/fan_tach/" + name}});
97 itemAssoc->initialize();
James Feist60c0ec72019-03-18 15:08:43 -070098 }
Zev Weiss6b6891c2021-04-22 02:46:21 -050099 setInitialProperties(conn, sensor_paths::unitRPMs);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700100 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700101}
102
103TachSensor::~TachSensor()
104{
105 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700106 inputDev.close();
107 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -0700108 objServer.remove_interface(thresholdInterfaceWarning);
109 objServer.remove_interface(thresholdInterfaceCritical);
110 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
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700116void TachSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700117{
118 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700119 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -0800120 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700121 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700122}
123
James Feistd8705872019-02-08 13:26:09 -0800124void TachSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700125{
126 if (err == boost::system::errc::bad_file_descriptor)
127 {
128 return; // we're being destroyed
129 }
James Feist7bc2bab2018-10-26 14:09:45 -0700130 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700131 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700132 if (presence)
133 {
134 if (!presence->getValue())
135 {
James Feist961bf092020-07-01 16:38:12 -0700136 markAvailable(false);
James Feist7bc2bab2018-10-26 14:09:45 -0700137 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700138 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700139 }
James Feist60c0ec72019-03-18 15:08:43 -0700140 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700141 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700142 std::istream responseStream(&readBuf);
James Feist7bc2bab2018-10-26 14:09:45 -0700143 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700144 {
James Feist7bc2bab2018-10-26 14:09:45 -0700145 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700146 {
James Feist7bc2bab2018-10-26 14:09:45 -0700147 std::string response;
148 try
James Feist6714a252018-09-10 15:26:18 -0700149 {
James Feist7bc2bab2018-10-26 14:09:45 -0700150 std::getline(responseStream, response);
Zhikui Rend3da1282020-09-11 17:02:01 -0700151 rawValue = std::stod(response);
James Feist7bc2bab2018-10-26 14:09:45 -0700152 responseStream.clear();
Zhikui Rend3da1282020-09-11 17:02:01 -0700153 updateValue(rawValue);
James Feist6714a252018-09-10 15:26:18 -0700154 }
James Feistd8705872019-02-08 13:26:09 -0800155 catch (const std::invalid_argument&)
James Feist7bc2bab2018-10-26 14:09:45 -0700156 {
James Feist961bf092020-07-01 16:38:12 -0700157 incrementError();
158 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700159 }
James Feist6714a252018-09-10 15:26:18 -0700160 }
161 else
162 {
James Feist961bf092020-07-01 16:38:12 -0700163 incrementError();
164 pollTime = sensorFailedPollTimeMs;
James Feist71d31b22019-01-02 16:57:54 -0800165 }
James Feist6714a252018-09-10 15:26:18 -0700166 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700167 responseStream.clear();
168 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700169 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700170 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700171 {
172 return; // we're no longer valid
173 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700174 inputDev.assign(fd);
James Feist7bc2bab2018-10-26 14:09:45 -0700175 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800176 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700177 if (ec == boost::asio::error::operation_aborted)
178 {
179 return; // we're being canceled
180 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700181 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700182 });
183}
184
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700185void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700186{
James Feistdc6c55f2018-10-31 12:53:20 -0700187 bool status = thresholds::checkThresholds(this);
James Feist95e54902019-09-04 15:13:36 -0700188
James Feist7b18b1e2019-05-14 13:42:09 -0700189 if (redundancy && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700190 {
James Feist7b18b1e2019-05-14 13:42:09 -0700191 (*redundancy)
192 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700193 }
James Feist49a8ccd2020-09-16 16:09:52 -0700194
195 bool curLed = !status;
196 if (led && ledState != curLed)
197 {
198 ledState = curLed;
199 setLed(dbusConnection, *led, curLed);
200 }
James Feist6714a252018-09-10 15:26:18 -0700201}
202
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800203PresenceSensor::PresenceSensor(const std::string& gpioName, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -0700204 boost::asio::io_service& io,
205 const std::string& name) :
Ed Tanous8a17c302021-09-02 15:07:11 -0700206 gpioLine(gpiod::find_line(gpioName)),
207 gpioFd(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700208{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800209 if (!gpioLine)
James Feist7bc2bab2018-10-26 14:09:45 -0700210 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800211 std::cerr << "Error requesting gpio: " << gpioName << "\n";
212 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700213 return;
214 }
215
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800216 try
James Feist7bc2bab2018-10-26 14:09:45 -0700217 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800218 gpioLine.request({"FanSensor", gpiod::line_request::EVENT_BOTH_EDGES,
219 inverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0});
220 status = gpioLine.get_value();
221
222 int gpioLineFd = gpioLine.event_get_fd();
223 if (gpioLineFd < 0)
224 {
225 std::cerr << "Failed to get " << gpioName << " fd\n";
226 return;
227 }
228
229 gpioFd.assign(gpioLineFd);
230 }
231 catch (std::system_error&)
232 {
233 std::cerr << "Error reading gpio: " << gpioName << "\n";
234 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700235 return;
236 }
James Feist7bc2bab2018-10-26 14:09:45 -0700237
James Feist7bc2bab2018-10-26 14:09:45 -0700238 monitorPresence();
James Feist7bc2bab2018-10-26 14:09:45 -0700239}
240
241PresenceSensor::~PresenceSensor()
242{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800243 gpioFd.close();
244 gpioLine.release();
James Feist7bc2bab2018-10-26 14:09:45 -0700245}
246
247void PresenceSensor::monitorPresence(void)
248{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800249 gpioFd.async_wait(boost::asio::posix::stream_descriptor::wait_read,
250 [this](const boost::system::error_code& ec) {
251 if (ec == boost::system::errc::bad_file_descriptor)
252 {
253 return; // we're being destroyed
254 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700255 if (ec)
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800256 {
257 std::cerr << "Error on presence sensor " << name
258 << " \n";
259 ;
260 }
261 else
262 {
263 read();
264 }
265 monitorPresence();
266 });
James Feist7bc2bab2018-10-26 14:09:45 -0700267}
268
269void PresenceSensor::read(void)
270{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800271 gpioLine.event_read();
272 status = gpioLine.get_value();
273 // Read is invoked when an edge event is detected by monitorPresence
274 if (status)
James Feist7bc2bab2018-10-26 14:09:45 -0700275 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800276 logFanInserted(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700277 }
278 else
279 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800280 logFanRemoved(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700281 }
282}
283
284bool PresenceSensor::getValue(void)
285{
286 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700287}
288
James Feist9bb67462019-03-15 15:09:50 -0700289RedundancySensor::RedundancySensor(size_t count,
290 const std::vector<std::string>& children,
291 sdbusplus::asio::object_server& objectServer,
292 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700293 count(count),
294 iface(objectServer.add_interface(
295 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700296 "xyz.openbmc_project.Control.FanRedundancy")),
297 association(objectServer.add_interface(
298 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700299 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700300 objectServer(objectServer)
301{
James Feist9bb67462019-03-15 15:09:50 -0700302 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700303 iface->register_property("Collection", children);
304 iface->register_property("Status", std::string("Full"));
305 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
306 iface->initialize();
307}
308RedundancySensor::~RedundancySensor()
309{
James Feist9bb67462019-03-15 15:09:50 -0700310 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700311 objectServer.remove_interface(iface);
312}
James Feistd8705872019-02-08 13:26:09 -0800313void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700314{
315 statuses[name] = failed;
316 size_t failedCount = 0;
317
James Feist7b18b1e2019-05-14 13:42:09 -0700318 std::string newState = redundancy::full;
James Feistd8705872019-02-08 13:26:09 -0800319 for (const auto& status : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700320 {
321 if (status.second)
322 {
323 failedCount++;
324 }
325 if (failedCount > count)
326 {
James Feist7b18b1e2019-05-14 13:42:09 -0700327 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700328 break;
329 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700330 if (failedCount)
James Feistdc6c55f2018-10-31 12:53:20 -0700331 {
James Feist7b18b1e2019-05-14 13:42:09 -0700332 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700333 }
334 }
James Feist7b18b1e2019-05-14 13:42:09 -0700335 if (state != newState)
336 {
337 if (state == redundancy::full)
338 {
339 logFanRedundancyLost();
340 }
341 else if (newState == redundancy::full)
342 {
343 logFanRedundancyRestored();
344 }
345 state = newState;
346 iface->set_property("Status", state);
347 }
James Feistdc6c55f2018-10-31 12:53:20 -0700348}