1ac19bd68SDeepak Kodihalli #pragma once
2ac19bd68SDeepak Kodihalli 
3ac19bd68SDeepak Kodihalli #include "libpldm/base.h"
4ac19bd68SDeepak Kodihalli #include "libpldm/platform.h"
5ac19bd68SDeepak Kodihalli 
6d130e1a3SDeepak Kodihalli #include "common/types.hpp"
7d130e1a3SDeepak Kodihalli #include "common/utils.hpp"
83aec997cSPavithra Barithaya #include "libpldmresponder/event_parser.hpp"
9ac19bd68SDeepak Kodihalli #include "libpldmresponder/pdr_utils.hpp"
101521f6d1SDeepak Kodihalli #include "pldmd/dbus_impl_requester.hpp"
1174f27c73STom Joseph #include "requester/handler.hpp"
12ac19bd68SDeepak Kodihalli 
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 
24ac19bd68SDeepak Kodihalli using EntityType = uint16_t;
25ac19bd68SDeepak Kodihalli // vector which would hold the PDR record handle data returned by
26ac19bd68SDeepak Kodihalli // pldmPDRRepositoryChgEvent event data
27ac19bd68SDeepak Kodihalli using ChangeEntry = uint32_t;
287246e0cdSDeepak Kodihalli using PDRRecordHandles = std::deque<ChangeEntry>;
29ac19bd68SDeepak Kodihalli 
30ac19bd68SDeepak Kodihalli /** @struct SensorEntry
31ac19bd68SDeepak Kodihalli  *
32ac19bd68SDeepak Kodihalli  *  SensorEntry is a unique key which maps a sensorEventType request in the
33ac19bd68SDeepak Kodihalli  *  PlatformEventMessage command to a host sensor PDR. This struct is a key
34ac19bd68SDeepak Kodihalli  *  in a std::map, so implemented operator==and operator<.
35ac19bd68SDeepak Kodihalli  */
36ac19bd68SDeepak Kodihalli struct SensorEntry
37ac19bd68SDeepak Kodihalli {
38ac19bd68SDeepak Kodihalli     pdr::TerminusID terminusID;
39ac19bd68SDeepak Kodihalli     pdr::SensorID sensorID;
40ac19bd68SDeepak Kodihalli 
41ac19bd68SDeepak Kodihalli     bool operator==(const SensorEntry& e) const
42ac19bd68SDeepak Kodihalli     {
43ac19bd68SDeepak Kodihalli         return ((terminusID == e.terminusID) && (sensorID == e.sensorID));
44ac19bd68SDeepak Kodihalli     }
45ac19bd68SDeepak Kodihalli 
46ac19bd68SDeepak Kodihalli     bool operator<(const SensorEntry& e) const
47ac19bd68SDeepak Kodihalli     {
48ac19bd68SDeepak Kodihalli         return ((terminusID < e.terminusID) ||
49ac19bd68SDeepak Kodihalli                 ((terminusID == e.terminusID) && (sensorID < e.sensorID)));
50ac19bd68SDeepak Kodihalli     }
51ac19bd68SDeepak Kodihalli };
52ac19bd68SDeepak Kodihalli 
534f2538a6SPavithra Barithaya /* @struct TerminusLocatorInfo
544f2538a6SPavithra Barithaya  * Contains validity, eid, terminus_id and terminus handle
554f2538a6SPavithra Barithaya  * of a terminus locator PDR.
564f2538a6SPavithra Barithaya  */
574f2538a6SPavithra Barithaya struct TlInfo
584f2538a6SPavithra Barithaya {
594f2538a6SPavithra Barithaya     uint8_t valid;
604f2538a6SPavithra Barithaya     uint8_t eid;
614f2538a6SPavithra Barithaya     uint8_t tid;
624f2538a6SPavithra Barithaya     uint16_t terminusHandle;
634f2538a6SPavithra Barithaya };
644f2538a6SPavithra Barithaya 
65ac19bd68SDeepak Kodihalli using HostStateSensorMap = std::map<SensorEntry, pdr::SensorInfo>;
66868c879aSSampa Misra using PDRList = std::vector<std::vector<uint8_t>>;
67ac19bd68SDeepak Kodihalli 
68ac19bd68SDeepak Kodihalli /** @class HostPDRHandler
69ac19bd68SDeepak Kodihalli  *  @brief This class can fetch and process PDRs from host firmware
70ac19bd68SDeepak Kodihalli  *  @details Provides an API to fetch PDRs from the host firmware. Upon
71ac19bd68SDeepak Kodihalli  *  receiving the PDRs, they are stored into the BMC's primary PDR repo.
72ac19bd68SDeepak Kodihalli  *  Adjustments are made to entity association PDRs received from the host,
73ac19bd68SDeepak Kodihalli  *  because they need to be assimilated into the BMC's entity association
74ac19bd68SDeepak Kodihalli  *  tree. A PLDM event containing the record handles of the updated entity
75ac19bd68SDeepak Kodihalli  *  association PDRs is sent to the host.
76ac19bd68SDeepak Kodihalli  */
77ac19bd68SDeepak Kodihalli class HostPDRHandler
78ac19bd68SDeepak Kodihalli {
79ac19bd68SDeepak Kodihalli   public:
80ac19bd68SDeepak Kodihalli     HostPDRHandler() = delete;
81ac19bd68SDeepak Kodihalli     HostPDRHandler(const HostPDRHandler&) = delete;
82ac19bd68SDeepak Kodihalli     HostPDRHandler(HostPDRHandler&&) = delete;
83ac19bd68SDeepak Kodihalli     HostPDRHandler& operator=(const HostPDRHandler&) = delete;
84ac19bd68SDeepak Kodihalli     HostPDRHandler& operator=(HostPDRHandler&&) = delete;
85ac19bd68SDeepak Kodihalli     ~HostPDRHandler() = default;
86ac19bd68SDeepak Kodihalli 
87868c879aSSampa Misra     using TLPDRMap = std::map<pdr::TerminusHandle, pdr::TerminusID>;
88868c879aSSampa Misra 
89ac19bd68SDeepak Kodihalli     /** @brief Constructor
90ac19bd68SDeepak Kodihalli      *  @param[in] mctp_fd - fd of MCTP communications socket
91ac19bd68SDeepak Kodihalli      *  @param[in] mctp_eid - MCTP EID of host firmware
92ac19bd68SDeepak Kodihalli      *  @param[in] event - reference of main event loop of pldmd
93ac19bd68SDeepak Kodihalli      *  @param[in] repo - pointer to BMC's primary PDR repo
943aec997cSPavithra Barithaya      *  @param[in] eventsJsonDir - directory path which has the config JSONs
9574f27c73STom Joseph      *  @param[in] entityTree - Pointer to BMC and Host entity association tree
9674f27c73STom Joseph      *  @param[in] bmcEntityTree - pointer to BMC's entity association tree
97ac19bd68SDeepak Kodihalli      *  @param[in] requester - reference to Requester object
9874f27c73STom Joseph      *  @param[in] handler - PLDM request handler
99ac19bd68SDeepak Kodihalli      */
10074f27c73STom Joseph     explicit HostPDRHandler(
10174f27c73STom Joseph         int mctp_fd, uint8_t mctp_eid, sdeventplus::Event& event,
10274f27c73STom Joseph         pldm_pdr* repo, const std::string& eventsJsonsDir,
103ac19bd68SDeepak Kodihalli         pldm_entity_association_tree* entityTree,
1045079ac4aSBrad Bishop         pldm_entity_association_tree* bmcEntityTree,
1055079ac4aSBrad Bishop         pldm::dbus_api::Requester& requester,
106*e5268cdaSTom Joseph         pldm::requester::Handler<pldm::requester::Request>* handler);
107ac19bd68SDeepak Kodihalli 
108ac19bd68SDeepak Kodihalli     /** @brief fetch PDRs from host firmware. See @class.
109ac19bd68SDeepak Kodihalli      *  @param[in] recordHandles - list of record handles pointing to host's
110ac19bd68SDeepak Kodihalli      *             PDRs that need to be fetched.
111ac19bd68SDeepak Kodihalli      */
112ac19bd68SDeepak Kodihalli 
1137246e0cdSDeepak Kodihalli     void fetchPDR(PDRRecordHandles&& recordHandles);
114ac19bd68SDeepak Kodihalli 
115ac19bd68SDeepak Kodihalli     /** @brief Send a PLDM event to host firmware containing a list of record
116ac19bd68SDeepak Kodihalli      *  handles of PDRs that the host firmware has to fetch.
117ac19bd68SDeepak Kodihalli      *  @param[in] pdrTypes - list of PDR types that need to be looked up in the
118ac19bd68SDeepak Kodihalli      *                        BMC repo
119ac19bd68SDeepak Kodihalli      *  @param[in] eventDataFormat - format for PDRRepositoryChgEvent in DSP0248
120ac19bd68SDeepak Kodihalli      */
121ac19bd68SDeepak Kodihalli     void sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
122ac19bd68SDeepak Kodihalli                                    uint8_t eventDataFormat);
123ac19bd68SDeepak Kodihalli 
124ac19bd68SDeepak Kodihalli     /** @brief Lookup host sensor info corresponding to requested SensorEntry
125ac19bd68SDeepak Kodihalli      *
126ac19bd68SDeepak Kodihalli      *  @param[in] entry - TerminusID and SensorID
127ac19bd68SDeepak Kodihalli      *
128ac19bd68SDeepak Kodihalli      *  @return SensorInfo corresponding to the input paramter SensorEntry
129ac19bd68SDeepak Kodihalli      *          throw std::out_of_range exception if not found
130ac19bd68SDeepak Kodihalli      */
131ac19bd68SDeepak Kodihalli     const pdr::SensorInfo& lookupSensorInfo(const SensorEntry& entry) const
132ac19bd68SDeepak Kodihalli     {
133ac19bd68SDeepak Kodihalli         return sensorMap.at(entry);
134ac19bd68SDeepak Kodihalli     }
135ac19bd68SDeepak Kodihalli 
1363aec997cSPavithra Barithaya     /** @brief Handles state sensor event
1373aec997cSPavithra Barithaya      *
1383aec997cSPavithra Barithaya      *  @param[in] entry - state sensor entry
1393aec997cSPavithra Barithaya      *  @param[in] state - event state
1403aec997cSPavithra Barithaya      *
1413aec997cSPavithra Barithaya      *  @return PLDM completion code
1423aec997cSPavithra Barithaya      */
1435079ac4aSBrad Bishop     int handleStateSensorEvent(
1445079ac4aSBrad Bishop         const pldm::responder::events::StateSensorEntry& entry,
1453aec997cSPavithra Barithaya         pdr::EventState state);
1463aec997cSPavithra Barithaya 
147868c879aSSampa Misra     /** @brief Parse state sensor PDRs and populate the sensorMap lookup data
148868c879aSSampa Misra      *         structure
149868c879aSSampa Misra      *
150868c879aSSampa Misra      *  @param[in] stateSensorPDRs - host state sensor PDRs
151868c879aSSampa Misra      *  @param[in] tlpdrInfo - terminus locator PDRs info
152868c879aSSampa Misra      *
153868c879aSSampa Misra      */
154868c879aSSampa Misra     void parseStateSensorPDRs(const PDRList& stateSensorPDRs,
155868c879aSSampa Misra                               const TLPDRMap& tlpdrInfo);
156868c879aSSampa Misra 
157c0c79481SSampa Misra     /** @brief this function sends a GetPDR request to Host firmware.
158c0c79481SSampa Misra      *  And processes the PDRs based on type
159c0c79481SSampa Misra      *
160c0c79481SSampa Misra      *  @param[in] - nextRecordHandle - the next record handle to ask for
161c0c79481SSampa Misra      */
162c0c79481SSampa Misra     void getHostPDR(uint32_t nextRecordHandle = 0);
163c0c79481SSampa Misra 
164f9ba8c19SSampa Misra     /** @brief set the Host firmware condition when pldmd starts
1656decfc18Ssampmisr      */
166f9ba8c19SSampa Misra     void setHostFirmwareCondition();
1676decfc18Ssampmisr 
1684f2538a6SPavithra Barithaya     /** @brief set HostSensorStates when pldmd starts or restarts
1694f2538a6SPavithra Barithaya      *  and updates the D-Bus property
1704f2538a6SPavithra Barithaya      *  @param[in] stateSensorPDRs - host state sensor PDRs
1714f2538a6SPavithra Barithaya      *  @param[in] tlinfo - vector of struct TlInfo
1724f2538a6SPavithra Barithaya      */
1734f2538a6SPavithra Barithaya     void setHostSensorState(const PDRList& stateSensorPDRs,
1744f2538a6SPavithra Barithaya                             const std::vector<TlInfo>& tlinfo);
1754f2538a6SPavithra Barithaya 
1766decfc18Ssampmisr     /** @brief check whether Host is running when pldmd starts
1776decfc18Ssampmisr      */
1786decfc18Ssampmisr     bool isHostUp();
1796decfc18Ssampmisr 
1806decfc18Ssampmisr   private:
1816decfc18Ssampmisr     /** @brief deferred function to fetch PDR from Host, scheduled to work on
1826decfc18Ssampmisr      *  the event loop. The PDR exchg with the host is async.
1836decfc18Ssampmisr      *  @param[in] source - sdeventplus event source
1846decfc18Ssampmisr      */
1856decfc18Ssampmisr     void _fetchPDR(sdeventplus::source::EventBase& source);
1866decfc18Ssampmisr 
187ac19bd68SDeepak Kodihalli     /** @brief Merge host firmware's entity association PDRs into BMC's
188ac19bd68SDeepak Kodihalli      *  @details A merge operation involves adding a pldm_entity under the
189ac19bd68SDeepak Kodihalli      *  appropriate parent, and updating container ids.
190ac19bd68SDeepak Kodihalli      *  @param[in] pdr - entity association pdr
191ac19bd68SDeepak Kodihalli      */
192ac19bd68SDeepak Kodihalli     void mergeEntityAssociations(const std::vector<uint8_t>& pdr);
193ac19bd68SDeepak Kodihalli 
194ac19bd68SDeepak Kodihalli     /** @brief Find parent of input entity type, from the entity association
195ac19bd68SDeepak Kodihalli      *  tree
196ac19bd68SDeepak Kodihalli      *  @param[in] type - PLDM entity type
197ac19bd68SDeepak Kodihalli      *  @param[out] parent - PLDM entity information of parent
198ac19bd68SDeepak Kodihalli      *  @return bool - true if parent found, false otherwise
199ac19bd68SDeepak Kodihalli      */
200ac19bd68SDeepak Kodihalli     bool getParent(EntityType type, pldm_entity& parent);
201ac19bd68SDeepak Kodihalli 
202c0c79481SSampa Misra     /** @brief process the Host's PDR and add to BMC's PDR repo
203c0c79481SSampa Misra      *  @param[in] eid - MCTP id of Host
204c0c79481SSampa Misra      *  @param[in] response - response from Host for GetPDR
205c0c79481SSampa Misra      *  @param[in] respMsgLen - response message length
206c0c79481SSampa Misra      */
207c0c79481SSampa Misra     void processHostPDRs(mctp_eid_t eid, const pldm_msg* response,
208c0c79481SSampa Misra                          size_t respMsgLen);
209c0c79481SSampa Misra 
210c0c79481SSampa Misra     /** @brief send PDR Repo change after merging Host's PDR to BMC PDR repo
211c0c79481SSampa Misra      *  @param[in] source - sdeventplus event source
212c0c79481SSampa Misra      */
213c0c79481SSampa Misra     void _processPDRRepoChgEvent(sdeventplus::source::EventBase& source);
214c0c79481SSampa Misra 
215c0c79481SSampa Misra     /** @brief fetch the next PDR based on the record handle sent by Host
216c0c79481SSampa Misra      *  @param[in] nextRecordHandle - next record handle
217c0c79481SSampa Misra      *  @param[in] source - sdeventplus event source
218c0c79481SSampa Misra      */
219c0c79481SSampa Misra     void _processFetchPDREvent(uint32_t nextRecordHandle,
220c0c79481SSampa Misra                                sdeventplus::source::EventBase& source);
221c0c79481SSampa Misra 
222ac19bd68SDeepak Kodihalli     /** @brief fd of MCTP communications socket */
223ac19bd68SDeepak Kodihalli     int mctp_fd;
224ac19bd68SDeepak Kodihalli     /** @brief MCTP EID of host firmware */
225ac19bd68SDeepak Kodihalli     uint8_t mctp_eid;
226ac19bd68SDeepak Kodihalli     /** @brief reference of main event loop of pldmd, primarily used to schedule
227ac19bd68SDeepak Kodihalli      *  work.
228ac19bd68SDeepak Kodihalli      */
229ac19bd68SDeepak Kodihalli     sdeventplus::Event& event;
230ac19bd68SDeepak Kodihalli     /** @brief pointer to BMC's primary PDR repo, host PDRs are added here */
231ac19bd68SDeepak Kodihalli     pldm_pdr* repo;
2323aec997cSPavithra Barithaya 
2335079ac4aSBrad Bishop     pldm::responder::events::StateSensorHandler stateSensorHandler;
234c073a20eSSampa Misra     /** @brief Pointer to BMC's and Host's entity association tree */
235ac19bd68SDeepak Kodihalli     pldm_entity_association_tree* entityTree;
236c073a20eSSampa Misra 
237c073a20eSSampa Misra     /** @brief Pointer to BMC's entity association tree */
238c073a20eSSampa Misra     pldm_entity_association_tree* bmcEntityTree;
239c073a20eSSampa Misra 
240ac19bd68SDeepak Kodihalli     /** @brief reference to Requester object, primarily used to access API to
241ac19bd68SDeepak Kodihalli      *  obtain PLDM instance id.
242ac19bd68SDeepak Kodihalli      */
2435079ac4aSBrad Bishop     pldm::dbus_api::Requester& requester;
24474f27c73STom Joseph 
24574f27c73STom Joseph     /** @brief PLDM request handler */
246c0c79481SSampa Misra     pldm::requester::Handler<pldm::requester::Request>* handler;
24774f27c73STom Joseph 
248ac19bd68SDeepak Kodihalli     /** @brief sdeventplus event source */
249ac19bd68SDeepak Kodihalli     std::unique_ptr<sdeventplus::source::Defer> pdrFetchEvent;
250c0c79481SSampa Misra     std::unique_ptr<sdeventplus::source::Defer> deferredFetchPDREvent;
251c0c79481SSampa Misra     std::unique_ptr<sdeventplus::source::Defer> deferredPDRRepoChgEvent;
252c0c79481SSampa Misra 
253ac19bd68SDeepak Kodihalli     /** @brief list of PDR record handles pointing to host's PDRs */
254ac19bd68SDeepak Kodihalli     PDRRecordHandles pdrRecordHandles;
255ac19bd68SDeepak Kodihalli     /** @brief maps an entity type to parent pldm_entity from the BMC's entity
256ac19bd68SDeepak Kodihalli      *  association tree
257ac19bd68SDeepak Kodihalli      */
258ac19bd68SDeepak Kodihalli     std::map<EntityType, pldm_entity> parents;
259ac19bd68SDeepak Kodihalli     /** @brief D-Bus property changed signal match */
260ac19bd68SDeepak Kodihalli     std::unique_ptr<sdbusplus::bus::match::match> hostOffMatch;
261ac19bd68SDeepak Kodihalli 
262ac19bd68SDeepak Kodihalli     /** @brief sensorMap is a lookup data structure that is build from the
263ac19bd68SDeepak Kodihalli      *         hostPDR that speeds up the lookup of <TerminusID, SensorID> in
264ac19bd68SDeepak Kodihalli      *         PlatformEventMessage command request.
265ac19bd68SDeepak Kodihalli      */
266ac19bd68SDeepak Kodihalli     HostStateSensorMap sensorMap;
2676decfc18Ssampmisr 
2686decfc18Ssampmisr     /** @brief whether response received from Host */
2696decfc18Ssampmisr     bool responseReceived;
270ac19bd68SDeepak Kodihalli };
271ac19bd68SDeepak Kodihalli 
272ac19bd68SDeepak Kodihalli } // namespace pldm
273