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 <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 | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 30 | static constexpr size_t warnAfterErrorCount = 10; |
James Feist | 6714a25 | 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 | dc6c55f | 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 | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 41 | configuration(sensorConfiguration), |
| 42 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 43 | inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), errCount(0), |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 44 | // todo, get these from config |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 45 | maxValue(127), minValue(-128) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 46 | { |
James Feist | 251c782 | 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 | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 50 | if (thresholds::hasWarningInterface(thresholds)) |
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 | thresholdInterfaceWarning = objectServer.add_interface( |
James Feist | 6714a25 | 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 | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 56 | if (thresholds::hasCriticalInterface(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 | thresholdInterfaceCritical = objectServer.add_interface( |
James Feist | 6714a25 | 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 | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 62 | setInitialProperties(conn); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 63 | isPowerOn(dbusConnection); // first call initializes |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 64 | setupRead(); |
James Feist | 6714a25 | 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 | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 70 | inputDev.close(); |
| 71 | waitTimer.cancel(); |
James Feist | 251c782 | 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 | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 77 | void CPUSensor::setupRead(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 78 | { |
| 79 | boost::asio::async_read_until( |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 80 | inputDev, readBuf, '\n', |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 81 | [&](const boost::system::error_code &ec, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 82 | std::size_t /*bytes_transfered*/) { handleResponse(ec); }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 85 | void CPUSensor::handleResponse(const boost::system::error_code &err) |
James Feist | 6714a25 | 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 | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 91 | size_t pollTime = CPUSensor::sensorPollMs; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 92 | std::istream responseStream(&readBuf); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 93 | if (!err) |
| 94 | { |
| 95 | std::string response; |
| 96 | try |
| 97 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 98 | std::getline(responseStream, response); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 99 | float nvalue = std::stof(response); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 100 | responseStream.clear(); |
| 101 | nvalue /= CPUSensor::sensorScaleFactor; |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame^] | 102 | if (!isnan(overriddenValue)) |
| 103 | { |
| 104 | nvalue = overriddenValue; |
| 105 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 106 | if (nvalue != value) |
| 107 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 108 | updateValue(nvalue); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 109 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 110 | errCount = 0; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 111 | } |
| 112 | catch (const std::invalid_argument &) |
| 113 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 114 | errCount++; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | else |
| 118 | { |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 119 | pollTime = sensorFailedPollTimeMs; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 120 | errCount++; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 121 | } |
| 122 | |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 123 | if (errCount >= warnAfterErrorCount) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 124 | { |
| 125 | // only an error if power is on |
| 126 | if (isPowerOn(dbusConnection)) |
| 127 | { |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 128 | // only print once |
| 129 | if (errCount == warnAfterErrorCount) |
| 130 | { |
| 131 | std::cerr << "Failure to read sensor " << name << " at " << path |
| 132 | << "\n"; |
| 133 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 134 | updateValue(0); |
| 135 | errCount++; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 136 | } |
| 137 | else |
| 138 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 139 | errCount = 0; // check power again in 10 cycles |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 140 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 144 | responseStream.clear(); |
| 145 | inputDev.close(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 146 | int fd = open(path.c_str(), O_RDONLY); |
| 147 | if (fd <= 0) |
| 148 | { |
| 149 | return; // we're no longer valid |
| 150 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 151 | inputDev.assign(fd); |
James Feist | 1169eb4 | 2018-10-31 10:08:47 -0700 | [diff] [blame] | 152 | waitTimer.expires_from_now(boost::posix_time::milliseconds(pollTime)); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 153 | waitTimer.async_wait([&](const boost::system::error_code &ec) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 154 | if (ec == boost::asio::error::operation_aborted) |
| 155 | { |
| 156 | return; // we're being canceled |
| 157 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 158 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 159 | }); |
| 160 | } |
| 161 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 162 | void CPUSensor::checkThresholds(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 163 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 164 | thresholds::checkThresholds(this); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 167 | void CPUSensor::updateValue(const double &newValue) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 168 | { |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame^] | 169 | // Indicate that it is internal set call |
| 170 | internalSet = true; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 171 | sensorInterface->set_property("Value", newValue); |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame^] | 172 | internalSet = false; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 173 | value = newValue; |
| 174 | checkThresholds(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 177 | void CPUSensor::setInitialProperties( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 178 | std::shared_ptr<sdbusplus::asio::connection> &conn) |
| 179 | { |
| 180 | // todo, get max and min from configuration |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 181 | sensorInterface->register_property("MaxValue", maxValue); |
| 182 | sensorInterface->register_property("MinValue", minValue); |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame^] | 183 | sensorInterface->register_property( |
| 184 | "Value", value, [&](const double &newValue, double &oldValue) { |
| 185 | return setSensorValue(newValue, oldValue); |
| 186 | }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 187 | |
| 188 | for (auto &threshold : thresholds) |
| 189 | { |
| 190 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface; |
| 191 | std::string level; |
| 192 | std::string alarm; |
| 193 | if (threshold.level == thresholds::Level::CRITICAL) |
| 194 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 195 | iface = thresholdInterfaceCritical; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 196 | if (threshold.direction == thresholds::Direction::HIGH) |
| 197 | { |
| 198 | level = "CriticalHigh"; |
| 199 | alarm = "CriticalAlarmHigh"; |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | level = "CriticalLow"; |
| 204 | alarm = "CriticalAlarmLow"; |
| 205 | } |
| 206 | } |
| 207 | else if (threshold.level == thresholds::Level::WARNING) |
| 208 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 209 | iface = thresholdInterfaceWarning; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 210 | if (threshold.direction == thresholds::Direction::HIGH) |
| 211 | { |
| 212 | level = "WarningHigh"; |
| 213 | alarm = "WarningAlarmHigh"; |
| 214 | } |
| 215 | else |
| 216 | { |
| 217 | level = "WarningLow"; |
| 218 | alarm = "WarningAlarmLow"; |
| 219 | } |
| 220 | } |
| 221 | else |
| 222 | { |
| 223 | std::cerr << "Unknown threshold level" << threshold.level << "\n"; |
| 224 | continue; |
| 225 | } |
| 226 | if (!iface) |
| 227 | { |
| 228 | std::cout << "trying to set uninitialized interface\n"; |
| 229 | continue; |
| 230 | } |
| 231 | if (threshold.writeable) |
| 232 | { |
| 233 | iface->register_property( |
| 234 | level, threshold.value, |
| 235 | [&](const double &request, double &oldValue) { |
| 236 | oldValue = request; // todo, just let the config do this? |
| 237 | threshold.value = request; |
| 238 | thresholds::persistThreshold(configuration, objectType, |
| 239 | threshold, conn); |
| 240 | return 1; |
| 241 | }); |
| 242 | } |
| 243 | else |
| 244 | { |
| 245 | iface->register_property(level, threshold.value); |
| 246 | } |
| 247 | iface->register_property(alarm, false); |
| 248 | } |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 249 | if (!sensorInterface->initialize()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 250 | { |
| 251 | std::cerr << "error initializing value interface\n"; |
| 252 | } |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 253 | if (thresholdInterfaceWarning && !thresholdInterfaceWarning->initialize()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 254 | { |
| 255 | std::cerr << "error initializing warning threshold interface\n"; |
| 256 | } |
| 257 | |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 258 | if (thresholdInterfaceCritical && !thresholdInterfaceCritical->initialize()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 259 | { |
| 260 | std::cerr << "error initializing critical threshold interface\n"; |
| 261 | } |
| 262 | } |