blob: 0d368d15c63c193277111b690b9d6c334deda178 [file] [log] [blame]
Gunnar Millsab4cc6a2018-09-14 14:42:39 -05001#include "settings.hpp"
2
3#include "xyz/openbmc_project/Common/error.hpp"
4
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -05005#include <phosphor-logging/elog-errors.hpp>
George Liu947b5342022-07-01 16:12:18 +08006#include <phosphor-logging/lg2.hpp>
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -05007
8namespace settings
9{
10
11using namespace phosphor::logging;
12using namespace sdbusplus::xyz::openbmc_project::Common::Error;
13
14constexpr auto mapperService = "xyz.openbmc_project.ObjectMapper";
15constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
16constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
17
Patrick Williams38679262022-07-22 19:26:55 -050018Objects::Objects(sdbusplus::bus_t& bus) : bus(bus)
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -050019{
George Liu0a704522020-04-13 14:51:40 +080020 std::vector<std::string> settingsIntfs = {timeSyncIntf};
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -050021 auto depth = 0;
22
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050023 auto mapperCall = bus.new_method_call(mapperService, mapperPath, mapperIntf,
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -050024 "GetSubTree");
25 mapperCall.append(root);
26 mapperCall.append(depth);
27 mapperCall.append(settingsIntfs);
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -050028
29 using Interfaces = std::vector<Interface>;
Ed Tanous7aa715b2018-05-09 17:28:05 -070030 using MapperResponse = std::vector<
31 std::pair<Path, std::vector<std::pair<Service, Interfaces>>>>;
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -050032 MapperResponse result;
George Liuf344f842022-07-01 16:09:41 +080033
34 try
35 {
36 auto response = bus.call(mapperCall);
37 response.read(result);
38 }
Patrick Williams38679262022-07-22 19:26:55 -050039 catch (const sdbusplus::exception_t& ex)
George Liuf344f842022-07-01 16:09:41 +080040 {
George Liu947b5342022-07-01 16:12:18 +080041 lg2::error("Failed to invoke GetSubTree method: {ERROR}", "ERROR", ex);
George Liuf344f842022-07-01 16:09:41 +080042 }
43
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -050044 if (result.empty())
45 {
George Liu947b5342022-07-01 16:12:18 +080046 lg2::error("Invalid response from mapper");
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -050047 }
48
49 for (const auto& iter : result)
50 {
51 const Path& path = iter.first;
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050052 for (const auto& service_iter : iter.second)
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -050053 {
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050054 for (const Interface& interface : service_iter.second)
Ed Tanous7aa715b2018-05-09 17:28:05 -070055 {
George Liu3c2f4492020-04-12 11:35:57 +080056 if (timeSyncIntf == interface)
Ed Tanous7aa715b2018-05-09 17:28:05 -070057 {
58 timeSyncMethod = path;
59 }
Ed Tanous7aa715b2018-05-09 17:28:05 -070060 }
Lei YUdebe1d82017-10-13 13:21:37 +080061 }
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -050062 }
63}
64
65Service Objects::service(const Path& path, const Interface& interface) const
66{
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -050067 using Interfaces = std::vector<Interface>;
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050068 auto mapperCall =
69 bus.new_method_call(mapperService, mapperPath, mapperIntf, "GetObject");
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -050070 mapperCall.append(path);
71 mapperCall.append(Interfaces({interface}));
72
George Liuf344f842022-07-01 16:09:41 +080073 std::map<Service, Interfaces> result;
74 try
75 {
76 auto response = bus.call(mapperCall);
77 response.read(result);
78 }
Patrick Williams38679262022-07-22 19:26:55 -050079 catch (const sdbusplus::exception_t& ex)
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -050080 {
George Liu947b5342022-07-01 16:12:18 +080081 lg2::error("Error in mapper GetObject: {ERROR}", "ERROR", ex);
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -050082 }
83
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -050084 if (result.empty())
85 {
George Liu947b5342022-07-01 16:12:18 +080086 lg2::error("Invalid response from mapper");
Deepak Kodihalli20ed79e2017-07-25 05:48:58 -050087 elog<InternalFailure>();
88 }
89
90 return result.begin()->first;
91}
92
93} // namespace settings