From bda97eb107fc60544fb8cde0ec1ada1da2d66f73 Mon Sep 17 00:00:00 2001 From: Vishwanatha Subbanna Date: Wed, 30 Nov 2016 12:21:25 +0530 Subject: Use generated bindings for physical led controller Extends the generated interface and provides skeleton implementation of led controller. Change-Id: I13485f39755213b8a7324e526d6335f3ee0a2b67 Signed-off-by: Vishwanatha Subbanna --- Makefile.am | 5 +- controller.cpp | 1 - physical.hpp | 52 ++++ xyz.openbmc_project.Led.Physical.cpp | 355 ++++++++++++++++++++++++++++ xyz/openbmc_project/Led/Physical/server.hpp | 149 ++++++++++++ 5 files changed, 560 insertions(+), 2 deletions(-) create mode 100644 physical.hpp create mode 100644 xyz.openbmc_project.Led.Physical.cpp create mode 100644 xyz/openbmc_project/Led/Physical/server.hpp diff --git a/Makefile.am b/Makefile.am index fdf4020..b82fdf8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,9 @@ sbin_PROGRAMS = phosphor-ledcontroller -phosphor_ledcontroller_SOURCES = controller.cpp argument.cpp +phosphor_ledcontroller_SOURCES = \ + controller.cpp \ + argument.cpp \ + xyz.openbmc_project.Led.Physical.cpp phosphor_ledcontroller_LDFLAGS = $(SYSTEMD_LIBS) phosphor_ledcontroller_CFLAGS = $(SYSTEMD_CFLAGS) diff --git a/controller.cpp b/controller.cpp index 8db607c..bfc1c74 100644 --- a/controller.cpp +++ b/controller.cpp @@ -15,7 +15,6 @@ */ #include -#include #include "argument.hpp" static void ExitWithError(const char* err, char** argv) diff --git a/physical.hpp b/physical.hpp new file mode 100644 index 0000000..f0b4048 --- /dev/null +++ b/physical.hpp @@ -0,0 +1,52 @@ +#pragma once + +#include +#include +#include +#include "xyz/openbmc_project/Led/Physical/server.hpp" +namespace phosphor +{ +namespace led +{ + +/** @class Physical + * @brief Responsible for applying actions on a particular physical LED + */ +class Physical : public sdbusplus::server::object::object< + sdbusplus::xyz::openbmc_project::Led::server::Physical> +{ + public: + Physical() = delete; + ~Physical() = default; + Physical(const Physical&) = delete; + Physical& operator=(const Physical&) = delete; + Physical(Physical&&) = delete; + Physical& operator=(Physical&&) = delete; + + /** @brief Constructs LED object + * + * @param[in] bus - system dbus handler + * @param[in] objPath - The Dbus path that hosts physical LED + * @param[in] ledPath - sysfs path where this LED is exported + */ + Physical(sdbusplus::bus::bus& bus, + const std::string& objPath, + const std::string& ledPath) : + + sdbusplus::server::object::object< + sdbusplus::xyz::openbmc_project::Led::server::Physical>( + bus, objPath.c_str()), + path(ledPath) + { + // Nothing to do here + } + + private: + /** @brief File system location where this LED is exposed + * Typically /sys/class/leds/ + */ + std::string path; +}; + +} // namespace led +} // namespace phosphor diff --git a/xyz.openbmc_project.Led.Physical.cpp b/xyz.openbmc_project.Led.Physical.cpp new file mode 100644 index 0000000..6b96e81 --- /dev/null +++ b/xyz.openbmc_project.Led.Physical.cpp @@ -0,0 +1,355 @@ +#include +#include +#include +#include "xyz/openbmc_project/Led/Physical/server.hpp" + +namespace sdbusplus +{ +namespace xyz +{ +namespace openbmc_project +{ +namespace Led +{ +namespace server +{ + +Physical::Physical(bus::bus& bus, const char* path) + : _xyz_openbmc_project_Led_Physical_interface( + bus, path, _interface, _vtable, this) +{ +} + + + +auto Physical::state() const -> + Action +{ + return _state; +} + +int Physical::_callback_get_State( + 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(context); + m.append(convertForMessage(o->state())); + } + catch(sdbusplus::internal_exception_t& e) + { + sd_bus_error_set_const(error, e.name(), e.description()); + return -EINVAL; + } + + return true; +} + +auto Physical::state(Action value) -> + Action +{ + if (_state != value) + { + _state = value; + _xyz_openbmc_project_Led_Physical_interface.property_changed("State"); + } + + return _state; +} + +int Physical::_callback_set_State( + 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(context); + + std::string v{}; + m.read(v); + o->state(convertActionFromString(v)); + } + catch(sdbusplus::internal_exception_t& e) + { + sd_bus_error_set_const(error, e.name(), e.description()); + return -EINVAL; + } + + return true; +} + +namespace details +{ +namespace Physical +{ +static const auto _property_State = + utility::tuple_to_array(message::types::type_id< + std::string>()); +} +} +auto Physical::dutyOn() const -> + uint8_t +{ + return _dutyOn; +} + +int Physical::_callback_get_DutyOn( + 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(context); + m.append(convertForMessage(o->dutyOn())); + } + catch(sdbusplus::internal_exception_t& e) + { + sd_bus_error_set_const(error, e.name(), e.description()); + return -EINVAL; + } + + return true; +} + +auto Physical::dutyOn(uint8_t value) -> + uint8_t +{ + if (_dutyOn != value) + { + _dutyOn = value; + _xyz_openbmc_project_Led_Physical_interface.property_changed("DutyOn"); + } + + return _dutyOn; +} + +int Physical::_callback_set_DutyOn( + 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(context); + + uint8_t v{}; + m.read(v); + o->dutyOn(v); + } + catch(sdbusplus::internal_exception_t& e) + { + sd_bus_error_set_const(error, e.name(), e.description()); + return -EINVAL; + } + + return true; +} + +namespace details +{ +namespace Physical +{ +static const auto _property_DutyOn = + utility::tuple_to_array(message::types::type_id< + uint8_t>()); +} +} +auto Physical::color() const -> + Palette +{ + return _color; +} + +int Physical::_callback_get_Color( + 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(context); + m.append(convertForMessage(o->color())); + } + catch(sdbusplus::internal_exception_t& e) + { + sd_bus_error_set_const(error, e.name(), e.description()); + return -EINVAL; + } + + return true; +} + +auto Physical::color(Palette value) -> + Palette +{ + if (_color != value) + { + _color = value; + _xyz_openbmc_project_Led_Physical_interface.property_changed("Color"); + } + + return _color; +} + +int Physical::_callback_set_Color( + 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(context); + + std::string v{}; + m.read(v); + o->color(convertPaletteFromString(v)); + } + catch(sdbusplus::internal_exception_t& e) + { + sd_bus_error_set_const(error, e.name(), e.description()); + return -EINVAL; + } + + return true; +} + +namespace details +{ +namespace Physical +{ +static const auto _property_Color = + utility::tuple_to_array(message::types::type_id< + std::string>()); +} +} + + +namespace +{ +/** String to enum mapping for Physical::Action */ +static const std::tuple mappingPhysicalAction[] = + { + std::make_tuple( "xyz.openbmc_project.Led.Physical.Action.Off", Physical::Action::Off ), + std::make_tuple( "xyz.openbmc_project.Led.Physical.Action.On", Physical::Action::On ), + std::make_tuple( "xyz.openbmc_project.Led.Physical.Action.Blink", Physical::Action::Blink ), + }; + +} // anonymous namespace + +auto Physical::convertActionFromString(std::string& s) -> + Action +{ + auto i = std::find_if( + std::begin(mappingPhysicalAction), + std::end(mappingPhysicalAction), + [&s](auto& e){ return 0 == strcmp(s.c_str(), std::get<0>(e)); } ); + if (std::end(mappingPhysicalAction) == i) + { + throw sdbusplus::exception::InvalidEnumString(); + } + else + { + return std::get<1>(*i); + } +} + +std::string convertForMessage(Physical::Action v) +{ + auto i = std::find_if( + std::begin(mappingPhysicalAction), + std::end(mappingPhysicalAction), + [v](auto& e){ return v == std::get<1>(e); }); + return std::get<0>(*i); +} + +namespace +{ +/** String to enum mapping for Physical::Palette */ +static const std::tuple mappingPhysicalPalette[] = + { + std::make_tuple( "xyz.openbmc_project.Led.Physical.Palette.Unknown", Physical::Palette::Unknown ), + std::make_tuple( "xyz.openbmc_project.Led.Physical.Palette.Red", Physical::Palette::Red ), + std::make_tuple( "xyz.openbmc_project.Led.Physical.Palette.Green", Physical::Palette::Green ), + std::make_tuple( "xyz.openbmc_project.Led.Physical.Palette.Blue", Physical::Palette::Blue ), + std::make_tuple( "xyz.openbmc_project.Led.Physical.Palette.Yellow", Physical::Palette::Yellow ), + }; + +} // anonymous namespace + +auto Physical::convertPaletteFromString(std::string& s) -> + Palette +{ + auto i = std::find_if( + std::begin(mappingPhysicalPalette), + std::end(mappingPhysicalPalette), + [&s](auto& e){ return 0 == strcmp(s.c_str(), std::get<0>(e)); } ); + if (std::end(mappingPhysicalPalette) == i) + { + throw sdbusplus::exception::InvalidEnumString(); + } + else + { + return std::get<1>(*i); + } +} + +std::string convertForMessage(Physical::Palette v) +{ + auto i = std::find_if( + std::begin(mappingPhysicalPalette), + std::end(mappingPhysicalPalette), + [v](auto& e){ return v == std::get<1>(e); }); + return std::get<0>(*i); +} + +const vtable::vtable_t Physical::_vtable[] = { + vtable::start(), + vtable::property("State", + details::Physical::_property_State + .data(), + _callback_get_State, + _callback_set_State, + vtable::property_::emits_change), + vtable::property("DutyOn", + details::Physical::_property_DutyOn + .data(), + _callback_get_DutyOn, + _callback_set_DutyOn, + vtable::property_::emits_change), + vtable::property("Color", + details::Physical::_property_Color + .data(), + _callback_get_Color, + _callback_set_Color, + vtable::property_::emits_change), + vtable::end() +}; + +} // namespace server +} // namespace Led +} // namespace openbmc_project +} // namespace xyz +} // namespace sdbusplus + diff --git a/xyz/openbmc_project/Led/Physical/server.hpp b/xyz/openbmc_project/Led/Physical/server.hpp new file mode 100644 index 0000000..2269f42 --- /dev/null +++ b/xyz/openbmc_project/Led/Physical/server.hpp @@ -0,0 +1,149 @@ +#pragma once +#include +#include +#include + +namespace sdbusplus +{ +namespace xyz +{ +namespace openbmc_project +{ +namespace Led +{ +namespace server +{ + +class Physical +{ + 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. + */ + Physical() = delete; + Physical(const Physical&) = delete; + Physical& operator=(const Physical&) = delete; + Physical(Physical&&) = default; + Physical& operator=(Physical&&) = default; + virtual ~Physical() = 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. + */ + Physical(bus::bus& bus, const char* path); + + enum class Action + { + Off, + On, + Blink, + }; + enum class Palette + { + Unknown, + Red, + Green, + Blue, + Yellow, + }; + + + + /** Get value of State */ + virtual Action state() const; + /** Set value of State */ + virtual Action state(Action value); + /** Get value of DutyOn */ + virtual uint8_t dutyOn() const; + /** Set value of DutyOn */ + virtual uint8_t dutyOn(uint8_t value); + /** Get value of Color */ + virtual Palette color() const; + /** Set value of Color */ + virtual Palette color(Palette value); + + /** @brief Convert a string to an appropriate enum value. + * @param[in] s - The string to convert in the form of + * "xyz.openbmc_project.Led.Physical." + * @return - The enum value. + */ + static Action convertActionFromString(std::string& s); + /** @brief Convert a string to an appropriate enum value. + * @param[in] s - The string to convert in the form of + * "xyz.openbmc_project.Led.Physical." + * @return - The enum value. + */ + static Palette convertPaletteFromString(std::string& s); + + private: + + /** @brief sd-bus callback for get-property 'State' */ + static int _callback_get_State( + sd_bus*, const char*, const char*, const char*, + sd_bus_message*, void*, sd_bus_error*); + /** @brief sd-bus callback for set-property 'State' */ + static int _callback_set_State( + sd_bus*, const char*, const char*, const char*, + sd_bus_message*, void*, sd_bus_error*); + + /** @brief sd-bus callback for get-property 'DutyOn' */ + static int _callback_get_DutyOn( + sd_bus*, const char*, const char*, const char*, + sd_bus_message*, void*, sd_bus_error*); + /** @brief sd-bus callback for set-property 'DutyOn' */ + static int _callback_set_DutyOn( + sd_bus*, const char*, const char*, const char*, + sd_bus_message*, void*, sd_bus_error*); + + /** @brief sd-bus callback for get-property 'Color' */ + static int _callback_get_Color( + sd_bus*, const char*, const char*, const char*, + sd_bus_message*, void*, sd_bus_error*); + /** @brief sd-bus callback for set-property 'Color' */ + static int _callback_set_Color( + sd_bus*, const char*, const char*, const char*, + sd_bus_message*, void*, sd_bus_error*); + + + static constexpr auto _interface = "xyz.openbmc_project.Led.Physical"; + static const vtable::vtable_t _vtable[]; + sdbusplus::server::interface::interface + _xyz_openbmc_project_Led_Physical_interface; + + Action _state = Action::Off; + uint8_t _dutyOn = 50; + Palette _color = Palette::Unknown; + +}; + +/* Specialization of sdbusplus::server::bindings::details::convertForMessage + * for enum-type Physical::Action. + * + * 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(Physical::Action e); +/* Specialization of sdbusplus::server::bindings::details::convertForMessage + * for enum-type Physical::Palette. + * + * 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(Physical::Palette e); + +} // namespace server +} // namespace Led +} // namespace openbmc_project +} // namespace xyz +} // namespace sdbusplus + -- cgit v1.2.1