blob: aeef7c50b44d48fdf1e3470839daaf6b125ab2cc [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 =
74 objectServer.add_interface("/xyz/openbmc_project/Inventory/" + name,
75 "xyz.openbmc_project.Inventory.Item");
76 itemIface->register_property("PrettyName",
77 std::string()); // unused property
78 itemIface->register_property("Present", true);
79 itemIface->initialize();
80 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070081 setInitialProperties(conn);
James Feist6ef20402019-01-07 16:45:08 -080082 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070083 setupRead();
James Feist6714a252018-09-10 15:26:18 -070084}
85
86TachSensor::~TachSensor()
87{
88 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070089 inputDev.close();
90 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070091 objServer.remove_interface(thresholdInterfaceWarning);
92 objServer.remove_interface(thresholdInterfaceCritical);
93 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080094 objServer.remove_interface(association);
James Feist60c0ec72019-03-18 15:08:43 -070095 objServer.remove_interface(itemIface);
James Feist6714a252018-09-10 15:26:18 -070096}
97
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070098void TachSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070099{
100 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700101 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -0800102 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700103 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700104}
105
James Feistd8705872019-02-08 13:26:09 -0800106void TachSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700107{
108 if (err == boost::system::errc::bad_file_descriptor)
109 {
110 return; // we're being destroyed
111 }
James Feist7bc2bab2018-10-26 14:09:45 -0700112 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700113 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700114 if (presence)
115 {
116 if (!presence->getValue())
117 {
James Feist1169eb42018-10-31 10:08:47 -0700118 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700119 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700120 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700121 }
James Feist60c0ec72019-03-18 15:08:43 -0700122 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700123 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700124 std::istream responseStream(&readBuf);
James Feist7bc2bab2018-10-26 14:09:45 -0700125 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700126 {
James Feist7bc2bab2018-10-26 14:09:45 -0700127 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700128 {
James Feist7bc2bab2018-10-26 14:09:45 -0700129 std::string response;
130 try
James Feist6714a252018-09-10 15:26:18 -0700131 {
James Feist7bc2bab2018-10-26 14:09:45 -0700132 std::getline(responseStream, response);
133 float nvalue = std::stof(response);
134 responseStream.clear();
135 if (nvalue != value)
136 {
137 updateValue(nvalue);
138 }
139 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700140 }
James Feistd8705872019-02-08 13:26:09 -0800141 catch (const std::invalid_argument&)
James Feist7bc2bab2018-10-26 14:09:45 -0700142 {
143 errCount++;
144 }
James Feist6714a252018-09-10 15:26:18 -0700145 }
146 else
147 {
James Feist71d31b22019-01-02 16:57:54 -0800148 if (!isPowerOn())
James Feist7bc2bab2018-10-26 14:09:45 -0700149 {
James Feist71d31b22019-01-02 16:57:54 -0800150 errCount = 0;
151 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700152 }
153 else
154 {
James Feist71d31b22019-01-02 16:57:54 -0800155 pollTime = sensorFailedPollTimeMs;
156 errCount++;
James Feist7bc2bab2018-10-26 14:09:45 -0700157 }
James Feist6714a252018-09-10 15:26:18 -0700158 }
James Feist71d31b22019-01-02 16:57:54 -0800159 if (errCount >= warnAfterErrorCount)
160 {
161 // only print once
162 if (errCount == warnAfterErrorCount)
163 {
164 std::cerr << "Failure to read sensor " << name << " at " << path
165 << " ec:" << err << "\n";
166 }
167 updateValue(0);
168 }
James Feist6714a252018-09-10 15:26:18 -0700169 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700170 responseStream.clear();
171 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700172 int fd = open(path.c_str(), O_RDONLY);
173 if (fd <= 0)
174 {
175 return; // we're no longer valid
176 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700177 inputDev.assign(fd);
James Feist7bc2bab2018-10-26 14:09:45 -0700178 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800179 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700180 if (ec == boost::asio::error::operation_aborted)
181 {
182 return; // we're being canceled
183 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700184 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700185 });
186}
187
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700188void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700189{
James Feistdc6c55f2018-10-31 12:53:20 -0700190 bool status = thresholds::checkThresholds(this);
James Feist7b18b1e2019-05-14 13:42:09 -0700191 if (redundancy && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700192 {
James Feist7b18b1e2019-05-14 13:42:09 -0700193 (*redundancy)
194 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700195 }
James Feist6714a252018-09-10 15:26:18 -0700196}
197
James Feist7bc2bab2018-10-26 14:09:45 -0700198PresenceSensor::PresenceSensor(const size_t index, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -0700199 boost::asio::io_service& io,
200 const std::string& name) :
James Feist7bc2bab2018-10-26 14:09:45 -0700201 inverted(inverted),
James Feist7b18b1e2019-05-14 13:42:09 -0700202 inputDev(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700203{
204 // todo: use gpiodaemon
205 std::string device = gpioPath + std::string("gpio") + std::to_string(index);
206 fd = open((device + "/value").c_str(), O_RDONLY);
207 if (fd < 0)
208 {
209 std::cerr << "Error opening gpio " << index << "\n";
210 return;
211 }
212
213 std::ofstream deviceFile(device + "/edge");
214 if (!deviceFile.good())
215 {
216 std::cerr << "Error setting edge " << device << "\n";
217 return;
218 }
219 deviceFile << "both";
220 deviceFile.close();
221
222 inputDev.assign(boost::asio::ip::tcp::v4(), fd);
223 monitorPresence();
224 read();
225}
226
227PresenceSensor::~PresenceSensor()
228{
229 inputDev.close();
230 close(fd);
231}
232
233void PresenceSensor::monitorPresence(void)
234{
235 inputDev.async_wait(boost::asio::ip::tcp::socket::wait_error,
James Feistd8705872019-02-08 13:26:09 -0800236 [this](const boost::system::error_code& ec) {
James Feist7bc2bab2018-10-26 14:09:45 -0700237 if (ec == boost::system::errc::bad_file_descriptor)
238 {
239 return; // we're being destroyed
240 }
241 else if (ec)
242 {
243 std::cerr
244 << "Error on presence sensor socket\n";
245 }
246 else
247 {
248 read();
249 }
250 monitorPresence();
251 });
252}
253
254void PresenceSensor::read(void)
255{
256 constexpr size_t readSize = sizeof("0");
257 std::string readBuf;
258 readBuf.resize(readSize);
259 lseek(fd, 0, SEEK_SET);
260 size_t r = ::read(fd, readBuf.data(), readSize);
James Feist95b079b2018-11-21 09:28:00 -0800261 if (r != readSize)
James Feist7bc2bab2018-10-26 14:09:45 -0700262 {
263 std::cerr << "Error reading gpio\n";
264 }
265 else
266 {
267 bool value = std::stoi(readBuf);
268 if (inverted)
269 {
270 value = !value;
271 }
James Feist7b18b1e2019-05-14 13:42:09 -0700272 if (value != status)
273 {
274 status = value;
275 if (status)
276 {
277 logFanInserted(name);
278 }
279 else
280 {
281 logFanRemoved(name);
282 }
283 }
James Feist7bc2bab2018-10-26 14:09:45 -0700284 }
285}
286
287bool PresenceSensor::getValue(void)
288{
289 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700290}
291
James Feist9bb67462019-03-15 15:09:50 -0700292RedundancySensor::RedundancySensor(size_t count,
293 const std::vector<std::string>& children,
294 sdbusplus::asio::object_server& objectServer,
295 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700296 count(count),
297 iface(objectServer.add_interface(
298 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700299 "xyz.openbmc_project.Control.FanRedundancy")),
300 association(objectServer.add_interface(
301 "/xyz/openbmc_project/control/FanRedundancy/Tach",
302 "org.openbmc.Associations")),
James Feistdc6c55f2018-10-31 12:53:20 -0700303 objectServer(objectServer)
304{
James Feist9bb67462019-03-15 15:09:50 -0700305 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700306 iface->register_property("Collection", children);
307 iface->register_property("Status", std::string("Full"));
308 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
309 iface->initialize();
310}
311RedundancySensor::~RedundancySensor()
312{
James Feist9bb67462019-03-15 15:09:50 -0700313 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700314 objectServer.remove_interface(iface);
315}
James Feistd8705872019-02-08 13:26:09 -0800316void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700317{
318 statuses[name] = failed;
319 size_t failedCount = 0;
320
James Feist7b18b1e2019-05-14 13:42:09 -0700321 std::string newState = redundancy::full;
James Feistd8705872019-02-08 13:26:09 -0800322 for (const auto& status : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700323 {
324 if (status.second)
325 {
326 failedCount++;
327 }
328 if (failedCount > count)
329 {
James Feist7b18b1e2019-05-14 13:42:09 -0700330 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700331 break;
332 }
333 else if (failedCount)
334 {
James Feist7b18b1e2019-05-14 13:42:09 -0700335 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700336 }
337 }
James Feist7b18b1e2019-05-14 13:42:09 -0700338 if (state != newState)
339 {
340 if (state == redundancy::full)
341 {
342 logFanRedundancyLost();
343 }
344 else if (newState == redundancy::full)
345 {
346 logFanRedundancyRestored();
347 }
348 state = newState;
349 iface->set_property("Status", state);
350 }
James Feistdc6c55f2018-10-31 12:53:20 -0700351}