xref: /openbmc/openpower-hw-diags/util/dbus.hpp (revision a92dc027)
1 #pragma once
2 
3 #include <sdbusplus/bus.hpp>
4 #include <util/ffdc_file.hpp>
5 #include <util/trace.hpp>
6 
7 #include <string>
8 #include <variant>
9 #include <vector>
10 
11 namespace util
12 {
13 namespace dbus
14 {
15 using DBusValue = std::variant<std::string, bool, std::vector<uint8_t>,
16                                std::vector<std::string>, int32_t>;
17 using DBusProperty = std::string;
18 using DBusInterface = std::string;
19 using DBusService = std::string;
20 using DBusPath = std::string;
21 using DBusInterfaceList = std::vector<DBusInterface>;
22 using DBusSubTree =
23     std::map<DBusPath, std::map<DBusService, DBusInterfaceList>>;
24 
25 /**
26  * Find the dbus object path and service that implements the given interface
27  *
28  * @param[in]   i_interface Interface to search for
29  * @param[out]  o_path      Path of dbus object implementing the interface
30  * @param[out]  o_service   Service implementing the dbus object path
31  * @return      non-zero on error
32  */
33 int find(const std::string& i_interface, std::string& o_path,
34          std::string& o_service);
35 
36 /**
37  * Find the dbus service that implements the given dbus object and interface
38  *
39  * @param[in]   i_interface Interface that maps to the service
40  * @param[in]   i_path      Path that maps to the service
41  * @param[out]  o_service   Service implementing the dbus object and interface
42  * @return      non-zer on error
43  */
44 int findService(const std::string& i_interface, const std::string& i_path,
45                 std::string& o_service);
46 
47 /**
48  * Read a property from a dbus object interface
49  *
50  * @param[in]   i_interface Interface implementing the property
51  * @param[in]   i_path      Path of the dbus object
52  * @param[in]   i_service   Service implementing the dbus object and interface
53  * @param[in]   i_property  Property to read
54  * @return      non-zero on error
55  */
56 int getProperty(const std::string& i_interface, const std::string& i_path,
57                 const std::string& i_service, const std::string& i_property,
58                 DBusValue& o_response);
59 
60 template <typename T>
setProperty(const std::string & service,const std::string & object,const std::string & interface,const std::string & propertyName,const std::variant<T> & propertyValue)61 void setProperty(const std::string& service, const std::string& object,
62                  const std::string& interface, const std::string& propertyName,
63                  const std::variant<T>& propertyValue)
64 {
65     try
66     {
67         auto bus = sdbusplus::bus::new_default();
68         auto method = bus.new_method_call(service.c_str(), object.c_str(),
69                                           "org.freedesktop.DBus.Properties",
70                                           "Set");
71         method.append(interface);
72         method.append(propertyName);
73         method.append(propertyValue);
74 
75         bus.call(method);
76     }
77     catch (const sdbusplus::exception::SdBusError& e)
78     {
79         trace::err("util::dbus::setProperty exception");
80         std::string traceMsg = std::string(e.what());
81         trace::err(traceMsg.c_str());
82     }
83 }
84 
85 /**
86  * Get the IBM compatible names defined for this system
87  *
88  * @return     A vector of strings containing the system names
89  */
90 std::vector<std::string> systemNames();
91 
92 /** @brief Host transition states for host transition operations */
93 enum class HostState
94 {
95     Quiesce,
96     Diagnostic,
97     Crash
98 };
99 
100 /**
101  * @brief Transition the host state
102  *
103  * We will transition the host state by starting the appropriate dbus target.
104  *
105  * @param i_hostState the state to transition the host to
106  */
107 void transitionHost(const HostState i_hostState);
108 
109 /**
110  * @brief Read autoRebootEnabled property
111  *
112  * @return false if autoRebootEnabled policy false, else true
113  */
114 bool autoRebootEnabled();
115 
116 /** @brief Host running states for host running operations */
117 enum class HostRunningState
118 {
119     Unknown,
120     NotStarted,
121     Started,
122     Stopping
123 };
124 
125 /**
126  * Get the host running state
127  *
128  * Use host boot progress to determine if a host has been started. If host
129  * boot progress can not be determined then host state will be unknown.
130  *
131  * @return HostType == "Unknown", "Started or "NotStarted"
132  */
133 HostRunningState hostRunningState();
134 
135 /**
136  * @brief Read dumpPolicyEnabled property
137  *
138  * @return false if dumpPolicyEnabled property is false, else true
139  */
140 bool dumpPolicyEnabled();
141 
142 /**
143  * Create a PEL
144  *
145  * The additional data provided in the map will be placed in a user data
146  * section of the PEL and may additionally contain key words to trigger
147  * certain behaviors by the backend logging code. Each set of data described
148  * in the vector of ffdc data will be placed in additional user data
149  * sections. Note that the PID of the caller will be added to the additional
150  * data map with key "_PID".
151  *
152  * @param  i_message - the event type
153  * @param  i_severity - the severity level
154  * @param  io_additional - map of additional data
155  * @param  i_ffdc - vector of ffdc data
156  * @return Platform log id or 0 if error
157  */
158 uint32_t createPel(const std::string& i_message, const std::string& i_severity,
159                    std::map<std::string, std::string>& io_additional,
160                    const std::vector<FFDCTuple>& i_ffdc);
161 
162 /** @brief Machine ID definitions */
163 enum class MachineType
164 {
165     Rainier_2S4U,
166     Rainier_2S2U,
167     Rainier_1S4U,
168     Rainier_1S2U,
169     Everest,
170     Bonnell,
171 };
172 
173 /**
174  * @brief Read the System IM keyword to get the machine type
175  *
176  * @return An enum representing the machine type
177  */
178 MachineType getMachineType();
179 
180 /** @brief Get list of state sensor PDRs
181  *
182  *  @param[out] pdrList - list of PDRs
183  *  @param[in] stateSetId - ID of the state set of interest
184  *
185  *  @return true if successful otherwise false
186  */
187 bool getStateSensorPdrs(std::vector<std::vector<uint8_t>>& pdrList,
188                         uint16_t stateSetId);
189 
190 /** @brief Get list of state effecter PDRs
191  *
192  *  @param[out] pdrList -  list of PDRs
193  *  @param[in] stateSetId - ID of the state set of interest
194  *
195  *  @return true if successful otherwise false
196  */
197 bool getStateEffecterPdrs(std::vector<std::vector<uint8_t>>& pdrList,
198                           uint16_t stateSetId);
199 
200 /**
201  * @brief Get MCTP instance ID associated with endpoint
202  *
203  * @param[out] mctpInstance - instance of MCTP
204  * @param[in] Eid - MCTP enpoint ID
205  *
206  * @return True on success otherwise False
207  */
208 bool getMctpInstance(uint8_t& mctpInstance, uint8_t Eid);
209 
210 /**
211  * @brief Determine if power fault was detected
212  *
213  * @return true if power fault or unknown, false otherwise
214  */
215 bool powerFault();
216 
217 } // namespace dbus
218 } // namespace util
219