blob: 8fe02dc557d8fe92c69810429e2c4872ad457f4c [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
Lei YU7dc31bb2019-08-30 10:07:08 +080018#include <fstream>
19
Lei YUab093322019-10-09 16:43:22 +080020namespace phosphor
Matt Spinler974c9162017-08-04 08:36:37 -050021{
22namespace power
23{
24namespace util
25{
26
27constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
28constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
29constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
30
31using namespace phosphor::logging;
Lei YU7dc31bb2019-08-30 10:07:08 +080032using json = nlohmann::json;
Matt Spinler48b4a432017-08-04 11:57:37 -050033
Matt Spinlerf0f02b92018-10-25 16:12:43 -050034std::string getService(const std::string& path, const std::string& interface,
Matt Spinler974c9162017-08-04 08:36:37 -050035 sdbusplus::bus::bus& bus)
36{
Matt Spinlerf0f02b92018-10-25 16:12:43 -050037 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
38 MAPPER_INTERFACE, "GetObject");
Matt Spinler974c9162017-08-04 08:36:37 -050039
40 method.append(path);
41 method.append(std::vector<std::string>({interface}));
42
43 auto reply = bus.call(method);
Matt Spinler974c9162017-08-04 08:36:37 -050044
45 std::map<std::string, std::vector<std::string>> response;
46 reply.read(response);
47
48 if (response.empty())
49 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -050050 log<level::ERR>("Error in mapper response for getting service name",
51 entry("PATH=%s", path.c_str()),
52 entry("INTERFACE=%s", interface.c_str()));
Matt Spinler974c9162017-08-04 08:36:37 -050053 return std::string{};
54 }
55
56 return response.begin()->first;
57}
58
Lei YU7dc31bb2019-08-30 10:07:08 +080059json loadJSONFromFile(const char* path)
60{
61 std::ifstream ifs(path);
62 if (!ifs.good())
63 {
64 log<level::ERR>("Unable to open file", entry("PATH=%s", path));
65 return nullptr;
66 }
67 auto data = json::parse(ifs, nullptr, false);
68 if (data.is_discarded())
69 {
70 log<level::ERR>("Failed to parse json", entry("PATH=%s", path));
71 return nullptr;
72 }
73 return data;
74}
75
Matt Spinlerf0f02b92018-10-25 16:12:43 -050076} // namespace util
77} // namespace power
Lei YUab093322019-10-09 16:43:22 +080078} // namespace phosphor