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