blob: 92c709f42f8d0acd5731344833d7e092a529b171 [file] [log] [blame]
Thu Nguyen3c5486d2024-08-01 08:03:08 +00001#pragma once
2
3#include "libpldm/platform.h"
4#include "libpldm/pldm.h"
5
6#include "common/types.hpp"
Gilbert Chen77e6fe72024-08-06 09:23:30 +00007#include "common/utils.hpp"
Thu Nguyen3c5486d2024-08-01 08:03:08 +00008
9#include <sdbusplus/server/object.hpp>
10#include <xyz/openbmc_project/Association/Definitions/server.hpp>
11#include <xyz/openbmc_project/Sensor/Threshold/Critical/server.hpp>
12#include <xyz/openbmc_project/Sensor/Threshold/Warning/server.hpp>
13#include <xyz/openbmc_project/Sensor/Value/server.hpp>
14#include <xyz/openbmc_project/State/Decorator/Availability/server.hpp>
15#include <xyz/openbmc_project/State/Decorator/OperationalStatus/server.hpp>
16
17#include <string>
18
19namespace pldm
20{
21namespace platform_mc
22{
23
24using SensorUnit = sdbusplus::xyz::openbmc_project::Sensor::server::Value::Unit;
25using ValueIntf = sdbusplus::server::object_t<
26 sdbusplus::xyz::openbmc_project::Sensor::server::Value>;
27using ThresholdWarningIntf = sdbusplus::server::object_t<
28 sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Warning>;
29using ThresholdCriticalIntf = sdbusplus::server::object_t<
30 sdbusplus::xyz::openbmc_project::Sensor::Threshold::server::Critical>;
31using OperationalStatusIntf =
32 sdbusplus::server::object_t<sdbusplus::xyz::openbmc_project::State::
33 Decorator::server::OperationalStatus>;
34using AvailabilityIntf = sdbusplus::server::object_t<
35 sdbusplus::xyz::openbmc_project::State::Decorator::server::Availability>;
36using AssociationDefinitionsInft = sdbusplus::server::object_t<
37 sdbusplus::xyz::openbmc_project::Association::server::Definitions>;
38
39/**
40 * @brief NumericSensor
41 *
42 * This class handles sensor reading updated by sensor manager and export
43 * status to D-Bus interface.
44 */
45class NumericSensor
46{
47 public:
48 NumericSensor(const pldm_tid_t tid, const bool sensorDisabled,
49 std::shared_ptr<pldm_numeric_sensor_value_pdr> pdr,
50 std::string& sensorName, std::string& associationPath);
51
52 NumericSensor(const pldm_tid_t tid, const bool sensorDisabled,
53 std::shared_ptr<pldm_compact_numeric_sensor_pdr> pdr,
54 std::string& sensorName, std::string& associationPath);
55
Patrick Williams16c2a0a2024-08-16 15:20:59 -040056 ~NumericSensor() {};
Thu Nguyen3c5486d2024-08-01 08:03:08 +000057
Gilbert Cheneac61a42022-02-23 20:56:19 +000058 /** @brief The function called by Sensor Manager to set sensor to
59 * error status.
60 */
61 void handleErrGetSensorReading();
62
63 /** @brief Updating the sensor status to D-Bus interface
64 */
65 void updateReading(bool available, bool functional, double value = 0);
66
Thu Nguyen3c5486d2024-08-01 08:03:08 +000067 /** @brief ConversionFormula is used to convert raw value to the unit
68 * specified in PDR
69 *
70 * @param[in] value - raw value
71 * @return double - converted value
72 */
73 double conversionFormula(double value);
74
75 /** @brief UnitModifier is used to apply the unit modifier specified in PDR
76 *
77 * @param[in] value - raw value
78 * @return double - converted value
79 */
80 double unitModifier(double value);
81
Gilbert Cheneac61a42022-02-23 20:56:19 +000082 /** @brief Check if value is over threshold.
83 *
84 * @param[in] alarm - previous alarm state
85 * @param[in] direction - upper or lower threshold checking
86 * @param[in] value - raw value
87 * @param[in] threshold - threshold value
88 * @param[in] hyst - hysteresis value
89 * @return bool - new alarm state
90 */
91 bool checkThreshold(bool alarm, bool direction, double value,
92 double threshold, double hyst);
93
94 /** @brief Updating the association to D-Bus interface
95 * @param[in] inventoryPath - inventory path of the entity
96 */
97 inline void setInventoryPath(const std::string& inventoryPath)
98 {
99 if (associationDefinitionsIntf)
100 {
101 associationDefinitionsIntf->associations(
102 {{"chassis", "all_sensors", inventoryPath}});
103 }
104 }
105
106 /** @brief Get Upper Critical threshold
107 *
108 * @return double - Upper Critical threshold
109 */
110 double getThresholdUpperCritical()
111 {
112 if (thresholdCriticalIntf)
113 {
114 return thresholdCriticalIntf->criticalHigh();
115 }
116 else
117 {
118 return std::numeric_limits<double>::quiet_NaN();
119 }
120 };
121
122 /** @brief Get Lower Critical threshold
123 *
124 * @return double - Lower Critical threshold
125 */
126 double getThresholdLowerCritical()
127 {
128 if (thresholdCriticalIntf)
129 {
130 return thresholdCriticalIntf->criticalLow();
131 }
132 else
133 {
134 return std::numeric_limits<double>::quiet_NaN();
135 }
136 };
137
138 /** @brief Get Upper Warning threshold
139 *
140 * @return double - Upper Warning threshold
141 */
142 double getThresholdUpperWarning()
143 {
144 if (thresholdWarningIntf)
145 {
146 return thresholdWarningIntf->warningHigh();
147 }
148 else
149 {
150 return std::numeric_limits<double>::quiet_NaN();
151 }
152 };
153
154 /** @brief Get Lower Warning threshold
155 *
156 * @return double - Lower Warning threshold
157 */
158 double getThresholdLowerWarning()
159 {
160 if (thresholdWarningIntf)
161 {
162 return thresholdWarningIntf->warningLow();
163 }
164 else
165 {
166 return std::numeric_limits<double>::quiet_NaN();
167 }
168 };
169
Gilbert Chen77e6fe72024-08-06 09:23:30 +0000170 /** @brief Check if value is over threshold.
171 *
172 * @param[in] eventType - event level in pldm::utils::Level
173 * @param[in] direction - direction type in pldm::utils::Direction
174 * @param[in] rawValue - sensor raw value
175 * @param[in] newAlarm - trigger alarm true/false
176 * @param[in] assert - event type asserted/deasserted
177 *
178 * @return PLDM completion code
179 */
180 int triggerThresholdEvent(pldm::utils::Level eventType,
181 pldm::utils::Direction direction, double rawValue,
182 bool newAlarm, bool assert);
183
Thu Nguyen3c5486d2024-08-01 08:03:08 +0000184 /** @brief Terminus ID which the sensor belongs to */
185 pldm_tid_t tid;
186
187 /** @brief Sensor ID */
188 uint16_t sensorId;
189
Gilbert Cheneac61a42022-02-23 20:56:19 +0000190 /** @brief The time stamp since last getSensorReading command in usec */
191 uint64_t timeStamp;
192
Thu Nguyen3c5486d2024-08-01 08:03:08 +0000193 /** @brief The time of sensor update interval in usec */
194 uint64_t updateTime;
195
196 /** @brief sensorName */
197 std::string sensorName;
198
199 /** @brief sensorNameSpace */
200 std::string sensorNameSpace;
201
Thu Nguyen3c5486d2024-08-01 08:03:08 +0000202 private:
Gilbert Cheneac61a42022-02-23 20:56:19 +0000203 /**
204 * @brief Check sensor reading if any threshold has been crossed and update
205 * Threshold interfaces accordingly
206 */
207 void updateThresholds();
208
Thu Nguyen3c5486d2024-08-01 08:03:08 +0000209 std::unique_ptr<ValueIntf> valueIntf = nullptr;
210 std::unique_ptr<ThresholdWarningIntf> thresholdWarningIntf = nullptr;
211 std::unique_ptr<ThresholdCriticalIntf> thresholdCriticalIntf = nullptr;
212 std::unique_ptr<AvailabilityIntf> availabilityIntf = nullptr;
213 std::unique_ptr<OperationalStatusIntf> operationalStatusIntf = nullptr;
214 std::unique_ptr<AssociationDefinitionsInft> associationDefinitionsIntf =
215 nullptr;
216
217 /** @brief Amount of hysteresis associated with the sensor thresholds */
218 double hysteresis;
219
220 /** @brief The resolution of sensor in Units */
221 double resolution;
222
223 /** @brief A constant value that is added in as part of conversion process
224 * of converting a raw sensor reading to Units */
225 double offset;
226
227 /** @brief A power-of-10 multiplier for baseUnit */
228 int8_t baseUnitModifier;
229};
230} // namespace platform_mc
231} // namespace pldm