blob: 295c7e68a6610cc0f6cc159dcd4510e9dc007a83 [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,
James Feist60c0ec72019-03-18 15:08:43 -070037 std::unique_ptr<PresenceSensor>&& presenceSensor,
James Feist7b18b1e2019-05-14 13:42:09 -070038 std::optional<RedundancySensor>* redundancy,
James Feistd8705872019-02-08 13:26:09 -080039 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 Feist60c0ec72019-03-18 15:08:43 -070046 objServer(objectServer), presence(std::move(presenceSensor)),
James Feist71d31b22019-01-02 16:57:54 -080047 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");
James Feist60c0ec72019-03-18 15:08:43 -070069
70 if (presence)
71 {
72 itemIface =
73 objectServer.add_interface("/xyz/openbmc_project/Inventory/" + name,
74 "xyz.openbmc_project.Inventory.Item");
75 itemIface->register_property("PrettyName",
76 std::string()); // unused property
77 itemIface->register_property("Present", true);
78 itemIface->initialize();
79 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070080 setInitialProperties(conn);
James Feist6ef20402019-01-07 16:45:08 -080081 setupPowerMatch(conn);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070082 setupRead();
James Feist6714a252018-09-10 15:26:18 -070083}
84
85TachSensor::~TachSensor()
86{
87 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070088 inputDev.close();
89 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070090 objServer.remove_interface(thresholdInterfaceWarning);
91 objServer.remove_interface(thresholdInterfaceCritical);
92 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080093 objServer.remove_interface(association);
James Feist60c0ec72019-03-18 15:08:43 -070094 objServer.remove_interface(itemIface);
James Feist6714a252018-09-10 15:26:18 -070095}
96
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070097void TachSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070098{
99 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700100 inputDev, readBuf, '\n',
James Feistd8705872019-02-08 13:26:09 -0800101 [&](const boost::system::error_code& ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700102 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -0700103}
104
James Feistd8705872019-02-08 13:26:09 -0800105void TachSensor::handleResponse(const boost::system::error_code& err)
James Feist6714a252018-09-10 15:26:18 -0700106{
107 if (err == boost::system::errc::bad_file_descriptor)
108 {
109 return; // we're being destroyed
110 }
James Feist7bc2bab2018-10-26 14:09:45 -0700111 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -0700112 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700113 if (presence)
114 {
115 if (!presence->getValue())
116 {
James Feist1169eb42018-10-31 10:08:47 -0700117 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700118 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700119 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700120 }
James Feist60c0ec72019-03-18 15:08:43 -0700121 itemIface->set_property("Present", !missing);
James Feist7bc2bab2018-10-26 14:09:45 -0700122 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700123 std::istream responseStream(&readBuf);
James Feist7bc2bab2018-10-26 14:09:45 -0700124 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700125 {
James Feist7bc2bab2018-10-26 14:09:45 -0700126 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700127 {
James Feist7bc2bab2018-10-26 14:09:45 -0700128 std::string response;
129 try
James Feist6714a252018-09-10 15:26:18 -0700130 {
James Feist7bc2bab2018-10-26 14:09:45 -0700131 std::getline(responseStream, response);
132 float nvalue = std::stof(response);
133 responseStream.clear();
134 if (nvalue != value)
135 {
136 updateValue(nvalue);
137 }
138 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700139 }
James Feistd8705872019-02-08 13:26:09 -0800140 catch (const std::invalid_argument&)
James Feist7bc2bab2018-10-26 14:09:45 -0700141 {
142 errCount++;
143 }
James Feist6714a252018-09-10 15:26:18 -0700144 }
145 else
146 {
James Feist71d31b22019-01-02 16:57:54 -0800147 if (!isPowerOn())
James Feist7bc2bab2018-10-26 14:09:45 -0700148 {
James Feist71d31b22019-01-02 16:57:54 -0800149 errCount = 0;
150 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700151 }
152 else
153 {
James Feist71d31b22019-01-02 16:57:54 -0800154 pollTime = sensorFailedPollTimeMs;
155 errCount++;
James Feist7bc2bab2018-10-26 14:09:45 -0700156 }
James Feist6714a252018-09-10 15:26:18 -0700157 }
James Feist71d31b22019-01-02 16:57:54 -0800158 if (errCount >= warnAfterErrorCount)
159 {
160 // only print once
161 if (errCount == warnAfterErrorCount)
162 {
163 std::cerr << "Failure to read sensor " << name << " at " << path
164 << " ec:" << err << "\n";
165 }
166 updateValue(0);
167 }
James Feist6714a252018-09-10 15:26:18 -0700168 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700169 responseStream.clear();
170 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700171 int fd = open(path.c_str(), O_RDONLY);
172 if (fd <= 0)
173 {
174 return; // we're no longer valid
175 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700176 inputDev.assign(fd);
James Feist7bc2bab2018-10-26 14:09:45 -0700177 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
James Feistd8705872019-02-08 13:26:09 -0800178 waitTimer.async_wait([&](const boost::system::error_code& ec) {
James Feist6714a252018-09-10 15:26:18 -0700179 if (ec == boost::asio::error::operation_aborted)
180 {
181 return; // we're being canceled
182 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700183 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700184 });
185}
186
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700187void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700188{
James Feistdc6c55f2018-10-31 12:53:20 -0700189 bool status = thresholds::checkThresholds(this);
James Feist7b18b1e2019-05-14 13:42:09 -0700190 if (redundancy && *redundancy)
James Feistdc6c55f2018-10-31 12:53:20 -0700191 {
James Feist7b18b1e2019-05-14 13:42:09 -0700192 (*redundancy)
193 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
James Feistdc6c55f2018-10-31 12:53:20 -0700194 }
James Feist6714a252018-09-10 15:26:18 -0700195}
196
James Feist7bc2bab2018-10-26 14:09:45 -0700197PresenceSensor::PresenceSensor(const size_t index, bool inverted,
James Feist7b18b1e2019-05-14 13:42:09 -0700198 boost::asio::io_service& io,
199 const std::string& name) :
James Feist7bc2bab2018-10-26 14:09:45 -0700200 inverted(inverted),
James Feist7b18b1e2019-05-14 13:42:09 -0700201 inputDev(io), name(name)
James Feist7bc2bab2018-10-26 14:09:45 -0700202{
203 // todo: use gpiodaemon
204 std::string device = gpioPath + std::string("gpio") + std::to_string(index);
205 fd = open((device + "/value").c_str(), O_RDONLY);
206 if (fd < 0)
207 {
208 std::cerr << "Error opening gpio " << index << "\n";
209 return;
210 }
211
212 std::ofstream deviceFile(device + "/edge");
213 if (!deviceFile.good())
214 {
215 std::cerr << "Error setting edge " << device << "\n";
216 return;
217 }
218 deviceFile << "both";
219 deviceFile.close();
220
221 inputDev.assign(boost::asio::ip::tcp::v4(), fd);
222 monitorPresence();
223 read();
224}
225
226PresenceSensor::~PresenceSensor()
227{
228 inputDev.close();
229 close(fd);
230}
231
232void PresenceSensor::monitorPresence(void)
233{
234 inputDev.async_wait(boost::asio::ip::tcp::socket::wait_error,
James Feistd8705872019-02-08 13:26:09 -0800235 [this](const boost::system::error_code& ec) {
James Feist7bc2bab2018-10-26 14:09:45 -0700236 if (ec == boost::system::errc::bad_file_descriptor)
237 {
238 return; // we're being destroyed
239 }
240 else if (ec)
241 {
242 std::cerr
243 << "Error on presence sensor socket\n";
244 }
245 else
246 {
247 read();
248 }
249 monitorPresence();
250 });
251}
252
253void PresenceSensor::read(void)
254{
255 constexpr size_t readSize = sizeof("0");
256 std::string readBuf;
257 readBuf.resize(readSize);
258 lseek(fd, 0, SEEK_SET);
259 size_t r = ::read(fd, readBuf.data(), readSize);
James Feist95b079b2018-11-21 09:28:00 -0800260 if (r != readSize)
James Feist7bc2bab2018-10-26 14:09:45 -0700261 {
262 std::cerr << "Error reading gpio\n";
263 }
264 else
265 {
266 bool value = std::stoi(readBuf);
267 if (inverted)
268 {
269 value = !value;
270 }
James Feist7b18b1e2019-05-14 13:42:09 -0700271 if (value != status)
272 {
273 status = value;
274 if (status)
275 {
276 logFanInserted(name);
277 }
278 else
279 {
280 logFanRemoved(name);
281 }
282 }
James Feist7bc2bab2018-10-26 14:09:45 -0700283 }
284}
285
286bool PresenceSensor::getValue(void)
287{
288 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700289}
290
James Feist9bb67462019-03-15 15:09:50 -0700291RedundancySensor::RedundancySensor(size_t count,
292 const std::vector<std::string>& children,
293 sdbusplus::asio::object_server& objectServer,
294 const std::string& sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -0700295 count(count),
296 iface(objectServer.add_interface(
297 "/xyz/openbmc_project/control/FanRedundancy/Tach",
James Feist9bb67462019-03-15 15:09:50 -0700298 "xyz.openbmc_project.Control.FanRedundancy")),
299 association(objectServer.add_interface(
300 "/xyz/openbmc_project/control/FanRedundancy/Tach",
301 "org.openbmc.Associations")),
James Feistdc6c55f2018-10-31 12:53:20 -0700302 objectServer(objectServer)
303{
James Feist9bb67462019-03-15 15:09:50 -0700304 createAssociation(association, sensorConfiguration);
James Feistdc6c55f2018-10-31 12:53:20 -0700305 iface->register_property("Collection", children);
306 iface->register_property("Status", std::string("Full"));
307 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
308 iface->initialize();
309}
310RedundancySensor::~RedundancySensor()
311{
James Feist9bb67462019-03-15 15:09:50 -0700312 objectServer.remove_interface(association);
James Feistdc6c55f2018-10-31 12:53:20 -0700313 objectServer.remove_interface(iface);
314}
James Feistd8705872019-02-08 13:26:09 -0800315void RedundancySensor::update(const std::string& name, bool failed)
James Feistdc6c55f2018-10-31 12:53:20 -0700316{
317 statuses[name] = failed;
318 size_t failedCount = 0;
319
James Feist7b18b1e2019-05-14 13:42:09 -0700320 std::string newState = redundancy::full;
James Feistd8705872019-02-08 13:26:09 -0800321 for (const auto& status : statuses)
James Feistdc6c55f2018-10-31 12:53:20 -0700322 {
323 if (status.second)
324 {
325 failedCount++;
326 }
327 if (failedCount > count)
328 {
James Feist7b18b1e2019-05-14 13:42:09 -0700329 newState = redundancy::failed;
James Feistdc6c55f2018-10-31 12:53:20 -0700330 break;
331 }
332 else if (failedCount)
333 {
James Feist7b18b1e2019-05-14 13:42:09 -0700334 newState = redundancy::degraded;
James Feistdc6c55f2018-10-31 12:53:20 -0700335 }
336 }
James Feist7b18b1e2019-05-14 13:42:09 -0700337 if (state != newState)
338 {
339 if (state == redundancy::full)
340 {
341 logFanRedundancyLost();
342 }
343 else if (newState == redundancy::full)
344 {
345 logFanRedundancyRestored();
346 }
347 state = newState;
348 iface->set_property("Status", state);
349 }
James Feistdc6c55f2018-10-31 12:53:20 -0700350}