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 | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 34 | TachSensor::TachSensor(const std::string& path, const std::string& objectType, |
| 35 | sdbusplus::asio::object_server& objectServer, |
| 36 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 37 | std::unique_ptr<PresenceSensor>&& presenceSensor, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 38 | const std::shared_ptr<RedundancySensor>& redundancy, |
| 39 | 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 Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 43 | Sensor(boost::replace_all_copy(fanName, " ", "_"), path, |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 44 | std::move(_thresholds), sensorConfiguration, objectType, |
| 45 | limits.second, limits.first), |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 46 | objServer(objectServer), presence(std::move(presenceSensor)), |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 47 | redundancy(redundancy), inputDev(io, open(path.c_str(), O_RDONLY)), |
| 48 | waitTimer(io), errCount(0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 49 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 50 | sensorInterface = objectServer.add_interface( |
| 51 | "/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 52 | "xyz.openbmc_project.Sensor.Value"); |
| 53 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 54 | if (thresholds::hasWarningInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 55 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 56 | thresholdInterfaceWarning = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 57 | "/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 58 | "xyz.openbmc_project.Sensor.Threshold.Warning"); |
| 59 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 60 | if (thresholds::hasCriticalInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 61 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 62 | thresholdInterfaceCritical = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 63 | "/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 64 | "xyz.openbmc_project.Sensor.Threshold.Critical"); |
| 65 | } |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 66 | association = objectServer.add_interface( |
| 67 | "/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 68 | "org.openbmc.Associations"); |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 69 | |
| 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 Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 80 | setInitialProperties(conn); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 81 | setupPowerMatch(conn); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 82 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | TachSensor::~TachSensor() |
| 86 | { |
| 87 | // close the input dev to cancel async operations |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 88 | inputDev.close(); |
| 89 | waitTimer.cancel(); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 90 | objServer.remove_interface(thresholdInterfaceWarning); |
| 91 | objServer.remove_interface(thresholdInterfaceCritical); |
| 92 | objServer.remove_interface(sensorInterface); |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 93 | objServer.remove_interface(association); |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 94 | objServer.remove_interface(itemIface); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 97 | void TachSensor::setupRead(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 98 | { |
| 99 | boost::asio::async_read_until( |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 100 | inputDev, readBuf, '\n', |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 101 | [&](const boost::system::error_code& ec, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 102 | std::size_t /*bytes_transfered*/) { handleResponse(ec); }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 103 | } |
| 104 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 105 | void TachSensor::handleResponse(const boost::system::error_code& err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 106 | { |
| 107 | if (err == boost::system::errc::bad_file_descriptor) |
| 108 | { |
| 109 | return; // we're being destroyed |
| 110 | } |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 111 | bool missing = false; |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 112 | size_t pollTime = pwmPollMs; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 113 | if (presence) |
| 114 | { |
| 115 | if (!presence->getValue()) |
| 116 | { |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 117 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 118 | missing = true; |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 119 | pollTime = sensorFailedPollTimeMs; |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 120 | } |
James Feist | 60c0ec7 | 2019-03-18 15:08:43 -0700 | [diff] [blame] | 121 | itemIface->set_property("Present", !missing); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 122 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 123 | std::istream responseStream(&readBuf); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 124 | if (!missing) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 125 | { |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 126 | if (!err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 127 | { |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 128 | std::string response; |
| 129 | try |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 130 | { |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 131 | 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 Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 139 | } |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 140 | catch (const std::invalid_argument&) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 141 | { |
| 142 | errCount++; |
| 143 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 144 | } |
| 145 | else |
| 146 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 147 | if (!isPowerOn()) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 148 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 149 | errCount = 0; |
| 150 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 151 | } |
| 152 | else |
| 153 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 154 | pollTime = sensorFailedPollTimeMs; |
| 155 | errCount++; |
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 | } |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 158 | 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 Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 168 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 169 | responseStream.clear(); |
| 170 | inputDev.close(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 171 | int fd = open(path.c_str(), O_RDONLY); |
| 172 | if (fd <= 0) |
| 173 | { |
| 174 | return; // we're no longer valid |
| 175 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 176 | inputDev.assign(fd); |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 177 | waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime)); |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 178 | waitTimer.async_wait([&](const boost::system::error_code& ec) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 179 | if (ec == boost::asio::error::operation_aborted) |
| 180 | { |
| 181 | return; // we're being canceled |
| 182 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 183 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 184 | }); |
| 185 | } |
| 186 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 187 | void TachSensor::checkThresholds(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 188 | { |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 189 | bool status = thresholds::checkThresholds(this); |
| 190 | if (redundancy) |
| 191 | { |
| 192 | redundancy->update("/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 193 | !status); |
| 194 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 195 | } |
| 196 | |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 197 | PresenceSensor::PresenceSensor(const size_t index, bool inverted, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 198 | boost::asio::io_service& io) : |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 199 | inverted(inverted), |
| 200 | inputDev(io) |
| 201 | { |
| 202 | // todo: use gpiodaemon |
| 203 | std::string device = gpioPath + std::string("gpio") + std::to_string(index); |
| 204 | fd = open((device + "/value").c_str(), O_RDONLY); |
| 205 | if (fd < 0) |
| 206 | { |
| 207 | std::cerr << "Error opening gpio " << index << "\n"; |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | std::ofstream deviceFile(device + "/edge"); |
| 212 | if (!deviceFile.good()) |
| 213 | { |
| 214 | std::cerr << "Error setting edge " << device << "\n"; |
| 215 | return; |
| 216 | } |
| 217 | deviceFile << "both"; |
| 218 | deviceFile.close(); |
| 219 | |
| 220 | inputDev.assign(boost::asio::ip::tcp::v4(), fd); |
| 221 | monitorPresence(); |
| 222 | read(); |
| 223 | } |
| 224 | |
| 225 | PresenceSensor::~PresenceSensor() |
| 226 | { |
| 227 | inputDev.close(); |
| 228 | close(fd); |
| 229 | } |
| 230 | |
| 231 | void PresenceSensor::monitorPresence(void) |
| 232 | { |
| 233 | inputDev.async_wait(boost::asio::ip::tcp::socket::wait_error, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 234 | [this](const boost::system::error_code& ec) { |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 235 | if (ec == boost::system::errc::bad_file_descriptor) |
| 236 | { |
| 237 | return; // we're being destroyed |
| 238 | } |
| 239 | else if (ec) |
| 240 | { |
| 241 | std::cerr |
| 242 | << "Error on presence sensor socket\n"; |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | read(); |
| 247 | } |
| 248 | monitorPresence(); |
| 249 | }); |
| 250 | } |
| 251 | |
| 252 | void PresenceSensor::read(void) |
| 253 | { |
| 254 | constexpr size_t readSize = sizeof("0"); |
| 255 | std::string readBuf; |
| 256 | readBuf.resize(readSize); |
| 257 | lseek(fd, 0, SEEK_SET); |
| 258 | size_t r = ::read(fd, readBuf.data(), readSize); |
James Feist | 95b079b | 2018-11-21 09:28:00 -0800 | [diff] [blame] | 259 | if (r != readSize) |
James Feist | 7bc2bab | 2018-10-26 14:09:45 -0700 | [diff] [blame] | 260 | { |
| 261 | std::cerr << "Error reading gpio\n"; |
| 262 | } |
| 263 | else |
| 264 | { |
| 265 | bool value = std::stoi(readBuf); |
| 266 | if (inverted) |
| 267 | { |
| 268 | value = !value; |
| 269 | } |
| 270 | status = value; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | bool PresenceSensor::getValue(void) |
| 275 | { |
| 276 | return status; |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 277 | } |
| 278 | |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 279 | RedundancySensor::RedundancySensor(size_t count, |
| 280 | const std::vector<std::string>& children, |
| 281 | sdbusplus::asio::object_server& objectServer, |
| 282 | const std::string& sensorConfiguration) : |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 283 | count(count), |
| 284 | iface(objectServer.add_interface( |
| 285 | "/xyz/openbmc_project/control/FanRedundancy/Tach", |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 286 | "xyz.openbmc_project.Control.FanRedundancy")), |
| 287 | association(objectServer.add_interface( |
| 288 | "/xyz/openbmc_project/control/FanRedundancy/Tach", |
| 289 | "org.openbmc.Associations")), |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 290 | objectServer(objectServer) |
| 291 | { |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 292 | createAssociation(association, sensorConfiguration); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 293 | iface->register_property("Collection", children); |
| 294 | iface->register_property("Status", std::string("Full")); |
| 295 | iface->register_property("AllowedFailures", static_cast<uint8_t>(count)); |
| 296 | iface->initialize(); |
| 297 | } |
| 298 | RedundancySensor::~RedundancySensor() |
| 299 | { |
James Feist | 9bb6746 | 2019-03-15 15:09:50 -0700 | [diff] [blame] | 300 | objectServer.remove_interface(association); |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 301 | objectServer.remove_interface(iface); |
| 302 | } |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 303 | void RedundancySensor::update(const std::string& name, bool failed) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 304 | { |
| 305 | statuses[name] = failed; |
| 306 | size_t failedCount = 0; |
| 307 | |
| 308 | std::string state = "Full"; |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 309 | for (const auto& status : statuses) |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 310 | { |
| 311 | if (status.second) |
| 312 | { |
| 313 | failedCount++; |
| 314 | } |
| 315 | if (failedCount > count) |
| 316 | { |
| 317 | state = "Failed"; |
| 318 | break; |
| 319 | } |
| 320 | else if (failedCount) |
| 321 | { |
| 322 | state = "Degraded"; |
| 323 | } |
| 324 | } |
| 325 | iface->set_property("Status", state); |
| 326 | } |