blob: 1bb979e75b584639e438d50d3b59683e45683235 [file] [log] [blame]
Brandon Wyman5914f652017-03-16 18:17:07 -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 <string>
17#include "utility.hpp"
18
19namespace phosphor
20{
21namespace fan
22{
Matt Spinler5cfdf942017-04-10 14:25:47 -050023namespace util
Brandon Wyman5914f652017-03-16 18:17:07 -050024{
25
Matt Spinler5cfdf942017-04-10 14:25:47 -050026using namespace std::string_literals;
27
Dinesh Chinari618027a2017-06-26 23:26:50 -050028using namespace phosphor::logging;
29using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
30 Error::InternalFailure;
31
Matt Spinler5cfdf942017-04-10 14:25:47 -050032std::string getService(const std::string& path,
33 const std::string& interface,
34 sdbusplus::bus::bus& bus)
35{
Brandon Wyman5914f652017-03-16 18:17:07 -050036 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME,
37 MAPPER_PATH,
38 MAPPER_INTERFACE,
39 "GetObject");
40
Matt Spinler5cfdf942017-04-10 14:25:47 -050041 mapperCall.append(path);
42 mapperCall.append(std::vector<std::string>({interface}));
Brandon Wyman5914f652017-03-16 18:17:07 -050043
44 auto mapperResponseMsg = bus.call(mapperCall);
45 if (mapperResponseMsg.is_method_error())
46 {
Dinesh Chinari618027a2017-06-26 23:26:50 -050047 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 elog<InternalFailure>();
Brandon Wyman5914f652017-03-16 18:17:07 -050051 }
52
Matt Spinler5cfdf942017-04-10 14:25:47 -050053
Brandon Wyman5914f652017-03-16 18:17:07 -050054 std::map<std::string, std::vector<std::string>> mapperResponse;
55 mapperResponseMsg.read(mapperResponse);
56
57 if (mapperResponse.empty())
58 {
Dinesh Chinari618027a2017-06-26 23:26:50 -050059 log<level::ERR>(
60 "Error in mapper response for getting service name",
61 entry("PATH=%s", path.c_str()),
62 entry("INTERFACE=%s", interface.c_str()));
63 elog<InternalFailure>();
Brandon Wyman5914f652017-03-16 18:17:07 -050064 }
65
66 return mapperResponse.begin()->first;
67}
68
69}
70}
71}