blob: 9a7879a3bba041a089586f3241ba1cfa459aaa3c [file] [log] [blame]
Andrew Geisslere426b582020-05-28 12:40:55 -05001#include "settings.hpp"
2
3#include "xyz/openbmc_project/Common/error.hpp"
4
Deepak Kodihallia646edd2017-07-25 07:28:45 -05005#include <phosphor-logging/elog-errors.hpp>
6#include <phosphor-logging/log.hpp>
Anthony Wilson32c532e2018-10-25 21:56:07 -05007#include <sdbusplus/exception.hpp>
Deepak Kodihallia646edd2017-07-25 07:28:45 -05008
9namespace settings
10{
11
12using namespace phosphor::logging;
13using namespace sdbusplus::xyz::openbmc_project::Common::Error;
14
15constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
16constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
17constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
18
Andrew Geissler58a18012018-01-19 19:36:05 -080019Objects::Objects(sdbusplus::bus::bus& bus) : bus(bus)
Deepak Kodihallia646edd2017-07-25 07:28:45 -050020{
Andrew Geissler58a18012018-01-19 19:36:05 -080021 std::vector<std::string> settingsIntfs = {autoRebootIntf, powerRestoreIntf};
Deepak Kodihallia646edd2017-07-25 07:28:45 -050022 auto depth = 0;
23
Andrew Geissler58a18012018-01-19 19:36:05 -080024 auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf,
Deepak Kodihallia646edd2017-07-25 07:28:45 -050025 "GetSubTree");
26 mapperCall.append(root);
27 mapperCall.append(depth);
28 mapperCall.append(settingsIntfs);
Deepak Kodihallia646edd2017-07-25 07:28:45 -050029
30 using Interfaces = std::vector<Interface>;
31 using MapperResponse = std::map<Path, std::map<Service, Interfaces>>;
32 MapperResponse result;
Anthony Wilson32c532e2018-10-25 21:56:07 -050033
34 try
Deepak Kodihallia646edd2017-07-25 07:28:45 -050035 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050036 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 }
Patrick Williams0a675212021-09-02 09:49:43 -050045 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -050046 {
47 log<level::ERR>("Error in mapper GetSubTree",
48 entry("ERROR=%s", e.what()));
Deepak Kodihallia646edd2017-07-25 07:28:45 -050049 elog<InternalFailure>();
50 }
51
52 for (const auto& iter : result)
53 {
54 const Path& path = iter.first;
Deepak Kodihallia646edd2017-07-25 07:28:45 -050055
Matt Spinler14e14cd2018-09-07 15:04:16 -050056 for (const auto& serviceIter : iter.second)
Deepak Kodihallia646edd2017-07-25 07:28:45 -050057 {
Matt Spinler14e14cd2018-09-07 15:04:16 -050058 for (const auto& interface : serviceIter.second)
59 {
60 if (autoRebootIntf == interface)
61 {
Andrew Geisslere87db702020-10-22 19:33:26 -050062 /* 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 }
Matt Spinler14e14cd2018-09-07 15:04:16 -050076 }
77 else if (powerRestoreIntf == interface)
78 {
Andrew Geissler35ca2e32021-02-09 13:54:26 -060079 /* 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 }
Matt Spinler14e14cd2018-09-07 15:04:16 -050093 }
94 }
Deepak Kodihallia646edd2017-07-25 07:28:45 -050095 }
96 }
97}
98
99Service Objects::service(const Path& path, const Interface& interface) const
100{
101 using Interfaces = std::vector<Interface>;
Andrew Geissler58a18012018-01-19 19:36:05 -0800102 auto mapperCall =
103 bus.new_method_call(mapperService, mapperPath, mapperIntf, "GetObject");
Deepak Kodihallia646edd2017-07-25 07:28:45 -0500104 mapperCall.append(path);
105 mapperCall.append(Interfaces({interface}));
106
Anthony Wilson32c532e2018-10-25 21:56:07 -0500107 std::map<Service, Interfaces> result;
108
109 try
Deepak Kodihallia646edd2017-07-25 07:28:45 -0500110 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500111 auto response = bus.call(mapperCall);
112 response.read(result);
113 }
Patrick Williams0a675212021-09-02 09:49:43 -0500114 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500115 {
116 log<level::ERR>("Error in mapper GetObject",
117 entry("ERROR=%s", e.what()));
Deepak Kodihallia646edd2017-07-25 07:28:45 -0500118 elog<InternalFailure>();
119 }
120
Deepak Kodihallia646edd2017-07-25 07:28:45 -0500121 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