blob: 2b6915c7864d77e88626bb206a176ad14fef087a [file] [log] [blame]
Vernon Mauery9e801a22018-10-12 13:20:49 -07001#include "settings.hpp"
2
Tom Joseph19b4f402017-08-02 17:59:30 +05303#include <phosphor-logging/elog-errors.hpp>
4#include <phosphor-logging/log.hpp>
Vernon Maueryf41a5542018-10-15 16:04:17 -07005#include <xyz/openbmc_project/Common/error.hpp>
Tom Joseph19b4f402017-08-02 17:59:30 +05306
7namespace settings
8{
9
10using namespace phosphor::logging;
11using namespace sdbusplus::xyz::openbmc_project::Common::Error;
12
13constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
14constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
15constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
16
17Objects::Objects(sdbusplus::bus::bus& bus,
Vernon Mauery9e801a22018-10-12 13:20:49 -070018 const std::vector<Interface>& filter) :
Tom Joseph19b4f402017-08-02 17:59:30 +053019 bus(bus)
20{
21 auto depth = 0;
22
Vernon Mauery9e801a22018-10-12 13:20:49 -070023 auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf,
Tom Joseph19b4f402017-08-02 17:59:30 +053024 "GetSubTree");
25 mapperCall.append(root);
26 mapperCall.append(depth);
27 mapperCall.append(filter);
28 auto response = bus.call(mapperCall);
29 if (response.is_method_error())
30 {
31 log<level::ERR>("Error in mapper GetSubTree");
32 elog<InternalFailure>();
33 }
34
35 using Interfaces = std::vector<Interface>;
36 using MapperResponse = std::map<Path, std::map<Service, Interfaces>>;
37 MapperResponse result;
38 response.read(result);
39 if (result.empty())
40 {
41 log<level::ERR>("Invalid response from mapper");
42 elog<InternalFailure>();
43 }
44
45 for (auto& iter : result)
46 {
47 const auto& path = iter.first;
Tom Joseph7c78adf2017-09-11 12:52:56 +053048 for (auto& interface : iter.second.begin()->second)
49 {
50 auto found = map.find(interface);
51 if (map.end() != found)
52 {
53 auto& paths = found->second;
54 paths.push_back(path);
55 }
56 else
57 {
58 map.emplace(std::move(interface), std::vector<Path>({path}));
59 }
60 }
Tom Joseph19b4f402017-08-02 17:59:30 +053061 }
62}
63
64Service Objects::service(const Path& path, const Interface& interface) const
65{
66 using Interfaces = std::vector<Interface>;
Vernon Mauery9e801a22018-10-12 13:20:49 -070067 auto mapperCall =
68 bus.new_method_call(mapperService, mapperPath, mapperIntf, "GetObject");
Tom Joseph19b4f402017-08-02 17:59:30 +053069 mapperCall.append(path);
70 mapperCall.append(Interfaces({interface}));
71
72 auto response = bus.call(mapperCall);
73 if (response.is_method_error())
74 {
75 log<level::ERR>("Error in mapper GetObject");
76 elog<InternalFailure>();
77 }
78
79 std::map<Service, Interfaces> result;
80 response.read(result);
81 if (result.empty())
82 {
83 log<level::ERR>("Invalid response from mapper");
84 elog<InternalFailure>();
85 }
86
87 return result.begin()->first;
88}
89
Tom Joseph9c60a582017-09-13 17:10:46 +053090namespace boot
91{
92
93std::tuple<Path, OneTimeEnabled> setting(const Objects& objects,
94 const Interface& iface)
95{
96 constexpr auto bootObjCount = 2;
97 constexpr auto oneTime = "one_time";
98 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
99 constexpr auto propIntf = "org.freedesktop.DBus.Properties";
100
101 const std::vector<Path>& paths = objects.map.at(iface);
102 auto count = paths.size();
103 if (count != bootObjCount)
104 {
105 log<level::ERR>("Exactly two objects expected",
106 entry("INTERFACE=%s", iface.c_str()),
107 entry("COUNT=%d", count));
108 elog<InternalFailure>();
109 }
110 size_t index = 0;
111 if (std::string::npos == paths[0].rfind(oneTime))
112 {
113 index = 1;
114 }
115 const Path& oneTimeSetting = paths[index];
116 const Path& regularSetting = paths[!index];
117
Vernon Mauery9e801a22018-10-12 13:20:49 -0700118 auto method = objects.bus.new_method_call(
119 objects.service(oneTimeSetting, iface).c_str(), oneTimeSetting.c_str(),
120 propIntf, "Get");
Tom Joseph9c60a582017-09-13 17:10:46 +0530121 method.append(enabledIntf, "Enabled");
122 auto reply = objects.bus.call(method);
123 if (reply.is_method_error())
124 {
125 log<level::ERR>("Error in getting Enabled property",
126 entry("OBJECT=%s", oneTimeSetting.c_str()),
127 entry("INTERFACE=%s", iface.c_str()));
128 elog<InternalFailure>();
129 }
130
131 sdbusplus::message::variant<bool> enabled;
132 reply.read(enabled);
133 auto oneTimeEnabled = enabled.get<bool>();
134 const Path& setting = oneTimeEnabled ? oneTimeSetting : regularSetting;
135 return std::make_tuple(setting, oneTimeEnabled);
136}
137
138} // namespace boot
139
Tom Joseph19b4f402017-08-02 17:59:30 +0530140} // namespace settings