xref: /openbmc/pldm/platform-mc/terminus.hpp (revision b3b84b49)
1 #pragma once
2 
3 #include "libpldm/platform.h"
4 
5 #include "common/types.hpp"
6 #include "numeric_sensor.hpp"
7 #include "requester/handler.hpp"
8 #include "terminus.hpp"
9 
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>
16 #include <string>
17 #include <tuple>
18 #include <utility>
19 #include <vector>
20 
21 namespace pldm
22 {
23 namespace platform_mc
24 {
25 
26 using ContainerID = uint16_t;
27 using EntityInstanceNumber = uint16_t;
28 using EntityName = std::string;
29 using EntityType = uint16_t;
30 using SensorId = uint16_t;
31 using SensorCnt = uint8_t;
32 using NameLanguageTag = std::string;
33 using SensorName = std::string;
34 using SensorAuxiliaryNames = std::tuple<
35     SensorId, SensorCnt,
36     std::vector<std::vector<std::pair<NameLanguageTag, SensorName>>>>;
37 using InventoryItemBoardIntf = sdbusplus::server::object_t<
38     sdbusplus::xyz::openbmc_project::Inventory::Item::server::Board>;
39 
40 /** @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  */
46 struct 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 
59 using AuxiliaryNames = std::vector<std::pair<NameLanguageTag, std::string>>;
60 using EntityKey = struct EntityKey;
61 using EntityAuxiliaryNames = std::tuple<EntityKey, AuxiliaryNames>;
62 
63 /**
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  */
69 class 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     {
96         const size_t expectedSize =
97             PLDM_MAX_TYPES * (PLDM_MAX_CMDS_PER_TYPE / 8);
98         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     }
113 
114     /** @brief Parse the PDRs stored in the member variable, pdrs.
115      */
116     void parseTerminusPDRs();
117 
118     /** @brief The getter to return terminus's TID */
119     pldm_tid_t getTid()
120     {
121         return tid;
122     }
123 
124     /** @brief The getter to get terminus's mctp medium */
125     std::string_view getTerminusName()
126     {
127         return terminusName;
128     }
129 
130     /** @brief A list of PDRs fetched from Terminus */
131     std::vector<std::vector<uint8_t>> pdrs{};
132 
133     /** @brief A flag to indicate if terminus has been initialized */
134     bool initialized = false;
135 
136     /** @brief This value indicates the event messaging styles supported by the
137      *         terminus
138      */
139     bitfield8_t synchronyConfigurationSupported;
140 
141     /** @brief A list of numericSensors */
142     std::vector<std::shared_ptr<NumericSensor>> numericSensors{};
143 
144     /** @brief Get Sensor Auxiliary Names by sensorID
145      *
146      *  @param[in] id - sensor ID
147      *  @return sensor auxiliary names
148      */
149     std::shared_ptr<SensorAuxiliaryNames> getSensorAuxiliaryNames(SensorId id);
150 
151   private:
152     /** @brief Find the Terminus Name from the Entity Auxiliary name list
153      *         The Entity Auxiliary name list is entityAuxiliaryNamesTbl.
154      *  @return terminus name in string option
155      */
156     std::optional<std::string_view> findTerminusName();
157 
158     /** @brief Construct the NumericSensor sensor class for the PLDM sensor.
159      *         The NumericSensor class will handle create D-Bus object path,
160      *         provide the APIs to update sensor value, threshold...
161      *
162      *  @param[in] pdr - the numeric sensor PDR info
163      */
164     void addNumericSensor(
165         const std::shared_ptr<pldm_numeric_sensor_value_pdr> pdr);
166 
167     /** @brief Parse the numeric sensor PDRs
168      *
169      *  @param[in] pdrData - the response PDRs from GetPDR command
170      *  @return pointer to numeric sensor info struct
171      */
172     std::shared_ptr<pldm_numeric_sensor_value_pdr>
173         parseNumericSensorPDR(const std::vector<uint8_t>& pdrData);
174 
175     /** @brief Parse the sensor Auxiliary name PDRs
176      *
177      *  @param[in] pdrData - the response PDRs from GetPDR command
178      *  @return pointer to sensor Auxiliary name info struct
179      */
180     std::shared_ptr<SensorAuxiliaryNames>
181         parseSensorAuxiliaryNamesPDR(const std::vector<uint8_t>& pdrData);
182 
183     /** @brief Parse the Entity Auxiliary name PDRs
184      *
185      *  @param[in] pdrData - the response PDRs from GetPDR command
186      *  @return pointer to Entity Auxiliary name info struct
187      */
188     std::shared_ptr<EntityAuxiliaryNames>
189         parseEntityAuxiliaryNamesPDR(const std::vector<uint8_t>& pdrData);
190 
191     /** @brief Construct the NumericSensor sensor class for the compact numeric
192      *         PLDM sensor.
193      *
194      *  @param[in] pdr - the compact numeric sensor PDR info
195      */
196     void addCompactNumericSensor(
197         const std::shared_ptr<pldm_compact_numeric_sensor_pdr> pdr);
198 
199     /** @brief Parse the compact numeric sensor PDRs
200      *
201      *  @param[in] pdrData - the response PDRs from GetPDR command
202      *  @return pointer to compact numeric sensor info struct
203      */
204     std::shared_ptr<pldm_compact_numeric_sensor_pdr>
205         parseCompactNumericSensorPDR(const std::vector<uint8_t>& pdrData);
206 
207     /** @brief Parse the sensor Auxiliary name from compact numeric sensor PDRs
208      *
209      *  @param[in] pdrData - the response PDRs from GetPDR command
210      *  @return pointer to sensor Auxiliary name info struct
211      */
212     std::shared_ptr<SensorAuxiliaryNames>
213         parseCompactNumericSensorNames(const std::vector<uint8_t>& pdrData);
214 
215     /** @brief Create the terminus inventory path to
216      *         /xyz/openbmc_project/inventory/Item/Board/.
217      *
218      *  @param[in] tName - the terminus name
219      *  @return true/false: True if there is no error in creating inventory path
220      *
221      */
222     bool createInventoryPath(std::string tName);
223 
224     /* @brief The terminus's TID */
225     pldm_tid_t tid;
226 
227     /* @brief The supported PLDM command types of the terminus */
228     std::bitset<64> supportedTypes;
229 
230     /** @brief Store supported PLDM commands of a terminus
231      *         Maximum number of PLDM Type is PLDM_MAX_TYPES
232      *         Maximum number of PLDM command for each type is
233      *         PLDM_MAX_CMDS_PER_TYPE.
234      *         Each uint8_t can store the supported state of 8 PLDM commands.
235      *         Size of supportedCmds will be
236      *         PLDM_MAX_TYPES * (PLDM_MAX_CMDS_PER_TYPE / 8).
237      */
238     std::vector<uint8_t> supportedCmds;
239 
240     /* @brief Sensor Auxiliary Name list */
241     std::vector<std::shared_ptr<SensorAuxiliaryNames>>
242         sensorAuxiliaryNamesTbl{};
243 
244     /* @brief Entity Auxiliary Name list */
245     std::vector<std::shared_ptr<EntityAuxiliaryNames>>
246         entityAuxiliaryNamesTbl{};
247 
248     /** @brief Terminus name */
249     EntityName terminusName{};
250     /* @brief The pointer of iventory D-Bus interface for the terminus */
251     std::unique_ptr<InventoryItemBoardIntf> inventoryItemBoardInft = nullptr;
252 
253     /* @brief Inventory D-Bus object path of the terminus */
254     std::string inventoryPath;
255 };
256 } // namespace platform_mc
257 } // namespace pldm
258