Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1 | #include "settings.hpp" |
| 2 | |
Vernon Mauery | 6a98fe7 | 2019-03-11 15:57:48 -0700 | [diff] [blame] | 3 | #include <ipmid/utils.hpp> |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 4 | #include <phosphor-logging/elog-errors.hpp> |
| 5 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 6 | #include <sdbusplus/message/types.hpp> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 7 | #include <xyz/openbmc_project/Common/error.hpp> |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 8 | |
| 9 | namespace settings |
| 10 | { |
| 11 | |
| 12 | using namespace phosphor::logging; |
Willy Tu | 523e2d1 | 2023-09-05 11:36:48 -0700 | [diff] [blame] | 13 | using namespace sdbusplus::error::xyz::openbmc_project::common; |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [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 | |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 19 | Objects::Objects(sdbusplus::bus_t& bus, const std::vector<Interface>& filter) : |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 20 | bus(bus) |
| 21 | { |
| 22 | auto depth = 0; |
| 23 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 24 | auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf, |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 25 | "GetSubTree"); |
| 26 | mapperCall.append(root); |
| 27 | mapperCall.append(depth); |
| 28 | mapperCall.append(filter); |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 29 | |
| 30 | using Interfaces = std::vector<Interface>; |
| 31 | using MapperResponse = std::map<Path, std::map<Service, Interfaces>>; |
| 32 | MapperResponse result; |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 33 | try |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 34 | { |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 35 | auto response = bus.call(mapperCall); |
| 36 | response.read(result); |
| 37 | } |
| 38 | catch (const std::exception& e) |
| 39 | { |
| 40 | log<level::ERR>("Error in mapper GetSubTree", |
| 41 | entry("ERROR=%s", e.what())); |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 42 | elog<InternalFailure>(); |
| 43 | } |
| 44 | |
| 45 | for (auto& iter : result) |
| 46 | { |
| 47 | const auto& path = iter.first; |
Deepak Kodihalli | e602709 | 2017-08-27 08:13:37 -0500 | [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 | } |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
| 64 | Service Objects::service(const Path& path, const Interface& interface) const |
| 65 | { |
| 66 | using Interfaces = std::vector<Interface>; |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 67 | auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf, |
| 68 | "GetObject"); |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 69 | mapperCall.append(path); |
| 70 | mapperCall.append(Interfaces({interface})); |
| 71 | |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 72 | std::map<Service, Interfaces> result; |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 73 | try |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 74 | { |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 75 | auto response = bus.call(mapperCall); |
| 76 | response.read(result); |
| 77 | return result.begin()->first; |
| 78 | } |
| 79 | catch (const std::exception& e) |
| 80 | { |
| 81 | log<level::ERR>("Invalid response from mapper", |
| 82 | entry("ERROR=%s", e.what())); |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 83 | elog<InternalFailure>(); |
| 84 | } |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 85 | } |
| 86 | |
Deepak Kodihalli | 13791bd | 2017-08-28 06:50:51 -0500 | [diff] [blame] | 87 | namespace boot |
| 88 | { |
| 89 | |
| 90 | std::tuple<Path, OneTimeEnabled> setting(const Objects& objects, |
| 91 | const Interface& iface) |
| 92 | { |
| 93 | constexpr auto bootObjCount = 2; |
| 94 | constexpr auto oneTime = "one_time"; |
| 95 | constexpr auto enabledIntf = "xyz.openbmc_project.Object.Enable"; |
| 96 | |
| 97 | const std::vector<Path>& paths = objects.map.at(iface); |
| 98 | auto count = paths.size(); |
| 99 | if (count != bootObjCount) |
| 100 | { |
| 101 | log<level::ERR>("Exactly two objects expected", |
| 102 | entry("INTERFACE=%s", iface.c_str()), |
| 103 | entry("COUNT=%d", count)); |
| 104 | elog<InternalFailure>(); |
| 105 | } |
| 106 | size_t index = 0; |
| 107 | if (std::string::npos == paths[0].rfind(oneTime)) |
| 108 | { |
| 109 | index = 1; |
| 110 | } |
| 111 | const Path& oneTimeSetting = paths[index]; |
| 112 | const Path& regularSetting = paths[!index]; |
| 113 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 114 | auto method = objects.bus.new_method_call( |
| 115 | objects.service(oneTimeSetting, iface).c_str(), oneTimeSetting.c_str(), |
| 116 | ipmi::PROP_INTF, "Get"); |
Deepak Kodihalli | 13791bd | 2017-08-28 06:50:51 -0500 | [diff] [blame] | 117 | method.append(enabledIntf, "Enabled"); |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 118 | |
| 119 | std::variant<bool> enabled; |
| 120 | try |
| 121 | { |
| 122 | auto reply = objects.bus.call(method); |
| 123 | reply.read(enabled); |
| 124 | } |
| 125 | catch (const std::exception& e) |
Deepak Kodihalli | 13791bd | 2017-08-28 06:50:51 -0500 | [diff] [blame] | 126 | { |
| 127 | log<level::ERR>("Error in getting Enabled property", |
| 128 | entry("OBJECT=%s", oneTimeSetting.c_str()), |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 129 | entry("INTERFACE=%s", iface.c_str()), |
| 130 | entry("ERROR=%s", e.what())); |
Deepak Kodihalli | 13791bd | 2017-08-28 06:50:51 -0500 | [diff] [blame] | 131 | elog<InternalFailure>(); |
| 132 | } |
| 133 | |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 134 | auto oneTimeEnabled = std::get<bool>(enabled); |
Deepak Kodihalli | 13791bd | 2017-08-28 06:50:51 -0500 | [diff] [blame] | 135 | const Path& setting = oneTimeEnabled ? oneTimeSetting : regularSetting; |
| 136 | return std::make_tuple(setting, oneTimeEnabled); |
| 137 | } |
| 138 | |
| 139 | } // namespace boot |
| 140 | |
Deepak Kodihalli | 18aa044 | 2017-07-21 07:07:09 -0500 | [diff] [blame] | 141 | } // namespace settings |