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 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 19 | #include <HwmonTempSensor.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 20 | #include <boost/algorithm/string/predicate.hpp> |
| 21 | #include <boost/algorithm/string/replace.hpp> |
James Feist | 8086aba | 2020-08-25 16:00:59 -0700 | [diff] [blame] | 22 | #include <boost/asio/read_until.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 23 | #include <boost/date_time/posix_time/posix_time.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 24 | #include <sdbusplus/asio/connection.hpp> |
| 25 | #include <sdbusplus/asio/object_server.hpp> |
| 26 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 27 | #include <iostream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 28 | #include <istream> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 29 | #include <limits> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 30 | #include <memory> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 31 | #include <string> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 32 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 33 | |
Bruce Mitchell | e5fc3a5 | 2021-08-10 11:50:25 -0500 | [diff] [blame] | 34 | // Temperatures are read in milli degrees Celsius, we need degrees Celsius. |
| 35 | // Pressures are read in kilopascal, we need Pascals. On D-Bus for Open BMC |
| 36 | // we use the International System of Units without prefixes. |
| 37 | // Links to the kernel documentation: |
| 38 | // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface |
| 39 | // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio |
| 40 | // For IIO RAW sensors we get a raw_value, an offset, and scale to compute |
| 41 | // the value = (raw_value + offset) * scale |
| 42 | static constexpr double sensorOffset = 0.0; |
| 43 | static constexpr double sensorScale = 0.001; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 44 | static constexpr size_t warnAfterErrorCount = 10; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 45 | |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 46 | static constexpr double maxReading = 127; |
| 47 | static constexpr double minReading = -128; |
| 48 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 49 | HwmonTempSensor::HwmonTempSensor( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 50 | const std::string& path, const std::string& objectType, |
| 51 | sdbusplus::asio::object_server& objectServer, |
| 52 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 53 | boost::asio::io_service& io, const std::string& sensorName, |
Jeff Lin | 7b7a9de | 2021-02-22 11:16:27 +0800 | [diff] [blame] | 54 | std::vector<thresholds::Threshold>&& thresholdsIn, const float pollRate, |
James Feist | f9b01b6 | 2020-01-29 15:21:58 -0800 | [diff] [blame] | 55 | const std::string& sensorConfiguration, const PowerState powerState) : |
Jeff Lin | 7b7a9de | 2021-02-22 11:16:27 +0800 | [diff] [blame] | 56 | Sensor(boost::replace_all_copy(sensorName, " ", "_"), |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 57 | std::move(thresholdsIn), sensorConfiguration, objectType, false, |
| 58 | maxReading, minReading, conn, powerState), |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 59 | std::enable_shared_from_this<HwmonTempSensor>(), objServer(objectServer), |
Jeff Lin | 87bc67f | 2020-12-04 20:58:01 +0800 | [diff] [blame] | 60 | inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path), |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 61 | sensorPollMs(static_cast<unsigned int>(pollRate * 1000)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 62 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 63 | sensorInterface = objectServer.add_interface( |
| 64 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 65 | "xyz.openbmc_project.Sensor.Value"); |
| 66 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 67 | if (thresholds::hasWarningInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 68 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 69 | thresholdInterfaceWarning = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 70 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 71 | "xyz.openbmc_project.Sensor.Threshold.Warning"); |
| 72 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 73 | if (thresholds::hasCriticalInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 74 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 75 | thresholdInterfaceCritical = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 76 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 77 | "xyz.openbmc_project.Sensor.Threshold.Critical"); |
| 78 | } |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 79 | association = objectServer.add_interface( |
| 80 | "/xyz/openbmc_project/sensors/temperature/" + name, |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 81 | association::interface); |
Zev Weiss | 6b6891c | 2021-04-22 02:46:21 -0500 | [diff] [blame] | 82 | setInitialProperties(conn, sensor_paths::unitDegreesC); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | HwmonTempSensor::~HwmonTempSensor() |
| 86 | { |
| 87 | // close the input dev to cancel async operations |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 88 | inputDev.close(); |
| 89 | waitTimer.cancel(); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 90 | objServer.remove_interface(thresholdInterfaceWarning); |
| 91 | objServer.remove_interface(thresholdInterfaceCritical); |
| 92 | objServer.remove_interface(sensorInterface); |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 93 | objServer.remove_interface(association); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 96 | void HwmonTempSensor::setupRead(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 97 | { |
Konstantin Aladyshev | a734594 | 2021-04-28 17:09:02 +0300 | [diff] [blame] | 98 | if (!readingStateGood()) |
| 99 | { |
| 100 | markAvailable(false); |
| 101 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 102 | restartRead(); |
| 103 | return; |
| 104 | } |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 105 | |
Konstantin Aladyshev | a734594 | 2021-04-28 17:09:02 +0300 | [diff] [blame] | 106 | std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this(); |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 107 | boost::asio::async_read_until(inputDev, readBuf, '\n', |
| 108 | [weakRef](const boost::system::error_code& ec, |
| 109 | std::size_t /*bytes_transfered*/) { |
| 110 | std::shared_ptr<HwmonTempSensor> self = |
| 111 | weakRef.lock(); |
| 112 | if (self) |
| 113 | { |
| 114 | self->handleResponse(ec); |
| 115 | } |
| 116 | }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Konstantin Aladyshev | a734594 | 2021-04-28 17:09:02 +0300 | [diff] [blame] | 119 | void HwmonTempSensor::restartRead() |
| 120 | { |
| 121 | std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this(); |
| 122 | waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs)); |
| 123 | waitTimer.async_wait([weakRef](const boost::system::error_code& ec) { |
| 124 | if (ec == boost::asio::error::operation_aborted) |
| 125 | { |
| 126 | return; // we're being canceled |
| 127 | } |
| 128 | std::shared_ptr<HwmonTempSensor> self = weakRef.lock(); |
| 129 | if (!self) |
| 130 | { |
| 131 | return; |
| 132 | } |
| 133 | self->setupRead(); |
| 134 | }); |
| 135 | } |
| 136 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 137 | void HwmonTempSensor::handleResponse(const boost::system::error_code& err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 138 | { |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 139 | if ((err == boost::system::errc::bad_file_descriptor) || |
| 140 | (err == boost::asio::error::misc_errors::not_found)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 141 | { |
Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 142 | std::cerr << "Hwmon temp sensor " << name << " removed " << path |
| 143 | << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 144 | return; // we're being destroyed |
| 145 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 146 | std::istream responseStream(&readBuf); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 147 | if (!err) |
| 148 | { |
| 149 | std::string response; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 150 | std::getline(responseStream, response); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 151 | try |
| 152 | { |
Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 153 | rawValue = std::stod(response); |
Bruce Mitchell | e5fc3a5 | 2021-08-10 11:50:25 -0500 | [diff] [blame] | 154 | double nvalue = (rawValue + sensorOffset) * sensorScale; |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 155 | updateValue(nvalue); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 156 | } |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 157 | catch (const std::invalid_argument&) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 158 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 159 | incrementError(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | else |
| 163 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 164 | incrementError(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 165 | } |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 166 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 167 | responseStream.clear(); |
| 168 | inputDev.close(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 169 | int fd = open(path.c_str(), O_RDONLY); |
Jae Hyun Yoo | ef9665a | 2019-10-10 10:26:39 -0700 | [diff] [blame] | 170 | if (fd < 0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 171 | { |
Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 172 | std::cerr << "Hwmon temp sensor " << name << " not valid " << path |
| 173 | << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 174 | return; // we're no longer valid |
| 175 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 176 | inputDev.assign(fd); |
Konstantin Aladyshev | a734594 | 2021-04-28 17:09:02 +0300 | [diff] [blame] | 177 | restartRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 180 | void HwmonTempSensor::checkThresholds(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 181 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 182 | thresholds::checkThresholds(this); |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 183 | } |