1 #pragma once
2 
3 #include "xyz/openbmc_project/Common/FilePath/server.hpp"
4 #include "xyz/openbmc_project/Inventory/Decorator/Compatible/server.hpp"
5 #include "xyz/openbmc_project/Object/Delete/server.hpp"
6 #include "xyz/openbmc_project/Software/ExtendedVersion/server.hpp"
7 #include "xyz/openbmc_project/Software/Version/server.hpp"
8 
9 #include <sdbusplus/bus.hpp>
10 
11 #include <functional>
12 #include <string>
13 #include <vector>
14 
15 namespace phosphor
16 {
17 namespace software
18 {
19 namespace manager
20 {
21 
22 typedef std::function<void(std::string)> eraseFunc;
23 
24 using VersionInherit = sdbusplus::server::object_t<
25     sdbusplus::server::xyz::openbmc_project::software::ExtendedVersion,
26     sdbusplus::server::xyz::openbmc_project::software::Version,
27     sdbusplus::server::xyz::openbmc_project::common::FilePath,
28     sdbusplus::server::xyz::openbmc_project::inventory::decorator::Compatible>;
29 using DeleteInherit = sdbusplus::server::object_t<
30     sdbusplus::server::xyz::openbmc_project::object::Delete>;
31 
32 class Version;
33 class Delete;
34 
35 /** @class Delete
36  *  @brief OpenBMC Delete implementation.
37  *  @details A concrete implementation for xyz.openbmc_project.Object.Delete
38  *  D-Bus API.
39  */
40 class Delete : public DeleteInherit
41 {
42   public:
43     /** @brief Constructs Delete.
44      *
45      *  @param[in] bus    - The D-Bus bus object
46      *  @param[in] path   - The D-Bus object path
47      *  @param[in] parent - Parent object.
48      */
49     Delete(sdbusplus::bus_t& bus, const std::string& path, Version& parent) :
50         DeleteInherit(bus, path.c_str(), action::emit_interface_added),
51         parent(parent)
52     {
53         // Empty
54     }
55 
56     /** @brief delete the D-Bus object. */
57     void delete_() override;
58 
59   private:
60     /** @brief Parent Object. */
61     Version& parent;
62 };
63 
64 /** @class Version
65  *  @brief OpenBMC version software management implementation.
66  *  @details A concrete implementation for xyz.openbmc_project.Software.Version
67  *  D-Bus API.
68  */
69 class Version : public VersionInherit
70 {
71   public:
72     /** @brief Constructs Version Software Manager
73      *
74      * @param[in] bus             - The D-Bus bus object
75      * @param[in] objPath         - The D-Bus object path
76      * @param[in] versionString   - The version string
77      * @param[in] versionPurpose  - The version purpose
78      * @param[in] extVersion      - The extended version
79      * @param[in] filePath        - The image filesystem path
80      * @param[in] compatibleNames - The device compatibility names
81      * @param[in] callback        - The eraseFunc callback
82      */
83     Version(sdbusplus::bus_t& bus, const std::string& objPath,
84             const std::string& versionString, VersionPurpose versionPurpose,
85             const std::string& extVersion, const std::string& filePath,
86             const std::vector<std::string>& compatibleNames, eraseFunc callback,
87             const std::string& id) :
88         VersionInherit(bus, (objPath).c_str(),
89                        VersionInherit::action::defer_emit),
90         eraseCallback(std::move(callback)), id(id), objPath(objPath),
91         versionStr(versionString)
92     {
93         // Set properties.
94         extendedVersion(extVersion);
95         purpose(versionPurpose);
96         version(versionString);
97         path(filePath);
98         names(compatibleNames);
99         // Emit deferred signal.
100         emit_object_added();
101     }
102 
103     /**
104      * @brief Read the manifest file to get the value of the key.
105      *
106      * @return The value of the key.
107      **/
108     static std::string getValue(const std::string& manifestFilePath,
109                                 std::string key);
110 
111     /**
112      * @brief Read the manifest file to get the values of the repeated key.
113      *
114      * @return The values of the repeated key.
115      **/
116     static std::vector<std::string>
117         getRepeatedValues(const std::string& manifestFilePath, std::string key);
118 
119     /**
120      * @brief Calculate the version id from the version string.
121      *
122      * @details The version id is a unique 8 hexadecimal digit id
123      *          calculated from the version string.
124      *
125      * @param[in] versionWithSalt - The image's version string
126      *                              (e.g. v1.99.10-19) plus an optional salt
127      *                              string.
128      *
129      * @return The id.
130      */
131     static std::string getId(const std::string& versionWithSalt);
132 
133     /**
134      * @brief Get the active BMC machine name string.
135      *
136      * @param[in] releaseFilePath - The path to the file which contains
137      *                              the release machine string.
138      *
139      * @return The machine name string (e.g. romulus, tiogapass).
140      */
141     static std::string getBMCMachine(const std::string& releaseFilePath);
142 
143     /**
144      * @brief Get the BMC Extended Version string.
145      *
146      * @param[in] releaseFilePath - The path to the file which contains
147      *                              the release machine string.
148      *
149      * @return The extended version string.
150      */
151     static std::string
152         getBMCExtendedVersion(const std::string& releaseFilePath);
153 
154     /**
155      * @brief Get the active BMC version string.
156      *
157      * @param[in] releaseFilePath - The path to the file which contains
158      *                              the release version string.
159      *
160      * @return The version string (e.g. v1.99.10-19).
161      */
162     static std::string getBMCVersion(const std::string& releaseFilePath);
163 
164     /* @brief Check if this version is functional.
165      *
166      * @return - Returns the functional value.
167      */
168     bool isFunctional() const
169     {
170         return functional;
171     }
172 
173     /** @brief Set the functional value.
174      * @param[in] value - True or False
175      */
176     void setFunctional(bool value)
177     {
178         functional = value;
179     }
180 
181     /** @brief Persistent Delete D-Bus object */
182     std::unique_ptr<Delete> deleteObject;
183 
184     /** @brief The parent's erase callback. */
185     eraseFunc eraseCallback;
186 
187     /** @brief The version ID of the object */
188     const std::string id;
189 
190     /** @brief The path of the object */
191     std::string objPath;
192 
193   private:
194     /** @brief This Version's version string */
195     const std::string versionStr;
196 
197     /** @brief If this version is the functional one */
198     bool functional = false;
199 };
200 
201 } // namespace manager
202 } // namespace software
203 } // namespace phosphor
204