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