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