blob: bbd382f0571fa671e26893bd4ccd9452bc74b7c5 [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 Feistdc6c55f2018-10-31 12:53:20 -070045 Sensor(boost::replace_all_copy(fanName, " ", "_"), path,
James Feist87d713a2018-12-06 16:06:24 -080046 std::move(_thresholds), sensorConfiguration, objectType,
47 limits.second, limits.first),
James Feist60c0ec72019-03-18 15:08:43 -070048 objServer(objectServer), presence(std::move(presenceSensor)),
James Feist71d31b22019-01-02 16:57:54 -080049 redundancy(redundancy), inputDev(io, open(path.c_str(), O_RDONLY)),
50 waitTimer(io), errCount(0)
James Feist6714a252018-09-10 15:26:18 -070051{
James Feist251c7822018-09-12 12:54:15 -070052 sensorInterface = objectServer.add_interface(
53 "/xyz/openbmc_project/sensors/fan_tach/" + name,
54 "xyz.openbmc_project.Sensor.Value");
55
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070056 if (thresholds::hasWarningInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070057 {
James Feist251c7822018-09-12 12:54:15 -070058 thresholdInterfaceWarning = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070059 "/xyz/openbmc_project/sensors/fan_tach/" + name,
60 "xyz.openbmc_project.Sensor.Threshold.Warning");
61 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070062 if (thresholds::hasCriticalInterface(thresholds))
James Feist6714a252018-09-10 15:26:18 -070063 {
James Feist251c7822018-09-12 12:54:15 -070064 thresholdInterfaceCritical = objectServer.add_interface(
James Feist6714a252018-09-10 15:26:18 -070065 "/xyz/openbmc_project/sensors/fan_tach/" + name,
66 "xyz.openbmc_project.Sensor.Threshold.Critical");
67 }
James Feist078f2322019-03-08 11:09:05 -080068 association = objectServer.add_interface(
69 "/xyz/openbmc_project/sensors/fan_tach/" + name,
70 "org.openbmc.Associations");
James Feist60c0ec72019-03-18 15:08:43 -070071
72 if (presence)
73 {
74 itemIface =
75 objectServer.add_interface("/xyz/openbmc_project/Inventory/" + name,
76 "xyz.openbmc_project.Inventory.Item");
77 itemIface->register_property("PrettyName",
78 std::string()); // unused property
79 itemIface->register_property("Present", true);
80 itemIface->initialize();
81 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070082 setInitialProperties(conn);
James Feist6ef20402019-01-07 16:45:08 -080083 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070084 setupRead();
James Feist6714a252018-09-10 15:26:18 -070085}
86
87TachSensor::~TachSensor()
88{
89 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070090 inputDev.close();
91 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070092 objServer.remove_interface(thresholdInterfaceWarning);
93 objServer.remove_interface(thresholdInterfaceCritical);
94 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080095 objServer.remove_interface(association);
James Feist60c0ec72019-03-18 15:08:43 -070096 objServer.remove_interface(itemIface);
James Feist6714a252018-09-10 15:26:18 -070097}
98
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070099void TachSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700100{
101 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700102 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -0800103 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700104 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700105}
106
James Feistd8705872019-02-08 13:26:09 -0800107void TachSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700108{
109 if (err == boost::system::errc::bad_file_descriptor)
110 {
111 return; // we're being destroyed
112 }
James Feist7bc2bab2018-10-26 14:09:45 -0700113 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700114 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700115 if (presence)
116 {
117 if (!presence->getValue())
118 {
James Feist1169eb42018-10-31 10:08:47 -0700119 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700120 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700121 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700122 }
James Feist60c0ec72019-03-18 15:08:43 -0700123 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700124 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700125 std::istream responseStream(&readBuf);
James Feist7bc2bab2018-10-26 14:09:45 -0700126 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700127 {
James Feist7bc2bab2018-10-26 14:09:45 -0700128 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700129 {
James Feist7bc2bab2018-10-26 14:09:45 -0700130 std::string response;
131 try
James Feist6714a252018-09-10 15:26:18 -0700132 {
James Feist7bc2bab2018-10-26 14:09:45 -0700133 std::getline(responseStream, response);
134 float nvalue = std::stof(response);
135 responseStream.clear();
136 if (nvalue != value)
137 {
138 updateValue(nvalue);
139 }
140 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700141 }
James Feistd8705872019-02-08 13:26:09 -0800142 catch (const std::invalid_argument&)
James Feist7bc2bab2018-10-26 14:09:45 -0700143 {
144 errCount++;
145 }
James Feist6714a252018-09-10 15:26:18 -0700146 }
147 else
148 {
James Feist71d31b22019-01-02 16:57:54 -0800149 if (!isPowerOn())
James Feist7bc2bab2018-10-26 14:09:45 -0700150 {
James Feist71d31b22019-01-02 16:57:54 -0800151 errCount = 0;
152 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700153 }
154 else
155 {
James Feist71d31b22019-01-02 16:57:54 -0800156 pollTime = sensorFailedPollTimeMs;
157 errCount++;
James Feist7bc2bab2018-10-26 14:09:45 -0700158 }
James Feist6714a252018-09-10 15:26:18 -0700159 }
James Feist71d31b22019-01-02 16:57:54 -0800160 if (errCount >= warnAfterErrorCount)
161 {
162 // only print once
163 if (errCount == warnAfterErrorCount)
164 {
165 std::cerr << "Failure to read sensor " << name << " at " << path
166 << " ec:" << err << "\n";
167 }
168 updateValue(0);
169 }
James Feist6714a252018-09-10 15:26:18 -0700170 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700171 responseStream.clear();
172 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700173 int fd = open(path.c_str(), O_RDONLY);
174 if (fd <= 0)
175 {
176 return; // we're no longer valid
177 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700178 inputDev.assign(fd);
James Feist7bc2bab2018-10-26 14:09:45 -0700179 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800180 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700181 if (ec == boost::asio::error::operation_aborted)
182 {
183 return; // we're being canceled
184 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700185 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700186 });
187}
188
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700189void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700190{
James Feistdc6c55f2018-10-31 12:53:20 -0700191 bool status = thresholds::checkThresholds(this);
James Feist7b18b1e2019-05-14 13:42:09 -0700192 if (redundancy && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700193 {
James Feist7b18b1e2019-05-14 13:42:09 -0700194 (*redundancy)
195 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700196 }
James Feist6714a252018-09-10 15:26:18 -0700197}
198
James Feist7bc2bab2018-10-26 14:09:45 -0700199PresenceSensor::PresenceSensor(const size_t index, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -0700200 boost::asio::io_service& io,
201 const std::string& name) :
James Feist7bc2bab2018-10-26 14:09:45 -0700202 inverted(inverted),
James Feist7b18b1e2019-05-14 13:42:09 -0700203 inputDev(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700204{
205 // todo: use gpiodaemon
206 std::string device = gpioPath + std::string("gpio") + std::to_string(index);
207 fd = open((device + "/value").c_str(), O_RDONLY);
208 if (fd < 0)
209 {
210 std::cerr << "Error opening gpio " << index << "\n";
211 return;
212 }
213
214 std::ofstream deviceFile(device + "/edge");
215 if (!deviceFile.good())
216 {
217 std::cerr << "Error setting edge " << device << "\n";
218 return;
219 }
220 deviceFile << "both";
221 deviceFile.close();
222
223 inputDev.assign(boost::asio::ip::tcp::v4(), fd);
224 monitorPresence();
225 read();
226}
227
228PresenceSensor::~PresenceSensor()
229{
230 inputDev.close();
231 close(fd);
232}
233
234void PresenceSensor::monitorPresence(void)
235{
236 inputDev.async_wait(boost::asio::ip::tcp::socket::wait_error,
James Feistd8705872019-02-08 13:26:09 -0800237 [this](const boost::system::error_code& ec) {
James Feist7bc2bab2018-10-26 14:09:45 -0700238 if (ec == boost::system::errc::bad_file_descriptor)
239 {
240 return; // we're being destroyed
241 }
242 else if (ec)
243 {
244 std::cerr
245 << "Error on presence sensor socket\n";
246 }
247 else
248 {
249 read();
250 }
251 monitorPresence();
252 });
253}
254
255void PresenceSensor::read(void)
256{
257 constexpr size_t readSize = sizeof("0");
258 std::string readBuf;
259 readBuf.resize(readSize);
260 lseek(fd, 0, SEEK_SET);
261 size_t r = ::read(fd, readBuf.data(), readSize);
James Feist95b079b2018-11-21 09:28:00 -0800262 if (r != readSize)
James Feist7bc2bab2018-10-26 14:09:45 -0700263 {
264 std::cerr << "Error reading gpio\n";
265 }
266 else
267 {
268 bool value = std::stoi(readBuf);
269 if (inverted)
270 {
271 value = !value;
272 }
James Feist7b18b1e2019-05-14 13:42:09 -0700273 if (value != status)
274 {
275 status = value;
276 if (status)
277 {
278 logFanInserted(name);
279 }
280 else
281 {
282 logFanRemoved(name);
283 }
284 }
James Feist7bc2bab2018-10-26 14:09:45 -0700285 }
286}
287
288bool PresenceSensor::getValue(void)
289{
290 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700291}
292
James Feist9bb67462019-03-15 15:09:50 -0700293RedundancySensor::RedundancySensor(size_t count,
294 const std::vector<std::string>& children,
295 sdbusplus::asio::object_server& objectServer,
296 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700297 count(count),
298 iface(objectServer.add_interface(
299 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700300 "xyz.openbmc_project.Control.FanRedundancy")),
301 association(objectServer.add_interface(
302 "/xyz/openbmc_project/control/FanRedundancy/Tach",
303 "org.openbmc.Associations")),
James Feistdc6c55f2018-10-31 12:53:20 -0700304 objectServer(objectServer)
305{
James Feist9bb67462019-03-15 15:09:50 -0700306 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700307 iface->register_property("Collection", children);
308 iface->register_property("Status", std::string("Full"));
309 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
310 iface->initialize();
311}
312RedundancySensor::~RedundancySensor()
313{
James Feist9bb67462019-03-15 15:09:50 -0700314 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700315 objectServer.remove_interface(iface);
316}
James Feistd8705872019-02-08 13:26:09 -0800317void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700318{
319 statuses[name] = failed;
320 size_t failedCount = 0;
321
James Feist7b18b1e2019-05-14 13:42:09 -0700322 std::string newState = redundancy::full;
James Feistd8705872019-02-08 13:26:09 -0800323 for (const auto& status : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700324 {
325 if (status.second)
326 {
327 failedCount++;
328 }
329 if (failedCount > count)
330 {
James Feist7b18b1e2019-05-14 13:42:09 -0700331 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700332 break;
333 }
334 else if (failedCount)
335 {
James Feist7b18b1e2019-05-14 13:42:09 -0700336 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700337 }
338 }
James Feist7b18b1e2019-05-14 13:42:09 -0700339 if (state != newState)
340 {
341 if (state == redundancy::full)
342 {
343 logFanRedundancyLost();
344 }
345 else if (newState == redundancy::full)
346 {
347 logFanRedundancyRestored();
348 }
349 state = newState;
350 iface->set_property("Status", state);
351 }
James Feistdc6c55f2018-10-31 12:53:20 -0700352}