blob: 5ca8f915d6aa1c9553f3eaec3557e76ae4bbaa82 [file] [log] [blame]
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05001#pragma once
2
George Liu6492f522020-06-16 10:34:05 +08003#include "libpldm/base.h"
4#include "libpldm/platform.h"
5
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05006#include "common/types.hpp"
7#include "common/utils.hpp"
Pavithra Barithaya3aec9972020-12-14 01:55:44 -06008#include "libpldmresponder/event_parser.hpp"
Tom Josephb4268602020-04-17 17:20:45 +05309#include "libpldmresponder/pdr_utils.hpp"
Deepak Kodihalli1521f6d2020-06-16 08:51:02 -050010#include "pldmd/dbus_impl_requester.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -070011#include "requester/handler.hpp"
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050012
13#include <sdeventplus/event.hpp>
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050014#include <sdeventplus/source/event.hpp>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050015
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -050016#include <deque>
George Liu6492f522020-06-16 10:34:05 +080017#include <map>
18#include <memory>
19#include <vector>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050020
21using namespace pldm::dbus_api;
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060022using namespace pldm::responder::events;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050023
24namespace pldm
25{
26
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050027using EntityType = uint16_t;
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050028// vector which would hold the PDR record handle data returned by
29// pldmPDRRepositoryChgEvent event data
30using ChangeEntry = uint32_t;
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -050031using PDRRecordHandles = std::deque<ChangeEntry>;
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050032
Tom Josephb4268602020-04-17 17:20:45 +053033/** @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 */
39struct 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
56using HostStateSensorMap = std::map<SensorEntry, pdr::SensorInfo>;
Sampa Misra868c8792020-05-26 03:12:13 -050057using PDRList = std::vector<std::vector<uint8_t>>;
Tom Josephb4268602020-04-17 17:20:45 +053058
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050059/** @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 */
68class 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
Sampa Misra868c8792020-05-26 03:12:13 -050078 using TLPDRMap = std::map<pdr::TerminusHandle, pdr::TerminusID>;
79
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050080 /** @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
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060085 * @param[in] eventsJsonDir - directory path which has the config JSONs
Tom Joseph74f27c72021-05-16 07:58:53 -070086 * @param[in] entityTree - Pointer to BMC and Host entity association tree
87 * @param[in] bmcEntityTree - pointer to BMC's entity association tree
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050088 * @param[in] requester - reference to Requester object
Tom Joseph74f27c72021-05-16 07:58:53 -070089 * @param[in] handler - PLDM request handler
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050090 */
Tom Joseph74f27c72021-05-16 07:58:53 -070091 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);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050098
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 */
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500103
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500104 void fetchPDR(PDRRecordHandles&& recordHandles);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500105
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500106 /** @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
Tom Josephb4268602020-04-17 17:20:45 +0530115 /** @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
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600127 /** @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
Sampa Misra868c8792020-05-26 03:12:13 -0500137 /** @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
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500147 private:
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500148 /** @brief fetchPDR schedules work on the event loop, this method does the
149 * actual work. This is so that the PDR exchg with the host is async.
150 * @param[in] source - sdeventplus event source
151 */
152 void _fetchPDR(sdeventplus::source::EventBase& source);
153
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500154 /** @brief Merge host firmware's entity association PDRs into BMC's
155 * @details A merge operation involves adding a pldm_entity under the
156 * appropriate parent, and updating container ids.
157 * @param[in] pdr - entity association pdr
158 */
159 void mergeEntityAssociations(const std::vector<uint8_t>& pdr);
160
161 /** @brief Find parent of input entity type, from the entity association
162 * tree
163 * @param[in] type - PLDM entity type
164 * @param[out] parent - PLDM entity information of parent
165 * @return bool - true if parent found, false otherwise
166 */
167 bool getParent(EntityType type, pldm_entity& parent);
168
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500169 /** @brief fd of MCTP communications socket */
170 int mctp_fd;
171 /** @brief MCTP EID of host firmware */
172 uint8_t mctp_eid;
173 /** @brief reference of main event loop of pldmd, primarily used to schedule
174 * work.
175 */
176 sdeventplus::Event& event;
177 /** @brief pointer to BMC's primary PDR repo, host PDRs are added here */
178 pldm_pdr* repo;
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600179
180 StateSensorHandler stateSensorHandler;
Sampa Misrac073a202021-05-08 10:56:05 -0500181 /** @brief Pointer to BMC's and Host's entity association tree */
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500182 pldm_entity_association_tree* entityTree;
Sampa Misrac073a202021-05-08 10:56:05 -0500183
184 /** @brief Pointer to BMC's entity association tree */
185 pldm_entity_association_tree* bmcEntityTree;
186
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500187 /** @brief reference to Requester object, primarily used to access API to
188 * obtain PLDM instance id.
189 */
190 Requester& requester;
Tom Joseph74f27c72021-05-16 07:58:53 -0700191
192 /** @brief PLDM request handler */
193 pldm::requester::Handler<pldm::requester::Request>& handler;
194
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500195 /** @brief sdeventplus event source */
196 std::unique_ptr<sdeventplus::source::Defer> pdrFetchEvent;
197 /** @brief list of PDR record handles pointing to host's PDRs */
198 PDRRecordHandles pdrRecordHandles;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500199 /** @brief maps an entity type to parent pldm_entity from the BMC's entity
200 * association tree
201 */
202 std::map<EntityType, pldm_entity> parents;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500203 /** @brief D-Bus property changed signal match */
204 std::unique_ptr<sdbusplus::bus::match::match> hostOffMatch;
Tom Josephb4268602020-04-17 17:20:45 +0530205
206 /** @brief sensorMap is a lookup data structure that is build from the
207 * hostPDR that speeds up the lookup of <TerminusID, SensorID> in
208 * PlatformEventMessage command request.
209 */
210 HostStateSensorMap sensorMap;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600211 bool verbose;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500212};
213
214} // namespace pldm