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 Williams | e8771fd | 2023-05-10 07:51:06 -0500 | [diff] [blame] | 7 | #include <gpioplus/handle.hpp> |
| 8 | #include <stdplus/handle/managed.hpp> |
| 9 | |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame] | 10 | #include <cerrno> |
| 11 | #include <future> |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame] | 12 | #include <map> |
Patrick Venture | 2864b06 | 2018-12-19 08:13:41 -0800 | [diff] [blame] | 13 | #include <memory> |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 14 | #include <optional> |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 15 | #include <unordered_set> |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 16 | |
| 17 | namespace sensor |
| 18 | { |
| 19 | |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame] | 20 | using TimedoutMap = std::map<SensorSet::key_type, std::future<int64_t>>; |
| 21 | |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 22 | struct valueAdjust |
| 23 | { |
| 24 | double gain = 1.0; |
| 25 | int offset = 0; |
| 26 | std::unordered_set<int> rmRCs; |
| 27 | }; |
| 28 | |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame] | 29 | /** @brief Custom exception for async sensor reading timeout |
| 30 | */ |
| 31 | struct AsyncSensorReadTimeOut : public std::system_error |
| 32 | { |
| 33 | AsyncSensorReadTimeOut() : |
| 34 | system_error(std::error_code(ETIMEDOUT, std::system_category()), |
| 35 | "Async sensor read timed out") |
Patrick Williams | e8771fd | 2023-05-10 07:51:06 -0500 | [diff] [blame] | 36 | {} |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 39 | /** @class Sensor |
| 40 | * @brief Sensor object based on a SensorSet container's key type |
| 41 | * @details Sensor object to create and modify an associated device's sensor |
| 42 | * attributes based on the key type of each sensor in the set provided by the |
| 43 | * device. |
| 44 | */ |
| 45 | class Sensor |
| 46 | { |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 47 | public: |
| 48 | Sensor() = delete; |
| 49 | Sensor(const Sensor&) = delete; |
| 50 | Sensor(Sensor&&) = default; |
| 51 | Sensor& operator=(const Sensor&) = delete; |
| 52 | Sensor& operator=(Sensor&&) = default; |
| 53 | ~Sensor() = default; |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 54 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 55 | /** |
| 56 | * @brief Constructs Sensor object |
| 57 | * |
Manojkiran Eda | 30cab62 | 2024-06-17 14:50:58 +0530 | [diff] [blame] | 58 | * @param[in] sensor - A pair of sensor identifiers |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 59 | * @param[in] ioAccess - Hwmon sysfs access |
| 60 | * @param[in] devPath - Device sysfs path |
| 61 | */ |
| 62 | explicit Sensor(const SensorSet::key_type& sensor, |
Patrick Venture | 2864b06 | 2018-12-19 08:13:41 -0800 | [diff] [blame] | 63 | const hwmonio::HwmonIOInterface* ioAccess, |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 64 | const std::string& devPath); |
Matthew Barth | 2e41b13 | 2018-05-07 14:15:45 -0500 | [diff] [blame] | 65 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 66 | /** |
| 67 | * @brief Adds any sensor removal return codes for the sensor |
| 68 | * @details Add all return codes defined within a device's config file |
| 69 | * for the entire device or for the specific sensor. |
| 70 | * |
| 71 | * @param[in] rcList - List of return codes found for the sensor |
| 72 | */ |
| 73 | void addRemoveRCs(const std::string& rcList); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 74 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 75 | /** |
| 76 | * @brief Get the adjustments struct for the sensor |
| 77 | * |
| 78 | * @return - Sensor adjustment struct |
| 79 | */ |
Kun Yi | 15492e7 | 2019-07-15 22:04:34 -0700 | [diff] [blame] | 80 | inline const valueAdjust& getAdjusts() const |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 81 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 82 | return _sensorAdjusts; |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 83 | } |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 84 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 85 | /** |
| 86 | * @brief Adjusts a sensor value |
| 87 | * @details Adjusts the value given by any gain and/or offset defined |
| 88 | * for this sensor object and returns that adjusted value. |
| 89 | * |
| 90 | * @param[in] value - Value to be adjusted |
| 91 | * |
| 92 | * @return - Adjusted sensor value |
| 93 | */ |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 94 | SensorValueType adjustValue(SensorValueType value); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 95 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 96 | /** |
| 97 | * @brief Add value interface and value property for sensor |
| 98 | * @details When a sensor has an associated input file, the Sensor.Value |
| 99 | * interface is added along with setting the Value property to the |
| 100 | * corresponding value found in the input file. |
| 101 | * |
| 102 | * @param[in] retryIO - Hwmon sysfs file retry constraints |
| 103 | * (number of and delay between) |
| 104 | * @param[in] info - Sensor object information |
| 105 | * |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame] | 106 | * @param[in] timedoutMap - Map to track timed out threads |
| 107 | * |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 108 | * @return - Shared pointer to the value object |
| 109 | */ |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 110 | std::shared_ptr<ValueObject> addValue( |
| 111 | const RetryIO& retryIO, ObjectInfo& info, TimedoutMap& timedoutMap); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 112 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 113 | /** |
| 114 | * @brief Add status interface and functional property for sensor |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 115 | * @details OperationalStatus interface is added and the Functional property |
| 116 | * is set depending on whether a fault file exists and if it does it will |
| 117 | * also depend on the content of the fault file. _hasFaultFile will also be |
| 118 | * set to true if fault file exists. |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 119 | * |
| 120 | * @param[in] info - Sensor object information |
| 121 | * |
| 122 | * @return - Shared pointer to the status object |
| 123 | */ |
| 124 | std::shared_ptr<StatusObject> addStatus(ObjectInfo& info); |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 125 | |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 126 | /** |
George Liu | c9d6161 | 2022-10-12 14:31:39 +0800 | [diff] [blame] | 127 | * @brief Add Accuracy interface and accuracy property for sensor |
| 128 | * @details Accuracy interface is the accuracy range (+/-) of the sensor |
| 129 | * Value as a percentage, with a value between 0 and 100. |
| 130 | * |
| 131 | * @param[in] info - Sensor object information |
| 132 | * @param[in] accuracy - The accuracy value for sensor readings |
| 133 | * |
| 134 | * @return - Shared pointer to the accuracy object |
| 135 | */ |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 136 | std::shared_ptr<AccuracyObject> |
| 137 | addAccuracy(ObjectInfo& info, double accuracy); |
George Liu | c9d6161 | 2022-10-12 14:31:39 +0800 | [diff] [blame] | 138 | |
| 139 | /** |
Lakshmi Yadlapati | 47fb49a | 2023-10-19 14:47:08 -0500 | [diff] [blame] | 140 | * @brief Add Priority interface and priority property for sensors |
| 141 | * @details The Priority interface defines priority levels for sensors. |
| 142 | * |
| 143 | * @param[in] info - Sensor object information |
| 144 | * @param[in] priority - The priority level for the sensor |
| 145 | * |
| 146 | * @return - Shared pointer to the priority object |
| 147 | */ |
| 148 | |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 149 | std::shared_ptr<PriorityObject> |
| 150 | addPriority(ObjectInfo& info, size_t priority); |
Lakshmi Yadlapati | 47fb49a | 2023-10-19 14:47:08 -0500 | [diff] [blame] | 151 | |
| 152 | /** |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 153 | * @brief Get the scale from the sensor. |
| 154 | * |
| 155 | * @return - Scale value |
| 156 | */ |
Kun Yi | 15492e7 | 2019-07-15 22:04:34 -0700 | [diff] [blame] | 157 | inline int64_t getScale(void) const |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 158 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 159 | return _scale; |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Brandon Kim | db76d49 | 2019-06-17 11:53:04 -0700 | [diff] [blame] | 162 | /** |
| 163 | * @brief Get the GPIO handle from the sensor. |
| 164 | * |
| 165 | * @return - Pointer to the GPIO handle interface, can be nullptr. |
| 166 | */ |
Kun Yi | 15492e7 | 2019-07-15 22:04:34 -0700 | [diff] [blame] | 167 | inline const gpioplus::HandleInterface* getGpio(void) const |
Brandon Kim | db76d49 | 2019-06-17 11:53:04 -0700 | [diff] [blame] | 168 | { |
| 169 | return _handle.get(); |
| 170 | } |
| 171 | |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 172 | /** |
| 173 | * @brief Get whether the sensor has a fault file or not. |
| 174 | * |
| 175 | * @return - Boolean on whether the sensor has a fault file |
| 176 | */ |
Kun Yi | 15492e7 | 2019-07-15 22:04:34 -0700 | [diff] [blame] | 177 | inline bool hasFaultFile(void) const |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 178 | { |
| 179 | return _hasFaultFile; |
| 180 | } |
| 181 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 182 | private: |
| 183 | /** @brief Sensor object's identifiers */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 184 | SensorSet::key_type _sensor; |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 185 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 186 | /** @brief Hwmon sysfs access. */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 187 | const hwmonio::HwmonIOInterface* _ioAccess; |
Matthew Barth | 2e41b13 | 2018-05-07 14:15:45 -0500 | [diff] [blame] | 188 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 189 | /** @brief Physical device sysfs path. */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 190 | const std::string& _devPath; |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 191 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 192 | /** @brief Structure for storing sensor adjustments */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 193 | valueAdjust _sensorAdjusts; |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 194 | |
| 195 | /** @brief Optional pointer to GPIO handle. */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 196 | std::unique_ptr<gpioplus::HandleInterface> _handle; |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 197 | |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 198 | /** @brief sensor scale from configuration. */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 199 | int64_t _scale; |
Brandon Kim | 86dcac8 | 2019-06-18 17:48:51 -0700 | [diff] [blame] | 200 | |
| 201 | /** @brief Tracks whether the sensor has a fault file or not. */ |
| 202 | bool _hasFaultFile; |
Matthew Barth | 2e41b13 | 2018-05-07 14:15:45 -0500 | [diff] [blame] | 203 | }; |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 204 | |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 205 | /** |
| 206 | * @brief Locks the gpio represented by the handle |
| 207 | * |
| 208 | * @param[in] handle - The gpio handle to lock |
Brandon Kim | db76d49 | 2019-06-17 11:53:04 -0700 | [diff] [blame] | 209 | */ |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 210 | void gpioLock(const gpioplus::HandleInterface*&& handle); |
Brandon Kim | db76d49 | 2019-06-17 11:53:04 -0700 | [diff] [blame] | 211 | |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 212 | /** @brief The type which is responsible for managing the lock */ |
| 213 | using GpioLocker = |
| 214 | stdplus::Managed<const gpioplus::HandleInterface*>::Handle<gpioLock>; |
Brandon Kim | db76d49 | 2019-06-17 11:53:04 -0700 | [diff] [blame] | 215 | |
William A. Kennington III | 2227bd5 | 2019-06-19 11:32:22 -0700 | [diff] [blame] | 216 | /** |
| 217 | * @brief Unlocks the gpio and creates a lock object to ensure |
| 218 | * the gpio is locked again. |
| 219 | * |
| 220 | * @param[in] handle - The gpio handle to unlock and wrap |
| 221 | */ |
| 222 | std::optional<GpioLocker> gpioUnlock(const gpioplus::HandleInterface* handle); |
Brandon Kim | db76d49 | 2019-06-17 11:53:04 -0700 | [diff] [blame] | 223 | |
Brandon Kim | 6d50c3e | 2019-08-09 15:38:53 -0700 | [diff] [blame] | 224 | /** |
| 225 | * @brief Asynchronously read a sensor with timeout defined by |
| 226 | * ASYNC_READ_TIMEOUT environment variable |
| 227 | * |
| 228 | * @param[in] sensorSetKey - Sensor object's identifiers |
| 229 | * @param[in] ioAccess - Hwmon sysfs access |
| 230 | * @param[in] asyncTimeout - Async read timeout in milliseconds |
| 231 | * @param[in] timedoutMap - Map to track timed out threads |
| 232 | * |
| 233 | * (Params needed for HwmonIO::read) |
| 234 | * @param[in] type - The hwmon type (ex. temp). |
| 235 | * @param[in] id - The hwmon id (ex. 1). |
| 236 | * @param[in] sensor - The hwmon sensor (ex. input). |
| 237 | * @param[in] retries - The number of times to retry. |
| 238 | * @param[in] delay - The time to sleep between retry attempts. |
| 239 | * |
| 240 | * @return - SensorValueType read asynchronously, will throw if timed out |
| 241 | */ |
Patrick Williams | 02e598a | 2024-08-16 15:21:23 -0400 | [diff] [blame] | 242 | SensorValueType asyncRead( |
| 243 | const SensorSet::key_type& sensorSetKey, |
| 244 | const hwmonio::HwmonIOInterface* ioAccess, |
| 245 | std::chrono::milliseconds asyncTimeout, TimedoutMap& timedoutMap, |
| 246 | const std::string& type, const std::string& id, const std::string& sensor, |
| 247 | const size_t retries, const std::chrono::milliseconds delay); |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 248 | } // namespace sensor |