blob: 4ba777734404fc608c9efaf69999b751411ee221 [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,
Josh Lehanf920e092020-08-07 00:12:54 -070049 const std::pair<size_t, size_t>& 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),
56 presence(std::move(presenceSensor)),
James Feist49a8ccd2020-09-16 16:09:52 -070057 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
58 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 }
Zev Weiss6b6891c2021-04-22 02:46:21 -050093 setInitialProperties(conn, sensor_paths::unitRPMs);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070094 setupRead();
James Feist6714a252018-09-10 15:26:18 -070095}
96
97TachSensor::~TachSensor()
98{
99 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700100 inputDev.close();
101 waitTimer.cancel();
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530102 for (const auto& iface : thresholdInterfaces)
103 {
104 objServer.remove_interface(iface);
105 }
James Feist251c7822018-09-12 12:54:15 -0700106 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800107 objServer.remove_interface(association);
James Feist60c0ec72019-03-18 15:08:43 -0700108 objServer.remove_interface(itemIface);
James Feistd8bd5622019-06-26 12:09:05 -0700109 objServer.remove_interface(itemAssoc);
James Feist6714a252018-09-10 15:26:18 -0700110}
111
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700112void TachSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700113{
114 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700115 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -0800116 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700117 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700118}
119
James Feistd8705872019-02-08 13:26:09 -0800120void TachSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700121{
122 if (err == boost::system::errc::bad_file_descriptor)
123 {
124 return; // we're being destroyed
125 }
James Feist7bc2bab2018-10-26 14:09:45 -0700126 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700127 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700128 if (presence)
129 {
130 if (!presence->getValue())
131 {
James Feist961bf092020-07-01 16:38:12 -0700132 markAvailable(false);
James Feist7bc2bab2018-10-26 14:09:45 -0700133 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700134 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700135 }
James Feist60c0ec72019-03-18 15:08:43 -0700136 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700137 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700138 std::istream responseStream(&readBuf);
James Feist7bc2bab2018-10-26 14:09:45 -0700139 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700140 {
James Feist7bc2bab2018-10-26 14:09:45 -0700141 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700142 {
James Feist7bc2bab2018-10-26 14:09:45 -0700143 std::string response;
144 try
James Feist6714a252018-09-10 15:26:18 -0700145 {
James Feist7bc2bab2018-10-26 14:09:45 -0700146 std::getline(responseStream, response);
Zhikui Rend3da1282020-09-11 17:02:01 -0700147 rawValue = std::stod(response);
James Feist7bc2bab2018-10-26 14:09:45 -0700148 responseStream.clear();
Zhikui Rend3da1282020-09-11 17:02:01 -0700149 updateValue(rawValue);
James Feist6714a252018-09-10 15:26:18 -0700150 }
James Feistd8705872019-02-08 13:26:09 -0800151 catch (const std::invalid_argument&)
James Feist7bc2bab2018-10-26 14:09:45 -0700152 {
James Feist961bf092020-07-01 16:38:12 -0700153 incrementError();
154 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700155 }
James Feist6714a252018-09-10 15:26:18 -0700156 }
157 else
158 {
James Feist961bf092020-07-01 16:38:12 -0700159 incrementError();
160 pollTime = sensorFailedPollTimeMs;
James Feist71d31b22019-01-02 16:57:54 -0800161 }
James Feist6714a252018-09-10 15:26:18 -0700162 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700163 responseStream.clear();
164 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700165 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700166 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700167 {
168 return; // we're no longer valid
169 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700170 inputDev.assign(fd);
James Feist7bc2bab2018-10-26 14:09:45 -0700171 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800172 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700173 if (ec == boost::asio::error::operation_aborted)
174 {
175 return; // we're being canceled
176 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700177 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700178 });
179}
180
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700181void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700182{
Zhikui Ren00bd56d2021-11-19 12:32:27 -0800183 bool status = thresholds::checkThresholds(this);
James Feist95e54902019-09-04 15:13:36 -0700184
James Feist7b18b1e2019-05-14 13:42:09 -0700185 if (redundancy && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700186 {
James Feist7b18b1e2019-05-14 13:42:09 -0700187 (*redundancy)
188 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700189 }
James Feist49a8ccd2020-09-16 16:09:52 -0700190
191 bool curLed = !status;
192 if (led && ledState != curLed)
193 {
194 ledState = curLed;
195 setLed(dbusConnection, *led, curLed);
196 }
James Feist6714a252018-09-10 15:26:18 -0700197}
198
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800199PresenceSensor::PresenceSensor(const std::string& gpioName, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -0700200 boost::asio::io_service& io,
201 const std::string& name) :
Ed Tanous8a17c302021-09-02 15:07:11 -0700202 gpioLine(gpiod::find_line(gpioName)),
203 gpioFd(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700204{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800205 if (!gpioLine)
James Feist7bc2bab2018-10-26 14:09:45 -0700206 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800207 std::cerr << "Error requesting gpio: " << gpioName << "\n";
208 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700209 return;
210 }
211
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800212 try
James Feist7bc2bab2018-10-26 14:09:45 -0700213 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800214 gpioLine.request({"FanSensor", gpiod::line_request::EVENT_BOTH_EDGES,
215 inverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0});
216 status = gpioLine.get_value();
217
218 int gpioLineFd = gpioLine.event_get_fd();
219 if (gpioLineFd < 0)
220 {
221 std::cerr << "Failed to get " << gpioName << " fd\n";
222 return;
223 }
224
225 gpioFd.assign(gpioLineFd);
226 }
Patrick Williams26601e82021-10-06 12:43:25 -0500227 catch (const std::system_error&)
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800228 {
229 std::cerr << "Error reading gpio: " << gpioName << "\n";
230 status = false;
James Feist7bc2bab2018-10-26 14:09:45 -0700231 return;
232 }
James Feist7bc2bab2018-10-26 14:09:45 -0700233
James Feist7bc2bab2018-10-26 14:09:45 -0700234 monitorPresence();
James Feist7bc2bab2018-10-26 14:09:45 -0700235}
236
237PresenceSensor::~PresenceSensor()
238{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800239 gpioFd.close();
240 gpioLine.release();
James Feist7bc2bab2018-10-26 14:09:45 -0700241}
242
243void PresenceSensor::monitorPresence(void)
244{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800245 gpioFd.async_wait(boost::asio::posix::stream_descriptor::wait_read,
246 [this](const boost::system::error_code& ec) {
247 if (ec == boost::system::errc::bad_file_descriptor)
248 {
249 return; // we're being destroyed
250 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700251 if (ec)
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800252 {
253 std::cerr << "Error on presence sensor " << name
254 << " \n";
255 ;
256 }
257 else
258 {
259 read();
260 }
261 monitorPresence();
262 });
James Feist7bc2bab2018-10-26 14:09:45 -0700263}
264
265void PresenceSensor::read(void)
266{
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800267 gpioLine.event_read();
268 status = gpioLine.get_value();
269 // Read is invoked when an edge event is detected by monitorPresence
270 if (status)
James Feist7bc2bab2018-10-26 14:09:45 -0700271 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800272 logFanInserted(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700273 }
274 else
275 {
Zhikui Ren347dd4e2019-12-12 13:39:50 -0800276 logFanRemoved(name);
James Feist7bc2bab2018-10-26 14:09:45 -0700277 }
278}
279
280bool PresenceSensor::getValue(void)
281{
282 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700283}
284
James Feist9bb67462019-03-15 15:09:50 -0700285RedundancySensor::RedundancySensor(size_t count,
286 const std::vector<std::string>& children,
287 sdbusplus::asio::object_server& objectServer,
288 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700289 count(count),
290 iface(objectServer.add_interface(
291 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700292 "xyz.openbmc_project.Control.FanRedundancy")),
293 association(objectServer.add_interface(
294 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700295 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700296 objectServer(objectServer)
297{
James Feist9bb67462019-03-15 15:09:50 -0700298 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700299 iface->register_property("Collection", children);
300 iface->register_property("Status", std::string("Full"));
301 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
302 iface->initialize();
303}
304RedundancySensor::~RedundancySensor()
305{
James Feist9bb67462019-03-15 15:09:50 -0700306 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700307 objectServer.remove_interface(iface);
308}
James Feistd8705872019-02-08 13:26:09 -0800309void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700310{
311 statuses[name] = failed;
312 size_t failedCount = 0;
313
James Feist7b18b1e2019-05-14 13:42:09 -0700314 std::string newState = redundancy::full;
James Feistd8705872019-02-08 13:26:09 -0800315 for (const auto& status : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700316 {
317 if (status.second)
318 {
319 failedCount++;
320 }
321 if (failedCount > count)
322 {
James Feist7b18b1e2019-05-14 13:42:09 -0700323 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700324 break;
325 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700326 if (failedCount)
James Feistdc6c55f2018-10-31 12:53:20 -0700327 {
James Feist7b18b1e2019-05-14 13:42:09 -0700328 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700329 }
330 }
James Feist7b18b1e2019-05-14 13:42:09 -0700331 if (state != newState)
332 {
333 if (state == redundancy::full)
334 {
335 logFanRedundancyLost();
336 }
337 else if (newState == redundancy::full)
338 {
339 logFanRedundancyRestored();
340 }
341 state = newState;
342 iface->set_property("Status", state);
343 }
James Feistdc6c55f2018-10-31 12:53:20 -0700344}