blob: 59f9f336cf8fbc43f05845b097a63a460a90728f [file] [log] [blame]
Deepak Kodihalli18aa0442017-07-21 07:07:09 -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(sdbusplus::bus::bus& bus,
17 const std::vector<Interface>& filter):
18 bus(bus)
19{
20 auto depth = 0;
21
22 auto mapperCall = bus.new_method_call(mapperService,
23 mapperPath,
24 mapperIntf,
25 "GetSubTree");
26 mapperCall.append(root);
27 mapperCall.append(depth);
28 mapperCall.append(filter);
29 auto response = bus.call(mapperCall);
30 if (response.is_method_error())
31 {
32 log<level::ERR>("Error in mapper GetSubTree");
33 elog<InternalFailure>();
34 }
35
36 using Interfaces = std::vector<Interface>;
37 using MapperResponse = std::map<Path, std::map<Service, Interfaces>>;
38 MapperResponse result;
39 response.read(result);
40 if (result.empty())
41 {
42 log<level::ERR>("Invalid response from mapper");
43 elog<InternalFailure>();
44 }
45
46 for (auto& iter : result)
47 {
48 const auto& path = iter.first;
Deepak Kodihallie6027092017-08-27 08:13:37 -050049 for (auto& interface : iter.second.begin()->second)
50 {
51 auto found = map.find(interface);
52 if (map.end() != found)
53 {
54 auto& paths = found->second;
55 paths.push_back(path);
56 }
57 else
58 {
59 map.emplace(std::move(interface), std::vector<Path>({path}));
60 }
61 }
Deepak Kodihalli18aa0442017-07-21 07:07:09 -050062 }
63}
64
65Service Objects::service(const Path& path, const Interface& interface) const
66{
67 using Interfaces = std::vector<Interface>;
68 auto mapperCall = bus.new_method_call(mapperService,
69 mapperPath,
70 mapperIntf,
71 "GetObject");
72 mapperCall.append(path);
73 mapperCall.append(Interfaces({interface}));
74
75 auto response = bus.call(mapperCall);
76 if (response.is_method_error())
77 {
78 log<level::ERR>("Error in mapper GetObject");
79 elog<InternalFailure>();
80 }
81
82 std::map<Service, Interfaces> result;
83 response.read(result);
84 if (result.empty())
85 {
86 log<level::ERR>("Invalid response from mapper");
87 elog<InternalFailure>();
88 }
89
90 return result.begin()->first;
91}
92
93} // namespace settings