blob: a55ffe83a19beeaba91e0cf0d815d1cc11a55a81 [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 YUcfc040c2019-10-29 17:10:26 +080018#include "types.hpp"
19
Lei YU7dc31bb2019-08-30 10:07:08 +080020#include <fstream>
21
Lei YUab093322019-10-09 16:43:22 +080022namespace phosphor
Matt Spinler974c9162017-08-04 08:36:37 -050023{
24namespace power
25{
26namespace util
27{
28
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
33using namespace phosphor::logging;
Lei YU7dc31bb2019-08-30 10:07:08 +080034using json = nlohmann::json;
Matt Spinler48b4a432017-08-04 11:57:37 -050035
Matt Spinlerf0f02b92018-10-25 16:12:43 -050036std::string getService(const std::string& path, const std::string& interface,
Matthew Barthd2624402020-02-03 15:35:12 -060037 sdbusplus::bus::bus& bus, bool logError)
Matt Spinler974c9162017-08-04 08:36:37 -050038{
Matt Spinlerf0f02b92018-10-25 16:12:43 -050039 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
40 MAPPER_INTERFACE, "GetObject");
Matt Spinler974c9162017-08-04 08:36:37 -050041
42 method.append(path);
43 method.append(std::vector<std::string>({interface}));
44
45 auto reply = bus.call(method);
Matt Spinler974c9162017-08-04 08:36:37 -050046
47 std::map<std::string, std::vector<std::string>> response;
48 reply.read(response);
49
50 if (response.empty())
51 {
Matthew Barthd2624402020-02-03 15:35:12 -060052 if (logError)
53 {
54 log<level::ERR>("Error in mapper response for getting service name",
55 entry("PATH=%s", path.c_str()),
56 entry("INTERFACE=%s", interface.c_str()));
57 }
Matt Spinler974c9162017-08-04 08:36:37 -050058 return std::string{};
59 }
60
61 return response.begin()->first;
62}
63
Lei YU7dc31bb2019-08-30 10:07:08 +080064json loadJSONFromFile(const char* path)
65{
66 std::ifstream ifs(path);
67 if (!ifs.good())
68 {
69 log<level::ERR>("Unable to open file", entry("PATH=%s", path));
70 return nullptr;
71 }
72 auto data = json::parse(ifs, nullptr, false);
73 if (data.is_discarded())
74 {
75 log<level::ERR>("Failed to parse json", entry("PATH=%s", path));
76 return nullptr;
77 }
78 return data;
79}
80
Lei YU40705462019-10-09 17:07:11 +080081phosphor::pmbus::Type getPMBusAccessType(const json& json)
82{
83 using namespace phosphor::pmbus;
84 Type type;
85
86 auto typeStr = json.at("inventoryPMBusAccessType");
87
88 if (typeStr == "Hwmon")
89 {
90 type = Type::Hwmon;
91 }
92 else if (typeStr == "DeviceDebug")
93 {
94 type = Type::DeviceDebug;
95 }
96 else if (typeStr == "Debug")
97 {
98 type = Type::Debug;
99 }
100 else if (typeStr == "HwmonDeviceDebug")
101 {
102 type = Type::HwmonDeviceDebug;
103 }
104 else
105 {
106 type = Type::Base;
107 }
108 return type;
109}
110
Lei YUe8c9cd62019-11-04 14:24:41 +0800111bool isPoweredOn(sdbusplus::bus::bus& bus, bool defaultState)
Lei YUcfc040c2019-10-29 17:10:26 +0800112{
Lei YUe8c9cd62019-11-04 14:24:41 +0800113 int32_t state = defaultState;
Lei YUcfc040c2019-10-29 17:10:26 +0800114
115 try
116 {
Lei YUe8c9cd62019-11-04 14:24:41 +0800117 // When state = 1, system is powered on
Lei YUcfc040c2019-10-29 17:10:26 +0800118 auto service = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus);
119 getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH, service, bus,
120 state);
121 }
122 catch (std::exception& e)
123 {
Lei YUe8c9cd62019-11-04 14:24:41 +0800124 log<level::INFO>("Failed to get power state.");
Lei YUcfc040c2019-10-29 17:10:26 +0800125 }
126 return state != 0;
127}
128
Lei YUe8c9cd62019-11-04 14:24:41 +0800129std::vector<std::string> getPSUInventoryPaths(sdbusplus::bus::bus& bus)
130{
131 std::vector<std::string> paths;
132 auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
133 MAPPER_INTERFACE, "GetSubTreePaths");
134 method.append(INVENTORY_OBJ_PATH);
135 method.append(0); // Depth 0 to search all
136 method.append(std::vector<std::string>({PSU_INVENTORY_IFACE}));
137 auto reply = bus.call(method);
138
139 reply.read(paths);
140 return paths;
141}
142
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500143} // namespace util
144} // namespace power
Lei YUab093322019-10-09 16:43:22 +0800145} // namespace phosphor