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