blob: e9918bbea94a0671cc8d8762837c634872756a8c [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 Spinlerf0f02b92018-10-25 16:12:43 -050031std::string getService(const std::string& path, const std::string& interface,
Matt Spinler974c9162017-08-04 08:36:37 -050032 sdbusplus::bus::bus& bus)
33{
Matt Spinlerf0f02b92018-10-25 16:12:43 -050034 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
35 MAPPER_INTERFACE, "GetObject");
Matt Spinler974c9162017-08-04 08:36:37 -050036
37 method.append(path);
38 method.append(std::vector<std::string>({interface}));
39
40 auto reply = bus.call(method);
41 if (reply.is_method_error())
42 {
43 log<level::ERR>("Error in mapper call to get service name",
Matt Spinlerf0f02b92018-10-25 16:12:43 -050044 entry("PATH=%s", path.c_str()),
45 entry("INTERFACE=%s", interface.c_str()));
Matt Spinler974c9162017-08-04 08:36:37 -050046
47 // TODO openbmc/openbmc#851 - Once available, throw returned error
48 throw std::runtime_error("Error in mapper call to get service name");
49 }
50
51 std::map<std::string, std::vector<std::string>> response;
52 reply.read(response);
53
54 if (response.empty())
55 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -050056 log<level::ERR>("Error in mapper response for getting service name",
57 entry("PATH=%s", path.c_str()),
58 entry("INTERFACE=%s", interface.c_str()));
Matt Spinler974c9162017-08-04 08:36:37 -050059 return std::string{};
60 }
61
62 return response.begin()->first;
63}
64
Matt Spinlerf0f02b92018-10-25 16:12:43 -050065} // namespace util
66} // namespace power
67} // namespace witherspoon