blob: 46f53898fc82a22253ff987474ce39e96aacbe0b [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 Feistdc6c55f2018-10-31 12:53:20 -0700200 bool status = thresholds::checkThresholds(this);
James Feist7b18b1e2019-05-14 13:42:09 -0700201 if (redundancy && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700202 {
James Feist7b18b1e2019-05-14 13:42:09 -0700203 (*redundancy)
204 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700205 }
James Feist6714a252018-09-10 15:26:18 -0700206}
207
James Feist7bc2bab2018-10-26 14:09:45 -0700208PresenceSensor::PresenceSensor(const size_t index, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -0700209 boost::asio::io_service& io,
210 const std::string& name) :
James Feist7bc2bab2018-10-26 14:09:45 -0700211 inverted(inverted),
James Feist7b18b1e2019-05-14 13:42:09 -0700212 inputDev(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700213{
214 // todo: use gpiodaemon
215 std::string device = gpioPath + std::string("gpio") + std::to_string(index);
216 fd = open((device + "/value").c_str(), O_RDONLY);
217 if (fd < 0)
218 {
219 std::cerr << "Error opening gpio " << index << "\n";
220 return;
221 }
222
223 std::ofstream deviceFile(device + "/edge");
224 if (!deviceFile.good())
225 {
226 std::cerr << "Error setting edge " << device << "\n";
227 return;
228 }
229 deviceFile << "both";
230 deviceFile.close();
231
232 inputDev.assign(boost::asio::ip::tcp::v4(), fd);
233 monitorPresence();
234 read();
235}
236
237PresenceSensor::~PresenceSensor()
238{
239 inputDev.close();
240 close(fd);
241}
242
243void PresenceSensor::monitorPresence(void)
244{
245 inputDev.async_wait(boost::asio::ip::tcp::socket::wait_error,
James Feistd8705872019-02-08 13:26:09 -0800246 [this](const boost::system::error_code& ec) {
James Feist7bc2bab2018-10-26 14:09:45 -0700247 if (ec == boost::system::errc::bad_file_descriptor)
248 {
249 return; // we're being destroyed
250 }
251 else if (ec)
252 {
253 std::cerr
254 << "Error on presence sensor socket\n";
255 }
256 else
257 {
258 read();
259 }
260 monitorPresence();
261 });
262}
263
264void PresenceSensor::read(void)
265{
266 constexpr size_t readSize = sizeof("0");
267 std::string readBuf;
268 readBuf.resize(readSize);
269 lseek(fd, 0, SEEK_SET);
270 size_t r = ::read(fd, readBuf.data(), readSize);
James Feist95b079b2018-11-21 09:28:00 -0800271 if (r != readSize)
James Feist7bc2bab2018-10-26 14:09:45 -0700272 {
273 std::cerr << "Error reading gpio\n";
274 }
275 else
276 {
277 bool value = std::stoi(readBuf);
278 if (inverted)
279 {
280 value = !value;
281 }
James Feist7b18b1e2019-05-14 13:42:09 -0700282 if (value != status)
283 {
284 status = value;
285 if (status)
286 {
287 logFanInserted(name);
288 }
289 else
290 {
291 logFanRemoved(name);
292 }
293 }
James Feist7bc2bab2018-10-26 14:09:45 -0700294 }
295}
296
297bool PresenceSensor::getValue(void)
298{
299 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700300}
301
James Feist9bb67462019-03-15 15:09:50 -0700302RedundancySensor::RedundancySensor(size_t count,
303 const std::vector<std::string>& children,
304 sdbusplus::asio::object_server& objectServer,
305 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700306 count(count),
307 iface(objectServer.add_interface(
308 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700309 "xyz.openbmc_project.Control.FanRedundancy")),
310 association(objectServer.add_interface(
311 "/xyz/openbmc_project/control/FanRedundancy/Tach",
312 "org.openbmc.Associations")),
James Feistdc6c55f2018-10-31 12:53:20 -0700313 objectServer(objectServer)
314{
James Feist9bb67462019-03-15 15:09:50 -0700315 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700316 iface->register_property("Collection", children);
317 iface->register_property("Status", std::string("Full"));
318 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
319 iface->initialize();
320}
321RedundancySensor::~RedundancySensor()
322{
James Feist9bb67462019-03-15 15:09:50 -0700323 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700324 objectServer.remove_interface(iface);
325}
James Feistd8705872019-02-08 13:26:09 -0800326void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700327{
328 statuses[name] = failed;
329 size_t failedCount = 0;
330
James Feist7b18b1e2019-05-14 13:42:09 -0700331 std::string newState = redundancy::full;
James Feistd8705872019-02-08 13:26:09 -0800332 for (const auto& status : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700333 {
334 if (status.second)
335 {
336 failedCount++;
337 }
338 if (failedCount > count)
339 {
James Feist7b18b1e2019-05-14 13:42:09 -0700340 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700341 break;
342 }
343 else if (failedCount)
344 {
James Feist7b18b1e2019-05-14 13:42:09 -0700345 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700346 }
347 }
James Feist7b18b1e2019-05-14 13:42:09 -0700348 if (state != newState)
349 {
350 if (state == redundancy::full)
351 {
352 logFanRedundancyLost();
353 }
354 else if (newState == redundancy::full)
355 {
356 logFanRedundancyRestored();
357 }
358 state = newState;
359 iface->set_property("Status", state);
360 }
James Feistdc6c55f2018-10-31 12:53:20 -0700361}