1ac19bd68SDeepak Kodihalli #pragma once
2ac19bd68SDeepak Kodihalli 
3d130e1a3SDeepak Kodihalli #include "common/types.hpp"
4d130e1a3SDeepak Kodihalli #include "common/utils.hpp"
53aec997cSPavithra Barithaya #include "libpldmresponder/event_parser.hpp"
6ac19bd68SDeepak Kodihalli #include "libpldmresponder/pdr_utils.hpp"
71521f6d1SDeepak Kodihalli #include "pldmd/dbus_impl_requester.hpp"
874f27c73STom Joseph #include "requester/handler.hpp"
9ac19bd68SDeepak Kodihalli 
10*c453e164SGeorge Liu #include <libpldm/base.h>
11*c453e164SGeorge Liu #include <libpldm/platform.h>
12*c453e164SGeorge Liu 
13ac19bd68SDeepak Kodihalli #include <sdeventplus/event.hpp>
14ac19bd68SDeepak Kodihalli #include <sdeventplus/source/event.hpp>
15ac19bd68SDeepak Kodihalli 
167246e0cdSDeepak Kodihalli #include <deque>
17ac19bd68SDeepak Kodihalli #include <map>
18ac19bd68SDeepak Kodihalli #include <memory>
19ac19bd68SDeepak Kodihalli #include <vector>
20ac19bd68SDeepak Kodihalli 
21ac19bd68SDeepak Kodihalli namespace pldm
22ac19bd68SDeepak Kodihalli {
23ac19bd68SDeepak Kodihalli using EntityType = uint16_t;
24ac19bd68SDeepak Kodihalli // vector which would hold the PDR record handle data returned by
25ac19bd68SDeepak Kodihalli // pldmPDRRepositoryChgEvent event data
26ac19bd68SDeepak Kodihalli using ChangeEntry = uint32_t;
277246e0cdSDeepak Kodihalli using PDRRecordHandles = std::deque<ChangeEntry>;
28ac19bd68SDeepak Kodihalli 
29ac19bd68SDeepak Kodihalli /** @struct SensorEntry
30ac19bd68SDeepak Kodihalli  *
31ac19bd68SDeepak Kodihalli  *  SensorEntry is a unique key which maps a sensorEventType request in the
32ac19bd68SDeepak Kodihalli  *  PlatformEventMessage command to a host sensor PDR. This struct is a key
33ac19bd68SDeepak Kodihalli  *  in a std::map, so implemented operator==and operator<.
34ac19bd68SDeepak Kodihalli  */
35ac19bd68SDeepak Kodihalli struct SensorEntry
36ac19bd68SDeepak Kodihalli {
37ac19bd68SDeepak Kodihalli     pdr::TerminusID terminusID;
38ac19bd68SDeepak Kodihalli     pdr::SensorID sensorID;
39ac19bd68SDeepak Kodihalli 
40ac19bd68SDeepak Kodihalli     bool operator==(const SensorEntry& e) const
41ac19bd68SDeepak Kodihalli     {
42ac19bd68SDeepak Kodihalli         return ((terminusID == e.terminusID) && (sensorID == e.sensorID));
43ac19bd68SDeepak Kodihalli     }
44ac19bd68SDeepak Kodihalli 
45ac19bd68SDeepak Kodihalli     bool operator<(const SensorEntry& e) const
46ac19bd68SDeepak Kodihalli     {
47ac19bd68SDeepak Kodihalli         return ((terminusID < e.terminusID) ||
48ac19bd68SDeepak Kodihalli                 ((terminusID == e.terminusID) && (sensorID < e.sensorID)));
49ac19bd68SDeepak Kodihalli     }
50ac19bd68SDeepak Kodihalli };
51ac19bd68SDeepak Kodihalli 
52ac19bd68SDeepak Kodihalli using HostStateSensorMap = std::map<SensorEntry, pdr::SensorInfo>;
53868c879aSSampa Misra using PDRList = std::vector<std::vector<uint8_t>>;
54ac19bd68SDeepak Kodihalli 
55ac19bd68SDeepak Kodihalli /** @class HostPDRHandler
56ac19bd68SDeepak Kodihalli  *  @brief This class can fetch and process PDRs from host firmware
57ac19bd68SDeepak Kodihalli  *  @details Provides an API to fetch PDRs from the host firmware. Upon
58ac19bd68SDeepak Kodihalli  *  receiving the PDRs, they are stored into the BMC's primary PDR repo.
59ac19bd68SDeepak Kodihalli  *  Adjustments are made to entity association PDRs received from the host,
60ac19bd68SDeepak Kodihalli  *  because they need to be assimilated into the BMC's entity association
61ac19bd68SDeepak Kodihalli  *  tree. A PLDM event containing the record handles of the updated entity
62ac19bd68SDeepak Kodihalli  *  association PDRs is sent to the host.
63ac19bd68SDeepak Kodihalli  */
64ac19bd68SDeepak Kodihalli class HostPDRHandler
65ac19bd68SDeepak Kodihalli {
66ac19bd68SDeepak Kodihalli   public:
67ac19bd68SDeepak Kodihalli     HostPDRHandler() = delete;
68ac19bd68SDeepak Kodihalli     HostPDRHandler(const HostPDRHandler&) = delete;
69ac19bd68SDeepak Kodihalli     HostPDRHandler(HostPDRHandler&&) = delete;
70ac19bd68SDeepak Kodihalli     HostPDRHandler& operator=(const HostPDRHandler&) = delete;
71ac19bd68SDeepak Kodihalli     HostPDRHandler& operator=(HostPDRHandler&&) = delete;
72ac19bd68SDeepak Kodihalli     ~HostPDRHandler() = default;
73ac19bd68SDeepak Kodihalli 
7460e1fe91SManojkiran Eda     using TerminusInfo =
7560e1fe91SManojkiran Eda         std::tuple<pdr::TerminusID, pdr::EID, pdr::TerminusValidity>;
7660e1fe91SManojkiran Eda     using TLPDRMap = std::map<pdr::TerminusHandle, TerminusInfo>;
77868c879aSSampa Misra 
78ac19bd68SDeepak Kodihalli     /** @brief Constructor
79ac19bd68SDeepak Kodihalli      *  @param[in] mctp_fd - fd of MCTP communications socket
80ac19bd68SDeepak Kodihalli      *  @param[in] mctp_eid - MCTP EID of host firmware
81ac19bd68SDeepak Kodihalli      *  @param[in] event - reference of main event loop of pldmd
82ac19bd68SDeepak Kodihalli      *  @param[in] repo - pointer to BMC's primary PDR repo
833aec997cSPavithra Barithaya      *  @param[in] eventsJsonDir - directory path which has the config JSONs
8474f27c73STom Joseph      *  @param[in] entityTree - Pointer to BMC and Host entity association tree
8574f27c73STom Joseph      *  @param[in] bmcEntityTree - pointer to BMC's entity association tree
86ac19bd68SDeepak Kodihalli      *  @param[in] requester - reference to Requester object
8774f27c73STom Joseph      *  @param[in] handler - PLDM request handler
88ac19bd68SDeepak Kodihalli      */
8974f27c73STom Joseph     explicit HostPDRHandler(
9074f27c73STom Joseph         int mctp_fd, uint8_t mctp_eid, sdeventplus::Event& event,
9174f27c73STom Joseph         pldm_pdr* repo, const std::string& eventsJsonsDir,
92ac19bd68SDeepak Kodihalli         pldm_entity_association_tree* entityTree,
935079ac4aSBrad Bishop         pldm_entity_association_tree* bmcEntityTree,
945079ac4aSBrad Bishop         pldm::dbus_api::Requester& requester,
95e5268cdaSTom Joseph         pldm::requester::Handler<pldm::requester::Request>* handler);
96ac19bd68SDeepak Kodihalli 
97ac19bd68SDeepak Kodihalli     /** @brief fetch PDRs from host firmware. See @class.
98ac19bd68SDeepak Kodihalli      *  @param[in] recordHandles - list of record handles pointing to host's
99ac19bd68SDeepak Kodihalli      *             PDRs that need to be fetched.
100ac19bd68SDeepak Kodihalli      */
101ac19bd68SDeepak Kodihalli 
1027246e0cdSDeepak Kodihalli     void fetchPDR(PDRRecordHandles&& recordHandles);
103ac19bd68SDeepak Kodihalli 
104ac19bd68SDeepak Kodihalli     /** @brief Send a PLDM event to host firmware containing a list of record
105ac19bd68SDeepak Kodihalli      *  handles of PDRs that the host firmware has to fetch.
106ac19bd68SDeepak Kodihalli      *  @param[in] pdrTypes - list of PDR types that need to be looked up in the
107ac19bd68SDeepak Kodihalli      *                        BMC repo
108ac19bd68SDeepak Kodihalli      *  @param[in] eventDataFormat - format for PDRRepositoryChgEvent in DSP0248
109ac19bd68SDeepak Kodihalli      */
110ac19bd68SDeepak Kodihalli     void sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
111ac19bd68SDeepak Kodihalli                                    uint8_t eventDataFormat);
112ac19bd68SDeepak Kodihalli 
113ac19bd68SDeepak Kodihalli     /** @brief Lookup host sensor info corresponding to requested SensorEntry
114ac19bd68SDeepak Kodihalli      *
115ac19bd68SDeepak Kodihalli      *  @param[in] entry - TerminusID and SensorID
116ac19bd68SDeepak Kodihalli      *
117ac19bd68SDeepak Kodihalli      *  @return SensorInfo corresponding to the input paramter SensorEntry
118ac19bd68SDeepak Kodihalli      *          throw std::out_of_range exception if not found
119ac19bd68SDeepak Kodihalli      */
120ac19bd68SDeepak Kodihalli     const pdr::SensorInfo& lookupSensorInfo(const SensorEntry& entry) const
121ac19bd68SDeepak Kodihalli     {
122ac19bd68SDeepak Kodihalli         return sensorMap.at(entry);
123ac19bd68SDeepak Kodihalli     }
124ac19bd68SDeepak Kodihalli 
1253aec997cSPavithra Barithaya     /** @brief Handles state sensor event
1263aec997cSPavithra Barithaya      *
1273aec997cSPavithra Barithaya      *  @param[in] entry - state sensor entry
1283aec997cSPavithra Barithaya      *  @param[in] state - event state
1293aec997cSPavithra Barithaya      *
1303aec997cSPavithra Barithaya      *  @return PLDM completion code
1313aec997cSPavithra Barithaya      */
1325079ac4aSBrad Bishop     int handleStateSensorEvent(
1335079ac4aSBrad Bishop         const pldm::responder::events::StateSensorEntry& entry,
1343aec997cSPavithra Barithaya         pdr::EventState state);
1353aec997cSPavithra Barithaya 
136868c879aSSampa Misra     /** @brief Parse state sensor PDRs and populate the sensorMap lookup data
137868c879aSSampa Misra      *         structure
138868c879aSSampa Misra      *
139868c879aSSampa Misra      *  @param[in] stateSensorPDRs - host state sensor PDRs
140868c879aSSampa Misra      *
141868c879aSSampa Misra      */
14260e1fe91SManojkiran Eda     void parseStateSensorPDRs(const PDRList& stateSensorPDRs);
143868c879aSSampa Misra 
144c0c79481SSampa Misra     /** @brief this function sends a GetPDR request to Host firmware.
145c0c79481SSampa Misra      *  And processes the PDRs based on type
146c0c79481SSampa Misra      *
147c0c79481SSampa Misra      *  @param[in] - nextRecordHandle - the next record handle to ask for
148c0c79481SSampa Misra      */
149c0c79481SSampa Misra     void getHostPDR(uint32_t nextRecordHandle = 0);
150c0c79481SSampa Misra 
151f9ba8c19SSampa Misra     /** @brief set the Host firmware condition when pldmd starts
1526decfc18Ssampmisr      */
153f9ba8c19SSampa Misra     void setHostFirmwareCondition();
1546decfc18Ssampmisr 
1554f2538a6SPavithra Barithaya     /** @brief set HostSensorStates when pldmd starts or restarts
1564f2538a6SPavithra Barithaya      *  and updates the D-Bus property
1574f2538a6SPavithra Barithaya      *  @param[in] stateSensorPDRs - host state sensor PDRs
1584f2538a6SPavithra Barithaya      */
15960e1fe91SManojkiran Eda     void setHostSensorState(const PDRList& stateSensorPDRs);
1604f2538a6SPavithra Barithaya 
161ae5c97ebSPavithra Barithaya     /** @brief whether we received PLDM_RECORDS_MODIFIED event data operation
162ae5c97ebSPavithra Barithaya      *  from host
163ae5c97ebSPavithra Barithaya      */
164ae5c97ebSPavithra Barithaya     bool isHostPdrModified = false;
165ae5c97ebSPavithra Barithaya 
1666decfc18Ssampmisr     /** @brief check whether Host is running when pldmd starts
1676decfc18Ssampmisr      */
1686decfc18Ssampmisr     bool isHostUp();
1696decfc18Ssampmisr 
17060e1fe91SManojkiran Eda     /** @brief map that captures various terminus information **/
17160e1fe91SManojkiran Eda     TLPDRMap tlPDRInfo;
17260e1fe91SManojkiran Eda 
1736decfc18Ssampmisr   private:
1746decfc18Ssampmisr     /** @brief deferred function to fetch PDR from Host, scheduled to work on
1756decfc18Ssampmisr      *  the event loop. The PDR exchg with the host is async.
1766decfc18Ssampmisr      *  @param[in] source - sdeventplus event source
1776decfc18Ssampmisr      */
1786decfc18Ssampmisr     void _fetchPDR(sdeventplus::source::EventBase& source);
1796decfc18Ssampmisr 
180ac19bd68SDeepak Kodihalli     /** @brief Merge host firmware's entity association PDRs into BMC's
181ac19bd68SDeepak Kodihalli      *  @details A merge operation involves adding a pldm_entity under the
182ac19bd68SDeepak Kodihalli      *  appropriate parent, and updating container ids.
183ac19bd68SDeepak Kodihalli      *  @param[in] pdr - entity association pdr
184ac19bd68SDeepak Kodihalli      */
185ac19bd68SDeepak Kodihalli     void mergeEntityAssociations(const std::vector<uint8_t>& pdr);
186ac19bd68SDeepak Kodihalli 
187ac19bd68SDeepak Kodihalli     /** @brief Find parent of input entity type, from the entity association
188ac19bd68SDeepak Kodihalli      *  tree
189ac19bd68SDeepak Kodihalli      *  @param[in] type - PLDM entity type
190ac19bd68SDeepak Kodihalli      *  @param[out] parent - PLDM entity information of parent
191ac19bd68SDeepak Kodihalli      *  @return bool - true if parent found, false otherwise
192ac19bd68SDeepak Kodihalli      */
193ac19bd68SDeepak Kodihalli     bool getParent(EntityType type, pldm_entity& parent);
194ac19bd68SDeepak Kodihalli 
195c0c79481SSampa Misra     /** @brief process the Host's PDR and add to BMC's PDR repo
196c0c79481SSampa Misra      *  @param[in] eid - MCTP id of Host
197c0c79481SSampa Misra      *  @param[in] response - response from Host for GetPDR
198c0c79481SSampa Misra      *  @param[in] respMsgLen - response message length
199c0c79481SSampa Misra      */
200c0c79481SSampa Misra     void processHostPDRs(mctp_eid_t eid, const pldm_msg* response,
201c0c79481SSampa Misra                          size_t respMsgLen);
202c0c79481SSampa Misra 
203c0c79481SSampa Misra     /** @brief send PDR Repo change after merging Host's PDR to BMC PDR repo
204c0c79481SSampa Misra      *  @param[in] source - sdeventplus event source
205c0c79481SSampa Misra      */
206c0c79481SSampa Misra     void _processPDRRepoChgEvent(sdeventplus::source::EventBase& source);
207c0c79481SSampa Misra 
208c0c79481SSampa Misra     /** @brief fetch the next PDR based on the record handle sent by Host
209c0c79481SSampa Misra      *  @param[in] nextRecordHandle - next record handle
210c0c79481SSampa Misra      *  @param[in] source - sdeventplus event source
211c0c79481SSampa Misra      */
212c0c79481SSampa Misra     void _processFetchPDREvent(uint32_t nextRecordHandle,
213c0c79481SSampa Misra                                sdeventplus::source::EventBase& source);
214c0c79481SSampa Misra 
215ac19bd68SDeepak Kodihalli     /** @brief fd of MCTP communications socket */
216ac19bd68SDeepak Kodihalli     int mctp_fd;
217ac19bd68SDeepak Kodihalli     /** @brief MCTP EID of host firmware */
218ac19bd68SDeepak Kodihalli     uint8_t mctp_eid;
219ac19bd68SDeepak Kodihalli     /** @brief reference of main event loop of pldmd, primarily used to schedule
220ac19bd68SDeepak Kodihalli      *  work.
221ac19bd68SDeepak Kodihalli      */
222ac19bd68SDeepak Kodihalli     sdeventplus::Event& event;
223ac19bd68SDeepak Kodihalli     /** @brief pointer to BMC's primary PDR repo, host PDRs are added here */
224ac19bd68SDeepak Kodihalli     pldm_pdr* repo;
2253aec997cSPavithra Barithaya 
2265079ac4aSBrad Bishop     pldm::responder::events::StateSensorHandler stateSensorHandler;
227c073a20eSSampa Misra     /** @brief Pointer to BMC's and Host's entity association tree */
228ac19bd68SDeepak Kodihalli     pldm_entity_association_tree* entityTree;
229c073a20eSSampa Misra 
230c073a20eSSampa Misra     /** @brief Pointer to BMC's entity association tree */
231c073a20eSSampa Misra     pldm_entity_association_tree* bmcEntityTree;
232c073a20eSSampa Misra 
233ac19bd68SDeepak Kodihalli     /** @brief reference to Requester object, primarily used to access API to
234ac19bd68SDeepak Kodihalli      *  obtain PLDM instance id.
235ac19bd68SDeepak Kodihalli      */
2365079ac4aSBrad Bishop     pldm::dbus_api::Requester& requester;
23774f27c73STom Joseph 
23874f27c73STom Joseph     /** @brief PLDM request handler */
239c0c79481SSampa Misra     pldm::requester::Handler<pldm::requester::Request>* handler;
24074f27c73STom Joseph 
241ac19bd68SDeepak Kodihalli     /** @brief sdeventplus event source */
242ac19bd68SDeepak Kodihalli     std::unique_ptr<sdeventplus::source::Defer> pdrFetchEvent;
243c0c79481SSampa Misra     std::unique_ptr<sdeventplus::source::Defer> deferredFetchPDREvent;
244c0c79481SSampa Misra     std::unique_ptr<sdeventplus::source::Defer> deferredPDRRepoChgEvent;
245c0c79481SSampa Misra 
246ac19bd68SDeepak Kodihalli     /** @brief list of PDR record handles pointing to host's PDRs */
247ac19bd68SDeepak Kodihalli     PDRRecordHandles pdrRecordHandles;
248ac19bd68SDeepak Kodihalli     /** @brief maps an entity type to parent pldm_entity from the BMC's entity
249ac19bd68SDeepak Kodihalli      *  association tree
250ac19bd68SDeepak Kodihalli      */
251ae5c97ebSPavithra Barithaya 
252ae5c97ebSPavithra Barithaya     /** @brief list of PDR record handles modified pointing to host PDRs */
253ae5c97ebSPavithra Barithaya     PDRRecordHandles modifiedPDRRecordHandles;
254ae5c97ebSPavithra Barithaya 
255ac19bd68SDeepak Kodihalli     std::map<EntityType, pldm_entity> parents;
256ac19bd68SDeepak Kodihalli     /** @brief D-Bus property changed signal match */
25784b790cbSPatrick Williams     std::unique_ptr<sdbusplus::bus::match_t> hostOffMatch;
258ac19bd68SDeepak Kodihalli 
259ac19bd68SDeepak Kodihalli     /** @brief sensorMap is a lookup data structure that is build from the
260ac19bd68SDeepak Kodihalli      *         hostPDR that speeds up the lookup of <TerminusID, SensorID> in
261ac19bd68SDeepak Kodihalli      *         PlatformEventMessage command request.
262ac19bd68SDeepak Kodihalli      */
263ac19bd68SDeepak Kodihalli     HostStateSensorMap sensorMap;
2646decfc18Ssampmisr 
2656decfc18Ssampmisr     /** @brief whether response received from Host */
2666decfc18Ssampmisr     bool responseReceived;
267ac19bd68SDeepak Kodihalli };
268ac19bd68SDeepak Kodihalli 
269ac19bd68SDeepak Kodihalli } // namespace pldm
270