blob: 3f38c332b053beb9a1889e12b76d74519d27c0bd [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 <chrono>
8#include <gpioplus/handle.hpp>
Patrick Venture043d3232018-08-31 10:10:53 -07009#include <unordered_set>
Matthew Barth35819382018-04-18 14:53:01 -050010
11namespace sensor
12{
13
Matthew Barthcb3daaf2018-05-07 15:03:16 -050014struct valueAdjust
15{
16 double gain = 1.0;
17 int offset = 0;
18 std::unordered_set<int> rmRCs;
19};
20
Matthew Barth9c431062018-05-07 13:55:29 -050021/** @class Sensor
22 * @brief Sensor object based on a SensorSet container's key type
23 * @details Sensor object to create and modify an associated device's sensor
24 * attributes based on the key type of each sensor in the set provided by the
25 * device.
26 */
27class Sensor
28{
Patrick Venture043d3232018-08-31 10:10:53 -070029 public:
30 Sensor() = delete;
31 Sensor(const Sensor&) = delete;
32 Sensor(Sensor&&) = default;
33 Sensor& operator=(const Sensor&) = delete;
34 Sensor& operator=(Sensor&&) = default;
35 ~Sensor() = default;
Matthew Barth9c431062018-05-07 13:55:29 -050036
Patrick Venture043d3232018-08-31 10:10:53 -070037 /**
38 * @brief Constructs Sensor object
39 *
40 * @param[in] sensor - A pair of sensor indentifiers
41 * @param[in] ioAccess - Hwmon sysfs access
42 * @param[in] devPath - Device sysfs path
43 */
44 explicit Sensor(const SensorSet::key_type& sensor,
45 const hwmonio::HwmonIO& ioAccess,
46 const std::string& devPath);
Matthew Barth2e41b132018-05-07 14:15:45 -050047
Patrick Venture043d3232018-08-31 10:10:53 -070048 /**
49 * @brief Adds any sensor removal return codes for the sensor
50 * @details Add all return codes defined within a device's config file
51 * for the entire device or for the specific sensor.
52 *
53 * @param[in] rcList - List of return codes found for the sensor
54 */
55 void addRemoveRCs(const std::string& rcList);
Matthew Barthcb3daaf2018-05-07 15:03:16 -050056
Patrick Venture043d3232018-08-31 10:10:53 -070057 /**
58 * @brief Get the adjustments struct for the sensor
59 *
60 * @return - Sensor adjustment struct
61 */
62 inline const valueAdjust& getAdjusts()
63 {
64 return sensorAdjusts;
65 }
Matthew Barthac473092018-05-07 14:41:46 -050066
Patrick Venture043d3232018-08-31 10:10:53 -070067 /**
68 * @brief Adjusts a sensor value
69 * @details Adjusts the value given by any gain and/or offset defined
70 * for this sensor object and returns that adjusted value.
71 *
72 * @param[in] value - Value to be adjusted
73 *
74 * @return - Adjusted sensor value
75 */
76 int64_t adjustValue(int64_t value);
Matthew Barthcb3daaf2018-05-07 15:03:16 -050077
Patrick Venture043d3232018-08-31 10:10:53 -070078 /**
79 * @brief Add value interface and value property for sensor
80 * @details When a sensor has an associated input file, the Sensor.Value
81 * interface is added along with setting the Value property to the
82 * corresponding value found in the input file.
83 *
84 * @param[in] retryIO - Hwmon sysfs file retry constraints
85 * (number of and delay between)
86 * @param[in] info - Sensor object information
87 *
88 * @return - Shared pointer to the value object
89 */
90 std::shared_ptr<ValueObject> addValue(const RetryIO& retryIO,
91 ObjectInfo& info);
Matthew Barthcb3daaf2018-05-07 15:03:16 -050092
Patrick Venture043d3232018-08-31 10:10:53 -070093 /**
94 * @brief Add status interface and functional property for sensor
95 * @details When a sensor has an associated fault file, the
96 * OperationalStatus interface is added along with setting the
97 * Functional property to the corresponding value found in the
98 * fault file.
99 *
100 * @param[in] info - Sensor object information
101 *
102 * @return - Shared pointer to the status object
103 */
104 std::shared_ptr<StatusObject> addStatus(ObjectInfo& info);
Matthew Barth9c431062018-05-07 13:55:29 -0500105
Patrick Ventureb28f4322018-09-14 10:19:14 -0700106 /**
107 * @brief Unlock the gpio, set to high if relevant.
108 */
109 void unlockGpio();
110
111 /**
112 * @brief Lock the gpio, set to low if relevant.
113 */
114 void lockGpio();
115
Patrick Venture043d3232018-08-31 10:10:53 -0700116 private:
117 /** @brief Sensor object's identifiers */
118 SensorSet::key_type sensor;
Matthew Barth9c431062018-05-07 13:55:29 -0500119
Patrick Venture043d3232018-08-31 10:10:53 -0700120 /** @brief Hwmon sysfs access. */
121 const hwmonio::HwmonIO& ioAccess;
Matthew Barth2e41b132018-05-07 14:15:45 -0500122
Patrick Venture043d3232018-08-31 10:10:53 -0700123 /** @brief Physical device sysfs path. */
124 const std::string& devPath;
Matthew Barthcb3daaf2018-05-07 15:03:16 -0500125
Patrick Venture043d3232018-08-31 10:10:53 -0700126 /** @brief Structure for storing sensor adjustments */
127 valueAdjust sensorAdjusts;
Patrick Ventureb28f4322018-09-14 10:19:14 -0700128
129 /** @brief Optional pointer to GPIO handle. */
130 std::unique_ptr<gpioplus::Handle> handle;
131
132 /** @brief default pause after unlocking gpio. */
133 static constexpr std::chrono::milliseconds pause{500};
Matthew Barth2e41b132018-05-07 14:15:45 -0500134};
Matthew Barth35819382018-04-18 14:53:01 -0500135
136} // namespace sensor