Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "sensor.hpp" |
| 4 | |
| 5 | #include "env.hpp" |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 6 | #include "gpio_handle.hpp" |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 7 | #include "hwmon.hpp" |
| 8 | #include "sensorset.hpp" |
| 9 | #include "sysfs.hpp" |
| 10 | |
Patrick Williams | e8771fd | 2023-05-10 07:51:06 -0500 | [diff] [blame] | 11 | #include <phosphor-logging/elog-errors.hpp> |
| 12 | #include <xyz/openbmc_project/Common/error.hpp> |
| 13 | #include <xyz/openbmc_project/Sensor/Device/error.hpp> |
| 14 | |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 15 | #include <cassert> |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 16 | #include <chrono> |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 17 | #include <cmath> |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 18 | #include <cstring> |
Patrick Venture | 9e997b4 | 2019-03-08 13:42:10 -0800 | [diff] [blame] | 19 | #include <filesystem> |
Patrick Williams | 6412993 | 2024-02-13 21:10:17 -0600 | [diff] [blame] | 20 | #include <format> |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame] | 21 | #include <future> |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 22 | #include <thread> |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 23 | |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 24 | namespace sensor |
| 25 | { |
| 26 | |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 27 | using namespace phosphor::logging; |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 28 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 29 | |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 30 | // todo: this can be simplified once we move to the double interface |
Matthew Barth | 2e41b13 | 2018-05-07 14:15:45 -0500 | [diff] [blame] | 31 | Sensor::Sensor(const SensorSet::key_type& sensor, |
Patrick Venture | 2864b06 | 2018-12-19 08:13:41 -0800 | [diff] [blame] | 32 | const hwmonio::HwmonIOInterface* ioAccess, |
| 33 | const std::string& devPath) : |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 34 | _sensor(sensor), _ioAccess(ioAccess), _devPath(devPath), _scale(0), |
| 35 | _hasFaultFile(false) |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 36 | { |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 37 | auto chip = env::getEnv("GPIOCHIP", sensor); |
| 38 | auto access = env::getEnv("GPIO", sensor); |
| 39 | if (!access.empty() && !chip.empty()) |
| 40 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 41 | _handle = gpio::BuildGpioHandle(chip, access); |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 42 | |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 43 | if (!_handle) |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 44 | { |
| 45 | log<level::ERR>("Unable to set up gpio locking"); |
| 46 | elog<InternalFailure>(); |
| 47 | } |
| 48 | } |
| 49 | |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 50 | auto gain = env::getEnv("GAIN", sensor); |
| 51 | if (!gain.empty()) |
| 52 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 53 | _sensorAdjusts.gain = std::stod(gain); |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | auto offset = env::getEnv("OFFSET", sensor); |
| 57 | if (!offset.empty()) |
| 58 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 59 | _sensorAdjusts.offset = std::stoi(offset); |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 60 | } |
| 61 | auto senRmRCs = env::getEnv("REMOVERCS", sensor); |
| 62 | // Add sensor removal return codes defined per sensor |
| 63 | addRemoveRCs(senRmRCs); |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 64 | } |
| 65 | |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 66 | void Sensor::addRemoveRCs(const std::string& rcList) |
| 67 | { |
| 68 | if (rcList.empty()) |
| 69 | { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | // Convert to a char* for strtok |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 74 | std::vector<char> rmRCs(rcList.c_str(), rcList.c_str() + rcList.size() + 1); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 75 | auto rmRC = std::strtok(&rmRCs[0], ", "); |
| 76 | while (rmRC != nullptr) |
| 77 | { |
| 78 | try |
| 79 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 80 | _sensorAdjusts.rmRCs.insert(std::stoi(rmRC)); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 81 | } |
| 82 | catch (const std::logic_error& le) |
| 83 | { |
| 84 | // Unable to convert to int, continue to next token |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 85 | std::string name = _sensor.first + "_" + _sensor.second; |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 86 | log<level::INFO>("Unable to convert sensor removal return code", |
| 87 | entry("SENSOR=%s", name.c_str()), |
| 88 | entry("RC=%s", rmRC), |
| 89 | entry("EXCEPTION=%s", le.what())); |
| 90 | } |
| 91 | rmRC = std::strtok(nullptr, ", "); |
| 92 | } |
| 93 | } |
| 94 | |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 95 | SensorValueType Sensor::adjustValue(SensorValueType value) |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 96 | { |
| 97 | // Because read doesn't have an out pointer to store errors. |
| 98 | // let's assume negative values are errors if they have this |
| 99 | // set. |
Matt Spinler | d8cacfd | 2021-04-26 09:56:21 -0500 | [diff] [blame] | 100 | #if NEGATIVE_ERRNO_ON_FAIL |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 101 | if (value < 0) |
| 102 | { |
| 103 | return value; |
| 104 | } |
| 105 | #endif |
| 106 | |
| 107 | // Adjust based on gain and offset |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 108 | value = static_cast<decltype(value)>( |
| 109 | static_cast<double>(value) * _sensorAdjusts.gain + |
| 110 | _sensorAdjusts.offset); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 111 | |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 112 | if constexpr (std::is_same<SensorValueType, double>::value) |
| 113 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 114 | value *= std::pow(10, _scale); |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 117 | return value; |
| 118 | } |
| 119 | |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 120 | std::shared_ptr<ValueObject> Sensor::addValue( |
| 121 | const RetryIO& retryIO, ObjectInfo& info, TimedoutMap& timedoutMap) |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 122 | { |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 123 | // Get the initial value for the value interface. |
Patrick Williams | ad6043f | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 124 | auto& bus = *std::get<sdbusplus::bus_t*>(info); |
Patrick Venture | 6206723 | 2019-06-19 17:39:33 -0700 | [diff] [blame] | 125 | auto& obj = std::get<InterfaceMap>(info); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 126 | auto& objPath = std::get<std::string>(info); |
| 127 | |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 128 | SensorValueType val = 0; |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 129 | |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 130 | auto& statusIface = std::any_cast<std::shared_ptr<StatusObject>&>( |
| 131 | obj[InterfaceType::STATUS]); |
| 132 | // As long as addStatus is called before addValue, statusIface |
| 133 | // should never be nullptr |
| 134 | assert(statusIface); |
| 135 | |
| 136 | // Only read the input value if the status is functional |
| 137 | if (statusIface->functional()) |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 138 | { |
Matt Spinler | d8cacfd | 2021-04-26 09:56:21 -0500 | [diff] [blame] | 139 | #if UPDATE_FUNCTIONAL_ON_FAIL |
Brandon Kim | 79205b2 | 2019-06-20 12:18:24 -0700 | [diff] [blame] | 140 | try |
| 141 | #endif |
| 142 | { |
| 143 | // RAII object for GPIO unlock / lock |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 144 | auto locker = gpioUnlock(getGpio()); |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 145 | |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame] | 146 | // For sensors with attribute ASYNC_READ_TIMEOUT, |
| 147 | // spawn a thread with timeout |
| 148 | auto asyncReadTimeout = env::getEnv("ASYNC_READ_TIMEOUT", _sensor); |
| 149 | if (!asyncReadTimeout.empty()) |
| 150 | { |
| 151 | std::chrono::milliseconds asyncTimeout{ |
| 152 | std::stoi(asyncReadTimeout)}; |
| 153 | val = asyncRead(_sensor, _ioAccess, asyncTimeout, timedoutMap, |
| 154 | _sensor.first, _sensor.second, |
Brandon Kim | 79205b2 | 2019-06-20 12:18:24 -0700 | [diff] [blame] | 155 | hwmon::entry::cinput, std::get<size_t>(retryIO), |
| 156 | std::get<std::chrono::milliseconds>(retryIO)); |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame] | 157 | } |
| 158 | else |
| 159 | { |
| 160 | // Retry for up to a second if device is busy |
| 161 | // or has a transient error. |
| 162 | val = _ioAccess->read( |
| 163 | _sensor.first, _sensor.second, hwmon::entry::cinput, |
| 164 | std::get<size_t>(retryIO), |
| 165 | std::get<std::chrono::milliseconds>(retryIO)); |
| 166 | } |
Brandon Kim | 79205b2 | 2019-06-20 12:18:24 -0700 | [diff] [blame] | 167 | } |
Matt Spinler | d8cacfd | 2021-04-26 09:56:21 -0500 | [diff] [blame] | 168 | #if UPDATE_FUNCTIONAL_ON_FAIL |
Brandon Kim | 79205b2 | 2019-06-20 12:18:24 -0700 | [diff] [blame] | 169 | catch (const std::system_error& e) |
| 170 | { |
| 171 | // Catch the exception here and update the functional property. |
| 172 | // By catching the exception, it will not propagate it up the stack |
| 173 | // and thus the code will skip the "Remove RCs" check in |
| 174 | // MainLoop::getObject and will not exit on failure. |
| 175 | statusIface->functional(false); |
| 176 | } |
| 177 | #endif |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 178 | } |
| 179 | |
Patrick Williams | d273b1e | 2022-03-30 21:55:44 -0500 | [diff] [blame] | 180 | auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), |
| 181 | ValueObject::action::defer_emit); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 182 | |
| 183 | hwmon::Attributes attrs; |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 184 | if (hwmon::getAttributes(_sensor.first, attrs)) |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 185 | { |
| 186 | iface->unit(hwmon::getUnit(attrs)); |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 187 | |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 188 | _scale = hwmon::getScale(attrs); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Matt Spinler | 7ab1b25 | 2020-07-22 15:11:02 -0500 | [diff] [blame] | 191 | val = adjustValue(val); |
| 192 | iface->value(val); |
| 193 | |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 194 | auto maxValue = env::getEnv("MAXVALUE", _sensor); |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 195 | if (!maxValue.empty()) |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 196 | { |
| 197 | iface->maxValue(std::stoll(maxValue)); |
| 198 | } |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 199 | auto minValue = env::getEnv("MINVALUE", _sensor); |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 200 | if (!minValue.empty()) |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 201 | { |
| 202 | iface->minValue(std::stoll(minValue)); |
| 203 | } |
| 204 | |
| 205 | obj[InterfaceType::VALUE] = iface; |
| 206 | return iface; |
| 207 | } |
| 208 | |
Matthew Barth | 2e41b13 | 2018-05-07 14:15:45 -0500 | [diff] [blame] | 209 | std::shared_ptr<StatusObject> Sensor::addStatus(ObjectInfo& info) |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 210 | { |
Patrick Venture | 9e997b4 | 2019-03-08 13:42:10 -0800 | [diff] [blame] | 211 | namespace fs = std::filesystem; |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 212 | |
| 213 | std::shared_ptr<StatusObject> iface = nullptr; |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 214 | auto& objPath = std::get<std::string>(info); |
Patrick Venture | 6206723 | 2019-06-19 17:39:33 -0700 | [diff] [blame] | 215 | auto& obj = std::get<InterfaceMap>(info); |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 216 | |
| 217 | // Check if fault sysfs file exists |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 218 | std::string faultName = _sensor.first; |
| 219 | std::string faultID = _sensor.second; |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 220 | std::string entry = hwmon::entry::fault; |
| 221 | |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 222 | bool functional = true; |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 223 | auto sysfsFullPath = |
| 224 | sysfs::make_sysfs_path(_ioAccess->path(), faultName, faultID, entry); |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 225 | if (fs::exists(sysfsFullPath)) |
| 226 | { |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 227 | _hasFaultFile = true; |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 228 | try |
| 229 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 230 | uint32_t fault = _ioAccess->read(faultName, faultID, entry, |
| 231 | hwmonio::retries, hwmonio::delay); |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 232 | if (fault != 0) |
| 233 | { |
| 234 | functional = false; |
| 235 | } |
| 236 | } |
| 237 | catch (const std::system_error& e) |
| 238 | { |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 239 | using namespace sdbusplus::xyz::openbmc_project::Sensor::Device:: |
| 240 | Error; |
| 241 | using metadata = xyz::openbmc_project::Sensor::Device::ReadFailure; |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 242 | |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 243 | report<ReadFailure>( |
| 244 | metadata::CALLOUT_ERRNO(e.code().value()), |
| 245 | metadata::CALLOUT_DEVICE_PATH(_devPath.c_str())); |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 246 | |
Patrick Williams | 6412993 | 2024-02-13 21:10:17 -0600 | [diff] [blame] | 247 | log<level::INFO>(std::format("Failing sysfs file: {} errno {}", |
Matt Spinler | 6a391de | 2020-07-08 13:03:10 -0500 | [diff] [blame] | 248 | sysfsFullPath, e.code().value()) |
| 249 | .c_str()); |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 250 | } |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 251 | } |
| 252 | |
Patrick Williams | ad6043f | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 253 | auto& bus = *std::get<sdbusplus::bus_t*>(info); |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 254 | |
Patrick Williams | d273b1e | 2022-03-30 21:55:44 -0500 | [diff] [blame] | 255 | iface = std::make_shared<StatusObject>( |
| 256 | bus, objPath.c_str(), StatusObject::action::emit_no_signals); |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 257 | // Set functional property |
| 258 | iface->functional(functional); |
| 259 | |
| 260 | obj[InterfaceType::STATUS] = iface; |
| 261 | |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 262 | return iface; |
| 263 | } |
| 264 | |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 265 | std::shared_ptr<AccuracyObject> |
| 266 | Sensor::addAccuracy(ObjectInfo& info, double accuracy) |
George Liu | c9d6161 | 2022-10-12 14:31:39 +0800 | [diff] [blame] | 267 | { |
| 268 | auto& objPath = std::get<std::string>(info); |
| 269 | auto& obj = std::get<InterfaceMap>(info); |
| 270 | |
| 271 | auto& bus = *std::get<sdbusplus::bus_t*>(info); |
| 272 | auto iface = std::make_shared<AccuracyObject>( |
| 273 | bus, objPath.c_str(), AccuracyObject::action::emit_no_signals); |
| 274 | |
| 275 | iface->accuracy(accuracy); |
| 276 | obj[InterfaceType::ACCURACY] = iface; |
| 277 | |
| 278 | return iface; |
| 279 | } |
| 280 | |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 281 | std::shared_ptr<PriorityObject> |
| 282 | Sensor::addPriority(ObjectInfo& info, size_t priority) |
Lakshmi Yadlapati | 47fb49a | 2023-10-19 14:47:08 -0500 | [diff] [blame] | 283 | { |
| 284 | auto& objPath = std::get<std::string>(info); |
| 285 | auto& obj = std::get<InterfaceMap>(info); |
| 286 | |
| 287 | auto& bus = *std::get<sdbusplus::bus_t*>(info); |
| 288 | auto iface = std::make_shared<PriorityObject>( |
| 289 | bus, objPath.c_str(), PriorityObject::action::emit_no_signals); |
| 290 | |
| 291 | iface->priority(priority); |
| 292 | obj[InterfaceType::PRIORITY] = iface; |
| 293 | |
| 294 | return iface; |
| 295 | } |
| 296 | |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 297 | void gpioLock(const gpioplus::HandleInterface*&& handle) |
Brandon Kim | db76d49 | 2019-06-17 11:53:04 -0700 | [diff] [blame] | 298 | { |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 299 | handle->setValues({0}); |
Brandon Kim | db76d49 | 2019-06-17 11:53:04 -0700 | [diff] [blame] | 300 | } |
| 301 | |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 302 | std::optional<GpioLocker> gpioUnlock(const gpioplus::HandleInterface* handle) |
Brandon Kim | db76d49 | 2019-06-17 11:53:04 -0700 | [diff] [blame] | 303 | { |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 304 | if (handle == nullptr) |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 305 | { |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 306 | return std::nullopt; |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 307 | } |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 308 | |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 309 | handle->setValues({1}); |
| 310 | // Default pause needed to guarantee sensors are ready |
| 311 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 312 | return GpioLocker(std::move(handle)); |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 315 | SensorValueType asyncRead( |
| 316 | const SensorSet::key_type& sensorSetKey, |
| 317 | const hwmonio::HwmonIOInterface* ioAccess, |
| 318 | std::chrono::milliseconds asyncTimeout, TimedoutMap& timedoutMap, |
| 319 | const std::string& type, const std::string& id, const std::string& sensor, |
| 320 | const size_t retries, const std::chrono::milliseconds delay) |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame] | 321 | { |
| 322 | // Default async read timeout |
| 323 | bool valueIsValid = false; |
| 324 | std::future<int64_t> asyncThread; |
| 325 | |
| 326 | auto asyncIter = timedoutMap.find(sensorSetKey); |
| 327 | if (asyncIter == timedoutMap.end()) |
| 328 | { |
| 329 | // If sensor not found in timedoutMap, spawn an async thread |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 330 | asyncThread = |
| 331 | std::async(std::launch::async, &hwmonio::HwmonIOInterface::read, |
| 332 | ioAccess, type, id, sensor, retries, delay); |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame] | 333 | valueIsValid = true; |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | // If we already have the async thread in the timedoutMap, it means this |
| 338 | // sensor has already timed out in the previous reads. No need to wait |
| 339 | // on subsequent reads - proceed to check the future_status to see when |
| 340 | // the async thread finishes |
| 341 | asyncTimeout = std::chrono::seconds(0); |
| 342 | asyncThread = std::move(asyncIter->second); |
| 343 | } |
| 344 | |
| 345 | // TODO: This is still not a true asynchronous read as it still blocks the |
| 346 | // main thread for asyncTimeout amount of time. To make this completely |
| 347 | // asynchronous, schedule a read and register a callback to update the |
| 348 | // sensor value |
| 349 | std::future_status status = asyncThread.wait_for(asyncTimeout); |
| 350 | switch (status) |
| 351 | { |
| 352 | case std::future_status::ready: |
| 353 | // Read has finished |
| 354 | if (valueIsValid) |
| 355 | { |
| 356 | return asyncThread.get(); |
| 357 | // Good sensor reads should skip the code below |
| 358 | } |
| 359 | // Async read thread has completed but had previously timed out (was |
| 360 | // found in the timedoutMap). Erase from timedoutMap and throw to |
| 361 | // allow retry in the next read cycle. Not returning the read value |
| 362 | // as the sensor reading may be bad / corrupted if it took so long. |
| 363 | timedoutMap.erase(sensorSetKey); |
| 364 | throw AsyncSensorReadTimeOut(); |
| 365 | default: |
| 366 | // Read timed out so add the thread to the timedoutMap (if the entry |
| 367 | // already exists, operator[] updates it). |
| 368 | // |
| 369 | // Keeping the timed out futures in a map is required to prevent |
| 370 | // their destructor from being called when returning from this |
| 371 | // stack. The destructor will otherwise block until the read |
| 372 | // completes due to the limitation of std::async. |
| 373 | timedoutMap[sensorSetKey] = std::move(asyncThread); |
| 374 | throw AsyncSensorReadTimeOut(); |
| 375 | } |
| 376 | } |
| 377 | |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 378 | } // namespace sensor |