Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 1 | #pragma once |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 2 | #include "util_base.hpp" |
| 3 | #include "utility.hpp" |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 4 | #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 Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 12 | |
Adriana Kobylak | 52245b6 | 2021-09-13 15:46:21 +0000 | [diff] [blame] | 13 | #include <bitset> |
Brandon Wyman | 18a24d9 | 2022-04-19 22:48:34 +0000 | [diff] [blame] | 14 | #include <chrono> |
Adriana Kobylak | 52245b6 | 2021-09-13 15:46:21 +0000 | [diff] [blame] | 15 | |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 16 | namespace phosphor::power::psu |
| 17 | { |
| 18 | |
Matt Spinler | 0975eaf | 2022-02-14 15:38:30 -0600 | [diff] [blame] | 19 | using Property = std::string; |
| 20 | using Value = std::variant<bool, std::string>; |
| 21 | using PropertyMap = std::map<Property, Value>; |
| 22 | using Interface = std::string; |
| 23 | using InterfaceMap = std::map<Interface, PropertyMap>; |
| 24 | using Object = sdbusplus::message::object_path; |
| 25 | using ObjectMap = std::map<Object, InterfaceMap>; |
| 26 | |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 27 | class Util : public UtilBase |
| 28 | { |
| 29 | public: |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 30 | bool getPresence(sdbusplus::bus_t& bus, |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 31 | 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. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 41 | |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 42 | void setPresence(sdbusplus::bus_t& bus, const std::string& invpath, |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 43 | bool present, const std::string& name) const override |
| 44 | { |
| 45 | using InternalFailure = |
| 46 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 47 | PropertyMap invProp; |
| 48 | |
| 49 | invProp.emplace("Present", present); |
| 50 | invProp.emplace("PrettyName", name); |
| 51 | |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 52 | 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. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 60 | 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 |
Patrick Williams | 48781ae | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 75 | auto invMsg = bus.new_method_call(invService.c_str(), |
| 76 | INVENTORY_OBJ_PATH, |
| 77 | INVENTORY_MGR_IFACE, "Notify"); |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 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 Spinler | 0975eaf | 2022-02-14 15:38:30 -0600 | [diff] [blame] | 91 | |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 92 | void setAvailable(sdbusplus::bus_t& bus, const std::string& invpath, |
Matt Spinler | 0975eaf | 2022-02-14 15:38:30 -0600 | [diff] [blame] | 93 | 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 | { |
Matt Spinler | 0975eaf | 2022-02-14 15:38:30 -0600 | [diff] [blame] | 106 | auto invService = phosphor::power::util::getService( |
| 107 | INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus); |
| 108 | |
Patrick Williams | 48781ae | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 109 | auto invMsg = bus.new_method_call(invService.c_str(), |
| 110 | INVENTORY_OBJ_PATH, |
| 111 | INVENTORY_MGR_IFACE, "Notify"); |
Matt Spinler | 0975eaf | 2022-02-14 15:38:30 -0600 | [diff] [blame] | 112 | invMsg.append(std::move(invObj)); |
| 113 | auto invMgrResponseMsg = bus.call(invMsg); |
| 114 | } |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 115 | catch (const sdbusplus::exception_t& e) |
Matt Spinler | 0975eaf | 2022-02-14 15:38:30 -0600 | [diff] [blame] | 116 | { |
| 117 | using namespace phosphor::logging; |
| 118 | log<level::ERR>( |
| 119 | fmt::format("Error in inventory manager call to update " |
| 120 | "availability interface: {}", |
| 121 | e.what()) |
| 122 | .c_str()); |
| 123 | throw; |
| 124 | } |
| 125 | } |
Matt Spinler | ca1e9ea | 2022-02-18 14:03:08 -0600 | [diff] [blame] | 126 | |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 127 | void handleChassisHealthRollup(sdbusplus::bus_t& bus, |
Matt Spinler | ca1e9ea | 2022-02-18 14:03:08 -0600 | [diff] [blame] | 128 | const std::string& invpath, |
| 129 | bool addRollup) const override |
| 130 | { |
| 131 | using AssociationTuple = |
| 132 | std::tuple<std::string, std::string, std::string>; |
| 133 | using AssociationsProperty = std::vector<AssociationTuple>; |
| 134 | try |
| 135 | { |
| 136 | auto chassisPath = getChassis(bus, invpath); |
| 137 | |
| 138 | auto service = phosphor::power::util::getService( |
| 139 | invpath, ASSOC_DEF_IFACE, bus); |
| 140 | |
| 141 | AssociationsProperty associations; |
| 142 | phosphor::power::util::getProperty<AssociationsProperty>( |
| 143 | ASSOC_DEF_IFACE, ASSOC_PROP, invpath, service, bus, |
| 144 | associations); |
| 145 | |
| 146 | AssociationTuple critAssociation{"health_rollup", "critical", |
| 147 | chassisPath}; |
| 148 | |
| 149 | auto assocIt = std::find(associations.begin(), associations.end(), |
| 150 | critAssociation); |
| 151 | if (addRollup) |
| 152 | { |
| 153 | if (assocIt != associations.end()) |
| 154 | { |
| 155 | // It's already there |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | associations.push_back(critAssociation); |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | if (assocIt == associations.end()) |
| 164 | { |
| 165 | // It's already been removed. |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | // If the object still isn't functional, then don't clear |
| 170 | // the association. |
| 171 | bool functional = false; |
| 172 | phosphor::power::util::getProperty<bool>( |
| 173 | OPERATIONAL_STATE_IFACE, FUNCTIONAL_PROP, invpath, service, |
| 174 | bus, functional); |
| 175 | |
| 176 | if (!functional) |
| 177 | { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | associations.erase(assocIt); |
| 182 | } |
| 183 | |
| 184 | phosphor::power::util::setProperty(ASSOC_DEF_IFACE, ASSOC_PROP, |
| 185 | invpath, service, bus, |
| 186 | associations); |
| 187 | } |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 188 | catch (const sdbusplus::exception_t& e) |
Matt Spinler | ca1e9ea | 2022-02-18 14:03:08 -0600 | [diff] [blame] | 189 | { |
| 190 | using namespace phosphor::logging; |
| 191 | log<level::INFO>(fmt::format("Error trying to handle health rollup " |
| 192 | "associations for {}: {}", |
| 193 | invpath, e.what()) |
| 194 | .c_str()); |
| 195 | } |
| 196 | } |
| 197 | |
Patrick Williams | 7354ce6 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 198 | std::string getChassis(sdbusplus::bus_t& bus, |
Matt Spinler | ca1e9ea | 2022-02-18 14:03:08 -0600 | [diff] [blame] | 199 | const std::string& invpath) const |
| 200 | { |
Matt Spinler | 0659422 | 2023-05-01 10:44:43 -0500 | [diff] [blame] | 201 | sdbusplus::message::object_path assocPath = invpath + "/powering"; |
| 202 | sdbusplus::message::object_path basePath{"/"}; |
| 203 | std::vector<std::string> interfaces{CHASSIS_IFACE}; |
Matt Spinler | ca1e9ea | 2022-02-18 14:03:08 -0600 | [diff] [blame] | 204 | |
Matt Spinler | 0659422 | 2023-05-01 10:44:43 -0500 | [diff] [blame] | 205 | // Find the object path that implements the chassis interface |
| 206 | // and also shows up in the endpoints list of the powering assoc. |
| 207 | auto chassisPaths = phosphor::power::util::getAssociatedSubTreePaths( |
| 208 | bus, assocPath, basePath, interfaces, 0); |
Matt Spinler | ca1e9ea | 2022-02-18 14:03:08 -0600 | [diff] [blame] | 209 | |
Matt Spinler | 0659422 | 2023-05-01 10:44:43 -0500 | [diff] [blame] | 210 | if (chassisPaths.empty()) |
Matt Spinler | ca1e9ea | 2022-02-18 14:03:08 -0600 | [diff] [blame] | 211 | { |
Matt Spinler | 0659422 | 2023-05-01 10:44:43 -0500 | [diff] [blame] | 212 | throw std::runtime_error(fmt::format( |
| 213 | "No association to a chassis found for {}", invpath)); |
Matt Spinler | ca1e9ea | 2022-02-18 14:03:08 -0600 | [diff] [blame] | 214 | } |
| 215 | |
Matt Spinler | 0659422 | 2023-05-01 10:44:43 -0500 | [diff] [blame] | 216 | return chassisPaths[0]; |
Matt Spinler | ca1e9ea | 2022-02-18 14:03:08 -0600 | [diff] [blame] | 217 | } |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 220 | std::unique_ptr<GPIOInterfaceBase> createGPIO(const std::string& namedGpio); |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 221 | |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 222 | class GPIOInterface : public GPIOInterfaceBase |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 223 | { |
| 224 | public: |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 225 | GPIOInterface() = delete; |
| 226 | virtual ~GPIOInterface() = default; |
| 227 | GPIOInterface(const GPIOInterface&) = default; |
| 228 | GPIOInterface& operator=(const GPIOInterface&) = default; |
| 229 | GPIOInterface(GPIOInterface&&) = default; |
| 230 | GPIOInterface& operator=(GPIOInterface&&) = default; |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 231 | |
| 232 | /** |
| 233 | * Constructor |
| 234 | * |
| 235 | * @param[in] namedGpio - The string for the gpio-line-name |
| 236 | */ |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 237 | GPIOInterface(const std::string& namedGpio); |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 238 | |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 239 | static std::unique_ptr<GPIOInterfaceBase> |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 240 | createGPIO(const std::string& namedGpio); |
| 241 | |
| 242 | /** |
| 243 | * @brief Attempts to read the state of the GPIO line. |
| 244 | * |
| 245 | * Throws an exception if line not found, request line fails, or get_value |
| 246 | * from line fails. |
| 247 | * |
| 248 | * @return 1 for active (low/present), 0 for not active (high/not present). |
| 249 | */ |
| 250 | int read() override; |
| 251 | |
B. J. Wyman | d8b8cb1 | 2021-07-15 22:03:34 +0000 | [diff] [blame] | 252 | /** |
Adriana Kobylak | 52245b6 | 2021-09-13 15:46:21 +0000 | [diff] [blame] | 253 | * @brief Attempts to set the state of the GPIO line to the specified value. |
| 254 | * |
| 255 | * Throws an exception if line not found, request line fails, or set_value |
| 256 | * to line fails. |
| 257 | * |
| 258 | * @param[in] value - The value to set the state of the GPIO line, 1 or 0. |
| 259 | * @param[in] flags - Additional line request flags as defined in gpiod.hpp. |
| 260 | */ |
| 261 | void write(int value, std::bitset<32> flags) override; |
| 262 | |
| 263 | /** |
Brandon Wyman | 18a24d9 | 2022-04-19 22:48:34 +0000 | [diff] [blame] | 264 | * @brief Attempts to toggle (write) a GPIO low then high. |
| 265 | * |
| 266 | * Relies on write, so throws exception if line not found, etc. |
| 267 | * |
| 268 | * @param[in] delay - Milliseconds to delay betwen low/high toggle. |
| 269 | */ |
| 270 | void toggleLowHigh(const std::chrono::milliseconds& delay) override; |
| 271 | |
| 272 | /** |
B. J. Wyman | d8b8cb1 | 2021-07-15 22:03:34 +0000 | [diff] [blame] | 273 | * @brief Returns the name of the GPIO, if not empty. |
| 274 | */ |
| 275 | std::string getName() const override; |
| 276 | |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 277 | private: |
| 278 | gpiod::line line; |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | } // namespace phosphor::power::psu |