1 #include <phosphor-logging/elog-errors.hpp>
2 #include <phosphor-logging/log.hpp>
3 #include "xyz/openbmc_project/Common/error.hpp"
4 #include "settings.hpp"
5 
6 namespace settings
7 {
8 
9 using namespace phosphor::logging;
10 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
11 
12 constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
13 constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
14 constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
15 
16 Objects::Objects(sdbusplus::bus::bus& bus) : bus(bus)
17 {
18     std::vector<std::string> settingsIntfs = {autoRebootIntf, powerRestoreIntf};
19     auto depth = 0;
20 
21     auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf,
22                                           "GetSubTree");
23     mapperCall.append(root);
24     mapperCall.append(depth);
25     mapperCall.append(settingsIntfs);
26     auto response = bus.call(mapperCall);
27     if (response.is_method_error())
28     {
29         log<level::ERR>("Error in mapper GetSubTree");
30         elog<InternalFailure>();
31     }
32 
33     using Interfaces = std::vector<Interface>;
34     using MapperResponse = std::map<Path, std::map<Service, Interfaces>>;
35     MapperResponse result;
36     response.read(result);
37     if (result.empty())
38     {
39         log<level::ERR>("Invalid response from mapper");
40         elog<InternalFailure>();
41     }
42 
43     for (const auto& iter : result)
44     {
45         const Path& path = iter.first;
46         const Interface& interface = iter.second.begin()->second[0];
47 
48         if (autoRebootIntf == interface)
49         {
50             autoReboot = path;
51         }
52         else if (powerRestoreIntf == interface)
53         {
54             powerRestorePolicy = path;
55         }
56     }
57 }
58 
59 Service Objects::service(const Path& path, const Interface& interface) const
60 {
61     using Interfaces = std::vector<Interface>;
62     auto mapperCall =
63         bus.new_method_call(mapperService, mapperPath, mapperIntf, "GetObject");
64     mapperCall.append(path);
65     mapperCall.append(Interfaces({interface}));
66 
67     auto response = bus.call(mapperCall);
68     if (response.is_method_error())
69     {
70         log<level::ERR>("Error in mapper GetObject");
71         elog<InternalFailure>();
72     }
73 
74     std::map<Service, Interfaces> result;
75     response.read(result);
76     if (result.empty())
77     {
78         log<level::ERR>("Invalid response from mapper");
79         elog<InternalFailure>();
80     }
81 
82     return result.begin()->first;
83 }
84 
85 } // namespace settings
86