Add Sensor.Value server bindings

Temporarily add the server bindings for xyz.openbmc_project.Sensor.Value
until they are available elsewhere.

Change-Id: Ic04b88e4ef08308ce57732eca74407d5248050cf
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/Makefile.am b/Makefile.am
index d93b6da..6325121 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -10,6 +10,7 @@
 	argument.cpp \
 	directory.cpp \
 	sensorset.cpp \
-	mainloop.cpp
+	mainloop.cpp \
+	xyz.openbmc_project.Sensor.Value.cpp
 
 SUBDIRS = . test
diff --git a/xyz.openbmc_project.Sensor.Value.cpp b/xyz.openbmc_project.Sensor.Value.cpp
new file mode 100644
index 0000000..30d0766
--- /dev/null
+++ b/xyz.openbmc_project.Sensor.Value.cpp
@@ -0,0 +1,316 @@
+#include <algorithm>
+#include <sdbusplus/server.hpp>
+#include <sdbusplus/exception.hpp>
+#include <xyz/openbmc_project/Sensor/Value/server.hpp>
+
+namespace sdbusplus
+{
+namespace xyz
+{
+namespace openbmc_project
+{
+namespace Sensor
+{
+namespace server
+{
+
+Value::Value(bus::bus& bus, const char* path)
+        : _xyz_openbmc_project_Sensor_Value_interface(
+                bus, path, _interface, _vtable, this)
+{
+}
+
+
+
+auto Value::value() const ->
+        int64_t
+{
+    return _value;
+}
+
+int Value::_callback_get_Value(
+        sd_bus* bus, const char* path, const char* interface,
+        const char* property, sd_bus_message* reply, void* context,
+        sd_bus_error* error)
+{
+    using sdbusplus::server::binding::details::convertForMessage;
+
+    try
+    {
+        auto m = message::message(sd_bus_message_ref(reply));
+
+        auto o = static_cast<Value*>(context);
+        m.append(convertForMessage(o->value()));
+    }
+    catch(sdbusplus::internal_exception_t& e)
+    {
+        sd_bus_error_set_const(error, e.name(), e.description());
+        return -EINVAL;
+    }
+
+    return true;
+}
+
+auto Value::value(int64_t value) ->
+        int64_t
+{
+    if (_value != value)
+    {
+        _value = value;
+        _xyz_openbmc_project_Sensor_Value_interface.property_changed("Value");
+    }
+
+    return _value;
+}
+
+int Value::_callback_set_Value(
+        sd_bus* bus, const char* path, const char* interface,
+        const char* property, sd_bus_message* value, void* context,
+        sd_bus_error* error)
+{
+    try
+    {
+        auto m = message::message(sd_bus_message_ref(value));
+
+        auto o = static_cast<Value*>(context);
+
+        int64_t v{};
+        m.read(v);
+        o->value(v);
+    }
+    catch(sdbusplus::internal_exception_t& e)
+    {
+        sd_bus_error_set_const(error, e.name(), e.description());
+        return -EINVAL;
+    }
+
+    return true;
+}
+
+namespace details
+{
+namespace Value
+{
+static const auto _property_Value =
+    utility::tuple_to_array(message::types::type_id<
+            int64_t>());
+}
+}
+auto Value::unit() const ->
+        Unit
+{
+    return _unit;
+}
+
+int Value::_callback_get_Unit(
+        sd_bus* bus, const char* path, const char* interface,
+        const char* property, sd_bus_message* reply, void* context,
+        sd_bus_error* error)
+{
+    using sdbusplus::server::binding::details::convertForMessage;
+
+    try
+    {
+        auto m = message::message(sd_bus_message_ref(reply));
+
+        auto o = static_cast<Value*>(context);
+        m.append(convertForMessage(o->unit()));
+    }
+    catch(sdbusplus::internal_exception_t& e)
+    {
+        sd_bus_error_set_const(error, e.name(), e.description());
+        return -EINVAL;
+    }
+
+    return true;
+}
+
+auto Value::unit(Unit value) ->
+        Unit
+{
+    if (_unit != value)
+    {
+        _unit = value;
+        _xyz_openbmc_project_Sensor_Value_interface.property_changed("Unit");
+    }
+
+    return _unit;
+}
+
+int Value::_callback_set_Unit(
+        sd_bus* bus, const char* path, const char* interface,
+        const char* property, sd_bus_message* value, void* context,
+        sd_bus_error* error)
+{
+    try
+    {
+        auto m = message::message(sd_bus_message_ref(value));
+
+        auto o = static_cast<Value*>(context);
+
+        std::string v{};
+        m.read(v);
+        o->unit(convertUnitFromString(v));
+    }
+    catch(sdbusplus::internal_exception_t& e)
+    {
+        sd_bus_error_set_const(error, e.name(), e.description());
+        return -EINVAL;
+    }
+
+    return true;
+}
+
+namespace details
+{
+namespace Value
+{
+static const auto _property_Unit =
+    utility::tuple_to_array(message::types::type_id<
+            std::string>());
+}
+}
+auto Value::scale() const ->
+        int64_t
+{
+    return _scale;
+}
+
+int Value::_callback_get_Scale(
+        sd_bus* bus, const char* path, const char* interface,
+        const char* property, sd_bus_message* reply, void* context,
+        sd_bus_error* error)
+{
+    using sdbusplus::server::binding::details::convertForMessage;
+
+    try
+    {
+        auto m = message::message(sd_bus_message_ref(reply));
+
+        auto o = static_cast<Value*>(context);
+        m.append(convertForMessage(o->scale()));
+    }
+    catch(sdbusplus::internal_exception_t& e)
+    {
+        sd_bus_error_set_const(error, e.name(), e.description());
+        return -EINVAL;
+    }
+
+    return true;
+}
+
+auto Value::scale(int64_t value) ->
+        int64_t
+{
+    if (_scale != value)
+    {
+        _scale = value;
+        _xyz_openbmc_project_Sensor_Value_interface.property_changed("Scale");
+    }
+
+    return _scale;
+}
+
+int Value::_callback_set_Scale(
+        sd_bus* bus, const char* path, const char* interface,
+        const char* property, sd_bus_message* value, void* context,
+        sd_bus_error* error)
+{
+    try
+    {
+        auto m = message::message(sd_bus_message_ref(value));
+
+        auto o = static_cast<Value*>(context);
+
+        int64_t v{};
+        m.read(v);
+        o->scale(v);
+    }
+    catch(sdbusplus::internal_exception_t& e)
+    {
+        sd_bus_error_set_const(error, e.name(), e.description());
+        return -EINVAL;
+    }
+
+    return true;
+}
+
+namespace details
+{
+namespace Value
+{
+static const auto _property_Scale =
+    utility::tuple_to_array(message::types::type_id<
+            int64_t>());
+}
+}
+
+
+namespace
+{
+/** String to enum mapping for Value::Unit */
+static const std::tuple<const char*, Value::Unit> mappingValueUnit[] =
+        {
+            std::make_tuple( "xyz.openbmc_project.Sensor.Value.Unit.DegreesC",                 Value::Unit::DegreesC ),
+            std::make_tuple( "xyz.openbmc_project.Sensor.Value.Unit.RPMS",                 Value::Unit::RPMS ),
+            std::make_tuple( "xyz.openbmc_project.Sensor.Value.Unit.Volts",                 Value::Unit::Volts ),
+            std::make_tuple( "xyz.openbmc_project.Sensor.Value.Unit.Meters",                 Value::Unit::Meters ),
+        };
+
+} // anonymous namespace
+
+auto Value::convertUnitFromString(std::string& s) ->
+        Unit
+{
+    auto i = std::find_if(
+            std::begin(mappingValueUnit),
+            std::end(mappingValueUnit),
+            [&s](auto& e){ return 0 == strcmp(s.c_str(), std::get<0>(e)); } );
+    if (std::end(mappingValueUnit) == i)
+    {
+        throw sdbusplus::exception::InvalidEnumString();
+    }
+    else
+    {
+        return std::get<1>(*i);
+    }
+}
+
+std::string convertForMessage(Value::Unit v)
+{
+    auto i = std::find_if(
+            std::begin(mappingValueUnit),
+            std::end(mappingValueUnit),
+            [v](auto& e){ return v == std::get<1>(e); });
+    return std::get<0>(*i);
+}
+
+const vtable::vtable_t Value::_vtable[] = {
+    vtable::start(),
+    vtable::property("Value",
+                     details::Value::_property_Value
+                        .data(),
+                     _callback_get_Value,
+                     _callback_set_Value,
+                     vtable::property_::emits_change),
+    vtable::property("Unit",
+                     details::Value::_property_Unit
+                        .data(),
+                     _callback_get_Unit,
+                     _callback_set_Unit,
+                     vtable::property_::emits_change),
+    vtable::property("Scale",
+                     details::Value::_property_Scale
+                        .data(),
+                     _callback_get_Scale,
+                     _callback_set_Scale,
+                     vtable::property_::emits_change),
+    vtable::end()
+};
+
+} // namespace server
+} // namespace Sensor
+} // namespace openbmc_project
+} // namespace xyz
+} // namespace sdbusplus
+
diff --git a/xyz/openbmc_project/Sensor/Value/server.hpp b/xyz/openbmc_project/Sensor/Value/server.hpp
new file mode 100644
index 0000000..00d49e1
--- /dev/null
+++ b/xyz/openbmc_project/Sensor/Value/server.hpp
@@ -0,0 +1,127 @@
+#pragma once
+#include <tuple>
+#include <systemd/sd-bus.h>
+#include <sdbusplus/server.hpp>
+
+namespace sdbusplus
+{
+namespace xyz
+{
+namespace openbmc_project
+{
+namespace Sensor
+{
+namespace server
+{
+
+class Value
+{
+    public:
+        /* Define all of the basic class operations:
+         *     Not allowed:
+         *         - Default constructor to avoid nullptrs.
+         *         - Copy operations due to internal unique_ptr.
+         *     Allowed:
+         *         - Move operations.
+         *         - Destructor.
+         */
+        Value() = delete;
+        Value(const Value&) = delete;
+        Value& operator=(const Value&) = delete;
+        Value(Value&&) = default;
+        Value& operator=(Value&&) = default;
+        virtual ~Value() = default;
+
+        /** @brief Constructor to put object onto bus at a dbus path.
+         *  @param[in] bus - Bus to attach to.
+         *  @param[in] path - Path to attach at.
+         */
+        Value(bus::bus& bus, const char* path);
+
+        enum class Unit
+        {
+            DegreesC,
+            RPMS,
+            Volts,
+            Meters,
+        };
+
+
+
+        /** Get value of Value */
+        virtual int64_t value() const;
+        /** Set value of Value */
+        virtual int64_t value(int64_t value);
+        /** Get value of Unit */
+        virtual Unit unit() const;
+        /** Set value of Unit */
+        virtual Unit unit(Unit value);
+        /** Get value of Scale */
+        virtual int64_t scale() const;
+        /** Set value of Scale */
+        virtual int64_t scale(int64_t value);
+
+    /** @brief Convert a string to an appropriate enum value.
+     *  @param[in] s - The string to convert in the form of
+     *                 "xyz.openbmc_project.Sensor.Value.<value name>"
+     *  @return - The enum value.
+     */
+    static Unit convertUnitFromString(std::string& s);
+
+    private:
+
+        /** @brief sd-bus callback for get-property 'Value' */
+        static int _callback_get_Value(
+            sd_bus*, const char*, const char*, const char*,
+            sd_bus_message*, void*, sd_bus_error*);
+        /** @brief sd-bus callback for set-property 'Value' */
+        static int _callback_set_Value(
+            sd_bus*, const char*, const char*, const char*,
+            sd_bus_message*, void*, sd_bus_error*);
+
+        /** @brief sd-bus callback for get-property 'Unit' */
+        static int _callback_get_Unit(
+            sd_bus*, const char*, const char*, const char*,
+            sd_bus_message*, void*, sd_bus_error*);
+        /** @brief sd-bus callback for set-property 'Unit' */
+        static int _callback_set_Unit(
+            sd_bus*, const char*, const char*, const char*,
+            sd_bus_message*, void*, sd_bus_error*);
+
+        /** @brief sd-bus callback for get-property 'Scale' */
+        static int _callback_get_Scale(
+            sd_bus*, const char*, const char*, const char*,
+            sd_bus_message*, void*, sd_bus_error*);
+        /** @brief sd-bus callback for set-property 'Scale' */
+        static int _callback_set_Scale(
+            sd_bus*, const char*, const char*, const char*,
+            sd_bus_message*, void*, sd_bus_error*);
+
+
+        static constexpr auto _interface = "xyz.openbmc_project.Sensor.Value";
+        static const vtable::vtable_t _vtable[];
+        sdbusplus::server::interface::interface
+                _xyz_openbmc_project_Sensor_Value_interface;
+
+        int64_t _value{};
+        Unit _unit{};
+        int64_t _scale{};
+
+};
+
+/* Specialization of sdbusplus::server::bindings::details::convertForMessage
+ * for enum-type Value::Unit.
+ *
+ * This converts from the enum to a constant c-string representing the enum.
+ *
+ * @param[in] e - Enum value to convert.
+ * @return C-string representing the name for the enum value.
+ */
+std::string convertForMessage(Value::Unit e);
+
+} // namespace server
+} // namespace Sensor
+} // namespace openbmc_project
+} // namespace xyz
+} // namespace sdbusplus
+