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