1 #pragma once
2 
3 #include "xyz/openbmc_project/Common/OriginatedBy/server.hpp"
4 #include "xyz/openbmc_project/Common/Progress/server.hpp"
5 #include "xyz/openbmc_project/Dump/Entry/server.hpp"
6 #include "xyz/openbmc_project/Object/Delete/server.hpp"
7 #include "xyz/openbmc_project/Time/EpochTime/server.hpp"
8 
9 #include <sdbusplus/bus.hpp>
10 #include <sdbusplus/server/object.hpp>
11 #include <sdeventplus/event.hpp>
12 #include <sdeventplus/source/event.hpp>
13 
14 #include <filesystem>
15 
16 namespace phosphor
17 {
18 namespace dump
19 {
20 
21 template <typename T>
22 using ServerObject = typename sdbusplus::server::object_t<T>;
23 
24 // TODO Revisit whether sdbusplus::xyz::openbmc_project::Time::server::EpochTime
25 // still needed in dump entry since start time and completed time are available
26 // from sdbusplus::xyz::openbmc_project::Common::server::Progress
27 // #ibm-openbmc/2809
28 using EntryIfaces = sdbusplus::server::object_t<
29     sdbusplus::xyz::openbmc_project::Common::server::OriginatedBy,
30     sdbusplus::xyz::openbmc_project::Common::server::Progress,
31     sdbusplus::xyz::openbmc_project::Dump::server::Entry,
32     sdbusplus::xyz::openbmc_project::Object::server::Delete,
33     sdbusplus::xyz::openbmc_project::Time::server::EpochTime>;
34 
35 using OperationStatus =
36     sdbusplus::xyz::openbmc_project::Common::server::Progress::OperationStatus;
37 
38 using originatorTypes = sdbusplus::xyz::openbmc_project::Common::server::
39     OriginatedBy::OriginatorTypes;
40 
41 class Manager;
42 
43 /** @class Entry
44  *  @brief Base Dump Entry implementation.
45  *  @details A concrete implementation for the
46  *  xyz.openbmc_project.Dump.Entry DBus API
47  */
48 class Entry : public EntryIfaces
49 {
50   public:
51     Entry() = delete;
52     Entry(const Entry&) = delete;
53     Entry& operator=(const Entry&) = delete;
54     Entry(Entry&&) = delete;
55     Entry& operator=(Entry&&) = delete;
56     ~Entry() = default;
57 
58     /** @brief Constructor for the Dump Entry Object
59      *  @param[in] bus - Bus to attach to.
60      *  @param[in] objPath - Object path to attach to
61      *  @param[in] dumpId - Dump id.
62      *  @param[in] timeStamp - Dump creation timestamp
63      *             since the epoch.
64      *  @param[in] dumpSize - Dump file size in bytes.
65      *  @param[in] originId - Id of the originator of the dump
66      *  @param[in] originType - Originator type
67      *  @param[in] parent - The dump entry's parent.
68      */
69     Entry(sdbusplus::bus_t& bus, const std::string& objPath, uint32_t dumpId,
70           uint64_t timeStamp, uint64_t dumpSize,
71           const std::filesystem::path& file, OperationStatus dumpStatus,
72           std::string originId, originatorTypes originType, Manager& parent) :
73         EntryIfaces(bus, objPath.c_str(), EntryIfaces::action::emit_no_signals),
74         parent(parent), id(dumpId), file(file)
75     {
76         originatorId(originId);
77         originatorType(originType);
78 
79         size(dumpSize);
80         status(dumpStatus);
81 
82         // If the object is created after the dump creation keep
83         // all same as timeStamp
84         // if the object created before the dump creation, update
85         // only the start time. Completed and elapsed time will
86         // be updated once the dump is completed.
87         if (dumpStatus == OperationStatus::Completed)
88         {
89             elapsed(timeStamp);
90             startTime(timeStamp);
91             completedTime(timeStamp);
92         }
93         else
94         {
95             elapsed(0);
96             startTime(timeStamp);
97             completedTime(0);
98         }
99     };
100 
101     /** @brief Delete this d-bus object.
102      */
103     void delete_() override;
104 
105     /** @brief Method to initiate the offload of dump
106      *  @param[in] uri - URI to offload dump
107      */
108     void initiateOffload(std::string uri) override
109     {
110         offloadUri(uri);
111     }
112 
113     /** @brief Returns the dump id
114      *  @return the id associated with entry
115      */
116     uint32_t getDumpId()
117     {
118         return id;
119     }
120 
121     /** @brief Method to get the file handle of the dump
122      *  @returns A Unix file descriptor to the dump file
123      *  @throws sdbusplus::xyz::openbmc_project::Common::File::Error::Open on
124      *  failure to open the file
125      *  @throws sdbusplus::xyz::openbmc_project::Common::Error::Unavailable if
126      *  the file string is empty
127      */
128     sdbusplus::message::unix_fd getFileHandle() override;
129 
130   protected:
131     /** @brief This entry's parent */
132     Manager& parent;
133 
134     /** @brief This entry's id */
135     uint32_t id;
136 
137     /** @Dump file name */
138     std::filesystem::path file;
139 
140   private:
141     /** @brief Closes the file descriptor and removes the corresponding event
142      *  source.
143      *
144      */
145     void closeFD()
146     {
147         if (fdCloseEventSource)
148         {
149             close(fdCloseEventSource->first);
150             fdCloseEventSource.reset();
151         }
152     }
153 
154     /* @brief A pair of file descriptor and corresponding event source. */
155     std::optional<std::pair<int, std::unique_ptr<sdeventplus::source::Defer>>>
156         fdCloseEventSource;
157 };
158 
159 } // namespace dump
160 } // namespace phosphor
161