blob: 0445092394de4af9c50de01686ae51bffdd23e9d [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>
Patrick Williams9a286db2024-01-17 06:29:47 -06008#include <xyz/openbmc_project/ObjectMapper/client.hpp>
Deepak Kodihallia646edd2017-07-25 07:28:45 -05009
10namespace settings
11{
12
Andrew Geissler8ffdb262021-09-20 15:25:19 -050013PHOSPHOR_LOG2_USING;
14
Deepak Kodihallia646edd2017-07-25 07:28:45 -050015using namespace phosphor::logging;
16using namespace sdbusplus::xyz::openbmc_project::Common::Error;
17
Patrick Williams9a286db2024-01-17 06:29:47 -060018using ObjectMapper = sdbusplus::client::xyz::openbmc_project::ObjectMapper<>;
Deepak Kodihallia646edd2017-07-25 07:28:45 -050019
Patrick Williamsf053e6f2022-07-22 19:26:54 -050020Objects::Objects(sdbusplus::bus_t& bus, const Path& root) : 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
Patrick Williams9a286db2024-01-17 06:29:47 -060025 auto mapperCall = bus.new_method_call(
26 ObjectMapper::default_service, ObjectMapper::instance_path,
27 ObjectMapper::interface, "GetSubTree");
Deepak Kodihallia646edd2017-07-25 07:28:45 -050028 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>;
Patrick Williams1b2c3c02024-08-16 15:20:29 -0400103 auto mapperCall = bus.new_method_call(
104 ObjectMapper::default_service, ObjectMapper::instance_path,
105 ObjectMapper::interface, "GetObject");
Deepak Kodihallia646edd2017-07-25 07:28:45 -0500106 mapperCall.append(path);
107 mapperCall.append(Interfaces({interface}));
108
Anthony Wilson32c532e2018-10-25 21:56:07 -0500109 std::map<Service, Interfaces> result;
110
111 try
Deepak Kodihallia646edd2017-07-25 07:28:45 -0500112 {
Anthony Wilson32c532e2018-10-25 21:56:07 -0500113 auto response = bus.call(mapperCall);
114 response.read(result);
115 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500116 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500117 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500118 error("Error in mapper GetObject: {ERROR}", "ERROR", e);
Deepak Kodihallia646edd2017-07-25 07:28:45 -0500119 elog<InternalFailure>();
120 }
121
Deepak Kodihallia646edd2017-07-25 07:28:45 -0500122 if (result.empty())
123 {
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500124 error("Invalid response from mapper");
Deepak Kodihallia646edd2017-07-25 07:28:45 -0500125 elog<InternalFailure>();
126 }
127
128 return result.begin()->first;
129}
130
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500131HostObjects::HostObjects(sdbusplus::bus_t& bus, size_t id) :
Potin Laic328a4c2022-03-18 23:49:37 +0800132 Objects(bus, Path("/xyz/openbmc_project/control/host") + std::to_string(id))
133{}
134
Deepak Kodihallia646edd2017-07-25 07:28:45 -0500135} // namespace settings