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