| 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 | 39132a6 | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 40 | Sensor(boost::replace_all_copy(sensorName, " ", "_"), path, | 
|  | 41 | std::move(_thresholds)), | 
|  | 42 | objectType(objectType), configuration(sensorConfiguration), | 
|  | 43 | objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)), | 
|  | 44 | 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 | sensorInterface = objectServer.add_interface( | 
|  | 49 | "/xyz/openbmc_project/sensors/temperature/" + 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/temperature/" + 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/temperature/" + 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); | 
|  | 65 | setupRead(); | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 66 | } | 
|  | 67 |  | 
|  | 68 | HwmonTempSensor::~HwmonTempSensor() | 
|  | 69 | { | 
|  | 70 | // close the input dev to cancel async operations | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 71 | inputDev.close(); | 
|  | 72 | waitTimer.cancel(); | 
| James Feist | af79dd3 | 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 | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 76 | } | 
|  | 77 |  | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 78 | void HwmonTempSensor::setupRead(void) | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 79 | { | 
|  | 80 | boost::asio::async_read_until( | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 81 | inputDev, readBuf, '\n', | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 82 | [&](const boost::system::error_code &ec, | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 83 | std::size_t /*bytes_transfered*/) { handleResponse(ec); }); | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 86 | void HwmonTempSensor::handleResponse(const boost::system::error_code &err) | 
| James Feist | 139cb57 | 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 | } | 
| 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; | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 96 | std::getline(responseStream, response); | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 97 | try | 
|  | 98 | { | 
|  | 99 | float nvalue = std::stof(response); | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 100 | nvalue /= sensorScaleFactor; | 
| Richard Marian Thomaiyar | b7bd2a8 | 2018-11-06 20:25:38 +0530 | [diff] [blame^] | 101 | if (!isnan(overriddenValue)) | 
|  | 102 | { | 
|  | 103 | nvalue = overriddenValue; | 
|  | 104 | } | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 105 | if (nvalue != value) | 
|  | 106 | { | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 107 | updateValue(nvalue); | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 108 | } | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 109 | errCount = 0; | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 110 | } | 
|  | 111 | catch (const std::invalid_argument &) | 
|  | 112 | { | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 113 | errCount++; | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 114 | } | 
|  | 115 | } | 
|  | 116 | else | 
|  | 117 | { | 
| 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 | } | 
| James Feist | 39132a6 | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 120 |  | 
|  | 121 | // only print once | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 122 | if (errCount == warnAfterErrorCount) | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 123 | { | 
| James Feist | 39132a6 | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 124 | std::cerr << "Failure to read sensor " << name << " at " << path | 
|  | 125 | << " ec:" << err << "\n"; | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | if (errCount >= warnAfterErrorCount) | 
|  | 129 | { | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 130 | updateValue(0); | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 131 | } | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 132 | responseStream.clear(); | 
|  | 133 | inputDev.close(); | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 134 | int fd = open(path.c_str(), O_RDONLY); | 
|  | 135 | if (fd <= 0) | 
|  | 136 | { | 
|  | 137 | return; // we're no longer valid | 
|  | 138 | } | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 139 | inputDev.assign(fd); | 
|  | 140 | waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs)); | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 141 | waitTimer.async_wait([&](const boost::system::error_code &ec) { | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 142 | if (ec == boost::asio::error::operation_aborted) | 
|  | 143 | { | 
|  | 144 | return; // we're being canceled | 
|  | 145 | } | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 146 | setupRead(); | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 147 | }); | 
|  | 148 | } | 
|  | 149 |  | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 150 | void HwmonTempSensor::checkThresholds(void) | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 151 | { | 
| James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 152 | thresholds::checkThresholds(this); | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 153 | } | 
|  | 154 |  | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 155 | void HwmonTempSensor::updateValue(const double &newValue) | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 156 | { | 
| Richard Marian Thomaiyar | b7bd2a8 | 2018-11-06 20:25:38 +0530 | [diff] [blame^] | 157 | // Indicate that it is internal set call | 
|  | 158 | internalSet = true; | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 159 | sensorInterface->set_property("Value", newValue); | 
| Richard Marian Thomaiyar | b7bd2a8 | 2018-11-06 20:25:38 +0530 | [diff] [blame^] | 160 | internalSet = false; | 
| Jae Hyun Yoo | f78ec41 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 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 HwmonTempSensor::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); | 
| Richard Marian Thomaiyar | b7bd2a8 | 2018-11-06 20:25:38 +0530 | [diff] [blame^] | 171 | sensorInterface->register_property( | 
|  | 172 | "Value", value, [&](const double &newValue, double &oldValue) { | 
|  | 173 | return setSensorValue(newValue, oldValue); | 
|  | 174 | }); | 
| James Feist | 139cb57 | 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 | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 183 | iface = thresholdInterfaceCritical; | 
| James Feist | 139cb57 | 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 | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 197 | iface = thresholdInterfaceWarning; | 
| James Feist | 139cb57 | 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 | iface->register_property( | 
|  | 220 | level, threshold.value, | 
|  | 221 | [&](const double &request, double &oldValue) { | 
|  | 222 | oldValue = request; // todo, just let the config do this? | 
|  | 223 | threshold.value = request; | 
|  | 224 | thresholds::persistThreshold(configuration, objectType, | 
|  | 225 | threshold, conn); | 
|  | 226 | return 1; | 
|  | 227 | }); | 
|  | 228 | iface->register_property(alarm, false); | 
|  | 229 | } | 
| James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 230 | if (!sensorInterface->initialize()) | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 231 | { | 
|  | 232 | std::cerr << "error initializing value interface\n"; | 
|  | 233 | } | 
| James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 234 | if (thresholdInterfaceWarning && !thresholdInterfaceWarning->initialize()) | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 235 | { | 
|  | 236 | std::cerr << "error initializing warning threshold interface\n"; | 
|  | 237 | } | 
|  | 238 |  | 
| James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 239 | if (thresholdInterfaceCritical && !thresholdInterfaceCritical->initialize()) | 
| James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 240 | { | 
|  | 241 | std::cerr << "error initializing critical threshold interface\n"; | 
|  | 242 | } | 
|  | 243 | } |