xref: /openbmc/phosphor-psu-code-mgmt/src/item_updater.hpp (revision 1517f5f6781b1e6b258747d4ccb3eb803919f289)
101539e7eSLei YU #pragma once
201539e7eSLei YU 
301539e7eSLei YU #include "config.h"
401539e7eSLei YU 
501539e7eSLei YU #include "activation.hpp"
67f2a2152SLei YU #include "association_interface.hpp"
791029448SLei YU #include "types.hpp"
85e0dcb39SLei YU #include "utils.hpp"
901539e7eSLei YU #include "version.hpp"
1001539e7eSLei YU 
1158c26e3fSLei YU #include <filesystem>
125e0dcb39SLei YU #include <phosphor-logging/log.hpp>
1301539e7eSLei YU #include <sdbusplus/server.hpp>
1491029448SLei YU #include <xyz/openbmc_project/Association/Definitions/server.hpp>
1501539e7eSLei YU #include <xyz/openbmc_project/Collection/DeleteAll/server.hpp>
1601539e7eSLei YU 
17f77189f7SLei YU class TestItemUpdater;
18f77189f7SLei YU 
1901539e7eSLei YU namespace phosphor
2001539e7eSLei YU {
2101539e7eSLei YU namespace software
2201539e7eSLei YU {
2301539e7eSLei YU namespace updater
2401539e7eSLei YU {
2501539e7eSLei YU 
2601539e7eSLei YU class Version;
2701539e7eSLei YU 
2801539e7eSLei YU using ItemUpdaterInherit = sdbusplus::server::object::object<
29a5c47bb3SLei YU     sdbusplus::xyz::openbmc_project::Association::server::Definitions>;
30a5c47bb3SLei YU 
3101539e7eSLei YU namespace MatchRules = sdbusplus::bus::match::rules;
3201539e7eSLei YU 
3358c26e3fSLei YU namespace fs = std::filesystem;
3458c26e3fSLei YU 
3501539e7eSLei YU /** @class ItemUpdater
3601539e7eSLei YU  *  @brief Manages the activation of the PSU version items.
3701539e7eSLei YU  */
38ffb36539SLei YU class ItemUpdater : public ItemUpdaterInherit,
39ffb36539SLei YU                     public AssociationInterface,
40ffb36539SLei YU                     public ActivationListener
4101539e7eSLei YU {
42f77189f7SLei YU     friend class ::TestItemUpdater;
43f77189f7SLei YU 
4401539e7eSLei YU   public:
4501539e7eSLei YU     /** @brief Constructs ItemUpdater
4601539e7eSLei YU      *
4701539e7eSLei YU      * @param[in] bus    - The D-Bus bus object
4801539e7eSLei YU      * @param[in] path   - The D-Bus path
4901539e7eSLei YU      */
5001539e7eSLei YU     ItemUpdater(sdbusplus::bus::bus& bus, const std::string& path) :
5101539e7eSLei YU         ItemUpdaterInherit(bus, path.c_str()), bus(bus),
5201539e7eSLei YU         versionMatch(bus,
5301539e7eSLei YU                      MatchRules::interfacesAdded() +
5401539e7eSLei YU                          MatchRules::path(SOFTWARE_OBJPATH),
5501539e7eSLei YU                      std::bind(std::mem_fn(&ItemUpdater::createActivation),
5601539e7eSLei YU                                this, std::placeholders::_1))
5701539e7eSLei YU     {
58ad90ad51SLei YU         processPSUImage();
5958c26e3fSLei YU         processStoredImage();
6063f9e712SLei YU         syncToLatestImage();
6101539e7eSLei YU     }
6201539e7eSLei YU 
6301539e7eSLei YU     /** @brief Deletes version
6401539e7eSLei YU      *
6501539e7eSLei YU      *  @param[in] versionId - Id of the version to delete
6601539e7eSLei YU      */
67a5c47bb3SLei YU     void erase(const std::string& versionId);
6801539e7eSLei YU 
6991029448SLei YU     /** @brief Creates an active association to the
7091029448SLei YU      *  newly active software image
7191029448SLei YU      *
7291029448SLei YU      * @param[in]  path - The path to create the association to.
7391029448SLei YU      */
747f2a2152SLei YU     void createActiveAssociation(const std::string& path) override;
7591029448SLei YU 
76ad90ad51SLei YU     /** @brief Add the functional association to the
7791029448SLei YU      *  new "running" PSU images
7891029448SLei YU      *
79ad90ad51SLei YU      * @param[in]  path - The path to add the association to.
8091029448SLei YU      */
817f2a2152SLei YU     void addFunctionalAssociation(const std::string& path) override;
8291029448SLei YU 
8391029448SLei YU     /** @brief Removes the associations from the provided software image path
8491029448SLei YU      *
8591029448SLei YU      * @param[in]  path - The path to remove the association from.
8691029448SLei YU      */
877f2a2152SLei YU     void removeAssociation(const std::string& path) override;
8891029448SLei YU 
89ffb36539SLei YU     /** @brief Notify a PSU is updated
90ffb36539SLei YU      *
91ffb36539SLei YU      * @param[in]  versionId - The versionId of the activation
92ffb36539SLei YU      * @param[in]  psuInventoryPath - The PSU inventory path that is updated
93ffb36539SLei YU      */
94ffb36539SLei YU     void onUpdateDone(const std::string& versionId,
95ffb36539SLei YU                       const std::string& psuInventoryPath) override;
96ffb36539SLei YU 
977f2a2152SLei YU   private:
9801539e7eSLei YU     /** @brief Callback function for Software.Version match.
9901539e7eSLei YU      *  @details Creates an Activation D-Bus object.
10001539e7eSLei YU      *
10101539e7eSLei YU      * @param[in]  msg       - Data associated with subscribed signal
10201539e7eSLei YU      */
10301539e7eSLei YU     void createActivation(sdbusplus::message::message& msg);
10401539e7eSLei YU 
105a2c2cd72SLei YU     using Properties =
106a2c2cd72SLei YU         std::map<std::string, utils::UtilsInterface::PropertyType>;
107a2c2cd72SLei YU 
108ad90ad51SLei YU     /** @brief Callback function for PSU inventory match.
109ad90ad51SLei YU      *  @details Update an Activation D-Bus object for PSU inventory.
110ad90ad51SLei YU      *
111ad90ad51SLei YU      * @param[in]  msg       - Data associated with subscribed signal
112ad90ad51SLei YU      */
113a2c2cd72SLei YU     void onPsuInventoryChangedMsg(sdbusplus::message::message& msg);
114a2c2cd72SLei YU 
115a2c2cd72SLei YU     /** @brief Callback function for PSU inventory match.
116a2c2cd72SLei YU      *  @details Update an Activation D-Bus object for PSU inventory.
117a2c2cd72SLei YU      *
118a2c2cd72SLei YU      * @param[in]  psuPath - The PSU inventory path
119a2c2cd72SLei YU      * @param[in]  properties - The updated properties
120a2c2cd72SLei YU      */
121a2c2cd72SLei YU     void onPsuInventoryChanged(const std::string& psuPath,
122a2c2cd72SLei YU                                const Properties& properties);
123ad90ad51SLei YU 
12401539e7eSLei YU     /** @brief Create Activation object */
12501539e7eSLei YU     std::unique_ptr<Activation> createActivationObject(
12601539e7eSLei YU         const std::string& path, const std::string& versionId,
12758c26e3fSLei YU         const std::string& extVersion, Activation::Status activationStatus,
1289930137bSLei YU         const AssociationList& assocs, const std::string& filePath);
12901539e7eSLei YU 
13001539e7eSLei YU     /** @brief Create Version object */
13101539e7eSLei YU     std::unique_ptr<Version>
13201539e7eSLei YU         createVersionObject(const std::string& objPath,
13301539e7eSLei YU                             const std::string& versionId,
13401539e7eSLei YU                             const std::string& versionString,
13501539e7eSLei YU                             sdbusplus::xyz::openbmc_project::Software::server::
1369930137bSLei YU                                 Version::VersionPurpose versionPurpose);
13701539e7eSLei YU 
138bd3b0076SLei YU     /** @brief Create Activation and Version object for PSU inventory
139bd3b0076SLei YU      *  @details If the same version exists for multiple PSUs, just add
140bd3b0076SLei YU      *           related association, instead of creating new objects.
141bd3b0076SLei YU      * */
142ad90ad51SLei YU     void createPsuObject(const std::string& psuInventoryPath,
143ad90ad51SLei YU                          const std::string& psuVersion);
144ad90ad51SLei YU 
145bd3b0076SLei YU     /** @brief Remove Activation and Version object for PSU inventory
146bd3b0076SLei YU      *  @details If the same version exists for mutliple PSUs, just remove
147bd3b0076SLei YU      *           related association.
148bd3b0076SLei YU      *           If the version has no association, the Activation and
149bd3b0076SLei YU      *           Version object will be removed
150bd3b0076SLei YU      */
151bd3b0076SLei YU     void removePsuObject(const std::string& psuInventoryPath);
152bd3b0076SLei YU 
153ad90ad51SLei YU     /**
154ad90ad51SLei YU      * @brief Create and populate the active PSU Version.
155ad90ad51SLei YU      */
156ad90ad51SLei YU     void processPSUImage();
157ad90ad51SLei YU 
15858c26e3fSLei YU     /** @brief Create PSU Version from stored images */
15958c26e3fSLei YU     void processStoredImage();
16058c26e3fSLei YU 
16158c26e3fSLei YU     /** @brief Scan a directory and create PSU Version from stored images */
16258c26e3fSLei YU     void scanDirectory(const fs::path& p);
16358c26e3fSLei YU 
1646520748dSLei YU     /** @brief Get the versionId of the latest PSU version */
1656520748dSLei YU     std::optional<std::string> getLatestVersionId();
1666520748dSLei YU 
16763f9e712SLei YU     /** @brief Update PSUs to the latest version */
16863f9e712SLei YU     void syncToLatestImage();
16963f9e712SLei YU 
17063f9e712SLei YU     /** @brief Invoke the activation via DBus */
17163f9e712SLei YU     void invokeActivation(const std::unique_ptr<Activation>& activation);
17263f9e712SLei YU 
17301539e7eSLei YU     /** @brief Persistent sdbusplus D-Bus bus connection. */
17401539e7eSLei YU     sdbusplus::bus::bus& bus;
17501539e7eSLei YU 
17601539e7eSLei YU     /** @brief Persistent map of Activation D-Bus objects and their
17701539e7eSLei YU      * version id */
17801539e7eSLei YU     std::map<std::string, std::unique_ptr<Activation>> activations;
17901539e7eSLei YU 
18001539e7eSLei YU     /** @brief Persistent map of Version D-Bus objects and their
18101539e7eSLei YU      * version id */
18201539e7eSLei YU     std::map<std::string, std::unique_ptr<Version>> versions;
18301539e7eSLei YU 
184bd3b0076SLei YU     /** @brief The reference map of PSU Inventory objects and the
185bd3b0076SLei YU      * Activation*/
186bd3b0076SLei YU     std::map<std::string, const std::unique_ptr<Activation>&>
187bd3b0076SLei YU         psuPathActivationMap;
188bd3b0076SLei YU 
189ad90ad51SLei YU     /** @brief sdbusplus signal match for PSU Software*/
19001539e7eSLei YU     sdbusplus::bus::match_t versionMatch;
19191029448SLei YU 
192ad90ad51SLei YU     /** @brief sdbusplus signal matches for PSU Inventory */
193ad90ad51SLei YU     std::vector<sdbusplus::bus::match_t> psuMatches;
194ad90ad51SLei YU 
19591029448SLei YU     /** @brief This entry's associations */
19691029448SLei YU     AssociationList assocs;
1976520748dSLei YU 
1986520748dSLei YU     /** @brief A collection of the version strings */
1996520748dSLei YU     std::set<std::string> versionStrings;
200*1517f5f6SLei YU 
201*1517f5f6SLei YU     /** @brief A struct to hold the PSU present status and model */
202*1517f5f6SLei YU     struct psuStatus
203*1517f5f6SLei YU     {
204*1517f5f6SLei YU         bool present;
205*1517f5f6SLei YU         std::string model;
206*1517f5f6SLei YU     };
207*1517f5f6SLei YU 
208*1517f5f6SLei YU     /** @brief The map of PSU inventory path and the psuStatus
209*1517f5f6SLei YU      *
210*1517f5f6SLei YU      * It is used to handle psu inventory changed event, that only create psu
211*1517f5f6SLei YU      * software object when a PSU is present and the model is retrieved */
212*1517f5f6SLei YU     std::map<std::string, psuStatus> psuStatusMap;
21301539e7eSLei YU };
21401539e7eSLei YU 
21501539e7eSLei YU } // namespace updater
21601539e7eSLei YU } // namespace software
21701539e7eSLei YU } // namespace phosphor
218