blob: ac614d4eadb1dbbecf2c4e396a6b85fc786ef15a [file] [log] [blame]
Matt Spinler974c9162017-08-04 08:36:37 -05001/**
2 * Copyright © 2017 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "utility.hpp"
17
18namespace witherspoon
19{
20namespace power
21{
22namespace util
23{
24
25constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
26constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
27constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
28
29using namespace phosphor::logging;
Matt Spinler48b4a432017-08-04 11:57:37 -050030
Matt Spinler974c9162017-08-04 08:36:37 -050031
32std::string getService(const std::string& path,
33 const std::string& interface,
34 sdbusplus::bus::bus& bus)
35{
36 auto method = bus.new_method_call(MAPPER_BUSNAME,
37 MAPPER_PATH,
38 MAPPER_INTERFACE,
39 "GetObject");
40
41 method.append(path);
42 method.append(std::vector<std::string>({interface}));
43
44 auto reply = bus.call(method);
45 if (reply.is_method_error())
46 {
47 log<level::ERR>("Error in mapper call to get service name",
48 entry("PATH=%s", path.c_str()),
49 entry("INTERFACE=%s", interface.c_str()));
50
51 // TODO openbmc/openbmc#851 - Once available, throw returned error
52 throw std::runtime_error("Error in mapper call to get service name");
53 }
54
55 std::map<std::string, std::vector<std::string>> response;
56 reply.read(response);
57
58 if (response.empty())
59 {
60 log<level::ERR>(
61 "Error in mapper response for getting service name",
62 entry("PATH=%s", path.c_str()),
63 entry("INTERFACE=%s", interface.c_str()));
64 return std::string{};
65 }
66
67 return response.begin()->first;
68}
69
Matt Spinler974c9162017-08-04 08:36:37 -050070}
71}
72}