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