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 <chrono> |
| 8 | #include <gpioplus/handle.hpp> |
Patrick Venture | 2864b06 | 2018-12-19 08:13:41 -0800 | [diff] [blame] | 9 | #include <memory> |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 10 | #include <unordered_set> |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 11 | |
| 12 | namespace sensor |
| 13 | { |
| 14 | |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 15 | struct valueAdjust |
| 16 | { |
| 17 | double gain = 1.0; |
| 18 | int offset = 0; |
| 19 | std::unordered_set<int> rmRCs; |
| 20 | }; |
| 21 | |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 22 | /** @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 | */ |
| 28 | class Sensor |
| 29 | { |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 30 | 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 Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 37 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 38 | /** |
| 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 Venture | 2864b06 | 2018-12-19 08:13:41 -0800 | [diff] [blame] | 46 | const hwmonio::HwmonIOInterface* ioAccess, |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 47 | const std::string& devPath); |
Matthew Barth | 2e41b13 | 2018-05-07 14:15:45 -0500 | [diff] [blame] | 48 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 49 | /** |
| 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 Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 57 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 58 | /** |
| 59 | * @brief Get the adjustments struct for the sensor |
| 60 | * |
| 61 | * @return - Sensor adjustment struct |
| 62 | */ |
| 63 | inline const valueAdjust& getAdjusts() |
| 64 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 65 | return _sensorAdjusts; |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 66 | } |
Matthew Barth | ac47309 | 2018-05-07 14:41:46 -0500 | [diff] [blame] | 67 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 68 | /** |
| 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 Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 77 | SensorValueType adjustValue(SensorValueType value); |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 78 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 79 | /** |
| 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 Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 93 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 94 | /** |
| 95 | * @brief Add status interface and functional property for sensor |
| 96 | * @details When a sensor has an associated fault file, the |
| 97 | * OperationalStatus interface is added along with setting the |
| 98 | * Functional property to the corresponding value found in the |
| 99 | * fault file. |
| 100 | * |
| 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 Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 106 | |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 107 | /** |
| 108 | * @brief Unlock the gpio, set to high if relevant. |
| 109 | */ |
| 110 | void unlockGpio(); |
| 111 | |
| 112 | /** |
| 113 | * @brief Lock the gpio, set to low if relevant. |
| 114 | */ |
| 115 | void lockGpio(); |
| 116 | |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 117 | /** |
| 118 | * @brief Get the scale from the sensor. |
| 119 | * |
| 120 | * @return - Scale value |
| 121 | */ |
| 122 | inline int64_t getScale(void) |
| 123 | { |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 124 | return _scale; |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 127 | private: |
| 128 | /** @brief Sensor object's identifiers */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 129 | SensorSet::key_type _sensor; |
Matthew Barth | 9c43106 | 2018-05-07 13:55:29 -0500 | [diff] [blame] | 130 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 131 | /** @brief Hwmon sysfs access. */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 132 | const hwmonio::HwmonIOInterface* _ioAccess; |
Matthew Barth | 2e41b13 | 2018-05-07 14:15:45 -0500 | [diff] [blame] | 133 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 134 | /** @brief Physical device sysfs path. */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 135 | const std::string& _devPath; |
Matthew Barth | cb3daaf | 2018-05-07 15:03:16 -0500 | [diff] [blame] | 136 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 137 | /** @brief Structure for storing sensor adjustments */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 138 | valueAdjust _sensorAdjusts; |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 139 | |
| 140 | /** @brief Optional pointer to GPIO handle. */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 141 | std::unique_ptr<gpioplus::HandleInterface> _handle; |
Patrick Venture | b28f432 | 2018-09-14 10:19:14 -0700 | [diff] [blame] | 142 | |
| 143 | /** @brief default pause after unlocking gpio. */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 144 | static constexpr std::chrono::milliseconds _pause{500}; |
James Feist | ee73f5b | 2018-08-01 16:31:42 -0700 | [diff] [blame] | 145 | |
| 146 | /** @brief sensor scale from configuration. */ |
Patrick Venture | 12659aa | 2018-12-19 13:58:43 -0800 | [diff] [blame] | 147 | int64_t _scale; |
Matthew Barth | 2e41b13 | 2018-05-07 14:15:45 -0500 | [diff] [blame] | 148 | }; |
Matthew Barth | 3581938 | 2018-04-18 14:53:01 -0500 | [diff] [blame] | 149 | |
| 150 | } // namespace sensor |