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), |
Patrick Williams | bab5ed9 | 2024-08-16 15:20:54 -0400 | [diff] [blame] | 54 | versionMatch( |
| 55 | bus, |
| 56 | MatchRules::interfacesAdded() + MatchRules::path(SOFTWARE_OBJPATH), |
| 57 | std::bind(std::mem_fn(&ItemUpdater::createActivation), this, |
| 58 | std::placeholders::_1)), |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 59 | psuInterfaceMatch( |
| 60 | bus, |
| 61 | MatchRules::interfacesAdded() + |
| 62 | MatchRules::path("/xyz/openbmc_project/inventory") + |
| 63 | MatchRules::sender("xyz.openbmc_project.Inventory.Manager"), |
| 64 | std::bind(std::mem_fn(&ItemUpdater::onPSUInterfaceAdded), this, |
| 65 | std::placeholders::_1)) |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 66 | { |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 67 | processPSUImageAndSyncToLatest(); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** @brief Deletes version |
| 71 | * |
| 72 | * @param[in] versionId - Id of the version to delete |
| 73 | */ |
Lei YU | a5c47bb | 2019-09-29 11:28:53 +0800 | [diff] [blame] | 74 | void erase(const std::string& versionId); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 75 | |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 76 | /** @brief Creates an active association to the |
| 77 | * newly active software image |
| 78 | * |
| 79 | * @param[in] path - The path to create the association to. |
| 80 | */ |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 81 | void createActiveAssociation(const std::string& path) override; |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 82 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 83 | /** @brief Add the functional association to the |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 84 | * new "running" PSU images |
| 85 | * |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 86 | * @param[in] path - The path to add the association to. |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 87 | */ |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 88 | void addFunctionalAssociation(const std::string& path) override; |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 89 | |
Lei YU | a8b966f | 2020-03-18 10:32:24 +0800 | [diff] [blame] | 90 | /** @brief Add the updateable association to the |
| 91 | * "running" PSU software image |
| 92 | * |
| 93 | * @param[in] path - The path to create the association. |
| 94 | */ |
| 95 | void addUpdateableAssociation(const std::string& path) override; |
| 96 | |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 97 | /** @brief Removes the associations from the provided software image path |
| 98 | * |
| 99 | * @param[in] path - The path to remove the association from. |
| 100 | */ |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 101 | void removeAssociation(const std::string& path) override; |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 102 | |
Lei YU | ffb3653 | 2019-10-15 13:55:24 +0800 | [diff] [blame] | 103 | /** @brief Notify a PSU is updated |
| 104 | * |
| 105 | * @param[in] versionId - The versionId of the activation |
| 106 | * @param[in] psuInventoryPath - The PSU inventory path that is updated |
| 107 | */ |
| 108 | void onUpdateDone(const std::string& versionId, |
| 109 | const std::string& psuInventoryPath) override; |
| 110 | |
Lei YU | 7f2a215 | 2019-09-16 16:50:18 +0800 | [diff] [blame] | 111 | private: |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 112 | /** @brief Callback function for Software.Version match. |
| 113 | * @details Creates an Activation D-Bus object. |
| 114 | * |
| 115 | * @param[in] msg - Data associated with subscribed signal |
| 116 | */ |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 117 | void createActivation(sdbusplus::message_t& msg); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 118 | |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 119 | using Properties = |
| 120 | std::map<std::string, utils::UtilsInterface::PropertyType>; |
| 121 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 122 | /** @brief Callback function for PSU inventory match. |
| 123 | * @details Update an Activation D-Bus object for PSU inventory. |
| 124 | * |
| 125 | * @param[in] msg - Data associated with subscribed signal |
| 126 | */ |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 127 | void onPsuInventoryChangedMsg(sdbusplus::message_t& msg); |
Lei YU | a2c2cd7 | 2019-08-09 15:54:10 +0800 | [diff] [blame] | 128 | |
| 129 | /** @brief Callback function for PSU inventory match. |
| 130 | * @details Update an Activation D-Bus object for PSU inventory. |
| 131 | * |
| 132 | * @param[in] psuPath - The PSU inventory path |
| 133 | * @param[in] properties - The updated properties |
| 134 | */ |
| 135 | void onPsuInventoryChanged(const std::string& psuPath, |
| 136 | const Properties& properties); |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 137 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 138 | /** @brief Create Activation object */ |
| 139 | std::unique_ptr<Activation> createActivationObject( |
| 140 | const std::string& path, const std::string& versionId, |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 141 | const std::string& extVersion, Activation::Status activationStatus, |
Lei YU | 9930137 | 2019-09-29 16:27:12 +0800 | [diff] [blame] | 142 | const AssociationList& assocs, const std::string& filePath); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 143 | |
| 144 | /** @brief Create Version object */ |
Patrick Williams | bab5ed9 | 2024-08-16 15:20:54 -0400 | [diff] [blame] | 145 | std::unique_ptr<Version> createVersionObject( |
| 146 | const std::string& objPath, const std::string& versionId, |
| 147 | const std::string& versionString, |
| 148 | sdbusplus::xyz::openbmc_project::Software::server::Version:: |
| 149 | VersionPurpose versionPurpose); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 150 | |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 151 | /** @brief Create Activation and Version object for PSU inventory |
| 152 | * @details If the same version exists for multiple PSUs, just add |
| 153 | * related association, instead of creating new objects. |
| 154 | * */ |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 155 | void createPsuObject(const std::string& psuInventoryPath, |
| 156 | const std::string& psuVersion); |
| 157 | |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 158 | /** @brief Remove Activation and Version object for PSU inventory |
Manojkiran Eda | 33cf9f0 | 2024-06-17 14:40:44 +0530 | [diff] [blame] | 159 | * @details If the same version exists for multiple PSUs, just remove |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 160 | * related association. |
| 161 | * If the version has no association, the Activation and |
| 162 | * Version object will be removed |
| 163 | */ |
| 164 | void removePsuObject(const std::string& psuInventoryPath); |
| 165 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 166 | /** |
| 167 | * @brief Create and populate the active PSU Version. |
| 168 | */ |
| 169 | void processPSUImage(); |
| 170 | |
Lei YU | 58c26e3 | 2019-09-27 17:52:06 +0800 | [diff] [blame] | 171 | /** @brief Create PSU Version from stored images */ |
| 172 | void processStoredImage(); |
| 173 | |
| 174 | /** @brief Scan a directory and create PSU Version from stored images */ |
| 175 | void scanDirectory(const fs::path& p); |
| 176 | |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 177 | /** @brief Get the versionId of the latest PSU version */ |
| 178 | std::optional<std::string> getLatestVersionId(); |
| 179 | |
Lei YU | 63f9e71 | 2019-10-12 15:16:55 +0800 | [diff] [blame] | 180 | /** @brief Update PSUs to the latest version */ |
| 181 | void syncToLatestImage(); |
| 182 | |
| 183 | /** @brief Invoke the activation via DBus */ |
George Liu | 80c2daa | 2024-08-23 15:46:12 +0800 | [diff] [blame^] | 184 | static void invokeActivation(const std::unique_ptr<Activation>& activation); |
Lei YU | 63f9e71 | 2019-10-12 15:16:55 +0800 | [diff] [blame] | 185 | |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 186 | /** @brief Callback function for interfaces added signal. |
| 187 | * |
| 188 | * This method is called when a new interface is added. It updates the |
| 189 | * internal status map and process the new PSU if it's present. |
| 190 | * |
| 191 | * @param[in] msg - Data associated with subscribed signal |
| 192 | */ |
| 193 | void onPSUInterfaceAdded(sdbusplus::message_t& msg); |
| 194 | |
| 195 | /** |
| 196 | * @brief Handles the processing of PSU images. |
| 197 | * |
| 198 | * This function responsible for invoking the sequence of processing PSU |
| 199 | * images, processing stored images, and syncing to the latest firmware |
| 200 | * image. |
| 201 | */ |
| 202 | void processPSUImageAndSyncToLatest(); |
| 203 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 204 | /** @brief Persistent sdbusplus D-Bus bus connection. */ |
Patrick Williams | 374fae5 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 205 | sdbusplus::bus_t& bus; |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 206 | |
| 207 | /** @brief Persistent map of Activation D-Bus objects and their |
| 208 | * version id */ |
| 209 | std::map<std::string, std::unique_ptr<Activation>> activations; |
| 210 | |
| 211 | /** @brief Persistent map of Version D-Bus objects and their |
| 212 | * version id */ |
| 213 | std::map<std::string, std::unique_ptr<Version>> versions; |
| 214 | |
Lei YU | bd3b007 | 2019-08-08 13:09:50 +0800 | [diff] [blame] | 215 | /** @brief The reference map of PSU Inventory objects and the |
| 216 | * Activation*/ |
| 217 | std::map<std::string, const std::unique_ptr<Activation>&> |
| 218 | psuPathActivationMap; |
| 219 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 220 | /** @brief sdbusplus signal match for PSU Software*/ |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 221 | sdbusplus::bus::match_t versionMatch; |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 222 | |
Lei YU | ad90ad5 | 2019-08-06 11:19:28 +0800 | [diff] [blame] | 223 | /** @brief sdbusplus signal matches for PSU Inventory */ |
| 224 | std::vector<sdbusplus::bus::match_t> psuMatches; |
| 225 | |
Lei YU | 9102944 | 2019-08-01 15:57:31 +0800 | [diff] [blame] | 226 | /** @brief This entry's associations */ |
| 227 | AssociationList assocs; |
Lei YU | 6520748 | 2019-10-11 16:39:36 +0800 | [diff] [blame] | 228 | |
| 229 | /** @brief A collection of the version strings */ |
| 230 | std::set<std::string> versionStrings; |
Lei YU | 1517f5f | 2019-10-14 16:44:42 +0800 | [diff] [blame] | 231 | |
| 232 | /** @brief A struct to hold the PSU present status and model */ |
| 233 | struct psuStatus |
| 234 | { |
| 235 | bool present; |
| 236 | std::string model; |
| 237 | }; |
| 238 | |
| 239 | /** @brief The map of PSU inventory path and the psuStatus |
| 240 | * |
| 241 | * It is used to handle psu inventory changed event, that only create psu |
| 242 | * software object when a PSU is present and the model is retrieved */ |
| 243 | std::map<std::string, psuStatus> psuStatusMap; |
Faisal Awada | 760053d | 2024-05-16 13:31:32 -0500 | [diff] [blame] | 244 | |
| 245 | /** @brief Signal match for PSU interfaces added. |
| 246 | * |
| 247 | * This match listens for D-Bus signals indicating new interface has been |
| 248 | * added. When such a signal received, it triggers the |
| 249 | * `onInterfacesAdded` method to handle the new PSU. |
| 250 | */ |
| 251 | sdbusplus::bus::match_t psuInterfaceMatch; |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | } // namespace updater |
| 255 | } // namespace software |
| 256 | } // namespace phosphor |