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