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 #include <xyz/openbmc_project/ObjectMapper/client.hpp>
9
10 namespace settings
11 {
12
13 PHOSPHOR_LOG2_USING;
14
15 using namespace phosphor::logging;
16 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
17
18 using ObjectMapper = sdbusplus::client::xyz::openbmc_project::ObjectMapper<>;
19
Objects(sdbusplus::bus_t & bus,const Path & root)20 Objects::Objects(sdbusplus::bus_t& bus, const Path& root) : bus(bus)
21 {
22 std::vector<std::string> settingsIntfs = {autoRebootIntf, powerRestoreIntf};
23 auto depth = 0;
24
25 auto mapperCall = bus.new_method_call(
26 ObjectMapper::default_service, ObjectMapper::instance_path,
27 ObjectMapper::interface, "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_t& 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
service(const Path & path,const Interface & interface) const100 Service Objects::service(const Path& path, const Interface& interface) const
101 {
102 using Interfaces = std::vector<Interface>;
103 auto mapperCall = bus.new_method_call(
104 ObjectMapper::default_service, ObjectMapper::instance_path,
105 ObjectMapper::interface, "GetObject");
106 mapperCall.append(path);
107 mapperCall.append(Interfaces({interface}));
108
109 std::map<Service, Interfaces> result;
110
111 try
112 {
113 auto response = bus.call(mapperCall);
114 response.read(result);
115 }
116 catch (const sdbusplus::exception_t& e)
117 {
118 error("Error in mapper GetObject: {ERROR}", "ERROR", e);
119 elog<InternalFailure>();
120 }
121
122 if (result.empty())
123 {
124 error("Invalid response from mapper");
125 elog<InternalFailure>();
126 }
127
128 return result.begin()->first;
129 }
130
HostObjects(sdbusplus::bus_t & bus,size_t id)131 HostObjects::HostObjects(sdbusplus::bus_t& bus, size_t id) :
132 Objects(bus, Path("/xyz/openbmc_project/control/host") + std::to_string(id))
133 {}
134
135 } // namespace settings
136