xref: /openbmc/openpower-vpd-parser/vpd-manager/include/manager.hpp (revision 8dfa255465de5efa8bcbb8e21623bb9928a65371)
1 #pragma once
2 
3 #include "backup_restore.hpp"
4 #include "constants.hpp"
5 #include "gpio_monitor.hpp"
6 #include "logger.hpp"
7 #include "types.hpp"
8 #include "worker.hpp"
9 
10 #include <oem-handler/ibm_handler.hpp>
11 #include <sdbusplus/asio/object_server.hpp>
12 
13 namespace vpd
14 {
15 /**
16  * @brief Class to manage VPD processing.
17  *
18  * The class is responsible to implement methods to manage VPD on the system.
19  * It also implements methods to be exposed over D-Bus required to access/edit
20  * VPD data.
21  */
22 class Manager
23 {
24   public:
25     /**
26      * List of deleted methods.
27      */
28     Manager(const Manager&) = delete;
29     Manager& operator=(const Manager&) = delete;
30     Manager(Manager&&) = delete;
31 
32     /**
33      * @brief Constructor.
34      *
35      * @param[in] ioCon - IO context.
36      * @param[in] iFace - interface to implement.
37      * @param[in] progressiFace - Interface to track collection progress.
38      * @param[in] connection - Dbus Connection.
39      */
40     Manager(
41         const std::shared_ptr<boost::asio::io_context>& ioCon,
42         const std::shared_ptr<sdbusplus::asio::dbus_interface>& iFace,
43         const std::shared_ptr<sdbusplus::asio::dbus_interface>& progressiFace,
44         const std::shared_ptr<sdbusplus::asio::connection>& asioConnection);
45 
46     /**
47      * @brief Destructor.
48      */
49     ~Manager() = default;
50 
51     /**
52      * @brief Update keyword value.
53      *
54      * This API is used to update keyword value on the given input path and its
55      * redundant path(s) if any taken from system config JSON.
56      *
57      * To update IPZ type VPD, input parameter for writing should be in the form
58      * of (Record, Keyword, Value). Eg: ("VINI", "SN", {0x01, 0x02, 0x03}).
59      *
60      * To update Keyword type VPD, input parameter for writing should be in the
61      * form of (Keyword, Value). Eg: ("PE", {0x01, 0x02, 0x03}).
62      *
63      * @param[in] i_vpdPath - Path (inventory object path/FRU EEPROM path).
64      * @param[in] i_paramsToWriteData - Input details.
65      *
66      * @return On success returns number of bytes written, on failure returns
67      * -1.
68      */
69     int updateKeyword(const types::Path i_vpdPath,
70                       const types::WriteVpdParams i_paramsToWriteData);
71 
72     /**
73      * @brief Update keyword value on hardware.
74      *
75      * This API is used to update keyword value on hardware. Updates only on the
76      * given input hardware path, does not look for corresponding redundant or
77      * primary path against the given path. To update corresponding paths, make
78      * separate call with respective path.
79      *
80      * To update IPZ type VPD, input parameter for writing should be in the form
81      * of (Record, Keyword, Value). Eg: ("VINI", "SN", {0x01, 0x02, 0x03}).
82      *
83      * To update Keyword type VPD, input parameter for writing should be in the
84      * form of (Keyword, Value). Eg: ("PE", {0x01, 0x02, 0x03}).
85      *
86      * @param[in] i_fruPath - EEPROM path of the FRU.
87      * @param[in] i_paramsToWriteData - Input details.
88      *
89      * @return On success returns number of bytes written, on failure returns
90      * -1.
91      */
92     int updateKeywordOnHardware(
93         const types::Path i_fruPath,
94         const types::WriteVpdParams i_paramsToWriteData) noexcept;
95 
96     /**
97      * @brief Read keyword value.
98      *
99      * API can be used to read VPD keyword from the given input path.
100      *
101      * To read keyword of type IPZ, input parameter for reading should be in the
102      * form of (Record, Keyword). Eg: ("VINI", "SN").
103      *
104      * To read keyword from keyword type VPD, just keyword name has to be
105      * supplied in the input parameter. Eg: ("SN").
106      *
107      * @param[in] i_fruPath - EEPROM path.
108      * @param[in] i_paramsToReadData - Input details.
109      *
110      * @throw
111      * sdbusplus::xyz::openbmc_project::Common::Device::Error::ReadFailure.
112      *
113      * @return On success returns the read value in variant of array of bytes.
114      * On failure throws exception.
115      */
116     types::DbusVariantType readKeyword(
117         const types::Path i_fruPath,
118         const types::ReadVpdParams i_paramsToReadData);
119 
120     /**
121      * @brief Collect single FRU VPD
122      * API can be used to perform VPD collection for the given FRU, only if the
123      * current state of the system matches with the state at which the FRU is
124      * allowed for VPD recollection.
125      *
126      * @param[in] i_dbusObjPath - D-bus object path
127      */
128     void collectSingleFruVpd(
129         const sdbusplus::message::object_path& i_dbusObjPath);
130 
131     /**
132      * @brief Delete single FRU VPD
133      * API can be used to perform VPD deletion for the given FRU.
134      *
135      * @param[in] i_dbusObjPath - D-bus object path
136      */
137     void deleteSingleFruVpd(
138         const sdbusplus::message::object_path& i_dbusObjPath);
139 
140     /**
141      * @brief Get expanded location code.
142      *
143      * API to get expanded location code from the unexpanded location code.
144      *
145      * @param[in] i_unexpandedLocationCode - Unexpanded location code.
146      * @param[in] i_nodeNumber - Denotes the node in case of a multi-node
147      * configuration, defaulted to zero incase of single node system.
148      *
149      * @throw xyz.openbmc_project.Common.Error.InvalidArgument for
150      * invalid argument.
151      *
152      * @return Location code of the FRU.
153      */
154     std::string getExpandedLocationCode(
155         const std::string& i_unexpandedLocationCode,
156         [[maybe_unused]] const uint16_t i_nodeNumber = 0);
157 
158     /**
159      * @brief Get D-Bus object path of FRUs from expanded location code.
160      *
161      * An API to get list of FRU D-Bus object paths for a given expanded
162      * location code.
163      *
164      * @param[in] i_expandedLocationCode - Expanded location code.
165      *
166      * @throw xyz.openbmc_project.Common.Error.InvalidArgument for
167      * invalid argument.
168      *
169      * @return List of FRUs D-Bus object paths for the given location code.
170      */
171     types::ListOfPaths getFrusByExpandedLocationCode(
172         const std::string& i_expandedLocationCode);
173 
174     /**
175      * @brief Get D-Bus object path of FRUs from unexpanded location code.
176      *
177      * An API to get list of FRU D-Bus object paths for a given unexpanded
178      * location code.
179      *
180      * @param[in] i_unexpandedLocationCode - Unexpanded location code.
181      * @param[in] i_nodeNumber - Denotes the node in case of a multi-node
182      * configuration, defaulted to zero incase of single node system.
183      *
184      * @throw xyz.openbmc_project.Common.Error.InvalidArgument for
185      * invalid argument.
186      *
187      * @return List of FRUs D-Bus object paths for the given location code.
188      */
189     types::ListOfPaths getFrusByUnexpandedLocationCode(
190         const std::string& i_unexpandedLocationCode,
191         [[maybe_unused]] const uint16_t i_nodeNumber = 0);
192 
193     /**
194      * @brief Get Hardware path
195      * API can be used to get EEPROM path for the given inventory path.
196      *
197      * @param[in] i_dbusObjPath - D-bus object path
198      *
199      * @return Corresponding EEPROM path.
200      */
201     std::string getHwPath(const sdbusplus::message::object_path& i_dbusObjPath);
202 
203     /**
204      * @brief  Perform VPD recollection
205      * This api will trigger parser to perform VPD recollection for FRUs that
206      * can be replaced at standby.
207      */
208     void performVpdRecollection();
209 
210     /**
211      * @brief Get unexpanded location code.
212      *
213      * An API to get unexpanded location code and node number from expanded
214      * location code.
215      *
216      * @param[in] i_expandedLocationCode - Expanded location code.
217      *
218      * @throw xyz.openbmc_project.Common.Error.InvalidArgument for
219      * invalid argument.
220      *
221      * @return Location code in unexpanded format and its node number.
222      */
223     std::tuple<std::string, uint16_t> getUnexpandedLocationCode(
224         const std::string& i_expandedLocationCode);
225 
226     /**
227      * @brief API to collect all FRUs VPD.
228      *
229      * This api will call OEM handler API to perform VPD collection for all FRUs
230      * present in the system config JSON.
231      *
232      * Note:
233      * System VPD collection will always be skipped.
234      * If host is in power on state, FRUs marked as 'powerOffOnly' in the
235      * system config JSON will be skipped.
236      *
237      * @return true on successful request made, false otherwise.
238      */
239     bool collectAllFruVpd() const noexcept;
240 
241     /**
242      * @brief Get the Worker object .
243      *
244      * @return A const reference to the shared pointer holding the Worker
245      * object.
246      */
getWorkerObj() const247     inline const std::shared_ptr<Worker>& getWorkerObj() const
248     {
249         return m_worker;
250     }
251 
252   private:
253     /**
254      * @brief An api to check validity of unexpanded location code.
255      *
256      * @param[in] i_locationCode - Unexpanded location code.
257      *
258      * @return True/False based on validity check.
259      */
260     bool isValidUnexpandedLocationCode(const std::string& i_locationCode);
261 
262     /**
263      * @brief API to read VPD collection mode.
264      *
265      * Collection mode denotes if the VPD needs to be read from file or actual
266      * hardware.
267      */
268     void readVpdCollectionMode() noexcept;
269 
270     // Shared pointer to asio context object.
271     const std::shared_ptr<boost::asio::io_context>& m_ioContext;
272 
273     // Shared pointer to Dbus interface class.
274     const std::shared_ptr<sdbusplus::asio::dbus_interface>& m_interface;
275 
276     // Shared pointer to collection progress interface class.
277     const std::shared_ptr<sdbusplus::asio::dbus_interface>& m_progressInterface;
278 
279     // Shared pointer to bus connection.
280     const std::shared_ptr<sdbusplus::asio::connection>& m_asioConnection;
281 
282     // Shared pointer to worker class
283     std::shared_ptr<Worker> m_worker;
284 
285     // Shared pointer to GpioMonitor class
286     std::shared_ptr<GpioMonitor> m_gpioMonitor;
287 
288     // Variable to hold current collection status
289     std::string m_vpdCollectionStatus{constants::vpdCollectionNotStarted};
290 
291     // Shared pointer to backup and restore class
292     std::shared_ptr<BackupAndRestore> m_backupAndRestoreObj;
293 
294     // Shared pointer to oem specific class.
295     std::shared_ptr<IbmHandler> m_ibmHandler;
296 
297     // VPD collection mode. Default is hardware mode.
298     types::VpdCollectionMode m_vpdCollectionMode =
299         types::VpdCollectionMode::DEFAULT_MODE;
300 
301     // Shared pointer to logger class.
302     std::shared_ptr<Logger> m_logger;
303 };
304 
305 } // namespace vpd
306