Rework SysfsTemperature to be more generic
Change-Id: I68fe3bc86b0b92f0ac62a6f41d285da63741b6ed
diff --git a/src/ietf-hardware/IETFHardware.cpp b/src/ietf-hardware/IETFHardware.cpp
index ba079e9..618995b 100644
--- a/src/ietf-hardware/IETFHardware.cpp
+++ b/src/ietf-hardware/IETFHardware.cpp
@@ -145,33 +145,50 @@
return res;
}
-SysfsTemperature::SysfsTemperature(std::string componentName, std::optional<std::string> parent, std::shared_ptr<sysfs::HWMon> hwmon, int sysfsChannelNr)
+std::string getSysfsFilename(const SensorType type, int sysfsChannelNr)
+{
+ switch (type) {
+ case SensorType::Temperature:
+ return "temp"s + std::to_string(sysfsChannelNr) + "_input";
+ }
+
+ __builtin_unreachable();
+}
+
+template <SensorType TYPE> const DataTree sysfsStaticData;
+template <> const DataTree sysfsStaticData<SensorType::Temperature> = {
+ {"class", "iana-hardware:sensor"},
+ {"sensor-data/value-type", "celsius"},
+ {"sensor-data/value-scale", "milli"},
+ {"sensor-data/value-precision", "0"},
+ {"sensor-data/oper-status", "ok"},
+};
+
+template <SensorType TYPE>
+SysfsValue<TYPE>::SysfsValue(std::string componentName, std::optional<std::string> parent, std::shared_ptr<sysfs::HWMon> hwmon, int sysfsChannelNr)
: DataReader(std::move(componentName), std::move(parent))
, m_hwmon(std::move(hwmon))
- , m_sysfsTemperatureFile("temp"s + std::to_string(sysfsChannelNr) + "_input")
+ , m_sysfsFile(getSysfsFilename(TYPE, sysfsChannelNr))
{
addComponent(m_staticData,
m_componentName,
m_parent,
- DataTree {
- {"class", "iana-hardware:sensor"},
- {"sensor-data/value-type", "celsius"},
- {"sensor-data/value-scale", "milli"},
- {"sensor-data/value-precision", "0"},
- {"sensor-data/oper-status", "ok"},
- });
+ sysfsStaticData<TYPE>);
}
-DataTree SysfsTemperature::operator()() const
+template <SensorType TYPE>
+DataTree SysfsValue<TYPE>::operator()() const
{
DataTree res(m_staticData);
- int64_t sensorValue = m_hwmon->attributes().at(m_sysfsTemperatureFile);
+ int64_t sensorValue = m_hwmon->attributes().at(m_sysfsFile);
addSensorValue(res, m_componentName, std::to_string(sensorValue));
return res;
}
+template struct SysfsValue<SensorType::Temperature>;
+
EMMC::EMMC(std::string componentName, std::optional<std::string> parent, std::shared_ptr<sysfs::EMMC> emmc)
: DataReader(std::move(componentName), std::move(parent))
, m_emmc(std::move(emmc))