diff options
author | Patrick Venture <venture@google.com> | 2018-04-17 17:40:00 -0700 |
---|---|---|
committer | Patrick Venture <venture@google.com> | 2018-04-18 14:25:59 -0700 |
commit | 09791857fd5322e558ca5238f55ec0f914140b5a (patch) | |
tree | 568d83925f974a2eae2653865251147b34d5ded1 | |
parent | a24c8808d5c3b13b849e79115a6969b5a8faf8b0 (diff) | |
download | phosphor-hwmon-09791857fd5322e558ca5238f55ec0f914140b5a.tar.gz phosphor-hwmon-09791857fd5322e558ca5238f55ec0f914140b5a.zip |
mainloop cleanup: moved getAttrs to hwmon namespace
Moved the code that maps a sensor type to its dbus and
hwmon components from mainloop to the hwmon namespace.
Change-Id: I7963951c9484c02d17a3eb415906859609e0efd3
Signed-off-by: Patrick Venture <venture@google.com>
-rw-r--r-- | Makefile.am | 3 | ||||
-rw-r--r-- | hwmon.cpp | 26 | ||||
-rw-r--r-- | hwmon.hpp | 75 | ||||
-rw-r--r-- | mainloop.cpp | 106 |
4 files changed, 115 insertions, 95 deletions
diff --git a/Makefile.am b/Makefile.am index 932e4e2..238eaef 100644 --- a/Makefile.am +++ b/Makefile.am @@ -24,6 +24,7 @@ libhwmon_la_SOURCES = \ env.cpp \ fan_speed.cpp \ fan_pwm.cpp \ - timer.cpp + timer.cpp \ + hwmon.cpp SUBDIRS = . test msl diff --git a/hwmon.cpp b/hwmon.cpp new file mode 100644 index 0000000..4eb47ae --- /dev/null +++ b/hwmon.cpp @@ -0,0 +1,26 @@ +#include "hwmon.hpp" + +namespace hwmon { + +bool getAttributes(const std::string& type, Attributes& attributes) +{ + // *INDENT-OFF* + auto a = std::find_if( + typeAttrMap.begin(), + typeAttrMap.end(), + [&](const auto & e) + { + return type == getHwmonType(e); + }); + // *INDENT-ON* + + if (a == typeAttrMap.end()) + { + return false; + } + + attributes = *a; + return true; +} + +} // namespace hwmon @@ -1,6 +1,9 @@ #pragma once #include <string> +#include <tuple> + +#include "interface.hpp" namespace hwmon { @@ -36,6 +39,78 @@ static const std::string energy = cenergy; static const std::string power = cpower; static const std::string pwm = cpwm; } + +static constexpr auto typeAttrMap = +{ + // 1 - hwmon class + // 2 - unit + // 3 - sysfs scaling factor + // 4 - namespace + std::make_tuple( + hwmon::type::ctemp, + ValueInterface::Unit::DegreesC, + -3, + "temperature"), + std::make_tuple( + hwmon::type::cfan, + ValueInterface::Unit::RPMS, + 0, + "fan_tach"), + std::make_tuple( + hwmon::type::cvolt, + ValueInterface::Unit::Volts, + -3, + "voltage"), + std::make_tuple( + hwmon::type::ccurr, + ValueInterface::Unit::Amperes, + -3, + "current"), + std::make_tuple( + hwmon::type::cenergy, + ValueInterface::Unit::Joules, + -6, + "energy"), + std::make_tuple( + hwmon::type::cpower, + ValueInterface::Unit::Watts, + -6, + "power"), +}; + +inline auto getHwmonType(decltype(typeAttrMap)::const_reference attrs) +{ + return std::get<0>(attrs); } +inline auto getUnit(decltype(typeAttrMap)::const_reference attrs) +{ + return std::get<1>(attrs); +} + +inline auto getScale(decltype(typeAttrMap)::const_reference attrs) +{ + return std::get<2>(attrs); +} + +inline auto getNamespace(decltype(typeAttrMap)::const_reference attrs) +{ + return std::get<3>(attrs); +} + +using AttributeIterator = decltype(*typeAttrMap.begin()); +using Attributes + = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type; + +/** @brief Get Attribute tuple for the type + * + * Given a type, it tries to find the corresponding tuple + * + * @param[in] type the sensor type + * @param[in,out] A pointer to the Attribute tuple + */ +bool getAttributes(const std::string& type, Attributes& attributes); + +} // namespace hwmon + // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/mainloop.cpp b/mainloop.cpp index 37c2316..1633eef 100644 --- a/mainloop.cpp +++ b/mainloop.cpp @@ -22,15 +22,15 @@ #include <phosphor-logging/elog-errors.hpp> #include "config.h" -#include "sensorset.hpp" +#include "env.hpp" +#include "fan_pwm.hpp" +#include "fan_speed.hpp" #include "hwmon.hpp" +#include "sensorset.hpp" #include "sysfs.hpp" #include "mainloop.hpp" -#include "env.hpp" -#include "thresholds.hpp" #include "targets.hpp" -#include "fan_speed.hpp" -#include "fan_pwm.hpp" +#include "thresholds.hpp" #include <xyz/openbmc_project/Sensor/Device/error.hpp> @@ -75,88 +75,6 @@ struct valueAdjust // Store the valueAdjust for sensors std::map<SensorSet::key_type, valueAdjust> sensorAdjusts; -static constexpr auto typeAttrMap = -{ - // 1 - hwmon class - // 2 - unit - // 3 - sysfs scaling factor - std::make_tuple( - hwmon::type::ctemp, - ValueInterface::Unit::DegreesC, - -3, - "temperature"), - std::make_tuple( - hwmon::type::cfan, - ValueInterface::Unit::RPMS, - 0, - "fan_tach"), - std::make_tuple( - hwmon::type::cvolt, - ValueInterface::Unit::Volts, - -3, - "voltage"), - std::make_tuple( - hwmon::type::ccurr, - ValueInterface::Unit::Amperes, - -3, - "current"), - std::make_tuple( - hwmon::type::cenergy, - ValueInterface::Unit::Joules, - -6, - "energy"), - std::make_tuple( - hwmon::type::cpower, - ValueInterface::Unit::Watts, - -6, - "power"), -}; - -auto getHwmonType(decltype(typeAttrMap)::const_reference attrs) -{ - return std::get<0>(attrs); -} - -auto getUnit(decltype(typeAttrMap)::const_reference attrs) -{ - return std::get<1>(attrs); -} - -auto getScale(decltype(typeAttrMap)::const_reference attrs) -{ - return std::get<2>(attrs); -} - -auto getNamespace(decltype(typeAttrMap)::const_reference attrs) -{ - return std::get<3>(attrs); -} - -using AttributeIterator = decltype(*typeAttrMap.begin()); -using Attributes - = std::remove_cv<std::remove_reference<AttributeIterator>::type>::type; - -auto getAttributes(const std::string& type, Attributes& attributes) -{ - // *INDENT-OFF* - auto a = std::find_if( - typeAttrMap.begin(), - typeAttrMap.end(), - [&](const auto & e) - { - return type == getHwmonType(e); - }); - // *INDENT-ON* - - if (a == typeAttrMap.end()) - { - return false; - } - - attributes = *a; - return true; -} - void addRemoveRCs(const SensorSet::key_type& sensor, const std::string& rcList) { @@ -253,11 +171,11 @@ auto addValue(const SensorSet::key_type& sensor, auto iface = std::make_shared<ValueObject>(bus, objPath.c_str(), deferSignals); iface->value(val); - Attributes attrs; - if (getAttributes(sensor.first, attrs)) + hwmon::Attributes attrs; + if (hwmon::getAttributes(sensor.first, attrs)) { - iface->unit(getUnit(attrs)); - iface->scale(getScale(attrs)); + iface->unit(hwmon::getUnit(attrs)); + iface->scale(hwmon::getScale(attrs)); } auto maxValue = env::getEnv("MAXVALUE", sensor); @@ -327,8 +245,8 @@ void MainLoop::getObject(SensorSet::container_t::const_reference sensor) return; } - Attributes attrs; - if (!getAttributes(sensor.first.first, attrs)) + hwmon::Attributes attrs; + if (!hwmon::getAttributes(sensor.first.first, attrs)) { return; } @@ -341,7 +259,7 @@ void MainLoop::getObject(SensorSet::container_t::const_reference sensor) std::string objectPath{_root}; objectPath.append(1, '/'); - objectPath.append(getNamespace(attrs)); + objectPath.append(hwmon::getNamespace(attrs)); objectPath.append(1, '/'); objectPath.append(label); |