1 #pragma once
2 
3 #include "utility.hpp"
4 
5 #include <nlohmann/json.hpp>
6 #include <xyz/openbmc_project/Logging/Entry/server.hpp>
7 
8 #include <filesystem>
9 #include <string>
10 #include <tuple>
11 
12 namespace phosphor::fan::monitor
13 {
14 
15 /**
16  * @class FFDCFile
17  *
18  * This class holds a file that is used for event log FFDC
19  * which needs a file descriptor as input.  The file is
20  * deleted upon destruction.
21  */
22 class FFDCFile
23 {
24   public:
25     FFDCFile() = delete;
26     FFDCFile(const FFDCFile&) = delete;
27     FFDCFile& operator=(const FFDCFile&) = delete;
28     FFDCFile(FFDCFile&&) = delete;
29     FFDCFile& operator=(FFDCFile&&) = delete;
30 
31     /**
32      * @brief Constructor
33      *
34      * Opens the file and saves the descriptor
35      *
36      * @param[in] name - The filename
37      */
38     explicit FFDCFile(const std::filesystem::path& name);
39 
40     /**
41      * @brief Destructor - Deletes the file
42      */
43     ~FFDCFile()
44     {
45         std::filesystem::remove(_name);
46     }
47 
48     /**
49      * @brief Returns the file descriptor
50      *
51      * @return int - The descriptor
52      */
53     int fd()
54     {
55         return _fd();
56     }
57 
58   private:
59     /**
60      * @brief The file descriptor holder
61      */
62     util::FileDescriptor _fd;
63 
64     /**
65      * @brief The filename
66      */
67     const std::filesystem::path _name;
68 };
69 
70 /**
71  * @class FanError
72  *
73  * This class represents a fan error.  It has a commit() interface
74  * that will create the event log with certain FFDC.
75  */
76 class FanError
77 {
78   public:
79     FanError() = delete;
80     ~FanError() = default;
81     FanError(const FanError&) = delete;
82     FanError& operator=(const FanError&) = delete;
83     FanError(FanError&&) = delete;
84     FanError& operator=(FanError&&) = delete;
85 
86     /**
87      * @brief Constructor
88      *
89      * @param[in] error - The error name, like
90      *                    xyz.openbmc_project.Fan.Error.Fault
91      * @param[in] fan - The failing fan's inventory path
92      * @param[in] sensor - The failing sensor's inventory path.  Can be empty
93      *                     if the error is for the FRU and not the sensor.
94      * @param[in] severity - The severity of the error
95      */
96     FanError(const std::string& error, const std::string& fan,
97              const std::string& sensor,
98              sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level
99                  severity) :
100         _errorName(error),
101         _fanName(fan), _sensorName(sensor),
102         _severity(
103             sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
104                 severity))
105     {}
106 
107     /**
108      * @brief Commits the error by calling the D-Bus method to create
109      *        the event log.
110      *
111      * The FFDC is passed in here so that if an error is committed
112      * more than once it can have up to date FFDC.
113      *
114      * @param[in] jsonFFDC - Free form JSON data that should be sent in as
115      *                       FFDC.
116      */
117     void commit(const nlohmann::json& jsonFFDC);
118 
119   private:
120     /**
121      * @brief Returns an FFDCFile holding the Logger contents
122      *
123      * @return std::unique_ptr<FFDCFile> - The file object
124      */
125     std::unique_ptr<FFDCFile> makeLogFFDCFile();
126 
127     /**
128      * @brief Returns an FFDCFile holding the contents of the JSON FFDC
129      *
130      * @param[in] ffdcData - The JSON data to write to a file
131      *
132      * @return std::unique_ptr<FFDCFile> - The file object
133      */
134     std::unique_ptr<FFDCFile> makeJsonFFDCFile(const nlohmann::json& ffdcData);
135 
136     /**
137      * @brief Create and returns the AdditionalData property to use for the
138      *        event log.
139      *
140      * @return map<string, string> - The AdditionalData contents
141      */
142     std::map<std::string, std::string> getAdditionalData();
143 
144     /**
145      * @brief The error name (The event log's 'Message' property)
146      */
147     const std::string _errorName;
148 
149     /**
150      * @brief The inventory name of the failing fan
151      */
152     const std::string _fanName;
153 
154     /**
155      * @brief The inventory name of the failing sensor, if there is one.
156      */
157     const std::string _sensorName;
158 
159     /**
160      * @brief The severity of the event log.  This is the string
161      *        representation of the Entry::Level property.
162      */
163     const std::string _severity;
164 };
165 
166 } // namespace phosphor::fan::monitor
167