1 #pragma once
2 
3 #include "common/instance_id.hpp"
4 #include "common/types.hpp"
5 #include "common/utils.hpp"
6 #include "libpldmresponder/event_parser.hpp"
7 #include "libpldmresponder/oem_handler.hpp"
8 #include "libpldmresponder/pdr_utils.hpp"
9 #include "requester/handler.hpp"
10 #include "utils.hpp"
11 
12 #include <libpldm/base.h>
13 #include <libpldm/platform.h>
14 
15 #include <sdeventplus/event.hpp>
16 #include <sdeventplus/source/event.hpp>
17 
18 #include <deque>
19 #include <filesystem>
20 #include <map>
21 #include <memory>
22 #include <vector>
23 
24 namespace pldm
25 {
26 // vector which would hold the PDR record handle data returned by
27 // pldmPDRRepositoryChgEvent event data
28 using ChangeEntry = uint32_t;
29 using PDRRecordHandles = std::deque<ChangeEntry>;
30 
31 /** @struct SensorEntry
32  *
33  *  SensorEntry is a unique key which maps a sensorEventType request in the
34  *  PlatformEventMessage command to a host sensor PDR. This struct is a key
35  *  in a std::map, so implemented operator==and operator<.
36  */
37 struct SensorEntry
38 {
39     pdr::TerminusID terminusID;
40     pdr::SensorID sensorID;
41 
42     bool operator==(const SensorEntry& e) const
43     {
44         return ((terminusID == e.terminusID) && (sensorID == e.sensorID));
45     }
46 
47     bool operator<(const SensorEntry& e) const
48     {
49         return ((terminusID < e.terminusID) ||
50                 ((terminusID == e.terminusID) && (sensorID < e.sensorID)));
51     }
52 };
53 
54 using HostStateSensorMap = std::map<SensorEntry, pdr::SensorInfo>;
55 using PDRList = std::vector<std::vector<uint8_t>>;
56 
57 /** @class HostPDRHandler
58  *  @brief This class can fetch and process PDRs from host firmware
59  *  @details Provides an API to fetch PDRs from the host firmware. Upon
60  *  receiving the PDRs, they are stored into the BMC's primary PDR repo.
61  *  Adjustments are made to entity association PDRs received from the host,
62  *  because they need to be assimilated into the BMC's entity association
63  *  tree. A PLDM event containing the record handles of the updated entity
64  *  association PDRs is sent to the host.
65  */
66 class HostPDRHandler
67 {
68   public:
69     HostPDRHandler() = delete;
70     HostPDRHandler(const HostPDRHandler&) = delete;
71     HostPDRHandler(HostPDRHandler&&) = delete;
72     HostPDRHandler& operator=(const HostPDRHandler&) = delete;
73     HostPDRHandler& operator=(HostPDRHandler&&) = delete;
74     ~HostPDRHandler() = default;
75 
76     using TerminusInfo =
77         std::tuple<pdr::TerminusID, pdr::EID, pdr::TerminusValidity>;
78     using TLPDRMap = std::map<pdr::TerminusHandle, TerminusInfo>;
79 
80     /** @brief Constructor
81      *  @param[in] mctp_fd - fd of MCTP communications socket
82      *  @param[in] mctp_eid - MCTP EID of host firmware
83      *  @param[in] event - reference of main event loop of pldmd
84      *  @param[in] repo - pointer to BMC's primary PDR repo
85      *  @param[in] eventsJsonDir - directory path which has the config JSONs
86      *  @param[in] entityTree - Pointer to BMC and Host entity association tree
87      *  @param[in] bmcEntityTree - pointer to BMC's entity association tree
88      *  @param[in] instanceIdDb - reference to an InstanceIdDb object
89      *  @param[in] handler - PLDM request handler
90      *  @param[in] oemUtilsHandler - pointer oem utils handler
91      */
92     explicit HostPDRHandler(
93         int mctp_fd, uint8_t mctp_eid, sdeventplus::Event& event,
94         pldm_pdr* repo, const std::string& eventsJsonsDir,
95         pldm_entity_association_tree* entityTree,
96         pldm_entity_association_tree* bmcEntityTree,
97         pldm::InstanceIdDb& instanceIdDb,
98         pldm::requester::Handler<pldm::requester::Request>* handler);
99 
100     /** @brief fetch PDRs from host firmware. See @class.
101      *  @param[in] recordHandles - list of record handles pointing to host's
102      *             PDRs that need to be fetched.
103      */
104 
105     void fetchPDR(PDRRecordHandles&& recordHandles);
106 
107     /** @brief Send a PLDM event to host firmware containing a list of record
108      *  handles of PDRs that the host firmware has to fetch.
109      *  @param[in] pdrTypes - list of PDR types that need to be looked up in the
110      *                        BMC repo
111      *  @param[in] eventDataFormat - format for PDRRepositoryChgEvent in DSP0248
112      */
113     void sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
114                                    uint8_t eventDataFormat);
115 
116     /** @brief Lookup host sensor info corresponding to requested SensorEntry
117      *
118      *  @param[in] entry - TerminusID and SensorID
119      *
120      *  @return SensorInfo corresponding to the input parameter SensorEntry
121      *          throw std::out_of_range exception if not found
122      */
123     const pdr::SensorInfo& lookupSensorInfo(const SensorEntry& entry) const
124     {
125         return sensorMap.at(entry);
126     }
127 
128     /** @brief Handles state sensor event
129      *
130      *  @param[in] entry - state sensor entry
131      *  @param[in] state - event state
132      *
133      *  @return PLDM completion code
134      */
135     int handleStateSensorEvent(
136         const pldm::responder::events::StateSensorEntry& entry,
137         pdr::EventState state);
138 
139     /** @brief Parse state sensor PDRs and populate the sensorMap lookup data
140      *         structure
141      *
142      *  @param[in] stateSensorPDRs - host state sensor PDRs
143      *
144      */
145     void parseStateSensorPDRs(const PDRList& stateSensorPDRs);
146 
147     /** @brief this function sends a GetPDR request to Host firmware.
148      *  And processes the PDRs based on type
149      *
150      *  @param[in] - nextRecordHandle - the next record handle to ask for
151      */
152     void getHostPDR(uint32_t nextRecordHandle = 0);
153 
154     /** @brief set the Host firmware condition when pldmd starts
155      */
156     void setHostFirmwareCondition();
157 
158     /** @brief set HostSensorStates when pldmd starts or restarts
159      *  and updates the D-Bus property
160      *  @param[in] stateSensorPDRs - host state sensor PDRs
161      */
162     void setHostSensorState(const PDRList& stateSensorPDRs);
163 
164     /** @brief whether we received PLDM_RECORDS_MODIFIED event data operation
165      *  from host
166      */
167     bool isHostPdrModified = false;
168 
169     /** @brief check whether Host is running when pldmd starts
170      */
171     bool isHostUp();
172 
173     /* @brief Method to set the oem platform handler in host pdr handler class
174      *
175      * @param[in] handler - oem platform handler
176      */
177     inline void
178         setOemPlatformHandler(pldm::responder::oem_platform::Handler* handler)
179     {
180         oemPlatformHandler = handler;
181     }
182 
183     /* @brief Method to set the oem utils handler in host pdr handler class
184      *
185      * @param[in] handler - oem utils handler
186      */
187     inline void setOemUtilsHandler(pldm::responder::oem_utils::Handler* handler)
188     {
189         oemUtilsHandler = handler;
190     }
191 
192     /** @brief map that captures various terminus information **/
193     TLPDRMap tlPDRInfo;
194 
195   private:
196     /** @brief deferred function to fetch PDR from Host, scheduled to work on
197      *  the event loop. The PDR exchg with the host is async.
198      *  @param[in] source - sdeventplus event source
199      */
200     void _fetchPDR(sdeventplus::source::EventBase& source);
201 
202     /** @brief Merge host firmware's entity association PDRs into BMC's
203      *  @details A merge operation involves adding a pldm_entity under the
204      *  appropriate parent, and updating container ids.
205      *  @param[in] pdr - entity association pdr
206      *  @param[in] size - size of input PDR record in bytes
207      *  @param[in] record_handle - record handle of the PDR
208      */
209     void mergeEntityAssociations(
210         const std::vector<uint8_t>& pdr, [[maybe_unused]] const uint32_t& size,
211         [[maybe_unused]] const uint32_t& record_handle);
212 
213     /** @brief process the Host's PDR and add to BMC's PDR repo
214      *  @param[in] eid - MCTP id of Host
215      *  @param[in] response - response from Host for GetPDR
216      *  @param[in] respMsgLen - response message length
217      */
218     void processHostPDRs(mctp_eid_t eid, const pldm_msg* response,
219                          size_t respMsgLen);
220 
221     /** @brief send PDR Repo change after merging Host's PDR to BMC PDR repo
222      *  @param[in] source - sdeventplus event source
223      */
224     void _processPDRRepoChgEvent(sdeventplus::source::EventBase& source);
225 
226     /** @brief fetch the next PDR based on the record handle sent by Host
227      *  @param[in] nextRecordHandle - next record handle
228      *  @param[in] source - sdeventplus event source
229      */
230     void _processFetchPDREvent(uint32_t nextRecordHandle,
231                                sdeventplus::source::EventBase& source);
232 
233     /** @brief Get FRU record table metadata by remote PLDM terminus
234      *
235      *  @param[out] uint16_t    - total table records
236      */
237     void getFRURecordTableMetadataByRemote(const PDRList& fruRecordSetPDRs);
238 
239     /** @brief Set Location Code in the dbus objects
240      *
241      *  @param[in] fruRecordSetPDRs - the Fru Record set PDR's
242      *  @param[in] fruRecordData - the Fru Record Data
243      */
244 
245     void setFRUDataOnDBus(
246         const PDRList& fruRecordSetPDRs,
247         const std::vector<responder::pdr_utils::FruRecordDataFormat>&
248             fruRecordData);
249 
250     /** @brief Get FRU record table by remote PLDM terminus
251      *
252      *  @param[in] fruRecordSetPDRs  - the Fru Record set PDR's
253      *  @param[in] totalTableRecords - the Number of total table records
254      *  @return
255      */
256     void getFRURecordTableByRemote(const PDRList& fruRecordSetPDRs,
257                                    uint16_t totalTableRecords);
258 
259     /** @brief Create Dbus objects by remote PLDM entity Fru PDRs
260      *
261      *  @param[in] fruRecordSetPDRs - fru record set pdr
262      *
263      * @ return
264      */
265     void createDbusObjects(const PDRList& fruRecordSetPDRs);
266 
267     /** @brief Get FRU Record Set Identifier from FRU Record data Format
268      *  @param[in] fruRecordSetPDRs - fru record set pdr
269      *  @param[in] entity           - PLDM entity information
270      *  @return
271      */
272     std::optional<uint16_t> getRSI(const PDRList& fruRecordSetPDRs,
273                                    const pldm_entity& entity);
274 
275     /** @brief MCTP EID of host firmware */
276     uint8_t mctp_eid;
277     /** @brief reference of main event loop of pldmd, primarily used to schedule
278      *  work.
279      */
280     sdeventplus::Event& event;
281     /** @brief pointer to BMC's primary PDR repo, host PDRs are added here */
282     pldm_pdr* repo;
283 
284     pldm::responder::events::StateSensorHandler stateSensorHandler;
285     /** @brief Pointer to BMC's and Host's entity association tree */
286     pldm_entity_association_tree* entityTree;
287 
288     /** @brief reference to Instance ID database object, used to obtain PLDM
289      * instance IDs
290      */
291     pldm::InstanceIdDb& instanceIdDb;
292 
293     /** @brief PLDM request handler */
294     pldm::requester::Handler<pldm::requester::Request>* handler;
295 
296     /** @brief sdeventplus event source */
297     std::unique_ptr<sdeventplus::source::Defer> pdrFetchEvent;
298     std::unique_ptr<sdeventplus::source::Defer> deferredFetchPDREvent;
299     std::unique_ptr<sdeventplus::source::Defer> deferredPDRRepoChgEvent;
300 
301     /** @brief list of PDR record handles pointing to host's PDRs */
302     PDRRecordHandles pdrRecordHandles;
303     /** @brief maps an entity type to parent pldm_entity from the BMC's entity
304      *  association tree
305      */
306 
307     /** @brief list of PDR record handles modified pointing to host PDRs */
308     PDRRecordHandles modifiedPDRRecordHandles;
309 
310     /** @brief D-Bus property changed signal match */
311     std::unique_ptr<sdbusplus::bus::match_t> hostOffMatch;
312 
313     /** @brief sensorMap is a lookup data structure that is build from the
314      *         hostPDR that speeds up the lookup of <TerminusID, SensorID> in
315      *         PlatformEventMessage command request.
316      */
317     HostStateSensorMap sensorMap;
318 
319     /** @brief whether response received from Host */
320     bool responseReceived;
321 
322     /** @brief variable that captures if the first entity association PDR
323      *         from host is merged into the BMC tree
324      */
325     bool mergedHostParents;
326 
327     /** @brief maps an object path to pldm_entity from the BMC's entity
328      *         association tree
329      */
330     pldm::utils::ObjectPathMaps objPathMap;
331 
332     /** @brief maps an entity name to map, maps to entity name to pldm_entity
333      */
334     pldm::utils::EntityAssociations entityAssociations;
335 
336     /** @brief the vector of FRU Record Data Format
337      */
338     std::vector<responder::pdr_utils::FruRecordDataFormat> fruRecordData;
339 
340     /** @OEM platform handler */
341     pldm::responder::oem_platform::Handler* oemPlatformHandler = nullptr;
342 
343     /** @brief entityID and entity name is only loaded once
344      */
345     pldm::utils::EntityMaps entityMaps;
346 
347     /** @OEM Utils handler */
348     pldm::responder::oem_utils::Handler* oemUtilsHandler;
349 };
350 
351 } // namespace pldm
352