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