blob: 63ced19c720349d766591dd7b0e9595418e8d1e8 [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 Feistce3fca42018-11-21 12:58:24 -080034static constexpr double maxReading = 25000;
35static constexpr double minReading = 0;
36
37TachSensor::TachSensor(const std::string &path, const std::string &objectType,
James Feist6714a252018-09-10 15:26:18 -070038 sdbusplus::asio::object_server &objectServer,
39 std::shared_ptr<sdbusplus::asio::connection> &conn,
James Feist7bc2bab2018-10-26 14:09:45 -070040 std::unique_ptr<PresenceSensor> &&presence,
James Feist95b079b2018-11-21 09:28:00 -080041 const std::shared_ptr<RedundancySensor> &redundancy,
James Feist6714a252018-09-10 15:26:18 -070042 boost::asio::io_service &io, const std::string &fanName,
43 std::vector<thresholds::Threshold> &&_thresholds,
44 const std::string &sensorConfiguration) :
James Feistdc6c55f2018-10-31 12:53:20 -070045 Sensor(boost::replace_all_copy(fanName, " ", "_"), path,
James Feistce3fca42018-11-21 12:58:24 -080046 std::move(_thresholds), sensorConfiguration, objectType, maxReading,
47 minReading),
James Feistdc6c55f2018-10-31 12:53:20 -070048 objServer(objectServer), dbusConnection(conn),
49 presence(std::move(presence)), redundancy(redundancy),
James Feistce3fca42018-11-21 12:58:24 -080050 inputDev(io, open(path.c_str(), O_RDONLY)), 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 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070068 setInitialProperties(conn);
James Feist6714a252018-09-10 15:26:18 -070069 isPowerOn(dbusConnection); // first call initializes
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070070 setupRead();
James Feist6714a252018-09-10 15:26:18 -070071}
72
73TachSensor::~TachSensor()
74{
75 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070076 inputDev.close();
77 waitTimer.cancel();
James Feist251c7822018-09-12 12:54:15 -070078 objServer.remove_interface(thresholdInterfaceWarning);
79 objServer.remove_interface(thresholdInterfaceCritical);
80 objServer.remove_interface(sensorInterface);
James Feist6714a252018-09-10 15:26:18 -070081}
82
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070083void TachSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070084{
85 boost::asio::async_read_until(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070086 inputDev, readBuf, '\n',
James Feist6714a252018-09-10 15:26:18 -070087 [&](const boost::system::error_code &ec,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070088 std::size_t /*bytes_transfered*/) { handleResponse(ec); });
James Feist6714a252018-09-10 15:26:18 -070089}
90
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070091void TachSensor::handleResponse(const boost::system::error_code &err)
James Feist6714a252018-09-10 15:26:18 -070092{
93 if (err == boost::system::errc::bad_file_descriptor)
94 {
95 return; // we're being destroyed
96 }
James Feist7bc2bab2018-10-26 14:09:45 -070097 bool missing = false;
James Feist1169eb42018-10-31 10:08:47 -070098 size_t pollTime = pwmPollMs;
James Feist7bc2bab2018-10-26 14:09:45 -070099 if (presence)
100 {
101 if (!presence->getValue())
102 {
James Feist1169eb42018-10-31 10:08:47 -0700103 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700104 missing = true;
James Feist1169eb42018-10-31 10:08:47 -0700105 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700106 }
107 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700108 std::istream responseStream(&readBuf);
James Feist7bc2bab2018-10-26 14:09:45 -0700109 if (!missing)
James Feist6714a252018-09-10 15:26:18 -0700110 {
James Feist7bc2bab2018-10-26 14:09:45 -0700111 if (!err)
James Feist6714a252018-09-10 15:26:18 -0700112 {
James Feist7bc2bab2018-10-26 14:09:45 -0700113 std::string response;
114 try
James Feist6714a252018-09-10 15:26:18 -0700115 {
James Feist7bc2bab2018-10-26 14:09:45 -0700116 std::getline(responseStream, response);
117 float nvalue = std::stof(response);
118 responseStream.clear();
Richard Marian Thomaiyar87219222018-11-06 20:25:38 +0530119 if (!isnan(overriddenValue))
120 {
121 nvalue = overriddenValue;
122 }
James Feist7bc2bab2018-10-26 14:09:45 -0700123 if (nvalue != value)
124 {
125 updateValue(nvalue);
126 }
127 errCount = 0;
James Feist6714a252018-09-10 15:26:18 -0700128 }
James Feist7bc2bab2018-10-26 14:09:45 -0700129 catch (const std::invalid_argument &)
130 {
131 errCount++;
132 }
James Feist6714a252018-09-10 15:26:18 -0700133 }
134 else
135 {
James Feist1169eb42018-10-31 10:08:47 -0700136 pollTime = sensorFailedPollTimeMs;
James Feist7bc2bab2018-10-26 14:09:45 -0700137 errCount++;
138 }
James Feistdc6c55f2018-10-31 12:53:20 -0700139 if (errCount >= warnAfterErrorCount)
James Feist7bc2bab2018-10-26 14:09:45 -0700140 {
141 // only an error if power is on
142 if (isPowerOn(dbusConnection))
143 {
James Feistdc6c55f2018-10-31 12:53:20 -0700144 // only print once
145 if (errCount == warnAfterErrorCount)
146 {
147 std::cerr << "Failure to read sensor " << name << " at "
148 << path << " ec:" << err << "\n";
149 }
James Feist7bc2bab2018-10-26 14:09:45 -0700150 updateValue(0);
151 }
152 else
153 {
154 errCount = 0; // check power again in 10 cycles
James Feist1169eb42018-10-31 10:08:47 -0700155 updateValue(std::numeric_limits<double>::quiet_NaN());
James Feist7bc2bab2018-10-26 14:09:45 -0700156 }
James Feist6714a252018-09-10 15:26:18 -0700157 }
158 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700159 responseStream.clear();
160 inputDev.close();
James Feist6714a252018-09-10 15:26:18 -0700161 int fd = open(path.c_str(), O_RDONLY);
162 if (fd <= 0)
163 {
164 return; // we're no longer valid
165 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700166 inputDev.assign(fd);
James Feist7bc2bab2018-10-26 14:09:45 -0700167 waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime));
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700168 waitTimer.async_wait([&](const boost::system::error_code &ec) {
James Feist6714a252018-09-10 15:26:18 -0700169 if (ec == boost::asio::error::operation_aborted)
170 {
171 return; // we're being canceled
172 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700173 setupRead();
James Feist6714a252018-09-10 15:26:18 -0700174 });
175}
176
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700177void TachSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700178{
James Feistdc6c55f2018-10-31 12:53:20 -0700179 bool status = thresholds::checkThresholds(this);
180 if (redundancy)
181 {
182 redundancy->update("/xyz/openbmc_project/sensors/fan_tach/" + name,
183 !status);
184 }
James Feist6714a252018-09-10 15:26:18 -0700185}
186
James Feist7bc2bab2018-10-26 14:09:45 -0700187PresenceSensor::PresenceSensor(const size_t index, bool inverted,
188 boost::asio::io_service &io) :
189 inverted(inverted),
190 inputDev(io)
191{
192 // todo: use gpiodaemon
193 std::string device = gpioPath + std::string("gpio") + std::to_string(index);
194 fd = open((device + "/value").c_str(), O_RDONLY);
195 if (fd < 0)
196 {
197 std::cerr << "Error opening gpio " << index << "\n";
198 return;
199 }
200
201 std::ofstream deviceFile(device + "/edge");
202 if (!deviceFile.good())
203 {
204 std::cerr << "Error setting edge " << device << "\n";
205 return;
206 }
207 deviceFile << "both";
208 deviceFile.close();
209
210 inputDev.assign(boost::asio::ip::tcp::v4(), fd);
211 monitorPresence();
212 read();
213}
214
215PresenceSensor::~PresenceSensor()
216{
217 inputDev.close();
218 close(fd);
219}
220
221void PresenceSensor::monitorPresence(void)
222{
223 inputDev.async_wait(boost::asio::ip::tcp::socket::wait_error,
224 [this](const boost::system::error_code &ec) {
225 if (ec == boost::system::errc::bad_file_descriptor)
226 {
227 return; // we're being destroyed
228 }
229 else if (ec)
230 {
231 std::cerr
232 << "Error on presence sensor socket\n";
233 }
234 else
235 {
236 read();
237 }
238 monitorPresence();
239 });
240}
241
242void PresenceSensor::read(void)
243{
244 constexpr size_t readSize = sizeof("0");
245 std::string readBuf;
246 readBuf.resize(readSize);
247 lseek(fd, 0, SEEK_SET);
248 size_t r = ::read(fd, readBuf.data(), readSize);
James Feist95b079b2018-11-21 09:28:00 -0800249 if (r != readSize)
James Feist7bc2bab2018-10-26 14:09:45 -0700250 {
251 std::cerr << "Error reading gpio\n";
252 }
253 else
254 {
255 bool value = std::stoi(readBuf);
256 if (inverted)
257 {
258 value = !value;
259 }
260 status = value;
261 }
262}
263
264bool PresenceSensor::getValue(void)
265{
266 return status;
James Feistdc6c55f2018-10-31 12:53:20 -0700267}
268
269RedundancySensor::RedundancySensor(
270 size_t count, const std::vector<std::string> &children,
271 sdbusplus::asio::object_server &objectServer) :
272 count(count),
273 iface(objectServer.add_interface(
274 "/xyz/openbmc_project/control/FanRedundancy/Tach",
275 "xyz.openbmc_project.control.FanRedundancy")),
276 objectServer(objectServer)
277{
278 iface->register_property("Collection", children);
279 iface->register_property("Status", std::string("Full"));
280 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
281 iface->initialize();
282}
283RedundancySensor::~RedundancySensor()
284{
285 objectServer.remove_interface(iface);
286}
287void RedundancySensor::update(const std::string &name, bool failed)
288{
289 statuses[name] = failed;
290 size_t failedCount = 0;
291
292 std::string state = "Full";
293 for (const auto &status : statuses)
294 {
295 if (status.second)
296 {
297 failedCount++;
298 }
299 if (failedCount > count)
300 {
301 state = "Failed";
302 break;
303 }
304 else if (failedCount)
305 {
306 state = "Degraded";
307 }
308 }
309 iface->set_property("Status", state);
310}