Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 3 | #include "evdevpp/evdev.hpp" |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 4 | #include "psensor.hpp" |
| 5 | #include "utility.hpp" |
| 6 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 7 | #include <sdeventplus/source/io.hpp> |
| 8 | |
| 9 | #include <optional> |
| 10 | |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 11 | namespace phosphor |
| 12 | { |
| 13 | namespace fan |
| 14 | { |
| 15 | namespace presence |
| 16 | { |
| 17 | class RedundancyPolicy; |
| 18 | |
| 19 | /** |
| 20 | * @class Gpio |
| 21 | * @brief Gpio presence sensor implementation. |
| 22 | * |
| 23 | * The Gpio class uses a gpio wire to determine presence state. |
| 24 | */ |
| 25 | class Gpio : public PresenceSensor |
| 26 | { |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 27 | public: |
| 28 | /** |
| 29 | * @brief |
| 30 | * |
| 31 | * Cannot move or copy due to this ptr as context |
| 32 | * for sdevent callbacks. |
| 33 | */ |
| 34 | Gpio() = delete; |
| 35 | Gpio(const Gpio&) = delete; |
| 36 | Gpio& operator=(const Gpio&) = delete; |
| 37 | Gpio(Gpio&&) = delete; |
| 38 | Gpio& operator=(Gpio&&) = delete; |
| 39 | ~Gpio() = default; |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 40 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 41 | /** |
| 42 | * @brief Construct a gpio sensor. |
| 43 | * |
| 44 | * @param[in] physDevice - The physical gpio device path. |
| 45 | * @param[in] device - The gpio-keys input device. |
| 46 | * @param[in] physPin - The physical gpio pin number. |
| 47 | */ |
| 48 | Gpio(const std::string& physDevice, const std::string& device, |
| 49 | unsigned int physPin); |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 50 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 51 | /** |
| 52 | * @brief start |
| 53 | * |
| 54 | * Register for an sdevent io callback on the gpio. |
| 55 | * Query the initial state of the gpio. |
| 56 | * |
| 57 | * @return The current sensor state. |
| 58 | */ |
| 59 | bool start() override; |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 60 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 61 | /** |
| 62 | * @brief stop |
| 63 | * |
| 64 | * De-register sdevent io callback. |
| 65 | */ |
| 66 | void stop() override; |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 67 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 68 | /** |
| 69 | * @brief fail |
| 70 | * |
| 71 | * Call the gpio out. |
| 72 | */ |
| 73 | void fail() override; |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 74 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 75 | /** |
| 76 | * @brief Check the sensor. |
| 77 | * |
| 78 | * Query the gpio. |
| 79 | */ |
| 80 | bool present() override; |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 81 | |
Matt Spinler | c65d91d | 2021-04-21 13:09:49 -0500 | [diff] [blame] | 82 | /** |
| 83 | * @brief Called when this presence sensor doesn't agree with other ones. |
| 84 | * |
| 85 | * @param[in] fanInventoryPath - The fan inventory D-Bus object path. |
| 86 | */ |
| 87 | void logConflict(const std::string& fanInventoryPath) const override; |
| 88 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 89 | private: |
| 90 | /** @brief Get the policy associated with this sensor. */ |
| 91 | virtual RedundancyPolicy& getPolicy() = 0; |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 92 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 93 | /** @brief sdevent io callback. */ |
| 94 | void ioCallback(); |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 95 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 96 | /** The current state of the sensor. */ |
| 97 | bool currentState; |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 98 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 99 | /** Gpio event device file descriptor. */ |
| 100 | util::FileDescriptor evdevfd; |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 101 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 102 | /** Gpio event device. */ |
| 103 | evdevpp::evdev::EvDev evdev; |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 104 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 105 | /** Physical gpio device. */ |
| 106 | std::string phys; |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 107 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 108 | /** Gpio pin number. */ |
| 109 | unsigned int pin; |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 110 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 111 | /** sdevent io handle. */ |
| 112 | std::optional<sdeventplus::source::IO> source; |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 113 | }; |
| 114 | |
Mike Capps | a35a890 | 2021-06-10 10:20:14 -0400 | [diff] [blame] | 115 | /** |
| 116 | * @class NullGpio |
| 117 | * @brief a phony presence sensor implementation that always |
| 118 | * reports not-present. Used to keep fan-presence service |
| 119 | * running when hardware is offline. |
| 120 | * |
| 121 | */ |
| 122 | class NullGpio : public PresenceSensor |
| 123 | { |
| 124 | public: |
| 125 | NullGpio() = default; |
| 126 | |
| 127 | /** |
| 128 | * @brief start |
| 129 | * |
| 130 | * Required to conform to interface |
| 131 | * |
| 132 | * @return false [dummy implementation] |
| 133 | */ |
| 134 | bool start() override |
| 135 | { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @brief stop |
| 141 | * |
| 142 | * Required to conform to interface |
| 143 | */ |
| 144 | void stop() override |
| 145 | {} |
| 146 | |
| 147 | /** |
| 148 | * @brief Check the sensor. |
| 149 | * |
| 150 | * @return false [dummy implementation] |
| 151 | */ |
| 152 | bool present() override |
| 153 | { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @brief Called when this presence sensor doesn't agree with other ones. |
| 159 | * |
| 160 | * @param[in] fanInventoryPath - The fan inventory D-Bus object path. |
| 161 | */ |
Mike Capps | 808d7fe | 2022-06-13 10:12:16 -0400 | [diff] [blame] | 162 | void logConflict(const std::string& /*fanInventoryPath*/) const override |
Mike Capps | a35a890 | 2021-06-10 10:20:14 -0400 | [diff] [blame] | 163 | {} |
| 164 | |
| 165 | private: |
| 166 | /** |
| 167 | * @brief Required to conform to interface |
| 168 | * |
| 169 | */ |
| 170 | virtual RedundancyPolicy& getPolicy() = 0; |
| 171 | }; |
| 172 | |
Brad Bishop | 5c58948 | 2017-06-14 22:32:20 -0400 | [diff] [blame] | 173 | } // namespace presence |
| 174 | } // namespace fan |
| 175 | } // namespace phosphor |