blob: a3eada97ae11a951a727f69ac1356a34ee67548e [file] [log] [blame]
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -05001#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
6namespace settings
7{
8
9using namespace phosphor::logging;
10using namespace sdbusplus::xyz::openbmc_project::Common::Error;
11
12constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
13constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
14constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
15
16Objects::Objects()
17{
18 auto bus = sdbusplus::bus::new_default();
19 std::vector<std::string> settingsIntfs =
Lei YUdebe1d82017-10-13 13:21:37 +080020 {timeOwnerIntf, timeSyncIntf, hostStateIntf};
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -050021 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 }
Lei YUdebe1d82017-10-13 13:21:37 +080060 else if (hostStateIntf == interface)
61 {
62 hostState = path;
63 }
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -050064 }
65}
66
67Service 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