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