1 #pragma once
2 
3 #include "config.h"
4 
5 #include <sdbusplus/bus.hpp>
6 #include <xyz/openbmc_project/Object/Delete/server.hpp>
7 #include <xyz/openbmc_project/Software/Version/server.hpp>
8 
9 namespace phosphor
10 {
11 namespace software
12 {
13 namespace updater
14 {
15 
16 using eraseFunc = std::function<void(std::string)>;
17 
18 using VersionInherit = sdbusplus::server::object_t<
19     sdbusplus::xyz::openbmc_project::Software::server::Version>;
20 using DeleteInherit = sdbusplus::server::object_t<
21     sdbusplus::xyz::openbmc_project::Object::server::Delete>;
22 
23 namespace sdbusRule = sdbusplus::bus::match::rules;
24 
25 class Version;
26 
27 /** @class Delete
28  *  @brief OpenBMC Delete implementation.
29  *  @details A concrete implementation for xyz.openbmc_project.Object.Delete
30  *  D-Bus API.
31  */
32 class Delete : public DeleteInherit
33 {
34   public:
35     /** @brief Constructs Delete.
36      *
37      *  @param[in] bus    - The D-Bus bus object
38      *  @param[in] path   - The D-Bus object path
39      *  @param[in] version - The Version object.
40      */
Delete(sdbusplus::bus_t & bus,const std::string & path,Version & version)41     Delete(sdbusplus::bus_t& bus, const std::string& path, Version& version) :
42         DeleteInherit(bus, path.c_str(), action::emit_interface_added),
43         version(version)
44     {}
45 
46     /**
47      * @brief Delete the D-Bus object.
48      *        Overrides the default delete function by calling
49      *        Version class erase Method.
50      **/
51     void delete_() override;
52 
53   private:
54     /** @brief The Version Object. */
55     Version& version;
56 };
57 
58 /** @class Version
59  *  @brief OpenBMC version software management implementation.
60  *  @details A concrete implementation for xyz.openbmc_project.Software.Version
61  *  D-Bus API.
62  */
63 class Version : public VersionInherit
64 {
65   public:
66     /** @brief Constructs Version Software Manager.
67      *
68      * @param[in] bus            - The D-Bus bus object
69      * @param[in] objPath        - The D-Bus object path
70      * @param[in] versionId      - The version Id
71      * @param[in] versionString  - The version string
72      * @param[in] versionPurpose - The version purpose
73      * @param[in] callback       - The eraseFunc callback
74      */
Version(sdbusplus::bus_t & bus,const std::string & objPath,const std::string & versionId,const std::string & versionString,VersionPurpose versionPurpose,eraseFunc callback)75     Version(sdbusplus::bus_t& bus, const std::string& objPath,
76             const std::string& versionId, const std::string& versionString,
77             VersionPurpose versionPurpose, eraseFunc callback) :
78         VersionInherit(bus, (objPath).c_str(),
79                        VersionInherit::action::defer_emit),
80         eraseCallback(callback), bus(bus), objPath(objPath),
81         versionId(versionId), versionStr(versionString)
82     {
83         // Set properties.
84         purpose(versionPurpose);
85         version(versionString);
86 
87         deleteObject = std::make_unique<Delete>(bus, objPath, *this);
88 
89         // Emit deferred signal.
90         emit_object_added();
91     }
92 
93     /**
94      * @brief Return the version id
95      */
getVersionId() const96     std::string getVersionId() const
97     {
98         return versionId;
99     }
100 
101     /**
102      * @brief Read the manifest file to get the values of the keys.
103      *
104      * @param[in] filePath - The path to the file which contains the value
105      *                       of keys.
106      * @param[in] keys     - A vector of keys.
107      *
108      * @return The map of keys with filled values.
109      **/
110     static std::map<std::string, std::string>
111         getValues(const std::string& filePath,
112                   const std::vector<std::string>& keys);
113 
114     /**
115      * @brief Read the manifest file to get the value of the key.
116      *
117      * @param[in] filePath - The path to the file which contains the value
118      *                       of keys.
119      * @param[in] key      - The string of the key.
120      *
121      * @return The string of the value.
122      **/
123     static std::string getValue(const std::string& filePath,
124                                 const std::string& key);
125 
126     /** @brief Get information from extVersion
127      *
128      * @param[in] extVersion - The extended version string that contains
129      *                         key/value pairs separated by comma.
130      *
131      * @return The map of key/value pairs
132      */
133     static std::map<std::string, std::string>
134         getExtVersionInfo(const std::string& extVersion);
135 
136     /** @brief The temUpdater's erase callback. */
137     eraseFunc eraseCallback;
138 
139     /** @brief Get the version string. */
getVersionString() const140     const std::string& getVersionString() const
141     {
142         return versionStr;
143     }
144 
145   private:
146     /** @brief Persistent sdbusplus DBus bus connection */
147     sdbusplus::bus_t& bus;
148 
149     /** @brief Persistent DBus object path */
150     std::string objPath;
151 
152     /** @brief This Version's version Id */
153     const std::string versionId;
154 
155     /** @brief This Version's version string */
156     const std::string versionStr;
157 
158     /** @brief Persistent Delete D-Bus object */
159     std::unique_ptr<Delete> deleteObject;
160 };
161 
162 } // namespace updater
163 } // namespace software
164 } // namespace phosphor
165