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 | |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 11 | #include <cmath> |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 12 | #include <cstring> |
Patrick Venture | 9e997b4 | 2019-03-08 13:42:10 -0800 | [diff] [blame^] | 13 | #include <filesystem> |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 14 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 15 | #include <thread> |
| 16 | #include <xyz/openbmc_project/Common/error.hpp> |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 17 | #include <xyz/openbmc_project/Sensor/Device/error.hpp> |
| 18 | |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 19 | namespace sensor |
| 20 | { |
| 21 | |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 22 | using namespace phosphor::logging; |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 23 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 24 | |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 25 | // todo: this can be deleted once we move to double |
| 26 | // helper class to set the scale on the value iface only when it's available |
| 27 | template <typename T> |
| 28 | void setScale(T& iface, int64_t value, double) |
| 29 | { |
| 30 | } |
| 31 | template <typename T> |
| 32 | void setScale(T& iface, int64_t value, int64_t) |
| 33 | { |
| 34 | iface->scale(value); |
| 35 | } |
| 36 | |
| 37 | // todo: this can be simplified once we move to the double interface |
Matthew Barth | 2e41b13 | 2018-05-07 14:15:45 -0500 | [diff] [blame] | 38 | Sensor::Sensor(const SensorSet::key_type& sensor, |
Patrick Venture | 2864b06 | 2018-12-19 08:13:41 -0800 | [diff] [blame] | 39 | const hwmonio::HwmonIOInterface* ioAccess, |
| 40 | const std::string& devPath) : |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 41 | _sensor(sensor), |
Patrick Venture | e9764ff | 2019-01-24 14:11:45 -0800 | [diff] [blame] | 42 | _ioAccess(ioAccess), _devPath(devPath), _scale(0) |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 43 | { |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 44 | auto chip = env::getEnv("GPIOCHIP", sensor); |
| 45 | auto access = env::getEnv("GPIO", sensor); |
| 46 | if (!access.empty() && !chip.empty()) |
| 47 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 48 | _handle = gpio::BuildGpioHandle(chip, access); |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 49 | |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 50 | if (!_handle) |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 51 | { |
| 52 | log<level::ERR>("Unable to set up gpio locking"); |
| 53 | elog<InternalFailure>(); |
| 54 | } |
| 55 | } |
| 56 | |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 57 | auto gain = env::getEnv("GAIN", sensor); |
| 58 | if (!gain.empty()) |
| 59 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 60 | _sensorAdjusts.gain = std::stod(gain); |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | auto offset = env::getEnv("OFFSET", sensor); |
| 64 | if (!offset.empty()) |
| 65 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 66 | _sensorAdjusts.offset = std::stoi(offset); |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 67 | } |
| 68 | auto senRmRCs = env::getEnv("REMOVERCS", sensor); |
| 69 | // Add sensor removal return codes defined per sensor |
| 70 | addRemoveRCs(senRmRCs); |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 71 | } |
| 72 | |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 73 | void Sensor::addRemoveRCs(const std::string& rcList) |
| 74 | { |
| 75 | if (rcList.empty()) |
| 76 | { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | // Convert to a char* for strtok |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 81 | 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] | 82 | auto rmRC = std::strtok(&rmRCs[0], ", "); |
| 83 | while (rmRC != nullptr) |
| 84 | { |
| 85 | try |
| 86 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 87 | _sensorAdjusts.rmRCs.insert(std::stoi(rmRC)); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 88 | } |
| 89 | catch (const std::logic_error& le) |
| 90 | { |
| 91 | // Unable to convert to int, continue to next token |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 92 | std::string name = _sensor.first + "_" + _sensor.second; |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 93 | log<level::INFO>("Unable to convert sensor removal return code", |
| 94 | entry("SENSOR=%s", name.c_str()), |
| 95 | entry("RC=%s", rmRC), |
| 96 | entry("EXCEPTION=%s", le.what())); |
| 97 | } |
| 98 | rmRC = std::strtok(nullptr, ", "); |
| 99 | } |
| 100 | } |
| 101 | |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 102 | SensorValueType Sensor::adjustValue(SensorValueType value) |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 103 | { |
| 104 | // Because read doesn't have an out pointer to store errors. |
| 105 | // let's assume negative values are errors if they have this |
| 106 | // set. |
| 107 | #ifdef NEGATIVE_ERRNO_ON_FAIL |
| 108 | if (value < 0) |
| 109 | { |
| 110 | return value; |
| 111 | } |
| 112 | #endif |
| 113 | |
| 114 | // Adjust based on gain and offset |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 115 | value = static_cast<decltype(value)>(static_cast<double>(value) * |
| 116 | _sensorAdjusts.gain + |
| 117 | _sensorAdjusts.offset); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 118 | |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 119 | if constexpr (std::is_same<SensorValueType, double>::value) |
| 120 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 121 | value *= std::pow(10, _scale); |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 124 | return value; |
| 125 | } |
| 126 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 127 | std::shared_ptr<ValueObject> Sensor::addValue(const RetryIO& retryIO, |
| 128 | ObjectInfo& info) |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 129 | { |
| 130 | static constexpr bool deferSignals = true; |
| 131 | |
| 132 | // Get the initial value for the value interface. |
| 133 | auto& bus = *std::get<sdbusplus::bus::bus*>(info); |
| 134 | auto& obj = std::get<Object>(info); |
| 135 | auto& objPath = std::get<std::string>(info); |
| 136 | |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 137 | SensorValueType val = 0; |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 138 | std::shared_ptr<StatusObject> statusIface = nullptr; |
| 139 | auto it = obj.find(InterfaceType::STATUS); |
| 140 | if (it != obj.end()) |
| 141 | { |
William A. Kennington III | 4cbdfef | 2018-10-18 19:19:51 -0700 | [diff] [blame] | 142 | statusIface = std::any_cast<std::shared_ptr<StatusObject>>(it->second); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // If there's no fault file or the sensor has a fault file and |
| 146 | // its status is functional, read the input value. |
| 147 | if (!statusIface || (statusIface && statusIface->functional())) |
| 148 | { |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 149 | unlockGpio(); |
| 150 | |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 151 | // Retry for up to a second if device is busy |
| 152 | // or has a transient error. |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 153 | val = _ioAccess->read(_sensor.first, _sensor.second, |
| 154 | hwmon::entry::cinput, std::get<size_t>(retryIO), |
| 155 | std::get<std::chrono::milliseconds>(retryIO)); |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 156 | |
| 157 | lockGpio(); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 158 | val = adjustValue(val); |
| 159 | } |
| 160 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 161 | auto iface = |
| 162 | std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 163 | iface->value(val); |
| 164 | |
| 165 | hwmon::Attributes attrs; |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 166 | if (hwmon::getAttributes(_sensor.first, attrs)) |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 167 | { |
| 168 | iface->unit(hwmon::getUnit(attrs)); |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 169 | |
| 170 | setScale(iface, hwmon::getScale(attrs), val); |
| 171 | |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 172 | _scale = hwmon::getScale(attrs); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 173 | } |
| 174 | |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 175 | auto maxValue = env::getEnv("MAXVALUE", _sensor); |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 176 | if (!maxValue.empty()) |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 177 | { |
| 178 | iface->maxValue(std::stoll(maxValue)); |
| 179 | } |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 180 | auto minValue = env::getEnv("MINVALUE", _sensor); |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 181 | if (!minValue.empty()) |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 182 | { |
| 183 | iface->minValue(std::stoll(minValue)); |
| 184 | } |
| 185 | |
| 186 | obj[InterfaceType::VALUE] = iface; |
| 187 | return iface; |
| 188 | } |
| 189 | |
Matthew Barth | 2e41b13 | 2018-05-07 14:15:45 -0500 | [diff] [blame] | 190 | std::shared_ptr<StatusObject> Sensor::addStatus(ObjectInfo& info) |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 191 | { |
Patrick Venture | 9e997b4 | 2019-03-08 13:42:10 -0800 | [diff] [blame^] | 192 | namespace fs = std::filesystem; |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 193 | |
| 194 | std::shared_ptr<StatusObject> iface = nullptr; |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 195 | auto& objPath = std::get<std::string>(info); |
| 196 | auto& obj = std::get<Object>(info); |
| 197 | |
| 198 | // Check if fault sysfs file exists |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 199 | std::string faultName = _sensor.first; |
| 200 | std::string faultID = _sensor.second; |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 201 | std::string entry = hwmon::entry::fault; |
| 202 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 203 | auto sysfsFullPath = |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 204 | sysfs::make_sysfs_path(_ioAccess->path(), faultName, faultID, entry); |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 205 | if (fs::exists(sysfsFullPath)) |
| 206 | { |
| 207 | bool functional = true; |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 208 | try |
| 209 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 210 | uint32_t fault = _ioAccess->read(faultName, faultID, entry, |
| 211 | hwmonio::retries, hwmonio::delay); |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 212 | if (fault != 0) |
| 213 | { |
| 214 | functional = false; |
| 215 | } |
| 216 | } |
| 217 | catch (const std::system_error& e) |
| 218 | { |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 219 | using namespace sdbusplus::xyz::openbmc_project::Sensor::Device:: |
| 220 | Error; |
| 221 | using metadata = xyz::openbmc_project::Sensor::Device::ReadFailure; |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 222 | |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 223 | report<ReadFailure>( |
| 224 | metadata::CALLOUT_ERRNO(e.code().value()), |
| 225 | metadata::CALLOUT_DEVICE_PATH(_devPath.c_str())); |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 226 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 227 | log<level::INFO>( |
| 228 | "Logging failing sysfs file", |
| 229 | phosphor::logging::entry("FILE=%s", sysfsFullPath.c_str())); |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 230 | } |
| 231 | |
Patrick Venture | 685efa1 | 2018-10-12 18:00:13 -0700 | [diff] [blame] | 232 | static constexpr bool deferSignals = true; |
| 233 | auto& bus = *std::get<sdbusplus::bus::bus*>(info); |
| 234 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 235 | iface = |
| 236 | std::make_shared<StatusObject>(bus, objPath.c_str(), deferSignals); |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 237 | // Set functional property |
| 238 | iface->functional(functional); |
| 239 | |
| 240 | obj[InterfaceType::STATUS] = iface; |
| 241 | } |
| 242 | |
| 243 | return iface; |
| 244 | } |
| 245 | |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 246 | void Sensor::unlockGpio() |
| 247 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 248 | if (_handle) |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 249 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 250 | _handle->setValues({1}); |
| 251 | std::this_thread::sleep_for(_pause); |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | void Sensor::lockGpio() |
| 256 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 257 | if (_handle) |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 258 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 259 | _handle->setValues({0}); |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 263 | } // namespace sensor |