blob: 3c998a27f42067ed8378dacdaa7b6c50ff47fd49 [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"
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050011
12#include <sdeventplus/event.hpp>
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050013#include <sdeventplus/source/event.hpp>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050014
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -050015#include <deque>
George Liu6492f522020-06-16 10:34:05 +080016#include <map>
17#include <memory>
18#include <vector>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050019
20using namespace pldm::dbus_api;
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060021using namespace pldm::responder::events;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050022
23namespace pldm
24{
25
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050026using EntityType = uint16_t;
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050027// vector which would hold the PDR record handle data returned by
28// pldmPDRRepositoryChgEvent event data
29using ChangeEntry = uint32_t;
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -050030using PDRRecordHandles = std::deque<ChangeEntry>;
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050031
Tom Josephb4268602020-04-17 17:20:45 +053032/** @struct SensorEntry
33 *
34 * SensorEntry is a unique key which maps a sensorEventType request in the
35 * PlatformEventMessage command to a host sensor PDR. This struct is a key
36 * in a std::map, so implemented operator==and operator<.
37 */
38struct SensorEntry
39{
40 pdr::TerminusID terminusID;
41 pdr::SensorID sensorID;
42
43 bool operator==(const SensorEntry& e) const
44 {
45 return ((terminusID == e.terminusID) && (sensorID == e.sensorID));
46 }
47
48 bool operator<(const SensorEntry& e) const
49 {
50 return ((terminusID < e.terminusID) ||
51 ((terminusID == e.terminusID) && (sensorID < e.sensorID)));
52 }
53};
54
55using HostStateSensorMap = std::map<SensorEntry, pdr::SensorInfo>;
Sampa Misra868c8792020-05-26 03:12:13 -050056using PDRList = std::vector<std::vector<uint8_t>>;
Tom Josephb4268602020-04-17 17:20:45 +053057
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050058/** @class HostPDRHandler
59 * @brief This class can fetch and process PDRs from host firmware
60 * @details Provides an API to fetch PDRs from the host firmware. Upon
61 * receiving the PDRs, they are stored into the BMC's primary PDR repo.
62 * Adjustments are made to entity association PDRs received from the host,
63 * because they need to be assimilated into the BMC's entity association
64 * tree. A PLDM event containing the record handles of the updated entity
65 * association PDRs is sent to the host.
66 */
67class HostPDRHandler
68{
69 public:
70 HostPDRHandler() = delete;
71 HostPDRHandler(const HostPDRHandler&) = delete;
72 HostPDRHandler(HostPDRHandler&&) = delete;
73 HostPDRHandler& operator=(const HostPDRHandler&) = delete;
74 HostPDRHandler& operator=(HostPDRHandler&&) = delete;
75 ~HostPDRHandler() = default;
76
Sampa Misra868c8792020-05-26 03:12:13 -050077 using TLPDRMap = std::map<pdr::TerminusHandle, pdr::TerminusID>;
78
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050079 /** @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
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060084 * @param[in] eventsJsonDir - directory path which has the config JSONs
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050085 * @param[in] tree - pointer to BMC's entity association tree
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050086 * @param[in] requester - reference to Requester object
87 */
88 explicit HostPDRHandler(int mctp_fd, uint8_t mctp_eid,
89 sdeventplus::Event& event, pldm_pdr* repo,
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060090 const std::string& eventsJsonsDir,
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050091 pldm_entity_association_tree* entityTree,
92 Requester& requester);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050093
94 /** @brief fetch PDRs from host firmware. See @class.
95 * @param[in] recordHandles - list of record handles pointing to host's
96 * PDRs that need to be fetched.
97 */
Pavithra Barithayae8beb892020-04-14 23:24:25 -050098
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -050099 void fetchPDR(PDRRecordHandles&& recordHandles);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500100
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500101 /** @brief Send a PLDM event to host firmware containing a list of record
102 * handles of PDRs that the host firmware has to fetch.
103 * @param[in] pdrTypes - list of PDR types that need to be looked up in the
104 * BMC repo
105 * @param[in] eventDataFormat - format for PDRRepositoryChgEvent in DSP0248
106 */
107 void sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
108 uint8_t eventDataFormat);
109
Tom Josephb4268602020-04-17 17:20:45 +0530110 /** @brief Lookup host sensor info corresponding to requested SensorEntry
111 *
112 * @param[in] entry - TerminusID and SensorID
113 *
114 * @return SensorInfo corresponding to the input paramter SensorEntry
115 * throw std::out_of_range exception if not found
116 */
117 const pdr::SensorInfo& lookupSensorInfo(const SensorEntry& entry) const
118 {
119 return sensorMap.at(entry);
120 }
121
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600122 /** @brief Handles state sensor event
123 *
124 * @param[in] entry - state sensor entry
125 * @param[in] state - event state
126 *
127 * @return PLDM completion code
128 */
129 int handleStateSensorEvent(const StateSensorEntry& entry,
130 pdr::EventState state);
131
Sampa Misra868c8792020-05-26 03:12:13 -0500132 /** @brief Parse state sensor PDRs and populate the sensorMap lookup data
133 * structure
134 *
135 * @param[in] stateSensorPDRs - host state sensor PDRs
136 * @param[in] tlpdrInfo - terminus locator PDRs info
137 *
138 */
139 void parseStateSensorPDRs(const PDRList& stateSensorPDRs,
140 const TLPDRMap& tlpdrInfo);
141
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500142 private:
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500143 /** @brief fetchPDR schedules work on the event loop, this method does the
144 * actual work. This is so that the PDR exchg with the host is async.
145 * @param[in] source - sdeventplus event source
146 */
147 void _fetchPDR(sdeventplus::source::EventBase& source);
148
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500149 /** @brief Merge host firmware's entity association PDRs into BMC's
150 * @details A merge operation involves adding a pldm_entity under the
151 * appropriate parent, and updating container ids.
152 * @param[in] pdr - entity association pdr
153 */
154 void mergeEntityAssociations(const std::vector<uint8_t>& pdr);
155
156 /** @brief Find parent of input entity type, from the entity association
157 * tree
158 * @param[in] type - PLDM entity type
159 * @param[out] parent - PLDM entity information of parent
160 * @return bool - true if parent found, false otherwise
161 */
162 bool getParent(EntityType type, pldm_entity& parent);
163
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500164 /** @brief fd of MCTP communications socket */
165 int mctp_fd;
166 /** @brief MCTP EID of host firmware */
167 uint8_t mctp_eid;
168 /** @brief reference of main event loop of pldmd, primarily used to schedule
169 * work.
170 */
171 sdeventplus::Event& event;
172 /** @brief pointer to BMC's primary PDR repo, host PDRs are added here */
173 pldm_pdr* repo;
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600174
175 StateSensorHandler stateSensorHandler;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500176 /** @brief Pointer to BMC's entity association tree */
177 pldm_entity_association_tree* entityTree;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500178 /** @brief reference to Requester object, primarily used to access API to
179 * obtain PLDM instance id.
180 */
181 Requester& requester;
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500182 /** @brief sdeventplus event source */
183 std::unique_ptr<sdeventplus::source::Defer> pdrFetchEvent;
184 /** @brief list of PDR record handles pointing to host's PDRs */
185 PDRRecordHandles pdrRecordHandles;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500186 /** @brief maps an entity type to parent pldm_entity from the BMC's entity
187 * association tree
188 */
189 std::map<EntityType, pldm_entity> parents;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500190 /** @brief D-Bus property changed signal match */
191 std::unique_ptr<sdbusplus::bus::match::match> hostOffMatch;
Tom Josephb4268602020-04-17 17:20:45 +0530192
193 /** @brief sensorMap is a lookup data structure that is build from the
194 * hostPDR that speeds up the lookup of <TerminusID, SensorID> in
195 * PlatformEventMessage command request.
196 */
197 HostStateSensorMap sensorMap;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500198};
199
200} // namespace pldm