blob: fbb206cde0a44256498c2a5f4b3a80638e572019 [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"
Tom Josephb4268602020-04-17 17:20:45 +05308#include "libpldmresponder/pdr_utils.hpp"
Deepak Kodihalli1521f6d2020-06-16 08:51:02 -05009#include "pldmd/dbus_impl_requester.hpp"
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050010
11#include <sdeventplus/event.hpp>
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050012#include <sdeventplus/source/event.hpp>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050013
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -050014#include <deque>
George Liu6492f522020-06-16 10:34:05 +080015#include <map>
16#include <memory>
17#include <vector>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050018
19using namespace pldm::dbus_api;
20
21namespace pldm
22{
23
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050024using EntityType = uint16_t;
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050025// vector which would hold the PDR record handle data returned by
26// pldmPDRRepositoryChgEvent event data
27using ChangeEntry = uint32_t;
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -050028using PDRRecordHandles = std::deque<ChangeEntry>;
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050029
Tom Josephb4268602020-04-17 17:20:45 +053030/** @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 */
36struct 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
53using HostStateSensorMap = std::map<SensorEntry, pdr::SensorInfo>;
54
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050055/** @class HostPDRHandler
56 * @brief This class can fetch and process PDRs from host firmware
57 * @details Provides an API to fetch PDRs from the host firmware. Upon
58 * receiving the PDRs, they are stored into the BMC's primary PDR repo.
59 * Adjustments are made to entity association PDRs received from the host,
60 * because they need to be assimilated into the BMC's entity association
61 * tree. A PLDM event containing the record handles of the updated entity
62 * association PDRs is sent to the host.
63 */
64class HostPDRHandler
65{
66 public:
67 HostPDRHandler() = delete;
68 HostPDRHandler(const HostPDRHandler&) = delete;
69 HostPDRHandler(HostPDRHandler&&) = delete;
70 HostPDRHandler& operator=(const HostPDRHandler&) = delete;
71 HostPDRHandler& operator=(HostPDRHandler&&) = delete;
72 ~HostPDRHandler() = default;
73
74 /** @brief Constructor
75 * @param[in] mctp_fd - fd of MCTP communications socket
76 * @param[in] mctp_eid - MCTP EID of host firmware
77 * @param[in] event - reference of main event loop of pldmd
78 * @param[in] repo - pointer to BMC's primary PDR repo
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050079 * @param[in] tree - pointer to BMC's entity association tree
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050080 * @param[in] requester - reference to Requester object
81 */
82 explicit HostPDRHandler(int mctp_fd, uint8_t mctp_eid,
83 sdeventplus::Event& event, pldm_pdr* repo,
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050084 pldm_entity_association_tree* entityTree,
85 Requester& requester);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050086
87 /** @brief fetch PDRs from host firmware. See @class.
88 * @param[in] recordHandles - list of record handles pointing to host's
89 * PDRs that need to be fetched.
90 */
Pavithra Barithayae8beb892020-04-14 23:24:25 -050091
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -050092 void fetchPDR(PDRRecordHandles&& recordHandles);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050093
Pavithra Barithayae8beb892020-04-14 23:24:25 -050094 /** @brief Send a PLDM event to host firmware containing a list of record
95 * handles of PDRs that the host firmware has to fetch.
96 * @param[in] pdrTypes - list of PDR types that need to be looked up in the
97 * BMC repo
98 * @param[in] eventDataFormat - format for PDRRepositoryChgEvent in DSP0248
99 */
100 void sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
101 uint8_t eventDataFormat);
102
Tom Josephb4268602020-04-17 17:20:45 +0530103 /** @brief Lookup host sensor info corresponding to requested SensorEntry
104 *
105 * @param[in] entry - TerminusID and SensorID
106 *
107 * @return SensorInfo corresponding to the input paramter SensorEntry
108 * throw std::out_of_range exception if not found
109 */
110 const pdr::SensorInfo& lookupSensorInfo(const SensorEntry& entry) const
111 {
112 return sensorMap.at(entry);
113 }
114
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500115 private:
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500116 /** @brief fetchPDR schedules work on the event loop, this method does the
117 * actual work. This is so that the PDR exchg with the host is async.
118 * @param[in] source - sdeventplus event source
119 */
120 void _fetchPDR(sdeventplus::source::EventBase& source);
121
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500122 /** @brief Merge host firmware's entity association PDRs into BMC's
123 * @details A merge operation involves adding a pldm_entity under the
124 * appropriate parent, and updating container ids.
125 * @param[in] pdr - entity association pdr
126 */
127 void mergeEntityAssociations(const std::vector<uint8_t>& pdr);
128
129 /** @brief Find parent of input entity type, from the entity association
130 * tree
131 * @param[in] type - PLDM entity type
132 * @param[out] parent - PLDM entity information of parent
133 * @return bool - true if parent found, false otherwise
134 */
135 bool getParent(EntityType type, pldm_entity& parent);
136
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500137 /** @brief fd of MCTP communications socket */
138 int mctp_fd;
139 /** @brief MCTP EID of host firmware */
140 uint8_t mctp_eid;
141 /** @brief reference of main event loop of pldmd, primarily used to schedule
142 * work.
143 */
144 sdeventplus::Event& event;
145 /** @brief pointer to BMC's primary PDR repo, host PDRs are added here */
146 pldm_pdr* repo;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500147 /** @brief Pointer to BMC's entity association tree */
148 pldm_entity_association_tree* entityTree;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500149 /** @brief reference to Requester object, primarily used to access API to
150 * obtain PLDM instance id.
151 */
152 Requester& requester;
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500153 /** @brief sdeventplus event source */
154 std::unique_ptr<sdeventplus::source::Defer> pdrFetchEvent;
155 /** @brief list of PDR record handles pointing to host's PDRs */
156 PDRRecordHandles pdrRecordHandles;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500157 /** @brief maps an entity type to parent pldm_entity from the BMC's entity
158 * association tree
159 */
160 std::map<EntityType, pldm_entity> parents;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500161 /** @brief D-Bus property changed signal match */
162 std::unique_ptr<sdbusplus::bus::match::match> hostOffMatch;
Tom Josephb4268602020-04-17 17:20:45 +0530163
164 /** @brief sensorMap is a lookup data structure that is build from the
165 * hostPDR that speeds up the lookup of <TerminusID, SensorID> in
166 * PlatformEventMessage command request.
167 */
168 HostStateSensorMap sensorMap;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500169};
170
171} // namespace pldm