Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1 | #include "settings.hpp" |
| 2 | |
| 3 | #include "utils.hpp" |
| 4 | |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 5 | #include <phosphor-logging/elog-errors.hpp> |
| 6 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 7 | #include <sdbusplus/message/types.hpp> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 8 | #include <xyz/openbmc_project/Common/error.hpp> |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 9 | |
| 10 | namespace settings |
| 11 | { |
| 12 | |
| 13 | using namespace phosphor::logging; |
| 14 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 15 | namespace variant_ns = sdbusplus::message::variant_ns; |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 16 | |
| 17 | constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper"; |
| 18 | constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper"; |
| 19 | constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper"; |
| 20 | |
| 21 | Objects::Objects(sdbusplus::bus::bus& bus, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 22 | const std::vector<Interface>& filter) : |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 23 | bus(bus) |
| 24 | { |
| 25 | auto depth = 0; |
| 26 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 27 | auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf, |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 28 | "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 Kodihalli | e602709 | 2017-08-27 08:13:37 -0500 | [diff] [blame] | 52 | 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 Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | Service Objects::service(const Path& path, const Interface& interface) const |
| 69 | { |
| 70 | using Interfaces = std::vector<Interface>; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 71 | auto mapperCall = |
| 72 | bus.new_method_call(mapperService, mapperPath, mapperIntf, "GetObject"); |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 73 | 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 Kodihalli | 13791bd | 2017-08-28 06:50:51 -0500 | [diff] [blame] | 94 | namespace boot |
| 95 | { |
| 96 | |
| 97 | std::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 Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 121 | auto method = objects.bus.new_method_call( |
| 122 | objects.service(oneTimeSetting, iface).c_str(), oneTimeSetting.c_str(), |
| 123 | ipmi::PROP_INTF, "Get"); |
Deepak Kodihalli | 13791bd | 2017-08-28 06:50:51 -0500 | [diff] [blame] | 124 | 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 III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 136 | auto oneTimeEnabled = variant_ns::get<bool>(enabled); |
Deepak Kodihalli | 13791bd | 2017-08-28 06:50:51 -0500 | [diff] [blame] | 137 | const Path& setting = oneTimeEnabled ? oneTimeSetting : regularSetting; |
| 138 | return std::make_tuple(setting, oneTimeEnabled); |
| 139 | } |
| 140 | |
| 141 | } // namespace boot |
| 142 | |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 143 | } // namespace settings |