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