Allow building with double value interface
Sensor interface using double looks about ready to go in,
it was found this repo doesn't build. Fix it.
Change-Id: I48b943e5e8e77506d1b978d2b9a56db13876248e
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/sensors/host.cpp b/sensors/host.cpp
index 951c0ca..96a0514 100644
--- a/sensors/host.cpp
+++ b/sensors/host.cpp
@@ -21,6 +21,15 @@
#include <memory>
#include <mutex>
+template <typename T>
+void scaleHelper(T& ptr, int64_t value)
+{
+ if constexpr (std::is_same_v<ValueType, int64_t>)
+ {
+ ptr->scale(value);
+ }
+}
+
std::unique_ptr<Sensor> HostSensor::createTemp(const std::string& name,
int64_t timeout,
sdbusplus::bus::bus& bus,
@@ -36,7 +45,7 @@
// TODO(venture): Need to not hard-code that this is DegreesC and scale
// 10x-3 unless it is! :D
sensor->unit(ValueInterface::Unit::DegreesC);
- sensor->scale(-3);
+ scaleHelper(sensor, -3);
sensor->emit_object_added();
// emit_object_added() can be called twice, harmlessly, the second time it
// doesn't actually happen, but we don't want to call it before we set up
@@ -49,12 +58,22 @@
return sensor;
}
-int64_t HostSensor::value(int64_t value)
+template <typename T>
+int64_t getScale(T* sensor)
+{
+ if constexpr (std::is_same_v<ValueType, int64_t>)
+ {
+ return sensor->scale();
+ }
+ return 0;
+}
+
+ValueType HostSensor::value(ValueType value)
{
std::lock_guard<std::mutex> guard(_lock);
_updated = std::chrono::high_resolution_clock::now();
- _value = value * pow(10, scale()); /* scale value */
+ _value = value * pow(10, getScale(this)); /* scale value */
return ValueObject::value(value);
}
diff --git a/sensors/host.hpp b/sensors/host.hpp
index 3a8387d..82b1747 100644
--- a/sensors/host.hpp
+++ b/sensors/host.hpp
@@ -6,6 +6,7 @@
#include <mutex>
#include <sdbusplus/bus.hpp>
#include <sdbusplus/server.hpp>
+#include <type_traits>
#include <xyz/openbmc_project/Sensor/Value/server.hpp>
template <typename... T>
@@ -14,6 +15,20 @@
using ValueInterface = sdbusplus::xyz::openbmc_project::Sensor::server::Value;
using ValueObject = ServerObject<ValueInterface>;
+class ValueHelper : public ValueInterface
+{
+
+ public:
+ auto operator()() const
+ {
+ return value();
+ }
+};
+
+constexpr bool usingDouble =
+ std::is_same_v<std::result_of_t<ValueHelper()>, double>;
+using ValueType = std::conditional_t<usingDouble, double, int64_t>;
+
/*
* HostSensor object is a Sensor derivative that also implements a ValueObject,
* which comes from the dbus as an object that implements Sensor.Value.
@@ -33,8 +48,7 @@
{
}
- /* Note: This must be int64_t because it's from ValueObject */
- int64_t value(int64_t value) override;
+ ValueType value(ValueType value) override;
ReadReturn read(void) override;
void write(double value) override;