blob: c9a8fbcbb065cbc7017f510b233c9c602fb230c3 [file] [log] [blame]
Lei YUddd54422017-04-18 16:38:44 +08001#include "utils.hpp"
2
3#include <phosphor-logging/log.hpp>
4
Lei YUddd54422017-04-18 16:38:44 +08005namespace phosphor
6{
7namespace time
8{
9
10namespace // anonymous
11{
Lei YUdd8e9e42017-04-19 17:46:58 +080012constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
13constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
14constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050015} // namespace
Lei YUddd54422017-04-18 16:38:44 +080016
17namespace utils
18{
19
20using namespace phosphor::logging;
21
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050022std::string getService(sdbusplus::bus::bus& bus, const char* path,
Lei YUdd8e9e42017-04-19 17:46:58 +080023 const char* interface)
24{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050025 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
26 MAPPER_INTERFACE, "GetObject");
Lei YUdd8e9e42017-04-19 17:46:58 +080027
28 mapper.append(path, std::vector<std::string>({interface}));
Lei YU86c83f32018-07-13 15:14:56 +080029 try
Lei YUdd8e9e42017-04-19 17:46:58 +080030 {
Lei YU86c83f32018-07-13 15:14:56 +080031 auto mapperResponseMsg = bus.call(mapper);
Lei YUdd8e9e42017-04-19 17:46:58 +080032
Lei YU86c83f32018-07-13 15:14:56 +080033 std::vector<std::pair<std::string, std::vector<std::string>>>
34 mapperResponse;
35 mapperResponseMsg.read(mapperResponse);
36 if (mapperResponse.empty())
37 {
38 log<level::ERR>("Error reading mapper response");
39 throw std::runtime_error("Error reading mapper response");
40 }
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050041 if (mapperResponse.size() < 1)
42 {
Lei YU86c83f32018-07-13 15:14:56 +080043 return "";
44 }
45 return mapperResponse[0].first;
46 }
Patrick Williams295b96b2021-09-02 09:50:14 -050047 catch (const sdbusplus::exception::exception& ex)
Lei YUdd8e9e42017-04-19 17:46:58 +080048 {
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050049 log<level::ERR>("Mapper call failed", entry("METHOD=%d", "GetObject"),
Lei YU86c83f32018-07-13 15:14:56 +080050 entry("PATH=%s", path),
51 entry("INTERFACE=%s", interface));
52 throw std::runtime_error("Mapper call failed");
Lei YUdd8e9e42017-04-19 17:46:58 +080053 }
Lei YUdd8e9e42017-04-19 17:46:58 +080054}
55
Lei YUddd54422017-04-18 16:38:44 +080056Mode strToMode(const std::string& mode)
57{
Lei YUad143542017-07-25 14:27:07 +080058 return ModeSetting::convertMethodFromString(mode);
Lei YUddd54422017-04-18 16:38:44 +080059}
60
Lei YUad143542017-07-25 14:27:07 +080061std::string modeToStr(Mode mode)
Lei YUddd54422017-04-18 16:38:44 +080062{
Gunnar Millsab4cc6a2018-09-14 14:42:39 -050063 return sdbusplus::xyz::openbmc_project::Time::server::convertForMessage(
64 mode);
Lei YUddd54422017-04-18 16:38:44 +080065}
66
Lei YUddd54422017-04-18 16:38:44 +080067} // namespace utils
68} // namespace time
69} // namespace phosphor