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> |
| 14 | |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 15 | namespace phosphor::power::psu |
| 16 | { |
| 17 | |
Matt Spinler | 0975eaf | 2022-02-14 15:38:30 -0600 | [diff] [blame^] | 18 | using Property = std::string; |
| 19 | using Value = std::variant<bool, std::string>; |
| 20 | using PropertyMap = std::map<Property, Value>; |
| 21 | using Interface = std::string; |
| 22 | using InterfaceMap = std::map<Interface, PropertyMap>; |
| 23 | using Object = sdbusplus::message::object_path; |
| 24 | using ObjectMap = std::map<Object, InterfaceMap>; |
| 25 | |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 26 | class Util : public UtilBase |
| 27 | { |
| 28 | public: |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 29 | bool getPresence(sdbusplus::bus::bus& bus, |
| 30 | const std::string& invpath) const override |
| 31 | { |
| 32 | bool present = false; |
| 33 | |
| 34 | // Use getProperty utility function to get presence status. |
| 35 | util::getProperty(INVENTORY_IFACE, PRESENT_PROP, invpath, |
| 36 | INVENTORY_MGR_IFACE, bus, present); |
| 37 | |
| 38 | return present; |
| 39 | } |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 40 | |
| 41 | void setPresence(sdbusplus::bus::bus& bus, const std::string& invpath, |
| 42 | bool present, const std::string& name) const override |
| 43 | { |
| 44 | using InternalFailure = |
| 45 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 46 | PropertyMap invProp; |
| 47 | |
| 48 | invProp.emplace("Present", present); |
| 49 | invProp.emplace("PrettyName", name); |
| 50 | |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 51 | InterfaceMap invIntf; |
| 52 | invIntf.emplace("xyz.openbmc_project.Inventory.Item", |
| 53 | std::move(invProp)); |
| 54 | |
| 55 | Interface extraIface = "xyz.openbmc_project.Inventory.Item.PowerSupply"; |
| 56 | |
| 57 | invIntf.emplace(extraIface, PropertyMap()); |
| 58 | |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 59 | ObjectMap invObj; |
| 60 | invObj.emplace(std::move(invpath), std::move(invIntf)); |
| 61 | |
| 62 | using namespace phosphor::logging; |
| 63 | log<level::INFO>(fmt::format("Updating inventory present property. " |
| 64 | "present:{} invpath:{} name:{}", |
| 65 | present, invpath, name) |
| 66 | .c_str()); |
| 67 | |
| 68 | try |
| 69 | { |
| 70 | auto invService = phosphor::power::util::getService( |
| 71 | INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus); |
| 72 | |
| 73 | // Update inventory |
| 74 | auto invMsg = |
| 75 | bus.new_method_call(invService.c_str(), INVENTORY_OBJ_PATH, |
| 76 | INVENTORY_MGR_IFACE, "Notify"); |
| 77 | invMsg.append(std::move(invObj)); |
| 78 | auto invMgrResponseMsg = bus.call(invMsg); |
| 79 | } |
| 80 | catch (const std::exception& e) |
| 81 | { |
| 82 | log<level::ERR>( |
| 83 | fmt::format( |
| 84 | "Error in inventory manager call to update inventory: {}", |
| 85 | e.what()) |
| 86 | .c_str()); |
| 87 | elog<InternalFailure>(); |
| 88 | } |
| 89 | } |
Matt Spinler | 0975eaf | 2022-02-14 15:38:30 -0600 | [diff] [blame^] | 90 | |
| 91 | void setAvailable(sdbusplus::bus::bus& bus, const std::string& invpath, |
| 92 | bool available) const override |
| 93 | { |
| 94 | PropertyMap invProp; |
| 95 | InterfaceMap invIntf; |
| 96 | ObjectMap invObj; |
| 97 | |
| 98 | invProp.emplace(AVAILABLE_PROP, available); |
| 99 | invIntf.emplace(AVAILABILITY_IFACE, std::move(invProp)); |
| 100 | |
| 101 | invObj.emplace(std::move(invpath), std::move(invIntf)); |
| 102 | |
| 103 | try |
| 104 | { |
| 105 | |
| 106 | auto invService = phosphor::power::util::getService( |
| 107 | INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus); |
| 108 | |
| 109 | auto invMsg = |
| 110 | bus.new_method_call(invService.c_str(), INVENTORY_OBJ_PATH, |
| 111 | INVENTORY_MGR_IFACE, "Notify"); |
| 112 | invMsg.append(std::move(invObj)); |
| 113 | auto invMgrResponseMsg = bus.call(invMsg); |
| 114 | } |
| 115 | catch (const sdbusplus::exception::exception& e) |
| 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 | } |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 128 | std::unique_ptr<GPIOInterfaceBase> createGPIO(const std::string& namedGpio); |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 129 | |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 130 | class GPIOInterface : public GPIOInterfaceBase |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 131 | { |
| 132 | public: |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 133 | GPIOInterface() = delete; |
| 134 | virtual ~GPIOInterface() = default; |
| 135 | GPIOInterface(const GPIOInterface&) = default; |
| 136 | GPIOInterface& operator=(const GPIOInterface&) = default; |
| 137 | GPIOInterface(GPIOInterface&&) = default; |
| 138 | GPIOInterface& operator=(GPIOInterface&&) = default; |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * Constructor |
| 142 | * |
| 143 | * @param[in] namedGpio - The string for the gpio-line-name |
| 144 | */ |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 145 | GPIOInterface(const std::string& namedGpio); |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 146 | |
Adriana Kobylak | 3ca062a | 2021-10-20 15:27:23 +0000 | [diff] [blame] | 147 | static std::unique_ptr<GPIOInterfaceBase> |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 148 | createGPIO(const std::string& namedGpio); |
| 149 | |
| 150 | /** |
| 151 | * @brief Attempts to read the state of the GPIO line. |
| 152 | * |
| 153 | * Throws an exception if line not found, request line fails, or get_value |
| 154 | * from line fails. |
| 155 | * |
| 156 | * @return 1 for active (low/present), 0 for not active (high/not present). |
| 157 | */ |
| 158 | int read() override; |
| 159 | |
B. J. Wyman | d8b8cb1 | 2021-07-15 22:03:34 +0000 | [diff] [blame] | 160 | /** |
Adriana Kobylak | 52245b6 | 2021-09-13 15:46:21 +0000 | [diff] [blame] | 161 | * @brief Attempts to set the state of the GPIO line to the specified value. |
| 162 | * |
| 163 | * Throws an exception if line not found, request line fails, or set_value |
| 164 | * to line fails. |
| 165 | * |
| 166 | * @param[in] value - The value to set the state of the GPIO line, 1 or 0. |
| 167 | * @param[in] flags - Additional line request flags as defined in gpiod.hpp. |
| 168 | */ |
| 169 | void write(int value, std::bitset<32> flags) override; |
| 170 | |
| 171 | /** |
B. J. Wyman | d8b8cb1 | 2021-07-15 22:03:34 +0000 | [diff] [blame] | 172 | * @brief Returns the name of the GPIO, if not empty. |
| 173 | */ |
| 174 | std::string getName() const override; |
| 175 | |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 176 | private: |
| 177 | gpiod::line line; |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | } // namespace phosphor::power::psu |