1 #include "settings.hpp"
2 
3 #include "xyz/openbmc_project/Common/error.hpp"
4 
5 #include <phosphor-logging/elog-errors.hpp>
6 #include <phosphor-logging/lg2.hpp>
7 
8 namespace settings
9 {
10 
11 using namespace phosphor::logging;
12 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
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 
18 Objects::Objects(sdbusplus::bus_t& bus) : bus(bus)
19 {
20     std::vector<std::string> settingsIntfs = {timeSyncIntf};
21     auto depth = 0;
22 
23     auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf,
24                                           "GetSubTree");
25     mapperCall.append(root);
26     mapperCall.append(depth);
27     mapperCall.append(settingsIntfs);
28 
29     using Interfaces = std::vector<Interface>;
30     using MapperResponse = std::vector<
31         std::pair<Path, std::vector<std::pair<Service, Interfaces>>>>;
32     MapperResponse result;
33 
34     try
35     {
36         auto response = bus.call(mapperCall);
37         response.read(result);
38     }
39     catch (const sdbusplus::exception_t& ex)
40     {
41         lg2::error("Failed to invoke GetSubTree method: {ERROR}", "ERROR", ex);
42     }
43 
44     if (result.empty())
45     {
46         lg2::error("Invalid response from mapper");
47     }
48 
49     for (const auto& iter : result)
50     {
51         const Path& path = iter.first;
52         for (const auto& service_iter : iter.second)
53         {
54             for (const Interface& interface : service_iter.second)
55             {
56                 if (timeSyncIntf == interface)
57                 {
58                     timeSyncMethod = path;
59                 }
60             }
61         }
62     }
63 }
64 
65 Service Objects::service(const Path& path, const Interface& interface) const
66 {
67     using Interfaces = std::vector<Interface>;
68     auto mapperCall =
69         bus.new_method_call(mapperService, mapperPath, mapperIntf, "GetObject");
70     mapperCall.append(path);
71     mapperCall.append(Interfaces({interface}));
72 
73     std::map<Service, Interfaces> result;
74     try
75     {
76         auto response = bus.call(mapperCall);
77         response.read(result);
78     }
79     catch (const sdbusplus::exception_t& ex)
80     {
81         lg2::error("Error in mapper GetObject: {ERROR}", "ERROR", ex);
82     }
83 
84     if (result.empty())
85     {
86         lg2::error("Invalid response from mapper");
87         elog<InternalFailure>();
88     }
89 
90     return result.begin()->first;
91 }
92 
93 } // namespace settings
94