Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 1 | #include "settings.hpp" |
| 2 | |
| 3 | #include "xyz/openbmc_project/Common/error.hpp" |
| 4 | |
Deepak Kodihalli | a646edd | 2017-07-25 07:28:45 -0500 | [diff] [blame] | 5 | #include <phosphor-logging/elog-errors.hpp> |
| 6 | #include <phosphor-logging/log.hpp> |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 7 | #include <sdbusplus/exception.hpp> |
Deepak Kodihalli | a646edd | 2017-07-25 07:28:45 -0500 | [diff] [blame] | 8 | |
| 9 | namespace settings |
| 10 | { |
| 11 | |
| 12 | using namespace phosphor::logging; |
| 13 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 14 | using sdbusplus::exception::SdBusError; |
Deepak Kodihalli | a646edd | 2017-07-25 07:28:45 -0500 | [diff] [blame] | 15 | |
| 16 | constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper"; |
| 17 | constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper"; |
| 18 | constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper"; |
| 19 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 20 | Objects::Objects(sdbusplus::bus::bus& bus) : bus(bus) |
Deepak Kodihalli | a646edd | 2017-07-25 07:28:45 -0500 | [diff] [blame] | 21 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 22 | std::vector<std::string> settingsIntfs = {autoRebootIntf, powerRestoreIntf}; |
Deepak Kodihalli | a646edd | 2017-07-25 07:28:45 -0500 | [diff] [blame] | 23 | auto depth = 0; |
| 24 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 25 | auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf, |
Deepak Kodihalli | a646edd | 2017-07-25 07:28:45 -0500 | [diff] [blame] | 26 | "GetSubTree"); |
| 27 | mapperCall.append(root); |
| 28 | mapperCall.append(depth); |
| 29 | mapperCall.append(settingsIntfs); |
Deepak Kodihalli | a646edd | 2017-07-25 07:28:45 -0500 | [diff] [blame] | 30 | |
| 31 | using Interfaces = std::vector<Interface>; |
| 32 | using MapperResponse = std::map<Path, std::map<Service, Interfaces>>; |
| 33 | MapperResponse result; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 34 | |
| 35 | try |
Deepak Kodihalli | a646edd | 2017-07-25 07:28:45 -0500 | [diff] [blame] | 36 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 37 | auto response = bus.call(mapperCall); |
| 38 | |
| 39 | response.read(result); |
| 40 | if (result.empty()) |
| 41 | { |
| 42 | log<level::ERR>("Invalid response from mapper"); |
| 43 | elog<InternalFailure>(); |
| 44 | } |
| 45 | } |
| 46 | catch (const SdBusError& e) |
| 47 | { |
| 48 | log<level::ERR>("Error in mapper GetSubTree", |
| 49 | entry("ERROR=%s", e.what())); |
Deepak Kodihalli | a646edd | 2017-07-25 07:28:45 -0500 | [diff] [blame] | 50 | elog<InternalFailure>(); |
| 51 | } |
| 52 | |
| 53 | for (const auto& iter : result) |
| 54 | { |
| 55 | const Path& path = iter.first; |
Deepak Kodihalli | a646edd | 2017-07-25 07:28:45 -0500 | [diff] [blame] | 56 | |
Matt Spinler | 14e14cd | 2018-09-07 15:04:16 -0500 | [diff] [blame] | 57 | for (const auto& serviceIter : iter.second) |
Deepak Kodihalli | a646edd | 2017-07-25 07:28:45 -0500 | [diff] [blame] | 58 | { |
Matt Spinler | 14e14cd | 2018-09-07 15:04:16 -0500 | [diff] [blame] | 59 | for (const auto& interface : serviceIter.second) |
| 60 | { |
| 61 | if (autoRebootIntf == interface) |
| 62 | { |
Andrew Geissler | e87db70 | 2020-10-22 19:33:26 -0500 | [diff] [blame] | 63 | /* There are two implementations of the AutoReboot |
| 64 | * Interface. A persistent user setting and a one-time |
| 65 | * setting which is only valid for one boot of the system. |
| 66 | * The one-time setting will have "one_time" in its |
| 67 | * object path. |
| 68 | */ |
| 69 | if (path.find("one_time") != std::string::npos) |
| 70 | { |
| 71 | autoRebootOneTime = path; |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | autoReboot = path; |
| 76 | } |
Matt Spinler | 14e14cd | 2018-09-07 15:04:16 -0500 | [diff] [blame] | 77 | } |
| 78 | else if (powerRestoreIntf == interface) |
| 79 | { |
| 80 | powerRestorePolicy = path; |
| 81 | } |
| 82 | } |
Deepak Kodihalli | a646edd | 2017-07-25 07:28:45 -0500 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | Service Objects::service(const Path& path, const Interface& interface) const |
| 88 | { |
| 89 | using Interfaces = std::vector<Interface>; |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 90 | auto mapperCall = |
| 91 | bus.new_method_call(mapperService, mapperPath, mapperIntf, "GetObject"); |
Deepak Kodihalli | a646edd | 2017-07-25 07:28:45 -0500 | [diff] [blame] | 92 | mapperCall.append(path); |
| 93 | mapperCall.append(Interfaces({interface})); |
| 94 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 95 | std::map<Service, Interfaces> result; |
| 96 | |
| 97 | try |
Deepak Kodihalli | a646edd | 2017-07-25 07:28:45 -0500 | [diff] [blame] | 98 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 99 | auto response = bus.call(mapperCall); |
| 100 | response.read(result); |
| 101 | } |
| 102 | catch (const SdBusError& e) |
| 103 | { |
| 104 | log<level::ERR>("Error in mapper GetObject", |
| 105 | entry("ERROR=%s", e.what())); |
Deepak Kodihalli | a646edd | 2017-07-25 07:28:45 -0500 | [diff] [blame] | 106 | elog<InternalFailure>(); |
| 107 | } |
| 108 | |
Deepak Kodihalli | a646edd | 2017-07-25 07:28:45 -0500 | [diff] [blame] | 109 | if (result.empty()) |
| 110 | { |
| 111 | log<level::ERR>("Invalid response from mapper"); |
| 112 | elog<InternalFailure>(); |
| 113 | } |
| 114 | |
| 115 | return result.begin()->first; |
| 116 | } |
| 117 | |
| 118 | } // namespace settings |