blob: f60941088aadc64202b7209d97943bd49cbcde57 [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>
James Feist8086aba2020-08-25 16:00:59 -070022#include <boost/asio/read_until.hpp>
James Feist6714a252018-09-10 15:26:18 -070023#include <boost/date_time/posix_time/posix_time.hpp>
Zhikui Ren347dd4e2019-12-12 13:39:50 -080024#include <gpiod.hpp>
James Feist38fb5982020-05-28 10:09:54 -070025#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27
28#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),
Ed Tanous99c44092022-01-14 09:59:09 -080056 presence(std::move(presenceSensor)), inputDev(io), waitTimer(io),
57 path(path), led(ledIn)
James Feist6714a252018-09-10 15:26:18 -070058{
Ed Tanous99c44092022-01-14 09:59:09 -080059 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
60 int fd = open(path.c_str(), O_RDONLY);
61 if (fd < 0)
62 {
63 std::cerr << "Tach sensor failed to open file\n";
64 }
65 inputDev.assign(fd);
66
James Feist251c7822018-09-12 12:54:15 -070067 sensorInterface = objectServer.add_interface(
68 "/xyz/openbmc_project/sensors/fan_tach/" + name,
69 "xyz.openbmc_project.Sensor.Value");
70
Jayashree Dhanapal56678082022-01-04 17:27:20 +053071 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070072 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053073 std::string interface = thresholds::getInterface(threshold.level);
74 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
75 objectServer.add_interface(
76 "/xyz/openbmc_project/sensors/fan_tach/" + name, interface);
James Feist6714a252018-09-10 15:26:18 -070077 }
James Feist078f2322019-03-08 11:09:05 -080078 association = objectServer.add_interface(
79 "/xyz/openbmc_project/sensors/fan_tach/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070080 association::interface);
James Feist60c0ec72019-03-18 15:08:43 -070081
82 if (presence)
83 {
84 itemIface =
James Feistd8bd5622019-06-26 12:09:05 -070085 objectServer.add_interface("/xyz/openbmc_project/inventory/" + name,
James Feist60c0ec72019-03-18 15:08:43 -070086 "xyz.openbmc_project.Inventory.Item");
87 itemIface->register_property("PrettyName",
88 std::string()); // unused property
89 itemIface->register_property("Present", true);
90 itemIface->initialize();
James Feist2adc95c2019-09-30 14:55:28 -070091 itemAssoc = objectServer.add_interface(
92 "/xyz/openbmc_project/inventory/" + name, association::interface);
James Feistd8bd5622019-06-26 12:09:05 -070093 itemAssoc->register_property(
94 "associations",
95 std::vector<Association>{
96 {"sensors", "inventory",
97 "/xyz/openbmc_project/sensors/fan_tach/" + name}});
98 itemAssoc->initialize();
James Feist60c0ec72019-03-18 15:08:43 -070099 }
Andrei Kartashev39287412022-02-04 16:04:47 +0300100 setInitialProperties(sensor_paths::unitRPMs);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700101 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700102}
103
104TachSensor::~TachSensor()
105{
106 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700107 inputDev.close();
108 waitTimer.cancel();
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530109 for (const auto& iface : thresholdInterfaces)
110 {
111 objServer.remove_interface(iface);
112 }
James Feist251c7822018-09-12 12:54:15 -0700113 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800114 objServer.remove_interface(association);
James Feist60c0ec72019-03-18 15:08:43 -0700115 objServer.remove_interface(itemIface);
James Feistd8bd5622019-06-26 12:09:05 -0700116 objServer.remove_interface(itemAssoc);
James Feist6714a252018-09-10 15:26:18 -0700117}
118
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700119void TachSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700120{
121 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700122 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -0800123 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700124 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700125}
126
James Feistd8705872019-02-08 13:26:09 -0800127void TachSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700128{
129 if (err == boost::system::errc::bad_file_descriptor)
130 {
131 return; // we're being destroyed
132 }
James Feist7bc2bab2018-10-26 14:09:45 -0700133 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700134 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700135 if (presence)
136 {
137 if (!presence->getValue())
138 {
James Feist961bf092020-07-01 16:38:12 -0700139 markAvailable(false);
James Feist7bc2bab2018-10-26 14:09:45 -0700140 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700141 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700142 }
James Feist60c0ec72019-03-18 15:08:43 -0700143 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700144 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700145 std::istream responseStream(&readBuf);
James Feist7bc2bab2018-10-26 14:09:45 -0700146 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700147 {
James Feist7bc2bab2018-10-26 14:09:45 -0700148 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700149 {
James Feist7bc2bab2018-10-26 14:09:45 -0700150 std::string response;
151 try
James Feist6714a252018-09-10 15:26:18 -0700152 {
James Feist7bc2bab2018-10-26 14:09:45 -0700153 std::getline(responseStream, response);
Zhikui Rend3da1282020-09-11 17:02:01 -0700154 rawValue = std::stod(response);
James Feist7bc2bab2018-10-26 14:09:45 -0700155 responseStream.clear();
Zhikui Rend3da1282020-09-11 17:02:01 -0700156 updateValue(rawValue);
James Feist6714a252018-09-10 15:26:18 -0700157 }
James Feistd8705872019-02-08 13:26:09 -0800158 catch (const std::invalid_argument&)
James Feist7bc2bab2018-10-26 14:09:45 -0700159 {
James Feist961bf092020-07-01 16:38:12 -0700160 incrementError();
161 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700162 }
James Feist6714a252018-09-10 15:26:18 -0700163 }
164 else
165 {
James Feist961bf092020-07-01 16:38:12 -0700166 incrementError();
167 pollTime = sensorFailedPollTimeMs;
James Feist71d31b22019-01-02 16:57:54 -0800168 }
James Feist6714a252018-09-10 15:26:18 -0700169 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700170 responseStream.clear();
171 inputDev.close();
Ed Tanous99c44092022-01-14 09:59:09 -0800172
173 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
James Feist6714a252018-09-10 15:26:18 -0700174 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700175 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700176 {
177 return; // we're no longer valid
178 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700179 inputDev.assign(fd);
James Feist7bc2bab2018-10-26 14:09:45 -0700180 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800181 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700182 if (ec == boost::asio::error::operation_aborted)
183 {
184 return; // we're being canceled
185 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700186 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700187 });
188}
189
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700190void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700191{
Zhikui Ren00bd56d2021-11-19 12:32:27 -0800192 bool status = thresholds::checkThresholds(this);
James Feist95e54902019-09-04 15:13:36 -0700193
James Feist7b18b1e2019-05-14 13:42:09 -0700194 if (redundancy && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700195 {
James Feist7b18b1e2019-05-14 13:42:09 -0700196 (*redundancy)
197 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700198 }
James Feist49a8ccd2020-09-16 16:09:52 -0700199
200 bool curLed = !status;
201 if (led && ledState != curLed)
202 {
203 ledState = curLed;
204 setLed(dbusConnection, *led, curLed);
205 }
James Feist6714a252018-09-10 15:26:18 -0700206}
207
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800208PresenceSensor::PresenceSensor(const std::string& gpioName, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -0700209 boost::asio::io_service& io,
210 const std::string& name) :
Ed Tanous8a17c302021-09-02 15:07:11 -0700211 gpioLine(gpiod::find_line(gpioName)),
212 gpioFd(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700213{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800214 if (!gpioLine)
James Feist7bc2bab2018-10-26 14:09:45 -0700215 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800216 std::cerr << "Error requesting gpio: " << gpioName << "\n";
217 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700218 return;
219 }
220
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800221 try
James Feist7bc2bab2018-10-26 14:09:45 -0700222 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800223 gpioLine.request({"FanSensor", gpiod::line_request::EVENT_BOTH_EDGES,
224 inverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0});
225 status = gpioLine.get_value();
226
227 int gpioLineFd = gpioLine.event_get_fd();
228 if (gpioLineFd < 0)
229 {
230 std::cerr << "Failed to get " << gpioName << " fd\n";
231 return;
232 }
233
234 gpioFd.assign(gpioLineFd);
235 }
Patrick Williams26601e82021-10-06 12:43:25 -0500236 catch (const std::system_error&)
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800237 {
238 std::cerr << "Error reading gpio: " << gpioName << "\n";
239 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700240 return;
241 }
James Feist7bc2bab2018-10-26 14:09:45 -0700242
James Feist7bc2bab2018-10-26 14:09:45 -0700243 monitorPresence();
James Feist7bc2bab2018-10-26 14:09:45 -0700244}
245
246PresenceSensor::~PresenceSensor()
247{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800248 gpioFd.close();
249 gpioLine.release();
James Feist7bc2bab2018-10-26 14:09:45 -0700250}
251
252void PresenceSensor::monitorPresence(void)
253{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800254 gpioFd.async_wait(boost::asio::posix::stream_descriptor::wait_read,
255 [this](const boost::system::error_code& ec) {
256 if (ec == boost::system::errc::bad_file_descriptor)
257 {
258 return; // we're being destroyed
259 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700260 if (ec)
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800261 {
262 std::cerr << "Error on presence sensor " << name
263 << " \n";
264 ;
265 }
266 else
267 {
268 read();
269 }
270 monitorPresence();
271 });
James Feist7bc2bab2018-10-26 14:09:45 -0700272}
273
274void PresenceSensor::read(void)
275{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800276 gpioLine.event_read();
277 status = gpioLine.get_value();
278 // Read is invoked when an edge event is detected by monitorPresence
279 if (status)
James Feist7bc2bab2018-10-26 14:09:45 -0700280 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800281 logFanInserted(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700282 }
283 else
284 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800285 logFanRemoved(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700286 }
287}
288
289bool PresenceSensor::getValue(void)
290{
291 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700292}
293
James Feist9bb67462019-03-15 15:09:50 -0700294RedundancySensor::RedundancySensor(size_t count,
295 const std::vector<std::string>& children,
296 sdbusplus::asio::object_server& objectServer,
297 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700298 count(count),
299 iface(objectServer.add_interface(
300 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700301 "xyz.openbmc_project.Control.FanRedundancy")),
302 association(objectServer.add_interface(
303 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700304 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700305 objectServer(objectServer)
306{
James Feist9bb67462019-03-15 15:09:50 -0700307 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700308 iface->register_property("Collection", children);
309 iface->register_property("Status", std::string("Full"));
310 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
311 iface->initialize();
312}
313RedundancySensor::~RedundancySensor()
314{
James Feist9bb67462019-03-15 15:09:50 -0700315 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700316 objectServer.remove_interface(iface);
317}
James Feistd8705872019-02-08 13:26:09 -0800318void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700319{
320 statuses[name] = failed;
321 size_t failedCount = 0;
322
James Feist7b18b1e2019-05-14 13:42:09 -0700323 std::string newState = redundancy::full;
James Feistd8705872019-02-08 13:26:09 -0800324 for (const auto& status : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700325 {
326 if (status.second)
327 {
328 failedCount++;
329 }
330 if (failedCount > count)
331 {
James Feist7b18b1e2019-05-14 13:42:09 -0700332 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700333 break;
334 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700335 if (failedCount)
James Feistdc6c55f2018-10-31 12:53:20 -0700336 {
James Feist7b18b1e2019-05-14 13:42:09 -0700337 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700338 }
339 }
James Feist7b18b1e2019-05-14 13:42:09 -0700340 if (state != newState)
341 {
342 if (state == redundancy::full)
343 {
344 logFanRedundancyLost();
345 }
346 else if (newState == redundancy::full)
347 {
348 logFanRedundancyRestored();
349 }
350 state = newState;
351 iface->set_property("Status", state);
352 }
James Feistdc6c55f2018-10-31 12:53:20 -0700353}