Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "config.h" |
| 4 | |
| 5 | #include "activation.hpp" |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 6 | #include "association_interface.hpp" |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 7 | #include "types.hpp" |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 8 | #include "utils.hpp" |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 9 | #include "version.hpp" |
| 10 | |
Lei YU | 5e0dcb3 | 2019-08-02 18:04:34 +0800 | [diff] [blame] | 11 | #include <phosphor-logging/log.hpp> |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 12 | #include <sdbusplus/server.hpp> |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 13 | #include <xyz/openbmc_project/Association/Definitions/server.hpp> |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 14 | #include <xyz/openbmc_project/Collection/DeleteAll/server.hpp> |
| 15 | |
Patrick Williams | 5670b18 | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 16 | #include <filesystem> |
| 17 | |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 18 | class TestItemUpdater; |
| 19 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 20 | namespace phosphor |
| 21 | { |
| 22 | namespace software |
| 23 | { |
| 24 | namespace updater |
| 25 | { |
| 26 | |
| 27 | class Version; |
| 28 | |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 29 | using ItemUpdaterInherit = sdbusplus::server::object_t< |
Lei YU | a5c47bb | 2019-09-29 11:28:53 +0800 | [diff] [blame] | 30 | sdbusplus::xyz::openbmc_project::Association::server::Definitions>; |
| 31 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 32 | namespace MatchRules = sdbusplus::bus::match::rules; |
| 33 | |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 34 | namespace fs = std::filesystem; |
| 35 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 36 | /** @class ItemUpdater |
| 37 | * @brief Manages the activation of the PSU version items. |
| 38 | */ |
Patrick Williams | 5670b18 | 2023-05-10 07:50:50 -0500 | [diff] [blame] | 39 | class ItemUpdater : |
| 40 | public ItemUpdaterInherit, |
| 41 | public AssociationInterface, |
| 42 | public ActivationListener |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 43 | { |
Lei YU | f77189f | 2019-08-07 14:26:30 +0800 | [diff] [blame] | 44 | friend class ::TestItemUpdater; |
| 45 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 46 | public: |
| 47 | /** @brief Constructs ItemUpdater |
| 48 | * |
| 49 | * @param[in] bus - The D-Bus bus object |
| 50 | * @param[in] path - The D-Bus path |
| 51 | */ |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 52 | ItemUpdater(sdbusplus::bus_t& bus, const std::string& path) : |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 53 | ItemUpdaterInherit(bus, path.c_str()), bus(bus), |
| 54 | versionMatch(bus, |
| 55 | MatchRules::interfacesAdded() + |
| 56 | MatchRules::path(SOFTWARE_OBJPATH), |
| 57 | std::bind(std::mem_fn(&ItemUpdater::createActivation), |
| 58 | this, std::placeholders::_1)) |
| 59 | { |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 60 | processPSUImage(); |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 61 | processStoredImage(); |
Lei YU | 63f9e71 | 2019-10-12 15:16:55 +0800 | [diff] [blame] | 62 | syncToLatestImage(); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** @brief Deletes version |
| 66 | * |
| 67 | * @param[in] versionId - Id of the version to delete |
| 68 | */ |
Lei YU | a5c47bb | 2019-09-29 11:28:53 +0800 | [diff] [blame] | 69 | void erase(const std::string& versionId); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 70 | |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 71 | /** @brief Creates an active association to the |
| 72 | * newly active software image |
| 73 | * |
| 74 | * @param[in] path - The path to create the association to. |
| 75 | */ |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 76 | void createActiveAssociation(const std::string& path) override; |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 77 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 78 | /** @brief Add the functional association to the |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 79 | * new "running" PSU images |
| 80 | * |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 81 | * @param[in] path - The path to add the association to. |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 82 | */ |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 83 | void addFunctionalAssociation(const std::string& path) override; |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 84 | |
Lei YU | a8b966f | 2020-03-18 10:32:24 +0800 | [diff] [blame] | 85 | /** @brief Add the updateable association to the |
| 86 | * "running" PSU software image |
| 87 | * |
| 88 | * @param[in] path - The path to create the association. |
| 89 | */ |
| 90 | void addUpdateableAssociation(const std::string& path) override; |
| 91 | |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 92 | /** @brief Removes the associations from the provided software image path |
| 93 | * |
| 94 | * @param[in] path - The path to remove the association from. |
| 95 | */ |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 96 | void removeAssociation(const std::string& path) override; |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 97 | |
Lei YU | ffb3653 | 2019-10-15 13:55:24 +0800 | [diff] [blame] | 98 | /** @brief Notify a PSU is updated |
| 99 | * |
| 100 | * @param[in] versionId - The versionId of the activation |
| 101 | * @param[in] psuInventoryPath - The PSU inventory path that is updated |
| 102 | */ |
| 103 | void onUpdateDone(const std::string& versionId, |
| 104 | const std::string& psuInventoryPath) override; |
| 105 | |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 106 | private: |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 107 | /** @brief Callback function for Software.Version match. |
| 108 | * @details Creates an Activation D-Bus object. |
| 109 | * |
| 110 | * @param[in] msg - Data associated with subscribed signal |
| 111 | */ |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 112 | void createActivation(sdbusplus::message_t& msg); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 113 | |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 114 | using Properties = |
| 115 | std::map<std::string, utils::UtilsInterface::PropertyType>; |
| 116 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 117 | /** @brief Callback function for PSU inventory match. |
| 118 | * @details Update an Activation D-Bus object for PSU inventory. |
| 119 | * |
| 120 | * @param[in] msg - Data associated with subscribed signal |
| 121 | */ |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 122 | void onPsuInventoryChangedMsg(sdbusplus::message_t& msg); |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 123 | |
| 124 | /** @brief Callback function for PSU inventory match. |
| 125 | * @details Update an Activation D-Bus object for PSU inventory. |
| 126 | * |
| 127 | * @param[in] psuPath - The PSU inventory path |
| 128 | * @param[in] properties - The updated properties |
| 129 | */ |
| 130 | void onPsuInventoryChanged(const std::string& psuPath, |
| 131 | const Properties& properties); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 132 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 133 | /** @brief Create Activation object */ |
| 134 | std::unique_ptr<Activation> createActivationObject( |
| 135 | const std::string& path, const std::string& versionId, |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 136 | const std::string& extVersion, Activation::Status activationStatus, |
Lei YU | 9930137 | 2019-09-29 16:27:12 +0800 | [diff] [blame] | 137 | const AssociationList& assocs, const std::string& filePath); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 138 | |
| 139 | /** @brief Create Version object */ |
| 140 | std::unique_ptr<Version> |
| 141 | createVersionObject(const std::string& objPath, |
| 142 | const std::string& versionId, |
| 143 | const std::string& versionString, |
| 144 | sdbusplus::xyz::openbmc_project::Software::server:: |
Lei YU | 9930137 | 2019-09-29 16:27:12 +0800 | [diff] [blame] | 145 | Version::VersionPurpose versionPurpose); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 146 | |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 147 | /** @brief Create Activation and Version object for PSU inventory |
| 148 | * @details If the same version exists for multiple PSUs, just add |
| 149 | * related association, instead of creating new objects. |
| 150 | * */ |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 151 | void createPsuObject(const std::string& psuInventoryPath, |
| 152 | const std::string& psuVersion); |
| 153 | |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 154 | /** @brief Remove Activation and Version object for PSU inventory |
| 155 | * @details If the same version exists for mutliple PSUs, just remove |
| 156 | * related association. |
| 157 | * If the version has no association, the Activation and |
| 158 | * Version object will be removed |
| 159 | */ |
| 160 | void removePsuObject(const std::string& psuInventoryPath); |
| 161 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 162 | /** |
| 163 | * @brief Create and populate the active PSU Version. |
| 164 | */ |
| 165 | void processPSUImage(); |
| 166 | |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 167 | /** @brief Create PSU Version from stored images */ |
| 168 | void processStoredImage(); |
| 169 | |
| 170 | /** @brief Scan a directory and create PSU Version from stored images */ |
| 171 | void scanDirectory(const fs::path& p); |
| 172 | |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 173 | /** @brief Get the versionId of the latest PSU version */ |
| 174 | std::optional<std::string> getLatestVersionId(); |
| 175 | |
Lei YU | 63f9e71 | 2019-10-12 15:16:55 +0800 | [diff] [blame] | 176 | /** @brief Update PSUs to the latest version */ |
| 177 | void syncToLatestImage(); |
| 178 | |
| 179 | /** @brief Invoke the activation via DBus */ |
| 180 | void invokeActivation(const std::unique_ptr<Activation>& activation); |
| 181 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 182 | /** @brief Persistent sdbusplus D-Bus bus connection. */ |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 183 | sdbusplus::bus_t& bus; |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 184 | |
| 185 | /** @brief Persistent map of Activation D-Bus objects and their |
| 186 | * version id */ |
| 187 | std::map<std::string, std::unique_ptr<Activation>> activations; |
| 188 | |
| 189 | /** @brief Persistent map of Version D-Bus objects and their |
| 190 | * version id */ |
| 191 | std::map<std::string, std::unique_ptr<Version>> versions; |
| 192 | |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 193 | /** @brief The reference map of PSU Inventory objects and the |
| 194 | * Activation*/ |
| 195 | std::map<std::string, const std::unique_ptr<Activation>&> |
| 196 | psuPathActivationMap; |
| 197 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 198 | /** @brief sdbusplus signal match for PSU Software*/ |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 199 | sdbusplus::bus::match_t versionMatch; |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 200 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 201 | /** @brief sdbusplus signal matches for PSU Inventory */ |
| 202 | std::vector<sdbusplus::bus::match_t> psuMatches; |
| 203 | |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 204 | /** @brief This entry's associations */ |
| 205 | AssociationList assocs; |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 206 | |
| 207 | /** @brief A collection of the version strings */ |
| 208 | std::set<std::string> versionStrings; |
Lei YU | 1517f5f | 2019-10-14 16:44:42 +0800 | [diff] [blame] | 209 | |
| 210 | /** @brief A struct to hold the PSU present status and model */ |
| 211 | struct psuStatus |
| 212 | { |
| 213 | bool present; |
| 214 | std::string model; |
| 215 | }; |
| 216 | |
| 217 | /** @brief The map of PSU inventory path and the psuStatus |
| 218 | * |
| 219 | * It is used to handle psu inventory changed event, that only create psu |
| 220 | * software object when a PSU is present and the model is retrieved */ |
| 221 | std::map<std::string, psuStatus> psuStatusMap; |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 222 | }; |
| 223 | |
| 224 | } // namespace updater |
| 225 | } // namespace software |
| 226 | } // namespace phosphor |