1 #pragma once
2 
3 #include "common/instance_id.hpp"
4 #include "libpldmresponder/pdr_utils.hpp"
5 #include "requester/handler.hpp"
6 
7 #include <libpldm/platform.h>
8 
9 #include <map>
10 
11 namespace pldm
12 {
13 
14 using SensorId = uint16_t;
15 using DbusObjMaps =
16     std::map<SensorId, std::tuple<pldm::responder::pdr_utils::DbusMappings,
17                                   pldm::responder::pdr_utils::DbusValMaps>>;
18 using sensorEvent =
19     std::function<void(SensorId sensorId, const DbusObjMaps& dbusMaps)>;
20 using stateSensorCacheMaps =
21     std::map<pldm::pdr::SensorID, pldm::responder::pdr_utils::EventStates>;
22 
23 namespace state_sensor
24 {
25 /** @class DbusToPLDMEvent
26  *  @brief This class can listen to the state sensor PDRs and send PLDM event
27  *         msg when a D-Bus property changes
28  */
29 class DbusToPLDMEvent
30 {
31   public:
32     DbusToPLDMEvent() = delete;
33     DbusToPLDMEvent(const DbusToPLDMEvent&) = delete;
34     DbusToPLDMEvent(DbusToPLDMEvent&&) = delete;
35     DbusToPLDMEvent& operator=(const DbusToPLDMEvent&) = delete;
36     DbusToPLDMEvent& operator=(DbusToPLDMEvent&&) = delete;
37     ~DbusToPLDMEvent() = default;
38 
39     /** @brief Constructor
40      *  @param[in] mctp_fd - fd of MCTP communications socket
41      *  @param[in] mctp_eid - MCTP EID of host firmware
42      *  @param[in] requester - reference to Requester object
43      *  @param[in] handler - PLDM request handler
44      */
45     explicit DbusToPLDMEvent(
46         int mctp_fd, uint8_t mctp_eid, pldm::InstanceIdDb& instanceIdDb,
47         pldm::requester::Handler<pldm::requester::Request>* handler);
48 
49   public:
50     /** @brief Listen all of the state sensor PDRs
51      *  @param[in] repo - pdr utils repo object
52      *  @param[in] dbusMaps - The map of D-Bus mapping and value
53      */
54     void listenSensorEvent(const pldm::responder::pdr_utils::Repo& repo,
55                            const DbusObjMaps& dbusMaps);
56 
57     /** @brief get the sensor state cache */
getSensorCache()58     inline const stateSensorCacheMaps& getSensorCache()
59     {
60         return sensorCacheMap;
61     }
62 
63     /** @brief function to update the sensor cache
64      *  @param[in] sensorId - sensor Id of the corresponding sensor
65      *  @param[in] sensorRearm - sensor rearm value with in the sensor
66      *  @param[in] previousState - previous state of the sensor
67      */
updateSensorCacheMaps(pldm::pdr::SensorID sensorId,size_t sensorRearm,uint8_t previousState)68     inline void updateSensorCacheMaps(pldm::pdr::SensorID sensorId,
69                                       size_t sensorRearm, uint8_t previousState)
70     {
71         // update the sensor cache
72         sensorCacheMap[sensorId][sensorRearm] = previousState;
73     }
74 
75   private:
76     /** @brief Send state sensor event msg when a D-Bus property changes
77      *  @param[in] sensorId - sensor id
78      */
79     void sendStateSensorEvent(SensorId sensorId, const DbusObjMaps& dbusMaps);
80 
81     /** @brief Send all of sensor event
82      *  @param[in] eventType - PLDM Event types
83      *  @param[in] eventDataVec - std::vector, contains send event data
84      */
85     void sendEventMsg(uint8_t eventType,
86                       const std::vector<uint8_t>& eventDataVec);
87 
88     /** @brief fd of MCTP communications socket */
89     int mctp_fd;
90 
91     /** @brief MCTP EID of host firmware */
92     uint8_t mctp_eid;
93 
94     /** @brief reference to an Instance ID database object, used to obtain PLDM
95      * instance IDs
96      */
97     pldm::InstanceIdDb& instanceIdDb;
98 
99     /** @brief D-Bus property changed signal match */
100     std::vector<std::unique_ptr<sdbusplus::bus::match_t>> stateSensorMatchs;
101 
102     /** @brief PLDM request handler */
103     pldm::requester::Handler<pldm::requester::Request>* handler;
104 
105     /** @brief sensor cache */
106     stateSensorCacheMaps sensorCacheMap;
107 };
108 
109 } // namespace state_sensor
110 } // namespace pldm
111