1 #pragma once
2 
3 #include <systemd/sd-event.h>
4 #include <unistd.h>
5 
6 #include <sdbusplus/bus.hpp>
7 #include <xyz/openbmc_project/State/Boot/Progress/server.hpp>
8 
9 #include <memory>
10 
11 namespace phosphor
12 {
13 namespace dump
14 {
15 
16 using BootProgress = sdbusplus::xyz::openbmc_project::State::Boot::server::
17     Progress::ProgressStages;
18 
19 /* Need a custom deleter for freeing up sd_event */
20 struct EventDeleter
21 {
22     void operator()(sd_event* event) const
23     {
24         event = sd_event_unref(event);
25     }
26 };
27 using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
28 
29 /** @struct CustomFd
30  *
31  *  RAII wrapper for file descriptor.
32  */
33 struct CustomFd
34 {
35   private:
36     /** @brief File descriptor */
37     int fd = -1;
38 
39   public:
40     CustomFd() = delete;
41     CustomFd(const CustomFd&) = delete;
42     CustomFd& operator=(const CustomFd&) = delete;
43     CustomFd(CustomFd&&) = delete;
44     CustomFd& operator=(CustomFd&&) = delete;
45 
46     /** @brief Saves File descriptor and uses it to do file operation
47      *
48      *  @param[in] fd - File descriptor
49      */
50     CustomFd(int fd) : fd(fd)
51     {}
52 
53     ~CustomFd()
54     {
55         if (fd >= 0)
56         {
57             close(fd);
58         }
59     }
60 
61     int operator()() const
62     {
63         return fd;
64     }
65 };
66 
67 /**
68  * @brief Get the bus service
69  *
70  * @param[in] bus - Bus to attach to.
71  * @param[in] path - D-Bus path name.
72  * @param[in] interface - D-Bus interface name.
73  * @return the bus service as a string
74  **/
75 std::string getService(sdbusplus::bus_t& bus, const std::string& path,
76                        const std::string& interface);
77 
78 /**
79  * @brief Get the host boot progress stage
80  *
81  * @return BootProgress on success
82  *         Throw exception on failure
83  *
84  */
85 BootProgress getBootProgress();
86 
87 /**
88  * @brief Check whether host is running
89  *
90  * @return true if the host running else false.
91  *         Throw exception on failure.
92  */
93 bool isHostRunning();
94 
95 } // namespace dump
96 } // namespace phosphor
97