George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Andrew Jeffery | 2abbce7 | 2023-10-18 10:17:35 +1030 | [diff] [blame] | 3 | #include "common/instance_id.hpp" |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 4 | #include "libpldmresponder/pdr_utils.hpp" |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 5 | #include "requester/handler.hpp" |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 6 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 7 | #include <libpldm/platform.h> |
| 8 | |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 9 | #include <map> |
| 10 | |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 11 | namespace pldm |
| 12 | { |
| 13 | |
| 14 | using SensorId = uint16_t; |
| 15 | using DbusObjMaps = |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 16 | std::map<SensorId, std::tuple<pldm::responder::pdr_utils::DbusMappings, |
| 17 | pldm::responder::pdr_utils::DbusValMaps>>; |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 18 | using sensorEvent = |
| 19 | std::function<void(SensorId sensorId, const DbusObjMaps& dbusMaps)>; |
Manojkiran Eda | ae933cc | 2024-02-21 17:19:21 +0530 | [diff] [blame] | 20 | using stateSensorCacheMaps = |
| 21 | std::map<pldm::pdr::SensorID, pldm::responder::pdr_utils::EventStates>; |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 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 |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 43 | * @param[in] handler - PLDM request handler |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 44 | */ |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 45 | explicit DbusToPLDMEvent( |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 46 | int mctp_fd, uint8_t mctp_eid, pldm::InstanceIdDb& instanceIdDb, |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 47 | pldm::requester::Handler<pldm::requester::Request>* handler); |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 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 | */ |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 54 | void listenSensorEvent(const pldm::responder::pdr_utils::Repo& repo, |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 55 | const DbusObjMaps& dbusMaps); |
| 56 | |
Manojkiran Eda | ae933cc | 2024-02-21 17:19:21 +0530 | [diff] [blame] | 57 | /** @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 Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 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 | |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 88 | /** @brief MCTP EID of host firmware */ |
| 89 | uint8_t mctp_eid; |
| 90 | |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 91 | /** @brief reference to an Instance ID database object, used to obtain PLDM |
| 92 | * instance IDs |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 93 | */ |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 94 | pldm::InstanceIdDb& instanceIdDb; |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 95 | |
| 96 | /** @brief D-Bus property changed signal match */ |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 97 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> stateSensorMatchs; |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 98 | |
| 99 | /** @brief PLDM request handler */ |
| 100 | pldm::requester::Handler<pldm::requester::Request>* handler; |
Manojkiran Eda | ae933cc | 2024-02-21 17:19:21 +0530 | [diff] [blame] | 101 | |
| 102 | /** @brief sensor cache */ |
| 103 | stateSensorCacheMaps sensorCacheMap; |
George Liu | cae1866 | 2020-05-15 09:32:57 +0800 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | } // namespace state_sensor |
| 107 | } // namespace pldm |