xref: /openbmc/phosphor-bmc-code-mgmt/version.hpp (revision bf2bb2b11c4d70811e8745c2f4c411896c744111)
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::xyz::openbmc_project::Software::server::ExtendedVersion,
26     sdbusplus::xyz::openbmc_project::Software::server::Version,
27     sdbusplus::xyz::openbmc_project::Common::server::FilePath,
28     sdbusplus::xyz::openbmc_project::Inventory::Decorator::server::Compatible>;
29 using DeleteInherit = sdbusplus::server::object_t<
30     sdbusplus::xyz::openbmc_project::Object::server::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(callback), id(id), versionStr(versionString)
91     {
92         // Set properties.
93         extendedVersion(extVersion);
94         purpose(versionPurpose);
95         version(versionString);
96         path(filePath);
97         names(compatibleNames);
98         // Emit deferred signal.
99         emit_object_added();
100     }
101 
102     /**
103      * @brief Read the manifest file to get the value of the key.
104      *
105      * @return The value of the key.
106      **/
107     static std::string getValue(const std::string& manifestFilePath,
108                                 std::string key);
109 
110     /**
111      * @brief Read the manifest file to get the values of the repeated key.
112      *
113      * @return The values of the repeated key.
114      **/
115     static std::vector<std::string>
116         getRepeatedValues(const std::string& manifestFilePath, std::string key);
117 
118     /**
119      * @brief Calculate the version id from the version string.
120      *
121      * @details The version id is a unique 8 hexadecimal digit id
122      *          calculated from the version string.
123      *
124      * @param[in] versionWithSalt - The image's version string
125      *                              (e.g. v1.99.10-19) plus an optional salt
126      *                              string.
127      *
128      * @return The id.
129      */
130     static std::string getId(const std::string& versionWithSalt);
131 
132     /**
133      * @brief Get the active BMC machine name string.
134      *
135      * @param[in] releaseFilePath - The path to the file which contains
136      *                              the release machine string.
137      *
138      * @return The machine name string (e.g. romulus, tiogapass).
139      */
140     static std::string getBMCMachine(const std::string& releaseFilePath);
141 
142     /**
143      * @brief Get the BMC Extended Version string.
144      *
145      * @param[in] releaseFilePath - The path to the file which contains
146      *                              the release machine string.
147      *
148      * @return The extended version string.
149      */
150     static std::string
151         getBMCExtendedVersion(const std::string& releaseFilePath);
152 
153     /**
154      * @brief Get the active BMC version string.
155      *
156      * @param[in] releaseFilePath - The path to the file which contains
157      *                              the release version string.
158      *
159      * @return The version string (e.g. v1.99.10-19).
160      */
161     static std::string getBMCVersion(const std::string& releaseFilePath);
162 
163     /* @brief Check if this version is functional.
164      *
165      * @return - Returns the functional value.
166      */
167     bool isFunctional() const
168     {
169         return functional;
170     }
171 
172     /** @brief Set the functional value.
173      * @param[in] value - True or False
174      */
175     void setFunctional(bool value)
176     {
177         functional = value;
178     }
179 
180     /** @brief Persistent Delete D-Bus object */
181     std::unique_ptr<Delete> deleteObject;
182 
183     /** @brief The parent's erase callback. */
184     eraseFunc eraseCallback;
185 
186     /** @brief The version ID of the object */
187     const std::string id;
188 
189   private:
190     /** @brief This Version's version string */
191     const std::string versionStr;
192 
193     /** @brief If this version is the functional one */
194     bool functional = false;
195 };
196 
197 } // namespace manager
198 } // namespace software
199 } // namespace phosphor
200