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