1 #pragma once
2 
3 #include <sdbusplus/bus.hpp>
4 #include "xyz/openbmc_project/Software/Version/server.hpp"
5 #include "xyz/openbmc_project/Common/FilePath/server.hpp"
6 #include "xyz/openbmc_project/Object/Delete/server.hpp"
7 #include "config.h"
8 
9 namespace openpower
10 {
11 namespace software
12 {
13 namespace updater
14 {
15 
16 class ItemUpdater;
17 
18 typedef std::function<void(std::string)> eraseFunc;
19 
20 using VersionInherit = sdbusplus::server::object::object<
21         sdbusplus::xyz::openbmc_project::Software::server::Version,
22         sdbusplus::xyz::openbmc_project::Common::server::FilePath>;
23 using DeleteInherit = sdbusplus::server::object::object<
24         sdbusplus::xyz::openbmc_project::Object::server::Delete>;
25 
26 namespace sdbusRule = sdbusplus::bus::match::rules;
27 
28 class Delete;
29 class Version;
30 
31 /** @class Delete
32  *  @brief OpenBMC Delete implementation.
33  *  @details A concrete implementation for xyz.openbmc_project.Object.Delete
34  *  D-Bus API.
35 */
36 class Delete : public DeleteInherit
37 {
38     public:
39         /** @brief Constructs Delete.
40          *
41          *  @param[in] bus    - The D-Bus bus object
42          *  @param[in] path   - The D-Bus object path
43          *  @param[in] parent - Parent object.
44          */
45         Delete(sdbusplus::bus::bus& bus,
46                const std::string& path,
47                Version& parent) :
48                 DeleteInherit(bus, path.c_str(), true),
49                 parent(parent),
50                 bus(bus),
51                 path(path)
52         {
53             std::vector<std::string> interfaces({interface});
54             bus.emit_interfaces_added(path.c_str(), interfaces);
55         }
56 
57         ~Delete()
58         {
59             std::vector<std::string> interfaces({interface});
60             bus.emit_interfaces_removed(path.c_str(), interfaces);
61         }
62 
63         /**
64          * @brief Delete the D-Bus object.
65          *        Overrides the default delete function by calling
66          *        Version class erase Method.
67          **/
68         void delete_() override;
69 
70     private:
71 
72         /** @brief Parent Object. */
73         Version& parent;
74 
75         // TODO Remove once openbmc/openbmc#1975 is resolved
76         static constexpr auto interface =
77                 "xyz.openbmc_project.Object.Delete";
78         sdbusplus::bus::bus& bus;
79         std::string path;
80 };
81 
82 /** @class Version
83  *  @brief OpenBMC version software management implementation.
84  *  @details A concrete implementation for xyz.openbmc_project.Software.Version
85  *  D-Bus API.
86  */
87 class Version : public VersionInherit
88 {
89     public:
90         /** @brief Constructs Version Software Manager.
91          *
92          * @param[in] bus            - The D-Bus bus object
93          * @param[in] objPath        - The D-Bus object path
94          * @param[in] parent         - Parent object.
95          * @param[in] versionId      - The version Id
96          * @param[in] versionString  - The version string
97          * @param[in] versionPurpose - The version purpose
98          * @param[in] filePath       - The image filesystem path
99          * @param[in] callback       - The eraseFunc callback
100          */
101         Version(sdbusplus::bus::bus& bus,
102                 const std::string& objPath,
103                 ItemUpdater& parent,
104                 const std::string& versionId,
105                 const std::string& versionString,
106                 VersionPurpose versionPurpose,
107                 const std::string& filePath, eraseFunc callback) :
108                         VersionInherit(bus, (objPath).c_str(), true),
109                         bus(bus),
110                         objPath(objPath),
111                         parent(parent),
112                         versionId(versionId),
113                         versionStr(versionString),
114                 chassisStateSignals(
115                           bus,
116                           sdbusRule::type::signal() +
117                           sdbusRule::member("PropertiesChanged") +
118                           sdbusRule::path(CHASSIS_STATE_PATH) +
119                           sdbusRule::argN(0, CHASSIS_STATE_OBJ) +
120                           sdbusRule::interface(SYSTEMD_PROPERTY_INTERFACE),
121                           std::bind(std::mem_fn(
122                                   &Version::updateDeleteInterface), this,
123                                   std::placeholders::_1))
124         {
125             // Bind erase method
126             eraseCallback = callback;
127             // Set properties.
128             purpose(versionPurpose);
129             version(versionString);
130             path(filePath);
131 
132             // Emit deferred signal.
133             emit_object_added();
134         }
135 
136         /**
137          * @brief Update the Object.Delete interface for this activation
138          *
139          *        Update the delete interface based on whether or not this
140          *        activation is currently functional. A functional activation
141          *        will have no Object.Delete, while a non-functional activation
142          *        will have one.
143          *
144          * @param[in]  msg       - Data associated with subscribed signal
145          */
146         void updateDeleteInterface(sdbusplus::message::message& msg);
147 
148         /**
149          * @brief Read the manifest file to get the value of the key.
150          *
151          * @param[in] filePath - The path to the file which contains the value
152          *                       of keys.
153          * @param[in] keys     - A map of keys with empty values.
154          *
155          * @return The map of keys with filled values.
156          **/
157         static std::map<std::string, std::string> getValue(
158                 const std::string& filePath,
159                 std::map<std::string, std::string> keys);
160 
161         /**
162          * @brief Calculate the version id from the version string.
163          *
164          * @details The version id is a unique 8 hexadecimal digit id
165          *          calculated from the version string.
166          *
167          * @param[in] version - The image version string (e.g. v1.99.10-19).
168          *
169          * @return The id.
170          */
171         static std::string getId(const std::string& version);
172 
173         /** @brief Persistent Delete D-Bus object */
174         std::unique_ptr<Delete> deleteObject;
175 
176         /** @brief The parent's erase callback. */
177         eraseFunc eraseCallback;
178 
179     private:
180         /** @brief Persistent sdbusplus DBus bus connection */
181         sdbusplus::bus::bus& bus;
182 
183         /** @brief Persistent DBus object path */
184         std::string objPath;
185 
186         /** @brief Parent Object. */
187         ItemUpdater& parent;
188 
189         /** @brief This Version's version Id */
190         const std::string versionId;
191 
192         /** @brief This Version's version string */
193         const std::string versionStr;
194 
195         /** @brief Used to subscribe to chassis power state changes **/
196         sdbusplus::bus::match_t chassisStateSignals;
197 
198 };
199 
200 } // namespace updater
201 } // namespace software
202 } // namespace openpower
203