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