1 #pragma once 2 3 #include "hwmonio.hpp" 4 #include "sensorset.hpp" 5 #include "types.hpp" 6 7 #include <gpioplus/handle.hpp> 8 #include <memory> 9 #include <optional> 10 #include <stdplus/handle/managed.hpp> 11 #include <unordered_set> 12 13 namespace sensor 14 { 15 16 struct valueAdjust 17 { 18 double gain = 1.0; 19 int offset = 0; 20 std::unordered_set<int> rmRCs; 21 }; 22 23 /** @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 */ 29 class Sensor 30 { 31 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; 38 39 /** 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, 47 const hwmonio::HwmonIOInterface* ioAccess, 48 const std::string& devPath); 49 50 /** 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); 58 59 /** 60 * @brief Get the adjustments struct for the sensor 61 * 62 * @return - Sensor adjustment struct 63 */ 64 inline const valueAdjust& getAdjusts() const 65 { 66 return _sensorAdjusts; 67 } 68 69 /** 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 */ 78 SensorValueType adjustValue(SensorValueType value); 79 80 /** 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); 94 95 /** 96 * @brief Add status interface and functional property for sensor 97 * @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. 101 * 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); 107 108 /** 109 * @brief Get the scale from the sensor. 110 * 111 * @return - Scale value 112 */ 113 inline int64_t getScale(void) const 114 { 115 return _scale; 116 } 117 118 /** 119 * @brief Get the GPIO handle from the sensor. 120 * 121 * @return - Pointer to the GPIO handle interface, can be nullptr. 122 */ 123 inline const gpioplus::HandleInterface* getGpio(void) const 124 { 125 return _handle.get(); 126 } 127 128 /** 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 */ 133 inline bool hasFaultFile(void) const 134 { 135 return _hasFaultFile; 136 } 137 138 private: 139 /** @brief Sensor object's identifiers */ 140 SensorSet::key_type _sensor; 141 142 /** @brief Hwmon sysfs access. */ 143 const hwmonio::HwmonIOInterface* _ioAccess; 144 145 /** @brief Physical device sysfs path. */ 146 const std::string& _devPath; 147 148 /** @brief Structure for storing sensor adjustments */ 149 valueAdjust _sensorAdjusts; 150 151 /** @brief Optional pointer to GPIO handle. */ 152 std::unique_ptr<gpioplus::HandleInterface> _handle; 153 154 /** @brief sensor scale from configuration. */ 155 int64_t _scale; 156 157 /** @brief Tracks whether the sensor has a fault file or not. */ 158 bool _hasFaultFile; 159 }; 160 161 /** 162 * @brief Locks the gpio represented by the handle 163 * 164 * @param[in] handle - The gpio handle to lock 165 */ 166 void gpioLock(const gpioplus::HandleInterface*&& handle); 167 168 /** @brief The type which is responsible for managing the lock */ 169 using GpioLocker = 170 stdplus::Managed<const gpioplus::HandleInterface*>::Handle<gpioLock>; 171 172 /** 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 */ 178 std::optional<GpioLocker> gpioUnlock(const gpioplus::HandleInterface* handle); 179 180 } // namespace sensor 181