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 | |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [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 | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 21 | #include <boost/algorithm/string/predicate.hpp> |
| 22 | #include <boost/algorithm/string/replace.hpp> |
| 23 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 24 | #include <iostream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 25 | #include <istream> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 26 | #include <limits> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 27 | #include <memory> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 28 | #include <sdbusplus/asio/connection.hpp> |
| 29 | #include <sdbusplus/asio/object_server.hpp> |
| 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 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 33 | static constexpr unsigned int sensorPollMs = 500; |
| 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, |
| 45 | std::vector<thresholds::Threshold>&& _thresholds, |
James Feist | f9b01b6 | 2020-01-29 15:21:58 -0800 | [diff] [blame] | 46 | const std::string& sensorConfiguration, const PowerState powerState) : |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 47 | Sensor(boost::replace_all_copy(sensorName, " ", "_"), |
James Feist | ce3fca4 | 2018-11-21 12:58:24 -0800 | [diff] [blame] | 48 | std::move(_thresholds), sensorConfiguration, objectType, maxReading, |
| 49 | minReading), |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 50 | objServer(objectServer), inputDev(io, open(path.c_str(), O_RDONLY)), |
James Feist | f9b01b6 | 2020-01-29 15:21:58 -0800 | [diff] [blame] | 51 | waitTimer(io), path(path), errCount(0), readState(powerState) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 52 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 53 | sensorInterface = objectServer.add_interface( |
| 54 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 55 | "xyz.openbmc_project.Sensor.Value"); |
| 56 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 57 | if (thresholds::hasWarningInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 58 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 59 | thresholdInterfaceWarning = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 60 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 61 | "xyz.openbmc_project.Sensor.Threshold.Warning"); |
| 62 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 63 | if (thresholds::hasCriticalInterface(thresholds)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 64 | { |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 65 | thresholdInterfaceCritical = objectServer.add_interface( |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 66 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 67 | "xyz.openbmc_project.Sensor.Threshold.Critical"); |
| 68 | } |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 69 | association = objectServer.add_interface( |
| 70 | "/xyz/openbmc_project/sensors/temperature/" + name, |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 71 | association::interface); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 72 | setInitialProperties(conn); |
James Feist | f9b01b6 | 2020-01-29 15:21:58 -0800 | [diff] [blame] | 73 | setupPowerMatch(conn); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 74 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | HwmonTempSensor::~HwmonTempSensor() |
| 78 | { |
| 79 | // close the input dev to cancel async operations |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 80 | inputDev.close(); |
| 81 | waitTimer.cancel(); |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 82 | objServer.remove_interface(thresholdInterfaceWarning); |
| 83 | objServer.remove_interface(thresholdInterfaceCritical); |
| 84 | objServer.remove_interface(sensorInterface); |
James Feist | 078f232 | 2019-03-08 11:09:05 -0800 | [diff] [blame] | 85 | objServer.remove_interface(association); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 88 | void HwmonTempSensor::setupRead(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 89 | { |
| 90 | boost::asio::async_read_until( |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 91 | inputDev, readBuf, '\n', |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 92 | [&](const boost::system::error_code& ec, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 93 | std::size_t /*bytes_transfered*/) { handleResponse(ec); }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 94 | } |
| 95 | |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 96 | void HwmonTempSensor::handleResponse(const boost::system::error_code& err) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 97 | { |
| 98 | if (err == boost::system::errc::bad_file_descriptor) |
| 99 | { |
| 100 | return; // we're being destroyed |
| 101 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 102 | std::istream responseStream(&readBuf); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 103 | if (!err) |
| 104 | { |
| 105 | std::string response; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 106 | std::getline(responseStream, response); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 107 | try |
| 108 | { |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 109 | double nvalue = std::stod(response); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 110 | nvalue /= sensorScaleFactor; |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 111 | updateValue(nvalue); |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 112 | errCount = 0; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 113 | } |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 114 | catch (const std::invalid_argument&) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 115 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 116 | errCount++; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
James Feist | 3459d01 | 2020-01-30 13:17:43 -0800 | [diff] [blame] | 119 | else if (readState == PowerState::on && !isPowerOn()) |
| 120 | { |
| 121 | errCount = 0; |
| 122 | updateValue(std::numeric_limits<double>::quiet_NaN()); |
| 123 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 124 | else |
| 125 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 126 | errCount++; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 127 | } |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 128 | |
| 129 | // only print once |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 130 | if (errCount == warnAfterErrorCount) |
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 | std::cerr << "Failure to read sensor " << name << " at " << path |
| 133 | << " ec:" << err << "\n"; |
| 134 | } |
| 135 | |
| 136 | if (errCount >= warnAfterErrorCount) |
| 137 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 138 | updateValue(0); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 139 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 140 | responseStream.clear(); |
| 141 | inputDev.close(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 142 | int fd = open(path.c_str(), O_RDONLY); |
Jae Hyun Yoo | ef9665a | 2019-10-10 10:26:39 -0700 | [diff] [blame] | 143 | if (fd < 0) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 144 | { |
| 145 | return; // we're no longer valid |
| 146 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 147 | inputDev.assign(fd); |
| 148 | waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs)); |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 149 | waitTimer.async_wait([&](const boost::system::error_code& ec) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 150 | if (ec == boost::asio::error::operation_aborted) |
| 151 | { |
| 152 | return; // we're being canceled |
| 153 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 154 | setupRead(); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 155 | }); |
| 156 | } |
| 157 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 158 | void HwmonTempSensor::checkThresholds(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 159 | { |
James Feist | f9b01b6 | 2020-01-29 15:21:58 -0800 | [diff] [blame] | 160 | if (readState == PowerState::on && !isPowerOn()) |
| 161 | { |
| 162 | return; |
| 163 | } |
James Feist | 251c782 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 164 | thresholds::checkThresholds(this); |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 165 | } |