blob: 464897fc2479963e82b9377b9b55345454cbdbc2 [file] [log] [blame]
Gilbert Chen6c7fed42022-02-22 15:40:17 +00001#pragma once
2
3#include "libpldm/platform.h"
4
5#include "common/types.hpp"
Thu Nguyen3c5486d2024-08-01 08:03:08 +00006#include "numeric_sensor.hpp"
Gilbert Chen6c7fed42022-02-22 15:40:17 +00007#include "requester/handler.hpp"
Thu Nguyen3c5486d2024-08-01 08:03:08 +00008#include "terminus.hpp"
Gilbert Chen6c7fed42022-02-22 15:40:17 +00009
10#include <sdbusplus/server/object.hpp>
11#include <sdeventplus/event.hpp>
12#include <xyz/openbmc_project/Inventory/Item/Board/server.hpp>
13
14#include <algorithm>
15#include <bitset>
Gilbert Chende2a1322022-05-24 15:35:21 +010016#include <string>
17#include <tuple>
18#include <utility>
Gilbert Chen6c7fed42022-02-22 15:40:17 +000019#include <vector>
20
21namespace pldm
22{
23namespace platform_mc
24{
25
Thu Nguyenb8cf46b2024-06-15 02:44:35 +000026using ContainerID = uint16_t;
27using EntityInstanceNumber = uint16_t;
28using EntityName = std::string;
29using EntityType = uint16_t;
Gilbert Chende2a1322022-05-24 15:35:21 +010030using SensorId = uint16_t;
31using SensorCnt = uint8_t;
32using NameLanguageTag = std::string;
33using SensorName = std::string;
34using SensorAuxiliaryNames = std::tuple<
35 SensorId, SensorCnt,
36 std::vector<std::vector<std::pair<NameLanguageTag, SensorName>>>>;
Thu Nguyen3c5486d2024-08-01 08:03:08 +000037using InventoryItemBoardIntf = sdbusplus::server::object_t<
38 sdbusplus::xyz::openbmc_project::Inventory::Item::server::Board>;
Gilbert Chende2a1322022-05-24 15:35:21 +010039
Thu Nguyenb8cf46b2024-06-15 02:44:35 +000040/** @struct EntityKey
41 *
42 * EntityKey uniquely identifies the PLDM entity and a combination of Entity
43 * Type, Entity Instance Number, Entity Container ID
44 *
45 */
46struct EntityKey
47{
48 EntityType type; //!< Entity type
49 EntityInstanceNumber instanceIdx; //!< Entity instance number
50 ContainerID containerId; //!< Entity container ID
51
52 bool operator==(const EntityKey& e) const
53 {
54 return ((type == e.type) && (instanceIdx == e.instanceIdx) &&
55 (containerId == e.containerId));
56 }
57};
58
59using AuxiliaryNames = std::vector<std::pair<NameLanguageTag, std::string>>;
60using EntityKey = struct EntityKey;
61using EntityAuxiliaryNames = std::tuple<EntityKey, AuxiliaryNames>;
62
Gilbert Chen6c7fed42022-02-22 15:40:17 +000063/**
64 * @brief Terminus
65 *
66 * Terminus class holds the TID, supported PLDM Type or PDRs which are needed by
67 * other manager class for sensor monitoring and control.
68 */
69class Terminus
70{
71 public:
72 Terminus(pldm_tid_t tid, uint64_t supportedPLDMTypes);
73
74 /** @brief Check if the terminus supports the PLDM type message
75 *
76 * @param[in] type - PLDM Type
77 * @return support state - True if support, otherwise False
78 */
79 bool doesSupportType(uint8_t type);
80
81 /** @brief Check if the terminus supports the PLDM command message
82 *
83 * @param[in] type - PLDM Type
84 * @param[in] command - PLDM command
85 * @return support state - True if support, otherwise False
86 */
87 bool doesSupportCommand(uint8_t type, uint8_t command);
88
89 /** @brief Set the supported PLDM commands for terminus
90 *
91 * @param[in] cmds - bit mask of the supported PLDM commands
92 * @return success state - True if success, otherwise False
93 */
94 bool setSupportedCommands(const std::vector<uint8_t>& cmds)
95 {
Patrick Williams16c2a0a2024-08-16 15:20:59 -040096 const size_t expectedSize =
97 PLDM_MAX_TYPES * (PLDM_MAX_CMDS_PER_TYPE / 8);
Gilbert Chen6c7fed42022-02-22 15:40:17 +000098 if (cmds.empty() || cmds.size() != expectedSize)
99 {
100 lg2::error(
101 "setSupportedCommands received invalid bit mask size. Expected: {EXPECTED}, Received: {RECEIVED}",
102 "EXPECTED", expectedSize, "RECEIVED", cmds.size());
103 return false;
104 }
105
106 /* Assign Vector supportedCmds by Vector cmds */
107 supportedCmds.resize(cmds.size());
108 std::copy(cmds.begin(), cmds.begin() + cmds.size(),
109 supportedCmds.begin());
110
111 return true;
112 }
Gilbert Chende2a1322022-05-24 15:35:21 +0100113
114 /** @brief Parse the PDRs stored in the member variable, pdrs.
115 */
116 void parseTerminusPDRs();
117
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000118 /** @brief The getter to return terminus's TID */
119 pldm_tid_t getTid()
120 {
121 return tid;
122 }
123
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000124 /** @brief The getter to get terminus's mctp medium */
Thu Nguyen9fc79122024-09-10 10:15:01 +0000125 std::optional<std::string_view> getTerminusName()
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000126 {
Thu Nguyen9fc79122024-09-10 10:15:01 +0000127 if (terminusName.empty())
128 {
129 return std::nullopt;
130 }
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000131 return terminusName;
132 }
133
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000134 /** @brief A list of PDRs fetched from Terminus */
135 std::vector<std::vector<uint8_t>> pdrs{};
136
137 /** @brief A flag to indicate if terminus has been initialized */
138 bool initialized = false;
139
Gilbert Chen77e6fe72024-08-06 09:23:30 +0000140 /** @brief maximum message buffer size the terminus can send and receive */
141 uint16_t maxBufferSize;
142
Thu Nguyen51d66b52024-08-06 09:15:55 +0000143 /** @brief This value indicates the event messaging styles supported by the
144 * terminus
145 */
146 bitfield8_t synchronyConfigurationSupported;
147
Thu Nguyen3c5486d2024-08-01 08:03:08 +0000148 /** @brief A list of numericSensors */
149 std::vector<std::shared_ptr<NumericSensor>> numericSensors{};
150
Gilbert Chende2a1322022-05-24 15:35:21 +0100151 /** @brief Get Sensor Auxiliary Names by sensorID
152 *
153 * @param[in] id - sensor ID
154 * @return sensor auxiliary names
155 */
156 std::shared_ptr<SensorAuxiliaryNames> getSensorAuxiliaryNames(SensorId id);
157
Gilbert Chen77e6fe72024-08-06 09:23:30 +0000158 /** @brief Get Numeric Sensor Object by sensorID
159 *
160 * @param[in] id - sensor ID
161 *
162 * @return sensor object
163 */
164 std::shared_ptr<NumericSensor> getSensorObject(SensorId id);
165
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000166 private:
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000167 /** @brief Find the Terminus Name from the Entity Auxiliary name list
168 * The Entity Auxiliary name list is entityAuxiliaryNamesTbl.
169 * @return terminus name in string option
170 */
171 std::optional<std::string_view> findTerminusName();
172
Thu Nguyen3c5486d2024-08-01 08:03:08 +0000173 /** @brief Construct the NumericSensor sensor class for the PLDM sensor.
174 * The NumericSensor class will handle create D-Bus object path,
175 * provide the APIs to update sensor value, threshold...
176 *
177 * @param[in] pdr - the numeric sensor PDR info
178 */
179 void addNumericSensor(
180 const std::shared_ptr<pldm_numeric_sensor_value_pdr> pdr);
181
Gilbert Chende2a1322022-05-24 15:35:21 +0100182 /** @brief Parse the numeric sensor PDRs
183 *
184 * @param[in] pdrData - the response PDRs from GetPDR command
185 * @return pointer to numeric sensor info struct
186 */
187 std::shared_ptr<pldm_numeric_sensor_value_pdr>
188 parseNumericSensorPDR(const std::vector<uint8_t>& pdrData);
189
190 /** @brief Parse the sensor Auxiliary name PDRs
191 *
192 * @param[in] pdrData - the response PDRs from GetPDR command
193 * @return pointer to sensor Auxiliary name info struct
194 */
195 std::shared_ptr<SensorAuxiliaryNames>
196 parseSensorAuxiliaryNamesPDR(const std::vector<uint8_t>& pdrData);
197
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000198 /** @brief Parse the Entity Auxiliary name PDRs
199 *
200 * @param[in] pdrData - the response PDRs from GetPDR command
201 * @return pointer to Entity Auxiliary name info struct
202 */
203 std::shared_ptr<EntityAuxiliaryNames>
204 parseEntityAuxiliaryNamesPDR(const std::vector<uint8_t>& pdrData);
205
Thu Nguyen3c5486d2024-08-01 08:03:08 +0000206 /** @brief Construct the NumericSensor sensor class for the compact numeric
207 * PLDM sensor.
208 *
209 * @param[in] pdr - the compact numeric sensor PDR info
210 */
211 void addCompactNumericSensor(
212 const std::shared_ptr<pldm_compact_numeric_sensor_pdr> pdr);
213
Gilbert Chende2a1322022-05-24 15:35:21 +0100214 /** @brief Parse the compact numeric sensor PDRs
215 *
216 * @param[in] pdrData - the response PDRs from GetPDR command
217 * @return pointer to compact numeric sensor info struct
218 */
219 std::shared_ptr<pldm_compact_numeric_sensor_pdr>
220 parseCompactNumericSensorPDR(const std::vector<uint8_t>& pdrData);
221
222 /** @brief Parse the sensor Auxiliary name from compact numeric sensor PDRs
223 *
224 * @param[in] pdrData - the response PDRs from GetPDR command
225 * @return pointer to sensor Auxiliary name info struct
226 */
227 std::shared_ptr<SensorAuxiliaryNames>
228 parseCompactNumericSensorNames(const std::vector<uint8_t>& pdrData);
229
Thu Nguyen3c5486d2024-08-01 08:03:08 +0000230 /** @brief Create the terminus inventory path to
231 * /xyz/openbmc_project/inventory/Item/Board/.
232 *
233 * @param[in] tName - the terminus name
234 * @return true/false: True if there is no error in creating inventory path
235 *
236 */
237 bool createInventoryPath(std::string tName);
238
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000239 /* @brief The terminus's TID */
240 pldm_tid_t tid;
241
242 /* @brief The supported PLDM command types of the terminus */
243 std::bitset<64> supportedTypes;
244
245 /** @brief Store supported PLDM commands of a terminus
246 * Maximum number of PLDM Type is PLDM_MAX_TYPES
247 * Maximum number of PLDM command for each type is
248 * PLDM_MAX_CMDS_PER_TYPE.
249 * Each uint8_t can store the supported state of 8 PLDM commands.
250 * Size of supportedCmds will be
251 * PLDM_MAX_TYPES * (PLDM_MAX_CMDS_PER_TYPE / 8).
252 */
253 std::vector<uint8_t> supportedCmds;
Gilbert Chende2a1322022-05-24 15:35:21 +0100254
255 /* @brief Sensor Auxiliary Name list */
256 std::vector<std::shared_ptr<SensorAuxiliaryNames>>
257 sensorAuxiliaryNamesTbl{};
Thu Nguyenb8cf46b2024-06-15 02:44:35 +0000258
259 /* @brief Entity Auxiliary Name list */
260 std::vector<std::shared_ptr<EntityAuxiliaryNames>>
261 entityAuxiliaryNamesTbl{};
262
263 /** @brief Terminus name */
264 EntityName terminusName{};
Thu Nguyen3c5486d2024-08-01 08:03:08 +0000265 /* @brief The pointer of iventory D-Bus interface for the terminus */
266 std::unique_ptr<InventoryItemBoardIntf> inventoryItemBoardInft = nullptr;
267
268 /* @brief Inventory D-Bus object path of the terminus */
269 std::string inventoryPath;
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000270};
271} // namespace platform_mc
272} // namespace pldm