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 | |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 17 | #include "HwmonTempSensor.hpp" |
| 18 | |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 19 | #include "DeviceMgmt.hpp" |
| 20 | #include "Thresholds.hpp" |
| 21 | #include "Utils.hpp" |
| 22 | #include "sensor.hpp" |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 23 | |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 24 | #include <boost/algorithm/string/replace.hpp> |
| 25 | #include <boost/asio/buffer.hpp> |
| 26 | #include <boost/asio/error.hpp> |
| 27 | #include <boost/asio/io_context.hpp> |
| 28 | #include <boost/asio/random_access_file.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 29 | #include <sdbusplus/asio/connection.hpp> |
| 30 | #include <sdbusplus/asio/object_server.hpp> |
| 31 | |
Zev Weiss | d1c8f44 | 2022-07-29 11:54:41 -0700 | [diff] [blame] | 32 | #include <charconv> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 33 | #include <chrono> |
| 34 | #include <cstddef> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 35 | #include <iostream> |
| 36 | #include <limits> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 37 | #include <memory> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 38 | #include <string> |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 39 | #include <system_error> |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 40 | #include <utility> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 41 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 42 | |
Bruce Mitchell | e5fc3a5 | 2021-08-10 11:50:25 -0500 | [diff] [blame] | 43 | // Temperatures are read in milli degrees Celsius, we need degrees Celsius. |
| 44 | // Pressures are read in kilopascal, we need Pascals. On D-Bus for Open BMC |
| 45 | // we use the International System of Units without prefixes. |
| 46 | // Links to the kernel documentation: |
| 47 | // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface |
| 48 | // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio |
| 49 | // For IIO RAW sensors we get a raw_value, an offset, and scale to compute |
| 50 | // the value = (raw_value + offset) * scale |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 51 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 52 | HwmonTempSensor::HwmonTempSensor( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 53 | const std::string& path, const std::string& objectType, |
| 54 | sdbusplus::asio::object_server& objectServer, |
| 55 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 56 | boost::asio::io_context& io, const std::string& sensorName, |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 57 | std::vector<thresholds::Threshold>&& thresholdsIn, |
| 58 | const struct SensorParams& thisSensorParameters, const float pollRate, |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 59 | const std::string& sensorConfiguration, const PowerState powerState, |
| 60 | const std::shared_ptr<I2CDevice>& i2cDevice) : |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 61 | Sensor(boost::replace_all_copy(sensorName, " ", "_"), |
| 62 | std::move(thresholdsIn), sensorConfiguration, objectType, false, |
| 63 | false, thisSensorParameters.maxValue, thisSensorParameters.minValue, |
| 64 | conn, powerState), |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 65 | i2cDevice(i2cDevice), objServer(objectServer), |
Zev Weiss | d1c8f44 | 2022-07-29 11:54:41 -0700 | [diff] [blame] | 66 | inputDev(io, path, boost::asio::random_access_file::read_only), |
| 67 | waitTimer(io), path(path), offsetValue(thisSensorParameters.offsetValue), |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 68 | scaleValue(thisSensorParameters.scaleValue), |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 69 | sensorPollMs(static_cast<unsigned int>(pollRate * 1000)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 70 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 71 | sensorInterface = objectServer.add_interface( |
Bruce Mitchell | 544e7dc | 2021-07-29 18:05:49 -0500 | [diff] [blame] | 72 | "/xyz/openbmc_project/sensors/" + thisSensorParameters.typeName + "/" + |
| 73 | name, |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 74 | "xyz.openbmc_project.Sensor.Value"); |
| 75 | |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 76 | for (const auto& threshold : thresholds) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 77 | { |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 78 | std::string interface = thresholds::getInterface(threshold.level); |
| 79 | thresholdInterfaces[static_cast<size_t>(threshold.level)] = |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame^] | 80 | objectServer.add_interface( |
| 81 | "/xyz/openbmc_project/sensors/" + |
| 82 | thisSensorParameters.typeName + "/" + name, |
| 83 | interface); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 84 | } |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame^] | 85 | association = objectServer.add_interface( |
| 86 | "/xyz/openbmc_project/sensors/" + thisSensorParameters.typeName + "/" + |
| 87 | name, |
| 88 | association::interface); |
Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 89 | setInitialProperties(thisSensorParameters.units); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 92 | bool HwmonTempSensor::isActive() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 93 | { |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 94 | return inputDev.is_open(); |
| 95 | } |
| 96 | |
| 97 | void HwmonTempSensor::activate(const std::string& newPath, |
| 98 | const std::shared_ptr<I2CDevice>& newI2CDevice) |
| 99 | { |
| 100 | path = newPath; |
| 101 | i2cDevice = newI2CDevice; |
| 102 | inputDev.open(path, boost::asio::random_access_file::read_only); |
| 103 | markAvailable(true); |
| 104 | setupRead(); |
| 105 | } |
| 106 | |
| 107 | void HwmonTempSensor::deactivate() |
| 108 | { |
| 109 | markAvailable(false); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 110 | // close the input dev to cancel async operations |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 111 | inputDev.close(); |
| 112 | waitTimer.cancel(); |
Zev Weiss | a1456c4 | 2022-07-18 16:59:35 -0700 | [diff] [blame] | 113 | i2cDevice = nullptr; |
| 114 | path = ""; |
| 115 | } |
| 116 | |
| 117 | HwmonTempSensor::~HwmonTempSensor() |
| 118 | { |
| 119 | deactivate(); |
| 120 | |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 121 | for (const auto& iface : thresholdInterfaces) |
| 122 | { |
| 123 | objServer.remove_interface(iface); |
| 124 | } |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 125 | objServer.remove_interface(sensorInterface); |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 126 | objServer.remove_interface(association); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 129 | void HwmonTempSensor::setupRead() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 130 | { |
Konstantin Aladyshev | a734594 | 2021-04-28 17:09:02 +0300 | [diff] [blame] | 131 | if (!readingStateGood()) |
| 132 | { |
| 133 | markAvailable(false); |
| 134 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 135 | restartRead(); |
| 136 | return; |
| 137 | } |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 138 | |
Konstantin Aladyshev | a734594 | 2021-04-28 17:09:02 +0300 | [diff] [blame] | 139 | std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this(); |
Zev Weiss | d1c8f44 | 2022-07-29 11:54:41 -0700 | [diff] [blame] | 140 | inputDev.async_read_some_at( |
| 141 | 0, boost::asio::buffer(readBuf), |
| 142 | [weakRef](const boost::system::error_code& ec, std::size_t bytesRead) { |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame^] | 143 | std::shared_ptr<HwmonTempSensor> self = weakRef.lock(); |
| 144 | if (self) |
| 145 | { |
| 146 | self->handleResponse(ec, bytesRead); |
| 147 | } |
| 148 | }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Konstantin Aladyshev | a734594 | 2021-04-28 17:09:02 +0300 | [diff] [blame] | 151 | void HwmonTempSensor::restartRead() |
| 152 | { |
| 153 | std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this(); |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 154 | waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs)); |
Konstantin Aladyshev | a734594 | 2021-04-28 17:09:02 +0300 | [diff] [blame] | 155 | waitTimer.async_wait([weakRef](const boost::system::error_code& ec) { |
| 156 | if (ec == boost::asio::error::operation_aborted) |
| 157 | { |
| 158 | return; // we're being canceled |
| 159 | } |
| 160 | std::shared_ptr<HwmonTempSensor> self = weakRef.lock(); |
| 161 | if (!self) |
| 162 | { |
| 163 | return; |
| 164 | } |
| 165 | self->setupRead(); |
| 166 | }); |
| 167 | } |
| 168 | |
Zev Weiss | d1c8f44 | 2022-07-29 11:54:41 -0700 | [diff] [blame] | 169 | void HwmonTempSensor::handleResponse(const boost::system::error_code& err, |
| 170 | size_t bytesRead) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 171 | { |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 172 | if ((err == boost::system::errc::bad_file_descriptor) || |
| 173 | (err == boost::asio::error::misc_errors::not_found)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 174 | { |
Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 175 | std::cerr << "Hwmon temp sensor " << name << " removed " << path |
| 176 | << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 177 | return; // we're being destroyed |
| 178 | } |
Zev Weiss | d1c8f44 | 2022-07-29 11:54:41 -0700 | [diff] [blame] | 179 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 180 | if (!err) |
| 181 | { |
Zev Weiss | d1c8f44 | 2022-07-29 11:54:41 -0700 | [diff] [blame] | 182 | const char* bufEnd = readBuf.data() + bytesRead; |
| 183 | int nvalue = 0; |
Patrick Williams | 2aaf717 | 2024-08-16 15:20:40 -0400 | [diff] [blame^] | 184 | std::from_chars_result ret = |
| 185 | std::from_chars(readBuf.data(), bufEnd, nvalue); |
Zev Weiss | d1c8f44 | 2022-07-29 11:54:41 -0700 | [diff] [blame] | 186 | if (ret.ec != std::errc()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 187 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 188 | incrementError(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 189 | } |
Zev Weiss | d1c8f44 | 2022-07-29 11:54:41 -0700 | [diff] [blame] | 190 | else |
| 191 | { |
| 192 | updateValue((nvalue + offsetValue) * scaleValue); |
| 193 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 194 | } |
| 195 | else |
| 196 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 197 | incrementError(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 198 | } |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 199 | |
Konstantin Aladyshev | a734594 | 2021-04-28 17:09:02 +0300 | [diff] [blame] | 200 | restartRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 203 | void HwmonTempSensor::checkThresholds() |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 204 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 205 | thresholds::checkThresholds(this); |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 206 | } |