blob: f9f100f5a610b8ab75c3c0df1e879cc2407a40ac [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;
Anthony Wilson32c532e2018-10-25 21:56:07 -050014using sdbusplus::exception::SdBusError;
Deepak Kodihallia646edd2017-07-25 07:28:45 -050015
16constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
17constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
18constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
19
Andrew Geissler58a18012018-01-19 19:36:05 -080020Objects::Objects(sdbusplus::bus::bus& bus) : bus(bus)
Deepak Kodihallia646edd2017-07-25 07:28:45 -050021{
Andrew Geissler58a18012018-01-19 19:36:05 -080022 std::vector<std::string> settingsIntfs = {autoRebootIntf, powerRestoreIntf};
Deepak Kodihallia646edd2017-07-25 07:28:45 -050023 auto depth = 0;
24
Andrew Geissler58a18012018-01-19 19:36:05 -080025 auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf,
Deepak Kodihallia646edd2017-07-25 07:28:45 -050026 "GetSubTree");
27 mapperCall.append(root);
28 mapperCall.append(depth);
29 mapperCall.append(settingsIntfs);
Deepak Kodihallia646edd2017-07-25 07:28:45 -050030
31 using Interfaces = std::vector<Interface>;
32 using MapperResponse = std::map<Path, std::map<Service, Interfaces>>;
33 MapperResponse result;
Anthony Wilson32c532e2018-10-25 21:56:07 -050034
35 try
Deepak Kodihallia646edd2017-07-25 07:28:45 -050036 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050037 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()));
Deepak Kodihallia646edd2017-07-25 07:28:45 -050050 elog<InternalFailure>();
51 }
52
53 for (const auto& iter : result)
54 {
55 const Path& path = iter.first;
Deepak Kodihallia646edd2017-07-25 07:28:45 -050056
Matt Spinler14e14cd2018-09-07 15:04:16 -050057 for (const auto& serviceIter : iter.second)
Deepak Kodihallia646edd2017-07-25 07:28:45 -050058 {
Matt Spinler14e14cd2018-09-07 15:04:16 -050059 for (const auto& interface : serviceIter.second)
60 {
61 if (autoRebootIntf == interface)
62 {
Andrew Geisslere87db702020-10-22 19:33:26 -050063 /* 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 }
Matt Spinler14e14cd2018-09-07 15:04:16 -050077 }
78 else if (powerRestoreIntf == interface)
79 {
Andrew Geissler35ca2e32021-02-09 13:54:26 -060080 /* 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 }
Matt Spinler14e14cd2018-09-07 15:04:16 -050094 }
95 }
Deepak Kodihallia646edd2017-07-25 07:28:45 -050096 }
97 }
98}
99
100Service Objects::service(const Path& path, const Interface& interface) const
101{
102 using Interfaces = std::vector<Interface>;
Andrew Geissler58a18012018-01-19 19:36:05 -0800103 auto mapperCall =
104 bus.new_method_call(mapperService, mapperPath, mapperIntf, "GetObject");
Deepak Kodihallia646edd2017-07-25 07:28:45 -0500105 mapperCall.append(path);
106 mapperCall.append(Interfaces({interface}));
107
Anthony Wilson32c532e2018-10-25 21:56:07 -0500108 std::map<Service, Interfaces> result;
109
110 try
Deepak Kodihallia646edd2017-07-25 07:28:45 -0500111 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500112 auto response = bus.call(mapperCall);
113 response.read(result);
114 }
115 catch (const SdBusError& e)
116 {
117 log<level::ERR>("Error in mapper GetObject",
118 entry("ERROR=%s", e.what()));
Deepak Kodihallia646edd2017-07-25 07:28:45 -0500119 elog<InternalFailure>();
120 }
121
Deepak Kodihallia646edd2017-07-25 07:28:45 -0500122 if (result.empty())
123 {
124 log<level::ERR>("Invalid response from mapper");
125 elog<InternalFailure>();
126 }
127
128 return result.begin()->first;
129}
130
131} // namespace settings