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