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/lg2.hpp>
7 #include <sdbusplus/exception.hpp>
8 
9 namespace settings
10 {
11 
12 PHOSPHOR_LOG2_USING;
13 
14 using namespace phosphor::logging;
15 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
16 
17 constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
18 constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
19 constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
20 
21 Objects::Objects(sdbusplus::bus::bus& bus) : bus(bus)
22 {
23     std::vector<std::string> settingsIntfs = {autoRebootIntf, powerRestoreIntf};
24     auto depth = 0;
25 
26     auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf,
27                                           "GetSubTree");
28     mapperCall.append(root);
29     mapperCall.append(depth);
30     mapperCall.append(settingsIntfs);
31 
32     using Interfaces = std::vector<Interface>;
33     using MapperResponse = std::map<Path, std::map<Service, Interfaces>>;
34     MapperResponse result;
35 
36     try
37     {
38         auto response = bus.call(mapperCall);
39 
40         response.read(result);
41         if (result.empty())
42         {
43             error("Invalid response from mapper");
44             elog<InternalFailure>();
45         }
46     }
47     catch (const sdbusplus::exception::exception& e)
48     {
49         error("Error in mapper GetSubTree: {ERROR}", "ERROR", e);
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                     /* There are two implementations of the AutoReboot
64                      * Interface. A persistent user setting and a one-time
65                      * setting which is only valid for one boot of the system.
66                      * The one-time setting will have "one_time" in its
67                      * object path.
68                      */
69                     if (path.find("one_time") != std::string::npos)
70                     {
71                         autoRebootOneTime = path;
72                     }
73                     else
74                     {
75                         autoReboot = path;
76                     }
77                 }
78                 else if (powerRestoreIntf == interface)
79                 {
80                     /* There are two implementations of the PowerRestorePolicy
81                      * Interface. A persistent user setting and a one-time
82                      * setting which is only valid for one boot of the system.
83                      * The one-time setting will have "one_time" in its
84                      * object path.
85                      */
86                     if (path.find("one_time") != std::string::npos)
87                     {
88                         powerRestorePolicyOneTime = path;
89                     }
90                     else
91                     {
92                         powerRestorePolicy = path;
93                     }
94                 }
95             }
96         }
97     }
98 }
99 
100 Service Objects::service(const Path& path, const Interface& interface) const
101 {
102     using Interfaces = std::vector<Interface>;
103     auto mapperCall =
104         bus.new_method_call(mapperService, mapperPath, mapperIntf, "GetObject");
105     mapperCall.append(path);
106     mapperCall.append(Interfaces({interface}));
107 
108     std::map<Service, Interfaces> result;
109 
110     try
111     {
112         auto response = bus.call(mapperCall);
113         response.read(result);
114     }
115     catch (const sdbusplus::exception::exception& e)
116     {
117         error("Error in mapper GetObject: {ERROR}", "ERROR", e);
118         elog<InternalFailure>();
119     }
120 
121     if (result.empty())
122     {
123         error("Invalid response from mapper");
124         elog<InternalFailure>();
125     }
126 
127     return result.begin()->first;
128 }
129 
130 } // namespace settings
131