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