1 #pragma once 2 3 #include "libpldm/base.h" 4 #include "libpldm/platform.h" 5 6 #include "common/types.hpp" 7 #include "common/utils.hpp" 8 #include "libpldmresponder/event_parser.hpp" 9 #include "libpldmresponder/pdr_utils.hpp" 10 #include "pldmd/dbus_impl_requester.hpp" 11 #include "requester/handler.hpp" 12 13 #include <sdeventplus/event.hpp> 14 #include <sdeventplus/source/event.hpp> 15 16 #include <deque> 17 #include <map> 18 #include <memory> 19 #include <vector> 20 21 using namespace pldm::dbus_api; 22 using namespace pldm::responder::events; 23 24 namespace pldm 25 { 26 27 using EntityType = uint16_t; 28 // vector which would hold the PDR record handle data returned by 29 // pldmPDRRepositoryChgEvent event data 30 using ChangeEntry = uint32_t; 31 using PDRRecordHandles = std::deque<ChangeEntry>; 32 33 /** @struct SensorEntry 34 * 35 * SensorEntry is a unique key which maps a sensorEventType request in the 36 * PlatformEventMessage command to a host sensor PDR. This struct is a key 37 * in a std::map, so implemented operator==and operator<. 38 */ 39 struct SensorEntry 40 { 41 pdr::TerminusID terminusID; 42 pdr::SensorID sensorID; 43 44 bool operator==(const SensorEntry& e) const 45 { 46 return ((terminusID == e.terminusID) && (sensorID == e.sensorID)); 47 } 48 49 bool operator<(const SensorEntry& e) const 50 { 51 return ((terminusID < e.terminusID) || 52 ((terminusID == e.terminusID) && (sensorID < e.sensorID))); 53 } 54 }; 55 56 using HostStateSensorMap = std::map<SensorEntry, pdr::SensorInfo>; 57 using PDRList = std::vector<std::vector<uint8_t>>; 58 59 /** @class HostPDRHandler 60 * @brief This class can fetch and process PDRs from host firmware 61 * @details Provides an API to fetch PDRs from the host firmware. Upon 62 * receiving the PDRs, they are stored into the BMC's primary PDR repo. 63 * Adjustments are made to entity association PDRs received from the host, 64 * because they need to be assimilated into the BMC's entity association 65 * tree. A PLDM event containing the record handles of the updated entity 66 * association PDRs is sent to the host. 67 */ 68 class HostPDRHandler 69 { 70 public: 71 HostPDRHandler() = delete; 72 HostPDRHandler(const HostPDRHandler&) = delete; 73 HostPDRHandler(HostPDRHandler&&) = delete; 74 HostPDRHandler& operator=(const HostPDRHandler&) = delete; 75 HostPDRHandler& operator=(HostPDRHandler&&) = delete; 76 ~HostPDRHandler() = default; 77 78 using TLPDRMap = std::map<pdr::TerminusHandle, pdr::TerminusID>; 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] requester - reference to Requester object 89 * @param[in] handler - PLDM request handler 90 */ 91 explicit HostPDRHandler( 92 int mctp_fd, uint8_t mctp_eid, sdeventplus::Event& event, 93 pldm_pdr* repo, const std::string& eventsJsonsDir, 94 pldm_entity_association_tree* entityTree, 95 pldm_entity_association_tree* bmcEntityTree, Requester& requester, 96 pldm::requester::Handler<pldm::requester::Request>* handler, 97 bool verbose = false); 98 99 /** @brief fetch PDRs from host firmware. See @class. 100 * @param[in] recordHandles - list of record handles pointing to host's 101 * PDRs that need to be fetched. 102 */ 103 104 void fetchPDR(PDRRecordHandles&& recordHandles); 105 106 /** @brief Send a PLDM event to host firmware containing a list of record 107 * handles of PDRs that the host firmware has to fetch. 108 * @param[in] pdrTypes - list of PDR types that need to be looked up in the 109 * BMC repo 110 * @param[in] eventDataFormat - format for PDRRepositoryChgEvent in DSP0248 111 */ 112 void sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes, 113 uint8_t eventDataFormat); 114 115 /** @brief Lookup host sensor info corresponding to requested SensorEntry 116 * 117 * @param[in] entry - TerminusID and SensorID 118 * 119 * @return SensorInfo corresponding to the input paramter SensorEntry 120 * throw std::out_of_range exception if not found 121 */ 122 const pdr::SensorInfo& lookupSensorInfo(const SensorEntry& entry) const 123 { 124 return sensorMap.at(entry); 125 } 126 127 /** @brief Handles state sensor event 128 * 129 * @param[in] entry - state sensor entry 130 * @param[in] state - event state 131 * 132 * @return PLDM completion code 133 */ 134 int handleStateSensorEvent(const StateSensorEntry& entry, 135 pdr::EventState state); 136 137 /** @brief Parse state sensor PDRs and populate the sensorMap lookup data 138 * structure 139 * 140 * @param[in] stateSensorPDRs - host state sensor PDRs 141 * @param[in] tlpdrInfo - terminus locator PDRs info 142 * 143 */ 144 void parseStateSensorPDRs(const PDRList& stateSensorPDRs, 145 const TLPDRMap& tlpdrInfo); 146 147 private: 148 /** @brief deferred function to fetch PDR from Host, scheduled to work on 149 * the event loop. The PDR exchg with the host is async. 150 * @param[in] source - sdeventplus event source 151 */ 152 void _fetchPDR(sdeventplus::source::EventBase& source); 153 154 /** @brief this function sends a GetPDR request to Host firmware. 155 * And processes the PDRs based on type 156 * 157 * @param[in] - nextRecordHandle - the next record handle to ask for 158 */ 159 void getHostPDR(uint32_t nextRecordHandle = 0); 160 161 /** @brief Merge host firmware's entity association PDRs into BMC's 162 * @details A merge operation involves adding a pldm_entity under the 163 * appropriate parent, and updating container ids. 164 * @param[in] pdr - entity association pdr 165 */ 166 void mergeEntityAssociations(const std::vector<uint8_t>& pdr); 167 168 /** @brief Find parent of input entity type, from the entity association 169 * tree 170 * @param[in] type - PLDM entity type 171 * @param[out] parent - PLDM entity information of parent 172 * @return bool - true if parent found, false otherwise 173 */ 174 bool getParent(EntityType type, pldm_entity& parent); 175 176 /** @brief process the Host's PDR and add to BMC's PDR repo 177 * @param[in] eid - MCTP id of Host 178 * @param[in] response - response from Host for GetPDR 179 * @param[in] respMsgLen - response message length 180 */ 181 void processHostPDRs(mctp_eid_t eid, const pldm_msg* response, 182 size_t respMsgLen); 183 184 /** @brief send PDR Repo change after merging Host's PDR to BMC PDR repo 185 * @param[in] source - sdeventplus event source 186 */ 187 void _processPDRRepoChgEvent(sdeventplus::source::EventBase& source); 188 189 /** @brief fetch the next PDR based on the record handle sent by Host 190 * @param[in] nextRecordHandle - next record handle 191 * @param[in] source - sdeventplus event source 192 */ 193 void _processFetchPDREvent(uint32_t nextRecordHandle, 194 sdeventplus::source::EventBase& source); 195 196 /** @brief fd of MCTP communications socket */ 197 int mctp_fd; 198 /** @brief MCTP EID of host firmware */ 199 uint8_t mctp_eid; 200 /** @brief reference of main event loop of pldmd, primarily used to schedule 201 * work. 202 */ 203 sdeventplus::Event& event; 204 /** @brief pointer to BMC's primary PDR repo, host PDRs are added here */ 205 pldm_pdr* repo; 206 207 StateSensorHandler stateSensorHandler; 208 /** @brief Pointer to BMC's and Host's entity association tree */ 209 pldm_entity_association_tree* entityTree; 210 211 /** @brief Pointer to BMC's entity association tree */ 212 pldm_entity_association_tree* bmcEntityTree; 213 214 /** @brief reference to Requester object, primarily used to access API to 215 * obtain PLDM instance id. 216 */ 217 Requester& requester; 218 219 /** @brief PLDM request handler */ 220 pldm::requester::Handler<pldm::requester::Request>* handler; 221 222 /** @brief sdeventplus event source */ 223 std::unique_ptr<sdeventplus::source::Defer> pdrFetchEvent; 224 std::unique_ptr<sdeventplus::source::Defer> deferredFetchPDREvent; 225 std::unique_ptr<sdeventplus::source::Defer> deferredPDRRepoChgEvent; 226 227 /** @brief list of PDR record handles pointing to host's PDRs */ 228 PDRRecordHandles pdrRecordHandles; 229 /** @brief maps an entity type to parent pldm_entity from the BMC's entity 230 * association tree 231 */ 232 std::map<EntityType, pldm_entity> parents; 233 /** @brief D-Bus property changed signal match */ 234 std::unique_ptr<sdbusplus::bus::match::match> hostOffMatch; 235 236 /** @brief sensorMap is a lookup data structure that is build from the 237 * hostPDR that speeds up the lookup of <TerminusID, SensorID> in 238 * PlatformEventMessage command request. 239 */ 240 HostStateSensorMap sensorMap; 241 bool verbose; 242 }; 243 244 } // namespace pldm 245