blob: 147e6e70abbcfd13e15d95b83f6e3b1b76c9150a [file] [log] [blame]
Gilbert Chen6c7fed42022-02-22 15:40:17 +00001#pragma once
2
Gilbert Chen6c7fed42022-02-22 15:40:17 +00003#include "common/types.hpp"
Dung Caob6d39432024-06-05 03:46:47 +00004#include "dbus_impl_fru.hpp"
Thu Nguyen3c5486d2024-08-01 08:03:08 +00005#include "numeric_sensor.hpp"
Gilbert Chen6c7fed42022-02-22 15:40:17 +00006#include "requester/handler.hpp"
Thu Nguyen3c5486d2024-08-01 08:03:08 +00007#include "terminus.hpp"
Gilbert Chen6c7fed42022-02-22 15:40:17 +00008
Manojkiran Edafe252792025-03-13 19:24:19 +05309#include <libpldm/fru.h>
10#include <libpldm/platform.h>
11
Gilbert Chen6c7fed42022-02-22 15:40:17 +000012#include <sdbusplus/server/object.hpp>
13#include <sdeventplus/event.hpp>
Gilbert Chen6c7fed42022-02-22 15:40:17 +000014
15#include <algorithm>
16#include <bitset>
Gilbert Chende2a1322022-05-24 15:35:21 +010017#include <string>
18#include <tuple>
19#include <utility>
Gilbert Chen6c7fed42022-02-22 15:40:17 +000020#include <vector>
21
22namespace pldm
23{
24namespace platform_mc
25{
26
Chau Ly3419e182025-07-29 07:11:32 +000027using namespace pldm::pdr;
Thu Nguyenb8cf46b2024-06-15 02:44:35 +000028
Gilbert Chen6c7fed42022-02-22 15:40:17 +000029/**
30 * @brief Terminus
31 *
32 * Terminus class holds the TID, supported PLDM Type or PDRs which are needed by
33 * other manager class for sensor monitoring and control.
34 */
35class Terminus
36{
37 public:
Chaul Lyfdf61cc2025-01-22 07:55:45 +000038 Terminus(pldm_tid_t tid, uint64_t supportedPLDMTypes,
39 sdeventplus::Event& event);
Gilbert Chen6c7fed42022-02-22 15:40:17 +000040
41 /** @brief Check if the terminus supports the PLDM type message
42 *
43 * @param[in] type - PLDM Type
44 * @return support state - True if support, otherwise False
45 */
46 bool doesSupportType(uint8_t type);
47
48 /** @brief Check if the terminus supports the PLDM command message
49 *
50 * @param[in] type - PLDM Type
51 * @param[in] command - PLDM command
52 * @return support state - True if support, otherwise False
53 */
54 bool doesSupportCommand(uint8_t type, uint8_t command);
55
56 /** @brief Set the supported PLDM commands for terminus
57 *
58 * @param[in] cmds - bit mask of the supported PLDM commands
59 * @return success state - True if success, otherwise False
60 */
61 bool setSupportedCommands(const std::vector<uint8_t>& cmds)
62 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -040063 const size_t expectedSize =
64 PLDM_MAX_TYPES * (PLDM_MAX_CMDS_PER_TYPE / 8);
Gilbert Chen6c7fed42022-02-22 15:40:17 +000065 if (cmds.empty() || cmds.size() != expectedSize)
66 {
67 lg2::error(
68 "setSupportedCommands received invalid bit mask size. Expected: {EXPECTED}, Received: {RECEIVED}",
69 "EXPECTED", expectedSize, "RECEIVED", cmds.size());
70 return false;
71 }
72
73 /* Assign Vector supportedCmds by Vector cmds */
74 supportedCmds.resize(cmds.size());
75 std::copy(cmds.begin(), cmds.begin() + cmds.size(),
76 supportedCmds.begin());
77
78 return true;
79 }
Gilbert Chende2a1322022-05-24 15:35:21 +010080
Thu Nguyen6e615622024-06-22 02:10:34 +000081 /** @brief Set the PLDM supported type version for terminus
82 *
83 * @param[in] type - PLDM supported types
84 * @param[in] version - PLDM supported type version
85 * @return success state - True if success, otherwise False
86 */
87 inline bool setSupportedTypeVersions(const uint8_t type,
88 const ver32_t version)
89 {
90 if (type > PLDM_MAX_TYPES || type >= supportedTypeVersions.size())
91 {
92 return false;
93 }
94 supportedTypeVersions[type] = version;
95
96 return true;
97 }
98
Gilbert Chende2a1322022-05-24 15:35:21 +010099 /** @brief Parse the PDRs stored in the member variable, pdrs.
100 */
101 void parseTerminusPDRs();
102
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000103 /** @brief The getter to return terminus's TID */
104 pldm_tid_t getTid()
105 {
106 return tid;
107 }
108
Kevin Tung502bd602025-08-01 10:05:47 +0800109 /** @brief The setter to set terminus's mctp medium */
110 void setTerminusName(const EntityName& tName)
111 {
112 terminusName = tName;
113 }
114
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000115 /** @brief The getter to get terminus's mctp medium */
Thu Nguyen9fc79122024-09-10 10:15:01 +0000116 std::optional<std::string_view> getTerminusName()
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000117 {
Thu Nguyen9fc79122024-09-10 10:15:01 +0000118 if (terminusName.empty())
119 {
120 return std::nullopt;
121 }
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000122 return terminusName;
123 }
124
Dung Caob6d39432024-06-05 03:46:47 +0000125 /** @brief Parse record data from FRU table
126 *
127 * @param[in] fruData - pointer to FRU record table
128 * @param[in] fruLen - FRU table length
129 */
130 void updateInventoryWithFru(const uint8_t* fruData, const size_t fruLen);
131
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000132 /** @brief A list of PDRs fetched from Terminus */
133 std::vector<std::vector<uint8_t>> pdrs{};
134
135 /** @brief A flag to indicate if terminus has been initialized */
136 bool initialized = false;
137
Gilbert Chen77e6fe72024-08-06 09:23:30 +0000138 /** @brief maximum message buffer size the terminus can send and receive */
139 uint16_t maxBufferSize;
140
Thu Nguyen51d66b52024-08-06 09:15:55 +0000141 /** @brief This value indicates the event messaging styles supported by the
142 * terminus
143 */
144 bitfield8_t synchronyConfigurationSupported;
145
Thu Nguyen3c5486d2024-08-01 08:03:08 +0000146 /** @brief A list of numericSensors */
147 std::vector<std::shared_ptr<NumericSensor>> numericSensors{};
148
Dung Caof48015b2023-11-21 04:38:29 +0000149 /** @brief The flag indicates that the terminus FIFO contains a large
150 * message that will require a multipart transfer via the
151 * PollForPlatformEvent command
152 */
153 bool pollEvent;
154
155 /** @brief The sensor id is used in pollForPlatformMessage command */
156 uint16_t pollEventId;
157
158 /** @brief The dataTransferHandle from `pldmMessagePollEvent` event and will
159 * be used as `dataTransferHandle` for pollForPlatformMessage
160 * command.
161 */
162 uint32_t pollDataTransferHandle;
163
Gilbert Chende2a1322022-05-24 15:35:21 +0100164 /** @brief Get Sensor Auxiliary Names by sensorID
165 *
166 * @param[in] id - sensor ID
167 * @return sensor auxiliary names
168 */
Chau Ly3419e182025-07-29 07:11:32 +0000169 std::shared_ptr<SensorAuxiliaryNames> getSensorAuxiliaryNames(SensorID id);
Gilbert Chende2a1322022-05-24 15:35:21 +0100170
Gilbert Chen77e6fe72024-08-06 09:23:30 +0000171 /** @brief Get Numeric Sensor Object by sensorID
172 *
173 * @param[in] id - sensor ID
174 *
175 * @return sensor object
176 */
Chau Ly3419e182025-07-29 07:11:32 +0000177 std::shared_ptr<NumericSensor> getSensorObject(SensorID id);
Gilbert Chen77e6fe72024-08-06 09:23:30 +0000178
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000179 private:
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000180 /** @brief Find the Terminus Name from the Entity Auxiliary name list
181 * The Entity Auxiliary name list is entityAuxiliaryNamesTbl.
182 * @return terminus name in string option
183 */
184 std::optional<std::string_view> findTerminusName();
185
Thu Nguyen3c5486d2024-08-01 08:03:08 +0000186 /** @brief Construct the NumericSensor sensor class for the PLDM sensor.
187 * The NumericSensor class will handle create D-Bus object path,
188 * provide the APIs to update sensor value, threshold...
189 *
190 * @param[in] pdr - the numeric sensor PDR info
191 */
192 void addNumericSensor(
193 const std::shared_ptr<pldm_numeric_sensor_value_pdr> pdr);
194
Gilbert Chende2a1322022-05-24 15:35:21 +0100195 /** @brief Parse the numeric sensor PDRs
196 *
197 * @param[in] pdrData - the response PDRs from GetPDR command
198 * @return pointer to numeric sensor info struct
199 */
Patrick Williams366507c2025-02-03 14:28:01 -0500200 std::shared_ptr<pldm_numeric_sensor_value_pdr> parseNumericSensorPDR(
201 const std::vector<uint8_t>& pdrData);
Gilbert Chende2a1322022-05-24 15:35:21 +0100202
203 /** @brief Parse the sensor Auxiliary name PDRs
204 *
205 * @param[in] pdrData - the response PDRs from GetPDR command
206 * @return pointer to sensor Auxiliary name info struct
207 */
Patrick Williams366507c2025-02-03 14:28:01 -0500208 std::shared_ptr<SensorAuxiliaryNames> parseSensorAuxiliaryNamesPDR(
209 const std::vector<uint8_t>& pdrData);
Gilbert Chende2a1322022-05-24 15:35:21 +0100210
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000211 /** @brief Parse the Entity Auxiliary name PDRs
212 *
213 * @param[in] pdrData - the response PDRs from GetPDR command
214 * @return pointer to Entity Auxiliary name info struct
215 */
Patrick Williams366507c2025-02-03 14:28:01 -0500216 std::shared_ptr<EntityAuxiliaryNames> parseEntityAuxiliaryNamesPDR(
217 const std::vector<uint8_t>& pdrData);
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000218
Thu Nguyen3c5486d2024-08-01 08:03:08 +0000219 /** @brief Construct the NumericSensor sensor class for the compact numeric
220 * PLDM sensor.
221 *
222 * @param[in] pdr - the compact numeric sensor PDR info
223 */
224 void addCompactNumericSensor(
225 const std::shared_ptr<pldm_compact_numeric_sensor_pdr> pdr);
226
Gilbert Chende2a1322022-05-24 15:35:21 +0100227 /** @brief Parse the compact numeric sensor PDRs
228 *
229 * @param[in] pdrData - the response PDRs from GetPDR command
230 * @return pointer to compact numeric sensor info struct
231 */
232 std::shared_ptr<pldm_compact_numeric_sensor_pdr>
233 parseCompactNumericSensorPDR(const std::vector<uint8_t>& pdrData);
234
235 /** @brief Parse the sensor Auxiliary name from compact numeric sensor PDRs
236 *
237 * @param[in] pdrData - the response PDRs from GetPDR command
238 * @return pointer to sensor Auxiliary name info struct
239 */
Patrick Williams366507c2025-02-03 14:28:01 -0500240 std::shared_ptr<SensorAuxiliaryNames> parseCompactNumericSensorNames(
241 const std::vector<uint8_t>& pdrData);
Gilbert Chende2a1322022-05-24 15:35:21 +0100242
Thu Nguyen3c5486d2024-08-01 08:03:08 +0000243 /** @brief Create the terminus inventory path to
244 * /xyz/openbmc_project/inventory/Item/Board/.
245 *
246 * @param[in] tName - the terminus name
247 * @return true/false: True if there is no error in creating inventory path
248 *
249 */
250 bool createInventoryPath(std::string tName);
251
Chau Ly38a09d22025-03-05 05:47:43 +0000252 /** @brief Get sensor names from Sensor Auxiliary Names PDRs
253 *
254 * @param[in] sensorId - Sensor ID
255 * @param[in] isEffecter - This is an effecter, not a sensor
256 * @return vector of sensor name strings
257 *
258 */
Chau Ly3419e182025-07-29 07:11:32 +0000259 std::vector<std::string> getSensorNames(const SensorID& sensorId);
Chau Ly38a09d22025-03-05 05:47:43 +0000260
Chaul Lyfdf61cc2025-01-22 07:55:45 +0000261 /** @brief Add the next sensor PDR to this terminus, iterated by
262 * sensorPdrIt.
263 */
264 void addNextSensorFromPDRs();
265
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000266 /* @brief The terminus's TID */
267 pldm_tid_t tid;
268
269 /* @brief The supported PLDM command types of the terminus */
270 std::bitset<64> supportedTypes;
271
272 /** @brief Store supported PLDM commands of a terminus
273 * Maximum number of PLDM Type is PLDM_MAX_TYPES
274 * Maximum number of PLDM command for each type is
275 * PLDM_MAX_CMDS_PER_TYPE.
276 * Each uint8_t can store the supported state of 8 PLDM commands.
277 * Size of supportedCmds will be
278 * PLDM_MAX_TYPES * (PLDM_MAX_CMDS_PER_TYPE / 8).
279 */
280 std::vector<uint8_t> supportedCmds;
Gilbert Chende2a1322022-05-24 15:35:21 +0100281
Thu Nguyen6e615622024-06-22 02:10:34 +0000282 /* @brief The PLDM supported type version */
283 std::map<uint8_t, ver32_t> supportedTypeVersions;
284
Gilbert Chende2a1322022-05-24 15:35:21 +0100285 /* @brief Sensor Auxiliary Name list */
286 std::vector<std::shared_ptr<SensorAuxiliaryNames>>
287 sensorAuxiliaryNamesTbl{};
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000288
289 /* @brief Entity Auxiliary Name list */
290 std::vector<std::shared_ptr<EntityAuxiliaryNames>>
291 entityAuxiliaryNamesTbl{};
292
293 /** @brief Terminus name */
294 EntityName terminusName{};
Dung Caob6d39432024-06-05 03:46:47 +0000295 /* @brief The pointer of inventory D-Bus interface for the terminus */
296 std::unique_ptr<pldm::dbus_api::PldmEntityReq> inventoryItemBoardInft =
297 nullptr;
Thu Nguyen3c5486d2024-08-01 08:03:08 +0000298
299 /* @brief Inventory D-Bus object path of the terminus */
300 std::string inventoryPath;
Chaul Lyfdf61cc2025-01-22 07:55:45 +0000301
302 /** @brief reference of main event loop of pldmd, primarily used to schedule
303 * work
304 */
305 sdeventplus::Event& event;
306
307 /** @brief The event source to defer sensor creation tasks to event loop*/
308 std::unique_ptr<sdeventplus::source::Defer> sensorCreationEvent;
309
310 /** @brief Numeric Sensor PDR list */
311 std::vector<std::shared_ptr<pldm_numeric_sensor_value_pdr>>
312 numericSensorPdrs{};
313
314 /** @brief Compact Numeric Sensor PDR list */
315 std::vector<std::shared_ptr<pldm_compact_numeric_sensor_pdr>>
316 compactNumericSensorPdrs{};
317
318 /** @brief Iteration to loop through sensor PDRs when adding sensors */
Chau Ly3419e182025-07-29 07:11:32 +0000319 SensorID sensorPdrIt = 0;
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000320};
321} // namespace platform_mc
322} // namespace pldm