James Feist | 6714a25 | 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 <ADCSensor.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 | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 29 | static constexpr unsigned int sensorPollMs = 500; |
| 30 | static constexpr size_t warnAfterErrorCount = 10; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 31 | |
| 32 | // scaling factor from hwmon |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 33 | static constexpr unsigned int sensorScaleFactor = 1000; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 34 | |
| 35 | ADCSensor::ADCSensor(const std::string &path, |
| 36 | sdbusplus::asio::object_server &objectServer, |
| 37 | std::shared_ptr<sdbusplus::asio::connection> &conn, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 38 | boost::asio::io_service &io, const std::string &sensorName, |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 39 | std::vector<thresholds::Threshold> &&_thresholds, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 40 | const double scaleFactor, |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 41 | const std::string &sensorConfiguration) : |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 42 | Sensor(boost::replace_all_copy(sensorName, " ", "_"), path, |
| 43 | std::move(_thresholds)), |
| 44 | objServer(objectServer), configuration(sensorConfiguration), |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 45 | scaleFactor(scaleFactor), inputDev(io, open(path.c_str(), O_RDONLY)), |
| 46 | waitTimer(io), errCount(0), |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 47 | // todo, get these from config |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 48 | maxValue(20), minValue(0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 49 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 50 | sensorInterface = objectServer.add_interface( |
| 51 | "/xyz/openbmc_project/sensors/voltage/" + name, |
| 52 | "xyz.openbmc_project.Sensor.Value"); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 53 | if (thresholds::hasWarningInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 54 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 55 | thresholdInterfaceWarning = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 56 | "/xyz/openbmc_project/sensors/voltage/" + name, |
| 57 | "xyz.openbmc_project.Sensor.Threshold.Warning"); |
| 58 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 59 | if (thresholds::hasCriticalInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 60 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 61 | thresholdInterfaceCritical = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 62 | "/xyz/openbmc_project/sensors/voltage/" + name, |
| 63 | "xyz.openbmc_project.Sensor.Threshold.Critical"); |
| 64 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 65 | setInitialProperties(conn); |
| 66 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | ADCSensor::~ADCSensor() |
| 70 | { |
| 71 | // close the input dev to cancel async operations |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 72 | inputDev.close(); |
| 73 | waitTimer.cancel(); |
James Feist | 251c782 | 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 | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 79 | void ADCSensor::setupRead(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 80 | { |
| 81 | boost::asio::async_read_until( |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 82 | inputDev, readBuf, '\n', |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 83 | [&](const boost::system::error_code &ec, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 84 | std::size_t /*bytes_transfered*/) { handleResponse(ec); }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 87 | void ADCSensor::handleResponse(const boost::system::error_code &err) |
James Feist | 6714a25 | 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 | 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 | |
| 95 | if (!err) |
| 96 | { |
| 97 | std::string response; |
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 | |
| 100 | // todo read scaling factors from configuration |
| 101 | try |
| 102 | { |
| 103 | float nvalue = std::stof(response); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 104 | nvalue = (nvalue / sensorScaleFactor) / scaleFactor; |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 105 | if (!isnan(overriddenValue)) |
| 106 | { |
| 107 | nvalue = overriddenValue; |
| 108 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 109 | if (nvalue != value) |
| 110 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 111 | updateValue(nvalue); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 112 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 113 | errCount = 0; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 114 | } |
| 115 | catch (std::invalid_argument) |
| 116 | { |
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 | else |
| 121 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 122 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 123 | errCount++; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 124 | } |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 125 | // only print once |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 126 | if (errCount == warnAfterErrorCount) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 127 | { |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 128 | std::cerr << "Failure to read sensor " << name << " at " << path |
| 129 | << " ec:" << err << "\n"; |
| 130 | } |
| 131 | |
| 132 | if (errCount >= warnAfterErrorCount) |
| 133 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 134 | updateValue(0); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 137 | responseStream.clear(); |
| 138 | inputDev.close(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 139 | int fd = open(path.c_str(), O_RDONLY); |
| 140 | if (fd <= 0) |
| 141 | { |
| 142 | return; // we're no longer valid |
| 143 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 144 | inputDev.assign(fd); |
| 145 | waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs)); |
| 146 | waitTimer.async_wait([&](const boost::system::error_code &ec) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 147 | if (ec == boost::asio::error::operation_aborted) |
| 148 | { |
| 149 | return; // we're being canceled |
| 150 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 151 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 152 | }); |
| 153 | } |
| 154 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 155 | void ADCSensor::checkThresholds(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 156 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 157 | thresholds::checkThresholds(this); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 160 | void ADCSensor::updateValue(const double &newValue) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 161 | { |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 162 | // Indicate that it is internal set call |
| 163 | internalSet = true; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 164 | bool ret = sensorInterface->set_property("Value", newValue); |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 165 | internalSet = false; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 166 | value = newValue; |
| 167 | checkThresholds(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 170 | void ADCSensor::setInitialProperties( |
James Feist | 6714a25 | 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 | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 174 | sensorInterface->register_property("MaxValue", maxValue); |
| 175 | sensorInterface->register_property("MinValue", minValue); |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 176 | sensorInterface->register_property( |
| 177 | "Value", value, [&](const double &newValue, double &oldValue) { |
| 178 | return setSensorValue(newValue, oldValue); |
| 179 | }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 180 | |
| 181 | for (auto &threshold : thresholds) |
| 182 | { |
| 183 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface; |
| 184 | std::string level; |
| 185 | std::string alarm; |
| 186 | if (threshold.level == thresholds::Level::CRITICAL) |
| 187 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 188 | iface = thresholdInterfaceCritical; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 189 | if (threshold.direction == thresholds::Direction::HIGH) |
| 190 | { |
| 191 | level = "CriticalHigh"; |
| 192 | alarm = "CriticalAlarmHigh"; |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | level = "CriticalLow"; |
| 197 | alarm = "CriticalAlarmLow"; |
| 198 | } |
| 199 | } |
| 200 | else if (threshold.level == thresholds::Level::WARNING) |
| 201 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 202 | iface = thresholdInterfaceWarning; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 203 | if (threshold.direction == thresholds::Direction::HIGH) |
| 204 | { |
| 205 | level = "WarningHigh"; |
| 206 | alarm = "WarningAlarmHigh"; |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | level = "WarningLow"; |
| 211 | alarm = "WarningAlarmLow"; |
| 212 | } |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | std::cerr << "Unknown threshold level" << threshold.level << "\n"; |
| 217 | continue; |
| 218 | } |
| 219 | if (!iface) |
| 220 | { |
| 221 | std::cout << "trying to set uninitialized interface\n"; |
| 222 | continue; |
| 223 | } |
| 224 | iface->register_property( |
| 225 | level, threshold.value, |
| 226 | [&](const double &request, double &oldValue) { |
| 227 | oldValue = request; // todo, just let the config do this? |
| 228 | threshold.value = request; |
| 229 | thresholds::persistThreshold( |
| 230 | configuration, "xyz.openbmc_project.Configuration.ADC", |
| 231 | threshold, conn); |
| 232 | return 1; |
| 233 | }); |
| 234 | iface->register_property(alarm, false); |
| 235 | } |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 236 | if (!sensorInterface->initialize()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 237 | { |
| 238 | std::cerr << "error initializing value interface\n"; |
| 239 | } |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 240 | if (thresholdInterfaceWarning && !thresholdInterfaceWarning->initialize()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 241 | { |
| 242 | std::cerr << "error initializing warning threshold interface\n"; |
| 243 | } |
| 244 | |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 245 | if (thresholdInterfaceCritical && !thresholdInterfaceCritical->initialize()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 246 | { |
| 247 | std::cerr << "error initializing critical threshold interface\n"; |
| 248 | } |
| 249 | } |