blob: 83abe5f49ed46e79863fdddd50b8ab503e0cf099 [file] [log] [blame]
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05001#pragma once
2
3#include "dbus_impl_requester.hpp"
Tom Josephb4268602020-04-17 17:20:45 +05304#include "libpldmresponder/pdr_utils.hpp"
5#include "types.hpp"
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05006#include "utils.hpp"
7
Deepak Kodihalli87514cc2020-04-16 09:08:38 -05008#include <map>
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -05009#include <memory>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050010#include <sdeventplus/event.hpp>
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050011#include <sdeventplus/source/event.hpp>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050012#include <vector>
13
14#include "libpldm/base.h"
15#include "libpldm/platform.h"
16
17using namespace pldm::dbus_api;
18
19namespace pldm
20{
21
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050022using EntityType = uint16_t;
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050023// vector which would hold the PDR record handle data returned by
24// pldmPDRRepositoryChgEvent event data
25using ChangeEntry = uint32_t;
26using PDRRecordHandles = std::vector<ChangeEntry>;
27
Tom Josephb4268602020-04-17 17:20:45 +053028/** @struct SensorEntry
29 *
30 * SensorEntry is a unique key which maps a sensorEventType request in the
31 * PlatformEventMessage command to a host sensor PDR. This struct is a key
32 * in a std::map, so implemented operator==and operator<.
33 */
34struct SensorEntry
35{
36 pdr::TerminusID terminusID;
37 pdr::SensorID sensorID;
38
39 bool operator==(const SensorEntry& e) const
40 {
41 return ((terminusID == e.terminusID) && (sensorID == e.sensorID));
42 }
43
44 bool operator<(const SensorEntry& e) const
45 {
46 return ((terminusID < e.terminusID) ||
47 ((terminusID == e.terminusID) && (sensorID < e.sensorID)));
48 }
49};
50
51using HostStateSensorMap = std::map<SensorEntry, pdr::SensorInfo>;
52
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050053/** @class HostPDRHandler
54 * @brief This class can fetch and process PDRs from host firmware
55 * @details Provides an API to fetch PDRs from the host firmware. Upon
56 * receiving the PDRs, they are stored into the BMC's primary PDR repo.
57 * Adjustments are made to entity association PDRs received from the host,
58 * because they need to be assimilated into the BMC's entity association
59 * tree. A PLDM event containing the record handles of the updated entity
60 * association PDRs is sent to the host.
61 */
62class HostPDRHandler
63{
64 public:
65 HostPDRHandler() = delete;
66 HostPDRHandler(const HostPDRHandler&) = delete;
67 HostPDRHandler(HostPDRHandler&&) = delete;
68 HostPDRHandler& operator=(const HostPDRHandler&) = delete;
69 HostPDRHandler& operator=(HostPDRHandler&&) = delete;
70 ~HostPDRHandler() = default;
71
72 /** @brief Constructor
73 * @param[in] mctp_fd - fd of MCTP communications socket
74 * @param[in] mctp_eid - MCTP EID of host firmware
75 * @param[in] event - reference of main event loop of pldmd
76 * @param[in] repo - pointer to BMC's primary PDR repo
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050077 * @param[in] tree - pointer to BMC's entity association tree
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050078 * @param[in] requester - reference to Requester object
79 */
80 explicit HostPDRHandler(int mctp_fd, uint8_t mctp_eid,
81 sdeventplus::Event& event, pldm_pdr* repo,
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050082 pldm_entity_association_tree* entityTree,
83 Requester& requester);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050084
85 /** @brief fetch PDRs from host firmware. See @class.
86 * @param[in] recordHandles - list of record handles pointing to host's
87 * PDRs that need to be fetched.
88 */
Pavithra Barithayae8beb892020-04-14 23:24:25 -050089
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050090 void fetchPDR(std::vector<uint32_t>&& recordHandles);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050091
Pavithra Barithayae8beb892020-04-14 23:24:25 -050092 /** @brief Send a PLDM event to host firmware containing a list of record
93 * handles of PDRs that the host firmware has to fetch.
94 * @param[in] pdrTypes - list of PDR types that need to be looked up in the
95 * BMC repo
96 * @param[in] eventDataFormat - format for PDRRepositoryChgEvent in DSP0248
97 */
98 void sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
99 uint8_t eventDataFormat);
100
Tom Josephb4268602020-04-17 17:20:45 +0530101 /** @brief Lookup host sensor info corresponding to requested SensorEntry
102 *
103 * @param[in] entry - TerminusID and SensorID
104 *
105 * @return SensorInfo corresponding to the input paramter SensorEntry
106 * throw std::out_of_range exception if not found
107 */
108 const pdr::SensorInfo& lookupSensorInfo(const SensorEntry& entry) const
109 {
110 return sensorMap.at(entry);
111 }
112
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500113 private:
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500114 /** @brief fetchPDR schedules work on the event loop, this method does the
115 * actual work. This is so that the PDR exchg with the host is async.
116 * @param[in] source - sdeventplus event source
117 */
118 void _fetchPDR(sdeventplus::source::EventBase& source);
119
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500120 /** @brief Merge host firmware's entity association PDRs into BMC's
121 * @details A merge operation involves adding a pldm_entity under the
122 * appropriate parent, and updating container ids.
123 * @param[in] pdr - entity association pdr
124 */
125 void mergeEntityAssociations(const std::vector<uint8_t>& pdr);
126
127 /** @brief Find parent of input entity type, from the entity association
128 * tree
129 * @param[in] type - PLDM entity type
130 * @param[out] parent - PLDM entity information of parent
131 * @return bool - true if parent found, false otherwise
132 */
133 bool getParent(EntityType type, pldm_entity& parent);
134
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500135 /** @brief fd of MCTP communications socket */
136 int mctp_fd;
137 /** @brief MCTP EID of host firmware */
138 uint8_t mctp_eid;
139 /** @brief reference of main event loop of pldmd, primarily used to schedule
140 * work.
141 */
142 sdeventplus::Event& event;
143 /** @brief pointer to BMC's primary PDR repo, host PDRs are added here */
144 pldm_pdr* repo;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500145 /** @brief Pointer to BMC's entity association tree */
146 pldm_entity_association_tree* entityTree;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500147 /** @brief reference to Requester object, primarily used to access API to
148 * obtain PLDM instance id.
149 */
150 Requester& requester;
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500151 /** @brief sdeventplus event source */
152 std::unique_ptr<sdeventplus::source::Defer> pdrFetchEvent;
153 /** @brief list of PDR record handles pointing to host's PDRs */
154 PDRRecordHandles pdrRecordHandles;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500155 /** @brief maps an entity type to parent pldm_entity from the BMC's entity
156 * association tree
157 */
158 std::map<EntityType, pldm_entity> parents;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500159 /** @brief D-Bus property changed signal match */
160 std::unique_ptr<sdbusplus::bus::match::match> hostOffMatch;
Tom Josephb4268602020-04-17 17:20:45 +0530161
162 /** @brief sensorMap is a lookup data structure that is build from the
163 * hostPDR that speeds up the lookup of <TerminusID, SensorID> in
164 * PlatformEventMessage command request.
165 */
166 HostStateSensorMap sensorMap;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500167};
168
169} // namespace pldm