James Feist | 139cb57 | 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> |
| 24 | #include <iostream> |
| 25 | #include <limits> |
| 26 | #include <sdbusplus/asio/connection.hpp> |
| 27 | #include <sdbusplus/asio/object_server.hpp> |
| 28 | #include <string> |
| 29 | |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 30 | static constexpr unsigned int pwmPollMs = 500; |
| 31 | static constexpr size_t warnAfterErrorCount = 10; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 32 | |
| 33 | TachSensor::TachSensor(const std::string &path, |
| 34 | sdbusplus::asio::object_server &objectServer, |
| 35 | std::shared_ptr<sdbusplus::asio::connection> &conn, |
| 36 | boost::asio::io_service &io, const std::string &fanName, |
| 37 | std::vector<thresholds::Threshold> &&_thresholds, |
| 38 | const std::string &sensorConfiguration) : |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 39 | Sensor(), |
| 40 | path(path), objServer(objectServer), dbusConnection(conn), |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 41 | name(boost::replace_all_copy(fanName, " ", "_")), |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 42 | configuration(sensorConfiguration), |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 43 | inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), errCount(0), |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 44 | // todo, get these from config |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 45 | maxValue(25000), minValue(0) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 46 | { |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 47 | thresholds = std::move(_thresholds); |
| 48 | sensorInterface = objectServer.add_interface( |
| 49 | "/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 50 | "xyz.openbmc_project.Sensor.Value"); |
| 51 | |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 52 | if (thresholds::hasWarningInterface(thresholds)) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 53 | { |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 54 | thresholdInterfaceWarning = objectServer.add_interface( |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 55 | "/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 56 | "xyz.openbmc_project.Sensor.Threshold.Warning"); |
| 57 | } |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 58 | if (thresholds::hasCriticalInterface(thresholds)) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 59 | { |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 60 | thresholdInterfaceCritical = objectServer.add_interface( |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 61 | "/xyz/openbmc_project/sensors/fan_tach/" + name, |
| 62 | "xyz.openbmc_project.Sensor.Threshold.Critical"); |
| 63 | } |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 64 | setInitialProperties(conn); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 65 | isPowerOn(dbusConnection); // first call initializes |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 66 | setupRead(); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | TachSensor::~TachSensor() |
| 70 | { |
| 71 | // close the input dev to cancel async operations |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 72 | inputDev.close(); |
| 73 | waitTimer.cancel(); |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 74 | objServer.remove_interface(thresholdInterfaceWarning); |
| 75 | objServer.remove_interface(thresholdInterfaceCritical); |
| 76 | objServer.remove_interface(sensorInterface); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 79 | void TachSensor::setupRead(void) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 80 | { |
| 81 | boost::asio::async_read_until( |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 82 | inputDev, readBuf, '\n', |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 83 | [&](const boost::system::error_code &ec, |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 84 | std::size_t /*bytes_transfered*/) { handleResponse(ec); }); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 87 | void TachSensor::handleResponse(const boost::system::error_code &err) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 88 | { |
| 89 | if (err == boost::system::errc::bad_file_descriptor) |
| 90 | { |
| 91 | return; // we're being destroyed |
| 92 | } |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 93 | std::istream responseStream(&readBuf); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 94 | if (!err) |
| 95 | { |
| 96 | std::string response; |
| 97 | try |
| 98 | { |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 99 | std::getline(responseStream, response); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 100 | float nvalue = std::stof(response); |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 101 | responseStream.clear(); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 102 | if (nvalue != value) |
| 103 | { |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 104 | updateValue(nvalue); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 105 | } |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 106 | errCount = 0; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 107 | } |
| 108 | catch (const std::invalid_argument &) |
| 109 | { |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 110 | errCount++; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 116 | errCount++; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 117 | } |
| 118 | // only send value update once |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 119 | if (errCount == warnAfterErrorCount) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 120 | { |
| 121 | // only an error if power is on |
| 122 | if (isPowerOn(dbusConnection)) |
| 123 | { |
| 124 | std::cerr << "Failure to read sensor " << name << " at " << path |
| 125 | << "\n"; |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 126 | updateValue(0); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 127 | } |
| 128 | else |
| 129 | { |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 130 | errCount = 0; // check power again in 10 cycles |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 131 | sensorInterface->set_property( |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 132 | "Value", std::numeric_limits<double>::quiet_NaN()); |
| 133 | } |
| 134 | } |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 135 | responseStream.clear(); |
| 136 | inputDev.close(); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 137 | int fd = open(path.c_str(), O_RDONLY); |
| 138 | if (fd <= 0) |
| 139 | { |
| 140 | return; // we're no longer valid |
| 141 | } |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 142 | inputDev.assign(fd); |
| 143 | waitTimer.expires_from_now(boost::posix_time::milliseconds(pwmPollMs)); |
| 144 | waitTimer.async_wait([&](const boost::system::error_code &ec) { |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 145 | if (ec == boost::asio::error::operation_aborted) |
| 146 | { |
| 147 | return; // we're being canceled |
| 148 | } |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 149 | setupRead(); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 150 | }); |
| 151 | } |
| 152 | |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 153 | void TachSensor::checkThresholds(void) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 154 | { |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 155 | thresholds::checkThresholds(this); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 158 | void TachSensor::updateValue(const double &newValue) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 159 | { |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 160 | sensorInterface->set_property("Value", newValue); |
| 161 | value = newValue; |
| 162 | checkThresholds(); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 165 | void TachSensor::setInitialProperties( |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 166 | std::shared_ptr<sdbusplus::asio::connection> &conn) |
| 167 | { |
| 168 | // todo, get max and min from configuration |
Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 169 | sensorInterface->register_property("MaxValue", maxValue); |
| 170 | sensorInterface->register_property("MinValue", minValue); |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 171 | sensorInterface->register_property("Value", value); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 172 | |
| 173 | for (auto &threshold : thresholds) |
| 174 | { |
| 175 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface; |
| 176 | std::string level; |
| 177 | std::string alarm; |
| 178 | if (threshold.level == thresholds::Level::CRITICAL) |
| 179 | { |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 180 | iface = thresholdInterfaceCritical; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 181 | if (threshold.direction == thresholds::Direction::HIGH) |
| 182 | { |
| 183 | level = "CriticalHigh"; |
| 184 | alarm = "CriticalAlarmHigh"; |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | level = "CriticalLow"; |
| 189 | alarm = "CriticalAlarmLow"; |
| 190 | } |
| 191 | } |
| 192 | else if (threshold.level == thresholds::Level::WARNING) |
| 193 | { |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 194 | iface = thresholdInterfaceWarning; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 195 | if (threshold.direction == thresholds::Direction::HIGH) |
| 196 | { |
| 197 | level = "WarningHigh"; |
| 198 | alarm = "WarningAlarmHigh"; |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | level = "WarningLow"; |
| 203 | alarm = "WarningAlarmLow"; |
| 204 | } |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | std::cerr << "Unknown threshold level" << threshold.level << "\n"; |
| 209 | continue; |
| 210 | } |
| 211 | if (!iface) |
| 212 | { |
| 213 | std::cout << "trying to set uninitialized interface\n"; |
| 214 | continue; |
| 215 | } |
| 216 | iface->register_property( |
| 217 | level, threshold.value, |
| 218 | [&](const double &request, double &oldValue) { |
| 219 | oldValue = request; // todo, just let the config do this? |
| 220 | threshold.value = request; |
| 221 | thresholds::persistThreshold( |
| 222 | configuration, |
| 223 | "xyz.openbmc_project.Configuration.AspeedFan", threshold, |
| 224 | conn); |
| 225 | return 1; |
| 226 | }); |
| 227 | iface->register_property(alarm, false); |
| 228 | } |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 229 | if (!sensorInterface->initialize()) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 230 | { |
| 231 | std::cerr << "error initializing value interface\n"; |
| 232 | } |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 233 | if (thresholdInterfaceWarning && !thresholdInterfaceWarning->initialize()) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 234 | { |
| 235 | std::cerr << "error initializing warning threshold interface\n"; |
| 236 | } |
| 237 | |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 238 | if (thresholdInterfaceCritical && !thresholdInterfaceCritical->initialize()) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 239 | { |
| 240 | std::cerr << "error initializing critical threshold interface\n"; |
| 241 | } |
| 242 | } |