blob: 273a03aba4d1fd0418355e61c79cdd75d1a06893 [file] [log] [blame]
George Liucae18662020-05-15 09:32:57 +08001#pragma once
2
Andrew Jeffery2abbce72023-10-18 10:17:35 +10303#include "common/instance_id.hpp"
George Liucae18662020-05-15 09:32:57 +08004#include "libpldmresponder/pdr_utils.hpp"
Sampa Misrac0c79482021-06-02 08:01:54 -05005#include "requester/handler.hpp"
George Liucae18662020-05-15 09:32:57 +08006
George Liuc453e162022-12-21 17:16:23 +08007#include <libpldm/platform.h>
8
George Liucae18662020-05-15 09:32:57 +08009#include <map>
10
George Liucae18662020-05-15 09:32:57 +080011namespace pldm
12{
13
14using SensorId = uint16_t;
15using DbusObjMaps =
Brad Bishop5079ac42021-08-19 18:35:06 -040016 std::map<SensorId, std::tuple<pldm::responder::pdr_utils::DbusMappings,
17 pldm::responder::pdr_utils::DbusValMaps>>;
George Liucae18662020-05-15 09:32:57 +080018using sensorEvent =
19 std::function<void(SensorId sensorId, const DbusObjMaps& dbusMaps)>;
Manojkiran Edaae933cc2024-02-21 17:19:21 +053020using stateSensorCacheMaps =
21 std::map<pldm::pdr::SensorID, pldm::responder::pdr_utils::EventStates>;
George Liucae18662020-05-15 09:32:57 +080022
23namespace 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 */
29class 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
Sampa Misrac0c79482021-06-02 08:01:54 -050043 * @param[in] handler - PLDM request handler
George Liucae18662020-05-15 09:32:57 +080044 */
Sampa Misrac0c79482021-06-02 08:01:54 -050045 explicit DbusToPLDMEvent(
Andrew Jefferya330b2f2023-05-04 14:55:37 +093046 int mctp_fd, uint8_t mctp_eid, pldm::InstanceIdDb& instanceIdDb,
Sampa Misrac0c79482021-06-02 08:01:54 -050047 pldm::requester::Handler<pldm::requester::Request>* handler);
George Liucae18662020-05-15 09:32:57 +080048
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 */
Brad Bishop5079ac42021-08-19 18:35:06 -040054 void listenSensorEvent(const pldm::responder::pdr_utils::Repo& repo,
George Liucae18662020-05-15 09:32:57 +080055 const DbusObjMaps& dbusMaps);
56
Manojkiran Edaae933cc2024-02-21 17:19:21 +053057 /** @brief get the sensor state cache */
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 */
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
George Liucae18662020-05-15 09:32:57 +080075 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
Andrew Jefferya330b2f2023-05-04 14:55:37 +093094 /** @brief reference to an Instance ID database object, used to obtain PLDM
95 * instance IDs
George Liucae18662020-05-15 09:32:57 +080096 */
Andrew Jefferya330b2f2023-05-04 14:55:37 +093097 pldm::InstanceIdDb& instanceIdDb;
George Liucae18662020-05-15 09:32:57 +080098
99 /** @brief D-Bus property changed signal match */
Patrick Williams84b790c2022-07-22 19:26:56 -0500100 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> stateSensorMatchs;
Sampa Misrac0c79482021-06-02 08:01:54 -0500101
102 /** @brief PLDM request handler */
103 pldm::requester::Handler<pldm::requester::Request>* handler;
Manojkiran Edaae933cc2024-02-21 17:19:21 +0530104
105 /** @brief sensor cache */
106 stateSensorCacheMaps sensorCacheMap;
George Liucae18662020-05-15 09:32:57 +0800107};
108
109} // namespace state_sensor
110} // namespace pldm