blob: bd842499fa47223f0f7625d2b7e660f32eda9c32 [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
Lei YU40705462019-10-09 17:07:11 +080076phosphor::pmbus::Type getPMBusAccessType(const json& json)
77{
78 using namespace phosphor::pmbus;
79 Type type;
80
81 auto typeStr = json.at("inventoryPMBusAccessType");
82
83 if (typeStr == "Hwmon")
84 {
85 type = Type::Hwmon;
86 }
87 else if (typeStr == "DeviceDebug")
88 {
89 type = Type::DeviceDebug;
90 }
91 else if (typeStr == "Debug")
92 {
93 type = Type::Debug;
94 }
95 else if (typeStr == "HwmonDeviceDebug")
96 {
97 type = Type::HwmonDeviceDebug;
98 }
99 else
100 {
101 type = Type::Base;
102 }
103 return type;
104}
105
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500106} // namespace util
107} // namespace power
Lei YUab093322019-10-09 16:43:22 +0800108} // namespace phosphor