blob: be29c74d17f20dc183cfce8b74040b454d8b3abb [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
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050018std::string getService(sdbusplus::bus::bus& 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 }
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050037 if (mapperResponse.size() < 1)
38 {
Lei YU86c83f32018-07-13 15:14:56 +080039 return "";
40 }
41 return mapperResponse[0].first;
42 }
Patrick Williams295b96b2021-09-02 09:50:14 -050043 catch (const sdbusplus::exception::exception& ex)
Lei YUdd8e9e42017-04-19 17:46:58 +080044 {
George Liu947b5342022-07-01 16:12:18 +080045 lg2::error(
46 "Mapper call failed: path:{PATH}, interface:{INTF}, error:{ERROR}",
47 "PATH", path, "INTF", interface, "ERROR", ex);
Lei YU86c83f32018-07-13 15:14:56 +080048 throw std::runtime_error("Mapper call failed");
Lei YUdd8e9e42017-04-19 17:46:58 +080049 }
Lei YUdd8e9e42017-04-19 17:46:58 +080050}
51
Lei YUddd54422017-04-18 16:38:44 +080052Mode strToMode(const std::string& mode)
53{
Lei YUad143542017-07-25 14:27:07 +080054 return ModeSetting::convertMethodFromString(mode);
Lei YUddd54422017-04-18 16:38:44 +080055}
56
Lei YUad143542017-07-25 14:27:07 +080057std::string modeToStr(Mode mode)
Lei YUddd54422017-04-18 16:38:44 +080058{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050059 return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage(
60 mode);
Lei YUddd54422017-04-18 16:38:44 +080061}
62
Lei YUddd54422017-04-18 16:38:44 +080063} // namespace utils
64} // namespace time
65} // namespace phosphor