xref: /openbmc/pldm/fw-update/activation.hpp (revision 14107a10)
1 #pragma once
2 
3 #include "fw-update/update_manager.hpp"
4 
5 #include <sdbusplus/bus.hpp>
6 #include <xyz/openbmc_project/Object/Delete/server.hpp>
7 #include <xyz/openbmc_project/Software/Activation/server.hpp>
8 #include <xyz/openbmc_project/Software/ActivationProgress/server.hpp>
9 
10 #include <string>
11 
12 namespace pldm
13 {
14 
15 namespace fw_update
16 {
17 
18 using ActivationIntf = sdbusplus::server::object_t<
19     sdbusplus::xyz::openbmc_project::Software::server::Activation>;
20 using ActivationProgressIntf = sdbusplus::server::object_t<
21     sdbusplus::xyz::openbmc_project::Software::server::ActivationProgress>;
22 using DeleteIntf = sdbusplus::server::object_t<
23     sdbusplus::xyz::openbmc_project::Object::server::Delete>;
24 
25 /** @class ActivationProgress
26  *
27  *  Concrete implementation of xyz.openbmc_project.Software.ActivationProgress
28  *  D-Bus interface
29  */
30 class ActivationProgress : public ActivationProgressIntf
31 {
32   public:
33     /** @brief Constructor
34      *
35      * @param[in] bus - Bus to attach to
36      * @param[in] objPath - D-Bus object path
37      */
38     ActivationProgress(sdbusplus::bus_t& bus, const std::string& objPath) :
39         ActivationProgressIntf(bus, objPath.c_str(),
40                                action::emit_interface_added)
41     {
42         progress(0);
43     }
44 };
45 
46 /** @class Delete
47  *
48  *  Concrete implementation of xyz.openbmc_project.Object.Delete D-Bus interface
49  */
50 class Delete : public DeleteIntf
51 {
52   public:
53     /** @brief Constructor
54      *
55      *  @param[in] bus - Bus to attach to
56      *  @param[in] objPath - D-Bus object path
57      *  @param[in] updateManager - Reference to FW update manager
58      */
59     Delete(sdbusplus::bus_t& bus, const std::string& objPath,
60            UpdateManager* updateManager) :
61         DeleteIntf(bus, objPath.c_str(), action::emit_interface_added),
62         updateManager(updateManager)
63     {}
64 
65     /** @brief Delete the Activation D-Bus object for the FW update package */
66     void delete_() override
67     {
68         updateManager->clearActivationInfo();
69     }
70 
71   private:
72     UpdateManager* updateManager;
73 };
74 
75 /** @class Activation
76  *
77  *  Concrete implementation of xyz.openbmc_project.Object.Activation D-Bus
78  *  interface
79  */
80 class Activation : public ActivationIntf
81 {
82   public:
83     /** @brief Constructor
84      *
85      *  @param[in] bus - Bus to attach to
86      *  @param[in] objPath - D-Bus object path
87      *  @param[in] updateManager - Reference to FW update manager
88      */
89     Activation(sdbusplus::bus_t& bus, std::string objPath,
90                Activations activationStatus, UpdateManager* updateManager) :
91         ActivationIntf(bus, objPath.c_str(),
92                        ActivationIntf::action::defer_emit),
93         bus(bus), objPath(objPath), updateManager(updateManager)
94     {
95         activation(activationStatus);
96         deleteImpl = std::make_unique<Delete>(bus, objPath, updateManager);
97         emit_object_added();
98     }
99 
100     using sdbusplus::xyz::openbmc_project::Software::server::Activation::
101         activation;
102     using sdbusplus::xyz::openbmc_project::Software::server::Activation::
103         requestedActivation;
104 
105     /** @brief Overriding Activation property setter function
106      */
107     Activations activation(Activations value) override
108     {
109         if (value == Activations::Activating)
110         {
111             deleteImpl.reset();
112             updateManager->activatePackage();
113         }
114         else if (value == Activations::Active || value == Activations::Failed)
115         {
116             if (!deleteImpl)
117             {
118                 deleteImpl = std::make_unique<Delete>(bus, objPath,
119                                                       updateManager);
120             }
121         }
122 
123         return ActivationIntf::activation(value);
124     }
125 
126     /** @brief Overriding RequestedActivations property setter function
127      */
128     RequestedActivations
129         requestedActivation(RequestedActivations value) override
130     {
131         if ((value == RequestedActivations::Active) &&
132             (requestedActivation() != RequestedActivations::Active))
133         {
134             if ((ActivationIntf::activation() == Activations::Ready))
135             {
136                 activation(Activations::Activating);
137             }
138         }
139         return ActivationIntf::requestedActivation(value);
140     }
141 
142   private:
143     sdbusplus::bus_t& bus;
144     const std::string objPath;
145     UpdateManager* updateManager;
146     std::unique_ptr<Delete> deleteImpl;
147 };
148 
149 } // namespace fw_update
150 
151 } // namespace pldm
152