Add API to retrieve settings
Add API to retrieve all settings objects of interest to this repo, in
one shot.
Change-Id: Ie74d39f4d6b1d262e6e5721208efc94003eee46c
Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
diff --git a/settings.cpp b/settings.cpp
new file mode 100644
index 0000000..98254dd
--- /dev/null
+++ b/settings.cpp
@@ -0,0 +1,91 @@
+#include <phosphor-logging/elog-errors.hpp>
+#include <phosphor-logging/log.hpp>
+#include "xyz/openbmc_project/Common/error.hpp"
+#include "settings.hpp"
+
+namespace settings
+{
+
+using namespace phosphor::logging;
+using namespace sdbusplus::xyz::openbmc_project::Common::Error;
+
+constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
+constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
+constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
+
+Objects::Objects(sdbusplus::bus::bus& bus):
+ bus(bus)
+{
+ std::vector<std::string> settingsIntfs =
+ {autoRebootIntf, powerRestoreIntf};
+ auto depth = 0;
+
+ auto mapperCall = bus.new_method_call(mapperService,
+ mapperPath,
+ mapperIntf,
+ "GetSubTree");
+ mapperCall.append(root);
+ mapperCall.append(depth);
+ mapperCall.append(settingsIntfs);
+ auto response = bus.call(mapperCall);
+ if (response.is_method_error())
+ {
+ log<level::ERR>("Error in mapper GetSubTree");
+ elog<InternalFailure>();
+ }
+
+ using Interfaces = std::vector<Interface>;
+ using MapperResponse = std::map<Path, std::map<Service, Interfaces>>;
+ MapperResponse result;
+ response.read(result);
+ if (result.empty())
+ {
+ log<level::ERR>("Invalid response from mapper");
+ elog<InternalFailure>();
+ }
+
+ for (const auto& iter : result)
+ {
+ const Path& path = iter.first;
+ const Interface& interface = iter.second.begin()->second[0];
+
+ if (autoRebootIntf == interface)
+ {
+ autoReboot = path;
+ }
+ else if (powerRestoreIntf == interface)
+ {
+ powerRestorePolicy = path;
+ }
+ }
+}
+
+Service Objects::service(const Path& path, const Interface& interface) const
+{
+ using Interfaces = std::vector<Interface>;
+ auto mapperCall = bus.new_method_call(mapperService,
+ mapperPath,
+ mapperIntf,
+ "GetObject");
+ mapperCall.append(path);
+ mapperCall.append(Interfaces({interface}));
+
+ auto response = bus.call(mapperCall);
+ if (response.is_method_error())
+ {
+ log<level::ERR>("Error in mapper GetObject");
+ elog<InternalFailure>();
+ }
+
+ std::map<Service, Interfaces> result;
+ response.read(result);
+ if (result.empty())
+ {
+ log<level::ERR>("Invalid response from mapper");
+ elog<InternalFailure>();
+ }
+
+ return result.begin()->first;
+}
+
+} // namespace settings
diff --git a/settings.hpp b/settings.hpp
new file mode 100644
index 0000000..088628a
--- /dev/null
+++ b/settings.hpp
@@ -0,0 +1,57 @@
+#pragma once
+
+#include <string>
+#include <sdbusplus/bus.hpp>
+
+namespace settings
+{
+
+using Path = std::string;
+using Service = std::string;
+using Interface = std::string;
+
+constexpr auto root = "/";
+constexpr auto autoRebootIntf =
+ "xyz.openbmc_project.Control.Boot.RebootPolicy";
+constexpr auto powerRestoreIntf =
+ "xyz.openbmc_project.Control.Power.RestorePolicy";
+
+/** @class Objects
+ * @brief Fetch paths of settings d-bus objects of interest, upon construction
+ */
+struct Objects
+{
+ public:
+ /** @brief Constructor - fetch settings objects
+ *
+ * @param[in] bus - The Dbus bus object
+ */
+ Objects(sdbusplus::bus::bus& bus);
+ Objects(const Objects&) = delete;
+ Objects& operator=(const Objects&) = delete;
+ Objects(Objects&&) = delete;
+ Objects& operator=(Objects&&) = delete;
+ ~Objects() = default;
+
+ /** @brief Fetch d-bus service, given a path and an interface. The
+ * service can't be cached because mapper returns unique
+ * service names.
+ *
+ * @param[in] path - The Dbus object
+ * @param[in] interface - The Dbus interface
+ *
+ * @return std::string - the dbus service name
+ */
+ Service service(const Path& path, const Interface& interface) const;
+
+ /** @brief host auto_reboot settings object */
+ Path autoReboot;
+
+ /** @brief host power_restore_policy settings object */
+ Path powerRestorePolicy;
+
+ /** @brief The Dbus bus object */
+ sdbusplus::bus::bus& bus;
+};
+
+} // namespace settings