xref: /openbmc/phosphor-hwmon/sensor.hpp (revision db76d49c)
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 <memory>
10 #include <unordered_set>
11 
12 namespace sensor
13 {
14 
15 struct valueAdjust
16 {
17     double gain = 1.0;
18     int offset = 0;
19     std::unordered_set<int> rmRCs;
20 };
21 
22 /** @class Sensor
23  *  @brief Sensor object based on a SensorSet container's key type
24  *  @details Sensor object to create and modify an associated device's sensor
25  *  attributes based on the key type of each sensor in the set provided by the
26  *  device.
27  */
28 class Sensor
29 {
30   public:
31     Sensor() = delete;
32     Sensor(const Sensor&) = delete;
33     Sensor(Sensor&&) = default;
34     Sensor& operator=(const Sensor&) = delete;
35     Sensor& operator=(Sensor&&) = default;
36     ~Sensor() = default;
37 
38     /**
39      * @brief Constructs Sensor object
40      *
41      * @param[in] sensor - A pair of sensor indentifiers
42      * @param[in] ioAccess - Hwmon sysfs access
43      * @param[in] devPath - Device sysfs path
44      */
45     explicit Sensor(const SensorSet::key_type& sensor,
46                     const hwmonio::HwmonIOInterface* ioAccess,
47                     const std::string& devPath);
48 
49     /**
50      * @brief Adds any sensor removal return codes for the sensor
51      * @details Add all return codes defined within a device's config file
52      * for the entire device or for the specific sensor.
53      *
54      * @param[in] rcList - List of return codes found for the sensor
55      */
56     void addRemoveRCs(const std::string& rcList);
57 
58     /**
59      * @brief Get the adjustments struct for the sensor
60      *
61      * @return - Sensor adjustment struct
62      */
63     inline const valueAdjust& getAdjusts()
64     {
65         return _sensorAdjusts;
66     }
67 
68     /**
69      * @brief Adjusts a sensor value
70      * @details Adjusts the value given by any gain and/or offset defined
71      * for this sensor object and returns that adjusted value.
72      *
73      * @param[in] value - Value to be adjusted
74      *
75      * @return - Adjusted sensor value
76      */
77     SensorValueType adjustValue(SensorValueType value);
78 
79     /**
80      * @brief Add value interface and value property for sensor
81      * @details When a sensor has an associated input file, the Sensor.Value
82      * interface is added along with setting the Value property to the
83      * corresponding value found in the input file.
84      *
85      * @param[in] retryIO - Hwmon sysfs file retry constraints
86      *                      (number of and delay between)
87      * @param[in] info - Sensor object information
88      *
89      * @return - Shared pointer to the value object
90      */
91     std::shared_ptr<ValueObject> addValue(const RetryIO& retryIO,
92                                           ObjectInfo& info);
93 
94     /**
95      * @brief Add status interface and functional property for sensor
96      * @details When a sensor has an associated fault file, the
97      * OperationalStatus interface is added along with setting the
98      * Functional property to the corresponding value found in the
99      * fault file.
100      *
101      * @param[in] info - Sensor object information
102      *
103      * @return - Shared pointer to the status object
104      */
105     std::shared_ptr<StatusObject> addStatus(ObjectInfo& info);
106 
107     /**
108      * @brief Get the scale from the sensor.
109      *
110      * @return - Scale value
111      */
112     inline int64_t getScale(void)
113     {
114         return _scale;
115     }
116 
117     /**
118      * @brief Get the GPIO handle from the sensor.
119      *
120      * @return - Pointer to the GPIO handle interface, can be nullptr.
121      */
122     inline const gpioplus::HandleInterface* getGpio(void)
123     {
124         return _handle.get();
125     }
126 
127   private:
128     /** @brief Sensor object's identifiers */
129     SensorSet::key_type _sensor;
130 
131     /** @brief Hwmon sysfs access. */
132     const hwmonio::HwmonIOInterface* _ioAccess;
133 
134     /** @brief Physical device sysfs path. */
135     const std::string& _devPath;
136 
137     /** @brief Structure for storing sensor adjustments */
138     valueAdjust _sensorAdjusts;
139 
140     /** @brief Optional pointer to GPIO handle. */
141     std::unique_ptr<gpioplus::HandleInterface> _handle;
142 
143     /** @brief sensor scale from configuration. */
144     int64_t _scale;
145 };
146 
147 /** @class GpioLock
148  *  @brief RAII class for GPIO unlock and lock
149  *  @details Create this object in the stack to unlock the GPIO. GPIO will
150  *  be locked by the destructor when we go out of scope.
151  */
152 class GpioLock
153 {
154   public:
155     GpioLock() = delete;
156     GpioLock(const GpioLock&) = delete;
157     GpioLock(GpioLock&&) = default;
158     GpioLock& operator=(const GpioLock&) = delete;
159     GpioLock& operator=(GpioLock&&) = default;
160 
161     /**
162      * @brief Constructs GpioLock RAII object. Unlocks the GPIO.
163      *
164      * @param[in] handle - Pointer to a GPIO handle interface, can be nullptr.
165      */
166     explicit GpioLock(const gpioplus::HandleInterface* handle);
167 
168     /**
169      * @brief Destructs GpioLock RAII object. Locks the GPIO.
170      */
171     ~GpioLock();
172 
173     /**
174      * @brief Unlock the gpio, set to high if relevant.
175      */
176     void unlockGpio();
177 
178     /**
179      * @brief Lock the gpio, set to low if relevant.
180      */
181     void lockGpio();
182 
183   private:
184     /** @brief Pointer to GPIO handle. */
185     const gpioplus::HandleInterface* _handle;
186 
187     /** @brief default pause after unlocking gpio. */
188     static constexpr std::chrono::milliseconds _pause{500};
189 };
190 
191 } // namespace sensor
192