Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2019 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 <PSUSensor.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 | |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 29 | static constexpr const char* sensorPathPrefix = "/xyz/openbmc_project/sensors/"; |
| 30 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 31 | PSUSensor::PSUSensor(const std::string& path, const std::string& objectType, |
| 32 | sdbusplus::asio::object_server& objectServer, |
| 33 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 34 | boost::asio::io_service& io, const std::string& sensorName, |
| 35 | std::vector<thresholds::Threshold>&& _thresholds, |
| 36 | const std::string& sensorConfiguration, |
| 37 | std::string& sensorTypeName, unsigned int factor, |
| 38 | double max, double min) : |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 39 | Sensor(boost::replace_all_copy(sensorName, " ", "_"), |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 40 | std::move(_thresholds), sensorConfiguration, objectType, max, min), |
James Feist | 930fcde | 2019-05-28 12:58:43 -0700 | [diff] [blame] | 41 | path(path), objServer(objectServer), |
| 42 | inputDev(io, open(path.c_str(), O_RDONLY)), waitTimer(io), errCount(0), |
| 43 | sensorFactor(factor) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 44 | { |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 45 | std::string dbusPath = sensorPathPrefix + sensorTypeName + name; |
| 46 | |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 47 | sensorInterface = objectServer.add_interface( |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 48 | dbusPath, "xyz.openbmc_project.Sensor.Value"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 49 | |
| 50 | if (thresholds::hasWarningInterface(thresholds)) |
| 51 | { |
| 52 | thresholdInterfaceWarning = objectServer.add_interface( |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 53 | dbusPath, "xyz.openbmc_project.Sensor.Threshold.Warning"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 54 | } |
| 55 | if (thresholds::hasCriticalInterface(thresholds)) |
| 56 | { |
| 57 | thresholdInterfaceCritical = objectServer.add_interface( |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 58 | dbusPath, "xyz.openbmc_project.Sensor.Threshold.Critical"); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 59 | } |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame^] | 60 | setInitialProperties(conn); |
| 61 | |
James Feist | 690895f | 2019-04-23 13:01:08 -0700 | [diff] [blame] | 62 | association = |
| 63 | objectServer.add_interface(dbusPath, "org.openbmc.Associations"); |
| 64 | |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame^] | 65 | createInventoryAssoc(conn, association, configurationPath); |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 66 | setupRead(); |
| 67 | } |
| 68 | |
| 69 | PSUSensor::~PSUSensor() |
| 70 | { |
| 71 | inputDev.close(); |
| 72 | waitTimer.cancel(); |
| 73 | objServer.remove_interface(sensorInterface); |
| 74 | objServer.remove_interface(thresholdInterfaceWarning); |
| 75 | objServer.remove_interface(thresholdInterfaceCritical); |
| 76 | } |
| 77 | |
| 78 | void PSUSensor::setupRead(void) |
| 79 | { |
| 80 | boost::asio::async_read_until( |
| 81 | inputDev, readBuf, '\n', |
| 82 | [&](const boost::system::error_code& ec, |
| 83 | std::size_t /*bytes_transfered*/) { handleResponse(ec); }); |
| 84 | } |
| 85 | |
| 86 | void PSUSensor::handleResponse(const boost::system::error_code& err) |
| 87 | { |
| 88 | if (err == boost::system::errc::bad_file_descriptor) |
| 89 | { |
| 90 | return; |
| 91 | } |
| 92 | std::istream responseStream(&readBuf); |
| 93 | if (!err) |
| 94 | { |
| 95 | std::string response; |
| 96 | try |
| 97 | { |
| 98 | std::getline(responseStream, response); |
| 99 | float nvalue = std::stof(response); |
| 100 | responseStream.clear(); |
| 101 | nvalue /= sensorFactor; |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 102 | if (static_cast<double>(nvalue) != value) |
Cheng C Yang | 209ec56 | 2019-03-12 16:37:44 +0800 | [diff] [blame] | 103 | { |
| 104 | updateValue(nvalue); |
| 105 | } |
| 106 | errCount = 0; |
| 107 | } |
| 108 | catch (const std::invalid_argument&) |
| 109 | { |
| 110 | errCount++; |
| 111 | } |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | errCount++; |
| 116 | } |
| 117 | |
| 118 | if (errCount >= warnAfterErrorCount) |
| 119 | { |
| 120 | if (errCount == warnAfterErrorCount) |
| 121 | { |
| 122 | std::cerr << "Failure to read sensor " << name << " at " << path |
| 123 | << "\n"; |
| 124 | } |
| 125 | updateValue(0); |
| 126 | errCount++; |
| 127 | } |
| 128 | |
| 129 | responseStream.clear(); |
| 130 | inputDev.close(); |
| 131 | int fd = open(path.c_str(), O_RDONLY); |
| 132 | if (fd <= 0) |
| 133 | { |
| 134 | return; |
| 135 | } |
| 136 | inputDev.assign(fd); |
| 137 | waitTimer.expires_from_now(boost::posix_time::milliseconds(sensorPollMs)); |
| 138 | waitTimer.async_wait([&](const boost::system::error_code& ec) { |
| 139 | if (ec == boost::asio::error::operation_aborted) |
| 140 | { |
| 141 | return; |
| 142 | } |
| 143 | setupRead(); |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | void PSUSensor::checkThresholds(void) |
| 148 | { |
| 149 | thresholds::checkThresholds(this); |
| 150 | } |