blob: 23ae47749e414b8373f770de8ab88f735d5adc24 [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,
69 "org.openbmc.Associations");
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 Feistd8bd5622019-06-26 12:09:05 -070080 itemAssoc =
81 objectServer.add_interface("/xyz/openbmc_project/inventory/" + name,
82 "org.openbmc.Associations");
83 itemAssoc->register_property(
84 "associations",
85 std::vector<Association>{
86 {"sensors", "inventory",
87 "/xyz/openbmc_project/sensors/fan_tach/" + name}});
88 itemAssoc->initialize();
James Feist60c0ec72019-03-18 15:08:43 -070089 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070090 setInitialProperties(conn);
James Feist6ef20402019-01-07 16:45:08 -080091 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070092 setupRead();
James Feist6714a252018-09-10 15:26:18 -070093}
94
95TachSensor::~TachSensor()
96{
97 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070098 inputDev.close();
99 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -0700100 objServer.remove_interface(thresholdInterfaceWarning);
101 objServer.remove_interface(thresholdInterfaceCritical);
102 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800103 objServer.remove_interface(association);
James Feist60c0ec72019-03-18 15:08:43 -0700104 objServer.remove_interface(itemIface);
James Feistd8bd5622019-06-26 12:09:05 -0700105 objServer.remove_interface(itemAssoc);
James Feist6714a252018-09-10 15:26:18 -0700106}
107
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700108void TachSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700109{
110 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700111 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -0800112 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700113 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700114}
115
James Feistd8705872019-02-08 13:26:09 -0800116void TachSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700117{
118 if (err == boost::system::errc::bad_file_descriptor)
119 {
120 return; // we're being destroyed
121 }
James Feist7bc2bab2018-10-26 14:09:45 -0700122 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700123 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700124 if (presence)
125 {
126 if (!presence->getValue())
127 {
James Feist1169eb42018-10-31 10:08:47 -0700128 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700129 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700130 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700131 }
James Feist60c0ec72019-03-18 15:08:43 -0700132 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700133 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700134 std::istream responseStream(&readBuf);
James Feist7bc2bab2018-10-26 14:09:45 -0700135 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700136 {
James Feist7bc2bab2018-10-26 14:09:45 -0700137 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700138 {
James Feist7bc2bab2018-10-26 14:09:45 -0700139 std::string response;
140 try
James Feist6714a252018-09-10 15:26:18 -0700141 {
James Feist7bc2bab2018-10-26 14:09:45 -0700142 std::getline(responseStream, response);
143 float nvalue = std::stof(response);
144 responseStream.clear();
James Feistb6c0b912019-07-09 12:21:44 -0700145 if (static_cast<double>(nvalue) != value)
James Feist7bc2bab2018-10-26 14:09:45 -0700146 {
147 updateValue(nvalue);
148 }
149 errCount = 0;
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 {
153 errCount++;
154 }
James Feist6714a252018-09-10 15:26:18 -0700155 }
156 else
157 {
James Feist71d31b22019-01-02 16:57:54 -0800158 if (!isPowerOn())
James Feist7bc2bab2018-10-26 14:09:45 -0700159 {
James Feist71d31b22019-01-02 16:57:54 -0800160 errCount = 0;
161 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700162 }
163 else
164 {
James Feist71d31b22019-01-02 16:57:54 -0800165 pollTime = sensorFailedPollTimeMs;
166 errCount++;
James Feist7bc2bab2018-10-26 14:09:45 -0700167 }
James Feist6714a252018-09-10 15:26:18 -0700168 }
James Feist71d31b22019-01-02 16:57:54 -0800169 if (errCount >= warnAfterErrorCount)
170 {
171 // only print once
172 if (errCount == warnAfterErrorCount)
173 {
174 std::cerr << "Failure to read sensor " << name << " at " << path
175 << " ec:" << err << "\n";
176 }
177 updateValue(0);
178 }
James Feist6714a252018-09-10 15:26:18 -0700179 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700180 responseStream.clear();
181 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700182 int fd = open(path.c_str(), O_RDONLY);
183 if (fd <= 0)
184 {
185 return; // we're no longer valid
186 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700187 inputDev.assign(fd);
James Feist7bc2bab2018-10-26 14:09:45 -0700188 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800189 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700190 if (ec == boost::asio::error::operation_aborted)
191 {
192 return; // we're being canceled
193 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700194 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700195 });
196}
197
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700198void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700199{
James Feist95e54902019-09-04 15:13:36 -0700200 if (!isPowerOn())
201 {
202 return;
203 }
204
James Feistdc6c55f2018-10-31 12:53:20 -0700205 bool status = thresholds::checkThresholds(this);
James Feist95e54902019-09-04 15:13:36 -0700206
James Feist7b18b1e2019-05-14 13:42:09 -0700207 if (redundancy && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700208 {
James Feist7b18b1e2019-05-14 13:42:09 -0700209 (*redundancy)
210 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700211 }
James Feist6714a252018-09-10 15:26:18 -0700212}
213
James Feist7bc2bab2018-10-26 14:09:45 -0700214PresenceSensor::PresenceSensor(const size_t index, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -0700215 boost::asio::io_service& io,
216 const std::string& name) :
James Feist7bc2bab2018-10-26 14:09:45 -0700217 inverted(inverted),
James Feist7b18b1e2019-05-14 13:42:09 -0700218 inputDev(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700219{
220 // todo: use gpiodaemon
221 std::string device = gpioPath + std::string("gpio") + std::to_string(index);
222 fd = open((device + "/value").c_str(), O_RDONLY);
223 if (fd < 0)
224 {
225 std::cerr << "Error opening gpio " << index << "\n";
226 return;
227 }
228
229 std::ofstream deviceFile(device + "/edge");
230 if (!deviceFile.good())
231 {
232 std::cerr << "Error setting edge " << device << "\n";
233 return;
234 }
235 deviceFile << "both";
236 deviceFile.close();
237
238 inputDev.assign(boost::asio::ip::tcp::v4(), fd);
239 monitorPresence();
240 read();
241}
242
243PresenceSensor::~PresenceSensor()
244{
245 inputDev.close();
246 close(fd);
247}
248
249void PresenceSensor::monitorPresence(void)
250{
251 inputDev.async_wait(boost::asio::ip::tcp::socket::wait_error,
James Feistd8705872019-02-08 13:26:09 -0800252 [this](const boost::system::error_code& ec) {
James Feist7bc2bab2018-10-26 14:09:45 -0700253 if (ec == boost::system::errc::bad_file_descriptor)
254 {
255 return; // we're being destroyed
256 }
257 else if (ec)
258 {
259 std::cerr
260 << "Error on presence sensor socket\n";
261 }
262 else
263 {
264 read();
265 }
266 monitorPresence();
267 });
268}
269
270void PresenceSensor::read(void)
271{
272 constexpr size_t readSize = sizeof("0");
273 std::string readBuf;
274 readBuf.resize(readSize);
275 lseek(fd, 0, SEEK_SET);
276 size_t r = ::read(fd, readBuf.data(), readSize);
James Feist95b079b2018-11-21 09:28:00 -0800277 if (r != readSize)
James Feist7bc2bab2018-10-26 14:09:45 -0700278 {
279 std::cerr << "Error reading gpio\n";
280 }
281 else
282 {
283 bool value = std::stoi(readBuf);
284 if (inverted)
285 {
286 value = !value;
287 }
James Feist7b18b1e2019-05-14 13:42:09 -0700288 if (value != status)
289 {
290 status = value;
291 if (status)
292 {
293 logFanInserted(name);
294 }
295 else
296 {
297 logFanRemoved(name);
298 }
299 }
James Feist7bc2bab2018-10-26 14:09:45 -0700300 }
301}
302
303bool PresenceSensor::getValue(void)
304{
305 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700306}
307
James Feist9bb67462019-03-15 15:09:50 -0700308RedundancySensor::RedundancySensor(size_t count,
309 const std::vector<std::string>& children,
310 sdbusplus::asio::object_server& objectServer,
311 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700312 count(count),
313 iface(objectServer.add_interface(
314 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700315 "xyz.openbmc_project.Control.FanRedundancy")),
316 association(objectServer.add_interface(
317 "/xyz/openbmc_project/control/FanRedundancy/Tach",
318 "org.openbmc.Associations")),
James Feistdc6c55f2018-10-31 12:53:20 -0700319 objectServer(objectServer)
320{
James Feist9bb67462019-03-15 15:09:50 -0700321 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700322 iface->register_property("Collection", children);
323 iface->register_property("Status", std::string("Full"));
324 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
325 iface->initialize();
326}
327RedundancySensor::~RedundancySensor()
328{
James Feist9bb67462019-03-15 15:09:50 -0700329 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700330 objectServer.remove_interface(iface);
331}
James Feistd8705872019-02-08 13:26:09 -0800332void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700333{
334 statuses[name] = failed;
335 size_t failedCount = 0;
336
James Feist7b18b1e2019-05-14 13:42:09 -0700337 std::string newState = redundancy::full;
James Feistd8705872019-02-08 13:26:09 -0800338 for (const auto& status : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700339 {
340 if (status.second)
341 {
342 failedCount++;
343 }
344 if (failedCount > count)
345 {
James Feist7b18b1e2019-05-14 13:42:09 -0700346 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700347 break;
348 }
349 else if (failedCount)
350 {
James Feist7b18b1e2019-05-14 13:42:09 -0700351 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700352 }
353 }
James Feist7b18b1e2019-05-14 13:42:09 -0700354 if (state != newState)
355 {
356 if (state == redundancy::full)
357 {
358 logFanRedundancyLost();
359 }
360 else if (newState == redundancy::full)
361 {
362 logFanRedundancyRestored();
363 }
364 state = newState;
365 iface->set_property("Status", state);
366 }
James Feistdc6c55f2018-10-31 12:53:20 -0700367}