blob: 419e1a7d4d3376ae045cf8bd4fd0aa8fcc97bcb8 [file] [log] [blame]
Brandon Wymana0f33ce2019-10-17 18:32:29 -05001#pragma once
2
Brandon Wymanaed1f752019-11-25 18:10:52 -06003#include "types.hpp"
4
5#include <sdbusplus/bus/match.hpp>
6
Brandon Wymana0f33ce2019-10-17 18:32:29 -05007namespace phosphor::power::psu
8{
9/**
10 * @class PowerSupply
11 * Represents a PMBus power supply device.
12 */
13class PowerSupply
14{
15 public:
Brandon Wymanaed1f752019-11-25 18:10:52 -060016 PowerSupply() = delete;
Brandon Wymana0f33ce2019-10-17 18:32:29 -050017 PowerSupply(const PowerSupply&) = delete;
18 PowerSupply(PowerSupply&&) = delete;
19 PowerSupply& operator=(const PowerSupply&) = delete;
20 PowerSupply& operator=(PowerSupply&&) = delete;
21 ~PowerSupply() = default;
22
23 /**
Brandon Wymanc63941c2020-01-27 16:49:33 -060024 * @param[in] invpath - String for inventory path to use
25 * @param[in] i2cbus - The bus number this power supply is on
26 * @param[in] i2caddr - The 16-bit I2C address of the power supply
Brandon Wymanaed1f752019-11-25 18:10:52 -060027 */
Brandon Wymanc63941c2020-01-27 16:49:33 -060028 PowerSupply(sdbusplus::bus::bus& bus, const std::string& invpath,
29 std::uint8_t i2cbus, const std::string& i2caddr) :
30 bus(bus),
31 inventoryPath(invpath), i2cbus(i2cbus), i2caddr(i2caddr)
Brandon Wymanaed1f752019-11-25 18:10:52 -060032 {
33 // Setup the functions to call when the D-Bus inventory path for the
34 // Present property changes.
35 presentMatch = std::make_unique<sdbusplus::bus::match_t>(
36 bus,
37 sdbusplus::bus::match::rules::propertiesChanged(inventoryPath,
38 INVENTORY_IFACE),
39 [this](auto& msg) { this->inventoryChanged(msg); });
40 presentAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
41 bus,
42 sdbusplus::bus::match::rules::interfacesAdded() +
43 sdbusplus::bus::match::rules::path_namespace(inventoryPath),
44 [this](auto& msg) { this->inventoryChanged(msg); });
45 // Get the current state of the Present property.
46 updatePresence();
47 }
48
49 /**
Brandon Wymana0f33ce2019-10-17 18:32:29 -050050 * Power supply specific function to analyze for faults/errors.
51 *
52 * Various PMBus status bits will be checked for fault conditions.
53 * If a certain fault bits are on, the appropriate error will be
54 * committed.
55 */
56 void analyze()
57 {
58 }
59
60 /**
61 * Write PMBus CLEAR_FAULTS
62 *
63 * This function will be called in various situations in order to clear
64 * any fault status bits that may have been set, in order to start over
65 * with a clean state. Presence changes and power state changes will
66 * want to clear any faults logged.
67 */
68 void clearFaults()
69 {
70 }
71
72 /**
73 * @brief Adds properties to the inventory.
74 *
75 * Reads the values from the device and writes them to the
76 * associated power supply D-Bus inventory object.
77 *
78 * This needs to be done on startup, and each time the presence
79 * state changes.
80 *
81 * Properties added:
82 * - Serial Number
83 * - Part Number
84 * - CCIN (Customer Card Identification Number) - added as the Model
85 * - Firmware version
86 */
87 void updateInventory()
88 {
89 }
90
Brandon Wymanaed1f752019-11-25 18:10:52 -060091 /**
92 * @brief Accessor function to indicate present status
93 */
94 bool isPresent() const
95 {
96 return present;
97 }
98
Brandon Wymana0f33ce2019-10-17 18:32:29 -050099 private:
Brandon Wymanaed1f752019-11-25 18:10:52 -0600100 /** @brief systemd bus member */
101 sdbusplus::bus::bus& bus;
102
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500103 /** @brief True if a fault has already been found and not cleared */
104 bool faultFound = false;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600105
106 /**
107 * @brief D-Bus path to use for this power supply's inventory status.
108 **/
109 std::string inventoryPath;
110
Brandon Wymanc63941c2020-01-27 16:49:33 -0600111 /**
112 * @brief I2C bus that this power supply is on.
113 */
114 std::uint8_t i2cbus;
115
116 /**
117 * @brief I2C address of this power supply.
118 *
119 * The PMBus device driver will put this in a path with 16-bit address,
120 * represented as a file path string.
121 */
122 std::string i2caddr;
123
Brandon Wymanaed1f752019-11-25 18:10:52 -0600124 /** @brief True if the power supply is present. */
125 bool present = false;
126
127 /** @brief D-Bus match variable used to subscribe to Present property
128 * changes.
129 **/
130 std::unique_ptr<sdbusplus::bus::match_t> presentMatch;
131
132 /** @brief D-Bus match variable used to subscribe for Present property
133 * interface added.
134 */
135 std::unique_ptr<sdbusplus::bus::match_t> presentAddedMatch;
136
137 /**
138 * @brief Updates the presence status by querying D-Bus
139 *
140 * The D-Bus inventory properties for this power supply will be read to
141 * determine if the power supply is present or not and update this
142 * object's present member variable to reflect current status.
143 **/
144 void updatePresence();
145
146 /**
147 * @brief Callback for inventory property changes
148 *
149 * Process change of Present property for power supply.
150 *
151 * @param[in] msg - Data associated with Present change signal
152 **/
153 void inventoryChanged(sdbusplus::message::message& msg);
Brandon Wymana0f33ce2019-10-17 18:32:29 -0500154};
155
156} // namespace phosphor::power::psu