blob: 91a08c139e0d752f6a6d8a03171eb67d77dffe5a [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
James Feist58295ad2019-05-30 15:01:41 -070017#include "TachSensor.hpp"
18
19#include "Utils.hpp"
20
James Feist6714a252018-09-10 15:26:18 -070021#include <unistd.h>
22
James Feist6714a252018-09-10 15:26:18 -070023#include <boost/algorithm/string/predicate.hpp>
24#include <boost/algorithm/string/replace.hpp>
25#include <boost/date_time/posix_time/posix_time.hpp>
James Feist7bc2bab2018-10-26 14:09:45 -070026#include <fstream>
James Feist6714a252018-09-10 15:26:18 -070027#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070028#include <istream>
James Feist6714a252018-09-10 15:26:18 -070029#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070030#include <memory>
31#include <optional>
James Feist6714a252018-09-10 15:26:18 -070032#include <sdbusplus/asio/connection.hpp>
33#include <sdbusplus/asio/object_server.hpp>
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;
40static constexpr size_t warnAfterErrorCount = 10;
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,
48 std::vector<thresholds::Threshold>&& _thresholds,
49 const std::string& sensorConfiguration,
50 const std::pair<size_t, size_t>& limits) :
James Feist930fcde2019-05-28 12:58:43 -070051 Sensor(boost::replace_all_copy(fanName, " ", "_"), std::move(_thresholds),
52 sensorConfiguration, objectType, limits.second, limits.first),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050053 objServer(objectServer), redundancy(redundancy),
54 presence(std::move(presenceSensor)),
55 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path),
56 errCount(0)
James Feist6714a252018-09-10 15:26:18 -070057{
James Feist251c7822018-09-12 12:54:15 -070058 sensorInterface = objectServer.add_interface(
59 "/xyz/openbmc_project/sensors/fan_tach/" + name,
60 "xyz.openbmc_project.Sensor.Value");
61
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070062 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070063 {
James Feist251c7822018-09-12 12:54:15 -070064 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070065 "/xyz/openbmc_project/sensors/fan_tach/" + name,
66 "xyz.openbmc_project.Sensor.Threshold.Warning");
67 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070068 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070069 {
James Feist251c7822018-09-12 12:54:15 -070070 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070071 "/xyz/openbmc_project/sensors/fan_tach/" + name,
72 "xyz.openbmc_project.Sensor.Threshold.Critical");
73 }
James Feist078f2322019-03-08 11:09:05 -080074 association = objectServer.add_interface(
75 "/xyz/openbmc_project/sensors/fan_tach/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070076 association::interface);
James Feist60c0ec72019-03-18 15:08:43 -070077
78 if (presence)
79 {
80 itemIface =
James Feistd8bd5622019-06-26 12:09:05 -070081 objectServer.add_interface("/xyz/openbmc_project/inventory/" + name,
James Feist60c0ec72019-03-18 15:08:43 -070082 "xyz.openbmc_project.Inventory.Item");
83 itemIface->register_property("PrettyName",
84 std::string()); // unused property
85 itemIface->register_property("Present", true);
86 itemIface->initialize();
James Feist2adc95c2019-09-30 14:55:28 -070087 itemAssoc = objectServer.add_interface(
88 "/xyz/openbmc_project/inventory/" + name, association::interface);
James Feistd8bd5622019-06-26 12:09:05 -070089 itemAssoc->register_property(
90 "associations",
91 std::vector<Association>{
92 {"sensors", "inventory",
93 "/xyz/openbmc_project/sensors/fan_tach/" + name}});
94 itemAssoc->initialize();
James Feist60c0ec72019-03-18 15:08:43 -070095 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070096 setInitialProperties(conn);
James Feist6ef20402019-01-07 16:45:08 -080097 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070098 setupRead();
James Feist6714a252018-09-10 15:26:18 -070099}
100
101TachSensor::~TachSensor()
102{
103 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700104 inputDev.close();
105 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -0700106 objServer.remove_interface(thresholdInterfaceWarning);
107 objServer.remove_interface(thresholdInterfaceCritical);
108 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800109 objServer.remove_interface(association);
James Feist60c0ec72019-03-18 15:08:43 -0700110 objServer.remove_interface(itemIface);
James Feistd8bd5622019-06-26 12:09:05 -0700111 objServer.remove_interface(itemAssoc);
James Feist6714a252018-09-10 15:26:18 -0700112}
113
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700114void TachSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700115{
116 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700117 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -0800118 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700119 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700120}
121
James Feistd8705872019-02-08 13:26:09 -0800122void TachSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700123{
124 if (err == boost::system::errc::bad_file_descriptor)
125 {
126 return; // we're being destroyed
127 }
James Feist7bc2bab2018-10-26 14:09:45 -0700128 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700129 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700130 if (presence)
131 {
132 if (!presence->getValue())
133 {
James Feist1169eb42018-10-31 10:08:47 -0700134 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700135 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700136 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700137 }
James Feist60c0ec72019-03-18 15:08:43 -0700138 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700139 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700140 std::istream responseStream(&readBuf);
James Feist7bc2bab2018-10-26 14:09:45 -0700141 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700142 {
James Feist7bc2bab2018-10-26 14:09:45 -0700143 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700144 {
James Feist7bc2bab2018-10-26 14:09:45 -0700145 std::string response;
146 try
James Feist6714a252018-09-10 15:26:18 -0700147 {
James Feist7bc2bab2018-10-26 14:09:45 -0700148 std::getline(responseStream, response);
149 float nvalue = std::stof(response);
150 responseStream.clear();
James Feistb6c0b912019-07-09 12:21:44 -0700151 if (static_cast<double>(nvalue) != value)
James Feist7bc2bab2018-10-26 14:09:45 -0700152 {
153 updateValue(nvalue);
154 }
155 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700156 }
James Feistd8705872019-02-08 13:26:09 -0800157 catch (const std::invalid_argument&)
James Feist7bc2bab2018-10-26 14:09:45 -0700158 {
159 errCount++;
160 }
James Feist6714a252018-09-10 15:26:18 -0700161 }
162 else
163 {
James Feist71d31b22019-01-02 16:57:54 -0800164 if (!isPowerOn())
James Feist7bc2bab2018-10-26 14:09:45 -0700165 {
James Feist71d31b22019-01-02 16:57:54 -0800166 errCount = 0;
167 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700168 }
169 else
170 {
James Feist71d31b22019-01-02 16:57:54 -0800171 pollTime = sensorFailedPollTimeMs;
172 errCount++;
James Feist7bc2bab2018-10-26 14:09:45 -0700173 }
James Feist6714a252018-09-10 15:26:18 -0700174 }
James Feist71d31b22019-01-02 16:57:54 -0800175 if (errCount >= warnAfterErrorCount)
176 {
177 // only print once
178 if (errCount == warnAfterErrorCount)
179 {
180 std::cerr << "Failure to read sensor " << name << " at " << path
181 << " ec:" << err << "\n";
182 }
183 updateValue(0);
184 }
James Feist6714a252018-09-10 15:26:18 -0700185 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700186 responseStream.clear();
187 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700188 int fd = open(path.c_str(), O_RDONLY);
Jae Hyun Yooef9665a2019-10-10 10:26:39 -0700189 if (fd < 0)
James Feist6714a252018-09-10 15:26:18 -0700190 {
191 return; // we're no longer valid
192 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700193 inputDev.assign(fd);
James Feist7bc2bab2018-10-26 14:09:45 -0700194 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800195 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700196 if (ec == boost::asio::error::operation_aborted)
197 {
198 return; // we're being canceled
199 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700200 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700201 });
202}
203
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700204void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700205{
James Feist95e54902019-09-04 15:13:36 -0700206 if (!isPowerOn())
207 {
208 return;
209 }
210
James Feistdc6c55f2018-10-31 12:53:20 -0700211 bool status = thresholds::checkThresholds(this);
James Feist95e54902019-09-04 15:13:36 -0700212
James Feist7b18b1e2019-05-14 13:42:09 -0700213 if (redundancy && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700214 {
James Feist7b18b1e2019-05-14 13:42:09 -0700215 (*redundancy)
216 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700217 }
James Feist6714a252018-09-10 15:26:18 -0700218}
219
James Feist7bc2bab2018-10-26 14:09:45 -0700220PresenceSensor::PresenceSensor(const size_t index, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -0700221 boost::asio::io_service& io,
222 const std::string& name) :
James Feist7bc2bab2018-10-26 14:09:45 -0700223 inverted(inverted),
James Feist7b18b1e2019-05-14 13:42:09 -0700224 inputDev(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700225{
226 // todo: use gpiodaemon
227 std::string device = gpioPath + std::string("gpio") + std::to_string(index);
228 fd = open((device + "/value").c_str(), O_RDONLY);
229 if (fd < 0)
230 {
231 std::cerr << "Error opening gpio " << index << "\n";
232 return;
233 }
234
235 std::ofstream deviceFile(device + "/edge");
236 if (!deviceFile.good())
237 {
238 std::cerr << "Error setting edge " << device << "\n";
239 return;
240 }
241 deviceFile << "both";
242 deviceFile.close();
243
244 inputDev.assign(boost::asio::ip::tcp::v4(), fd);
245 monitorPresence();
246 read();
247}
248
249PresenceSensor::~PresenceSensor()
250{
251 inputDev.close();
252 close(fd);
253}
254
255void PresenceSensor::monitorPresence(void)
256{
257 inputDev.async_wait(boost::asio::ip::tcp::socket::wait_error,
James Feistd8705872019-02-08 13:26:09 -0800258 [this](const boost::system::error_code& ec) {
James Feist7bc2bab2018-10-26 14:09:45 -0700259 if (ec == boost::system::errc::bad_file_descriptor)
260 {
261 return; // we're being destroyed
262 }
263 else if (ec)
264 {
265 std::cerr
266 << "Error on presence sensor socket\n";
267 }
268 else
269 {
270 read();
271 }
272 monitorPresence();
273 });
274}
275
276void PresenceSensor::read(void)
277{
278 constexpr size_t readSize = sizeof("0");
279 std::string readBuf;
280 readBuf.resize(readSize);
281 lseek(fd, 0, SEEK_SET);
282 size_t r = ::read(fd, readBuf.data(), readSize);
James Feist95b079b2018-11-21 09:28:00 -0800283 if (r != readSize)
James Feist7bc2bab2018-10-26 14:09:45 -0700284 {
285 std::cerr << "Error reading gpio\n";
286 }
287 else
288 {
289 bool value = std::stoi(readBuf);
290 if (inverted)
291 {
292 value = !value;
293 }
James Feist7b18b1e2019-05-14 13:42:09 -0700294 if (value != status)
295 {
296 status = value;
297 if (status)
298 {
299 logFanInserted(name);
300 }
301 else
302 {
303 logFanRemoved(name);
304 }
305 }
James Feist7bc2bab2018-10-26 14:09:45 -0700306 }
307}
308
309bool PresenceSensor::getValue(void)
310{
311 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700312}
313
James Feist9bb67462019-03-15 15:09:50 -0700314RedundancySensor::RedundancySensor(size_t count,
315 const std::vector<std::string>& children,
316 sdbusplus::asio::object_server& objectServer,
317 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700318 count(count),
319 iface(objectServer.add_interface(
320 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700321 "xyz.openbmc_project.Control.FanRedundancy")),
322 association(objectServer.add_interface(
323 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700324 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700325 objectServer(objectServer)
326{
James Feist9bb67462019-03-15 15:09:50 -0700327 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700328 iface->register_property("Collection", children);
329 iface->register_property("Status", std::string("Full"));
330 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
331 iface->initialize();
332}
333RedundancySensor::~RedundancySensor()
334{
James Feist9bb67462019-03-15 15:09:50 -0700335 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700336 objectServer.remove_interface(iface);
337}
James Feistd8705872019-02-08 13:26:09 -0800338void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700339{
340 statuses[name] = failed;
341 size_t failedCount = 0;
342
James Feist7b18b1e2019-05-14 13:42:09 -0700343 std::string newState = redundancy::full;
James Feistd8705872019-02-08 13:26:09 -0800344 for (const auto& status : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700345 {
346 if (status.second)
347 {
348 failedCount++;
349 }
350 if (failedCount > count)
351 {
James Feist7b18b1e2019-05-14 13:42:09 -0700352 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700353 break;
354 }
355 else if (failedCount)
356 {
James Feist7b18b1e2019-05-14 13:42:09 -0700357 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700358 }
359 }
James Feist7b18b1e2019-05-14 13:42:09 -0700360 if (state != newState)
361 {
362 if (state == redundancy::full)
363 {
364 logFanRedundancyLost();
365 }
366 else if (newState == redundancy::full)
367 {
368 logFanRedundancyRestored();
369 }
370 state = newState;
371 iface->set_property("Status", state);
372 }
James Feistdc6c55f2018-10-31 12:53:20 -0700373}