xref: /openbmc/phosphor-host-ipmid/settings.cpp (revision 81eb6b32a6223e93e21f7da1901b96d78d8d003b)
1 #include "settings.hpp"
2 
3 #include <ipmid/utils.hpp>
4 #include <phosphor-logging/elog-errors.hpp>
5 #include <phosphor-logging/lg2.hpp>
6 #include <sdbusplus/message/types.hpp>
7 #include <xyz/openbmc_project/Common/error.hpp>
8 
9 namespace settings
10 {
11 
12 using namespace phosphor::logging;
13 using namespace sdbusplus::error::xyz::openbmc_project::common;
14 
15 constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
16 constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
17 constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
18 
19 Objects::Objects(sdbusplus::bus_t& bus, const std::vector<Interface>& filter) :
20     bus(bus)
21 {
22     ipmi::ObjectTree objectTree;
23     try
24     {
25         objectTree = ipmi::getSubTree(bus, filter);
26     }
27     catch (const std::exception& e)
28     {
29         lg2::error("Failed to call the getSubTree method: {ERROR}", "ERROR", e);
30         elog<InternalFailure>();
31     }
32 
33     for (auto& iter : objectTree)
34     {
35         const auto& path = iter.first;
36         for (auto& interface : iter.second.begin()->second)
37         {
38             auto found = map.find(interface);
39             if (map.end() != found)
40             {
41                 auto& paths = found->second;
42                 paths.push_back(path);
43             }
44             else
45             {
46                 map.emplace(std::move(interface), std::vector<Path>({path}));
47             }
48         }
49     }
50 }
51 
52 Service Objects::service(const Path& path, const Interface& interface) const
53 {
54     using Interfaces = std::vector<Interface>;
55     auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf,
56                                           "GetObject");
57     mapperCall.append(path);
58     mapperCall.append(Interfaces({interface}));
59 
60     std::map<Service, Interfaces> result;
61     try
62     {
63         auto response = bus.call(mapperCall);
64         response.read(result);
65         return result.begin()->first;
66     }
67     catch (const std::exception& e)
68     {
69         lg2::error("Invalid response from mapper: {ERROR}", "ERROR", e);
70         elog<InternalFailure>();
71     }
72 }
73 
74 } // namespace settings
75