1 #pragma once
2 
3 #include <memory>
4 #include "evdevpp/evdev.hpp"
5 #include "sdevent/io.hpp"
6 #include "sdevent/source.hpp"
7 #include "psensor.hpp"
8 #include "utility.hpp"
9 
10 namespace phosphor
11 {
12 namespace fan
13 {
14 namespace presence
15 {
16 class RedundancyPolicy;
17 
18 /**
19  * @class Gpio
20  * @brief Gpio presence sensor implementation.
21  *
22  * The Gpio class uses a gpio wire to determine presence state.
23  */
24 class Gpio : public PresenceSensor
25 {
26     public:
27         /**
28          * @brief
29          *
30          * Cannot move or copy due to this ptr as context
31          * for sdevent callbacks.
32          */
33         Gpio() = delete;
34         Gpio(const Gpio&) = delete;
35         Gpio& operator=(const Gpio&) = delete;
36         Gpio(Gpio&&) = delete;
37         Gpio& operator=(Gpio&&) = delete;
38         ~Gpio() = default;
39 
40         /**
41          * @brief Construct a gpio sensor.
42          *
43          * @param[in] physDevice - The physical gpio device path.
44          * @param[in] device - The gpio-keys input device.
45          * @param[in] physPin - The physical gpio pin number.
46          */
47         Gpio(
48                 const std::string& physDevice,
49                 const std::string& device,
50                 unsigned int physPin);
51 
52         /**
53          * @brief start
54          *
55          * Register for an sdevent io callback on the gpio.
56          * Query the initial state of the gpio.
57          *
58          * @return The current sensor state.
59          */
60         bool start() override;
61 
62         /**
63          * @brief stop
64          *
65          * De-register sdevent io callback.
66          */
67         void stop() override;
68 
69         /**
70          * @brief fail
71          *
72          * Call the gpio out.
73          */
74         void fail() override;
75 
76         /**
77          * @brief Check the sensor.
78          *
79          * Query the gpio.
80          */
81         bool present() override;
82 
83     private :
84          /** @brief Get the policy associated with this sensor. */
85         virtual RedundancyPolicy& getPolicy() = 0;
86 
87          /** @brief sdevent io callback. */
88         void ioCallback(sdevent::source::Source& source);
89 
90         /** The current state of the sensor. */
91         bool currentState;
92 
93         /** Gpio event device file descriptor. */
94         util::FileDescriptor evdevfd;
95 
96         /** Gpio event device. */
97         evdevpp::evdev::EvDev evdev;
98 
99         /** Physical gpio device. */
100         std::string phys;
101 
102         /** Gpio pin number. */
103         unsigned int pin;
104 
105         /** sdevent io callback handle. */
106         std::unique_ptr<sdevent::event::io::IO> callback;
107 };
108 
109 } // namespace presence
110 } // namespace fan
111 } // namespace phosphor
112