James Feist | 139cb57 | 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 | |
| 29 | static constexpr unsigned int SENSOR_POLL_MS = 500; |
| 30 | static constexpr unsigned int SENSOR_SCALE_FACTOR = 1000; |
| 31 | static constexpr size_t WARN_AFTER_ERROR_COUNT = 10; |
| 32 | |
| 33 | HwmonTempSensor::HwmonTempSensor( |
| 34 | const std::string &path, const std::string &objectType, |
| 35 | sdbusplus::asio::object_server &objectServer, |
| 36 | std::shared_ptr<sdbusplus::asio::connection> &conn, |
| 37 | boost::asio::io_service &io, const std::string &sensor_name, |
| 38 | std::vector<thresholds::Threshold> &&_thresholds, |
| 39 | const std::string &sensorConfiguration) : |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 40 | Sensor(), |
| 41 | path(path), objectType(objectType), configuration(sensorConfiguration), |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 42 | objServer(objectServer), |
| 43 | name(boost::replace_all_copy(sensor_name, " ", "_")), |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 44 | input_dev(io, open(path.c_str(), O_RDONLY)), wait_timer(io), err_count(0), |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 45 | // todo, get these from config |
| 46 | max_value(127), min_value(-128) |
| 47 | { |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 48 | thresholds = std::move(_thresholds); |
| 49 | sensorInterface = objectServer.add_interface( |
| 50 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 51 | "xyz.openbmc_project.Sensor.Value"); |
| 52 | |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 53 | if (thresholds::HasWarningInterface(thresholds)) |
| 54 | { |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 55 | thresholdInterfaceWarning = objectServer.add_interface( |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 56 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 57 | "xyz.openbmc_project.Sensor.Threshold.Warning"); |
| 58 | } |
| 59 | if (thresholds::HasCriticalInterface(thresholds)) |
| 60 | { |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 61 | thresholdInterfaceCritical = objectServer.add_interface( |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 62 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 63 | "xyz.openbmc_project.Sensor.Threshold.Critical"); |
| 64 | } |
| 65 | set_initial_properties(conn); |
| 66 | setup_read(); |
| 67 | } |
| 68 | |
| 69 | HwmonTempSensor::~HwmonTempSensor() |
| 70 | { |
| 71 | // close the input dev to cancel async operations |
| 72 | input_dev.close(); |
| 73 | wait_timer.cancel(); |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 74 | objServer.remove_interface(thresholdInterfaceWarning); |
| 75 | objServer.remove_interface(thresholdInterfaceCritical); |
| 76 | objServer.remove_interface(sensorInterface); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void HwmonTempSensor::setup_read(void) |
| 80 | { |
| 81 | boost::asio::async_read_until( |
| 82 | input_dev, read_buf, '\n', |
| 83 | [&](const boost::system::error_code &ec, |
| 84 | std::size_t /*bytes_transfered*/) { handle_response(ec); }); |
| 85 | } |
| 86 | |
| 87 | void HwmonTempSensor::handle_response(const boost::system::error_code &err) |
| 88 | { |
| 89 | if (err == boost::system::errc::bad_file_descriptor) |
| 90 | { |
| 91 | return; // we're being destroyed |
| 92 | } |
| 93 | std::istream response_stream(&read_buf); |
| 94 | if (!err) |
| 95 | { |
| 96 | std::string response; |
| 97 | std::getline(response_stream, response); |
| 98 | try |
| 99 | { |
| 100 | float nvalue = std::stof(response); |
| 101 | |
| 102 | nvalue /= SENSOR_SCALE_FACTOR; |
| 103 | if (nvalue != value) |
| 104 | { |
| 105 | update_value(nvalue); |
| 106 | } |
| 107 | err_count = 0; |
| 108 | } |
| 109 | catch (const std::invalid_argument &) |
| 110 | { |
| 111 | err_count++; |
| 112 | } |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | std::cerr << "Failure to read sensor " << name << " at " << path |
| 117 | << "\n"; |
| 118 | err_count++; |
| 119 | } |
| 120 | // only send value update once |
| 121 | if (err_count == WARN_AFTER_ERROR_COUNT) |
| 122 | { |
| 123 | update_value(0); |
| 124 | } |
| 125 | response_stream.clear(); |
| 126 | input_dev.close(); |
| 127 | int fd = open(path.c_str(), O_RDONLY); |
| 128 | if (fd <= 0) |
| 129 | { |
| 130 | return; // we're no longer valid |
| 131 | } |
| 132 | input_dev.assign(fd); |
| 133 | wait_timer.expires_from_now( |
| 134 | boost::posix_time::milliseconds(SENSOR_POLL_MS)); |
| 135 | ; |
| 136 | wait_timer.async_wait([&](const boost::system::error_code &ec) { |
| 137 | if (ec == boost::asio::error::operation_aborted) |
| 138 | { |
| 139 | return; // we're being canceled |
| 140 | } |
| 141 | setup_read(); |
| 142 | }); |
| 143 | } |
| 144 | |
| 145 | void HwmonTempSensor::check_thresholds(void) |
| 146 | { |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 147 | thresholds::checkThresholds(this); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void HwmonTempSensor::update_value(const double &new_value) |
| 151 | { |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 152 | sensorInterface->set_property("Value", new_value); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 153 | value = new_value; |
| 154 | check_thresholds(); |
| 155 | } |
| 156 | |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 157 | void HwmonTempSensor::set_initial_properties( |
| 158 | std::shared_ptr<sdbusplus::asio::connection> &conn) |
| 159 | { |
| 160 | // todo, get max and min from configuration |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 161 | sensorInterface->register_property("MaxValue", max_value); |
| 162 | sensorInterface->register_property("MinValue", min_value); |
| 163 | sensorInterface->register_property("Value", value); |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 164 | |
| 165 | for (auto &threshold : thresholds) |
| 166 | { |
| 167 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface; |
| 168 | std::string level; |
| 169 | std::string alarm; |
| 170 | if (threshold.level == thresholds::Level::CRITICAL) |
| 171 | { |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 172 | iface = thresholdInterfaceCritical; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 173 | if (threshold.direction == thresholds::Direction::HIGH) |
| 174 | { |
| 175 | level = "CriticalHigh"; |
| 176 | alarm = "CriticalAlarmHigh"; |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | level = "CriticalLow"; |
| 181 | alarm = "CriticalAlarmLow"; |
| 182 | } |
| 183 | } |
| 184 | else if (threshold.level == thresholds::Level::WARNING) |
| 185 | { |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 186 | iface = thresholdInterfaceWarning; |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 187 | if (threshold.direction == thresholds::Direction::HIGH) |
| 188 | { |
| 189 | level = "WarningHigh"; |
| 190 | alarm = "WarningAlarmHigh"; |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | level = "WarningLow"; |
| 195 | alarm = "WarningAlarmLow"; |
| 196 | } |
| 197 | } |
| 198 | else |
| 199 | { |
| 200 | std::cerr << "Unknown threshold level" << threshold.level << "\n"; |
| 201 | continue; |
| 202 | } |
| 203 | if (!iface) |
| 204 | { |
| 205 | std::cout << "trying to set uninitialized interface\n"; |
| 206 | continue; |
| 207 | } |
| 208 | iface->register_property( |
| 209 | level, threshold.value, |
| 210 | [&](const double &request, double &oldValue) { |
| 211 | oldValue = request; // todo, just let the config do this? |
| 212 | threshold.value = request; |
| 213 | thresholds::persistThreshold(configuration, objectType, |
| 214 | threshold, conn); |
| 215 | return 1; |
| 216 | }); |
| 217 | iface->register_property(alarm, false); |
| 218 | } |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 219 | if (!sensorInterface->initialize()) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 220 | { |
| 221 | std::cerr << "error initializing value interface\n"; |
| 222 | } |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 223 | if (thresholdInterfaceWarning && !thresholdInterfaceWarning->initialize()) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 224 | { |
| 225 | std::cerr << "error initializing warning threshold interface\n"; |
| 226 | } |
| 227 | |
James Feist | af79dd3 | 2018-09-12 12:54:15 -0700 | [diff] [blame] | 228 | if (thresholdInterfaceCritical && !thresholdInterfaceCritical->initialize()) |
James Feist | 139cb57 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 229 | { |
| 230 | std::cerr << "error initializing critical threshold interface\n"; |
| 231 | } |
| 232 | } |