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