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()
17 {
18     auto bus = sdbusplus::bus::new_default();
19     std::vector<std::string> settingsIntfs =
20         {timeOwnerIntf, timeSyncIntf, hostStateIntf};
21     auto depth = 0;
22 
23     auto mapperCall = bus.new_method_call(mapperService,
24                                           mapperPath,
25                                           mapperIntf,
26                                           "GetSubTree");
27     mapperCall.append(root);
28     mapperCall.append(depth);
29     mapperCall.append(settingsIntfs);
30     auto response = bus.call(mapperCall);
31     if (response.is_method_error())
32     {
33         log<level::ERR>("Error in mapper GetSubTree");
34         elog<InternalFailure>();
35     }
36 
37     using Interfaces = std::vector<Interface>;
38     using MapperResponse = std::map<Path, std::map<Service, Interfaces>>;
39     MapperResponse result;
40     response.read(result);
41     if (result.empty())
42     {
43         log<level::ERR>("Invalid response from mapper");
44         elog<InternalFailure>();
45     }
46 
47     for (const auto& iter : result)
48     {
49         const Path& path = iter.first;
50         const Interface& interface = iter.second.begin()->second.front();
51 
52         if (timeOwnerIntf == interface)
53         {
54             timeOwner = path;
55         }
56         else if (timeSyncIntf == interface)
57         {
58             timeSyncMethod = path;
59         }
60         else if (hostStateIntf == interface)
61         {
62             hostState = path;
63         }
64     }
65 }
66 
67 Service Objects::service(const Path& path, const Interface& interface) const
68 {
69     auto bus = sdbusplus::bus::new_default();
70     using Interfaces = std::vector<Interface>;
71     auto mapperCall = bus.new_method_call(mapperService,
72                                           mapperPath,
73                                           mapperIntf,
74                                           "GetObject");
75     mapperCall.append(path);
76     mapperCall.append(Interfaces({interface}));
77 
78     auto response = bus.call(mapperCall);
79     if (response.is_method_error())
80     {
81         log<level::ERR>("Error in mapper GetObject");
82         elog<InternalFailure>();
83     }
84 
85     std::map<Service, Interfaces> result;
86     response.read(result);
87     if (result.empty())
88     {
89         log<level::ERR>("Invalid response from mapper");
90         elog<InternalFailure>();
91     }
92 
93     return result.begin()->first;
94 }
95 
96 } // namespace settings
97