blob: 2fa2511ec8f3719040a9529ee7cf1249898f8273 [file] [log] [blame]
Patrick Venture0b02be92018-08-31 11:55:55 -07001#include "settings.hpp"
2
Vernon Mauery6a98fe72019-03-11 15:57:48 -07003#include <ipmid/utils.hpp>
Deepak Kodihalli18aa0442017-07-21 07:07:09 -05004#include <phosphor-logging/elog-errors.hpp>
5#include <phosphor-logging/log.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -07006#include <sdbusplus/message/types.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -07007#include <xyz/openbmc_project/Common/error.hpp>
Deepak Kodihalli18aa0442017-07-21 07:07:09 -05008
9namespace settings
10{
11
12using namespace phosphor::logging;
13using namespace sdbusplus::xyz::openbmc_project::Common::Error;
14
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,
Patrick Venture0b02be92018-08-31 11:55:55 -070020 const std::vector<Interface>& filter) :
Deepak Kodihalli18aa0442017-07-21 07:07:09 -050021 bus(bus)
22{
23 auto depth = 0;
24
Patrick Venture0b02be92018-08-31 11:55:55 -070025 auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf,
Deepak Kodihalli18aa0442017-07-21 07:07:09 -050026 "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;
Deepak Kodihallie6027092017-08-27 08:13:37 -050050 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 }
Deepak Kodihalli18aa0442017-07-21 07:07:09 -050063 }
64}
65
66Service Objects::service(const Path& path, const Interface& interface) const
67{
68 using Interfaces = std::vector<Interface>;
Patrick Venture0b02be92018-08-31 11:55:55 -070069 auto mapperCall =
70 bus.new_method_call(mapperService, mapperPath, mapperIntf, "GetObject");
Deepak Kodihalli18aa0442017-07-21 07:07:09 -050071 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
Deepak Kodihalli13791bd2017-08-28 06:50:51 -050092namespace 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
102 const std::vector<Path>& paths = objects.map.at(iface);
103 auto count = paths.size();
104 if (count != bootObjCount)
105 {
106 log<level::ERR>("Exactly two objects expected",
107 entry("INTERFACE=%s", iface.c_str()),
108 entry("COUNT=%d", count));
109 elog<InternalFailure>();
110 }
111 size_t index = 0;
112 if (std::string::npos == paths[0].rfind(oneTime))
113 {
114 index = 1;
115 }
116 const Path& oneTimeSetting = paths[index];
117 const Path& regularSetting = paths[!index];
118
Patrick Venture0b02be92018-08-31 11:55:55 -0700119 auto method = objects.bus.new_method_call(
120 objects.service(oneTimeSetting, iface).c_str(), oneTimeSetting.c_str(),
121 ipmi::PROP_INTF, "Get");
Deepak Kodihalli13791bd2017-08-28 06:50:51 -0500122 method.append(enabledIntf, "Enabled");
123 auto reply = objects.bus.call(method);
124 if (reply.is_method_error())
125 {
126 log<level::ERR>("Error in getting Enabled property",
127 entry("OBJECT=%s", oneTimeSetting.c_str()),
128 entry("INTERFACE=%s", iface.c_str()));
129 elog<InternalFailure>();
130 }
131
Vernon Mauery16b86932019-05-01 08:36:11 -0700132 std::variant<bool> enabled;
Deepak Kodihalli13791bd2017-08-28 06:50:51 -0500133 reply.read(enabled);
Vernon Maueryf442e112019-04-09 11:44:36 -0700134 auto oneTimeEnabled = std::get<bool>(enabled);
Deepak Kodihalli13791bd2017-08-28 06:50:51 -0500135 const Path& setting = oneTimeEnabled ? oneTimeSetting : regularSetting;
136 return std::make_tuple(setting, oneTimeEnabled);
137}
138
139} // namespace boot
140
Deepak Kodihalli18aa0442017-07-21 07:07:09 -0500141} // namespace settings