blob: 2829bea46f3115070b16c91a2f8069d28969948c [file] [log] [blame]
Brad Bishop5c589482017-06-14 22:32:20 -04001#pragma once
2
William A. Kennington III1cfc2f12018-10-19 17:29:46 -07003#include <sdeventplus/source/io.hpp>
4#include <optional>
Brad Bishop5c589482017-06-14 22:32:20 -04005#include "evdevpp/evdev.hpp"
Brad Bishop5c589482017-06-14 22:32:20 -04006#include "psensor.hpp"
7#include "utility.hpp"
8
9namespace phosphor
10{
11namespace fan
12{
13namespace presence
14{
15class RedundancyPolicy;
16
17/**
18 * @class Gpio
19 * @brief Gpio presence sensor implementation.
20 *
21 * The Gpio class uses a gpio wire to determine presence state.
22 */
23class Gpio : public PresenceSensor
24{
25 public:
26 /**
27 * @brief
28 *
29 * Cannot move or copy due to this ptr as context
30 * for sdevent callbacks.
31 */
32 Gpio() = delete;
33 Gpio(const Gpio&) = delete;
34 Gpio& operator=(const Gpio&) = delete;
35 Gpio(Gpio&&) = delete;
36 Gpio& operator=(Gpio&&) = delete;
37 ~Gpio() = default;
38
39 /**
40 * @brief Construct a gpio sensor.
41 *
42 * @param[in] physDevice - The physical gpio device path.
Brad Bishop2e9788d2017-07-28 22:24:03 -040043 * @param[in] device - The gpio-keys input device.
Brad Bishop5c589482017-06-14 22:32:20 -040044 * @param[in] physPin - The physical gpio pin number.
45 */
Brad Bishop2e9788d2017-07-28 22:24:03 -040046 Gpio(
47 const std::string& physDevice,
48 const std::string& device,
49 unsigned int physPin);
Brad Bishop5c589482017-06-14 22:32:20 -040050
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;
60
61 /**
62 * @brief stop
63 *
64 * De-register sdevent io callback.
65 */
66 void stop() override;
67
68 /**
69 * @brief fail
70 *
71 * Call the gpio out.
72 */
73 void fail() override;
74
75 /**
76 * @brief Check the sensor.
77 *
78 * Query the gpio.
79 */
80 bool present() override;
81
82 private :
83 /** @brief Get the policy associated with this sensor. */
84 virtual RedundancyPolicy& getPolicy() = 0;
85
86 /** @brief sdevent io callback. */
William A. Kennington III1cfc2f12018-10-19 17:29:46 -070087 void ioCallback();
Brad Bishop5c589482017-06-14 22:32:20 -040088
89 /** The current state of the sensor. */
90 bool currentState;
91
92 /** Gpio event device file descriptor. */
93 util::FileDescriptor evdevfd;
94
95 /** Gpio event device. */
96 evdevpp::evdev::EvDev evdev;
97
98 /** Physical gpio device. */
99 std::string phys;
100
101 /** Gpio pin number. */
102 unsigned int pin;
103
William A. Kennington III1cfc2f12018-10-19 17:29:46 -0700104 /** sdevent io handle. */
105 std::optional<sdeventplus::source::IO> source;
Brad Bishop5c589482017-06-14 22:32:20 -0400106};
107
108} // namespace presence
109} // namespace fan
110} // namespace phosphor