xref: /openbmc/phosphor-psu-code-mgmt/src/item_updater.hpp (revision 5670b188673132cfaf7f9c7418174e98e928382e)
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 
115e0dcb39SLei YU #include <phosphor-logging/log.hpp>
1201539e7eSLei YU #include <sdbusplus/server.hpp>
1391029448SLei YU #include <xyz/openbmc_project/Association/Definitions/server.hpp>
1401539e7eSLei YU #include <xyz/openbmc_project/Collection/DeleteAll/server.hpp>
1501539e7eSLei YU 
16*5670b188SPatrick Williams #include <filesystem>
17*5670b188SPatrick Williams 
18f77189f7SLei YU class TestItemUpdater;
19f77189f7SLei YU 
2001539e7eSLei YU namespace phosphor
2101539e7eSLei YU {
2201539e7eSLei YU namespace software
2301539e7eSLei YU {
2401539e7eSLei YU namespace updater
2501539e7eSLei YU {
2601539e7eSLei YU 
2701539e7eSLei YU class Version;
2801539e7eSLei YU 
29374fae56SPatrick Williams using ItemUpdaterInherit = sdbusplus::server::object_t<
30a5c47bb3SLei YU     sdbusplus::xyz::openbmc_project::Association::server::Definitions>;
31a5c47bb3SLei YU 
3201539e7eSLei YU namespace MatchRules = sdbusplus::bus::match::rules;
3301539e7eSLei YU 
3458c26e3fSLei YU namespace fs = std::filesystem;
3558c26e3fSLei YU 
3601539e7eSLei YU /** @class ItemUpdater
3701539e7eSLei YU  *  @brief Manages the activation of the PSU version items.
3801539e7eSLei YU  */
39*5670b188SPatrick Williams class ItemUpdater :
40*5670b188SPatrick Williams     public ItemUpdaterInherit,
41ffb36539SLei YU     public AssociationInterface,
42ffb36539SLei YU     public ActivationListener
4301539e7eSLei YU {
44f77189f7SLei YU     friend class ::TestItemUpdater;
45f77189f7SLei YU 
4601539e7eSLei YU   public:
4701539e7eSLei YU     /** @brief Constructs ItemUpdater
4801539e7eSLei YU      *
4901539e7eSLei YU      * @param[in] bus    - The D-Bus bus object
5001539e7eSLei YU      * @param[in] path   - The D-Bus path
5101539e7eSLei YU      */
52374fae56SPatrick Williams     ItemUpdater(sdbusplus::bus_t& bus, const std::string& path) :
5301539e7eSLei YU         ItemUpdaterInherit(bus, path.c_str()), bus(bus),
5401539e7eSLei YU         versionMatch(bus,
5501539e7eSLei YU                      MatchRules::interfacesAdded() +
5601539e7eSLei YU                          MatchRules::path(SOFTWARE_OBJPATH),
5701539e7eSLei YU                      std::bind(std::mem_fn(&ItemUpdater::createActivation),
5801539e7eSLei YU                                this, std::placeholders::_1))
5901539e7eSLei YU     {
60ad90ad51SLei YU         processPSUImage();
6158c26e3fSLei YU         processStoredImage();
6263f9e712SLei YU         syncToLatestImage();
6301539e7eSLei YU     }
6401539e7eSLei YU 
6501539e7eSLei YU     /** @brief Deletes version
6601539e7eSLei YU      *
6701539e7eSLei YU      *  @param[in] versionId - Id of the version to delete
6801539e7eSLei YU      */
69a5c47bb3SLei YU     void erase(const std::string& versionId);
7001539e7eSLei YU 
7191029448SLei YU     /** @brief Creates an active association to the
7291029448SLei YU      *  newly active software image
7391029448SLei YU      *
7491029448SLei YU      * @param[in]  path - The path to create the association to.
7591029448SLei YU      */
767f2a2152SLei YU     void createActiveAssociation(const std::string& path) override;
7791029448SLei YU 
78ad90ad51SLei YU     /** @brief Add the functional association to the
7991029448SLei YU      *  new "running" PSU images
8091029448SLei YU      *
81ad90ad51SLei YU      * @param[in]  path - The path to add the association to.
8291029448SLei YU      */
837f2a2152SLei YU     void addFunctionalAssociation(const std::string& path) override;
8491029448SLei YU 
85a8b966f1SLei YU     /** @brief Add the updateable association to the
86a8b966f1SLei YU      *  "running" PSU software image
87a8b966f1SLei YU      *
88a8b966f1SLei YU      * @param[in]  path - The path to create the association.
89a8b966f1SLei YU      */
90a8b966f1SLei YU     void addUpdateableAssociation(const std::string& path) override;
91a8b966f1SLei YU 
9291029448SLei YU     /** @brief Removes the associations from the provided software image path
9391029448SLei YU      *
9491029448SLei YU      * @param[in]  path - The path to remove the association from.
9591029448SLei YU      */
967f2a2152SLei YU     void removeAssociation(const std::string& path) override;
9791029448SLei YU 
98ffb36539SLei YU     /** @brief Notify a PSU is updated
99ffb36539SLei YU      *
100ffb36539SLei YU      * @param[in]  versionId - The versionId of the activation
101ffb36539SLei YU      * @param[in]  psuInventoryPath - The PSU inventory path that is updated
102ffb36539SLei YU      */
103ffb36539SLei YU     void onUpdateDone(const std::string& versionId,
104ffb36539SLei YU                       const std::string& psuInventoryPath) override;
105ffb36539SLei YU 
1067f2a2152SLei YU   private:
10701539e7eSLei YU     /** @brief Callback function for Software.Version match.
10801539e7eSLei YU      *  @details Creates an Activation D-Bus object.
10901539e7eSLei YU      *
11001539e7eSLei YU      * @param[in]  msg       - Data associated with subscribed signal
11101539e7eSLei YU      */
112374fae56SPatrick Williams     void createActivation(sdbusplus::message_t& msg);
11301539e7eSLei YU 
114a2c2cd72SLei YU     using Properties =
115a2c2cd72SLei YU         std::map<std::string, utils::UtilsInterface::PropertyType>;
116a2c2cd72SLei YU 
117ad90ad51SLei YU     /** @brief Callback function for PSU inventory match.
118ad90ad51SLei YU      *  @details Update an Activation D-Bus object for PSU inventory.
119ad90ad51SLei YU      *
120ad90ad51SLei YU      * @param[in]  msg       - Data associated with subscribed signal
121ad90ad51SLei YU      */
122374fae56SPatrick Williams     void onPsuInventoryChangedMsg(sdbusplus::message_t& msg);
123a2c2cd72SLei YU 
124a2c2cd72SLei YU     /** @brief Callback function for PSU inventory match.
125a2c2cd72SLei YU      *  @details Update an Activation D-Bus object for PSU inventory.
126a2c2cd72SLei YU      *
127a2c2cd72SLei YU      * @param[in]  psuPath - The PSU inventory path
128a2c2cd72SLei YU      * @param[in]  properties - The updated properties
129a2c2cd72SLei YU      */
130a2c2cd72SLei YU     void onPsuInventoryChanged(const std::string& psuPath,
131a2c2cd72SLei YU                                const Properties& properties);
132ad90ad51SLei YU 
13301539e7eSLei YU     /** @brief Create Activation object */
13401539e7eSLei YU     std::unique_ptr<Activation> createActivationObject(
13501539e7eSLei YU         const std::string& path, const std::string& versionId,
13658c26e3fSLei YU         const std::string& extVersion, Activation::Status activationStatus,
1379930137bSLei YU         const AssociationList& assocs, const std::string& filePath);
13801539e7eSLei YU 
13901539e7eSLei YU     /** @brief Create Version object */
14001539e7eSLei YU     std::unique_ptr<Version>
14101539e7eSLei YU         createVersionObject(const std::string& objPath,
14201539e7eSLei YU                             const std::string& versionId,
14301539e7eSLei YU                             const std::string& versionString,
14401539e7eSLei YU                             sdbusplus::xyz::openbmc_project::Software::server::
1459930137bSLei YU                                 Version::VersionPurpose versionPurpose);
14601539e7eSLei YU 
147bd3b0076SLei YU     /** @brief Create Activation and Version object for PSU inventory
148bd3b0076SLei YU      *  @details If the same version exists for multiple PSUs, just add
149bd3b0076SLei YU      *           related association, instead of creating new objects.
150bd3b0076SLei YU      * */
151ad90ad51SLei YU     void createPsuObject(const std::string& psuInventoryPath,
152ad90ad51SLei YU                          const std::string& psuVersion);
153ad90ad51SLei YU 
154bd3b0076SLei YU     /** @brief Remove Activation and Version object for PSU inventory
155bd3b0076SLei YU      *  @details If the same version exists for mutliple PSUs, just remove
156bd3b0076SLei YU      *           related association.
157bd3b0076SLei YU      *           If the version has no association, the Activation and
158bd3b0076SLei YU      *           Version object will be removed
159bd3b0076SLei YU      */
160bd3b0076SLei YU     void removePsuObject(const std::string& psuInventoryPath);
161bd3b0076SLei YU 
162ad90ad51SLei YU     /**
163ad90ad51SLei YU      * @brief Create and populate the active PSU Version.
164ad90ad51SLei YU      */
165ad90ad51SLei YU     void processPSUImage();
166ad90ad51SLei YU 
16758c26e3fSLei YU     /** @brief Create PSU Version from stored images */
16858c26e3fSLei YU     void processStoredImage();
16958c26e3fSLei YU 
17058c26e3fSLei YU     /** @brief Scan a directory and create PSU Version from stored images */
17158c26e3fSLei YU     void scanDirectory(const fs::path& p);
17258c26e3fSLei YU 
1736520748dSLei YU     /** @brief Get the versionId of the latest PSU version */
1746520748dSLei YU     std::optional<std::string> getLatestVersionId();
1756520748dSLei YU 
17663f9e712SLei YU     /** @brief Update PSUs to the latest version */
17763f9e712SLei YU     void syncToLatestImage();
17863f9e712SLei YU 
17963f9e712SLei YU     /** @brief Invoke the activation via DBus */
18063f9e712SLei YU     void invokeActivation(const std::unique_ptr<Activation>& activation);
18163f9e712SLei YU 
18201539e7eSLei YU     /** @brief Persistent sdbusplus D-Bus bus connection. */
183374fae56SPatrick Williams     sdbusplus::bus_t& bus;
18401539e7eSLei YU 
18501539e7eSLei YU     /** @brief Persistent map of Activation D-Bus objects and their
18601539e7eSLei YU      * version id */
18701539e7eSLei YU     std::map<std::string, std::unique_ptr<Activation>> activations;
18801539e7eSLei YU 
18901539e7eSLei YU     /** @brief Persistent map of Version D-Bus objects and their
19001539e7eSLei YU      * version id */
19101539e7eSLei YU     std::map<std::string, std::unique_ptr<Version>> versions;
19201539e7eSLei YU 
193bd3b0076SLei YU     /** @brief The reference map of PSU Inventory objects and the
194bd3b0076SLei YU      * Activation*/
195bd3b0076SLei YU     std::map<std::string, const std::unique_ptr<Activation>&>
196bd3b0076SLei YU         psuPathActivationMap;
197bd3b0076SLei YU 
198ad90ad51SLei YU     /** @brief sdbusplus signal match for PSU Software*/
19901539e7eSLei YU     sdbusplus::bus::match_t versionMatch;
20091029448SLei YU 
201ad90ad51SLei YU     /** @brief sdbusplus signal matches for PSU Inventory */
202ad90ad51SLei YU     std::vector<sdbusplus::bus::match_t> psuMatches;
203ad90ad51SLei YU 
20491029448SLei YU     /** @brief This entry's associations */
20591029448SLei YU     AssociationList assocs;
2066520748dSLei YU 
2076520748dSLei YU     /** @brief A collection of the version strings */
2086520748dSLei YU     std::set<std::string> versionStrings;
2091517f5f6SLei YU 
2101517f5f6SLei YU     /** @brief A struct to hold the PSU present status and model */
2111517f5f6SLei YU     struct psuStatus
2121517f5f6SLei YU     {
2131517f5f6SLei YU         bool present;
2141517f5f6SLei YU         std::string model;
2151517f5f6SLei YU     };
2161517f5f6SLei YU 
2171517f5f6SLei YU     /** @brief The map of PSU inventory path and the psuStatus
2181517f5f6SLei YU      *
2191517f5f6SLei YU      * It is used to handle psu inventory changed event, that only create psu
2201517f5f6SLei YU      * software object when a PSU is present and the model is retrieved */
2211517f5f6SLei YU     std::map<std::string, psuStatus> psuStatusMap;
22201539e7eSLei YU };
22301539e7eSLei YU 
22401539e7eSLei YU } // namespace updater
22501539e7eSLei YU } // namespace software
22601539e7eSLei YU } // namespace phosphor
227