xref: /openbmc/phosphor-hwmon/sensor.hpp (revision b28f432a)
1 #pragma once
2 
3 #include "hwmonio.hpp"
4 #include "sensorset.hpp"
5 #include "types.hpp"
6 
7 #include <chrono>
8 #include <gpioplus/handle.hpp>
9 #include <unordered_set>
10 
11 namespace sensor
12 {
13 
14 struct valueAdjust
15 {
16     double gain = 1.0;
17     int offset = 0;
18     std::unordered_set<int> rmRCs;
19 };
20 
21 /** @class Sensor
22  *  @brief Sensor object based on a SensorSet container's key type
23  *  @details Sensor object to create and modify an associated device's sensor
24  *  attributes based on the key type of each sensor in the set provided by the
25  *  device.
26  */
27 class Sensor
28 {
29   public:
30     Sensor() = delete;
31     Sensor(const Sensor&) = delete;
32     Sensor(Sensor&&) = default;
33     Sensor& operator=(const Sensor&) = delete;
34     Sensor& operator=(Sensor&&) = default;
35     ~Sensor() = default;
36 
37     /**
38      * @brief Constructs Sensor object
39      *
40      * @param[in] sensor - A pair of sensor indentifiers
41      * @param[in] ioAccess - Hwmon sysfs access
42      * @param[in] devPath - Device sysfs path
43      */
44     explicit Sensor(const SensorSet::key_type& sensor,
45                     const hwmonio::HwmonIO& ioAccess,
46                     const std::string& devPath);
47 
48     /**
49      * @brief Adds any sensor removal return codes for the sensor
50      * @details Add all return codes defined within a device's config file
51      * for the entire device or for the specific sensor.
52      *
53      * @param[in] rcList - List of return codes found for the sensor
54      */
55     void addRemoveRCs(const std::string& rcList);
56 
57     /**
58      * @brief Get the adjustments struct for the sensor
59      *
60      * @return - Sensor adjustment struct
61      */
62     inline const valueAdjust& getAdjusts()
63     {
64         return sensorAdjusts;
65     }
66 
67     /**
68      * @brief Adjusts a sensor value
69      * @details Adjusts the value given by any gain and/or offset defined
70      * for this sensor object and returns that adjusted value.
71      *
72      * @param[in] value - Value to be adjusted
73      *
74      * @return - Adjusted sensor value
75      */
76     int64_t adjustValue(int64_t value);
77 
78     /**
79      * @brief Add value interface and value property for sensor
80      * @details When a sensor has an associated input file, the Sensor.Value
81      * interface is added along with setting the Value property to the
82      * corresponding value found in the input file.
83      *
84      * @param[in] retryIO - Hwmon sysfs file retry constraints
85      *                      (number of and delay between)
86      * @param[in] info - Sensor object information
87      *
88      * @return - Shared pointer to the value object
89      */
90     std::shared_ptr<ValueObject> addValue(const RetryIO& retryIO,
91                                           ObjectInfo& info);
92 
93     /**
94      * @brief Add status interface and functional property for sensor
95      * @details When a sensor has an associated fault file, the
96      * OperationalStatus interface is added along with setting the
97      * Functional property to the corresponding value found in the
98      * fault file.
99      *
100      * @param[in] info - Sensor object information
101      *
102      * @return - Shared pointer to the status object
103      */
104     std::shared_ptr<StatusObject> addStatus(ObjectInfo& info);
105 
106     /**
107      * @brief Unlock the gpio, set to high if relevant.
108      */
109     void unlockGpio();
110 
111     /**
112      * @brief Lock the gpio, set to low if relevant.
113      */
114     void lockGpio();
115 
116   private:
117     /** @brief Sensor object's identifiers */
118     SensorSet::key_type sensor;
119 
120     /** @brief Hwmon sysfs access. */
121     const hwmonio::HwmonIO& ioAccess;
122 
123     /** @brief Physical device sysfs path. */
124     const std::string& devPath;
125 
126     /** @brief Structure for storing sensor adjustments */
127     valueAdjust sensorAdjusts;
128 
129     /** @brief Optional pointer to GPIO handle. */
130     std::unique_ptr<gpioplus::Handle> handle;
131 
132     /** @brief default pause after unlocking gpio. */
133     static constexpr std::chrono::milliseconds pause{500};
134 };
135 
136 } // namespace sensor
137