1 #include "settings.hpp"
2 
3 #include "xyz/openbmc_project/Common/error.hpp"
4 
5 #include <phosphor-logging/elog-errors.hpp>
6 #include <phosphor-logging/log.hpp>
7 #include <sdbusplus/exception.hpp>
8 
9 namespace settings
10 {
11 
12 using namespace phosphor::logging;
13 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
14 using sdbusplus::exception::SdBusError;
15 
16 constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
17 constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
18 constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
19 
20 Objects::Objects(sdbusplus::bus::bus& bus) : bus(bus)
21 {
22     std::vector<std::string> settingsIntfs = {autoRebootIntf, powerRestoreIntf};
23     auto depth = 0;
24 
25     auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf,
26                                           "GetSubTree");
27     mapperCall.append(root);
28     mapperCall.append(depth);
29     mapperCall.append(settingsIntfs);
30 
31     using Interfaces = std::vector<Interface>;
32     using MapperResponse = std::map<Path, std::map<Service, Interfaces>>;
33     MapperResponse result;
34 
35     try
36     {
37         auto response = bus.call(mapperCall);
38 
39         response.read(result);
40         if (result.empty())
41         {
42             log<level::ERR>("Invalid response from mapper");
43             elog<InternalFailure>();
44         }
45     }
46     catch (const SdBusError& e)
47     {
48         log<level::ERR>("Error in mapper GetSubTree",
49                         entry("ERROR=%s", e.what()));
50         elog<InternalFailure>();
51     }
52 
53     for (const auto& iter : result)
54     {
55         const Path& path = iter.first;
56 
57         for (const auto& serviceIter : iter.second)
58         {
59             for (const auto& interface : serviceIter.second)
60             {
61                 if (autoRebootIntf == interface)
62                 {
63                     autoReboot = path;
64                 }
65                 else if (powerRestoreIntf == interface)
66                 {
67                     powerRestorePolicy = path;
68                 }
69             }
70         }
71     }
72 }
73 
74 Service Objects::service(const Path& path, const Interface& interface) const
75 {
76     using Interfaces = std::vector<Interface>;
77     auto mapperCall =
78         bus.new_method_call(mapperService, mapperPath, mapperIntf, "GetObject");
79     mapperCall.append(path);
80     mapperCall.append(Interfaces({interface}));
81 
82     std::map<Service, Interfaces> result;
83 
84     try
85     {
86         auto response = bus.call(mapperCall);
87         response.read(result);
88     }
89     catch (const SdBusError& e)
90     {
91         log<level::ERR>("Error in mapper GetObject",
92                         entry("ERROR=%s", e.what()));
93         elog<InternalFailure>();
94     }
95 
96     if (result.empty())
97     {
98         log<level::ERR>("Invalid response from mapper");
99         elog<InternalFailure>();
100     }
101 
102     return result.begin()->first;
103 }
104 
105 } // namespace settings
106