From 76794495882082db189e2fc6d6c05dfe3756338a Mon Sep 17 00:00:00 2001 From: Deepak Kodihalli Date: Thu, 16 Feb 2017 23:48:18 -0600 Subject: refactor: move types and utils in their own files Change-Id: I477dd69f0c6c648b63171dc2db264d3e70dafaf4 Signed-off-by: Deepak Kodihalli --- Makefile.am | 7 +++-- test/Makefile.am | 3 ++- types.hpp | 23 +++++++++++++++++ utils.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ utils.hpp | 26 +++++++++++++++++++ writefru.mako.hpp | 76 ++----------------------------------------------------- 6 files changed, 131 insertions(+), 77 deletions(-) create mode 100644 utils.cpp create mode 100644 utils.hpp diff --git a/Makefile.am b/Makefile.am index 89c3cc9..de3232a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -6,7 +6,9 @@ noinst_HEADERS = \ types.hpp \ write.hpp \ impl.hpp \ - args.hpp + args.hpp \ + types.hpp \ + utils.hpp # Be sure to build writefru.hpp before compiling BUILT_SOURCES = writefru.hpp @@ -27,7 +29,8 @@ openpower_read_vpd_SOURCES = \ args.cpp \ impl.cpp \ parser.cpp \ - write.cpp + write.cpp \ + utils.cpp openpower_read_vpd_LDFLAGS = $(SDBUSPLUS_LIBS) $(PHOSPHOR_LOGGING_LIBS) openpower_read_vpd_CXXFLAGS = $(SDBUSPLUS_CFLAGS) $(PHOSPHOR_LOGGING_CFLAGS) diff --git a/test/Makefile.am b/test/Makefile.am index 0eb18ac..4e69969 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -12,6 +12,7 @@ parser_test_SOURCES = \ parser/parser.cpp \ ../impl.cpp \ ../parser.cpp \ - ../write.cpp + ../write.cpp \ + ../utils.cpp parser_test_LDFLAGS = $(SDBUSPLUS_LIBS) $(PHOSPHOR_LOGGING_LIBS) parser_test_CXXFLAGS = $(SDBUSPLUS_CFLAGS) $(PHOSPHOR_LOGGING_CFLAGS) diff --git a/types.hpp b/types.hpp index 7e56eb4..d65aa70 100644 --- a/types.hpp +++ b/types.hpp @@ -2,6 +2,9 @@ #include #include +#include +#include +#include namespace openpower { @@ -14,5 +17,25 @@ static_assert((8 == CHAR_BIT), "A byte is not 8 bits!"); using Byte = uint8_t; using Binary = std::vector; +namespace inventory +{ + +using Path = std::string; +using Property = std::string; +using Value = sdbusplus::message::variant; +using PropertyMap = std::map; + +using Interface = std::string; +using InterfaceMap = std::map; + +using Object = sdbusplus::message::object_path; +using ObjectMap = std::map; + +using namespace std::string_literals; +static const auto pimPath = "/xyz/openbmc_project/Inventory"s; +static const auto pimIntf = "xyz.openbmc_project.Inventory.Manager"s; + +} // namespace inventory + } // namespace vpd } // namespace openpower diff --git a/utils.cpp b/utils.cpp new file mode 100644 index 0000000..4226d44 --- /dev/null +++ b/utils.cpp @@ -0,0 +1,73 @@ +#include "utils.hpp" +#include +#include +#include + +namespace openpower +{ +namespace vpd +{ + +namespace inventory +{ + +auto getPIMService() +{ + auto bus = sdbusplus::bus::new_default(); + auto mapper = + bus.new_method_call( + "xyz.openbmc_project.ObjectMapper", + "/xyz/openbmc_project/ObjectMapper", + "xyz.openbmc_project.ObjectMapper", + "GetObject"); + + mapper.append(pimPath); + mapper.append(std::vector({pimIntf})); + + auto result = bus.call(mapper); + if(result.is_method_error()) + { + throw std::runtime_error("ObjectMapper GetObject failed"); + } + + std::map> response; + result.read(response); + if(response.empty()) + { + throw std::runtime_error("ObjectMapper GetObject bad response"); + } + + return response.begin()->first; +} + +void callPIM(ObjectMap&& objects) +{ + std::string service; + + try + { + service = getPIMService(); + auto bus = sdbusplus::bus::new_default(); + auto pimMsg = bus.new_method_call( + service.c_str(), + pimPath.c_str(), + pimIntf.c_str(), + "Notify"); + pimMsg.append(std::move(objects)); + auto result = bus.call(pimMsg); + if(result.is_method_error()) + { + std::cerr << "PIM Notify() failed\n"; + } + } + catch (const std::runtime_error& e) + { + using namespace phosphor::logging; + log(e.what()); + } +} + +} // namespace inventory + +} //namespace vpd +} //namespace openpower diff --git a/utils.hpp b/utils.hpp new file mode 100644 index 0000000..0aeeb32 --- /dev/null +++ b/utils.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include "types.hpp" + +namespace openpower +{ +namespace vpd +{ + +namespace inventory +{ + +/** @brief Get inventory-manager's d-bus service + */ +auto getPIMService(); + +/** @brief Call inventory-manager to add objects + * + * @param [in] objects - Map of inventory object paths + */ +void callPIM(ObjectMap&& objects); + +} // namespace inventory + +} //namespace vpd +} //namespace openpower diff --git a/writefru.mako.hpp b/writefru.mako.hpp index 7066f9a..9275e9f 100755 --- a/writefru.mako.hpp +++ b/writefru.mako.hpp @@ -7,10 +7,10 @@ #include #include -#include -#include #include "defines.hpp" #include "store.hpp" +#include "types.hpp" +#include "utils.hpp" namespace openpower { @@ -19,78 +19,6 @@ namespace vpd namespace inventory { -using Property = std::string; -using Value = sdbusplus::message::variant; -using PropertyMap = std::map; - -using Interface = std::string; -using InterfaceMap = std::map; - -using Object = sdbusplus::message::object_path; -using ObjectMap = std::map; - -using namespace std::string_literals; -static const auto pimPath = "/xyz/openbmc_project/Inventory"s; -static const auto pimIntf = "xyz.openbmc_project.Inventory.Manager"s; - -/** @brief Get inventory-manager's d-bus service - */ -auto getPIMService() -{ - auto bus = sdbusplus::bus::new_default(); - auto mapper = - bus.new_method_call( - "xyz.openbmc_project.ObjectMapper", - "/xyz/openbmc_project/ObjectMapper", - "xyz.openbmc_project.ObjectMapper", - "GetObject"); - - mapper.append(pimPath); - mapper.append(std::vector({pimIntf})); - - auto result = bus.call(mapper); - if(result.is_method_error()) - { - throw std::runtime_error("ObjectMapper GetObject failed"); - } - - std::map> response; - result.read(response); - if(response.empty()) - { - throw std::runtime_error("ObjectMapper GetObject bad response"); - } - - return response.begin()->first; -} - -auto callPIM(ObjectMap&& objects) -{ - std::string service; - - try - { - service = getPIMService(); - auto bus = sdbusplus::bus::new_default(); - auto pimMsg = bus.new_method_call( - service.c_str(), - pimPath.c_str(), - pimIntf.c_str(), - "Notify"); - pimMsg.append(std::move(objects)); - auto result = bus.call(pimMsg); - if(result.is_method_error()) - { - std::cerr << "PIM Notify() failed\n"; - } - } - catch (const std::runtime_error& e) - { - using namespace phosphor::logging; - log(e.what()); - } -} - /** @brief API to write parsed VPD to inventory, * for a specifc FRU * -- cgit v1.2.1