blob: 3ec3ddacd98d8ad86b33512604921ba128d620d2 [file] [log] [blame]
Matthew Barth35819382018-04-18 14:53:01 -05001#pragma once
2
Matthew Barth9c431062018-05-07 13:55:29 -05003#include "hwmonio.hpp"
Patrick Venture043d3232018-08-31 10:10:53 -07004#include "sensorset.hpp"
5#include "types.hpp"
6
Patrick Ventureb28f4322018-09-14 10:19:14 -07007#include <chrono>
8#include <gpioplus/handle.hpp>
Patrick Venture2864b062018-12-19 08:13:41 -08009#include <memory>
Patrick Venture043d3232018-08-31 10:10:53 -070010#include <unordered_set>
Matthew Barth35819382018-04-18 14:53:01 -050011
12namespace sensor
13{
14
Matthew Barthcb3daaf2018-05-07 15:03:16 -050015struct valueAdjust
16{
17 double gain = 1.0;
18 int offset = 0;
19 std::unordered_set<int> rmRCs;
20};
21
Matthew Barth9c431062018-05-07 13:55:29 -050022/** @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 */
28class Sensor
29{
Patrick Venture043d3232018-08-31 10:10:53 -070030 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;
Matthew Barth9c431062018-05-07 13:55:29 -050037
Patrick Venture043d3232018-08-31 10:10:53 -070038 /**
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,
Patrick Venture2864b062018-12-19 08:13:41 -080046 const hwmonio::HwmonIOInterface* ioAccess,
Patrick Venture043d3232018-08-31 10:10:53 -070047 const std::string& devPath);
Matthew Barth2e41b132018-05-07 14:15:45 -050048
Patrick Venture043d3232018-08-31 10:10:53 -070049 /**
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);
Matthew Barthcb3daaf2018-05-07 15:03:16 -050057
Patrick Venture043d3232018-08-31 10:10:53 -070058 /**
59 * @brief Get the adjustments struct for the sensor
60 *
61 * @return - Sensor adjustment struct
62 */
Kun Yi15492e72019-07-15 22:04:34 -070063 inline const valueAdjust& getAdjusts() const
Patrick Venture043d3232018-08-31 10:10:53 -070064 {
Patrick Venture12659aa2018-12-19 13:58:43 -080065 return _sensorAdjusts;
Patrick Venture043d3232018-08-31 10:10:53 -070066 }
Matthew Barthac473092018-05-07 14:41:46 -050067
Patrick Venture043d3232018-08-31 10:10:53 -070068 /**
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 */
James Feistee73f5b2018-08-01 16:31:42 -070077 SensorValueType adjustValue(SensorValueType value);
Matthew Barthcb3daaf2018-05-07 15:03:16 -050078
Patrick Venture043d3232018-08-31 10:10:53 -070079 /**
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);
Matthew Barthcb3daaf2018-05-07 15:03:16 -050093
Patrick Venture043d3232018-08-31 10:10:53 -070094 /**
95 * @brief Add status interface and functional property for sensor
Brandon Kim86dcac82019-06-18 17:48:51 -070096 * @details OperationalStatus interface is added and the Functional property
97 * is set depending on whether a fault file exists and if it does it will
98 * also depend on the content of the fault file. _hasFaultFile will also be
99 * set to true if fault file exists.
Patrick Venture043d3232018-08-31 10:10:53 -0700100 *
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);
Matthew Barth9c431062018-05-07 13:55:29 -0500106
Patrick Ventureb28f4322018-09-14 10:19:14 -0700107 /**
James Feistee73f5b2018-08-01 16:31:42 -0700108 * @brief Get the scale from the sensor.
109 *
110 * @return - Scale value
111 */
Kun Yi15492e72019-07-15 22:04:34 -0700112 inline int64_t getScale(void) const
James Feistee73f5b2018-08-01 16:31:42 -0700113 {
Patrick Venture12659aa2018-12-19 13:58:43 -0800114 return _scale;
James Feistee73f5b2018-08-01 16:31:42 -0700115 }
116
Brandon Kimdb76d492019-06-17 11:53:04 -0700117 /**
118 * @brief Get the GPIO handle from the sensor.
119 *
120 * @return - Pointer to the GPIO handle interface, can be nullptr.
121 */
Kun Yi15492e72019-07-15 22:04:34 -0700122 inline const gpioplus::HandleInterface* getGpio(void) const
Brandon Kimdb76d492019-06-17 11:53:04 -0700123 {
124 return _handle.get();
125 }
126
Brandon Kim86dcac82019-06-18 17:48:51 -0700127 /**
128 * @brief Get whether the sensor has a fault file or not.
129 *
130 * @return - Boolean on whether the sensor has a fault file
131 */
Kun Yi15492e72019-07-15 22:04:34 -0700132 inline bool hasFaultFile(void) const
Brandon Kim86dcac82019-06-18 17:48:51 -0700133 {
134 return _hasFaultFile;
135 }
136
Patrick Venture043d3232018-08-31 10:10:53 -0700137 private:
138 /** @brief Sensor object's identifiers */
Patrick Venture12659aa2018-12-19 13:58:43 -0800139 SensorSet::key_type _sensor;
Matthew Barth9c431062018-05-07 13:55:29 -0500140
Patrick Venture043d3232018-08-31 10:10:53 -0700141 /** @brief Hwmon sysfs access. */
Patrick Venture12659aa2018-12-19 13:58:43 -0800142 const hwmonio::HwmonIOInterface* _ioAccess;
Matthew Barth2e41b132018-05-07 14:15:45 -0500143
Patrick Venture043d3232018-08-31 10:10:53 -0700144 /** @brief Physical device sysfs path. */
Patrick Venture12659aa2018-12-19 13:58:43 -0800145 const std::string& _devPath;
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500146
Patrick Venture043d3232018-08-31 10:10:53 -0700147 /** @brief Structure for storing sensor adjustments */
Patrick Venture12659aa2018-12-19 13:58:43 -0800148 valueAdjust _sensorAdjusts;
Patrick Ventureb28f4322018-09-14 10:19:14 -0700149
150 /** @brief Optional pointer to GPIO handle. */
Patrick Venture12659aa2018-12-19 13:58:43 -0800151 std::unique_ptr<gpioplus::HandleInterface> _handle;
Patrick Ventureb28f4322018-09-14 10:19:14 -0700152
James Feistee73f5b2018-08-01 16:31:42 -0700153 /** @brief sensor scale from configuration. */
Patrick Venture12659aa2018-12-19 13:58:43 -0800154 int64_t _scale;
Brandon Kim86dcac82019-06-18 17:48:51 -0700155
156 /** @brief Tracks whether the sensor has a fault file or not. */
157 bool _hasFaultFile;
Matthew Barth2e41b132018-05-07 14:15:45 -0500158};
Matthew Barth35819382018-04-18 14:53:01 -0500159
Brandon Kimdb76d492019-06-17 11:53:04 -0700160/** @class GpioLock
161 * @brief RAII class for GPIO unlock and lock
162 * @details Create this object in the stack to unlock the GPIO. GPIO will
163 * be locked by the destructor when we go out of scope.
164 */
165class GpioLock
166{
167 public:
168 GpioLock() = delete;
169 GpioLock(const GpioLock&) = delete;
170 GpioLock(GpioLock&&) = default;
171 GpioLock& operator=(const GpioLock&) = delete;
172 GpioLock& operator=(GpioLock&&) = default;
173
174 /**
175 * @brief Constructs GpioLock RAII object. Unlocks the GPIO.
176 *
177 * @param[in] handle - Pointer to a GPIO handle interface, can be nullptr.
178 */
179 explicit GpioLock(const gpioplus::HandleInterface* handle);
180
181 /**
182 * @brief Destructs GpioLock RAII object. Locks the GPIO.
183 */
184 ~GpioLock();
185
186 /**
187 * @brief Unlock the gpio, set to high if relevant.
188 */
189 void unlockGpio();
190
191 /**
192 * @brief Lock the gpio, set to low if relevant.
193 */
194 void lockGpio();
195
196 private:
197 /** @brief Pointer to GPIO handle. */
198 const gpioplus::HandleInterface* _handle;
199
200 /** @brief default pause after unlocking gpio. */
201 static constexpr std::chrono::milliseconds _pause{500};
202};
203
Matthew Barth35819382018-04-18 14:53:01 -0500204} // namespace sensor