blob: 9bf83cbe780c99c0a192ddadeb9c4838afdfa2ee [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{
Lei YUdd8e9e42017-04-19 17:46:58 +080010constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
11constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
12constexpr auto MAPPER_INTERFACE = "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
Patrick Williams38679262022-07-22 19:26:55 -050018std::string getService(sdbusplus::bus_t& bus, const char* path,
Lei YUdd8e9e42017-04-19 17:46:58 +080019 const char* interface)
20{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050021 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
22 MAPPER_INTERFACE, "GetObject");
Lei YUdd8e9e42017-04-19 17:46:58 +080023
24 mapper.append(path, std::vector<std::string>({interface}));
Lei YU86c83f32018-07-13 15:14:56 +080025 try
Lei YUdd8e9e42017-04-19 17:46:58 +080026 {
Lei YU86c83f32018-07-13 15:14:56 +080027 auto mapperResponseMsg = bus.call(mapper);
Lei YUdd8e9e42017-04-19 17:46:58 +080028
Lei YU86c83f32018-07-13 15:14:56 +080029 std::vector<std::pair<std::string, std::vector<std::string>>>
30 mapperResponse;
31 mapperResponseMsg.read(mapperResponse);
32 if (mapperResponse.empty())
33 {
George Liu947b5342022-07-01 16:12:18 +080034 lg2::error("Error reading mapper response");
Lei YU86c83f32018-07-13 15:14:56 +080035 throw std::runtime_error("Error reading mapper response");
36 }
George Liu261525d2022-07-01 17:02:55 +080037
Lei YU86c83f32018-07-13 15:14:56 +080038 return mapperResponse[0].first;
39 }
Patrick Williams38679262022-07-22 19:26:55 -050040 catch (const sdbusplus::exception_t& ex)
Lei YUdd8e9e42017-04-19 17:46:58 +080041 {
George Liu947b5342022-07-01 16:12:18 +080042 lg2::error(
43 "Mapper call failed: path:{PATH}, interface:{INTF}, error:{ERROR}",
44 "PATH", path, "INTF", interface, "ERROR", ex);
Lei YU86c83f32018-07-13 15:14:56 +080045 throw std::runtime_error("Mapper call failed");
Lei YUdd8e9e42017-04-19 17:46:58 +080046 }
Lei YUdd8e9e42017-04-19 17:46:58 +080047}
48
Lei YUddd54422017-04-18 16:38:44 +080049Mode strToMode(const std::string& mode)
50{
Lei YUad143542017-07-25 14:27:07 +080051 return ModeSetting::convertMethodFromString(mode);
Lei YUddd54422017-04-18 16:38:44 +080052}
53
Lei YUad143542017-07-25 14:27:07 +080054std::string modeToStr(Mode mode)
Lei YUddd54422017-04-18 16:38:44 +080055{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050056 return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage(
57 mode);
Lei YUddd54422017-04-18 16:38:44 +080058}
59
Lei YUddd54422017-04-18 16:38:44 +080060} // namespace utils
61} // namespace time
62} // namespace phosphor