blob: ac996606c4f516302aabec4bedd86e1389fae3d1 [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>
Andrew Geissler8ffdb262021-09-20 15:25:19 -05006#include <phosphor-logging/lg2.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
Andrew Geissler8ffdb262021-09-20 15:25:19 -050012PHOSPHOR_LOG2_USING;
13
Deepak Kodihallia646edd2017-07-25 07:28:45 -050014using namespace phosphor::logging;
15using namespace sdbusplus::xyz::openbmc_project::Common::Error;
16
17constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
18constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
19constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
20
Patrick Williamsf053e6f2022-07-22 19:26:54 -050021Objects::Objects(sdbusplus::bus_t& bus, const Path& root) : bus(bus)
Deepak Kodihallia646edd2017-07-25 07:28:45 -050022{
Andrew Geissler58a18012018-01-19 19:36:05 -080023 std::vector<std::string> settingsIntfs = {autoRebootIntf, powerRestoreIntf};
Deepak Kodihallia646edd2017-07-25 07:28:45 -050024 auto depth = 0;
25
Andrew Geissler58a18012018-01-19 19:36:05 -080026 auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf,
Deepak Kodihallia646edd2017-07-25 07:28:45 -050027 "GetSubTree");
28 mapperCall.append(root);
29 mapperCall.append(depth);
30 mapperCall.append(settingsIntfs);
Deepak Kodihallia646edd2017-07-25 07:28:45 -050031
32 using Interfaces = std::vector<Interface>;
33 using MapperResponse = std::map<Path, std::map<Service, Interfaces>>;
34 MapperResponse result;
Anthony Wilson32c532e2018-10-25 21:56:07 -050035
36 try
Deepak Kodihallia646edd2017-07-25 07:28:45 -050037 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050038 auto response = bus.call(mapperCall);
39
40 response.read(result);
41 if (result.empty())
42 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050043 error("Invalid response from mapper");
Anthony Wilson32c532e2018-10-25 21:56:07 -050044 elog<InternalFailure>();
45 }
46 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -050047 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -050048 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -050049 error("Error in mapper GetSubTree: {ERROR}", "ERROR", e);
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 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500115 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500116 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500117 error("Error in mapper GetObject: {ERROR}", "ERROR", e);
Deepak Kodihallia646edd2017-07-25 07:28:45 -0500118 elog<InternalFailure>();
119 }
120
Deepak Kodihallia646edd2017-07-25 07:28:45 -0500121 if (result.empty())
122 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500123 error("Invalid response from mapper");
Deepak Kodihallia646edd2017-07-25 07:28:45 -0500124 elog<InternalFailure>();
125 }
126
127 return result.begin()->first;
128}
129
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500130HostObjects::HostObjects(sdbusplus::bus_t& bus, size_t id) :
Potin Laic328a4c2022-03-18 23:49:37 +0800131 Objects(bus, Path("/xyz/openbmc_project/control/host") + std::to_string(id))
132{}
133
Deepak Kodihallia646edd2017-07-25 07:28:45 -0500134} // namespace settings