blob: 4b2d281cf0d9cfbc7c8015fbe67a5e87a4aa901f [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 <gpioplus/handle.hpp>
Patrick Venture2864b062018-12-19 08:13:41 -08008#include <memory>
William A. Kennington III2227bd52019-06-19 11:32:22 -07009#include <optional>
10#include <stdplus/handle/managed.hpp>
Patrick Venture043d3232018-08-31 10:10:53 -070011#include <unordered_set>
Matthew Barth35819382018-04-18 14:53:01 -050012
13namespace sensor
14{
15
Matthew Barthcb3daaf2018-05-07 15:03:16 -050016struct valueAdjust
17{
18 double gain = 1.0;
19 int offset = 0;
20 std::unordered_set<int> rmRCs;
21};
22
Matthew Barth9c431062018-05-07 13:55:29 -050023/** @class Sensor
24 * @brief Sensor object based on a SensorSet container's key type
25 * @details Sensor object to create and modify an associated device's sensor
26 * attributes based on the key type of each sensor in the set provided by the
27 * device.
28 */
29class Sensor
30{
Patrick Venture043d3232018-08-31 10:10:53 -070031 public:
32 Sensor() = delete;
33 Sensor(const Sensor&) = delete;
34 Sensor(Sensor&&) = default;
35 Sensor& operator=(const Sensor&) = delete;
36 Sensor& operator=(Sensor&&) = default;
37 ~Sensor() = default;
Matthew Barth9c431062018-05-07 13:55:29 -050038
Patrick Venture043d3232018-08-31 10:10:53 -070039 /**
40 * @brief Constructs Sensor object
41 *
42 * @param[in] sensor - A pair of sensor indentifiers
43 * @param[in] ioAccess - Hwmon sysfs access
44 * @param[in] devPath - Device sysfs path
45 */
46 explicit Sensor(const SensorSet::key_type& sensor,
Patrick Venture2864b062018-12-19 08:13:41 -080047 const hwmonio::HwmonIOInterface* ioAccess,
Patrick Venture043d3232018-08-31 10:10:53 -070048 const std::string& devPath);
Matthew Barth2e41b132018-05-07 14:15:45 -050049
Patrick Venture043d3232018-08-31 10:10:53 -070050 /**
51 * @brief Adds any sensor removal return codes for the sensor
52 * @details Add all return codes defined within a device's config file
53 * for the entire device or for the specific sensor.
54 *
55 * @param[in] rcList - List of return codes found for the sensor
56 */
57 void addRemoveRCs(const std::string& rcList);
Matthew Barthcb3daaf2018-05-07 15:03:16 -050058
Patrick Venture043d3232018-08-31 10:10:53 -070059 /**
60 * @brief Get the adjustments struct for the sensor
61 *
62 * @return - Sensor adjustment struct
63 */
Kun Yi15492e72019-07-15 22:04:34 -070064 inline const valueAdjust& getAdjusts() const
Patrick Venture043d3232018-08-31 10:10:53 -070065 {
Patrick Venture12659aa2018-12-19 13:58:43 -080066 return _sensorAdjusts;
Patrick Venture043d3232018-08-31 10:10:53 -070067 }
Matthew Barthac473092018-05-07 14:41:46 -050068
Patrick Venture043d3232018-08-31 10:10:53 -070069 /**
70 * @brief Adjusts a sensor value
71 * @details Adjusts the value given by any gain and/or offset defined
72 * for this sensor object and returns that adjusted value.
73 *
74 * @param[in] value - Value to be adjusted
75 *
76 * @return - Adjusted sensor value
77 */
James Feistee73f5b2018-08-01 16:31:42 -070078 SensorValueType adjustValue(SensorValueType value);
Matthew Barthcb3daaf2018-05-07 15:03:16 -050079
Patrick Venture043d3232018-08-31 10:10:53 -070080 /**
81 * @brief Add value interface and value property for sensor
82 * @details When a sensor has an associated input file, the Sensor.Value
83 * interface is added along with setting the Value property to the
84 * corresponding value found in the input file.
85 *
86 * @param[in] retryIO - Hwmon sysfs file retry constraints
87 * (number of and delay between)
88 * @param[in] info - Sensor object information
89 *
90 * @return - Shared pointer to the value object
91 */
92 std::shared_ptr<ValueObject> addValue(const RetryIO& retryIO,
93 ObjectInfo& info);
Matthew Barthcb3daaf2018-05-07 15:03:16 -050094
Patrick Venture043d3232018-08-31 10:10:53 -070095 /**
96 * @brief Add status interface and functional property for sensor
Brandon Kim86dcac82019-06-18 17:48:51 -070097 * @details OperationalStatus interface is added and the Functional property
98 * is set depending on whether a fault file exists and if it does it will
99 * also depend on the content of the fault file. _hasFaultFile will also be
100 * set to true if fault file exists.
Patrick Venture043d3232018-08-31 10:10:53 -0700101 *
102 * @param[in] info - Sensor object information
103 *
104 * @return - Shared pointer to the status object
105 */
106 std::shared_ptr<StatusObject> addStatus(ObjectInfo& info);
Matthew Barth9c431062018-05-07 13:55:29 -0500107
Patrick Ventureb28f4322018-09-14 10:19:14 -0700108 /**
James Feistee73f5b2018-08-01 16:31:42 -0700109 * @brief Get the scale from the sensor.
110 *
111 * @return - Scale value
112 */
Kun Yi15492e72019-07-15 22:04:34 -0700113 inline int64_t getScale(void) const
James Feistee73f5b2018-08-01 16:31:42 -0700114 {
Patrick Venture12659aa2018-12-19 13:58:43 -0800115 return _scale;
James Feistee73f5b2018-08-01 16:31:42 -0700116 }
117
Brandon Kimdb76d492019-06-17 11:53:04 -0700118 /**
119 * @brief Get the GPIO handle from the sensor.
120 *
121 * @return - Pointer to the GPIO handle interface, can be nullptr.
122 */
Kun Yi15492e72019-07-15 22:04:34 -0700123 inline const gpioplus::HandleInterface* getGpio(void) const
Brandon Kimdb76d492019-06-17 11:53:04 -0700124 {
125 return _handle.get();
126 }
127
Brandon Kim86dcac82019-06-18 17:48:51 -0700128 /**
129 * @brief Get whether the sensor has a fault file or not.
130 *
131 * @return - Boolean on whether the sensor has a fault file
132 */
Kun Yi15492e72019-07-15 22:04:34 -0700133 inline bool hasFaultFile(void) const
Brandon Kim86dcac82019-06-18 17:48:51 -0700134 {
135 return _hasFaultFile;
136 }
137
Patrick Venture043d3232018-08-31 10:10:53 -0700138 private:
139 /** @brief Sensor object's identifiers */
Patrick Venture12659aa2018-12-19 13:58:43 -0800140 SensorSet::key_type _sensor;
Matthew Barth9c431062018-05-07 13:55:29 -0500141
Patrick Venture043d3232018-08-31 10:10:53 -0700142 /** @brief Hwmon sysfs access. */
Patrick Venture12659aa2018-12-19 13:58:43 -0800143 const hwmonio::HwmonIOInterface* _ioAccess;
Matthew Barth2e41b132018-05-07 14:15:45 -0500144
Patrick Venture043d3232018-08-31 10:10:53 -0700145 /** @brief Physical device sysfs path. */
Patrick Venture12659aa2018-12-19 13:58:43 -0800146 const std::string& _devPath;
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500147
Patrick Venture043d3232018-08-31 10:10:53 -0700148 /** @brief Structure for storing sensor adjustments */
Patrick Venture12659aa2018-12-19 13:58:43 -0800149 valueAdjust _sensorAdjusts;
Patrick Ventureb28f4322018-09-14 10:19:14 -0700150
151 /** @brief Optional pointer to GPIO handle. */
Patrick Venture12659aa2018-12-19 13:58:43 -0800152 std::unique_ptr<gpioplus::HandleInterface> _handle;
Patrick Ventureb28f4322018-09-14 10:19:14 -0700153
James Feistee73f5b2018-08-01 16:31:42 -0700154 /** @brief sensor scale from configuration. */
Patrick Venture12659aa2018-12-19 13:58:43 -0800155 int64_t _scale;
Brandon Kim86dcac82019-06-18 17:48:51 -0700156
157 /** @brief Tracks whether the sensor has a fault file or not. */
158 bool _hasFaultFile;
Matthew Barth2e41b132018-05-07 14:15:45 -0500159};
Matthew Barth35819382018-04-18 14:53:01 -0500160
William A. Kennington III2227bd52019-06-19 11:32:22 -0700161/**
162 * @brief Locks the gpio represented by the handle
163 *
164 * @param[in] handle - The gpio handle to lock
Brandon Kimdb76d492019-06-17 11:53:04 -0700165 */
William A. Kennington III2227bd52019-06-19 11:32:22 -0700166void gpioLock(const gpioplus::HandleInterface*&& handle);
Brandon Kimdb76d492019-06-17 11:53:04 -0700167
William A. Kennington III2227bd52019-06-19 11:32:22 -0700168/** @brief The type which is responsible for managing the lock */
169using GpioLocker =
170 stdplus::Managed<const gpioplus::HandleInterface*>::Handle<gpioLock>;
Brandon Kimdb76d492019-06-17 11:53:04 -0700171
William A. Kennington III2227bd52019-06-19 11:32:22 -0700172/**
173 * @brief Unlocks the gpio and creates a lock object to ensure
174 * the gpio is locked again.
175 *
176 * @param[in] handle - The gpio handle to unlock and wrap
177 */
178std::optional<GpioLocker> gpioUnlock(const gpioplus::HandleInterface* handle);
Brandon Kimdb76d492019-06-17 11:53:04 -0700179
Matthew Barth35819382018-04-18 14:53:01 -0500180} // namespace sensor