Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 3 | #include "hwmonio.hpp" |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 4 | #include "sensorset.hpp" |
| 5 | #include "types.hpp" |
| 6 | |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 7 | #include <gpioplus/handle.hpp> |
Patrick Venture | 2864b06 | 2018-12-19 08:13:41 -0800 | [diff] [blame] | 8 | #include <memory> |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 9 | #include <optional> |
| 10 | #include <stdplus/handle/managed.hpp> |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 11 | #include <unordered_set> |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 12 | |
| 13 | namespace sensor |
| 14 | { |
| 15 | |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 16 | struct valueAdjust |
| 17 | { |
| 18 | double gain = 1.0; |
| 19 | int offset = 0; |
| 20 | std::unordered_set<int> rmRCs; |
| 21 | }; |
| 22 | |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 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 | { |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 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; |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 38 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 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, |
Patrick Venture | 2864b06 | 2018-12-19 08:13:41 -0800 | [diff] [blame] | 47 | const hwmonio::HwmonIOInterface* ioAccess, |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 48 | const std::string& devPath); |
Matthew Barth | 2e41b13 | 2018-05-07 14:15:45 -0500 | [diff] [blame] | 49 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 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); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 58 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 59 | /** |
| 60 | * @brief Get the adjustments struct for the sensor |
| 61 | * |
| 62 | * @return - Sensor adjustment struct |
| 63 | */ |
Kun Yi | 15492e7 | 2019-07-15 22:04:34 -0700 | [diff] [blame] | 64 | inline const valueAdjust& getAdjusts() const |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 65 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 66 | return _sensorAdjusts; |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 67 | } |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 68 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 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 | */ |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 78 | SensorValueType adjustValue(SensorValueType value); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 79 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 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); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 94 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 95 | /** |
| 96 | * @brief Add status interface and functional property for sensor |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 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. |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 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); |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 107 | |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 108 | /** |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 109 | * @brief Get the scale from the sensor. |
| 110 | * |
| 111 | * @return - Scale value |
| 112 | */ |
Kun Yi | 15492e7 | 2019-07-15 22:04:34 -0700 | [diff] [blame] | 113 | inline int64_t getScale(void) const |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 114 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 115 | return _scale; |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Brandon Kim | db76d49 | 2019-06-17 11:53:04 -0700 | [diff] [blame] | 118 | /** |
| 119 | * @brief Get the GPIO handle from the sensor. |
| 120 | * |
| 121 | * @return - Pointer to the GPIO handle interface, can be nullptr. |
| 122 | */ |
Kun Yi | 15492e7 | 2019-07-15 22:04:34 -0700 | [diff] [blame] | 123 | inline const gpioplus::HandleInterface* getGpio(void) const |
Brandon Kim | db76d49 | 2019-06-17 11:53:04 -0700 | [diff] [blame] | 124 | { |
| 125 | return _handle.get(); |
| 126 | } |
| 127 | |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 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 | */ |
Kun Yi | 15492e7 | 2019-07-15 22:04:34 -0700 | [diff] [blame] | 133 | inline bool hasFaultFile(void) const |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 134 | { |
| 135 | return _hasFaultFile; |
| 136 | } |
| 137 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 138 | private: |
| 139 | /** @brief Sensor object's identifiers */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 140 | SensorSet::key_type _sensor; |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 141 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 142 | /** @brief Hwmon sysfs access. */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 143 | const hwmonio::HwmonIOInterface* _ioAccess; |
Matthew Barth | 2e41b13 | 2018-05-07 14:15:45 -0500 | [diff] [blame] | 144 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 145 | /** @brief Physical device sysfs path. */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 146 | const std::string& _devPath; |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 147 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 148 | /** @brief Structure for storing sensor adjustments */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 149 | valueAdjust _sensorAdjusts; |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 150 | |
| 151 | /** @brief Optional pointer to GPIO handle. */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 152 | std::unique_ptr<gpioplus::HandleInterface> _handle; |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 153 | |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 154 | /** @brief sensor scale from configuration. */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 155 | int64_t _scale; |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 156 | |
| 157 | /** @brief Tracks whether the sensor has a fault file or not. */ |
| 158 | bool _hasFaultFile; |
Matthew Barth | 2e41b13 | 2018-05-07 14:15:45 -0500 | [diff] [blame] | 159 | }; |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 160 | |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 161 | /** |
| 162 | * @brief Locks the gpio represented by the handle |
| 163 | * |
| 164 | * @param[in] handle - The gpio handle to lock |
Brandon Kim | db76d49 | 2019-06-17 11:53:04 -0700 | [diff] [blame] | 165 | */ |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 166 | void gpioLock(const gpioplus::HandleInterface*&& handle); |
Brandon Kim | db76d49 | 2019-06-17 11:53:04 -0700 | [diff] [blame] | 167 | |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 168 | /** @brief The type which is responsible for managing the lock */ |
| 169 | using GpioLocker = |
| 170 | stdplus::Managed<const gpioplus::HandleInterface*>::Handle<gpioLock>; |
Brandon Kim | db76d49 | 2019-06-17 11:53:04 -0700 | [diff] [blame] | 171 | |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 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); |
Brandon Kim | db76d49 | 2019-06-17 11:53:04 -0700 | [diff] [blame] | 179 | |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 180 | } // namespace sensor |