1 #pragma once
2 
3 #include "status.hpp"
4 
5 #include <memory>
6 #include <sdbusplus/bus.hpp>
7 #include <sdbusplus/bus/match.hpp>
8 #include <string>
9 
10 namespace ipmi_flash
11 {
12 
13 class SystemdNoFile : public TriggerableActionInterface
14 {
15   public:
16     static std::unique_ptr<TriggerableActionInterface>
17         CreateSystemdNoFile(sdbusplus::bus::bus&& bus,
18                             const std::string& service,
19                             const std::string& mode);
20 
21     SystemdNoFile(sdbusplus::bus::bus&& bus, const std::string& service,
22                   const std::string& mode) :
23         bus(std::move(bus)),
24         triggerService(service), mode(mode)
25     {
26     }
27 
28     SystemdNoFile(const SystemdNoFile&) = delete;
29     SystemdNoFile& operator=(const SystemdNoFile&) = delete;
30     // sdbusplus match requires us to be pinned
31     SystemdNoFile(SystemdNoFile&&) = delete;
32     SystemdNoFile& operator=(SystemdNoFile&&) = delete;
33 
34     bool trigger() override;
35     void abort() override;
36     ActionStatus status() override;
37 
38     const std::string& getMode() const;
39 
40   private:
41     sdbusplus::bus::bus bus;
42     const std::string triggerService;
43     const std::string mode;
44 
45     std::optional<sdbusplus::bus::match::match> jobMonitor;
46     std::optional<std::string> job;
47     ActionStatus currentStatus = ActionStatus::unknown;
48 
49     void match(sdbusplus::message::message& m);
50 };
51 
52 /**
53  * Representation of what is used for triggering an action with systemd and
54  * checking the result by reading a file.
55  */
56 class SystemdWithStatusFile : public SystemdNoFile
57 {
58   public:
59     /**
60      * Create a default SystemdWithStatusFile object that uses systemd to
61      * trigger the process.
62      *
63      * @param[in] bus - an sdbusplus handler for a bus to use.
64      * @param[in] path - the path to check for verification status.
65      * @param[in] service - the systemd service to start to trigger
66      * verification.
67      * @param[in] mode - the job-mode when starting the systemd Unit.
68      */
69     static std::unique_ptr<TriggerableActionInterface>
70         CreateSystemdWithStatusFile(sdbusplus::bus::bus&& bus,
71                                     const std::string& path,
72                                     const std::string& service,
73                                     const std::string& mode);
74 
75     SystemdWithStatusFile(sdbusplus::bus::bus&& bus, const std::string& path,
76                           const std::string& service, const std::string& mode) :
77         SystemdNoFile(std::move(bus), service, mode),
78         checkPath(path)
79     {
80     }
81 
82     bool trigger() override;
83     ActionStatus status() override;
84 
85   private:
86     const std::string checkPath;
87 };
88 
89 } // namespace ipmi_flash
90