blob: 0ddd7a640669e13d41383a88b164368026a33f1e [file] [log] [blame]
Patrick Venture0b02be92018-08-31 11:55:55 -07001#include "settings.hpp"
2
3#include "utils.hpp"
4
Deepak Kodihalli18aa0442017-07-21 07:07:09 -05005#include <phosphor-logging/elog-errors.hpp>
6#include <phosphor-logging/log.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -07007#include <sdbusplus/message/types.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -07008#include <xyz/openbmc_project/Common/error.hpp>
Deepak Kodihalli18aa0442017-07-21 07:07:09 -05009
10namespace settings
11{
12
13using namespace phosphor::logging;
14using namespace sdbusplus::xyz::openbmc_project::Common::Error;
William A. Kennington III4c008022018-10-12 17:18:14 -070015namespace variant_ns = sdbusplus::message::variant_ns;
Deepak Kodihalli18aa0442017-07-21 07:07:09 -050016
17constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
18constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
19constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
20
21Objects::Objects(sdbusplus::bus::bus& bus,
Patrick Venture0b02be92018-08-31 11:55:55 -070022 const std::vector<Interface>& filter) :
Deepak Kodihalli18aa0442017-07-21 07:07:09 -050023 bus(bus)
24{
25 auto depth = 0;
26
Patrick Venture0b02be92018-08-31 11:55:55 -070027 auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf,
Deepak Kodihalli18aa0442017-07-21 07:07:09 -050028 "GetSubTree");
29 mapperCall.append(root);
30 mapperCall.append(depth);
31 mapperCall.append(filter);
32 auto response = bus.call(mapperCall);
33 if (response.is_method_error())
34 {
35 log<level::ERR>("Error in mapper GetSubTree");
36 elog<InternalFailure>();
37 }
38
39 using Interfaces = std::vector<Interface>;
40 using MapperResponse = std::map<Path, std::map<Service, Interfaces>>;
41 MapperResponse result;
42 response.read(result);
43 if (result.empty())
44 {
45 log<level::ERR>("Invalid response from mapper");
46 elog<InternalFailure>();
47 }
48
49 for (auto& iter : result)
50 {
51 const auto& path = iter.first;
Deepak Kodihallie6027092017-08-27 08:13:37 -050052 for (auto& interface : iter.second.begin()->second)
53 {
54 auto found = map.find(interface);
55 if (map.end() != found)
56 {
57 auto& paths = found->second;
58 paths.push_back(path);
59 }
60 else
61 {
62 map.emplace(std::move(interface), std::vector<Path>({path}));
63 }
64 }
Deepak Kodihalli18aa0442017-07-21 07:07:09 -050065 }
66}
67
68Service Objects::service(const Path& path, const Interface& interface) const
69{
70 using Interfaces = std::vector<Interface>;
Patrick Venture0b02be92018-08-31 11:55:55 -070071 auto mapperCall =
72 bus.new_method_call(mapperService, mapperPath, mapperIntf, "GetObject");
Deepak Kodihalli18aa0442017-07-21 07:07:09 -050073 mapperCall.append(path);
74 mapperCall.append(Interfaces({interface}));
75
76 auto response = bus.call(mapperCall);
77 if (response.is_method_error())
78 {
79 log<level::ERR>("Error in mapper GetObject");
80 elog<InternalFailure>();
81 }
82
83 std::map<Service, Interfaces> result;
84 response.read(result);
85 if (result.empty())
86 {
87 log<level::ERR>("Invalid response from mapper");
88 elog<InternalFailure>();
89 }
90
91 return result.begin()->first;
92}
93
Deepak Kodihalli13791bd2017-08-28 06:50:51 -050094namespace boot
95{
96
97std::tuple<Path, OneTimeEnabled> setting(const Objects& objects,
98 const Interface& iface)
99{
100 constexpr auto bootObjCount = 2;
101 constexpr auto oneTime = "one_time";
102 constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable";
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
Patrick Venture0b02be92018-08-31 11:55:55 -0700121 auto method = objects.bus.new_method_call(
122 objects.service(oneTimeSetting, iface).c_str(), oneTimeSetting.c_str(),
123 ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -0500124 method.append(enabledIntf, "Enabled");
125 auto reply = objects.bus.call(method);
126 if (reply.is_method_error())
127 {
128 log<level::ERR>("Error in getting Enabled property",
129 entry("OBJECT=%s", oneTimeSetting.c_str()),
130 entry("INTERFACE=%s", iface.c_str()));
131 elog<InternalFailure>();
132 }
133
134 sdbusplus::message::variant<bool> enabled;
135 reply.read(enabled);
William A. Kennington III4c008022018-10-12 17:18:14 -0700136 auto oneTimeEnabled = variant_ns::get<bool>(enabled);
Deepak Kodihalli13791bd2017-08-28 06:50:51 -0500137 const Path& setting = oneTimeEnabled ? oneTimeSetting : regularSetting;
138 return std::make_tuple(setting, oneTimeEnabled);
139}
140
141} // namespace boot
142
Deepak Kodihalli18aa0442017-07-21 07:07:09 -0500143} // namespace settings