blob: 2cfeac8feb497fa2d0bb6a6e89914e7f6d6f1a12 [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
Brandon Wyman5914f652017-03-16 18:17:07 -050028//TODO Should get these from phosphor-objmgr config.h
29constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
30constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
31constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
32
33//TODO Should get these from phosphor-inventory-manager config.h
Matt Spinler5cfdf942017-04-10 14:25:47 -050034const auto INVENTORY_PATH = "/xyz/openbmc_project/inventory"s;
35const auto INVENTORY_INTF = "xyz.openbmc_project.Inventory.Manager"s;
Brandon Wyman5914f652017-03-16 18:17:07 -050036
37std::string getInvService(sdbusplus::bus::bus& bus)
38{
Matt Spinler5cfdf942017-04-10 14:25:47 -050039 return getService(INVENTORY_PATH, INVENTORY_INTF, bus);
40}
41
42
43std::string getService(const std::string& path,
44 const std::string& interface,
45 sdbusplus::bus::bus& bus)
46{
Brandon Wyman5914f652017-03-16 18:17:07 -050047 auto mapperCall = bus.new_method_call(MAPPER_BUSNAME,
48 MAPPER_PATH,
49 MAPPER_INTERFACE,
50 "GetObject");
51
Matt Spinler5cfdf942017-04-10 14:25:47 -050052 mapperCall.append(path);
53 mapperCall.append(std::vector<std::string>({interface}));
Brandon Wyman5914f652017-03-16 18:17:07 -050054
55 auto mapperResponseMsg = bus.call(mapperCall);
56 if (mapperResponseMsg.is_method_error())
57 {
58 throw std::runtime_error(
Matt Spinler5cfdf942017-04-10 14:25:47 -050059 "Error in mapper call to get service name");
Brandon Wyman5914f652017-03-16 18:17:07 -050060 }
61
Matt Spinler5cfdf942017-04-10 14:25:47 -050062
Brandon Wyman5914f652017-03-16 18:17:07 -050063 std::map<std::string, std::vector<std::string>> mapperResponse;
64 mapperResponseMsg.read(mapperResponse);
65
66 if (mapperResponse.empty())
67 {
68 throw std::runtime_error(
Matt Spinler5cfdf942017-04-10 14:25:47 -050069 "Error in mapper response for getting service name");
Brandon Wyman5914f652017-03-16 18:17:07 -050070 }
71
72 return mapperResponse.begin()->first;
73}
74
75}
76}
77}