xref: /openbmc/phosphor-psu-code-mgmt/src/version.hpp (revision a31e568e5ee0ddea11f602f840f16ffa90c349ec)
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(std::move(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> getValues(
111         const std::string& filePath, const std::vector<std::string>& keys);
112 
113     /**
114      * @brief Read the manifest file to get the value of the key.
115      *
116      * @param[in] filePath - The path to the file which contains the value
117      *                       of keys.
118      * @param[in] key      - The string of the key.
119      *
120      * @return The string of the value.
121      **/
122     static std::string getValue(const std::string& filePath,
123                                 const std::string& key);
124 
125     /** @brief Get information from extVersion
126      *
127      * @param[in] extVersion - The extended version string that contains
128      *                         key/value pairs separated by comma.
129      *
130      * @return The map of key/value pairs
131      */
132     static std::map<std::string, std::string>
133         getExtVersionInfo(const std::string& extVersion);
134 
135     /** @brief The temUpdater's erase callback. */
136     eraseFunc eraseCallback;
137 
138     /** @brief Get the version string. */
getVersionString() const139     const std::string& getVersionString() const
140     {
141         return versionStr;
142     }
143 
144   private:
145     /** @brief Persistent sdbusplus DBus bus connection */
146     sdbusplus::bus_t& bus;
147 
148     /** @brief Persistent DBus object path */
149     std::string objPath;
150 
151     /** @brief This Version's version Id */
152     const std::string versionId;
153 
154     /** @brief This Version's version string */
155     const std::string versionStr;
156 
157     /** @brief Persistent Delete D-Bus object */
158     std::unique_ptr<Delete> deleteObject;
159 };
160 
161 } // namespace updater
162 } // namespace software
163 } // namespace phosphor
164