blob: 7f9f236fb14cd270a9c67d93c607cf796c38e459 [file] [log] [blame]
Deepak Kodihallia646edd2017-07-25 07:28:45 -05001#include <phosphor-logging/elog-errors.hpp>
2#include <phosphor-logging/log.hpp>
Anthony Wilson32c532e2018-10-25 21:56:07 -05003#include <sdbusplus/exception.hpp>
Deepak Kodihallia646edd2017-07-25 07:28:45 -05004#include "xyz/openbmc_project/Common/error.hpp"
5#include "settings.hpp"
6
7namespace settings
8{
9
10using namespace phosphor::logging;
11using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Anthony Wilson32c532e2018-10-25 21:56:07 -050012using sdbusplus::exception::SdBusError;
Deepak Kodihallia646edd2017-07-25 07:28:45 -050013
14constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
15constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
16constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
17
Andrew Geissler58a18012018-01-19 19:36:05 -080018Objects::Objects(sdbusplus::bus::bus& bus) : bus(bus)
Deepak Kodihallia646edd2017-07-25 07:28:45 -050019{
Andrew Geissler58a18012018-01-19 19:36:05 -080020 std::vector<std::string> settingsIntfs = {autoRebootIntf, powerRestoreIntf};
Deepak Kodihallia646edd2017-07-25 07:28:45 -050021 auto depth = 0;
22
Andrew Geissler58a18012018-01-19 19:36:05 -080023 auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf,
Deepak Kodihallia646edd2017-07-25 07:28:45 -050024 "GetSubTree");
25 mapperCall.append(root);
26 mapperCall.append(depth);
27 mapperCall.append(settingsIntfs);
Deepak Kodihallia646edd2017-07-25 07:28:45 -050028
29 using Interfaces = std::vector<Interface>;
30 using MapperResponse = std::map<Path, std::map<Service, Interfaces>>;
31 MapperResponse result;
Anthony Wilson32c532e2018-10-25 21:56:07 -050032
33 try
Deepak Kodihallia646edd2017-07-25 07:28:45 -050034 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050035 auto response = bus.call(mapperCall);
36
37 response.read(result);
38 if (result.empty())
39 {
40 log<level::ERR>("Invalid response from mapper");
41 elog<InternalFailure>();
42 }
43 }
44 catch (const SdBusError& e)
45 {
46 log<level::ERR>("Error in mapper GetSubTree",
47 entry("ERROR=%s", e.what()));
Deepak Kodihallia646edd2017-07-25 07:28:45 -050048 elog<InternalFailure>();
49 }
50
51 for (const auto& iter : result)
52 {
53 const Path& path = iter.first;
Deepak Kodihallia646edd2017-07-25 07:28:45 -050054
Matt Spinler14e14cd2018-09-07 15:04:16 -050055 for (const auto& serviceIter : iter.second)
Deepak Kodihallia646edd2017-07-25 07:28:45 -050056 {
Matt Spinler14e14cd2018-09-07 15:04:16 -050057 for (const auto& interface : serviceIter.second)
58 {
59 if (autoRebootIntf == interface)
60 {
61 autoReboot = path;
62 }
63 else if (powerRestoreIntf == interface)
64 {
65 powerRestorePolicy = path;
66 }
67 }
Deepak Kodihallia646edd2017-07-25 07:28:45 -050068 }
69 }
70}
71
72Service Objects::service(const Path& path, const Interface& interface) const
73{
74 using Interfaces = std::vector<Interface>;
Andrew Geissler58a18012018-01-19 19:36:05 -080075 auto mapperCall =
76 bus.new_method_call(mapperService, mapperPath, mapperIntf, "GetObject");
Deepak Kodihallia646edd2017-07-25 07:28:45 -050077 mapperCall.append(path);
78 mapperCall.append(Interfaces({interface}));
79
Anthony Wilson32c532e2018-10-25 21:56:07 -050080 std::map<Service, Interfaces> result;
81
82 try
Deepak Kodihallia646edd2017-07-25 07:28:45 -050083 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050084 auto response = bus.call(mapperCall);
85 response.read(result);
86 }
87 catch (const SdBusError& e)
88 {
89 log<level::ERR>("Error in mapper GetObject",
90 entry("ERROR=%s", e.what()));
Deepak Kodihallia646edd2017-07-25 07:28:45 -050091 elog<InternalFailure>();
92 }
93
Deepak Kodihallia646edd2017-07-25 07:28:45 -050094 if (result.empty())
95 {
96 log<level::ERR>("Invalid response from mapper");
97 elog<InternalFailure>();
98 }
99
100 return result.begin()->first;
101}
102
103} // namespace settings