James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | /* |
| 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 Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 24 | #include <fstream> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 25 | #include <iostream> |
| 26 | #include <limits> |
| 27 | #include <sdbusplus/asio/connection.hpp> |
| 28 | #include <sdbusplus/asio/object_server.hpp> |
| 29 | #include <string> |
| 30 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 31 | static constexpr unsigned int pwmPollMs = 500; |
| 32 | static constexpr size_t warnAfterErrorCount = 10; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 33 | |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 34 | static constexpr double maxReading = 25000; |
| 35 | static constexpr double minReading = 0; |
| 36 | |
| 37 | TachSensor::TachSensor(const std::string &path, const std::string &objectType, |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 38 | sdbusplus::asio::object_server &objectServer, |
| 39 | std::shared_ptr<sdbusplus::asio::connection> &conn, |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 40 | std::unique_ptr<PresenceSensor> &&presence, |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 41 | const std::shared_ptr<RedundancySensor> &redundancy, |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 42 | boost::asio::io_service &io, const std::string &fanName, |
| 43 | std::vector<thresholds::Threshold> &&_thresholds, |
| 44 | const std::string &sensorConfiguration) : |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 45 | Sensor(boost::replace_all_copy(fanName, " ", "_"), path, |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 46 | std::move(_thresholds), sensorConfiguration, objectType, maxReading, |
| 47 | minReading), |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 48 | objServer(objectServer), dbusConnection(conn), |
| 49 | presence(std::move(presence)), redundancy(redundancy), |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 50 | inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), errCount(0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 51 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 52 | sensorInterface = objectServer.add_interface( |
| 53 | "/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 54 | "xyz.openbmc_project.Sensor.Value"); |
| 55 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 56 | if (thresholds::hasWarningInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 57 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 58 | thresholdInterfaceWarning = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 59 | "/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 60 | "xyz.openbmc_project.Sensor.Threshold.Warning"); |
| 61 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 62 | if (thresholds::hasCriticalInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 63 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 64 | thresholdInterfaceCritical = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 65 | "/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 66 | "xyz.openbmc_project.Sensor.Threshold.Critical"); |
| 67 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 68 | setInitialProperties(conn); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 69 | isPowerOn(dbusConnection); // first call initializes |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 70 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | TachSensor::~TachSensor() |
| 74 | { |
| 75 | // close the input dev to cancel async operations |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 76 | inputDev.close(); |
| 77 | waitTimer.cancel(); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 78 | objServer.remove_interface(thresholdInterfaceWarning); |
| 79 | objServer.remove_interface(thresholdInterfaceCritical); |
| 80 | objServer.remove_interface(sensorInterface); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 83 | void TachSensor::setupRead(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 84 | { |
| 85 | boost::asio::async_read_until( |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 86 | inputDev, readBuf, '\n', |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 87 | [&](const boost::system::error_code &ec, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 88 | std::size_t /*bytes_transfered*/) { handleResponse(ec); }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 91 | void TachSensor::handleResponse(const boost::system::error_code &err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 92 | { |
| 93 | if (err == boost::system::errc::bad_file_descriptor) |
| 94 | { |
| 95 | return; // we're being destroyed |
| 96 | } |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 97 | bool missing = false; |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 98 | size_t pollTime = pwmPollMs; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 99 | if (presence) |
| 100 | { |
| 101 | if (!presence->getValue()) |
| 102 | { |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 103 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 104 | missing = true; |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 105 | pollTime = sensorFailedPollTimeMs; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 108 | std::istream responseStream(&readBuf); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 109 | if (!missing) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 110 | { |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 111 | if (!err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 112 | { |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 113 | std::string response; |
| 114 | try |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 115 | { |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 116 | std::getline(responseStream, response); |
| 117 | float nvalue = std::stof(response); |
| 118 | responseStream.clear(); |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 119 | if (!isnan(overriddenValue)) |
| 120 | { |
| 121 | nvalue = overriddenValue; |
| 122 | } |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 123 | if (nvalue != value) |
| 124 | { |
| 125 | updateValue(nvalue); |
| 126 | } |
| 127 | errCount = 0; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 128 | } |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 129 | catch (const std::invalid_argument &) |
| 130 | { |
| 131 | errCount++; |
| 132 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 133 | } |
| 134 | else |
| 135 | { |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 136 | pollTime = sensorFailedPollTimeMs; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 137 | errCount++; |
| 138 | } |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 139 | if (errCount >= warnAfterErrorCount) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 140 | { |
| 141 | // only an error if power is on |
| 142 | if (isPowerOn(dbusConnection)) |
| 143 | { |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 144 | // only print once |
| 145 | if (errCount == warnAfterErrorCount) |
| 146 | { |
| 147 | std::cerr << "Failure to read sensor " << name << " at " |
| 148 | << path << " ec:" << err << "\n"; |
| 149 | } |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 150 | updateValue(0); |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | errCount = 0; // check power again in 10 cycles |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 155 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 156 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 159 | responseStream.clear(); |
| 160 | inputDev.close(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 161 | int fd = open(path.c_str(), O_RDONLY); |
| 162 | if (fd <= 0) |
| 163 | { |
| 164 | return; // we're no longer valid |
| 165 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 166 | inputDev.assign(fd); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 167 | waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime)); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 168 | waitTimer.async_wait([&](const boost::system::error_code &ec) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 169 | if (ec == boost::asio::error::operation_aborted) |
| 170 | { |
| 171 | return; // we're being canceled |
| 172 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 173 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 174 | }); |
| 175 | } |
| 176 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 177 | void TachSensor::checkThresholds(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 178 | { |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 179 | bool status = thresholds::checkThresholds(this); |
| 180 | if (redundancy) |
| 181 | { |
| 182 | redundancy->update("/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 183 | !status); |
| 184 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 185 | } |
| 186 | |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 187 | PresenceSensor::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 | |
| 215 | PresenceSensor::~PresenceSensor() |
| 216 | { |
| 217 | inputDev.close(); |
| 218 | close(fd); |
| 219 | } |
| 220 | |
| 221 | void 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 | |
| 242 | void 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 Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 249 | if (r != readSize) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 250 | { |
| 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 | |
| 264 | bool PresenceSensor::getValue(void) |
| 265 | { |
| 266 | return status; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | RedundancySensor::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 | } |
| 283 | RedundancySensor::~RedundancySensor() |
| 284 | { |
| 285 | objectServer.remove_interface(iface); |
| 286 | } |
| 287 | void 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 | } |