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         elog<InternalFailure>();
48     }
49 
50     for (const auto& iter : result)
51     {
52         const Path& path = iter.first;
53         for (const auto& service_iter : iter.second)
54         {
55             for (const Interface& interface : service_iter.second)
56             {
57                 if (timeSyncIntf == interface)
58                 {
59                     timeSyncMethod = path;
60                 }
61             }
62         }
63     }
64 }
65 
66 Service Objects::service(const Path& path, const Interface& interface) const
67 {
68     using Interfaces = std::vector<Interface>;
69     auto mapperCall =
70         bus.new_method_call(mapperService, mapperPath, mapperIntf, "GetObject");
71     mapperCall.append(path);
72     mapperCall.append(Interfaces({interface}));
73 
74     std::map<Service, Interfaces> result;
75     try
76     {
77         auto response = bus.call(mapperCall);
78         response.read(result);
79     }
80     catch (const sdbusplus::exception_t& ex)
81     {
82         lg2::error("Error in mapper GetObject: {ERROR}", "ERROR", ex);
83     }
84 
85     if (result.empty())
86     {
87         lg2::error("Invalid response from mapper");
88         elog<InternalFailure>();
89     }
90 
91     return result.begin()->first;
92 }
93 
94 } // namespace settings
95