blob: c8424641b01cfdf65ba2f64d3203893c1776967a [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
19#include <TachSensor.hpp>
20#include <Utils.hpp>
21#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
23#include <boost/date_time/posix_time/posix_time.hpp>
James Feist7bc2bab2018-10-26 14:09:45 -070024#include <fstream>
James Feist6714a252018-09-10 15:26:18 -070025#include <iostream>
26#include <limits>
27#include <sdbusplus/asio/connection.hpp>
28#include <sdbusplus/asio/object_server.hpp>
29#include <string>
30
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070031static constexpr unsigned int pwmPollMs = 500;
32static constexpr size_t warnAfterErrorCount = 10;
James Feist6714a252018-09-10 15:26:18 -070033
James Feistd8705872019-02-08 13:26:09 -080034TachSensor::TachSensor(const std::string& path, const std::string& objectType,
35 sdbusplus::asio::object_server& objectServer,
36 std::shared_ptr<sdbusplus::asio::connection>& conn,
37 std::unique_ptr<PresenceSensor>&& presence,
38 const std::shared_ptr<RedundancySensor>& redundancy,
39 boost::asio::io_service& io, const std::string& fanName,
40 std::vector<thresholds::Threshold>&& _thresholds,
41 const std::string& sensorConfiguration,
42 const std::pair<size_t, size_t>& limits) :
James Feistdc6c55f2018-10-31 12:53:20 -070043 Sensor(boost::replace_all_copy(fanName, " ", "_"), path,
James Feist87d713a2018-12-06 16:06:24 -080044 std::move(_thresholds), sensorConfiguration, objectType,
45 limits.second, limits.first),
James Feist71d31b22019-01-02 16:57:54 -080046 objServer(objectServer), presence(std::move(presence)),
47 redundancy(redundancy), inputDev(io, open(path.c_str(), O_RDONLY)),
48 waitTimer(io), errCount(0)
James Feist6714a252018-09-10 15:26:18 -070049{
James Feist251c7822018-09-12 12:54:15 -070050 sensorInterface = objectServer.add_interface(
51 "/xyz/openbmc_project/sensors/fan_tach/" + name,
52 "xyz.openbmc_project.Sensor.Value");
53
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070054 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070055 {
James Feist251c7822018-09-12 12:54:15 -070056 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070057 "/xyz/openbmc_project/sensors/fan_tach/" + name,
58 "xyz.openbmc_project.Sensor.Threshold.Warning");
59 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070060 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070061 {
James Feist251c7822018-09-12 12:54:15 -070062 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070063 "/xyz/openbmc_project/sensors/fan_tach/" + name,
64 "xyz.openbmc_project.Sensor.Threshold.Critical");
65 }
James Feist078f2322019-03-08 11:09:05 -080066 association = objectServer.add_interface(
67 "/xyz/openbmc_project/sensors/fan_tach/" + name,
68 "org.openbmc.Associations");
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070069 setInitialProperties(conn);
James Feist6ef20402019-01-07 16:45:08 -080070 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070071 setupRead();
James Feist6714a252018-09-10 15:26:18 -070072}
73
74TachSensor::~TachSensor()
75{
76 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070077 inputDev.close();
78 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070079 objServer.remove_interface(thresholdInterfaceWarning);
80 objServer.remove_interface(thresholdInterfaceCritical);
81 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080082 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070083}
84
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070085void TachSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070086{
87 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070088 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -080089 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070090 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -070091}
92
James Feistd8705872019-02-08 13:26:09 -080093void TachSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -070094{
95 if (err == boost::system::errc::bad_file_descriptor)
96 {
97 return; // we're being destroyed
98 }
James Feist7bc2bab2018-10-26 14:09:45 -070099 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700100 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700101 if (presence)
102 {
103 if (!presence->getValue())
104 {
James Feist1169eb42018-10-31 10:08:47 -0700105 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700106 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700107 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700108 }
109 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700110 std::istream responseStream(&readBuf);
James Feist7bc2bab2018-10-26 14:09:45 -0700111 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700112 {
James Feist7bc2bab2018-10-26 14:09:45 -0700113 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700114 {
James Feist7bc2bab2018-10-26 14:09:45 -0700115 std::string response;
116 try
James Feist6714a252018-09-10 15:26:18 -0700117 {
James Feist7bc2bab2018-10-26 14:09:45 -0700118 std::getline(responseStream, response);
119 float nvalue = std::stof(response);
120 responseStream.clear();
Richard Marian Thomaiyarb2eb29a2018-12-08 18:26:58 +0530121 if (overridenState)
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530122 {
123 nvalue = overriddenValue;
124 }
James Feist7bc2bab2018-10-26 14:09:45 -0700125 if (nvalue != value)
126 {
127 updateValue(nvalue);
128 }
129 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700130 }
James Feistd8705872019-02-08 13:26:09 -0800131 catch (const std::invalid_argument&)
James Feist7bc2bab2018-10-26 14:09:45 -0700132 {
133 errCount++;
134 }
James Feist6714a252018-09-10 15:26:18 -0700135 }
136 else
137 {
James Feist71d31b22019-01-02 16:57:54 -0800138 if (!isPowerOn())
James Feist7bc2bab2018-10-26 14:09:45 -0700139 {
James Feist71d31b22019-01-02 16:57:54 -0800140 errCount = 0;
141 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700142 }
143 else
144 {
James Feist71d31b22019-01-02 16:57:54 -0800145 pollTime = sensorFailedPollTimeMs;
146 errCount++;
James Feist7bc2bab2018-10-26 14:09:45 -0700147 }
James Feist6714a252018-09-10 15:26:18 -0700148 }
James Feist71d31b22019-01-02 16:57:54 -0800149 if (errCount >= warnAfterErrorCount)
150 {
151 // only print once
152 if (errCount == warnAfterErrorCount)
153 {
154 std::cerr << "Failure to read sensor " << name << " at " << path
155 << " ec:" << err << "\n";
156 }
157 updateValue(0);
158 }
James Feist6714a252018-09-10 15:26:18 -0700159 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700160 responseStream.clear();
161 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700162 int fd = open(path.c_str(), O_RDONLY);
163 if (fd <= 0)
164 {
165 return; // we're no longer valid
166 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700167 inputDev.assign(fd);
James Feist7bc2bab2018-10-26 14:09:45 -0700168 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800169 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700170 if (ec == boost::asio::error::operation_aborted)
171 {
172 return; // we're being canceled
173 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700174 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700175 });
176}
177
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700178void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700179{
James Feistdc6c55f2018-10-31 12:53:20 -0700180 bool status = thresholds::checkThresholds(this);
181 if (redundancy)
182 {
183 redundancy->update("/xyz/openbmc_project/sensors/fan_tach/" + name,
184 !status);
185 }
James Feist6714a252018-09-10 15:26:18 -0700186}
187
James Feist7bc2bab2018-10-26 14:09:45 -0700188PresenceSensor::PresenceSensor(const size_t index, bool inverted,
James Feistd8705872019-02-08 13:26:09 -0800189 boost::asio::io_service& io) :
James Feist7bc2bab2018-10-26 14:09:45 -0700190 inverted(inverted),
191 inputDev(io)
192{
193 // todo: use gpiodaemon
194 std::string device = gpioPath + std::string("gpio") + std::to_string(index);
195 fd = open((device + "/value").c_str(), O_RDONLY);
196 if (fd < 0)
197 {
198 std::cerr << "Error opening gpio " << index << "\n";
199 return;
200 }
201
202 std::ofstream deviceFile(device + "/edge");
203 if (!deviceFile.good())
204 {
205 std::cerr << "Error setting edge " << device << "\n";
206 return;
207 }
208 deviceFile << "both";
209 deviceFile.close();
210
211 inputDev.assign(boost::asio::ip::tcp::v4(), fd);
212 monitorPresence();
213 read();
214}
215
216PresenceSensor::~PresenceSensor()
217{
218 inputDev.close();
219 close(fd);
220}
221
222void PresenceSensor::monitorPresence(void)
223{
224 inputDev.async_wait(boost::asio::ip::tcp::socket::wait_error,
James Feistd8705872019-02-08 13:26:09 -0800225 [this](const boost::system::error_code& ec) {
James Feist7bc2bab2018-10-26 14:09:45 -0700226 if (ec == boost::system::errc::bad_file_descriptor)
227 {
228 return; // we're being destroyed
229 }
230 else if (ec)
231 {
232 std::cerr
233 << "Error on presence sensor socket\n";
234 }
235 else
236 {
237 read();
238 }
239 monitorPresence();
240 });
241}
242
243void PresenceSensor::read(void)
244{
245 constexpr size_t readSize = sizeof("0");
246 std::string readBuf;
247 readBuf.resize(readSize);
248 lseek(fd, 0, SEEK_SET);
249 size_t r = ::read(fd, readBuf.data(), readSize);
James Feist95b079b2018-11-21 09:28:00 -0800250 if (r != readSize)
James Feist7bc2bab2018-10-26 14:09:45 -0700251 {
252 std::cerr << "Error reading gpio\n";
253 }
254 else
255 {
256 bool value = std::stoi(readBuf);
257 if (inverted)
258 {
259 value = !value;
260 }
261 status = value;
262 }
263}
264
265bool PresenceSensor::getValue(void)
266{
267 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700268}
269
270RedundancySensor::RedundancySensor(
James Feistd8705872019-02-08 13:26:09 -0800271 size_t count, const std::vector<std::string>& children,
272 sdbusplus::asio::object_server& objectServer) :
James Feistdc6c55f2018-10-31 12:53:20 -0700273 count(count),
274 iface(objectServer.add_interface(
275 "/xyz/openbmc_project/control/FanRedundancy/Tach",
276 "xyz.openbmc_project.control.FanRedundancy")),
277 objectServer(objectServer)
278{
279 iface->register_property("Collection", children);
280 iface->register_property("Status", std::string("Full"));
281 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
282 iface->initialize();
283}
284RedundancySensor::~RedundancySensor()
285{
286 objectServer.remove_interface(iface);
287}
James Feistd8705872019-02-08 13:26:09 -0800288void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700289{
290 statuses[name] = failed;
291 size_t failedCount = 0;
292
293 std::string state = "Full";
James Feistd8705872019-02-08 13:26:09 -0800294 for (const auto& status : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700295 {
296 if (status.second)
297 {
298 failedCount++;
299 }
300 if (failedCount > count)
301 {
302 state = "Failed";
303 break;
304 }
305 else if (failedCount)
306 {
307 state = "Degraded";
308 }
309 }
310 iface->set_property("Status", state);
311}