1224882b0SJayanth Othayoth #pragma once
2224882b0SJayanth Othayoth 
374a1f39cSAsmitha Karunanithi #include "xyz/openbmc_project/Common/OriginatedBy/server.hpp"
4a6ab806dSDhruvaraj Subhashchandran #include "xyz/openbmc_project/Common/Progress/server.hpp"
5224882b0SJayanth Othayoth #include "xyz/openbmc_project/Dump/Entry/server.hpp"
6224882b0SJayanth Othayoth #include "xyz/openbmc_project/Object/Delete/server.hpp"
7224882b0SJayanth Othayoth #include "xyz/openbmc_project/Time/EpochTime/server.hpp"
8224882b0SJayanth Othayoth 
9*93f06410SDhruvaraj Subhashchandran #include <phosphor-logging/lg2.hpp>
10cb65ffceSJayanth Othayoth #include <sdbusplus/bus.hpp>
11cb65ffceSJayanth Othayoth #include <sdbusplus/server/object.hpp>
1264f8da9eSDhruvaraj Subhashchandran #include <sdeventplus/event.hpp>
1364f8da9eSDhruvaraj Subhashchandran #include <sdeventplus/source/event.hpp>
14cb65ffceSJayanth Othayoth 
150af74a5eSJayanth Othayoth #include <filesystem>
16*93f06410SDhruvaraj Subhashchandran #include <fstream>
170af74a5eSJayanth Othayoth 
18224882b0SJayanth Othayoth namespace phosphor
19224882b0SJayanth Othayoth {
20224882b0SJayanth Othayoth namespace dump
21224882b0SJayanth Othayoth {
22224882b0SJayanth Othayoth 
23*93f06410SDhruvaraj Subhashchandran // Current serialiation version of the class increment if there any change
24*93f06410SDhruvaraj Subhashchandran // in the serialized data members
25*93f06410SDhruvaraj Subhashchandran constexpr size_t CLASS_SERIALIZATION_VERSION = 1;
26*93f06410SDhruvaraj Subhashchandran 
27*93f06410SDhruvaraj Subhashchandran // Folder to store serialized dump contents
28*93f06410SDhruvaraj Subhashchandran constexpr auto PRESERVE = ".preserve";
29*93f06410SDhruvaraj Subhashchandran 
30*93f06410SDhruvaraj Subhashchandran // Binary file store the contents
31*93f06410SDhruvaraj Subhashchandran constexpr auto SERIAL_FILE = "serialized_entry.json";
32*93f06410SDhruvaraj Subhashchandran 
33224882b0SJayanth Othayoth template <typename T>
349b18bf2dSPatrick Williams using ServerObject = typename sdbusplus::server::object_t<T>;
35224882b0SJayanth Othayoth 
36a6ab806dSDhruvaraj Subhashchandran // TODO Revisit whether sdbusplus::xyz::openbmc_project::Time::server::EpochTime
37a6ab806dSDhruvaraj Subhashchandran // still needed in dump entry since start time and completed time are available
38a6ab806dSDhruvaraj Subhashchandran // from sdbusplus::xyz::openbmc_project::Common::server::Progress
39a6ab806dSDhruvaraj Subhashchandran // #ibm-openbmc/2809
409b18bf2dSPatrick Williams using EntryIfaces = sdbusplus::server::object_t<
4174a1f39cSAsmitha Karunanithi     sdbusplus::xyz::openbmc_project::Common::server::OriginatedBy,
42a6ab806dSDhruvaraj Subhashchandran     sdbusplus::xyz::openbmc_project::Common::server::Progress,
43224882b0SJayanth Othayoth     sdbusplus::xyz::openbmc_project::Dump::server::Entry,
44224882b0SJayanth Othayoth     sdbusplus::xyz::openbmc_project::Object::server::Delete,
45224882b0SJayanth Othayoth     sdbusplus::xyz::openbmc_project::Time::server::EpochTime>;
46224882b0SJayanth Othayoth 
47a6ab806dSDhruvaraj Subhashchandran using OperationStatus =
48a6ab806dSDhruvaraj Subhashchandran     sdbusplus::xyz::openbmc_project::Common::server::Progress::OperationStatus;
49a320c7caSJayanth Othayoth 
5074a1f39cSAsmitha Karunanithi using originatorTypes = sdbusplus::xyz::openbmc_project::Common::server::
5174a1f39cSAsmitha Karunanithi     OriginatedBy::OriginatorTypes;
5274a1f39cSAsmitha Karunanithi 
53a320c7caSJayanth Othayoth class Manager;
54a320c7caSJayanth Othayoth 
55224882b0SJayanth Othayoth /** @class Entry
56f140f665SDhruvaraj Subhashchandran  *  @brief Base Dump Entry implementation.
57224882b0SJayanth Othayoth  *  @details A concrete implementation for the
58224882b0SJayanth Othayoth  *  xyz.openbmc_project.Dump.Entry DBus API
59224882b0SJayanth Othayoth  */
60224882b0SJayanth Othayoth class Entry : public EntryIfaces
61224882b0SJayanth Othayoth {
62224882b0SJayanth Othayoth   public:
63224882b0SJayanth Othayoth     Entry() = delete;
64224882b0SJayanth Othayoth     Entry(const Entry&) = delete;
65224882b0SJayanth Othayoth     Entry& operator=(const Entry&) = delete;
66224882b0SJayanth Othayoth     Entry(Entry&&) = delete;
67224882b0SJayanth Othayoth     Entry& operator=(Entry&&) = delete;
68a320c7caSJayanth Othayoth     ~Entry() = default;
69224882b0SJayanth Othayoth 
70224882b0SJayanth Othayoth     /** @brief Constructor for the Dump Entry Object
71224882b0SJayanth Othayoth      *  @param[in] bus - Bus to attach to.
72a320c7caSJayanth Othayoth      *  @param[in] objPath - Object path to attach to
73a320c7caSJayanth Othayoth      *  @param[in] dumpId - Dump id.
74a320c7caSJayanth Othayoth      *  @param[in] timeStamp - Dump creation timestamp
75a320c7caSJayanth Othayoth      *             since the epoch.
764a98e8feSDhruvaraj Subhashchandran      *  @param[in] dumpSize - Dump file size in bytes.
7774a1f39cSAsmitha Karunanithi      *  @param[in] originId - Id of the originator of the dump
7874a1f39cSAsmitha Karunanithi      *  @param[in] originType - Originator type
79a320c7caSJayanth Othayoth      *  @param[in] parent - The dump entry's parent.
80224882b0SJayanth Othayoth      */
Entry(sdbusplus::bus_t & bus,const std::string & objPath,uint32_t dumpId,uint64_t timeStamp,uint64_t dumpSize,const std::filesystem::path & file,OperationStatus dumpStatus,std::string originId,originatorTypes originType,Manager & parent)819b18bf2dSPatrick Williams     Entry(sdbusplus::bus_t& bus, const std::string& objPath, uint32_t dumpId,
8264f8da9eSDhruvaraj Subhashchandran           uint64_t timeStamp, uint64_t dumpSize,
8364f8da9eSDhruvaraj Subhashchandran           const std::filesystem::path& file, OperationStatus dumpStatus,
8474a1f39cSAsmitha Karunanithi           std::string originId, originatorTypes originType, Manager& parent) :
8573f64076SPatrick Williams         EntryIfaces(bus, objPath.c_str(), EntryIfaces::action::emit_no_signals),
8664f8da9eSDhruvaraj Subhashchandran         parent(parent), id(dumpId), file(file)
87a320c7caSJayanth Othayoth     {
8874a1f39cSAsmitha Karunanithi         originatorId(originId);
8974a1f39cSAsmitha Karunanithi         originatorType(originType);
9074a1f39cSAsmitha Karunanithi 
914a98e8feSDhruvaraj Subhashchandran         size(dumpSize);
92a6ab806dSDhruvaraj Subhashchandran         status(dumpStatus);
93a6ab806dSDhruvaraj Subhashchandran 
94a6ab806dSDhruvaraj Subhashchandran         // If the object is created after the dump creation keep
95a6ab806dSDhruvaraj Subhashchandran         // all same as timeStamp
96a6ab806dSDhruvaraj Subhashchandran         // if the object created before the dump creation, update
97a6ab806dSDhruvaraj Subhashchandran         // only the start time. Completed and elapsed time will
98a6ab806dSDhruvaraj Subhashchandran         // be updated once the dump is completed.
99a6ab806dSDhruvaraj Subhashchandran         if (dumpStatus == OperationStatus::Completed)
100a6ab806dSDhruvaraj Subhashchandran         {
101a320c7caSJayanth Othayoth             elapsed(timeStamp);
102a6ab806dSDhruvaraj Subhashchandran             startTime(timeStamp);
103a6ab806dSDhruvaraj Subhashchandran             completedTime(timeStamp);
104a6ab806dSDhruvaraj Subhashchandran         }
105a6ab806dSDhruvaraj Subhashchandran         else
106a6ab806dSDhruvaraj Subhashchandran         {
107a6ab806dSDhruvaraj Subhashchandran             elapsed(0);
108a6ab806dSDhruvaraj Subhashchandran             startTime(timeStamp);
109a6ab806dSDhruvaraj Subhashchandran             completedTime(0);
110a6ab806dSDhruvaraj Subhashchandran         }
111a320c7caSJayanth Othayoth     };
112224882b0SJayanth Othayoth 
113224882b0SJayanth Othayoth     /** @brief Delete this d-bus object.
114224882b0SJayanth Othayoth      */
115224882b0SJayanth Othayoth     void delete_() override;
116224882b0SJayanth Othayoth 
1174a98e8feSDhruvaraj Subhashchandran     /** @brief Method to initiate the offload of dump
11869e6152fSDhruvaraj Subhashchandran      *  @param[in] uri - URI to offload dump
1194a98e8feSDhruvaraj Subhashchandran      */
initiateOffload(std::string uri)12069e6152fSDhruvaraj Subhashchandran     void initiateOffload(std::string uri) override
1214a98e8feSDhruvaraj Subhashchandran     {
12269e6152fSDhruvaraj Subhashchandran         offloadUri(uri);
1234a98e8feSDhruvaraj Subhashchandran     }
124a320c7caSJayanth Othayoth 
125270355baSDhruvaraj Subhashchandran     /** @brief Returns the dump id
126270355baSDhruvaraj Subhashchandran      *  @return the id associated with entry
127270355baSDhruvaraj Subhashchandran      */
getDumpId()128270355baSDhruvaraj Subhashchandran     uint32_t getDumpId()
129270355baSDhruvaraj Subhashchandran     {
130270355baSDhruvaraj Subhashchandran         return id;
131270355baSDhruvaraj Subhashchandran     }
132270355baSDhruvaraj Subhashchandran 
13364f8da9eSDhruvaraj Subhashchandran     /** @brief Method to get the file handle of the dump
13464f8da9eSDhruvaraj Subhashchandran      *  @returns A Unix file descriptor to the dump file
13564f8da9eSDhruvaraj Subhashchandran      *  @throws sdbusplus::xyz::openbmc_project::Common::File::Error::Open on
13664f8da9eSDhruvaraj Subhashchandran      *  failure to open the file
13764f8da9eSDhruvaraj Subhashchandran      *  @throws sdbusplus::xyz::openbmc_project::Common::Error::Unavailable if
13864f8da9eSDhruvaraj Subhashchandran      *  the file string is empty
13964f8da9eSDhruvaraj Subhashchandran      */
14064f8da9eSDhruvaraj Subhashchandran     sdbusplus::message::unix_fd getFileHandle() override;
14164f8da9eSDhruvaraj Subhashchandran 
142*93f06410SDhruvaraj Subhashchandran     /**
143*93f06410SDhruvaraj Subhashchandran      * @brief Serialize the dump entry attributes to a file.
144*93f06410SDhruvaraj Subhashchandran      *
145*93f06410SDhruvaraj Subhashchandran      */
146*93f06410SDhruvaraj Subhashchandran     virtual void serialize();
147*93f06410SDhruvaraj Subhashchandran 
148*93f06410SDhruvaraj Subhashchandran     /**
149*93f06410SDhruvaraj Subhashchandran      * @brief Deserialize the dump entry attributes from a file.
150*93f06410SDhruvaraj Subhashchandran      *
151*93f06410SDhruvaraj Subhashchandran      * @param[in] dumpPath - The path where the .preserve folder is located.
152*93f06410SDhruvaraj Subhashchandran      */
153*93f06410SDhruvaraj Subhashchandran     virtual void deserialize(const std::filesystem::path& dumpPath);
154*93f06410SDhruvaraj Subhashchandran 
1554a98e8feSDhruvaraj Subhashchandran   protected:
156a320c7caSJayanth Othayoth     /** @brief This entry's parent */
157a320c7caSJayanth Othayoth     Manager& parent;
158a320c7caSJayanth Othayoth 
159a320c7caSJayanth Othayoth     /** @brief This entry's id */
160a320c7caSJayanth Othayoth     uint32_t id;
16164f8da9eSDhruvaraj Subhashchandran 
16264f8da9eSDhruvaraj Subhashchandran     /** @Dump file name */
16364f8da9eSDhruvaraj Subhashchandran     std::filesystem::path file;
16464f8da9eSDhruvaraj Subhashchandran 
16564f8da9eSDhruvaraj Subhashchandran   private:
16664f8da9eSDhruvaraj Subhashchandran     /** @brief Closes the file descriptor and removes the corresponding event
16764f8da9eSDhruvaraj Subhashchandran      *  source.
16864f8da9eSDhruvaraj Subhashchandran      *
16964f8da9eSDhruvaraj Subhashchandran      */
closeFD()17064f8da9eSDhruvaraj Subhashchandran     void closeFD()
17164f8da9eSDhruvaraj Subhashchandran     {
17264f8da9eSDhruvaraj Subhashchandran         if (fdCloseEventSource)
17364f8da9eSDhruvaraj Subhashchandran         {
17464f8da9eSDhruvaraj Subhashchandran             close(fdCloseEventSource->first);
17564f8da9eSDhruvaraj Subhashchandran             fdCloseEventSource.reset();
17664f8da9eSDhruvaraj Subhashchandran         }
17764f8da9eSDhruvaraj Subhashchandran     }
17864f8da9eSDhruvaraj Subhashchandran 
17964f8da9eSDhruvaraj Subhashchandran     /* @brief A pair of file descriptor and corresponding event source. */
18064f8da9eSDhruvaraj Subhashchandran     std::optional<std::pair<int, std::unique_ptr<sdeventplus::source::Defer>>>
18164f8da9eSDhruvaraj Subhashchandran         fdCloseEventSource;
182224882b0SJayanth Othayoth };
183224882b0SJayanth Othayoth 
184224882b0SJayanth Othayoth } // namespace dump
185224882b0SJayanth Othayoth } // namespace phosphor
186