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 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 34 | static constexpr unsigned int sensorScaleFactor = 1000; |
| 35 | static constexpr size_t warnAfterErrorCount = 10; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 36 | |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 37 | static constexpr double maxReading = 127; |
| 38 | static constexpr double minReading = -128; |
| 39 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 40 | HwmonTempSensor::HwmonTempSensor( |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 41 | const std::string& path, const std::string& objectType, |
| 42 | sdbusplus::asio::object_server& objectServer, |
| 43 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 44 | boost::asio::io_service& io, const std::string& sensorName, |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame^] | 45 | std::vector<thresholds::Threshold>&& thresholds, const float pollRate, |
James Feist | f9b01b6 | 2020-01-29 15:21:58 -0800 | [diff] [blame] | 46 | const std::string& sensorConfiguration, const PowerState powerState) : |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame^] | 47 | Sensor(boost::replace_all_copy(sensorName, " ", "_"), std::move(thresholds), |
| 48 | sensorConfiguration, objectType, maxReading, minReading, conn, |
| 49 | powerState), |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 50 | std::enable_shared_from_this<HwmonTempSensor>(), objServer(objectServer), |
Jeff Lin | 87bc67f | 2020-12-04 20:58:01 +0800 | [diff] [blame] | 51 | inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), path(path), |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame^] | 52 | sensorPollMs(static_cast<unsigned int>(pollRate * 1000)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 53 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 54 | sensorInterface = objectServer.add_interface( |
| 55 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 56 | "xyz.openbmc_project.Sensor.Value"); |
| 57 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 58 | if (thresholds::hasWarningInterface(thresholds)) |
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 | thresholdInterfaceWarning = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 61 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 62 | "xyz.openbmc_project.Sensor.Threshold.Warning"); |
| 63 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 64 | if (thresholds::hasCriticalInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 65 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 66 | thresholdInterfaceCritical = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 67 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 68 | "xyz.openbmc_project.Sensor.Threshold.Critical"); |
| 69 | } |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 70 | association = objectServer.add_interface( |
| 71 | "/xyz/openbmc_project/sensors/temperature/" + name, |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 72 | association::interface); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 73 | setInitialProperties(conn); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | HwmonTempSensor::~HwmonTempSensor() |
| 77 | { |
| 78 | // close the input dev to cancel async operations |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 79 | inputDev.close(); |
| 80 | waitTimer.cancel(); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 81 | objServer.remove_interface(thresholdInterfaceWarning); |
| 82 | objServer.remove_interface(thresholdInterfaceCritical); |
| 83 | objServer.remove_interface(sensorInterface); |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 84 | objServer.remove_interface(association); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 87 | void HwmonTempSensor::setupRead(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 88 | { |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 89 | std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this(); |
| 90 | |
| 91 | boost::asio::async_read_until(inputDev, readBuf, '\n', |
| 92 | [weakRef](const boost::system::error_code& ec, |
| 93 | std::size_t /*bytes_transfered*/) { |
| 94 | std::shared_ptr<HwmonTempSensor> self = |
| 95 | weakRef.lock(); |
| 96 | if (self) |
| 97 | { |
| 98 | self->handleResponse(ec); |
| 99 | } |
| 100 | }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 101 | } |
| 102 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 103 | void HwmonTempSensor::handleResponse(const boost::system::error_code& err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 104 | { |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 105 | if ((err == boost::system::errc::bad_file_descriptor) || |
| 106 | (err == boost::asio::error::misc_errors::not_found)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 107 | { |
Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 108 | std::cerr << "Hwmon temp sensor " << name << " removed " << path |
| 109 | << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 110 | return; // we're being destroyed |
| 111 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 112 | std::istream responseStream(&readBuf); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 113 | if (!err) |
| 114 | { |
| 115 | std::string response; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 116 | std::getline(responseStream, response); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 117 | try |
| 118 | { |
Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 119 | rawValue = std::stod(response); |
| 120 | double nvalue = rawValue / sensorScaleFactor; |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 121 | updateValue(nvalue); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 122 | } |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 123 | catch (const std::invalid_argument&) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 124 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 125 | incrementError(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | else |
| 129 | { |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 130 | incrementError(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 131 | } |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 132 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 133 | responseStream.clear(); |
| 134 | inputDev.close(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 135 | int fd = open(path.c_str(), O_RDONLY); |
Jae Hyun Yoo | ef9665a | 2019-10-10 10:26:39 -0700 | [diff] [blame] | 136 | if (fd < 0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 137 | { |
Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 138 | std::cerr << "Hwmon temp sensor " << name << " not valid " << path |
| 139 | << "\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 140 | return; // we're no longer valid |
| 141 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 142 | inputDev.assign(fd); |
| 143 | waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs)); |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 144 | std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this(); |
| 145 | waitTimer.async_wait([weakRef](const boost::system::error_code& ec) { |
| 146 | std::shared_ptr<HwmonTempSensor> self = weakRef.lock(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 147 | if (ec == boost::asio::error::operation_aborted) |
| 148 | { |
Zhikui Ren | d3da128 | 2020-09-11 17:02:01 -0700 | [diff] [blame] | 149 | if (self) |
| 150 | { |
| 151 | std::cerr << "Hwmon temp sensor " << self->name |
| 152 | << " read cancelled " << self->path << "\n"; |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | std::cerr << "Hwmon sensor read cancelled, no self\n"; |
| 157 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 158 | return; // we're being canceled |
| 159 | } |
Yong Li | f3fd191 | 2020-03-25 21:35:23 +0800 | [diff] [blame] | 160 | if (self) |
| 161 | { |
| 162 | self->setupRead(); |
| 163 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 164 | }); |
| 165 | } |
| 166 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 167 | void HwmonTempSensor::checkThresholds(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 168 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 169 | thresholds::checkThresholds(this); |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 170 | } |