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 | a34c2ab | 2018-10-15 16:06:18 -0700 | [diff] [blame] | 5 | #include <sdbusplus/message/types.hpp> |
Vernon Mauery | f41a554 | 2018-10-15 16:04:17 -0700 | [diff] [blame] | 6 | #include <xyz/openbmc_project/Common/error.hpp> |
Tom Joseph | 19b4f40 | 2017-08-02 17:59:30 +0530 | [diff] [blame] | 7 | |
| 8 | namespace settings |
| 9 | { |
| 10 | |
| 11 | using namespace phosphor::logging; |
| 12 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Vernon Mauery | a34c2ab | 2018-10-15 16:06:18 -0700 | [diff] [blame] | 13 | namespace variant_ns = sdbusplus::message::variant_ns; |
Tom Joseph | 19b4f40 | 2017-08-02 17:59:30 +0530 | [diff] [blame] | 14 | |
| 15 | constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper"; |
| 16 | constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper"; |
| 17 | constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper"; |
| 18 | |
| 19 | Objects::Objects(sdbusplus::bus::bus& bus, |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 20 | const std::vector<Interface>& filter) : |
Tom Joseph | 19b4f40 | 2017-08-02 17:59:30 +0530 | [diff] [blame] | 21 | bus(bus) |
| 22 | { |
| 23 | auto depth = 0; |
| 24 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 25 | auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf, |
Tom Joseph | 19b4f40 | 2017-08-02 17:59:30 +0530 | [diff] [blame] | 26 | "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; |
Tom Joseph | 7c78adf | 2017-09-11 12:52:56 +0530 | [diff] [blame] | 50 | 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 | } |
Tom Joseph | 19b4f40 | 2017-08-02 17:59:30 +0530 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | Service Objects::service(const Path& path, const Interface& interface) const |
| 67 | { |
| 68 | using Interfaces = std::vector<Interface>; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 69 | auto mapperCall = |
| 70 | bus.new_method_call(mapperService, mapperPath, mapperIntf, "GetObject"); |
Tom Joseph | 19b4f40 | 2017-08-02 17:59:30 +0530 | [diff] [blame] | 71 | 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 | |
Tom Joseph | 9c60a58 | 2017-09-13 17:10:46 +0530 | [diff] [blame] | 92 | namespace boot |
| 93 | { |
| 94 | |
| 95 | std::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 | constexpr auto propIntf = "org.freedesktop.DBus.Properties"; |
| 102 | |
| 103 | const std::vector<Path>& paths = objects.map.at(iface); |
| 104 | auto count = paths.size(); |
| 105 | if (count != bootObjCount) |
| 106 | { |
| 107 | log<level::ERR>("Exactly two objects expected", |
| 108 | entry("INTERFACE=%s", iface.c_str()), |
| 109 | entry("COUNT=%d", count)); |
| 110 | elog<InternalFailure>(); |
| 111 | } |
| 112 | size_t index = 0; |
| 113 | if (std::string::npos == paths[0].rfind(oneTime)) |
| 114 | { |
| 115 | index = 1; |
| 116 | } |
| 117 | const Path& oneTimeSetting = paths[index]; |
| 118 | const Path& regularSetting = paths[!index]; |
| 119 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 120 | auto method = objects.bus.new_method_call( |
| 121 | objects.service(oneTimeSetting, iface).c_str(), oneTimeSetting.c_str(), |
| 122 | propIntf, "Get"); |
Tom Joseph | 9c60a58 | 2017-09-13 17:10:46 +0530 | [diff] [blame] | 123 | method.append(enabledIntf, "Enabled"); |
| 124 | auto reply = objects.bus.call(method); |
| 125 | if (reply.is_method_error()) |
| 126 | { |
| 127 | log<level::ERR>("Error in getting Enabled property", |
| 128 | entry("OBJECT=%s", oneTimeSetting.c_str()), |
| 129 | entry("INTERFACE=%s", iface.c_str())); |
| 130 | elog<InternalFailure>(); |
| 131 | } |
| 132 | |
| 133 | sdbusplus::message::variant<bool> enabled; |
| 134 | reply.read(enabled); |
Vernon Mauery | a34c2ab | 2018-10-15 16:06:18 -0700 | [diff] [blame] | 135 | auto oneTimeEnabled = variant_ns::get<bool>(enabled); |
Tom Joseph | 9c60a58 | 2017-09-13 17:10:46 +0530 | [diff] [blame] | 136 | const Path& setting = oneTimeEnabled ? oneTimeSetting : regularSetting; |
| 137 | return std::make_tuple(setting, oneTimeEnabled); |
| 138 | } |
| 139 | |
| 140 | } // namespace boot |
| 141 | |
Tom Joseph | 19b4f40 | 2017-08-02 17:59:30 +0530 | [diff] [blame] | 142 | } // namespace settings |