blob: b09731a99f78678ff2dbb66ec33f6c5e6b81be8f [file] [log] [blame]
Brandon Wyman3f1242f2020-01-28 13:11:25 -06001#pragma once
Brandon Wyman3f1242f2020-01-28 13:11:25 -06002#include "util_base.hpp"
3#include "utility.hpp"
B. J. Wyman681b2a32021-04-20 22:31:22 +00004#include "xyz/openbmc_project/Common/error.hpp"
5
6#include <fmt/format.h>
7
8#include <gpiod.hpp>
9#include <phosphor-logging/elog-errors.hpp>
10#include <phosphor-logging/elog.hpp>
11#include <phosphor-logging/log.hpp>
Brandon Wyman3f1242f2020-01-28 13:11:25 -060012
Adriana Kobylak52245b62021-09-13 15:46:21 +000013#include <bitset>
Brandon Wyman18a24d92022-04-19 22:48:34 +000014#include <chrono>
Adriana Kobylak52245b62021-09-13 15:46:21 +000015
Brandon Wyman3f1242f2020-01-28 13:11:25 -060016namespace phosphor::power::psu
17{
18
Matt Spinler0975eaf2022-02-14 15:38:30 -060019using Property = std::string;
20using Value = std::variant<bool, std::string>;
21using PropertyMap = std::map<Property, Value>;
22using Interface = std::string;
23using InterfaceMap = std::map<Interface, PropertyMap>;
24using Object = sdbusplus::message::object_path;
25using ObjectMap = std::map<Object, InterfaceMap>;
26
Brandon Wyman3f1242f2020-01-28 13:11:25 -060027class Util : public UtilBase
28{
29 public:
Patrick Williams7354ce62022-07-22 19:26:56 -050030 bool getPresence(sdbusplus::bus_t& bus,
Brandon Wyman3f1242f2020-01-28 13:11:25 -060031 const std::string& invpath) const override
32 {
33 bool present = false;
34
35 // Use getProperty utility function to get presence status.
36 util::getProperty(INVENTORY_IFACE, PRESENT_PROP, invpath,
37 INVENTORY_MGR_IFACE, bus, present);
38
39 return present;
40 }
B. J. Wyman681b2a32021-04-20 22:31:22 +000041
Patrick Williams7354ce62022-07-22 19:26:56 -050042 void setPresence(sdbusplus::bus_t& bus, const std::string& invpath,
B. J. Wyman681b2a32021-04-20 22:31:22 +000043 bool present, const std::string& name) const override
44 {
45 using InternalFailure =
46 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
B. J. Wyman681b2a32021-04-20 22:31:22 +000047 PropertyMap invProp;
48
49 invProp.emplace("Present", present);
50 invProp.emplace("PrettyName", name);
51
B. J. Wyman681b2a32021-04-20 22:31:22 +000052 InterfaceMap invIntf;
53 invIntf.emplace("xyz.openbmc_project.Inventory.Item",
54 std::move(invProp));
55
56 Interface extraIface = "xyz.openbmc_project.Inventory.Item.PowerSupply";
57
58 invIntf.emplace(extraIface, PropertyMap());
59
B. J. Wyman681b2a32021-04-20 22:31:22 +000060 ObjectMap invObj;
61 invObj.emplace(std::move(invpath), std::move(invIntf));
62
63 using namespace phosphor::logging;
64 log<level::INFO>(fmt::format("Updating inventory present property. "
65 "present:{} invpath:{} name:{}",
66 present, invpath, name)
67 .c_str());
68
69 try
70 {
71 auto invService = phosphor::power::util::getService(
72 INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
73
74 // Update inventory
75 auto invMsg =
76 bus.new_method_call(invService.c_str(), INVENTORY_OBJ_PATH,
77 INVENTORY_MGR_IFACE, "Notify");
78 invMsg.append(std::move(invObj));
79 auto invMgrResponseMsg = bus.call(invMsg);
80 }
81 catch (const std::exception& e)
82 {
83 log<level::ERR>(
84 fmt::format(
85 "Error in inventory manager call to update inventory: {}",
86 e.what())
87 .c_str());
88 elog<InternalFailure>();
89 }
90 }
Matt Spinler0975eaf2022-02-14 15:38:30 -060091
Patrick Williams7354ce62022-07-22 19:26:56 -050092 void setAvailable(sdbusplus::bus_t& bus, const std::string& invpath,
Matt Spinler0975eaf2022-02-14 15:38:30 -060093 bool available) const override
94 {
95 PropertyMap invProp;
96 InterfaceMap invIntf;
97 ObjectMap invObj;
98
99 invProp.emplace(AVAILABLE_PROP, available);
100 invIntf.emplace(AVAILABILITY_IFACE, std::move(invProp));
101
102 invObj.emplace(std::move(invpath), std::move(invIntf));
103
104 try
105 {
106
107 auto invService = phosphor::power::util::getService(
108 INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
109
110 auto invMsg =
111 bus.new_method_call(invService.c_str(), INVENTORY_OBJ_PATH,
112 INVENTORY_MGR_IFACE, "Notify");
113 invMsg.append(std::move(invObj));
114 auto invMgrResponseMsg = bus.call(invMsg);
115 }
Patrick Williams7354ce62022-07-22 19:26:56 -0500116 catch (const sdbusplus::exception_t& e)
Matt Spinler0975eaf2022-02-14 15:38:30 -0600117 {
118 using namespace phosphor::logging;
119 log<level::ERR>(
120 fmt::format("Error in inventory manager call to update "
121 "availability interface: {}",
122 e.what())
123 .c_str());
124 throw;
125 }
126 }
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600127
Patrick Williams7354ce62022-07-22 19:26:56 -0500128 void handleChassisHealthRollup(sdbusplus::bus_t& bus,
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600129 const std::string& invpath,
130 bool addRollup) const override
131 {
132 using AssociationTuple =
133 std::tuple<std::string, std::string, std::string>;
134 using AssociationsProperty = std::vector<AssociationTuple>;
135 try
136 {
137 auto chassisPath = getChassis(bus, invpath);
138
139 auto service = phosphor::power::util::getService(
140 invpath, ASSOC_DEF_IFACE, bus);
141
142 AssociationsProperty associations;
143 phosphor::power::util::getProperty<AssociationsProperty>(
144 ASSOC_DEF_IFACE, ASSOC_PROP, invpath, service, bus,
145 associations);
146
147 AssociationTuple critAssociation{"health_rollup", "critical",
148 chassisPath};
149
150 auto assocIt = std::find(associations.begin(), associations.end(),
151 critAssociation);
152 if (addRollup)
153 {
154 if (assocIt != associations.end())
155 {
156 // It's already there
157 return;
158 }
159
160 associations.push_back(critAssociation);
161 }
162 else
163 {
164 if (assocIt == associations.end())
165 {
166 // It's already been removed.
167 return;
168 }
169
170 // If the object still isn't functional, then don't clear
171 // the association.
172 bool functional = false;
173 phosphor::power::util::getProperty<bool>(
174 OPERATIONAL_STATE_IFACE, FUNCTIONAL_PROP, invpath, service,
175 bus, functional);
176
177 if (!functional)
178 {
179 return;
180 }
181
182 associations.erase(assocIt);
183 }
184
185 phosphor::power::util::setProperty(ASSOC_DEF_IFACE, ASSOC_PROP,
186 invpath, service, bus,
187 associations);
188 }
Patrick Williams7354ce62022-07-22 19:26:56 -0500189 catch (const sdbusplus::exception_t& e)
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600190 {
191 using namespace phosphor::logging;
192 log<level::INFO>(fmt::format("Error trying to handle health rollup "
193 "associations for {}: {}",
194 invpath, e.what())
195 .c_str());
196 }
197 }
198
Patrick Williams7354ce62022-07-22 19:26:56 -0500199 std::string getChassis(sdbusplus::bus_t& bus,
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600200 const std::string& invpath) const
201 {
Matt Spinler06594222023-05-01 10:44:43 -0500202 sdbusplus::message::object_path assocPath = invpath + "/powering";
203 sdbusplus::message::object_path basePath{"/"};
204 std::vector<std::string> interfaces{CHASSIS_IFACE};
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600205
Matt Spinler06594222023-05-01 10:44:43 -0500206 // Find the object path that implements the chassis interface
207 // and also shows up in the endpoints list of the powering assoc.
208 auto chassisPaths = phosphor::power::util::getAssociatedSubTreePaths(
209 bus, assocPath, basePath, interfaces, 0);
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600210
Matt Spinler06594222023-05-01 10:44:43 -0500211 if (chassisPaths.empty())
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600212 {
Matt Spinler06594222023-05-01 10:44:43 -0500213 throw std::runtime_error(fmt::format(
214 "No association to a chassis found for {}", invpath));
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600215 }
216
Matt Spinler06594222023-05-01 10:44:43 -0500217 return chassisPaths[0];
Matt Spinlerca1e9ea2022-02-18 14:03:08 -0600218 }
B. J. Wyman681b2a32021-04-20 22:31:22 +0000219};
220
Adriana Kobylak3ca062a2021-10-20 15:27:23 +0000221std::unique_ptr<GPIOInterfaceBase> createGPIO(const std::string& namedGpio);
B. J. Wyman681b2a32021-04-20 22:31:22 +0000222
Adriana Kobylak3ca062a2021-10-20 15:27:23 +0000223class GPIOInterface : public GPIOInterfaceBase
B. J. Wyman681b2a32021-04-20 22:31:22 +0000224{
225 public:
Adriana Kobylak3ca062a2021-10-20 15:27:23 +0000226 GPIOInterface() = delete;
227 virtual ~GPIOInterface() = default;
228 GPIOInterface(const GPIOInterface&) = default;
229 GPIOInterface& operator=(const GPIOInterface&) = default;
230 GPIOInterface(GPIOInterface&&) = default;
231 GPIOInterface& operator=(GPIOInterface&&) = default;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000232
233 /**
234 * Constructor
235 *
236 * @param[in] namedGpio - The string for the gpio-line-name
237 */
Adriana Kobylak3ca062a2021-10-20 15:27:23 +0000238 GPIOInterface(const std::string& namedGpio);
B. J. Wyman681b2a32021-04-20 22:31:22 +0000239
Adriana Kobylak3ca062a2021-10-20 15:27:23 +0000240 static std::unique_ptr<GPIOInterfaceBase>
B. J. Wyman681b2a32021-04-20 22:31:22 +0000241 createGPIO(const std::string& namedGpio);
242
243 /**
244 * @brief Attempts to read the state of the GPIO line.
245 *
246 * Throws an exception if line not found, request line fails, or get_value
247 * from line fails.
248 *
249 * @return 1 for active (low/present), 0 for not active (high/not present).
250 */
251 int read() override;
252
B. J. Wymand8b8cb12021-07-15 22:03:34 +0000253 /**
Adriana Kobylak52245b62021-09-13 15:46:21 +0000254 * @brief Attempts to set the state of the GPIO line to the specified value.
255 *
256 * Throws an exception if line not found, request line fails, or set_value
257 * to line fails.
258 *
259 * @param[in] value - The value to set the state of the GPIO line, 1 or 0.
260 * @param[in] flags - Additional line request flags as defined in gpiod.hpp.
261 */
262 void write(int value, std::bitset<32> flags) override;
263
264 /**
Brandon Wyman18a24d92022-04-19 22:48:34 +0000265 * @brief Attempts to toggle (write) a GPIO low then high.
266 *
267 * Relies on write, so throws exception if line not found, etc.
268 *
269 * @param[in] delay - Milliseconds to delay betwen low/high toggle.
270 */
271 void toggleLowHigh(const std::chrono::milliseconds& delay) override;
272
273 /**
B. J. Wymand8b8cb12021-07-15 22:03:34 +0000274 * @brief Returns the name of the GPIO, if not empty.
275 */
276 std::string getName() const override;
277
B. J. Wyman681b2a32021-04-20 22:31:22 +0000278 private:
279 gpiod::line line;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600280};
281
282} // namespace phosphor::power::psu