1 #pragma once
2 
3 #include "libpldm/platform.h"
4 #include "libpldm/pldm.h"
5 
6 #include "common/types.hpp"
7 #include "numeric_sensor.hpp"
8 #include "pldmd/dbus_impl_requester.hpp"
9 #include "requester/handler.hpp"
10 #include "terminus.hpp"
11 #include "terminus_manager.hpp"
12 
13 namespace pldm
14 {
15 namespace platform_mc
16 {
17 
18 /**
19  * @brief EventManager
20  *
21  * This class manages PLDM events from terminus. The function includes providing
22  * the API for process event data and using phosphor-logging API to log the
23  * event.
24  *
25  */
26 class EventManager
27 {
28   public:
29     EventManager() = delete;
30     EventManager(const EventManager&) = delete;
31     EventManager(EventManager&&) = delete;
32     EventManager& operator=(const EventManager&) = delete;
33     EventManager& operator=(EventManager&&) = delete;
34     virtual ~EventManager() = default;
35 
36     explicit EventManager(TerminusManager& terminusManager,
37                           TerminiMapper& termini) :
38         terminusManager(terminusManager), termini(termini) {};
39 
40     /** @brief Handle platform event
41      *
42      *  @param[in] tid - tid where the event is from
43      *  @param[in] eventId - event Id
44      *  @param[in] eventClass - event class
45      *  @param[in] eventData - event data
46      *  @param[in] eventDataSize - size of event data
47      *  @return PLDM completion code
48      */
49     int handlePlatformEvent(pldm_tid_t tid, uint16_t eventId,
50                             uint8_t eventClass, const uint8_t* eventData,
51                             size_t eventDataSize);
52 
53     /** @brief Set available state of terminus for pldm request.
54      *
55      *  @param[in] tid - terminus ID
56      *  @param[in] state - Terminus available state for PLDM request messages
57      */
58     void updateAvailableState(pldm_tid_t tid, Availability state)
59     {
60         availableState[tid] = state;
61     };
62 
63     /** @brief Get available state of terminus for pldm request.
64      *
65      *  @param[in] tid - terminus ID
66      */
67     bool getAvailableState(pldm_tid_t tid)
68     {
69         if (!availableState.contains(tid))
70         {
71             return false;
72         }
73         return availableState[tid];
74     };
75 
76   protected:
77     /** @brief Helper method to process the PLDM Numeric sensor event class
78      *
79      *  @param[in] tid - tid where the event is from
80      *  @param[in] sensorId - Sensor ID which is the source of event
81      *  @param[in] sensorData - Numeric sensor event data
82      *  @param[in] sensorDataLength - event data length
83      *
84      *  @return PLDM completion code
85      */
86     int processNumericSensorEvent(pldm_tid_t tid, uint16_t sensorId,
87                                   const uint8_t* sensorData,
88                                   size_t sensorDataLength);
89 
90     /** @brief Helper method to process the PLDM CPER event class
91      *
92      *  @param[in] tid - tid where the event is from
93      *  @param[in] eventId - Event ID which is the source of event
94      *  @param[in] eventData - CPER event data
95      *  @param[in] eventDataSize - event data length
96      *
97      *  @return PLDM completion code
98      */
99     int processCperEvent(pldm_tid_t tid, uint16_t eventId,
100                          const uint8_t* eventData, const size_t eventDataSize);
101 
102     /** @brief Helper method to create CPER dump log
103      *
104      *  @param[in] dataType - CPER event data type
105      *  @param[in] dataPath - CPER event data fault log file path
106      *  @param[in] typeName - Terminus name which creates CPER event
107      *
108      *  @return PLDM completion code
109      */
110     int createCperDumpEntry(const std::string& dataType,
111                             const std::string& dataPath,
112                             const std::string& typeName);
113 
114     /** @brief Reference of terminusManager */
115     TerminusManager& terminusManager;
116 
117     /** @brief List of discovered termini */
118     TerminiMapper& termini;
119 
120     /** @brief Available state for pldm request of terminus */
121     std::unordered_map<pldm_tid_t, Availability> availableState;
122 };
123 } // namespace platform_mc
124 } // namespace pldm
125