blob: bbe8611a5119a7dda22403c648d650c4e8c689c4 [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 Mauerya34c2ab2018-10-15 16:06:18 -07005#include <sdbusplus/message/types.hpp>
Vernon Maueryf41a5542018-10-15 16:04:17 -07006#include <xyz/openbmc_project/Common/error.hpp>
Tom Joseph19b4f402017-08-02 17:59:30 +05307
8namespace settings
9{
10
11using namespace phosphor::logging;
12using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Vernon Mauerya34c2ab2018-10-15 16:06:18 -070013namespace variant_ns = sdbusplus::message::variant_ns;
Tom Joseph19b4f402017-08-02 17:59:30 +053014
15constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
16constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
17constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
18
19Objects::Objects(sdbusplus::bus::bus& bus,
Vernon Mauery9e801a22018-10-12 13:20:49 -070020 const std::vector<Interface>& filter) :
Tom Joseph19b4f402017-08-02 17:59:30 +053021 bus(bus)
22{
23 auto depth = 0;
24
Vernon Mauery9e801a22018-10-12 13:20:49 -070025 auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf,
Tom Joseph19b4f402017-08-02 17:59:30 +053026 "GetSubTree");
27 mapperCall.append(root);
28 mapperCall.append(depth);
29 mapperCall.append(filter);
30 auto response = bus.call(mapperCall);
31 if (response.is_method_error())
32 {
33 log<level::ERR>("Error in mapper GetSubTree");
34 elog<InternalFailure>();
35 }
36
37 using Interfaces = std::vector<Interface>;
38 using MapperResponse = std::map<Path, std::map<Service, Interfaces>>;
39 MapperResponse result;
40 response.read(result);
41 if (result.empty())
42 {
43 log<level::ERR>("Invalid response from mapper");
44 elog<InternalFailure>();
45 }
46
47 for (auto& iter : result)
48 {
49 const auto& path = iter.first;
Tom Joseph7c78adf2017-09-11 12:52:56 +053050 for (auto& interface : iter.second.begin()->second)
51 {
52 auto found = map.find(interface);
53 if (map.end() != found)
54 {
55 auto& paths = found->second;
56 paths.push_back(path);
57 }
58 else
59 {
60 map.emplace(std::move(interface), std::vector<Path>({path}));
61 }
62 }
Tom Joseph19b4f402017-08-02 17:59:30 +053063 }
64}
65
66Service Objects::service(const Path& path, const Interface& interface) const
67{
68 using Interfaces = std::vector<Interface>;
Vernon Mauery9e801a22018-10-12 13:20:49 -070069 auto mapperCall =
70 bus.new_method_call(mapperService, mapperPath, mapperIntf, "GetObject");
Tom Joseph19b4f402017-08-02 17:59:30 +053071 mapperCall.append(path);
72 mapperCall.append(Interfaces({interface}));
73
74 auto response = bus.call(mapperCall);
75 if (response.is_method_error())
76 {
77 log<level::ERR>("Error in mapper GetObject");
78 elog<InternalFailure>();
79 }
80
81 std::map<Service, Interfaces> result;
82 response.read(result);
83 if (result.empty())
84 {
85 log<level::ERR>("Invalid response from mapper");
86 elog<InternalFailure>();
87 }
88
89 return result.begin()->first;
90}
91
Tom Joseph9c60a582017-09-13 17:10:46 +053092namespace boot
93{
94
95std::tuple<Path, OneTimeEnabled> setting(const Objects& objects,
96 const Interface& iface)
97{
98 constexpr auto bootObjCount = 2;
99 constexpr auto oneTime = "one_time";
100 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
101 constexpr auto propIntf = "org.freedesktop.DBus.Properties";
102
103 const std::vector<Path>& paths = objects.map.at(iface);
104 auto count = paths.size();
105 if (count != bootObjCount)
106 {
107 log<level::ERR>("Exactly two objects expected",
108 entry("INTERFACE=%s", iface.c_str()),
109 entry("COUNT=%d", count));
110 elog<InternalFailure>();
111 }
112 size_t index = 0;
113 if (std::string::npos == paths[0].rfind(oneTime))
114 {
115 index = 1;
116 }
117 const Path& oneTimeSetting = paths[index];
118 const Path& regularSetting = paths[!index];
119
Vernon Mauery9e801a22018-10-12 13:20:49 -0700120 auto method = objects.bus.new_method_call(
121 objects.service(oneTimeSetting, iface).c_str(), oneTimeSetting.c_str(),
122 propIntf, "Get");
Tom Joseph9c60a582017-09-13 17:10:46 +0530123 method.append(enabledIntf, "Enabled");
124 auto reply = objects.bus.call(method);
125 if (reply.is_method_error())
126 {
127 log<level::ERR>("Error in getting Enabled property",
128 entry("OBJECT=%s", oneTimeSetting.c_str()),
129 entry("INTERFACE=%s", iface.c_str()));
130 elog<InternalFailure>();
131 }
132
133 sdbusplus::message::variant<bool> enabled;
134 reply.read(enabled);
Vernon Mauerya34c2ab2018-10-15 16:06:18 -0700135 auto oneTimeEnabled = variant_ns::get<bool>(enabled);
Tom Joseph9c60a582017-09-13 17:10:46 +0530136 const Path& setting = oneTimeEnabled ? oneTimeSetting : regularSetting;
137 return std::make_tuple(setting, oneTimeEnabled);
138}
139
140} // namespace boot
141
Tom Joseph19b4f402017-08-02 17:59:30 +0530142} // namespace settings