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