sensor.cpp (641299377c7352f9c946edf1527e5ded73c01f52) sensor.cpp (02e598ab445833e0ce615f88ac0aed7288a100e0)
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"

--- 17 unchanged lines hidden (view full) ---

26
27using namespace phosphor::logging;
28using namespace sdbusplus::xyz::openbmc_project::Common::Error;
29
30// todo: this can be simplified once we move to the double interface
31Sensor::Sensor(const SensorSet::key_type& sensor,
32 const hwmonio::HwmonIOInterface* ioAccess,
33 const std::string& devPath) :
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"

--- 17 unchanged lines hidden (view full) ---

26
27using namespace phosphor::logging;
28using namespace sdbusplus::xyz::openbmc_project::Common::Error;
29
30// todo: this can be simplified once we move to the double interface
31Sensor::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)
34 _sensor(sensor), _ioAccess(ioAccess), _devPath(devPath), _scale(0),
35 _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)

--- 56 unchanged lines hidden (view full) ---

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
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)

--- 56 unchanged lines hidden (view full) ---

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);
108 value = static_cast<decltype(value)>(
109 static_cast<double>(value) * _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
111
112 if constexpr (std::is_same<SensorValueType, double>::value)
113 {
114 value *= std::pow(10, _scale);
115 }
116
117 return value;
118}
119
120std::shared_ptr<ValueObject> Sensor::addValue(const RetryIO& retryIO,
121 ObjectInfo& info,
122 TimedoutMap& timedoutMap)
120std::shared_ptr<ValueObject> Sensor::addValue(
121 const RetryIO& retryIO, ObjectInfo& info, TimedoutMap& timedoutMap)
123{
124 // Get the initial value for the value interface.
125 auto& bus = *std::get<sdbusplus::bus_t*>(info);
126 auto& obj = std::get<InterfaceMap>(info);
127 auto& objPath = std::get<std::string>(info);
128
129 SensorValueType val = 0;
130

--- 85 unchanged lines hidden (view full) ---

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;
122{
123 // Get the initial value for the value interface.
124 auto& bus = *std::get<sdbusplus::bus_t*>(info);
125 auto& obj = std::get<InterfaceMap>(info);
126 auto& objPath = std::get<std::string>(info);
127
128 SensorValueType val = 0;
129

--- 85 unchanged lines hidden (view full) ---

215 auto& obj = std::get<InterfaceMap>(info);
216
217 // Check if fault sysfs file exists
218 std::string faultName = _sensor.first;
219 std::string faultID = _sensor.second;
220 std::string entry = hwmon::entry::fault;
221
222 bool functional = true;
224 auto sysfsFullPath = sysfs::make_sysfs_path(_ioAccess->path(), faultName,
225 faultID, entry);
223 auto sysfsFullPath =
224 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)

--- 24 unchanged lines hidden (view full) ---

258 // Set functional property
259 iface->functional(functional);
260
261 obj[InterfaceType::STATUS] = iface;
262
263 return iface;
264}
265
225 if (fs::exists(sysfsFullPath))
226 {
227 _hasFaultFile = true;
228 try
229 {
230 uint32_t fault = _ioAccess->read(faultName, faultID, entry,
231 hwmonio::retries, hwmonio::delay);
232 if (fault != 0)

--- 24 unchanged lines hidden (view full) ---

257 // Set functional property
258 iface->functional(functional);
259
260 obj[InterfaceType::STATUS] = iface;
261
262 return iface;
263}
264
266std::shared_ptr<AccuracyObject> Sensor::addAccuracy(ObjectInfo& info,
267 double accuracy)
265std::shared_ptr<AccuracyObject>
266 Sensor::addAccuracy(ObjectInfo& info, double accuracy)
268{
269 auto& objPath = std::get<std::string>(info);
270 auto& obj = std::get<InterfaceMap>(info);
271
272 auto& bus = *std::get<sdbusplus::bus_t*>(info);
273 auto iface = std::make_shared<AccuracyObject>(
274 bus, objPath.c_str(), AccuracyObject::action::emit_no_signals);
275
276 iface->accuracy(accuracy);
277 obj[InterfaceType::ACCURACY] = iface;
278
279 return iface;
280}
281
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
282std::shared_ptr<PriorityObject> Sensor::addPriority(ObjectInfo& info,
283 size_t priority)
281std::shared_ptr<PriorityObject>
282 Sensor::addPriority(ObjectInfo& info, size_t priority)
284{
285 auto& objPath = std::get<std::string>(info);
286 auto& obj = std::get<InterfaceMap>(info);
287
288 auto& bus = *std::get<sdbusplus::bus_t*>(info);
289 auto iface = std::make_shared<PriorityObject>(
290 bus, objPath.c_str(), PriorityObject::action::emit_no_signals);
291

--- 16 unchanged lines hidden (view full) ---

308 }
309
310 handle->setValues({1});
311 // Default pause needed to guarantee sensors are ready
312 std::this_thread::sleep_for(std::chrono::milliseconds(500));
313 return GpioLocker(std::move(handle));
314}
315
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

--- 16 unchanged lines hidden (view full) ---

307 }
308
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));
313}
314
316SensorValueType asyncRead(const SensorSet::key_type& sensorSetKey,
317 const hwmonio::HwmonIOInterface* ioAccess,
318 std::chrono::milliseconds asyncTimeout,
319 TimedoutMap& timedoutMap, const std::string& type,
320 const std::string& id, const std::string& sensor,
321 const size_t retries,
322 const std::chrono::milliseconds delay)
315SensorValueType 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)
323{
324 // Default async read timeout
325 bool valueIsValid = false;
326 std::future<int64_t> asyncThread;
327
328 auto asyncIter = timedoutMap.find(sensorSetKey);
329 if (asyncIter == timedoutMap.end())
330 {
331 // If sensor not found in timedoutMap, spawn an async thread
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
332 asyncThread = std::async(std::launch::async,
333 &hwmonio::HwmonIOInterface::read, ioAccess,
334 type, id, sensor, retries, delay);
330 asyncThread =
331 std::async(std::launch::async, &hwmonio::HwmonIOInterface::read,
332 ioAccess, type, id, sensor, retries, delay);
335 valueIsValid = true;
336 }
337 else
338 {
339 // If we already have the async thread in the timedoutMap, it means this
340 // sensor has already timed out in the previous reads. No need to wait
341 // on subsequent reads - proceed to check the future_status to see when
342 // the async thread finishes

--- 38 unchanged lines hidden ---
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

--- 38 unchanged lines hidden ---