blob: f925bdb4ad40392d9499b205cc39f25639c8d3a5 [file] [log] [blame]
Lei YUddd54422017-04-18 16:38:44 +08001#include "utils.hpp"
2
Lei YUddd54422017-04-18 16:38:44 +08003namespace phosphor
4{
5namespace time
6{
7
8namespace // anonymous
9{
Pavithra Barithaya864e1732023-04-11 04:30:23 -050010constexpr auto mapperBusname = "xyz.openbmc_project.ObjectMapper";
11constexpr auto mapperPath = "/xyz/openbmc_project/object_mapper";
12constexpr auto mapperInterface = "xyz.openbmc_project.ObjectMapper";
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050013} // namespace
Lei YUddd54422017-04-18 16:38:44 +080014
15namespace utils
16{
17
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -050018PHOSPHOR_LOG2_USING;
19
Patrick Williams38679262022-07-22 19:26:55 -050020std::string getService(sdbusplus::bus_t& bus, const char* path,
Lei YUdd8e9e42017-04-19 17:46:58 +080021 const char* interface)
22{
Pavithra Barithaya864e1732023-04-11 04:30:23 -050023 auto mapper = bus.new_method_call(mapperBusname, mapperPath,
24 mapperInterface, "GetObject");
Lei YUdd8e9e42017-04-19 17:46:58 +080025
26 mapper.append(path, std::vector<std::string>({interface}));
Lei YU86c83f32018-07-13 15:14:56 +080027 try
Lei YUdd8e9e42017-04-19 17:46:58 +080028 {
Lei YU86c83f32018-07-13 15:14:56 +080029 auto mapperResponseMsg = bus.call(mapper);
Lei YUdd8e9e42017-04-19 17:46:58 +080030
Lei YU86c83f32018-07-13 15:14:56 +080031 std::vector<std::pair<std::string, std::vector<std::string>>>
32 mapperResponse;
33 mapperResponseMsg.read(mapperResponse);
34 if (mapperResponse.empty())
35 {
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -050036 error("Error reading mapper response");
Lei YU86c83f32018-07-13 15:14:56 +080037 throw std::runtime_error("Error reading mapper response");
38 }
George Liu261525d2022-07-01 17:02:55 +080039
Lei YU86c83f32018-07-13 15:14:56 +080040 return mapperResponse[0].first;
41 }
Patrick Williams38679262022-07-22 19:26:55 -050042 catch (const sdbusplus::exception_t& ex)
Lei YUdd8e9e42017-04-19 17:46:58 +080043 {
Pavithra Barithayadd42c7f2022-08-11 05:09:02 -050044 error(
George Liu947b5342022-07-01 16:12:18 +080045 "Mapper call failed: path:{PATH}, interface:{INTF}, error:{ERROR}",
46 "PATH", path, "INTF", interface, "ERROR", ex);
Lei YU86c83f32018-07-13 15:14:56 +080047 throw std::runtime_error("Mapper call failed");
Lei YUdd8e9e42017-04-19 17:46:58 +080048 }
Lei YUdd8e9e42017-04-19 17:46:58 +080049}
50
George Liudc746c02022-09-02 11:10:55 +080051MapperResponse getSubTree(sdbusplus::bus_t& bus, const std::string& root,
52 const Interfaces& interfaces, int32_t depth)
53{
Pavithra Barithaya864e1732023-04-11 04:30:23 -050054 auto mapperCall = bus.new_method_call(mapperBusname, mapperPath,
55 mapperInterface, "GetSubTree");
George Liudc746c02022-09-02 11:10:55 +080056 mapperCall.append(root);
57 mapperCall.append(depth);
58 mapperCall.append(interfaces);
59
60 auto response = bus.call(mapperCall);
61
62 MapperResponse result;
63 response.read(result);
64 return result;
65}
66
Lei YUddd54422017-04-18 16:38:44 +080067Mode strToMode(const std::string& mode)
68{
Lei YUad143542017-07-25 14:27:07 +080069 return ModeSetting::convertMethodFromString(mode);
Lei YUddd54422017-04-18 16:38:44 +080070}
71
Lei YUad143542017-07-25 14:27:07 +080072std::string modeToStr(Mode mode)
Lei YUddd54422017-04-18 16:38:44 +080073{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050074 return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage(
75 mode);
Lei YUddd54422017-04-18 16:38:44 +080076}
77
Lei YUddd54422017-04-18 16:38:44 +080078} // namespace utils
79} // namespace time
80} // namespace phosphor