blob: e000780a4dc43a0dca85bd5ffd7760a468465833 [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>
28#include <limits>
29#include <sdbusplus/asio/connection.hpp>
30#include <sdbusplus/asio/object_server.hpp>
31#include <string>
32
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070033static constexpr unsigned int pwmPollMs = 500;
34static constexpr size_t warnAfterErrorCount = 10;
James Feist6714a252018-09-10 15:26:18 -070035
James Feistd8705872019-02-08 13:26:09 -080036TachSensor::TachSensor(const std::string& path, const std::string& objectType,
37 sdbusplus::asio::object_server& objectServer,
38 std::shared_ptr<sdbusplus::asio::connection>& conn,
James Feist60c0ec72019-03-18 15:08:43 -070039 std::unique_ptr<PresenceSensor>&& presenceSensor,
James Feist7b18b1e2019-05-14 13:42:09 -070040 std::optional<RedundancySensor>* redundancy,
James Feistd8705872019-02-08 13:26:09 -080041 boost::asio::io_service& io, const std::string& fanName,
42 std::vector<thresholds::Threshold>&& _thresholds,
43 const std::string& sensorConfiguration,
44 const std::pair<size_t, size_t>& limits) :
James Feist930fcde2019-05-28 12:58:43 -070045 Sensor(boost::replace_all_copy(fanName, " ", "_"), std::move(_thresholds),
46 sensorConfiguration, objectType, limits.second, limits.first),
47 path(path), objServer(objectServer), presence(std::move(presenceSensor)),
James Feist71d31b22019-01-02 16:57:54 -080048 redundancy(redundancy), inputDev(io, open(path.c_str(), O_RDONLY)),
49 waitTimer(io), errCount(0)
James Feist6714a252018-09-10 15:26:18 -070050{
James Feist251c7822018-09-12 12:54:15 -070051 sensorInterface = objectServer.add_interface(
52 "/xyz/openbmc_project/sensors/fan_tach/" + name,
53 "xyz.openbmc_project.Sensor.Value");
54
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070055 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070056 {
James Feist251c7822018-09-12 12:54:15 -070057 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070058 "/xyz/openbmc_project/sensors/fan_tach/" + name,
59 "xyz.openbmc_project.Sensor.Threshold.Warning");
60 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070061 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070062 {
James Feist251c7822018-09-12 12:54:15 -070063 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070064 "/xyz/openbmc_project/sensors/fan_tach/" + name,
65 "xyz.openbmc_project.Sensor.Threshold.Critical");
66 }
James Feist078f2322019-03-08 11:09:05 -080067 association = objectServer.add_interface(
68 "/xyz/openbmc_project/sensors/fan_tach/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070069 association::interface);
James Feist60c0ec72019-03-18 15:08:43 -070070
71 if (presence)
72 {
73 itemIface =
James Feistd8bd5622019-06-26 12:09:05 -070074 objectServer.add_interface("/xyz/openbmc_project/inventory/" + name,
James Feist60c0ec72019-03-18 15:08:43 -070075 "xyz.openbmc_project.Inventory.Item");
76 itemIface->register_property("PrettyName",
77 std::string()); // unused property
78 itemIface->register_property("Present", true);
79 itemIface->initialize();
James Feist2adc95c2019-09-30 14:55:28 -070080 itemAssoc = objectServer.add_interface(
81 "/xyz/openbmc_project/inventory/" + name, association::interface);
James Feistd8bd5622019-06-26 12:09:05 -070082 itemAssoc->register_property(
83 "associations",
84 std::vector<Association>{
85 {"sensors", "inventory",
86 "/xyz/openbmc_project/sensors/fan_tach/" + name}});
87 itemAssoc->initialize();
James Feist60c0ec72019-03-18 15:08:43 -070088 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070089 setInitialProperties(conn);
James Feist6ef20402019-01-07 16:45:08 -080090 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070091 setupRead();
James Feist6714a252018-09-10 15:26:18 -070092}
93
94TachSensor::~TachSensor()
95{
96 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070097 inputDev.close();
98 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070099 objServer.remove_interface(thresholdInterfaceWarning);
100 objServer.remove_interface(thresholdInterfaceCritical);
101 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800102 objServer.remove_interface(association);
James Feist60c0ec72019-03-18 15:08:43 -0700103 objServer.remove_interface(itemIface);
James Feistd8bd5622019-06-26 12:09:05 -0700104 objServer.remove_interface(itemAssoc);
James Feist6714a252018-09-10 15:26:18 -0700105}
106
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700107void TachSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700108{
109 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700110 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -0800111 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700112 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700113}
114
James Feistd8705872019-02-08 13:26:09 -0800115void TachSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700116{
117 if (err == boost::system::errc::bad_file_descriptor)
118 {
119 return; // we're being destroyed
120 }
James Feist7bc2bab2018-10-26 14:09:45 -0700121 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700122 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700123 if (presence)
124 {
125 if (!presence->getValue())
126 {
James Feist1169eb42018-10-31 10:08:47 -0700127 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700128 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700129 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700130 }
James Feist60c0ec72019-03-18 15:08:43 -0700131 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700132 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700133 std::istream responseStream(&readBuf);
James Feist7bc2bab2018-10-26 14:09:45 -0700134 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700135 {
James Feist7bc2bab2018-10-26 14:09:45 -0700136 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700137 {
James Feist7bc2bab2018-10-26 14:09:45 -0700138 std::string response;
139 try
James Feist6714a252018-09-10 15:26:18 -0700140 {
James Feist7bc2bab2018-10-26 14:09:45 -0700141 std::getline(responseStream, response);
142 float nvalue = std::stof(response);
143 responseStream.clear();
James Feistb6c0b912019-07-09 12:21:44 -0700144 if (static_cast<double>(nvalue) != value)
James Feist7bc2bab2018-10-26 14:09:45 -0700145 {
146 updateValue(nvalue);
147 }
148 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700149 }
James Feistd8705872019-02-08 13:26:09 -0800150 catch (const std::invalid_argument&)
James Feist7bc2bab2018-10-26 14:09:45 -0700151 {
152 errCount++;
153 }
James Feist6714a252018-09-10 15:26:18 -0700154 }
155 else
156 {
James Feist71d31b22019-01-02 16:57:54 -0800157 if (!isPowerOn())
James Feist7bc2bab2018-10-26 14:09:45 -0700158 {
James Feist71d31b22019-01-02 16:57:54 -0800159 errCount = 0;
160 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700161 }
162 else
163 {
James Feist71d31b22019-01-02 16:57:54 -0800164 pollTime = sensorFailedPollTimeMs;
165 errCount++;
James Feist7bc2bab2018-10-26 14:09:45 -0700166 }
James Feist6714a252018-09-10 15:26:18 -0700167 }
James Feist71d31b22019-01-02 16:57:54 -0800168 if (errCount >= warnAfterErrorCount)
169 {
170 // only print once
171 if (errCount == warnAfterErrorCount)
172 {
173 std::cerr << "Failure to read sensor " << name << " at " << path
174 << " ec:" << err << "\n";
175 }
176 updateValue(0);
177 }
James Feist6714a252018-09-10 15:26:18 -0700178 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700179 responseStream.clear();
180 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700181 int fd = open(path.c_str(), O_RDONLY);
182 if (fd <= 0)
183 {
184 return; // we're no longer valid
185 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700186 inputDev.assign(fd);
James Feist7bc2bab2018-10-26 14:09:45 -0700187 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800188 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700189 if (ec == boost::asio::error::operation_aborted)
190 {
191 return; // we're being canceled
192 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700193 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700194 });
195}
196
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700197void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700198{
James Feist95e54902019-09-04 15:13:36 -0700199 if (!isPowerOn())
200 {
201 return;
202 }
203
James Feistdc6c55f2018-10-31 12:53:20 -0700204 bool status = thresholds::checkThresholds(this);
James Feist95e54902019-09-04 15:13:36 -0700205
James Feist7b18b1e2019-05-14 13:42:09 -0700206 if (redundancy && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700207 {
James Feist7b18b1e2019-05-14 13:42:09 -0700208 (*redundancy)
209 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700210 }
James Feist6714a252018-09-10 15:26:18 -0700211}
212
James Feist7bc2bab2018-10-26 14:09:45 -0700213PresenceSensor::PresenceSensor(const size_t index, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -0700214 boost::asio::io_service& io,
215 const std::string& name) :
James Feist7bc2bab2018-10-26 14:09:45 -0700216 inverted(inverted),
James Feist7b18b1e2019-05-14 13:42:09 -0700217 inputDev(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700218{
219 // todo: use gpiodaemon
220 std::string device = gpioPath + std::string("gpio") + std::to_string(index);
221 fd = open((device + "/value").c_str(), O_RDONLY);
222 if (fd < 0)
223 {
224 std::cerr << "Error opening gpio " << index << "\n";
225 return;
226 }
227
228 std::ofstream deviceFile(device + "/edge");
229 if (!deviceFile.good())
230 {
231 std::cerr << "Error setting edge " << device << "\n";
232 return;
233 }
234 deviceFile << "both";
235 deviceFile.close();
236
237 inputDev.assign(boost::asio::ip::tcp::v4(), fd);
238 monitorPresence();
239 read();
240}
241
242PresenceSensor::~PresenceSensor()
243{
244 inputDev.close();
245 close(fd);
246}
247
248void PresenceSensor::monitorPresence(void)
249{
250 inputDev.async_wait(boost::asio::ip::tcp::socket::wait_error,
James Feistd8705872019-02-08 13:26:09 -0800251 [this](const boost::system::error_code& ec) {
James Feist7bc2bab2018-10-26 14:09:45 -0700252 if (ec == boost::system::errc::bad_file_descriptor)
253 {
254 return; // we're being destroyed
255 }
256 else if (ec)
257 {
258 std::cerr
259 << "Error on presence sensor socket\n";
260 }
261 else
262 {
263 read();
264 }
265 monitorPresence();
266 });
267}
268
269void PresenceSensor::read(void)
270{
271 constexpr size_t readSize = sizeof("0");
272 std::string readBuf;
273 readBuf.resize(readSize);
274 lseek(fd, 0, SEEK_SET);
275 size_t r = ::read(fd, readBuf.data(), readSize);
James Feist95b079b2018-11-21 09:28:00 -0800276 if (r != readSize)
James Feist7bc2bab2018-10-26 14:09:45 -0700277 {
278 std::cerr << "Error reading gpio\n";
279 }
280 else
281 {
282 bool value = std::stoi(readBuf);
283 if (inverted)
284 {
285 value = !value;
286 }
James Feist7b18b1e2019-05-14 13:42:09 -0700287 if (value != status)
288 {
289 status = value;
290 if (status)
291 {
292 logFanInserted(name);
293 }
294 else
295 {
296 logFanRemoved(name);
297 }
298 }
James Feist7bc2bab2018-10-26 14:09:45 -0700299 }
300}
301
302bool PresenceSensor::getValue(void)
303{
304 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700305}
306
James Feist9bb67462019-03-15 15:09:50 -0700307RedundancySensor::RedundancySensor(size_t count,
308 const std::vector<std::string>& children,
309 sdbusplus::asio::object_server& objectServer,
310 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700311 count(count),
312 iface(objectServer.add_interface(
313 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700314 "xyz.openbmc_project.Control.FanRedundancy")),
315 association(objectServer.add_interface(
316 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist2adc95c2019-09-30 14:55:28 -0700317 association::interface)),
James Feistdc6c55f2018-10-31 12:53:20 -0700318 objectServer(objectServer)
319{
James Feist9bb67462019-03-15 15:09:50 -0700320 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700321 iface->register_property("Collection", children);
322 iface->register_property("Status", std::string("Full"));
323 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
324 iface->initialize();
325}
326RedundancySensor::~RedundancySensor()
327{
James Feist9bb67462019-03-15 15:09:50 -0700328 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700329 objectServer.remove_interface(iface);
330}
James Feistd8705872019-02-08 13:26:09 -0800331void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700332{
333 statuses[name] = failed;
334 size_t failedCount = 0;
335
James Feist7b18b1e2019-05-14 13:42:09 -0700336 std::string newState = redundancy::full;
James Feistd8705872019-02-08 13:26:09 -0800337 for (const auto& status : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700338 {
339 if (status.second)
340 {
341 failedCount++;
342 }
343 if (failedCount > count)
344 {
James Feist7b18b1e2019-05-14 13:42:09 -0700345 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700346 break;
347 }
348 else if (failedCount)
349 {
James Feist7b18b1e2019-05-14 13:42:09 -0700350 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700351 }
352 }
James Feist7b18b1e2019-05-14 13:42:09 -0700353 if (state != newState)
354 {
355 if (state == redundancy::full)
356 {
357 logFanRedundancyLost();
358 }
359 else if (newState == redundancy::full)
360 {
361 logFanRedundancyRestored();
362 }
363 state = newState;
364 iface->set_property("Status", state);
365 }
James Feistdc6c55f2018-10-31 12:53:20 -0700366}