blob: 0f441cdcc3702f0eae04949a27be2f784d765115 [file] [log] [blame]
James Feist139cb572018-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 Feist7c391352018-10-26 14:09:45 -070024#include <fstream>
James Feist139cb572018-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 Yoof78ec412018-10-25 10:42:39 -070031static constexpr unsigned int pwmPollMs = 500;
32static constexpr size_t warnAfterErrorCount = 10;
James Feist139cb572018-09-10 15:26:18 -070033
34TachSensor::TachSensor(const std::string &path,
35 sdbusplus::asio::object_server &objectServer,
36 std::shared_ptr<sdbusplus::asio::connection> &conn,
James Feist7c391352018-10-26 14:09:45 -070037 std::unique_ptr<PresenceSensor> &&presence,
James Feist39132a62018-10-31 12:53:20 -070038 std::unique_ptr<RedundancySensor> &redundancy,
James Feist139cb572018-09-10 15:26:18 -070039 boost::asio::io_service &io, const std::string &fanName,
40 std::vector<thresholds::Threshold> &&_thresholds,
41 const std::string &sensorConfiguration) :
James Feist39132a62018-10-31 12:53:20 -070042 Sensor(boost::replace_all_copy(fanName, " ", "_"), path,
43 std::move(_thresholds)),
44 objServer(objectServer), dbusConnection(conn),
45 presence(std::move(presence)), redundancy(redundancy),
James Feistaf79dd32018-09-12 12:54:15 -070046 configuration(sensorConfiguration),
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070047 inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), errCount(0),
James Feist139cb572018-09-10 15:26:18 -070048 // todo, get these from config
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070049 maxValue(25000), minValue(0)
James Feist139cb572018-09-10 15:26:18 -070050{
James Feistaf79dd32018-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 Yoof78ec412018-10-25 10:42:39 -070055 if (thresholds::hasWarningInterface(thresholds))
James Feist139cb572018-09-10 15:26:18 -070056 {
James Feistaf79dd32018-09-12 12:54:15 -070057 thresholdInterfaceWarning = objectServer.add_interface(
James Feist139cb572018-09-10 15:26:18 -070058 "/xyz/openbmc_project/sensors/fan_tach/" + name,
59 "xyz.openbmc_project.Sensor.Threshold.Warning");
60 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070061 if (thresholds::hasCriticalInterface(thresholds))
James Feist139cb572018-09-10 15:26:18 -070062 {
James Feistaf79dd32018-09-12 12:54:15 -070063 thresholdInterfaceCritical = objectServer.add_interface(
James Feist139cb572018-09-10 15:26:18 -070064 "/xyz/openbmc_project/sensors/fan_tach/" + name,
65 "xyz.openbmc_project.Sensor.Threshold.Critical");
66 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070067 setInitialProperties(conn);
James Feist139cb572018-09-10 15:26:18 -070068 isPowerOn(dbusConnection); // first call initializes
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070069 setupRead();
James Feist139cb572018-09-10 15:26:18 -070070}
71
72TachSensor::~TachSensor()
73{
74 // close the input dev to cancel async operations
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070075 inputDev.close();
76 waitTimer.cancel();
James Feistaf79dd32018-09-12 12:54:15 -070077 objServer.remove_interface(thresholdInterfaceWarning);
78 objServer.remove_interface(thresholdInterfaceCritical);
79 objServer.remove_interface(sensorInterface);
James Feist139cb572018-09-10 15:26:18 -070080}
81
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070082void TachSensor::setupRead(void)
James Feist139cb572018-09-10 15:26:18 -070083{
84 boost::asio::async_read_until(
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070085 inputDev, readBuf, '\n',
James Feist139cb572018-09-10 15:26:18 -070086 [&](const boost::system::error_code &ec,
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070087 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist139cb572018-09-10 15:26:18 -070088}
89
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070090void TachSensor::handleResponse(const boost::system::error_code &err)
James Feist139cb572018-09-10 15:26:18 -070091{
92 if (err == boost::system::errc::bad_file_descriptor)
93 {
94 return; // we're being destroyed
95 }
James Feist7c391352018-10-26 14:09:45 -070096 bool missing = false;
James Feist3452f0e2018-10-31 10:08:47 -070097 size_t pollTime = pwmPollMs;
James Feist7c391352018-10-26 14:09:45 -070098 if (presence)
99 {
100 if (!presence->getValue())
101 {
James Feist3452f0e2018-10-31 10:08:47 -0700102 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7c391352018-10-26 14:09:45 -0700103 missing = true;
James Feist3452f0e2018-10-31 10:08:47 -0700104 pollTime = sensorFailedPollTimeMs;
James Feist7c391352018-10-26 14:09:45 -0700105 }
106 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700107 std::istream responseStream(&readBuf);
James Feist7c391352018-10-26 14:09:45 -0700108 if (!missing)
James Feist139cb572018-09-10 15:26:18 -0700109 {
James Feist7c391352018-10-26 14:09:45 -0700110 if (!err)
James Feist139cb572018-09-10 15:26:18 -0700111 {
James Feist7c391352018-10-26 14:09:45 -0700112 std::string response;
113 try
James Feist139cb572018-09-10 15:26:18 -0700114 {
James Feist7c391352018-10-26 14:09:45 -0700115 std::getline(responseStream, response);
116 float nvalue = std::stof(response);
117 responseStream.clear();
118 if (nvalue != value)
119 {
120 updateValue(nvalue);
121 }
122 errCount = 0;
James Feist139cb572018-09-10 15:26:18 -0700123 }
James Feist7c391352018-10-26 14:09:45 -0700124 catch (const std::invalid_argument &)
125 {
126 errCount++;
127 }
James Feist139cb572018-09-10 15:26:18 -0700128 }
129 else
130 {
James Feist3452f0e2018-10-31 10:08:47 -0700131 pollTime = sensorFailedPollTimeMs;
James Feist7c391352018-10-26 14:09:45 -0700132 errCount++;
133 }
James Feist39132a62018-10-31 12:53:20 -0700134 if (errCount >= warnAfterErrorCount)
James Feist7c391352018-10-26 14:09:45 -0700135 {
136 // only an error if power is on
137 if (isPowerOn(dbusConnection))
138 {
James Feist39132a62018-10-31 12:53:20 -0700139 // only print once
140 if (errCount == warnAfterErrorCount)
141 {
142 std::cerr << "Failure to read sensor " << name << " at "
143 << path << " ec:" << err << "\n";
144 }
James Feist7c391352018-10-26 14:09:45 -0700145 updateValue(0);
146 }
147 else
148 {
149 errCount = 0; // check power again in 10 cycles
James Feist3452f0e2018-10-31 10:08:47 -0700150 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7c391352018-10-26 14:09:45 -0700151 }
James Feist139cb572018-09-10 15:26:18 -0700152 }
153 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700154 responseStream.clear();
155 inputDev.close();
James Feist139cb572018-09-10 15:26:18 -0700156 int fd = open(path.c_str(), O_RDONLY);
157 if (fd <= 0)
158 {
159 return; // we're no longer valid
160 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700161 inputDev.assign(fd);
James Feist7c391352018-10-26 14:09:45 -0700162 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700163 waitTimer.async_wait([&](const boost::system::error_code &ec) {
James Feist139cb572018-09-10 15:26:18 -0700164 if (ec == boost::asio::error::operation_aborted)
165 {
166 return; // we're being canceled
167 }
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700168 setupRead();
James Feist139cb572018-09-10 15:26:18 -0700169 });
170}
171
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700172void TachSensor::checkThresholds(void)
James Feist139cb572018-09-10 15:26:18 -0700173{
James Feist39132a62018-10-31 12:53:20 -0700174 bool status = thresholds::checkThresholds(this);
175 if (redundancy)
176 {
177 redundancy->update("/xyz/openbmc_project/sensors/fan_tach/" + name,
178 !status);
179 }
James Feist139cb572018-09-10 15:26:18 -0700180}
181
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700182void TachSensor::updateValue(const double &newValue)
James Feist139cb572018-09-10 15:26:18 -0700183{
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700184 sensorInterface->set_property("Value", newValue);
185 value = newValue;
186 checkThresholds();
James Feist139cb572018-09-10 15:26:18 -0700187}
188
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700189void TachSensor::setInitialProperties(
James Feist139cb572018-09-10 15:26:18 -0700190 std::shared_ptr<sdbusplus::asio::connection> &conn)
191{
192 // todo, get max and min from configuration
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700193 sensorInterface->register_property("MaxValue", maxValue);
194 sensorInterface->register_property("MinValue", minValue);
James Feistaf79dd32018-09-12 12:54:15 -0700195 sensorInterface->register_property("Value", value);
James Feist139cb572018-09-10 15:26:18 -0700196
197 for (auto &threshold : thresholds)
198 {
199 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
200 std::string level;
201 std::string alarm;
202 if (threshold.level == thresholds::Level::CRITICAL)
203 {
James Feistaf79dd32018-09-12 12:54:15 -0700204 iface = thresholdInterfaceCritical;
James Feist139cb572018-09-10 15:26:18 -0700205 if (threshold.direction == thresholds::Direction::HIGH)
206 {
207 level = "CriticalHigh";
208 alarm = "CriticalAlarmHigh";
209 }
210 else
211 {
212 level = "CriticalLow";
213 alarm = "CriticalAlarmLow";
214 }
215 }
216 else if (threshold.level == thresholds::Level::WARNING)
217 {
James Feistaf79dd32018-09-12 12:54:15 -0700218 iface = thresholdInterfaceWarning;
James Feist139cb572018-09-10 15:26:18 -0700219 if (threshold.direction == thresholds::Direction::HIGH)
220 {
221 level = "WarningHigh";
222 alarm = "WarningAlarmHigh";
223 }
224 else
225 {
226 level = "WarningLow";
227 alarm = "WarningAlarmLow";
228 }
229 }
230 else
231 {
232 std::cerr << "Unknown threshold level" << threshold.level << "\n";
233 continue;
234 }
235 if (!iface)
236 {
237 std::cout << "trying to set uninitialized interface\n";
238 continue;
239 }
240 iface->register_property(
241 level, threshold.value,
242 [&](const double &request, double &oldValue) {
243 oldValue = request; // todo, just let the config do this?
244 threshold.value = request;
245 thresholds::persistThreshold(
246 configuration,
247 "xyz.openbmc_project.Configuration.AspeedFan", threshold,
248 conn);
249 return 1;
250 });
251 iface->register_property(alarm, false);
252 }
James Feistaf79dd32018-09-12 12:54:15 -0700253 if (!sensorInterface->initialize())
James Feist139cb572018-09-10 15:26:18 -0700254 {
255 std::cerr << "error initializing value interface\n";
256 }
James Feistaf79dd32018-09-12 12:54:15 -0700257 if (thresholdInterfaceWarning && !thresholdInterfaceWarning->initialize())
James Feist139cb572018-09-10 15:26:18 -0700258 {
259 std::cerr << "error initializing warning threshold interface\n";
260 }
261
James Feistaf79dd32018-09-12 12:54:15 -0700262 if (thresholdInterfaceCritical && !thresholdInterfaceCritical->initialize())
James Feist139cb572018-09-10 15:26:18 -0700263 {
264 std::cerr << "error initializing critical threshold interface\n";
265 }
266}
James Feist7c391352018-10-26 14:09:45 -0700267
268PresenceSensor::PresenceSensor(const size_t index, bool inverted,
269 boost::asio::io_service &io) :
270 inverted(inverted),
271 inputDev(io)
272{
273 // todo: use gpiodaemon
274 std::string device = gpioPath + std::string("gpio") + std::to_string(index);
275 fd = open((device + "/value").c_str(), O_RDONLY);
276 if (fd < 0)
277 {
278 std::cerr << "Error opening gpio " << index << "\n";
279 return;
280 }
281
282 std::ofstream deviceFile(device + "/edge");
283 if (!deviceFile.good())
284 {
285 std::cerr << "Error setting edge " << device << "\n";
286 return;
287 }
288 deviceFile << "both";
289 deviceFile.close();
290
291 inputDev.assign(boost::asio::ip::tcp::v4(), fd);
292 monitorPresence();
293 read();
294}
295
296PresenceSensor::~PresenceSensor()
297{
298 inputDev.close();
299 close(fd);
300}
301
302void PresenceSensor::monitorPresence(void)
303{
304 inputDev.async_wait(boost::asio::ip::tcp::socket::wait_error,
305 [this](const boost::system::error_code &ec) {
306 if (ec == boost::system::errc::bad_file_descriptor)
307 {
308 return; // we're being destroyed
309 }
310 else if (ec)
311 {
312 std::cerr
313 << "Error on presence sensor socket\n";
314 }
315 else
316 {
317 read();
318 }
319 monitorPresence();
320 });
321}
322
323void PresenceSensor::read(void)
324{
325 constexpr size_t readSize = sizeof("0");
326 std::string readBuf;
327 readBuf.resize(readSize);
328 lseek(fd, 0, SEEK_SET);
329 size_t r = ::read(fd, readBuf.data(), readSize);
330 if (r != 1)
331 {
332 std::cerr << "Error reading gpio\n";
333 }
334 else
335 {
336 bool value = std::stoi(readBuf);
337 if (inverted)
338 {
339 value = !value;
340 }
341 status = value;
342 }
343}
344
345bool PresenceSensor::getValue(void)
346{
347 return status;
James Feist39132a62018-10-31 12:53:20 -0700348}
349
350RedundancySensor::RedundancySensor(
351 size_t count, const std::vector<std::string> &children,
352 sdbusplus::asio::object_server &objectServer) :
353 count(count),
354 iface(objectServer.add_interface(
355 "/xyz/openbmc_project/control/FanRedundancy/Tach",
356 "xyz.openbmc_project.control.FanRedundancy")),
357 objectServer(objectServer)
358{
359 iface->register_property("Collection", children);
360 iface->register_property("Status", std::string("Full"));
361 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
362 iface->initialize();
363}
364RedundancySensor::~RedundancySensor()
365{
366 objectServer.remove_interface(iface);
367}
368void RedundancySensor::update(const std::string &name, bool failed)
369{
370 statuses[name] = failed;
371 size_t failedCount = 0;
372
373 std::string state = "Full";
374 for (const auto &status : statuses)
375 {
376 if (status.second)
377 {
378 failedCount++;
379 }
380 if (failedCount > count)
381 {
382 state = "Failed";
383 break;
384 }
385 else if (failedCount)
386 {
387 state = "Degraded";
388 }
389 }
390 iface->set_property("Status", state);
391}