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 | |
James Feist | cc6d4fe | 2018-11-14 16:38:26 -0800 | [diff] [blame] | 35 | static constexpr double roundFactor = 10000; // 3 decimal places |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 36 | static constexpr double maxReading = 20; |
| 37 | static constexpr double minReading = 0; |
James Feist | cc6d4fe | 2018-11-14 16:38:26 -0800 | [diff] [blame] | 38 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 39 | ADCSensor::ADCSensor(const std::string& path, |
| 40 | sdbusplus::asio::object_server& objectServer, |
| 41 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 42 | boost::asio::io_service& io, const std::string& sensorName, |
| 43 | std::vector<thresholds::Threshold>&& _thresholds, |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 44 | const double scaleFactor, PowerState readState, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 45 | const std::string& sensorConfiguration) : |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 46 | Sensor(boost::replace_all_copy(sensorName, " ", "_"), path, |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 47 | std::move(_thresholds), sensorConfiguration, |
| 48 | "xyz.openbmc_project.Configuration.ADC", maxReading, minReading), |
| 49 | objServer(objectServer), scaleFactor(scaleFactor), |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 50 | readState(std::move(readState)), inputDev(io, open(path.c_str(), O_RDONLY)), |
| 51 | waitTimer(io), errCount(0) |
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 | sensorInterface = objectServer.add_interface( |
| 54 | "/xyz/openbmc_project/sensors/voltage/" + name, |
| 55 | "xyz.openbmc_project.Sensor.Value"); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 56 | if (thresholds::hasWarningInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 57 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 58 | thresholdInterfaceWarning = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 59 | "/xyz/openbmc_project/sensors/voltage/" + name, |
| 60 | "xyz.openbmc_project.Sensor.Threshold.Warning"); |
| 61 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 62 | if (thresholds::hasCriticalInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 63 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 64 | thresholdInterfaceCritical = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 65 | "/xyz/openbmc_project/sensors/voltage/" + name, |
| 66 | "xyz.openbmc_project.Sensor.Threshold.Critical"); |
| 67 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 68 | setInitialProperties(conn); |
| 69 | setupRead(); |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 70 | |
| 71 | // setup match |
| 72 | setupPowerMatch(conn); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | ADCSensor::~ADCSensor() |
| 76 | { |
| 77 | // close the input dev to cancel async operations |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 78 | inputDev.close(); |
| 79 | waitTimer.cancel(); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 80 | objServer.remove_interface(thresholdInterfaceWarning); |
| 81 | objServer.remove_interface(thresholdInterfaceCritical); |
| 82 | objServer.remove_interface(sensorInterface); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 85 | void ADCSensor::setupRead(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 86 | { |
| 87 | boost::asio::async_read_until( |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 88 | inputDev, readBuf, '\n', |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 89 | [&](const boost::system::error_code& ec, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 90 | std::size_t /*bytes_transfered*/) { handleResponse(ec); }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 91 | } |
| 92 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 93 | void ADCSensor::handleResponse(const boost::system::error_code& err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 94 | { |
| 95 | if (err == boost::system::errc::bad_file_descriptor) |
| 96 | { |
| 97 | return; // we're being destroyed |
| 98 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 99 | std::istream responseStream(&readBuf); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 100 | |
| 101 | if (!err) |
| 102 | { |
| 103 | std::string response; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 104 | std::getline(responseStream, response); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 105 | |
| 106 | // todo read scaling factors from configuration |
| 107 | try |
| 108 | { |
James Feist | cc6d4fe | 2018-11-14 16:38:26 -0800 | [diff] [blame] | 109 | double nvalue = std::stof(response); |
| 110 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 111 | nvalue = (nvalue / sensorScaleFactor) / scaleFactor; |
James Feist | cc6d4fe | 2018-11-14 16:38:26 -0800 | [diff] [blame] | 112 | nvalue = std::round(nvalue * roundFactor) / roundFactor; |
| 113 | |
Richard Marian Thomaiyar | b2eb29a | 2018-12-08 18:26:58 +0530 | [diff] [blame] | 114 | if (overridenState) |
Richard Marian Thomaiyar | 8721922 | 2018-11-06 20:25:38 +0530 | [diff] [blame] | 115 | { |
| 116 | nvalue = overriddenValue; |
| 117 | } |
James Feist | cc6d4fe | 2018-11-14 16:38:26 -0800 | [diff] [blame] | 118 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 119 | if (nvalue != value) |
| 120 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 121 | updateValue(nvalue); |
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 = 0; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 124 | } |
| 125 | catch (std::invalid_argument) |
| 126 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 127 | errCount++; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | else |
| 131 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 132 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 133 | errCount++; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 134 | } |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 135 | // only print once |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 136 | if (errCount == warnAfterErrorCount) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 137 | { |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 138 | std::cerr << "Failure to read sensor " << name << " at " << path |
| 139 | << " ec:" << err << "\n"; |
| 140 | } |
| 141 | |
| 142 | if (errCount >= warnAfterErrorCount) |
| 143 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 144 | updateValue(0); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 147 | responseStream.clear(); |
| 148 | inputDev.close(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 149 | int fd = open(path.c_str(), O_RDONLY); |
| 150 | if (fd <= 0) |
| 151 | { |
| 152 | return; // we're no longer valid |
| 153 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 154 | inputDev.assign(fd); |
| 155 | waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs)); |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 156 | waitTimer.async_wait([&](const boost::system::error_code& ec) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 157 | if (ec == boost::asio::error::operation_aborted) |
| 158 | { |
| 159 | return; // we're being canceled |
| 160 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 161 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 162 | }); |
| 163 | } |
| 164 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 165 | void ADCSensor::checkThresholds(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 166 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 167 | if (readState == PowerState::on && !isPowerOn()) |
| 168 | { |
| 169 | return; |
| 170 | } |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 171 | thresholds::checkThresholds(this); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 172 | } |